001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.util.asn1.x509;
019    
020    import java.util.Enumeration;
021    import java.util.Vector;
022    
023    import org.apache.geronimo.util.asn1.ASN1Encodable;
024    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
025    import org.apache.geronimo.util.asn1.ASN1Sequence;
026    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
027    import org.apache.geronimo.util.asn1.DERObject;
028    import org.apache.geronimo.util.asn1.DERObjectIdentifier;
029    import org.apache.geronimo.util.asn1.DERSequence;
030    
031    public class CertificatePolicies
032        extends ASN1Encodable
033    {
034        static final DERObjectIdentifier anyPolicy = new DERObjectIdentifier("2.5.29.32.0");
035    
036        Vector policies = new Vector();
037    
038    /**
039     * @deprecated use an ASN1Sequence of PolicyInformation
040     */
041        public static CertificatePolicies getInstance(
042            ASN1TaggedObject obj,
043            boolean explicit)
044        {
045            return getInstance(ASN1Sequence.getInstance(obj, explicit));
046        }
047    
048    /**
049     * @deprecated use an ASN1Sequence of PolicyInformation
050     */
051        public static CertificatePolicies getInstance(
052            Object  obj)
053        {
054            if (obj instanceof CertificatePolicies)
055            {
056                return (CertificatePolicies)obj;
057            }
058            else if (obj instanceof ASN1Sequence)
059            {
060                return new CertificatePolicies((ASN1Sequence)obj);
061            }
062    
063            throw new IllegalArgumentException("unknown object in factory");
064        }
065    
066    /**
067     * @deprecated use an ASN1Sequence of PolicyInformation
068     */
069        public CertificatePolicies(
070            ASN1Sequence   seq)
071        {
072            Enumeration e = seq.getObjects();
073            while (e.hasMoreElements())
074            {
075                ASN1Sequence s = (ASN1Sequence)e.nextElement();
076                policies.addElement(s.getObjectAt(0));
077            }
078            // For now we just don't handle PolicyQualifiers
079        }
080    
081        /**
082         * create a certificate policy with the given OID.
083         * @deprecated use an ASN1Sequence of PolicyInformation
084         */
085        public CertificatePolicies(
086            DERObjectIdentifier p)
087        {
088            policies.addElement(p);
089        }
090    
091        /**
092         * create a certificate policy with the policy given by the OID represented
093         * by the string p.
094         * @deprecated use an ASN1Sequence of PolicyInformation
095         */
096        public CertificatePolicies(
097            String p)
098        {
099            this(new DERObjectIdentifier(p));
100        }
101    
102        public void addPolicy(
103            String p)
104        {
105            policies.addElement(new DERObjectIdentifier(p));
106        }
107    
108        public String getPolicy(int nr)
109        {
110            if (policies.size() > nr)
111                return ((DERObjectIdentifier)policies.elementAt(nr)).getId();
112    
113            return null;
114        }
115    
116        /**
117         * <pre>
118         * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
119         *
120         * PolicyInformation ::= SEQUENCE {
121         *   policyIdentifier   CertPolicyId,
122         *   policyQualifiers   SEQUENCE SIZE (1..MAX) OF
123         *                           PolicyQualifierInfo OPTIONAL }
124         *
125         * CertPolicyId ::= OBJECT IDENTIFIER
126         *
127         * PolicyQualifierInfo ::= SEQUENCE {
128         *   policyQualifierId  PolicyQualifierId,
129         *   qualifier          ANY DEFINED BY policyQualifierId }
130         *
131         * PolicyQualifierId ::=
132         *   OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
133         * </pre>
134         * @deprecated use an ASN1Sequence of PolicyInformation
135         */
136        public DERObject toASN1Object()
137        {
138            ASN1EncodableVector  v = new ASN1EncodableVector();
139    
140            // We only do policyIdentifier yet...
141            for (int i=0;i<policies.size();i++)
142            {
143                v.add(new DERSequence((DERObjectIdentifier)policies.elementAt(i)));
144            }
145    
146            return new DERSequence(v);
147        }
148    
149        public String toString()
150        {
151            String p = null;
152            for (int i=0;i<policies.size();i++)
153            {
154                if (p != null)
155                    p += ", ";
156                p += ((DERObjectIdentifier)policies.elementAt(i)).getId();
157            }
158            return "CertificatePolicies: "+p;
159        }
160    }