@@ -316,19 +316,22 @@ private Cipher(CipherSpi firstSpi, Service firstService,
316
316
317
317
private static final String SHA512TRUNCATED = "SHA512/2" ;
318
318
319
+ // Parse the specified cipher transformation for algorithm and the
320
+ // optional mode and padding. If the transformation contains only
321
+ // algorithm, then only algorithm is returned. Otherwise, the
322
+ // transformation must contain all 3 and they must be non-empty.
319
323
private static String [] tokenizeTransformation (String transformation )
320
324
throws NoSuchAlgorithmException {
321
325
if (transformation == null ) {
322
326
throw new NoSuchAlgorithmException ("No transformation given" );
323
327
}
324
328
/*
325
- * array containing the components of a cipher transformation:
329
+ * Components of a cipher transformation:
326
330
*
327
- * index 0: algorithm component (e.g., AES)
328
- * index 1: feedback component (e.g., CFB)
329
- * index 2: padding component (e.g., PKCS5Padding)
331
+ * 1) algorithm component (e.g., AES)
332
+ * 2) feedback component (e.g., CFB) - optional
333
+ * 3) padding component (e.g., PKCS5Padding) - optional
330
334
*/
331
- String [] parts = { "" , "" , "" };
332
335
333
336
// check if the transformation contains algorithms with "/" in their
334
337
// name which can cause the parsing logic to go wrong
@@ -337,27 +340,35 @@ private static String[] tokenizeTransformation(String transformation)
337
340
int startIdx = (sha512Idx == -1 ? 0 :
338
341
sha512Idx + SHA512TRUNCATED .length ());
339
342
int endIdx = transformation .indexOf ('/' , startIdx );
340
- if (endIdx == -1 ) {
341
- // algorithm
342
- parts [0 ] = transformation .trim ();
343
+
344
+ boolean algorithmOnly = (endIdx == -1 );
345
+ String algo = (algorithmOnly ? transformation .trim () :
346
+ transformation .substring (0 , endIdx ).trim ());
347
+ if (algo .isEmpty ()) {
348
+ throw new NoSuchAlgorithmException ("Invalid transformation: " +
349
+ "algorithm not specified-"
350
+ + transformation );
351
+ }
352
+ if (algorithmOnly ) { // done
353
+ return new String [] { algo };
343
354
} else {
344
- // algorithm/mode/padding
345
- parts [0 ] = transformation .substring (0 , endIdx ).trim ();
355
+ // continue parsing mode and padding
346
356
startIdx = endIdx +1 ;
347
357
endIdx = transformation .indexOf ('/' , startIdx );
348
358
if (endIdx == -1 ) {
349
359
throw new NoSuchAlgorithmException ("Invalid transformation"
350
360
+ " format:" + transformation );
351
361
}
352
- parts [ 1 ] = transformation .substring (startIdx , endIdx ).trim ();
353
- parts [ 2 ] = transformation .substring (endIdx +1 ).trim ();
354
- }
355
- if (parts [ 0 ] .isEmpty ()) {
356
- throw new NoSuchAlgorithmException ("Invalid transformation: " +
357
- "algorithm not specified -"
362
+ String mode = transformation .substring (startIdx , endIdx ).trim ();
363
+ String padding = transformation .substring (endIdx +1 ).trim ();
364
+ // ensure mode and padding are specified
365
+ if (mode . isEmpty () || padding .isEmpty ()) {
366
+ throw new NoSuchAlgorithmException ("Invalid transformation: " +
367
+ "missing mode and/or padding -"
358
368
+ transformation );
369
+ }
370
+ return new String [] { algo , mode , padding };
359
371
}
360
- return parts ;
361
372
}
362
373
363
374
// Provider attribute name for supported chaining mode
@@ -453,28 +464,17 @@ private static List<Transform> getTransforms(String transformation)
453
464
throws NoSuchAlgorithmException {
454
465
String [] parts = tokenizeTransformation (transformation );
455
466
456
- String alg = parts [0 ];
457
- String mode = (parts [1 ].length () == 0 ? null : parts [1 ]);
458
- String pad = (parts [2 ].length () == 0 ? null : parts [2 ]);
459
-
460
- if ((mode == null ) && (pad == null )) {
467
+ if (parts .length == 1 ) {
461
468
// Algorithm only
462
- Transform tr = new Transform (alg , "" , null , null );
463
- return Collections .singletonList (tr );
469
+ return List .of (new Transform (parts [0 ], "" , null , null ));
464
470
} else {
465
- // Algorithm w/ at least mode or padding or both
466
- List <Transform > list = new ArrayList <>(4 );
467
- if ((mode != null ) && (pad != null )) {
468
- list .add (new Transform (alg , "/" + mode + "/" + pad , null , null ));
469
- }
470
- if (mode != null ) {
471
- list .add (new Transform (alg , "/" + mode , null , pad ));
472
- }
473
- if (pad != null ) {
474
- list .add (new Transform (alg , "//" + pad , mode , null ));
475
- }
476
- list .add (new Transform (alg , "" , mode , pad ));
477
- return list ;
471
+ // Algorithm w/ both mode and padding
472
+ return List .of (
473
+ new Transform (parts [0 ], "/" + parts [1 ] + "/" + parts [2 ],
474
+ null , null ),
475
+ new Transform (parts [0 ], "/" + parts [1 ], null , parts [2 ]),
476
+ new Transform (parts [0 ], "//" + parts [2 ], parts [1 ], null ),
477
+ new Transform (parts [0 ], "" , parts [1 ], parts [2 ]));
478
478
}
479
479
}
480
480
0 commit comments