Skip to content

Commit 424c58f

Browse files
committed
8187634: keystore.getCertificateAlias(cert) returns original alias, inconsistent with fix of JDK-6483657
Reviewed-by: mullan
1 parent 14dab31 commit 424c58f

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/CKeyStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, 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
@@ -636,7 +636,7 @@ public String engineGetCertificateAlias(Certificate cert) {
636636
if (entry.certChain != null &&
637637
entry.certChain.length > 0 &&
638638
entry.certChain[0].equals(cert)) {
639-
return entry.getAlias();
639+
return mapEntry.getKey();
640640
}
641641
}
642642

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2023, 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+
import jdk.test.lib.Asserts;
24+
import sun.security.tools.keytool.CertAndKeyGen;
25+
import sun.security.x509.X500Name;
26+
27+
import java.security.KeyStore;
28+
import java.security.MessageDigest;
29+
import java.security.cert.X509Certificate;
30+
import java.util.HexFormat;
31+
32+
/**
33+
* @test
34+
* @bug 8187634
35+
* @requires os.family == "windows"
36+
* @library /test/lib
37+
* @modules java.base/sun.security.tools.keytool
38+
* java.base/sun.security.x509
39+
* @summary getCertificateAlias should return correct alias
40+
*/
41+
public class DupAlias {
42+
public static void main(String[] args) throws Exception {
43+
44+
String nn = "8187634";
45+
String na = nn + "a";
46+
String nb = nn + "b";
47+
String n1 = nn + " (1)";
48+
49+
CertAndKeyGen g = new CertAndKeyGen("EC", "SHA256withECDSA");
50+
g.generate(-1);
51+
X509Certificate a = g.getSelfCertificate(new X500Name("CN=" + na), 1000);
52+
g.generate(-1);
53+
X509Certificate b = g.getSelfCertificate(new X500Name("CN=" + nb), 1000);
54+
55+
KeyStore ks = KeyStore.getInstance("Windows-MY-CURRENTUSER");
56+
try {
57+
ks.load(null, null);
58+
ks.deleteEntry(na);
59+
ks.deleteEntry(nb);
60+
ks.deleteEntry(nn);
61+
ks.deleteEntry(n1);
62+
ks.setCertificateEntry(na, a);
63+
ks.setCertificateEntry(nb, b);
64+
65+
ps(String.format("""
66+
$cert = Get-Item Cert:/CurrentUser/My/%s;
67+
$cert.FriendlyName = %s;
68+
$cert = Get-Item Cert:/CurrentUser/My/%s;
69+
$cert.FriendlyName = %s;
70+
""", thumbprint(a), nn, thumbprint(b), nn));
71+
72+
ks.load(null, null);
73+
Asserts.assertFalse(ks.containsAlias(na));
74+
Asserts.assertFalse(ks.containsAlias(nb));
75+
Asserts.assertEquals(ks.getCertificateAlias(ks.getCertificate(nn)), nn);
76+
Asserts.assertEquals(ks.getCertificateAlias(ks.getCertificate(n1)), n1);
77+
} finally {
78+
ks.deleteEntry(na);
79+
ks.deleteEntry(nb);
80+
ks.deleteEntry(nn);
81+
ks.deleteEntry(n1);
82+
}
83+
}
84+
85+
static void ps(String f) throws Exception {
86+
ProcessBuilder pb = new ProcessBuilder("powershell", "-Command", f);
87+
pb.inheritIO();
88+
if (pb.start().waitFor() != 0) {
89+
throw new RuntimeException("Failed");
90+
}
91+
}
92+
93+
static String thumbprint(X509Certificate c) throws Exception {
94+
return HexFormat.of().formatHex(
95+
MessageDigest.getInstance("SHA-1").digest(c.getEncoded()));
96+
}
97+
}

0 commit comments

Comments
 (0)