Skip to content

Commit ba6cdbe

Browse files
author
Valerie Peng
committedAug 22, 2023
8309214: sun/security/pkcs11/KeyStore/CertChainRemoval.java fails after 8301154
Reviewed-by: mbaesken, jnimeh
1 parent 9f4a9fe commit ba6cdbe

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed
 

‎src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyStore.java

+40-13
Original file line numberDiff line numberDiff line change
@@ -1559,22 +1559,50 @@ private void storeCert(String alias, X509Certificate cert)
15591559
cert.getSerialNumber().toByteArray()));
15601560
attrList.add(new CK_ATTRIBUTE(CKA_VALUE, cert.getEncoded()));
15611561

1562-
if (alias != null) {
1563-
attrList.add(new CK_ATTRIBUTE(CKA_LABEL, alias));
1564-
attrList.add(new CK_ATTRIBUTE(CKA_ID, alias));
1565-
} else {
1566-
// ibutton requires something to be set
1567-
// - alias must be unique
1568-
attrList.add(new CK_ATTRIBUTE(CKA_ID,
1569-
getID(cert.getSubjectX500Principal().getName
1570-
(X500Principal.CANONICAL), cert)));
1571-
}
1572-
15731562
Session session = null;
15741563
try {
15751564
session = token.getOpSession();
1565+
long[] ch = findObjects(session,
1566+
attrList.toArray(new CK_ATTRIBUTE[attrList.size()]));
1567+
if (ch.length != 0) { // found a match
1568+
if (debug != null) {
1569+
String certInfo = (alias == null?
1570+
"CA cert " + cert.getSubjectX500Principal() :
1571+
"EE cert for alias " + alias);
1572+
debug.println("storeCert: found a match for " + certInfo);
1573+
}
1574+
if (alias != null) {
1575+
// Add the alias to the existing cert
1576+
CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
1577+
new CK_ATTRIBUTE(CKA_LABEL, alias),
1578+
new CK_ATTRIBUTE(CKA_ID, alias) };
1579+
token.p11.C_SetAttributeValue
1580+
(session.id(), ch[0], attrs);
1581+
if (debug != null) {
1582+
debug.println("storeCert: added alias: " + alias);
1583+
}
1584+
}
1585+
// done; no need to create the cert
1586+
return;
1587+
}
1588+
if (alias != null) {
1589+
attrList.add(new CK_ATTRIBUTE(CKA_LABEL, alias));
1590+
attrList.add(new CK_ATTRIBUTE(CKA_ID, alias));
1591+
} else {
1592+
// ibutton requires something to be set
1593+
// - alias must be unique
1594+
attrList.add(new CK_ATTRIBUTE(CKA_ID,
1595+
getID(cert.getSubjectX500Principal().getName
1596+
(X500Principal.CANONICAL), cert)));
1597+
}
15761598
token.p11.C_CreateObject(session.id(),
1577-
attrList.toArray(new CK_ATTRIBUTE[attrList.size()]));
1599+
attrList.toArray(new CK_ATTRIBUTE[attrList.size()]));
1600+
if (debug != null) {
1601+
String certInfo = (alias == null?
1602+
"CA cert " + cert.getSubjectX500Principal() :
1603+
"EE cert for alias " + alias);
1604+
debug.println("storeCert: created " + certInfo);
1605+
}
15781606
} finally {
15791607
token.releaseSession(session);
15801608
}
@@ -1587,7 +1615,6 @@ private void storeChain(String alias, X509Certificate[] chain)
15871615
//
15881616
// end cert has CKA_LABEL and CKA_ID set to alias.
15891617
// other certs in chain have neither set.
1590-
15911618
storeCert(alias, chain[0]);
15921619
storeCaCerts(chain, 1);
15931620
}

‎test/jdk/sun/security/pkcs11/KeyStore/CertChainRemoval.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323

2424
/* @test
25-
* @bug 8301154
25+
* @bug 8301154 8309214
2626
* @summary test cert chain deletion logic w/ NSS PKCS11 KeyStore
2727
* @library /test/lib ..
2828
* @run testng/othervm CertChainRemoval

0 commit comments

Comments
 (0)
Please sign in to comment.