Skip to content

Commit 91b08b7

Browse files
seanjmullanslowhog
authored andcommittedApr 20, 2021
8261779: JCK test api/javax_crypto/EncryptedPrivateKeyInfo/Ctor4.html is failing with assertion error when assertions enabled
Reviewed-by: rhalade, pkoppula, mschoene, weijun
1 parent 2fcd920 commit 91b08b7

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed
 

‎src/java.base/share/classes/sun/security/x509/AlgorithmId.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public AlgorithmId(ObjectIdentifier oid, AlgorithmParameters algparams) {
118118
// initialized (which should not occur), or if it was
119119
// initialized with bogus parameters, which should have
120120
// been detected when init was called.
121-
assert false;
122121
}
123122
}
124123
}
@@ -180,7 +179,12 @@ public void derEncode (OutputStream out) throws IOException {
180179
bytes.putOID(algid);
181180
// Setup params from algParams since no DER encoding is given
182181
if (constructedFromDer == false) {
183-
if (encodedParams != null) {
182+
if (algParams != null) {
183+
if (encodedParams == null) {
184+
// call getEncoded again in case algParams were initialized
185+
// after being passed in to ctor.
186+
encodedParams = algParams.getEncoded();
187+
}
184188
params = new DerValue(encodedParams);
185189
} else {
186190
params = null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @bug 8261779
27+
* @summary Check that EncryptedPrivateKeyInfo.getEncoded() calls
28+
* AlgorithmParameters.getEncoded() when first called
29+
*/
30+
31+
import java.io.IOException;
32+
import java.security.AlgorithmParameters;
33+
import java.security.AlgorithmParametersSpi;
34+
import java.security.Provider;
35+
import java.security.spec.AlgorithmParameterSpec;
36+
import java.security.spec.ECGenParameterSpec;
37+
import java.security.spec.InvalidParameterSpecException;
38+
import java.util.Arrays;
39+
import javax.crypto.EncryptedPrivateKeyInfo;
40+
41+
public class GetEncoded {
42+
43+
public static void main(String[] argv) throws Exception {
44+
45+
AlgorithmParameters params =
46+
AlgorithmParameters.getInstance("EC", new MyProvider());
47+
EncryptedPrivateKeyInfo epki =
48+
new EncryptedPrivateKeyInfo(params, new byte[] {1, 2, 3, 4});
49+
try {
50+
epki.getEncoded();
51+
throw new Exception("Should have thrown IOException");
52+
} catch (IOException ioe) {
53+
// test passed, expected exception
54+
}
55+
56+
AlgorithmParameters ap1 = AlgorithmParameters.getInstance("EC");
57+
EncryptedPrivateKeyInfo epki1 =
58+
new EncryptedPrivateKeyInfo(ap1, new byte[] {1, 2, 3, 4});
59+
ap1.init(new ECGenParameterSpec("secp256r1"));
60+
61+
EncryptedPrivateKeyInfo epki2 =
62+
new EncryptedPrivateKeyInfo(epki1.getEncoded());
63+
64+
AlgorithmParameters ap2 = epki2.getAlgParameters();
65+
if (ap2 == null || !Arrays.equals(ap1.getEncoded(), ap2.getEncoded())) {
66+
throw new Exception("AlgorithmParameters are not equal");
67+
}
68+
}
69+
70+
public static class MyProvider extends Provider {
71+
72+
MyProvider() {
73+
super("MyProvider", "0.0", "My Provider");
74+
put("AlgorithmParameters.EC", UnsupportedParameters.class.getName());
75+
}
76+
}
77+
78+
public static class UnsupportedParameters extends AlgorithmParametersSpi {
79+
80+
protected void engineInit(AlgorithmParameterSpec paramSpec)
81+
throws InvalidParameterSpecException {
82+
throw new InvalidParameterSpecException("Not supported");
83+
}
84+
protected void engineInit(byte[] params) throws IOException {
85+
throw new IOException("Not supported");
86+
}
87+
protected void engineInit(byte[] params, String format) throws IOException {
88+
throw new IOException("Not supported");
89+
}
90+
protected <T extends AlgorithmParameterSpec> T engineGetParameterSpec(
91+
Class<T> paramSpec) throws InvalidParameterSpecException {
92+
throw new InvalidParameterSpecException("Not supported");
93+
}
94+
protected byte[] engineGetEncoded() throws IOException {
95+
throw new IOException("Not supported");
96+
}
97+
protected byte[] engineGetEncoded(String format) throws IOException {
98+
throw new IOException("Not supported");
99+
}
100+
protected String engineToString() {
101+
return null;
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)
Please sign in to comment.