1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.security.x509;
import java.io.IOException;
import java.security.cert.PolicyQualifierInfo;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import sun.security.util.DerValue;
import sun.security.util.DerOutputStream;
/**
* PolicyInformation is the class that contains a specific certificate policy
* that is part of the CertificatePoliciesExtension. A
* CertificatePolicyExtension value consists of a vector of these objects.
* <p>
* The ASN.1 syntax for PolicyInformation (IMPLICIT tagging is defined in the
* module definition):
* <pre>
*
* PolicyInformation ::= SEQUENCE {
* policyIdentifier CertPolicyId,
* policyQualifiers SEQUENCE SIZE (1..MAX) OF
* PolicyQualifierInfo OPTIONAL }
*
* CertPolicyId ::= OBJECT IDENTIFIER
*
* PolicyQualifierInfo ::= SEQUENCE {
* policyQualifierId PolicyQualifierId,
* qualifier ANY DEFINED BY policyQualifierId }
* </pre>
*
* @author Sean Mullan
* @author Anne Anderson
* @since 1.4
*/
public class PolicyInformation {
// Attribute names
public static final String NAME = "PolicyInformation";
public static final String ID = "id";
public static final String QUALIFIERS = "qualifiers";
/* The policy OID */
private CertificatePolicyId policyIdentifier;
/* A Set of java.security.cert.PolicyQualifierInfo objects */
private Set<PolicyQualifierInfo> policyQualifiers;
/**
* Create an instance of PolicyInformation
*
* @param policyIdentifier the policyIdentifier as a
* CertificatePolicyId
* @param policyQualifiers a Set of PolicyQualifierInfo objects.
* Must not be NULL. Specify an empty Set for no qualifiers.
* @exception IOException on decoding errors.
*/
public PolicyInformation(CertificatePolicyId policyIdentifier,
Set<PolicyQualifierInfo> policyQualifiers) throws IOException {
if (policyQualifiers == null) {
throw new NullPointerException("policyQualifiers is null");
}
this.policyQualifiers =
new LinkedHashSet<PolicyQualifierInfo>(policyQualifiers);
this.policyIdentifier = policyIdentifier;
}
/**
* Create an instance of PolicyInformation, decoding from
* the passed DerValue.
*
* @param val the DerValue to construct the PolicyInformation from.
* @exception IOException on decoding errors.
*/
public PolicyInformation(DerValue val) throws IOException {
if (val.tag != DerValue.tag_Sequence) {
throw new IOException("Invalid encoding of PolicyInformation");
}
policyIdentifier = new CertificatePolicyId(val.data.getDerValue());
if (val.data.available() != 0) {
policyQualifiers = new LinkedHashSet<PolicyQualifierInfo>();
DerValue opt = val.data.getDerValue();
if (opt.tag != DerValue.tag_Sequence)
throw new IOException("Invalid encoding of PolicyInformation");
if (opt.data.available() == 0)
throw new IOException("No data available in policyQualifiers");
while (opt.data.available() != 0)
policyQualifiers.add(new PolicyQualifierInfo
(opt.data.getDerValue().toByteArray()));
} else {
policyQualifiers = Collections.emptySet();
}
}
/**
* Compare this PolicyInformation with another object for equality
*
* @param other object to be compared with this
* @return true iff the PolicyInformation objects match
*/
public boolean equals(Object other) {
if (!(other instanceof PolicyInformation))
return false;
PolicyInformation piOther = (PolicyInformation)other;
if (!policyIdentifier.equals(piOther.getPolicyIdentifier()))
return false;
return policyQualifiers.equals(piOther.getPolicyQualifiers());
}
/**
* Returns the hash code for this PolicyInformation.
*
* @return a hash code value.
*/
public int hashCode() {
int myhash = 37 + policyIdentifier.hashCode();
myhash = 37 * myhash + policyQualifiers.hashCode();
return myhash;
}
/**
* Return the policyIdentifier value
*
* @return The CertificatePolicyId object containing
* the policyIdentifier (not a copy).
*/
public CertificatePolicyId getPolicyIdentifier() {
return policyIdentifier;
}
/**
* Return the policyQualifiers value
*
* @return a Set of PolicyQualifierInfo objects associated
* with this certificate policy (not a copy).
* Returns an empty Set if there are no qualifiers.
* Never returns null.
*/
public Set<PolicyQualifierInfo> getPolicyQualifiers() {
return policyQualifiers;
}
/**
* Get the attribute value.
*/
public Object get(String name) throws IOException {
if (name.equalsIgnoreCase(ID)) {
return policyIdentifier;
} else if (name.equalsIgnoreCase(QUALIFIERS)) {
return policyQualifiers;
} else {
throw new IOException("Attribute name [" + name +
"] not recognized by PolicyInformation.");
}
}
/**
* Set the attribute value.
*/
@SuppressWarnings("unchecked") // Checked with instanceof
public void set(String name, Object obj) throws IOException {
if (name.equalsIgnoreCase(ID)) {
if (obj instanceof CertificatePolicyId)
policyIdentifier = (CertificatePolicyId)obj;
else
throw new IOException("Attribute value must be instance " +
"of CertificatePolicyId.");
} else if (name.equalsIgnoreCase(QUALIFIERS)) {
if (policyIdentifier == null) {
throw new IOException("Attribute must have a " +
"CertificatePolicyIdentifier value before " +
"PolicyQualifierInfo can be set.");
}
if (obj instanceof Set) {
Iterator<?> i = ((Set<?>)obj).iterator();
while (i.hasNext()) {
Object obj1 = i.next();
if (!(obj1 instanceof PolicyQualifierInfo)) {
throw new IOException("Attribute value must be a" +
"Set of PolicyQualifierInfo objects.");
}
}
policyQualifiers = (Set<PolicyQualifierInfo>) obj;
} else {
throw new IOException("Attribute value must be of type Set.");
}
} else {
throw new IOException("Attribute name [" + name +
"] not recognized by PolicyInformation");
}
}
/**
* Delete the attribute value.
*/
public void delete(String name) throws IOException {
if (name.equalsIgnoreCase(QUALIFIERS)) {
policyQualifiers = Collections.emptySet();
} else if (name.equalsIgnoreCase(ID)) {
throw new IOException("Attribute ID may not be deleted from " +
"PolicyInformation.");
} else {
//ID may not be deleted
throw new IOException("Attribute name [" + name +
"] not recognized by PolicyInformation.");
}
}
/**
* Return an enumeration of names of attributes existing within this
* attribute.
*/
public Enumeration<String> getElements() {
AttributeNameEnumeration elements = new AttributeNameEnumeration();
elements.addElement(ID);
elements.addElement(QUALIFIERS);
return elements.elements();
}
/**
* Return the name of this attribute.
*/
public String getName() {
return NAME;
}
/**
* Return a printable representation of the PolicyInformation.
*/
public String toString() {
StringBuilder s = new StringBuilder(" [" + policyIdentifier.toString());
s.append(policyQualifiers + " ]\n");
return s.toString();
}
/**
* Write the PolicyInformation to the DerOutputStream.
*
* @param out the DerOutputStream to write the extension to.
* @exception IOException on encoding errors.
*/
public void encode(DerOutputStream out) throws IOException {
DerOutputStream tmp = new DerOutputStream();
policyIdentifier.encode(tmp);
if (!policyQualifiers.isEmpty()) {
DerOutputStream tmp2 = new DerOutputStream();
for (PolicyQualifierInfo pq : policyQualifiers) {
tmp2.write(pq.getEncoded());
}
tmp.write(DerValue.tag_Sequence, tmp2);
}
out.write(DerValue.tag_Sequence, tmp);
}
}
|