1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* 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 {
295
295
* (e.g., the given password is wrong).
296
296
*/
297
297
public Key engineGetKey (String alias , char [] password )
298
- throws NoSuchAlgorithmException , UnrecoverableKeyException
299
- {
298
+ throws NoSuchAlgorithmException , UnrecoverableKeyException {
300
299
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 {
301
305
Key key ;
302
306
303
307
if (!(entry instanceof KeyEntry )) {
@@ -321,7 +325,7 @@ public Key engineGetKey(String alias, char[] password)
321
325
try {
322
326
// get the encrypted private key
323
327
EncryptedPrivateKeyInfo encrInfo =
324
- new EncryptedPrivateKeyInfo (encrBytes );
328
+ new EncryptedPrivateKeyInfo (encrBytes );
325
329
encryptedKey = encrInfo .getEncryptedData ();
326
330
327
331
// parse Algorithm parameters
@@ -332,20 +336,20 @@ public Key engineGetKey(String alias, char[] password)
332
336
333
337
} catch (IOException ioe ) {
334
338
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 );
337
341
uke .initCause (ioe );
338
342
throw uke ;
339
343
}
340
344
341
- try {
345
+ try {
342
346
PBEParameterSpec pbeSpec ;
343
347
int ic ;
344
348
345
349
if (algParams != null ) {
346
350
try {
347
351
pbeSpec =
348
- algParams .getParameterSpec (PBEParameterSpec .class );
352
+ algParams .getParameterSpec (PBEParameterSpec .class );
349
353
} catch (InvalidParameterSpecException ipse ) {
350
354
throw new IOException ("Invalid PBE algorithm parameters" );
351
355
}
@@ -392,7 +396,7 @@ public Key engineGetKey(String alias, char[] password)
392
396
393
397
if (debug != null ) {
394
398
debug .println ("Retrieved a protected private key at alias" +
395
- " '" + alias + "' (" +
399
+ " '" + entry . alias + "' (" +
396
400
aid .getName () +
397
401
" iterations: " + ic + ")" );
398
402
}
@@ -433,7 +437,7 @@ public Key engineGetKey(String alias, char[] password)
433
437
434
438
if (debug != null ) {
435
439
debug .println ("Retrieved a protected secret key at alias " +
436
- "'" + alias + "' (" +
440
+ "'" + entry . alias + "' (" +
437
441
aid .getName () +
438
442
" iterations: " + ic + ")" );
439
443
}
@@ -450,8 +454,8 @@ public Key engineGetKey(String alias, char[] password)
450
454
451
455
} catch (Exception e ) {
452
456
UnrecoverableKeyException uke =
453
- new UnrecoverableKeyException ("Get Key failed: " +
454
- e .getMessage ());
457
+ new UnrecoverableKeyException ("Get Key failed: " +
458
+ e .getMessage ());
455
459
uke .initCause (e );
456
460
throw uke ;
457
461
}
@@ -471,15 +475,19 @@ public Key engineGetKey(String alias, char[] password)
471
475
*/
472
476
public Certificate [] engineGetCertificateChain (String alias ) {
473
477
Entry entry = entries .get (alias .toLowerCase (Locale .ENGLISH ));
478
+ return internalGetCertificateChain (entry );
479
+ }
480
+
481
+ private Certificate [] internalGetCertificateChain (Entry entry ) {
474
482
if (entry instanceof PrivateKeyEntry privateKeyEntry ) {
475
483
if (privateKeyEntry .chain == null ) {
476
484
return null ;
477
485
} else {
478
486
479
487
if (debug != null ) {
480
488
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 + "'" );
483
491
}
484
492
485
493
return privateKeyEntry .chain .clone ();
@@ -1013,18 +1021,19 @@ public synchronized void engineDeleteEntry(String alias)
1013
1021
debug .println ("Removing entry at alias '" + alias + "'" );
1014
1022
}
1015
1023
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 --;
1020
1035
}
1021
- privateKeyCount --;
1022
- } else if (entry instanceof CertEntry ) {
1023
- certificateCount --;
1024
- } else if (entry instanceof SecretKeyEntry ) {
1025
- secretKeyCount --;
1026
1036
}
1027
- entries .remove (alias .toLowerCase (Locale .ENGLISH ));
1028
1037
}
1029
1038
1030
1039
/**
@@ -1065,6 +1074,10 @@ public int engineSize() {
1065
1074
*/
1066
1075
public boolean engineIsKeyEntry (String alias ) {
1067
1076
Entry entry = entries .get (alias .toLowerCase (Locale .ENGLISH ));
1077
+ return internalIsKeyEntry (entry );
1078
+ }
1079
+
1080
+ private boolean internalIsKeyEntry (Entry entry ) {
1068
1081
return entry instanceof KeyEntry ;
1069
1082
}
1070
1083
@@ -1075,8 +1088,13 @@ public boolean engineIsKeyEntry(String alias) {
1075
1088
* @return true if the entry identified by the given alias is a
1076
1089
* <i>trusted certificate entry</i>, false otherwise.
1077
1090
*/
1091
+
1078
1092
public boolean engineIsCertificateEntry (String alias ) {
1079
1093
Entry entry = entries .get (alias .toLowerCase (Locale .ENGLISH ));
1094
+ return internalIsCertificateEntry (entry );
1095
+ }
1096
+
1097
+ private boolean internalIsCertificateEntry (Entry entry ) {
1080
1098
return entry instanceof CertEntry certEntry &&
1081
1099
certEntry .trustedKeyUsage != null ;
1082
1100
}
@@ -1306,36 +1324,32 @@ public KeyStore.Entry engineGetEntry(String alias,
1306
1324
1307
1325
Entry entry = entries .get (alias .toLowerCase (Locale .ENGLISH ));
1308
1326
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 " +
1315
1330
"alias '" + alias + "'" );
1316
- }
1331
+ }
1317
1332
1318
- return new KeyStore .TrustedCertificateEntry (
1333
+ return new KeyStore .TrustedCertificateEntry (
1319
1334
((CertEntry )entry ).cert , entry .attributes );
1320
- }
1321
1335
} else {
1322
1336
throw new UnrecoverableKeyException
1323
1337
("requested entry requires a password" );
1324
1338
}
1325
1339
}
1326
1340
1327
1341
if (protParam instanceof KeyStore .PasswordProtection ) {
1328
- if (engineIsCertificateEntry ( alias )) {
1342
+ if (internalIsCertificateEntry ( entry )) {
1329
1343
throw new UnsupportedOperationException
1330
1344
("trusted certificate entries are not password-protected" );
1331
- } else if (engineIsKeyEntry ( alias )) {
1345
+ } else if (internalIsKeyEntry ( entry )) {
1332
1346
KeyStore .PasswordProtection pp =
1333
1347
(KeyStore .PasswordProtection )protParam ;
1334
1348
char [] password = pp .getPassword ();
1335
1349
1336
- Key key = engineGetKey ( alias , password );
1350
+ Key key = internalGetKey ( entry , password );
1337
1351
if (key instanceof PrivateKey ) {
1338
- Certificate [] chain = engineGetCertificateChain ( alias );
1352
+ Certificate [] chain = internalGetCertificateChain ( entry );
1339
1353
1340
1354
return new KeyStore .PrivateKeyEntry ((PrivateKey )key , chain ,
1341
1355
entry .attributes );
@@ -1345,7 +1359,7 @@ public KeyStore.Entry engineGetEntry(String alias,
1345
1359
return new KeyStore .SecretKeyEntry ((SecretKey )key ,
1346
1360
entry .attributes );
1347
1361
}
1348
- } else if (! engineIsKeyEntry ( alias )) {
1362
+ } else {
1349
1363
throw new UnsupportedOperationException
1350
1364
("untrusted certificate entries are not " +
1351
1365
"password-protected" );
0 commit comments