1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56: public class CertificatePolicies extends Extension.Value
57: {
58:
59:
60:
61:
62: public static final OID ID = new OID("2.5.29.32");
63:
64: private final List policies;
65: private final Map policyQualifierInfos;
66:
67:
68:
69:
70: public CertificatePolicies(final byte[] encoded) throws IOException
71: {
72: super(encoded);
73: DERReader der = new DERReader(encoded);
74: DERValue pol = der.read();
75: if (!pol.isConstructed())
76: throw new IOException("malformed CertificatePolicies");
77:
78: int len = 0;
79: LinkedList policyList = new LinkedList();
80: HashMap qualifierMap = new HashMap();
81: while (len < pol.getLength())
82: {
83: DERValue policyInfo = der.read();
84: if (!policyInfo.isConstructed())
85: throw new IOException("malformed PolicyInformation");
86: DERValue val = der.read();
87: if (val.getTag() != DER.OBJECT_IDENTIFIER)
88: throw new IOException("malformed CertPolicyId");
89: OID policyId = (OID) val.getValue();
90: policyList.add(policyId);
91: if (val.getEncodedLength() < policyInfo.getLength())
92: {
93: DERValue qual = der.read();
94: int len2 = 0;
95: LinkedList quals = new LinkedList();
96: while (len2 < qual.getLength())
97: {
98: val = der.read();
99: quals.add(new PolicyQualifierInfo(val.getEncoded()));
100: der.skip(val.getLength());
101: len2 += val.getEncodedLength();
102: }
103: qualifierMap.put(policyId, quals);
104: }
105: len += policyInfo.getEncodedLength();
106: }
107:
108: policies = Collections.unmodifiableList(policyList);
109: policyQualifierInfos = Collections.unmodifiableMap(qualifierMap);
110: }
111:
112: public CertificatePolicies (final List policies,
113: final Map policyQualifierInfos)
114: {
115: for (Iterator it = policies.iterator(); it.hasNext(); )
116: if (!(it.next() instanceof OID))
117: throw new IllegalArgumentException ("policies must be OIDs");
118: for (Iterator it = policyQualifierInfos.entrySet().iterator(); it.hasNext();)
119: {
120: Map.Entry e = (Map.Entry) it.next();
121: if (!(e.getKey() instanceof OID) || !policies.contains (e.getKey()))
122: throw new IllegalArgumentException
123: ("policyQualifierInfos keys must be OIDs");
124: if (!(e.getValue() instanceof List))
125: throw new IllegalArgumentException
126: ("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
127: for (Iterator it2 = ((List) e.getValue()).iterator(); it.hasNext(); )
128: if (!(it2.next() instanceof PolicyQualifierInfo))
129: throw new IllegalArgumentException
130: ("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
131: }
132: this.policies = Collections.unmodifiableList (new ArrayList (policies));
133: this.policyQualifierInfos = Collections.unmodifiableMap
134: (new HashMap (policyQualifierInfos));
135: }
136:
137:
138:
139:
140: public List getPolicies()
141: {
142: return policies;
143: }
144:
145: public List getPolicyQualifierInfos(OID oid)
146: {
147: return (List) policyQualifierInfos.get(oid);
148: }
149:
150: public byte[] getEncoded()
151: {
152: if (encoded == null)
153: {
154: List pol = new ArrayList (policies.size());
155: for (Iterator it = policies.iterator(); it.hasNext(); )
156: {
157: OID policy = (OID) it.next();
158: List qualifiers = getPolicyQualifierInfos (policy);
159: List l = new ArrayList (qualifiers == null ? 1 : 2);
160: l.add (new DERValue (DER.OBJECT_IDENTIFIER, policy));
161: if (qualifiers != null)
162: {
163: List ll = new ArrayList (qualifiers.size());
164: for (Iterator it2 = qualifiers.iterator(); it.hasNext(); )
165: {
166: PolicyQualifierInfo info = (PolicyQualifierInfo) it2.next();
167: try
168: {
169: ll.add (DERReader.read (info.getEncoded()));
170: }
171: catch (IOException ioe)
172: {
173: }
174: }
175: l.add (new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, ll));
176: }
177: pol.add (new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, l));
178: }
179: encoded = new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, pol).getEncoded();
180: }
181: return (byte[]) encoded.clone();
182: }
183:
184: public String toString()
185: {
186: return CertificatePolicies.class.getName() + " [ policies=" + policies +
187: " policyQualifierInfos=" + policyQualifierInfos + " ]";
188: }
189: }