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