Skip to content

Commit ddc56d1

Browse files
seanjmullanslowhog
authored andcommittedApr 20, 2021
8259428: AlgorithmId.getEncodedParams() should return copy
Reviewed-by: weijun, valeriep
1 parent ae33d2a commit ddc56d1

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed
 

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected void decodeParams() throws IOException {
153153
}
154154

155155
// Decode (parse) the parameters
156-
algParams.init(encodedParams);
156+
algParams.init(encodedParams.clone());
157157
}
158158

159159
/**
@@ -312,17 +312,21 @@ public AlgorithmParameters getParameters() {
312312
* Returns the DER encoded parameter, which can then be
313313
* used to initialize java.security.AlgorithmParameters.
314314
*
315+
* Note that this* method should always return a new array as it is called
316+
* directly by the JDK implementation of X509Certificate.getSigAlgParams()
317+
* and X509CRL.getSigAlgParams().
318+
*
315319
* Note: for ecdsa-with-SHA2 plus hash algorithm (Ex: SHA-256), this method
316320
* returns null because {@link #getName()} has already returned the "full"
317321
* signature algorithm (Ex: SHA256withECDSA).
318322
*
319323
* @return DER encoded parameters, or null not present.
320324
*/
321325
public byte[] getEncodedParams() throws IOException {
322-
return (params == null ||
326+
return (encodedParams == null ||
323327
algid.toString().equals(KnownOIDs.SpecifiedSHA2withECDSA.value()))
324328
? null
325-
: encodedParams;
329+
: encodedParams.clone();
326330
}
327331

328332
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 8259428
27+
* @summary Verify X509Certificate.getSigAlgParams() returns new array each
28+
* time it is called
29+
* @modules java.base/sun.security.tools.keytool java.base/sun.security.x509
30+
*/
31+
32+
import java.security.cert.X509Certificate;
33+
import sun.security.tools.keytool.CertAndKeyGen;
34+
import sun.security.x509.X500Name;
35+
36+
public class GetSigAlgParams {
37+
38+
public static void main(String[] args) throws Exception {
39+
40+
CertAndKeyGen cakg = new CertAndKeyGen("RSASSA-PSS", "RSASSA-PSS");
41+
cakg.generate(1024);
42+
X509Certificate c = cakg.getSelfCertificate(new X500Name("CN=Me"), 100);
43+
if (c.getSigAlgParams() == c.getSigAlgParams()) {
44+
throw new Exception("Encoded params are the same byte array");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)
Please sign in to comment.