001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
004: import org.bouncycastle.asn1.DERObjectIdentifier;
005: import org.bouncycastle.asn1.DEREncodable;
006: import org.bouncycastle.asn1.ASN1OutputStream;
007: import org.bouncycastle.asn1.ASN1InputStream;
008:
009: import java.util.Enumeration;
010: import java.util.Hashtable;
011: import java.util.Vector;
012: import java.io.ObjectOutputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.IOException;
015: import java.io.ObjectInputStream;
016:
017: class PKCS12BagAttributeCarrierImpl implements
018: PKCS12BagAttributeCarrier {
019: private Hashtable pkcs12Attributes;
020: private Vector pkcs12Ordering;
021:
022: PKCS12BagAttributeCarrierImpl(Hashtable attributes, Vector ordering) {
023: this .pkcs12Attributes = attributes;
024: this .pkcs12Ordering = ordering;
025: }
026:
027: public PKCS12BagAttributeCarrierImpl() {
028: this (new Hashtable(), new Vector());
029: }
030:
031: public void setBagAttribute(DERObjectIdentifier oid,
032: DEREncodable attribute) {
033: if (pkcs12Attributes.containsKey(oid)) { // preserve original ordering
034: pkcs12Attributes.put(oid, attribute);
035: } else {
036: pkcs12Attributes.put(oid, attribute);
037: pkcs12Ordering.addElement(oid);
038: }
039: }
040:
041: public DEREncodable getBagAttribute(DERObjectIdentifier oid) {
042: return (DEREncodable) pkcs12Attributes.get(oid);
043: }
044:
045: public Enumeration getBagAttributeKeys() {
046: return pkcs12Ordering.elements();
047: }
048:
049: int size() {
050: return pkcs12Ordering.size();
051: }
052:
053: Hashtable getAttributes() {
054: return pkcs12Attributes;
055: }
056:
057: Vector getOrdering() {
058: return pkcs12Ordering;
059: }
060:
061: public void writeObject(ObjectOutputStream out) throws IOException {
062: if (pkcs12Ordering.size() == 0) {
063: out.writeObject(new Hashtable());
064: out.writeObject(new Vector());
065: } else {
066: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
067: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
068:
069: Enumeration e = this .getBagAttributeKeys();
070:
071: while (e.hasMoreElements()) {
072: DERObjectIdentifier oid = (DERObjectIdentifier) e
073: .nextElement();
074:
075: aOut.writeObject(oid);
076: aOut.writeObject(pkcs12Attributes.get(oid));
077: }
078:
079: out.writeObject(bOut.toByteArray());
080: }
081: }
082:
083: public void readObject(ObjectInputStream in) throws IOException,
084: ClassNotFoundException {
085: Object obj = in.readObject();
086:
087: if (obj instanceof Hashtable) {
088: this .pkcs12Attributes = (Hashtable) obj;
089: this .pkcs12Ordering = (Vector) in.readObject();
090: } else {
091: ASN1InputStream aIn = new ASN1InputStream((byte[]) obj);
092:
093: DERObjectIdentifier oid;
094:
095: while ((oid = (DERObjectIdentifier) aIn.readObject()) != null) {
096: this.setBagAttribute(oid, aIn.readObject());
097: }
098: }
099: }
100: }
|