Skip to content

Commit 5c596e2

Browse files
author
Valerie Peng
committed
8360463: Ambiguity in Cipher.getInstance() specification between NoSuchAlgorithmException and NoSuchPaddingException
Reviewed-by: mullan
1 parent 32ab0db commit 5c596e2

File tree

2 files changed

+55
-44
lines changed

2 files changed

+55
-44
lines changed

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,12 @@ private static Transform getTransform(Service s,
515515
* transformation
516516
*
517517
* @throws NoSuchAlgorithmException if {@code transformation}
518-
* is {@code null}, empty, in an invalid format,
519-
* or if no provider supports a {@code CipherSpi}
520-
* implementation for the specified algorithm
518+
* is {@code null}, empty or in an invalid format;
519+
* or if a {@code CipherSpi} implementation is not found or
520+
* is found but does not support the mode
521521
*
522-
* @throws NoSuchPaddingException if {@code transformation}
523-
* contains a padding scheme that is not available
522+
* @throws NoSuchPaddingException if a {@code CipherSpi} implementation
523+
* is found but does not support the padding scheme
524524
*
525525
* @see java.security.Provider
526526
*/
@@ -573,17 +573,21 @@ public static final Cipher getInstance(String transformation)
573573
failure = e;
574574
}
575575
}
576+
if (failure instanceof NoSuchPaddingException nspe) {
577+
throw nspe;
578+
}
576579
throw new NoSuchAlgorithmException
577-
("Cannot find any provider supporting " + transformation, failure);
580+
("Cannot find any provider supporting " + transformation,
581+
failure);
578582
}
579583

580584
/**
581585
* Returns a {@code Cipher} object that implements the specified
582586
* transformation.
583587
*
584588
* <p> A new {@code Cipher} object encapsulating the
585-
* {@code CipherSpi} implementation from the specified provider
586-
* is returned. The specified provider must be registered
589+
* {@code CipherSpi} implementation from the specified {@code provider}
590+
* is returned. The specified {@code provider} must be registered
587591
* in the security provider list.
588592
*
589593
* <p> Note that the list of registered providers may be retrieved via
@@ -625,15 +629,16 @@ public static final Cipher getInstance(String transformation)
625629
* is {@code null} or empty
626630
*
627631
* @throws NoSuchAlgorithmException if {@code transformation}
628-
* is {@code null}, empty, in an invalid format,
629-
* or if a {@code CipherSpi} implementation for the
630-
* specified algorithm is not available from the specified
631-
* provider
632+
* is {@code null}, empty or in an invalid format;
633+
* or if a {@code CipherSpi} implementation from the specified
634+
* {@code provider} is not found or is found but does not support
635+
* the mode
632636
*
633-
* @throws NoSuchPaddingException if {@code transformation}
634-
* contains a padding scheme that is not available
637+
* @throws NoSuchPaddingException if a {@code CipherSpi} implementation
638+
* from the specified {@code provider} is found but does not
639+
* support the padding scheme
635640
*
636-
* @throws NoSuchProviderException if the specified provider is not
641+
* @throws NoSuchProviderException if the specified {@code provider} is not
637642
* registered in the security provider list
638643
*
639644
* @see java.security.Provider
@@ -706,13 +711,14 @@ private String getProviderName() {
706711
* is {@code null}
707712
*
708713
* @throws NoSuchAlgorithmException if {@code transformation}
709-
* is {@code null}, empty, in an invalid format,
710-
* or if a {@code CipherSpi} implementation for the
711-
* specified algorithm is not available from the specified
712-
* {@code provider} object
713-
*
714-
* @throws NoSuchPaddingException if {@code transformation}
715-
* contains a padding scheme that is not available
714+
* is {@code null}, empty or in an invalid format;
715+
* or if a {@code CipherSpi} implementation from the specified
716+
* {@code provider} is not found or is found but does not support
717+
* the mode
718+
*
719+
* @throws NoSuchPaddingException if a {@code CipherSpi} implementation
720+
* from the specified {@code provider} is found but does not
721+
* support the padding scheme
716722
*
717723
* @see java.security.Provider
718724
*/

test/jdk/com/sun/crypto/provider/Cipher/ChaCha20/unittest/ChaCha20CipherUnitTest.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8153029
26+
* @bug 8153029 8360463
2727
* @library /test/lib
2828
* @run main ChaCha20CipherUnitTest
2929
* @summary Unit test for com.sun.crypto.provider.ChaCha20Cipher.
@@ -38,6 +38,7 @@
3838
import java.util.HexFormat;
3939

4040
import javax.crypto.Cipher;
41+
import javax.crypto.NoSuchPaddingException;
4142
import javax.crypto.spec.ChaCha20ParameterSpec;
4243
import javax.crypto.spec.IvParameterSpec;
4344
import javax.crypto.spec.SecretKeySpec;
@@ -66,32 +67,36 @@ public static void main(String[] args) throws Exception {
6667
private static void testTransformations() throws Exception {
6768
System.out.println("== transformations ==");
6869

69-
checkTransformation("ChaCha20", true);
70-
checkTransformation("ChaCha20/None/NoPadding", true);
71-
checkTransformation("ChaCha20-Poly1305", true);
72-
checkTransformation("ChaCha20-Poly1305/None/NoPadding", true);
73-
74-
checkTransformation("ChaCha20/ECB/NoPadding", false);
75-
checkTransformation("ChaCha20/None/PKCS5Padding", false);
76-
checkTransformation("ChaCha20-Poly1305/ECB/NoPadding", false);
77-
checkTransformation("ChaCha20-Poly1305/None/PKCS5Padding", false);
70+
Class NSAE = NoSuchAlgorithmException.class;
71+
Class NSPE = NoSuchPaddingException.class;
72+
73+
checkTransformation("ChaCha20", null);
74+
checkTransformation("ChaCha20/None/NoPadding", null);
75+
checkTransformation("ChaCha20-Poly1305", null);
76+
checkTransformation("ChaCha20-Poly1305/None/NoPadding", null);
77+
checkTransformation("ChaCha20/ECB/NoPadding", NSAE);
78+
checkTransformation("ChaCha20/None/PKCS5Padding", NSPE);
79+
checkTransformation("ChaCha20-Poly1305/ECB/NoPadding", NSAE);
80+
checkTransformation("ChaCha20-Poly1305/None/PKCS5Padding", NSPE);
7881
}
7982

80-
private static void checkTransformation(String transformation,
81-
boolean expected) throws Exception {
83+
private static void checkTransformation(String transformation, Class exCls)
84+
throws Exception {
8285
try {
83-
Cipher.getInstance(transformation);
84-
if (!expected) {
85-
throw new RuntimeException(
86-
"Unexpected transformation: " + transformation);
86+
Cipher.getInstance(transformation,
87+
System.getProperty("test.provider.name", "SunJCE"));
88+
if (exCls != null) {
89+
throw new RuntimeException("Expected Exception not thrown: " +
90+
exCls);
8791
} else {
88-
System.out.println("Expected transformation: " + transformation);
92+
System.out.println(transformation + ": pass");
8993
}
90-
} catch (NoSuchAlgorithmException e) {
91-
if (!expected) {
92-
System.out.println("Unexpected transformation: " + transformation);
94+
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
95+
if (e.getClass() != exCls) {
96+
throw new RuntimeException("Unexpected Exception", e);
9397
} else {
94-
throw new RuntimeException("Unexpected fail: " + transformation, e);
98+
System.out.println(transformation + ": got expected " +
99+
exCls.getName());
95100
}
96101
}
97102
}

0 commit comments

Comments
 (0)