1 package org.bouncycastle.jcajce.provider.asymmetric.util; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.io.ObjectInputStream; 6 import java.io.ObjectOutputStream; 7 import java.util.Enumeration; 8 import java.util.Hashtable; 9 import java.util.Vector; 10 11 import org.bouncycastle.asn1.ASN1Encodable; 12 import org.bouncycastle.asn1.ASN1InputStream; 13 import org.bouncycastle.asn1.ASN1ObjectIdentifier; 14 import org.bouncycastle.asn1.ASN1OutputStream; 15 import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier; 16 17 public class PKCS12BagAttributeCarrierImpl 18 implements PKCS12BagAttributeCarrier 19 { 20 private Hashtable pkcs12Attributes; 21 private Vector pkcs12Ordering; 22 PKCS12BagAttributeCarrierImpl(Hashtable attributes, Vector ordering)23 PKCS12BagAttributeCarrierImpl(Hashtable attributes, Vector ordering) 24 { 25 this.pkcs12Attributes = attributes; 26 this.pkcs12Ordering = ordering; 27 } 28 PKCS12BagAttributeCarrierImpl()29 public PKCS12BagAttributeCarrierImpl() 30 { 31 this(new Hashtable(), new Vector()); 32 } 33 setBagAttribute( ASN1ObjectIdentifier oid, ASN1Encodable attribute)34 public void setBagAttribute( 35 ASN1ObjectIdentifier oid, 36 ASN1Encodable attribute) 37 { 38 if (pkcs12Attributes.containsKey(oid)) 39 { // preserve original ordering 40 pkcs12Attributes.put(oid, attribute); 41 } 42 else 43 { 44 pkcs12Attributes.put(oid, attribute); 45 pkcs12Ordering.addElement(oid); 46 } 47 } 48 getBagAttribute( ASN1ObjectIdentifier oid)49 public ASN1Encodable getBagAttribute( 50 ASN1ObjectIdentifier oid) 51 { 52 return (ASN1Encodable)pkcs12Attributes.get(oid); 53 } 54 getBagAttributeKeys()55 public Enumeration getBagAttributeKeys() 56 { 57 return pkcs12Ordering.elements(); 58 } 59 size()60 int size() 61 { 62 return pkcs12Ordering.size(); 63 } 64 getAttributes()65 Hashtable getAttributes() 66 { 67 return pkcs12Attributes; 68 } 69 getOrdering()70 Vector getOrdering() 71 { 72 return pkcs12Ordering; 73 } 74 writeObject(ObjectOutputStream out)75 public void writeObject(ObjectOutputStream out) 76 throws IOException 77 { 78 if (pkcs12Ordering.size() == 0) 79 { 80 out.writeObject(new Hashtable()); 81 out.writeObject(new Vector()); 82 } 83 else 84 { 85 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 86 ASN1OutputStream aOut = new ASN1OutputStream(bOut); 87 88 Enumeration e = this.getBagAttributeKeys(); 89 90 while (e.hasMoreElements()) 91 { 92 ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)e.nextElement(); 93 94 aOut.writeObject(oid); 95 aOut.writeObject((ASN1Encodable)pkcs12Attributes.get(oid)); 96 } 97 98 out.writeObject(bOut.toByteArray()); 99 } 100 } 101 readObject(ObjectInputStream in)102 public void readObject(ObjectInputStream in) 103 throws IOException, ClassNotFoundException 104 { 105 Object obj = in.readObject(); 106 107 if (obj instanceof Hashtable) 108 { 109 this.pkcs12Attributes = (Hashtable)obj; 110 this.pkcs12Ordering = (Vector)in.readObject(); 111 } 112 else 113 { 114 ASN1InputStream aIn = new ASN1InputStream((byte[])obj); 115 116 ASN1ObjectIdentifier oid; 117 118 while ((oid = (ASN1ObjectIdentifier)aIn.readObject()) != null) 119 { 120 this.setBagAttribute(oid, aIn.readObject()); 121 } 122 } 123 } 124 } 125