@@ -1559,22 +1559,50 @@ private void storeCert(String alias, X509Certificate cert)
1559
1559
cert .getSerialNumber ().toByteArray ()));
1560
1560
attrList .add (new CK_ATTRIBUTE (CKA_VALUE , cert .getEncoded ()));
1561
1561
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
-
1573
1562
Session session = null ;
1574
1563
try {
1575
1564
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
+ }
1576
1598
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
+ }
1578
1606
} finally {
1579
1607
token .releaseSession (session );
1580
1608
}
@@ -1587,7 +1615,6 @@ private void storeChain(String alias, X509Certificate[] chain)
1587
1615
//
1588
1616
// end cert has CKA_LABEL and CKA_ID set to alias.
1589
1617
// other certs in chain have neither set.
1590
-
1591
1618
storeCert (alias , chain [0 ]);
1592
1619
storeCaCerts (chain , 1 );
1593
1620
}
0 commit comments