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
|
/*
* Copyright (c) 1998, 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.lang.reflect.Constructor;
import java.util.Arrays;
import sun.security.util.*;
/**
* This class represents the OtherName as required by the GeneralNames
* ASN.1 object. It supplies the generic framework to allow specific
* Other Name types, and also provides minimal support for unrecognized
* Other Name types.
*
* The ASN.1 definition for OtherName is:
* <pre>
* OtherName ::= SEQUENCE {
* type-id OBJECT IDENTIFIER,
* value [0] EXPLICIT ANY DEFINED BY type-id
* }
* </pre>
* @author Hemma Prafullchandra
*/
public class OtherName implements GeneralNameInterface {
private String name;
private ObjectIdentifier oid;
private byte[] nameValue = null;
private GeneralNameInterface gni = null;
private static final byte TAG_VALUE = 0;
private int myhash = -1;
/**
* Create the OtherName object from a passed ObjectIdentfier and
* byte array name value
*
* @param oid ObjectIdentifier of this OtherName object
* @param value the DER-encoded value of the OtherName
* @throws IOException on error
*/
public OtherName(ObjectIdentifier oid, byte[] value) throws IOException {
if (oid == null || value == null) {
throw new NullPointerException("parameters may not be null");
}
this.oid = oid;
this.nameValue = value;
gni = getGNI(oid, value);
if (gni != null) {
name = gni.toString();
} else {
name = "Unrecognized ObjectIdentifier: " + oid.toString();
}
}
/**
* Create the OtherName object from the passed encoded Der value.
*
* @param derValue the encoded DER OtherName.
* @exception IOException on error.
*/
public OtherName(DerValue derValue) throws IOException {
DerInputStream in = derValue.toDerInputStream();
oid = in.getOID();
DerValue val = in.getDerValue();
nameValue = val.toByteArray();
gni = getGNI(oid, nameValue);
if (gni != null) {
name = gni.toString();
} else {
name = "Unrecognized ObjectIdentifier: " + oid.toString();
}
}
/**
* Get ObjectIdentifier
*/
public ObjectIdentifier getOID() {
//XXXX May want to consider cloning this
return oid;
}
/**
* Get name value
*/
public byte[] getNameValue() {
return nameValue.clone();
}
/**
* Get GeneralNameInterface
*/
private GeneralNameInterface getGNI(ObjectIdentifier oid, byte[] nameValue)
throws IOException {
try {
Class<?> extClass = OIDMap.getClass(oid);
if (extClass == null) { // Unsupported OtherName
return null;
}
Class<?>[] params = { Object.class };
Constructor<?> cons = extClass.getConstructor(params);
Object[] passed = new Object[] { nameValue };
GeneralNameInterface gni =
(GeneralNameInterface)cons.newInstance(passed);
return gni;
} catch (Exception e) {
throw new IOException("Instantiation error: " + e, e);
}
}
/**
* Return the type of the GeneralName.
*/
public int getType() {
return GeneralNameInterface.NAME_ANY;
}
/**
* Encode the Other name into the DerOutputStream.
*
* @param out the DER stream to encode the Other-Name to.
* @exception IOException on encoding errors.
*/
public void encode(DerOutputStream out) throws IOException {
if (gni != null) {
// This OtherName has a supported class
gni.encode(out);
return;
} else {
// This OtherName has no supporting class
DerOutputStream tmp = new DerOutputStream();
tmp.putOID(oid);
tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_VALUE), nameValue);
out.write(DerValue.tag_Sequence, tmp);
}
}
/**
* Compares this name with another, for equality.
*
* @return true iff the names are identical.
*/
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof OtherName)) {
return false;
}
OtherName otherOther = (OtherName)other;
if (!(otherOther.oid.equals((Object)oid))) {
return false;
}
GeneralNameInterface otherGNI = null;
try {
otherGNI = getGNI(otherOther.oid, otherOther.nameValue);
} catch (IOException ioe) {
return false;
}
boolean result;
if (otherGNI != null) {
try {
result = (otherGNI.constrains(this) == NAME_MATCH);
} catch (UnsupportedOperationException ioe) {
result = false;
}
} else {
result = Arrays.equals(nameValue, otherOther.nameValue);
}
return result;
}
/**
* Returns the hash code for this OtherName.
*
* @return a hash code value.
*/
public int hashCode() {
if (myhash == -1) {
myhash = 37 + oid.hashCode();
for (int i = 0; i < nameValue.length; i++) {
myhash = 37 * myhash + nameValue[i];
}
}
return myhash;
}
/**
* Convert the name into user readable string.
*/
public String toString() {
return "Other-Name: " + name;
}
/**
* Return type of constraint inputName places on this name:<ul>
* <li>NAME_DIFF_TYPE = -1: input name is different type from name
* (i.e. does not constrain).
* <li>NAME_MATCH = 0: input name matches name.
* <li>NAME_NARROWS = 1: input name narrows name (is lower in the
* naming subtree)
* <li>NAME_WIDENS = 2: input name widens name (is higher in the
* naming subtree)
* <li>NAME_SAME_TYPE = 3: input name does not match or narrow name,
* but is same type.
* </ul>. These results are used in checking NameConstraints during
* certification path verification.
*
* @param inputName to be checked for being constrained
* @returns constraint type above
* @throws UnsupportedOperationException if name is same type, but
* comparison operations are not supported for this name type.
*/
public int constrains(GeneralNameInterface inputName) {
int constraintType;
if (inputName == null) {
constraintType = NAME_DIFF_TYPE;
} else if (inputName.getType() != NAME_ANY) {
constraintType = NAME_DIFF_TYPE;
} else {
throw new UnsupportedOperationException("Narrowing, widening, "
+ "and matching are not supported for OtherName.");
}
return constraintType;
}
/**
* Return subtree depth of this name for purposes of determining
* NameConstraints minimum and maximum bounds.
*
* @returns distance of name from root
* @throws UnsupportedOperationException if not supported for this name type
*/
public int subtreeDepth() {
throw new UnsupportedOperationException
("subtreeDepth() not supported for generic OtherName");
}
}
|