Skip to content

Commit 7eef10a

Browse files
author
duke
committedMar 11, 2024
Automatic merge of jdk:master into master
2 parents b6cce6b + ffd43c9 commit 7eef10a

File tree

2 files changed

+202
-38
lines changed

2 files changed

+202
-38
lines changed
 

‎src/java.base/share/classes/sun/security/pkcs12/PKCS12KeyStore.java

+52-38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2024, 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
@@ -295,9 +295,13 @@ static <S> S run(RetryWithZero<S> f, char[] password) throws Exception {
295295
* (e.g., the given password is wrong).
296296
*/
297297
public Key engineGetKey(String alias, char[] password)
298-
throws NoSuchAlgorithmException, UnrecoverableKeyException
299-
{
298+
throws NoSuchAlgorithmException, UnrecoverableKeyException {
300299
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
300+
return internalGetKey(entry, password);
301+
}
302+
303+
private Key internalGetKey(Entry entry, char[] password)
304+
throws NoSuchAlgorithmException, UnrecoverableKeyException {
301305
Key key;
302306

303307
if (!(entry instanceof KeyEntry)) {
@@ -321,7 +325,7 @@ public Key engineGetKey(String alias, char[] password)
321325
try {
322326
// get the encrypted private key
323327
EncryptedPrivateKeyInfo encrInfo =
324-
new EncryptedPrivateKeyInfo(encrBytes);
328+
new EncryptedPrivateKeyInfo(encrBytes);
325329
encryptedKey = encrInfo.getEncryptedData();
326330

327331
// parse Algorithm parameters
@@ -332,20 +336,20 @@ public Key engineGetKey(String alias, char[] password)
332336

333337
} catch (IOException ioe) {
334338
UnrecoverableKeyException uke =
335-
new UnrecoverableKeyException("Private key not stored as "
336-
+ "PKCS#8 EncryptedPrivateKeyInfo: " + ioe);
339+
new UnrecoverableKeyException("Private key not stored as "
340+
+ "PKCS#8 EncryptedPrivateKeyInfo: " + ioe);
337341
uke.initCause(ioe);
338342
throw uke;
339343
}
340344

341-
try {
345+
try {
342346
PBEParameterSpec pbeSpec;
343347
int ic;
344348

345349
if (algParams != null) {
346350
try {
347351
pbeSpec =
348-
algParams.getParameterSpec(PBEParameterSpec.class);
352+
algParams.getParameterSpec(PBEParameterSpec.class);
349353
} catch (InvalidParameterSpecException ipse) {
350354
throw new IOException("Invalid PBE algorithm parameters");
351355
}
@@ -392,7 +396,7 @@ public Key engineGetKey(String alias, char[] password)
392396

393397
if (debug != null) {
394398
debug.println("Retrieved a protected private key at alias" +
395-
" '" + alias + "' (" +
399+
" '" + entry.alias + "' (" +
396400
aid.getName() +
397401
" iterations: " + ic + ")");
398402
}
@@ -433,7 +437,7 @@ public Key engineGetKey(String alias, char[] password)
433437

434438
if (debug != null) {
435439
debug.println("Retrieved a protected secret key at alias " +
436-
"'" + alias + "' (" +
440+
"'" + entry.alias + "' (" +
437441
aid.getName() +
438442
" iterations: " + ic + ")");
439443
}
@@ -450,8 +454,8 @@ public Key engineGetKey(String alias, char[] password)
450454

451455
} catch (Exception e) {
452456
UnrecoverableKeyException uke =
453-
new UnrecoverableKeyException("Get Key failed: " +
454-
e.getMessage());
457+
new UnrecoverableKeyException("Get Key failed: " +
458+
e.getMessage());
455459
uke.initCause(e);
456460
throw uke;
457461
}
@@ -471,15 +475,19 @@ public Key engineGetKey(String alias, char[] password)
471475
*/
472476
public Certificate[] engineGetCertificateChain(String alias) {
473477
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
478+
return internalGetCertificateChain(entry);
479+
}
480+
481+
private Certificate[] internalGetCertificateChain(Entry entry) {
474482
if (entry instanceof PrivateKeyEntry privateKeyEntry) {
475483
if (privateKeyEntry.chain == null) {
476484
return null;
477485
} else {
478486

479487
if (debug != null) {
480488
debug.println("Retrieved a " +
481-
privateKeyEntry.chain.length +
482-
"-certificate chain at alias '" + alias + "'");
489+
privateKeyEntry.chain.length +
490+
"-certificate chain at alias '" + entry.alias + "'");
483491
}
484492

485493
return privateKeyEntry.chain.clone();
@@ -1013,18 +1021,19 @@ public synchronized void engineDeleteEntry(String alias)
10131021
debug.println("Removing entry at alias '" + alias + "'");
10141022
}
10151023

1016-
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
1017-
if (entry instanceof PrivateKeyEntry keyEntry) {
1018-
if (keyEntry.chain != null) {
1019-
certificateCount -= keyEntry.chain.length;
1024+
Entry entry = entries.remove(alias.toLowerCase(Locale.ENGLISH));
1025+
if (entry != null) {
1026+
if (entry instanceof PrivateKeyEntry keyEntry) {
1027+
if (keyEntry.chain != null) {
1028+
certificateCount -= keyEntry.chain.length;
1029+
}
1030+
privateKeyCount--;
1031+
} else if (entry instanceof CertEntry) {
1032+
certificateCount--;
1033+
} else if (entry instanceof SecretKeyEntry) {
1034+
secretKeyCount--;
10201035
}
1021-
privateKeyCount--;
1022-
} else if (entry instanceof CertEntry) {
1023-
certificateCount--;
1024-
} else if (entry instanceof SecretKeyEntry) {
1025-
secretKeyCount--;
10261036
}
1027-
entries.remove(alias.toLowerCase(Locale.ENGLISH));
10281037
}
10291038

10301039
/**
@@ -1065,6 +1074,10 @@ public int engineSize() {
10651074
*/
10661075
public boolean engineIsKeyEntry(String alias) {
10671076
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
1077+
return internalIsKeyEntry(entry);
1078+
}
1079+
1080+
private boolean internalIsKeyEntry(Entry entry) {
10681081
return entry instanceof KeyEntry;
10691082
}
10701083

@@ -1075,8 +1088,13 @@ public boolean engineIsKeyEntry(String alias) {
10751088
* @return true if the entry identified by the given alias is a
10761089
* <i>trusted certificate entry</i>, false otherwise.
10771090
*/
1091+
10781092
public boolean engineIsCertificateEntry(String alias) {
10791093
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
1094+
return internalIsCertificateEntry(entry);
1095+
}
1096+
1097+
private boolean internalIsCertificateEntry(Entry entry) {
10801098
return entry instanceof CertEntry certEntry &&
10811099
certEntry.trustedKeyUsage != null;
10821100
}
@@ -1306,36 +1324,32 @@ public KeyStore.Entry engineGetEntry(String alias,
13061324

13071325
Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
13081326
if (protParam == null) {
1309-
if (engineIsCertificateEntry(alias)) {
1310-
if (entry instanceof CertEntry &&
1311-
((CertEntry) entry).trustedKeyUsage != null) {
1312-
1313-
if (debug != null) {
1314-
debug.println("Retrieved a trusted certificate at " +
1327+
if (internalIsCertificateEntry(entry)) {
1328+
if (debug != null) {
1329+
debug.println("Retrieved a trusted certificate at " +
13151330
"alias '" + alias + "'");
1316-
}
1331+
}
13171332

1318-
return new KeyStore.TrustedCertificateEntry(
1333+
return new KeyStore.TrustedCertificateEntry(
13191334
((CertEntry)entry).cert, entry.attributes);
1320-
}
13211335
} else {
13221336
throw new UnrecoverableKeyException
13231337
("requested entry requires a password");
13241338
}
13251339
}
13261340

13271341
if (protParam instanceof KeyStore.PasswordProtection) {
1328-
if (engineIsCertificateEntry(alias)) {
1342+
if (internalIsCertificateEntry(entry)) {
13291343
throw new UnsupportedOperationException
13301344
("trusted certificate entries are not password-protected");
1331-
} else if (engineIsKeyEntry(alias)) {
1345+
} else if (internalIsKeyEntry(entry)) {
13321346
KeyStore.PasswordProtection pp =
13331347
(KeyStore.PasswordProtection)protParam;
13341348
char[] password = pp.getPassword();
13351349

1336-
Key key = engineGetKey(alias, password);
1350+
Key key = internalGetKey(entry, password);
13371351
if (key instanceof PrivateKey) {
1338-
Certificate[] chain = engineGetCertificateChain(alias);
1352+
Certificate[] chain = internalGetCertificateChain(entry);
13391353

13401354
return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain,
13411355
entry.attributes);
@@ -1345,7 +1359,7 @@ public KeyStore.Entry engineGetEntry(String alias,
13451359
return new KeyStore.SecretKeyEntry((SecretKey)key,
13461360
entry.attributes);
13471361
}
1348-
} else if (!engineIsKeyEntry(alias)) {
1362+
} else {
13491363
throw new UnsupportedOperationException
13501364
("untrusted certificate entries are not " +
13511365
"password-protected");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Copyright (c) 2024, 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 8327461
27+
* @summary engineGetEntry in PKCS12KeyStore should be thread-safe
28+
* @library /test/lib ../../../java/security/testlibrary
29+
* @modules java.base/sun.security.x509
30+
* java.base/sun.security.util
31+
* @build CertificateBuilder
32+
* @run main GetSetEntryTest
33+
*/
34+
35+
import java.math.BigInteger;
36+
import java.security.cert.X509Certificate;
37+
import java.security.KeyPair;
38+
import java.security.KeyPairGenerator;
39+
import java.security.KeyStore;
40+
import java.security.spec.ECGenParameterSpec;
41+
import java.util.concurrent.atomic.AtomicBoolean;
42+
import java.util.concurrent.TimeUnit;
43+
import java.util.Date;
44+
45+
import sun.security.testlibrary.CertificateBuilder;
46+
47+
public class GetSetEntryTest {
48+
49+
public static final String TEST = "test";
50+
51+
public static void main(String[] args) throws Exception {
52+
KeyStore ks = KeyStore.getInstance("PKCS12");
53+
char[] password = "password".toCharArray();
54+
KeyStore.PasswordProtection protParam = new KeyStore.PasswordProtection(password);
55+
ks.load(null, null);
56+
57+
CertificateBuilder cbld = new CertificateBuilder();
58+
KeyPairGenerator keyPairGen1 = KeyPairGenerator.getInstance("EC");
59+
keyPairGen1.initialize(new ECGenParameterSpec("secp256r1"));
60+
KeyPair ecKeyPair = keyPairGen1.genKeyPair();
61+
62+
long start = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(60);
63+
long end = start + TimeUnit.DAYS.toMillis(1085);
64+
boolean[] kuBitSettings = {true, false, false, false, false, true,
65+
true, false, false};
66+
67+
// Set up the EC Cert
68+
cbld.setSubjectName("CN=EC Test Cert, O=SomeCompany").
69+
setPublicKey(ecKeyPair.getPublic()).
70+
setSerialNumber(new BigInteger("1")).
71+
setValidity(new Date(start), new Date(end)).
72+
addSubjectKeyIdExt(ecKeyPair.getPublic()).
73+
addAuthorityKeyIdExt(ecKeyPair.getPublic()).
74+
addBasicConstraintsExt(true, true, -1).
75+
addKeyUsageExt(kuBitSettings);
76+
77+
X509Certificate ecCert = cbld.build(null, ecKeyPair.getPrivate(), "SHA256withECDSA");
78+
79+
KeyPairGenerator keyPairGen2 = KeyPairGenerator.getInstance("RSA");
80+
keyPairGen2.initialize(4096);
81+
KeyPair rsaKeyPair = keyPairGen2.genKeyPair();
82+
83+
cbld.reset();
84+
// Set up the RSA Cert
85+
cbld.setSubjectName("CN=RSA Test Cert, O=SomeCompany").
86+
setPublicKey(rsaKeyPair.getPublic()).
87+
setSerialNumber(new BigInteger("1")).
88+
setValidity(new Date(start), new Date(end)).
89+
addSubjectKeyIdExt(rsaKeyPair.getPublic()).
90+
addAuthorityKeyIdExt(rsaKeyPair.getPublic()).
91+
addBasicConstraintsExt(true, true, -1).
92+
addKeyUsageExt(kuBitSettings);
93+
94+
X509Certificate rsaCert = cbld.build(null, rsaKeyPair.getPrivate(), "SHA256withRSA");
95+
96+
KeyStore.PrivateKeyEntry ecEntry = new KeyStore.PrivateKeyEntry(ecKeyPair.getPrivate(),
97+
new X509Certificate[]{ecCert});
98+
KeyStore.PrivateKeyEntry rsaEntry = new KeyStore.PrivateKeyEntry(rsaKeyPair.getPrivate(),
99+
new X509Certificate[]{rsaCert});
100+
101+
test(ks, ecEntry, rsaEntry, protParam);
102+
}
103+
104+
private static final int MAX_ITERATIONS = 100;
105+
106+
private static void test(KeyStore ks, KeyStore.PrivateKeyEntry ec,
107+
KeyStore.PrivateKeyEntry rsa,
108+
KeyStore.PasswordProtection protParam)
109+
throws Exception {
110+
ks.setEntry(TEST, ec, protParam);
111+
112+
AtomicBoolean syncIssue = new AtomicBoolean(false);
113+
114+
Thread thread = new Thread(() -> {
115+
int iterations = 0;
116+
while (!syncIssue.get() && iterations < MAX_ITERATIONS) {
117+
try {
118+
ks.setEntry(TEST, ec, protParam);
119+
ks.setEntry(TEST, rsa, protParam);
120+
} catch (Exception ex) {
121+
syncIssue.set(true);
122+
ex.printStackTrace();
123+
System.out.println("Test failed");
124+
System.exit(1);
125+
}
126+
iterations++;
127+
}
128+
});
129+
thread.start();
130+
131+
int iterations = 0;
132+
while (!syncIssue.get() && iterations < MAX_ITERATIONS) {
133+
try {
134+
ks.getEntry(TEST, protParam);
135+
} catch (Exception ex) {
136+
syncIssue.set(true);
137+
ex.printStackTrace();
138+
System.out.println("Test failed");
139+
System.exit(1);
140+
}
141+
iterations++;
142+
}
143+
144+
thread.join();
145+
146+
if (!syncIssue.get()) {
147+
System.out.println("Test completed successfully");
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)
Please sign in to comment.