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<OID> policies;
65: private final Map<OID, List<PolicyQualifierInfo>> 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<OID> policyList = new LinkedList<OID>();
80: HashMap<OID, List<PolicyQualifierInfo>> qualifierMap
81: = new HashMap<OID, List<PolicyQualifierInfo>>();
82: while (len < pol.getLength())
83: {
84: DERValue policyInfo = der.read();
85: if (!policyInfo.isConstructed())
86: throw new IOException("malformed PolicyInformation");
87: DERValue val = der.read();
88: if (val.getTag() != DER.OBJECT_IDENTIFIER)
89: throw new IOException("malformed CertPolicyId");
90: OID policyId = (OID) val.getValue();
91: policyList.add(policyId);
92: if (val.getEncodedLength() < policyInfo.getLength())
93: {
94: DERValue qual = der.read();
95: int len2 = 0;
96: LinkedList<PolicyQualifierInfo> quals = new LinkedList<PolicyQualifierInfo>();
97: while (len2 < qual.getLength())
98: {
99: val = der.read();
100: quals.add(new PolicyQualifierInfo(val.getEncoded()));
101: der.skip(val.getLength());
102: len2 += val.getEncodedLength();
103: }
104: qualifierMap.put(policyId, quals);
105: }
106: len += policyInfo.getEncodedLength();
107: }
108:
109: policies = Collections.unmodifiableList(policyList);
110: policyQualifierInfos = Collections.unmodifiableMap(qualifierMap);
111: }
112:
113: public CertificatePolicies (final List<OID> policies,
114: final Map<OID, List<PolicyQualifierInfo>> policyQualifierInfos)
115: {
116: for (Iterator it = policies.iterator(); it.hasNext(); )
117: if (!(it.next() instanceof OID))
118: throw new IllegalArgumentException ("policies must be OIDs");
119: for (Iterator it = policyQualifierInfos.entrySet().iterator(); it.hasNext();)
120: {
121: Map.Entry e = (Map.Entry) it.next();
122: if (!(e.getKey() instanceof OID) || !policies.contains (e.getKey()))
123: throw new IllegalArgumentException
124: ("policyQualifierInfos keys must be OIDs");
125: if (!(e.getValue() instanceof List))
126: throw new IllegalArgumentException
127: ("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
128: for (Iterator it2 = ((List) e.getValue()).iterator(); it.hasNext(); )
129: if (!(it2.next() instanceof PolicyQualifierInfo))
130: throw new IllegalArgumentException
131: ("policyQualifierInfos values must be Lists of PolicyQualifierInfos");
132: }
133: this.policies = Collections.unmodifiableList (new ArrayList<OID>(policies));
134: this.policyQualifierInfos = Collections.unmodifiableMap
135: (new HashMap<OID, List<PolicyQualifierInfo>>(policyQualifierInfos));
136: }
137:
138:
139:
140:
141: public List<OID> getPolicies()
142: {
143: return policies;
144: }
145:
146:
151: public List<String> getPolicyStrings()
152: {
153: List<String> l = new ArrayList<String>(policies.size());
154: for (OID oid : policies)
155: {
156: l.add(oid.toString());
157: }
158: return l;
159: }
160:
161: public List<PolicyQualifierInfo> getPolicyQualifierInfos(OID oid)
162: {
163: return policyQualifierInfos.get(oid);
164: }
165:
166: public byte[] getEncoded()
167: {
168: if (encoded == null)
169: {
170: List<DERValue> pol = new ArrayList<DERValue>(policies.size());
171: for (Iterator<OID> it = policies.iterator(); it.hasNext(); )
172: {
173: OID policy = it.next();
174: List<PolicyQualifierInfo> qualifiers = getPolicyQualifierInfos(policy);
175: List<DERValue> l = new ArrayList<DERValue>(qualifiers == null ? 1 : 2);
176: l.add(new DERValue(DER.OBJECT_IDENTIFIER, policy));
177: if (qualifiers != null)
178: {
179: List<DERValue> ll = new ArrayList<DERValue>(qualifiers.size());
180: for (Iterator<PolicyQualifierInfo> it2 = qualifiers.iterator(); it.hasNext(); )
181: {
182: PolicyQualifierInfo info = it2.next();
183: try
184: {
185: ll.add(DERReader.read(info.getEncoded()));
186: }
187: catch (IOException ioe)
188: {
189: }
190: }
191: l.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, ll));
192: }
193: pol.add(new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, l));
194: }
195: encoded = new DERValue(DER.CONSTRUCTED|DER.SEQUENCE, pol).getEncoded();
196: }
197: return (byte[]) encoded.clone();
198: }
199:
200: public String toString()
201: {
202: return CertificatePolicies.class.getName() + " [ policies=" + policies +
203: " policyQualifierInfos=" + policyQualifierInfos + " ]";
204: }
205: }