1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.util.asn1.x509;
20
21 import java.util.Enumeration;
22 import java.util.Vector;
23
24 import org.apache.geronimo.util.asn1.ASN1Encodable;
25 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
26 import org.apache.geronimo.util.asn1.ASN1Sequence;
27 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
28 import org.apache.geronimo.util.asn1.DERObject;
29 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
30 import org.apache.geronimo.util.asn1.DERSequence;
31
32 public class CertificatePolicies
33 extends ASN1Encodable
34 {
35 static final DERObjectIdentifier anyPolicy = new DERObjectIdentifier("2.5.29.32.0");
36
37 Vector policies = new Vector();
38
39 /**
40 * @deprecated use an ASN1Sequence of PolicyInformation
41 */
42 public static CertificatePolicies getInstance(
43 ASN1TaggedObject obj,
44 boolean explicit)
45 {
46 return getInstance(ASN1Sequence.getInstance(obj, explicit));
47 }
48
49 /**
50 * @deprecated use an ASN1Sequence of PolicyInformation
51 */
52 public static CertificatePolicies getInstance(
53 Object obj)
54 {
55 if (obj instanceof CertificatePolicies)
56 {
57 return (CertificatePolicies)obj;
58 }
59 else if (obj instanceof ASN1Sequence)
60 {
61 return new CertificatePolicies((ASN1Sequence)obj);
62 }
63
64 throw new IllegalArgumentException("unknown object in factory");
65 }
66
67 /**
68 * @deprecated use an ASN1Sequence of PolicyInformation
69 */
70 public CertificatePolicies(
71 ASN1Sequence seq)
72 {
73 Enumeration e = seq.getObjects();
74 while (e.hasMoreElements())
75 {
76 ASN1Sequence s = (ASN1Sequence)e.nextElement();
77 policies.addElement(s.getObjectAt(0));
78 }
79
80 }
81
82 /**
83 * create a certificate policy with the given OID.
84 * @deprecated use an ASN1Sequence of PolicyInformation
85 */
86 public CertificatePolicies(
87 DERObjectIdentifier p)
88 {
89 policies.addElement(p);
90 }
91
92 /**
93 * create a certificate policy with the policy given by the OID represented
94 * by the string p.
95 * @deprecated use an ASN1Sequence of PolicyInformation
96 */
97 public CertificatePolicies(
98 String p)
99 {
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
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 }