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/DNSName.java
41159 views
1
/*
2
* Copyright (c) 1997, 2020, 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
import java.util.Locale;
30
31
import sun.security.util.*;
32
33
/**
34
* This class implements the DNSName as required by the GeneralNames
35
* ASN.1 object.
36
* <p>
37
* [RFC5280] When the subjectAltName extension contains a domain name system
38
* label, the domain name MUST be stored in the dNSName (an IA5String).
39
* The name MUST be in the "preferred name syntax", as specified by
40
* Section 3.5 of [RFC1034] and as modified by Section 2.1 of
41
* [RFC1123]. Note that while uppercase and lowercase letters are
42
* allowed in domain names, no significance is attached to the case. In
43
* addition, while the string " " is a legal domain name, subjectAltName
44
* extensions with a dNSName of " " MUST NOT be used. Finally, the use
45
* of the DNS representation for Internet mail addresses
46
* (subscriber.example.com instead of [email protected]) MUST NOT
47
* be used; such identities are to be encoded as rfc822Name.
48
*
49
* @author Amit Kapoor
50
* @author Hemma Prafullchandra
51
*/
52
public class DNSName implements GeneralNameInterface {
53
private String name;
54
55
private static final String alphaDigits =
56
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
57
58
/**
59
* Create the DNSName object from the passed encoded Der value.
60
*
61
* @param derValue the encoded DER DNSName.
62
* @exception IOException on error.
63
*/
64
public DNSName(DerValue derValue) throws IOException {
65
name = derValue.getIA5String();
66
}
67
68
/**
69
* Create the DNSName object with the specified name.
70
*
71
* @param name the DNSName.
72
* @param allowWildcard the flag for wildcard checking.
73
* @throws IOException if the name is not a valid DNSName
74
*/
75
public DNSName(String name, boolean allowWildcard) throws IOException {
76
if (name == null || name.isEmpty())
77
throw new IOException("DNSName must not be null or empty");
78
if (name.contains(" "))
79
throw new IOException("DNSName with blank components is not permitted");
80
if (name.startsWith(".") || name.endsWith("."))
81
throw new IOException("DNSName may not begin or end with a .");
82
/*
83
* Name will consist of label components separated by "."
84
* startIndex is the index of the first character of a component
85
* endIndex is the index of the last character of a component plus 1
86
*/
87
for (int endIndex,startIndex = 0; startIndex < name.length(); startIndex = endIndex+1) {
88
endIndex = name.indexOf('.', startIndex);
89
if (endIndex < 0) {
90
endIndex = name.length();
91
}
92
if (endIndex - startIndex < 1)
93
throw new IOException("DNSName with empty components are not permitted");
94
95
if (allowWildcard) {
96
// RFC 1123: DNSName components must begin with a letter or digit
97
// or RFC 4592: the first component of a DNSName can have only a wildcard
98
// character * (asterisk), i.e. *.example.com. Asterisks at other components
99
// will not be allowed as a wildcard.
100
if (alphaDigits.indexOf(name.charAt(startIndex)) < 0) {
101
// Checking to make sure the wildcard only appears in the first component,
102
// and it has to be at least 3-char long with the form of *.[alphaDigit]
103
if ((name.length() < 3) || (name.indexOf('*', 0) != 0) ||
104
(name.charAt(startIndex+1) != '.') ||
105
(alphaDigits.indexOf(name.charAt(startIndex+2)) < 0))
106
throw new IOException("DNSName components must begin with a letter, digit, "
107
+ "or the first component can have only a wildcard character *");
108
}
109
} else {
110
// RFC 1123: DNSName components must begin with a letter or digit
111
if (alphaDigits.indexOf(name.charAt(startIndex)) < 0)
112
throw new IOException("DNSName components must begin with a letter or digit");
113
}
114
115
//nonStartIndex: index for characters in the component beyond the first one
116
for (int nonStartIndex=startIndex+1; nonStartIndex < endIndex; nonStartIndex++) {
117
char x = name.charAt(nonStartIndex);
118
if ((alphaDigits).indexOf(x) < 0 && x != '-')
119
throw new IOException("DNSName components must consist of letters, digits, and hyphens");
120
}
121
}
122
this.name = name;
123
}
124
125
/**
126
* Create the DNSName object with the specified name.
127
*
128
* @param name the DNSName.
129
* @throws IOException if the name is not a valid DNSName
130
*/
131
public DNSName(String name) throws IOException {
132
this(name, false);
133
}
134
135
/**
136
* Return the type of the GeneralName.
137
*/
138
public int getType() {
139
return (GeneralNameInterface.NAME_DNS);
140
}
141
142
/**
143
* Return the actual name value of the GeneralName.
144
*/
145
public String getName() {
146
return name;
147
}
148
149
/**
150
* Encode the DNSName into the DerOutputStream.
151
*
152
* @param out the DER stream to encode the DNSName to.
153
* @exception IOException on encoding errors.
154
*/
155
public void encode(DerOutputStream out) throws IOException {
156
out.putIA5String(name);
157
}
158
159
/**
160
* Convert the name into user readable string.
161
*/
162
public String toString() {
163
return ("DNSName: " + name);
164
}
165
166
/**
167
* Compares this name with another, for equality.
168
*
169
* @return true iff the names are equivalent
170
* according to RFC5280.
171
*/
172
public boolean equals(Object obj) {
173
if (this == obj)
174
return true;
175
176
if (!(obj instanceof DNSName))
177
return false;
178
179
DNSName other = (DNSName)obj;
180
181
// RFC5280 mandates that these names are
182
// not case-sensitive
183
return name.equalsIgnoreCase(other.name);
184
}
185
186
/**
187
* Returns the hash code value for this object.
188
*
189
* @return a hash code value for this object.
190
*/
191
public int hashCode() {
192
return name.toUpperCase(Locale.ENGLISH).hashCode();
193
}
194
195
/**
196
* Return type of constraint inputName places on this name:<ul>
197
* <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain).
198
* <li>NAME_MATCH = 0: input name matches name.
199
* <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming subtree)
200
* <li>NAME_WIDENS = 2: input name widens name (is higher in the naming subtree)
201
* <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type.
202
* </ul>. These results are used in checking NameConstraints during
203
* certification path verification.
204
* <p>
205
* RFC5280: DNS name restrictions are expressed as host.example.com.
206
* Any DNS name that can be constructed by simply adding zero or more
207
* labels to the left-hand side of the name satisfies the name constraint.
208
* For example, www.host.example.com would satisfy the constraint but
209
* host1.example.com would not.
210
* <p>
211
* RFC 5280: DNSName restrictions are expressed as foo.bar.com.
212
* Any DNSName that
213
* can be constructed by simply adding to the left hand side of the name
214
* satisfies the name constraint. For example, www.foo.bar.com would
215
* satisfy the constraint but foo1.bar.com would not.
216
* <p>
217
* RFC1034: By convention, domain names can be stored with arbitrary case, but
218
* domain name comparisons for all present domain functions are done in a
219
* case-insensitive manner, assuming an ASCII character set, and a high
220
* order zero bit.
221
*
222
* @param inputName to be checked for being constrained
223
* @return constraint type above
224
* @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
225
* not supported for this name type.
226
*/
227
public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {
228
int constraintType;
229
if (inputName == null)
230
constraintType = NAME_DIFF_TYPE;
231
else if (inputName.getType() != NAME_DNS)
232
constraintType = NAME_DIFF_TYPE;
233
else {
234
String inName =
235
(((DNSName)inputName).getName()).toLowerCase(Locale.ENGLISH);
236
String thisName = name.toLowerCase(Locale.ENGLISH);
237
if (inName.equals(thisName))
238
constraintType = NAME_MATCH;
239
else if (thisName.endsWith(inName)) {
240
int inNdx = thisName.lastIndexOf(inName);
241
if (thisName.charAt(inNdx-1) == '.' )
242
constraintType = NAME_WIDENS;
243
else
244
constraintType = NAME_SAME_TYPE;
245
} else if (inName.endsWith(thisName)) {
246
int ndx = inName.lastIndexOf(thisName);
247
if (inName.charAt(ndx-1) == '.' )
248
constraintType = NAME_NARROWS;
249
else
250
constraintType = NAME_SAME_TYPE;
251
} else {
252
constraintType = NAME_SAME_TYPE;
253
}
254
}
255
return constraintType;
256
}
257
258
/**
259
* Return subtree depth of this name for purposes of determining
260
* NameConstraints minimum and maximum bounds and for calculating
261
* path lengths in name subtrees.
262
*
263
* @return distance of name from root
264
* @throws UnsupportedOperationException if not supported for this name type
265
*/
266
public int subtreeDepth() throws UnsupportedOperationException {
267
// subtree depth is always at least 1
268
int sum = 1;
269
270
// count dots
271
for (int i = name.indexOf('.'); i >= 0; i = name.indexOf('.', i + 1)) {
272
++sum;
273
}
274
275
return sum;
276
}
277
278
}
279
280