001: package org.bouncycastle.asn1.x509;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005:
006: import org.bouncycastle.asn1.ASN1Encodable;
007: import org.bouncycastle.asn1.ASN1EncodableVector;
008: import org.bouncycastle.asn1.ASN1Sequence;
009: import org.bouncycastle.asn1.ASN1TaggedObject;
010: import org.bouncycastle.asn1.DERObject;
011: import org.bouncycastle.asn1.DERObjectIdentifier;
012: import org.bouncycastle.asn1.DERSequence;
013:
014: public class CertificatePolicies extends ASN1Encodable {
015: static final DERObjectIdentifier anyPolicy = new DERObjectIdentifier(
016: "2.5.29.32.0");
017:
018: Vector policies = new Vector();
019:
020: /**
021: * @deprecated use an ASN1Sequence of PolicyInformation
022: */
023: public static CertificatePolicies getInstance(ASN1TaggedObject obj,
024: boolean explicit) {
025: return getInstance(ASN1Sequence.getInstance(obj, explicit));
026: }
027:
028: /**
029: * @deprecated use an ASN1Sequence of PolicyInformation
030: */
031: public static CertificatePolicies getInstance(Object obj) {
032: if (obj instanceof CertificatePolicies) {
033: return (CertificatePolicies) obj;
034: } else if (obj instanceof ASN1Sequence) {
035: return new CertificatePolicies((ASN1Sequence) obj);
036: }
037:
038: throw new IllegalArgumentException("unknown object in factory");
039: }
040:
041: /**
042: * @deprecated use an ASN1Sequence of PolicyInformation
043: */
044: public CertificatePolicies(ASN1Sequence seq) {
045: Enumeration e = seq.getObjects();
046: while (e.hasMoreElements()) {
047: ASN1Sequence s = ASN1Sequence.getInstance(e.nextElement());
048: policies.addElement(s.getObjectAt(0));
049: }
050: // For now we just don't handle PolicyQualifiers
051: }
052:
053: /**
054: * create a certificate policy with the given OID.
055: * @deprecated use an ASN1Sequence of PolicyInformation
056: */
057: public CertificatePolicies(DERObjectIdentifier p) {
058: policies.addElement(p);
059: }
060:
061: /**
062: * create a certificate policy with the policy given by the OID represented
063: * by the string p.
064: * @deprecated use an ASN1Sequence of PolicyInformation
065: */
066: public CertificatePolicies(String p) {
067: this (new DERObjectIdentifier(p));
068: }
069:
070: public void addPolicy(String p) {
071: policies.addElement(new DERObjectIdentifier(p));
072: }
073:
074: public String getPolicy(int nr) {
075: if (policies.size() > nr) {
076: return ((DERObjectIdentifier) policies.elementAt(nr))
077: .getId();
078: }
079:
080: return null;
081: }
082:
083: /**
084: * <pre>
085: * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
086: *
087: * PolicyInformation ::= SEQUENCE {
088: * policyIdentifier CertPolicyId,
089: * policyQualifiers SEQUENCE SIZE (1..MAX) OF
090: * PolicyQualifierInfo OPTIONAL }
091: *
092: * CertPolicyId ::= OBJECT IDENTIFIER
093: *
094: * PolicyQualifierInfo ::= SEQUENCE {
095: * policyQualifierId PolicyQualifierId,
096: * qualifier ANY DEFINED BY policyQualifierId }
097: *
098: * PolicyQualifierId ::=
099: * OBJECT IDENTIFIER (id-qt-cps | id-qt-unotice)
100: * </pre>
101: * @deprecated use an ASN1Sequence of PolicyInformation
102: */
103: public DERObject toASN1Object() {
104: ASN1EncodableVector v = new ASN1EncodableVector();
105:
106: // We only do policyIdentifier yet...
107: for (int i = 0; i < policies.size(); i++) {
108: v.add(new DERSequence((DERObjectIdentifier) policies
109: .elementAt(i)));
110: }
111:
112: return new DERSequence(v);
113: }
114:
115: public String toString() {
116: String p = null;
117: for (int i = 0; i < policies.size(); i++) {
118: if (p != null) {
119: p += ", ";
120: }
121: p += ((DERObjectIdentifier) policies.elementAt(i)).getId();
122: }
123: return "CertificatePolicies: " + p;
124: }
125: }
|