Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/x509/GeneralName.java
41159 views
1
/*
2
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.x509;
27
28
import java.io.IOException;
29
30
import sun.security.util.*;
31
32
/**
33
* This class implements the ASN.1 GeneralName object class.
34
* <p>
35
* The ASN.1 syntax for this is:
36
* <pre>
37
* GeneralName ::= CHOICE {
38
* otherName [0] OtherName,
39
* rfc822Name [1] IA5String,
40
* dNSName [2] IA5String,
41
* x400Address [3] ORAddress,
42
* directoryName [4] Name,
43
* ediPartyName [5] EDIPartyName,
44
* uniformResourceIdentifier [6] IA5String,
45
* iPAddress [7] OCTET STRING,
46
* registeredID [8] OBJECT IDENTIFIER
47
* }
48
* </pre>
49
* @author Amit Kapoor
50
* @author Hemma Prafullchandra
51
*/
52
public class GeneralName {
53
54
// Private data members
55
private GeneralNameInterface name = null;
56
57
/**
58
* Default constructor for the class.
59
*
60
* @param name the selected CHOICE from the list.
61
* @throws NullPointerException if name is null
62
*/
63
public GeneralName(GeneralNameInterface name) {
64
if (name == null) {
65
throw new NullPointerException("GeneralName must not be null");
66
}
67
this.name = name;
68
}
69
70
/**
71
* Create the object from its DER encoded value.
72
*
73
* @param encName the DER encoded GeneralName.
74
*/
75
public GeneralName(DerValue encName) throws IOException {
76
this(encName, false);
77
}
78
79
/**
80
* Create the object from its DER encoded value.
81
*
82
* @param encName the DER encoded GeneralName.
83
* @param nameConstraint true if general name is a name constraint
84
*/
85
public GeneralName(DerValue encName, boolean nameConstraint)
86
throws IOException {
87
short tag = (byte)(encName.tag & 0x1f);
88
89
// All names except for NAME_DIRECTORY should be encoded with the
90
// IMPLICIT tag.
91
switch (tag) {
92
case GeneralNameInterface.NAME_ANY:
93
if (encName.isContextSpecific() && encName.isConstructed()) {
94
encName.resetTag(DerValue.tag_Sequence);
95
name = new OtherName(encName);
96
} else {
97
throw new IOException("Invalid encoding of Other-Name");
98
}
99
break;
100
101
case GeneralNameInterface.NAME_RFC822:
102
if (encName.isContextSpecific() && !encName.isConstructed()) {
103
encName.resetTag(DerValue.tag_IA5String);
104
name = new RFC822Name(encName);
105
} else {
106
throw new IOException("Invalid encoding of RFC822 name");
107
}
108
break;
109
110
case GeneralNameInterface.NAME_DNS:
111
if (encName.isContextSpecific() && !encName.isConstructed()) {
112
encName.resetTag(DerValue.tag_IA5String);
113
name = new DNSName(encName);
114
} else {
115
throw new IOException("Invalid encoding of DNSName");
116
}
117
break;
118
119
case GeneralNameInterface.NAME_X400:
120
if (encName.isContextSpecific() && encName.isConstructed()) {
121
encName.resetTag(DerValue.tag_IA5String);
122
name = new X400Address(encName);
123
} else {
124
throw new IOException("Invalid encoding of X400Address name");
125
}
126
break;
127
128
case GeneralNameInterface.NAME_URI:
129
if (encName.isContextSpecific() && !encName.isConstructed()) {
130
encName.resetTag(DerValue.tag_IA5String);
131
name = (nameConstraint ? URIName.nameConstraint(encName) :
132
new URIName(encName));
133
} else {
134
throw new IOException("Invalid encoding of URI");
135
}
136
break;
137
138
case GeneralNameInterface.NAME_IP:
139
if (encName.isContextSpecific() && !encName.isConstructed()) {
140
encName.resetTag(DerValue.tag_OctetString);
141
name = new IPAddressName(encName);
142
} else {
143
throw new IOException("Invalid encoding of IP address");
144
}
145
break;
146
147
case GeneralNameInterface.NAME_OID:
148
if (encName.isContextSpecific() && !encName.isConstructed()) {
149
encName.resetTag(DerValue.tag_ObjectId);
150
name = new OIDName(encName);
151
} else {
152
throw new IOException("Invalid encoding of OID name");
153
}
154
break;
155
156
case GeneralNameInterface.NAME_DIRECTORY:
157
if (encName.isContextSpecific() && encName.isConstructed()) {
158
name = new X500Name(encName.getData());
159
} else {
160
throw new IOException("Invalid encoding of Directory name");
161
}
162
break;
163
164
case GeneralNameInterface.NAME_EDI:
165
if (encName.isContextSpecific() && encName.isConstructed()) {
166
encName.resetTag(DerValue.tag_Sequence);
167
name = new EDIPartyName(encName);
168
} else {
169
throw new IOException("Invalid encoding of EDI name");
170
}
171
break;
172
173
default:
174
throw new IOException("Unrecognized GeneralName tag, ("
175
+ tag +")");
176
}
177
}
178
179
/**
180
* Return the type of the general name.
181
*/
182
public int getType() {
183
return name.getType();
184
}
185
186
/**
187
* Return the GeneralNameInterface name.
188
*/
189
public GeneralNameInterface getName() {
190
//XXXX May want to consider cloning this
191
return name;
192
}
193
194
/**
195
* Return the name as user readable string
196
*/
197
public String toString() {
198
return name.toString();
199
}
200
201
/**
202
* Compare this GeneralName with another
203
*
204
* @param other GeneralName to compare to this
205
* @return true if match
206
*/
207
public boolean equals(Object other) {
208
if (this == other) {
209
return true;
210
}
211
if (!(other instanceof GeneralName))
212
return false;
213
GeneralNameInterface otherGNI = ((GeneralName)other).name;
214
try {
215
return name.constrains(otherGNI) == GeneralNameInterface.NAME_MATCH;
216
} catch (UnsupportedOperationException ioe) {
217
return false;
218
}
219
}
220
221
/**
222
* Returns the hash code for this GeneralName.
223
*
224
* @return a hash code value.
225
*/
226
public int hashCode() {
227
return name.hashCode();
228
}
229
230
/**
231
* Encode the name to the specified DerOutputStream.
232
*
233
* @param out the DerOutputStream to encode the GeneralName to.
234
* @exception IOException on encoding errors.
235
*/
236
public void encode(DerOutputStream out) throws IOException {
237
DerOutputStream tmp = new DerOutputStream();
238
name.encode(tmp);
239
int nameType = name.getType();
240
if (nameType == GeneralNameInterface.NAME_ANY ||
241
nameType == GeneralNameInterface.NAME_X400 ||
242
nameType == GeneralNameInterface.NAME_EDI) {
243
244
// implicit, constructed form
245
out.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
246
true, (byte)nameType), tmp);
247
} else if (nameType == GeneralNameInterface.NAME_DIRECTORY) {
248
// explicit, constructed form since underlying tag is CHOICE
249
// (see X.680 section 30.6, part c)
250
out.write(DerValue.createTag(DerValue.TAG_CONTEXT,
251
true, (byte)nameType), tmp);
252
} else {
253
// implicit, primitive form
254
out.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
255
false, (byte)nameType), tmp);
256
}
257
}
258
}
259
260