Skip to content

Commit 3ff83ec

Browse files
author
Varada M
committed
8358159: Empty mode/padding in cipher transformations
Reviewed-by: amitkumar, valeriep
1 parent 7c9c8ba commit 3ff83ec

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

src/java.base/share/classes/javax/crypto/Cipher.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,19 +454,25 @@ private static List<Transform> getTransforms(String transformation)
454454
String[] parts = tokenizeTransformation(transformation);
455455

456456
String alg = parts[0];
457-
String mode = parts[1];
458-
String pad = parts[2];
457+
String mode = (parts[1].length() == 0 ? null : parts[1]);
458+
String pad = (parts[2].length() == 0 ? null : parts[2]);
459459

460-
if ((mode.length() == 0) && (pad.length() == 0)) {
460+
if ((mode == null) && (pad == null)) {
461461
// Algorithm only
462462
Transform tr = new Transform(alg, "", null, null);
463463
return Collections.singletonList(tr);
464464
} else {
465465
// Algorithm w/ at least mode or padding or both
466466
List<Transform> list = new ArrayList<>(4);
467-
list.add(new Transform(alg, "/" + mode + "/" + pad, null, null));
468-
list.add(new Transform(alg, "/" + mode, null, pad));
469-
list.add(new Transform(alg, "//" + pad, mode, null));
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+
}
470476
list.add(new Transform(alg, "", mode, pad));
471477
return list;
472478
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2025 IBM Corporation. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21+
* or visit www.oracle.com if you need additional information or have any
22+
* questions.
23+
*/
24+
25+
/*
26+
* @test
27+
* @bug 8358159
28+
* @summary test that the Cipher.getInstance() handles
29+
* transformations with empty mode and/or padding
30+
* @run main TestEmptyModePadding
31+
*/
32+
33+
34+
import java.security.*;
35+
import javax.crypto.*;
36+
37+
public class TestEmptyModePadding {
38+
39+
public static void main(String[] args) throws Exception {
40+
Provider provider = Security.getProvider(System.getProperty("test.provider.name", "SunJCE"));
41+
42+
test("AES", provider);
43+
test("AES/ECB/PKCS5Padding", provider);
44+
test("AES//PKCS5Padding", provider); // Empty mode
45+
test("AES/CBC/", provider); // Empty padding
46+
test("AES/ /NoPadding", provider); // Mode is a space
47+
test("AES/CBC/ ", provider); // Padding is a space
48+
test("AES/ / ", provider); // Both mode and padding are spaces
49+
test("AES//", provider); // Both mode and padding are missing
50+
51+
}
52+
53+
private static void test(String transformation, Provider provider) throws Exception {
54+
Cipher c = Cipher.getInstance(transformation, provider);
55+
}
56+
}

0 commit comments

Comments
 (0)