001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DERObject;
008: import org.bouncycastle.asn1.DERObjectIdentifier;
009: import org.bouncycastle.asn1.DERSequence;
010:
011: import java.util.Enumeration;
012: import java.util.Hashtable;
013: import java.util.Vector;
014:
015: /**
016: * The extendedKeyUsage object.
017: * <pre>
018: * extendedKeyUsage ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
019: * </pre>
020: */
021: public class ExtendedKeyUsage extends ASN1Encodable {
022: Hashtable usageTable = new Hashtable();
023: ASN1Sequence seq;
024:
025: public static ExtendedKeyUsage getInstance(ASN1TaggedObject obj,
026: boolean explicit) {
027: return getInstance(ASN1Sequence.getInstance(obj, explicit));
028: }
029:
030: public static ExtendedKeyUsage getInstance(Object obj) {
031: if (obj instanceof ExtendedKeyUsage) {
032: return (ExtendedKeyUsage) obj;
033: }
034:
035: if (obj instanceof ASN1Sequence) {
036: return new ExtendedKeyUsage((ASN1Sequence) obj);
037: }
038:
039: if (obj instanceof X509Extension) {
040: return getInstance(X509Extension
041: .convertValueToObject((X509Extension) obj));
042: }
043:
044: throw new IllegalArgumentException("Invalid ExtendedKeyUsage: "
045: + obj.getClass().getName());
046: }
047:
048: public ExtendedKeyUsage(KeyPurposeId usage) {
049: this .seq = new DERSequence(usage);
050:
051: this .usageTable.put(usage, usage);
052: }
053:
054: public ExtendedKeyUsage(ASN1Sequence seq) {
055: this .seq = seq;
056:
057: Enumeration e = seq.getObjects();
058:
059: while (e.hasMoreElements()) {
060: Object o = e.nextElement();
061: if (!(o instanceof DERObjectIdentifier)) {
062: throw new IllegalArgumentException(
063: "Only DERObjectIdentifiers allowed in ExtendedKeyUsage.");
064: }
065: this .usageTable.put(o, o);
066: }
067: }
068:
069: public ExtendedKeyUsage(Vector usages) {
070: ASN1EncodableVector v = new ASN1EncodableVector();
071: Enumeration e = usages.elements();
072:
073: while (e.hasMoreElements()) {
074: DERObject o = (DERObject) e.nextElement();
075:
076: v.add(o);
077: this .usageTable.put(o, o);
078: }
079:
080: this .seq = new DERSequence(v);
081: }
082:
083: public boolean hasKeyPurposeId(KeyPurposeId keyPurposeId) {
084: return (usageTable.get(keyPurposeId) != null);
085: }
086:
087: /**
088: * Returns all extended key usages.
089: * The returned vector contains DERObjectIdentifiers.
090: * @return A vector with all key purposes.
091: */
092: public Vector getUsages() {
093: Vector temp = new Vector();
094: for (Enumeration it = usageTable.elements(); it
095: .hasMoreElements();) {
096: temp.addElement(it.nextElement());
097: }
098: return temp;
099: }
100:
101: public int size() {
102: return usageTable.size();
103: }
104:
105: public DERObject toASN1Object() {
106: return seq;
107: }
108: }
|