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