Skip to content

Commit 0e855fe

Browse files
Hai-May Chaowangweij
Hai-May Chao
authored andcommitted
8252377: Incorrect encoding for EC AlgorithmIdentifier
Reviewed-by: weijun
1 parent 9150b90 commit 0e855fe

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,20 @@ public void derEncode (OutputStream out) throws IOException {
207207
bytes.putNull();
208208
}*/
209209
if (algid.equals(RSASSA_PSS_oid) || algid.equals(ed448_oid)
210-
|| algid.equals(ed25519_oid)) {
210+
|| algid.equals(ed25519_oid)
211+
|| algid.equals(x448_oid)
212+
|| algid.equals(x25519_oid)
213+
|| algid.equals(SHA224withECDSA_oid)
214+
|| algid.equals(SHA256withECDSA_oid)
215+
|| algid.equals(SHA384withECDSA_oid)
216+
|| algid.equals(SHA512withECDSA_oid)) {
211217
// RFC 4055 3.3: when an RSASSA-PSS key does not require
212218
// parameter validation, field is absent.
219+
// RFC 8410 3: for id-X25519, id-X448, id-Ed25519, and
220+
// id-Ed448, the parameters must be absent.
221+
// RFC 5758 3.2: the encoding must omit the parameters field
222+
// for ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-SHA384
223+
// and ecdsa-with-SHA512.
213224
} else {
214225
bytes.putNull();
215226
}
@@ -644,6 +655,20 @@ private static ConcurrentHashMap<String, String> collectOIDAliases() {
644655
public static final ObjectIdentifier ed448_oid =
645656
ObjectIdentifier.of(KnownOIDs.Ed448);
646657

658+
public static final ObjectIdentifier x25519_oid =
659+
ObjectIdentifier.of(KnownOIDs.X25519);
660+
public static final ObjectIdentifier x448_oid =
661+
ObjectIdentifier.of(KnownOIDs.X448);
662+
663+
public static final ObjectIdentifier SHA224withECDSA_oid =
664+
ObjectIdentifier.of(KnownOIDs.SHA224withECDSA);
665+
public static final ObjectIdentifier SHA256withECDSA_oid =
666+
ObjectIdentifier.of(KnownOIDs.SHA256withECDSA);
667+
public static final ObjectIdentifier SHA384withECDSA_oid =
668+
ObjectIdentifier.of(KnownOIDs.SHA384withECDSA);
669+
public static final ObjectIdentifier SHA512withECDSA_oid =
670+
ObjectIdentifier.of(KnownOIDs.SHA512withECDSA);
671+
647672
/**
648673
* Creates a signature algorithm name from a digest algorithm
649674
* name and a encryption algorithm name.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2020, 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 8252377
27+
* @library /test/lib
28+
* @modules java.base/sun.security.util
29+
* java.base/sun.security.x509
30+
* @summary The AlgorithmIdentifier for ECDSA should omit the parameters field
31+
*/
32+
33+
import jdk.test.lib.Asserts;
34+
import jdk.test.lib.SecurityTools;
35+
import jdk.test.lib.process.OutputAnalyzer;
36+
import static jdk.test.lib.security.DerUtils.*;
37+
38+
import java.io.File;
39+
import java.security.KeyStore;
40+
import java.security.cert.X509Certificate;
41+
import sun.security.util.*;
42+
43+
public class OmitAlgIdParam {
44+
45+
public static void main(String[] args) throws Exception {
46+
keytool("-genkeypair -keyalg ec -dname CN=EC1 -alias ecsha224 "
47+
+ "-sigalg SHA224withECDSA -keystore ks -storepass changeit");
48+
49+
keytool("-genkeypair -keyalg ec -dname CN=EC2 -alias ecsha256 "
50+
+ "-sigalg SHA256withECDSA -keystore ks -storepass changeit");
51+
52+
keytool("-genkeypair -keyalg ec -dname CN=EC3 -alias ecsha384 "
53+
+ "-sigalg SHA384withECDSA -keystore ks -storepass changeit");
54+
55+
keytool("-genkeypair -keyalg ec -dname CN=EC4 -alias ecsha512 "
56+
+ "-sigalg SHA512withECDSA -keystore ks -storepass changeit");
57+
58+
KeyStore kstore = KeyStore.getInstance(
59+
new File("ks"), "changeit".toCharArray());
60+
61+
// SHA224withECDSA
62+
checkAlgId(kstore, "ecsha224", "SHA224withECDSA",
63+
ObjectIdentifier.of(KnownOIDs.SHA224withECDSA));
64+
65+
// SHA256withECDSA
66+
checkAlgId(kstore, "ecsha256", "SHA256withECDSA",
67+
ObjectIdentifier.of(KnownOIDs.SHA256withECDSA));
68+
69+
// SHA384withECDSA
70+
checkAlgId(kstore, "ecsha384", "SHA384withECDSA",
71+
ObjectIdentifier.of(KnownOIDs.SHA384withECDSA));
72+
73+
// SHA512withECDSA
74+
checkAlgId(kstore, "ecsha512", "SHA512withECDSA",
75+
ObjectIdentifier.of(KnownOIDs.SHA512withECDSA));
76+
}
77+
78+
private static void checkAlgId(KeyStore ks, String alias, String alg,
79+
ObjectIdentifier oid) throws Exception {
80+
X509Certificate cert = (X509Certificate)ks.getCertificate(alias);
81+
System.out.println("SigAlgName = " + cert.getSigAlgName());
82+
83+
Asserts.assertEQ(cert.getPublicKey().getAlgorithm(), "EC");
84+
Asserts.assertEQ(cert.getSigAlgName(), alg);
85+
86+
byte[] data = cert.getEncoded();
87+
// Parameters field in the specified AlgorithmIdentifier should be omitted
88+
// Checking the first signature AlgorithmIdentifier in the cert
89+
checkAlg(data, "020", oid);
90+
shouldNotExist(data, "021");
91+
// Checking the second signature AlgorithmIdentifier in the cert
92+
checkAlg(data, "10", oid);
93+
shouldNotExist(data, "11");
94+
}
95+
96+
static OutputAnalyzer keytool(String cmd) throws Exception {
97+
return SecurityTools.keytool(cmd).shouldHaveExitValue(0);
98+
}
99+
}

0 commit comments

Comments
 (0)