@@ -1264,45 +1264,49 @@ private void processKeyBag(SafeBag b)
12641264 //
12651265 // set the attributes on the key
12661266 //
1267- PKCS12BagAttributeCarrier bagAttr = (PKCS12BagAttributeCarrier )privKey ;
12681267 String alias = null ;
12691268 ASN1OctetString localId = null ;
12701269
1271- Enumeration e = b .getBagAttributes ().getObjects ();
1272- while (e .hasMoreElements ())
1270+ if (privKey instanceof PKCS12BagAttributeCarrier )
12731271 {
1274- ASN1Sequence sq = ASN1Sequence .getInstance (e .nextElement ());
1275- ASN1ObjectIdentifier aOid = ASN1ObjectIdentifier .getInstance (sq .getObjectAt (0 ));
1276- ASN1Set attrSet = ASN1Set .getInstance (sq .getObjectAt (1 ));
1277- ASN1Primitive attr = null ;
1272+ PKCS12BagAttributeCarrier bagAttr = (PKCS12BagAttributeCarrier )privKey ;
12781273
1279- if (attrSet .size () > 0 )
1274+ Enumeration e = b .getBagAttributes ().getObjects ();
1275+ while (e .hasMoreElements ())
12801276 {
1281- attr = (ASN1Primitive )attrSet .getObjectAt (0 );
1277+ ASN1Sequence sq = ASN1Sequence .getInstance (e .nextElement ());
1278+ ASN1ObjectIdentifier aOid = ASN1ObjectIdentifier .getInstance (sq .getObjectAt (0 ));
1279+ ASN1Set attrSet = ASN1Set .getInstance (sq .getObjectAt (1 ));
1280+ ASN1Primitive attr = null ;
12821281
1283- ASN1Encodable existing = bagAttr .getBagAttribute (aOid );
1284- if (existing != null )
1282+ if (attrSet .size () > 0 )
12851283 {
1286- // OK, but the value has to be the same
1287- if (!existing .toASN1Primitive ().equals (attr ))
1284+ attr = (ASN1Primitive )attrSet .getObjectAt (0 );
1285+
1286+ ASN1Encodable existing = bagAttr .getBagAttribute (aOid );
1287+ if (existing != null )
1288+ {
1289+ // OK, but the value has to be the same
1290+ if (!existing .toASN1Primitive ().equals (attr ))
1291+ {
1292+ throw new IOException (
1293+ "attempt to add existing attribute with different value" );
1294+ }
1295+ }
1296+ else
12881297 {
1289- throw new IOException (
1290- "attempt to add existing attribute with different value" );
1298+ bagAttr .setBagAttribute (aOid , attr );
12911299 }
1292- }
1293- else
1294- {
1295- bagAttr .setBagAttribute (aOid , attr );
1296- }
12971300
1298- if (aOid .equals (pkcs_9_at_friendlyName ))
1299- {
1300- alias = ((ASN1BMPString )attr ).getString ();
1301- keys .put (alias , privKey );
1302- }
1303- else if (aOid .equals (pkcs_9_at_localKeyId ))
1304- {
1305- localId = (ASN1OctetString )attr ;
1301+ if (aOid .equals (pkcs_9_at_friendlyName ))
1302+ {
1303+ alias = ((ASN1BMPString )attr ).getString ();
1304+ keys .put (alias , privKey );
1305+ }
1306+ else if (aOid .equals (pkcs_9_at_localKeyId ))
1307+ {
1308+ localId = (ASN1OctetString )attr ;
1309+ }
13061310 }
13071311 }
13081312 }
@@ -1454,11 +1458,14 @@ private void syncFriendlyName()
14541458 String keyId = (String ) cs .nextElement ();
14551459 PrivateKey key = (PrivateKey )keys .get (keyId );
14561460
1457- ASN1Encodable friendlyName = ((PKCS12BagAttributeCarrier )key ).getBagAttribute (PKCSObjectIdentifiers .pkcs_9_at_friendlyName );
1458- if (friendlyName != null && !keyId .equals (friendlyName .toString ()))
1461+ if (key instanceof PKCS12BagAttributeCarrier )
14591462 {
1460- keys .put (friendlyName .toString (), key );
1461- keys .remove (keyId );
1463+ ASN1Encodable friendlyName = ((PKCS12BagAttributeCarrier )key ).getBagAttribute (PKCSObjectIdentifiers .pkcs_9_at_friendlyName );
1464+ if (friendlyName != null && !keyId .equals (friendlyName .toString ()))
1465+ {
1466+ keys .put (friendlyName .toString (), key );
1467+ keys .remove (keyId );
1468+ }
14621469 }
14631470 }
14641471
@@ -1469,11 +1476,14 @@ private void syncFriendlyName()
14691476 String certId = (String ) cs .nextElement ();
14701477 Certificate cert = (Certificate )certs .get (certId );
14711478
1472- ASN1Encodable friendlyName = ((PKCS12BagAttributeCarrier )cert ).getBagAttribute (PKCSObjectIdentifiers .pkcs_9_at_friendlyName );
1473- if (friendlyName != null && !certId .equals (friendlyName .toString ()))
1479+ if (cert instanceof PKCS12BagAttributeCarrier )
14741480 {
1475- certs .put (friendlyName .toString (), cert );
1476- certs .remove (certId );
1481+ ASN1Encodable friendlyName = ((PKCS12BagAttributeCarrier )cert ).getBagAttribute (PKCSObjectIdentifiers .pkcs_9_at_friendlyName );
1482+ if (friendlyName != null && !certId .equals (friendlyName .toString ()))
1483+ {
1484+ certs .put (friendlyName .toString (), cert );
1485+ certs .remove (certId );
1486+ }
14771487 }
14781488 }
14791489 cs = keyCerts .keys ();
@@ -1483,11 +1493,14 @@ private void syncFriendlyName()
14831493 String certId = (String ) cs .nextElement ();
14841494 Certificate cert = (Certificate )keyCerts .get (certId );
14851495
1486- ASN1Encodable friendlyName = ((PKCS12BagAttributeCarrier )cert ).getBagAttribute (PKCSObjectIdentifiers .pkcs_9_at_friendlyName );
1487- if (friendlyName != null && !certId .equals (friendlyName .toString ()))
1496+ if (cert instanceof PKCS12BagAttributeCarrier )
14881497 {
1489- keyCerts .put (friendlyName .toString (), cert );
1490- keyCerts .remove (certId );
1498+ ASN1Encodable friendlyName = ((PKCS12BagAttributeCarrier )cert ).getBagAttribute (PKCSObjectIdentifiers .pkcs_9_at_friendlyName );
1499+ if (friendlyName != null && !certId .equals (friendlyName .toString ()))
1500+ {
1501+ keyCerts .put (friendlyName .toString (), cert );
1502+ keyCerts .remove (certId );
1503+ }
14911504 }
14921505 }
14931506 }
@@ -1835,7 +1848,6 @@ private void doStore(OutputStream stream, char[] password, boolean useDEREncodin
18351848 }
18361849 }
18371850
1838-
18391851 SafeBag sBag = new SafeBag (certBag , cBag .toASN1Primitive (), new DERSet (fName ));
18401852
18411853 certSeq .add (sBag );
@@ -2145,7 +2157,7 @@ public void put(String key, Object value)
21452157
21462158 public Enumeration keys ()
21472159 {
2148- return orig .keys ();
2160+ return new Hashtable ( orig ) .keys ();
21492161 }
21502162
21512163 public Object remove (String alias )
0 commit comments