1 | /*
|
---|
2 | * Copyright (c) 1997, 2010, 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 | package org.openstreetmap.josm.io.remotecontrol;
|
---|
26 |
|
---|
27 | import java.io.IOException;
|
---|
28 | import java.util.Locale;
|
---|
29 |
|
---|
30 | import sun.security.util.DerOutputStream;
|
---|
31 | import sun.security.x509.GeneralNameInterface;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * This class implements the DNSName as required by the GeneralNames
|
---|
35 | * ASN.1 object.
|
---|
36 | * <p>
|
---|
37 | * [RFC2459] When the subjectAltName extension contains a domain name service
|
---|
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 RFC
|
---|
40 | * 1034 [RFC 1034]. Note that while upper and lower case letters are
|
---|
41 | * allowed in domain names, no signifigance is attached to the case. In
|
---|
42 | * addition, while the string " " is a legal domain name, subjectAltName
|
---|
43 | * extensions with a dNSName " " are not permitted. Finally, the use of
|
---|
44 | * the DNS representation for Internet mail addresses (wpolk.nist.gov
|
---|
45 | * instead of wpolk@nist.gov) is not permitted; such identities are to
|
---|
46 | * be encoded as rfc822Name.
|
---|
47 | *
|
---|
48 | * This class has been copied from OpenJDK7u repository and modified
|
---|
49 | * in order to fix Java bug 8016345:
|
---|
50 | * https://bugs.openjdk.java.net/browse/JDK-8016345
|
---|
51 | *
|
---|
52 | * It can be deleted after a migration to a Java release fixing this bug.
|
---|
53 | * <p>
|
---|
54 | * @author Amit Kapoor
|
---|
55 | * @author Hemma Prafullchandra
|
---|
56 | * @author JOSM developers
|
---|
57 | * @since 7347
|
---|
58 | */
|
---|
59 | public final class DNSName implements GeneralNameInterface {
|
---|
60 | private final String name;
|
---|
61 |
|
---|
62 | private static final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
---|
63 | private static final String digitsAndHyphen = "0123456789-";
|
---|
64 | private static final String alphaDigitsAndHyphen = alpha + digitsAndHyphen;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Create the DNSName object with the specified name.
|
---|
68 | *
|
---|
69 | * @param name the DNSName.
|
---|
70 | * @throws IOException if the name is not a valid DNSName subjectAltName
|
---|
71 | */
|
---|
72 | public DNSName(String name) throws IOException {
|
---|
73 | if (name == null || name.isEmpty())
|
---|
74 | throw new IOException("DNS name must not be null");
|
---|
75 | if (name.indexOf(' ') != -1)
|
---|
76 | throw new IOException("DNS names or NameConstraints with blank components are not permitted");
|
---|
77 | if (name.charAt(0) == '.' || name.charAt(name.length() -1) == '.')
|
---|
78 | throw new IOException("DNS names or NameConstraints may not begin or end with a .");
|
---|
79 | //Name will consist of label components separated by "."
|
---|
80 | //startIndex is the index of the first character of a component
|
---|
81 | //endIndex is the index of the last character of a component plus 1
|
---|
82 | for (int endIndex, startIndex = 0; startIndex < name.length(); startIndex = endIndex+1) {
|
---|
83 | endIndex = name.indexOf('.', startIndex);
|
---|
84 | if (endIndex < 0) {
|
---|
85 | endIndex = name.length();
|
---|
86 | }
|
---|
87 | if ((endIndex-startIndex) < 1)
|
---|
88 | throw new IOException("DNSName SubjectAltNames with empty components are not permitted");
|
---|
89 |
|
---|
90 | //nonStartIndex: index for characters in the component beyond the first one
|
---|
91 | for (int nonStartIndex = startIndex+1; nonStartIndex < endIndex; nonStartIndex++) {
|
---|
92 | char x = name.charAt(nonStartIndex);
|
---|
93 | if ((alphaDigitsAndHyphen).indexOf(x) < 0)
|
---|
94 | throw new IOException("DNSName components must consist of letters, digits, and hyphens");
|
---|
95 | }
|
---|
96 | }
|
---|
97 | this.name = name;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Return the type of the GeneralName.
|
---|
102 | * @return the type of the GeneralName
|
---|
103 | */
|
---|
104 | @Override
|
---|
105 | public int getType() {
|
---|
106 | return GeneralNameInterface.NAME_DNS;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Return the actual name value of the GeneralName.
|
---|
111 | * @return the actual name value of the GeneralName
|
---|
112 | */
|
---|
113 | public String getName() {
|
---|
114 | return name;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Encode the DNS name into the DerOutputStream.
|
---|
119 | *
|
---|
120 | * @param out the DER stream to encode the DNSName to.
|
---|
121 | * @throws IOException on encoding errors.
|
---|
122 | */
|
---|
123 | @Override
|
---|
124 | public void encode(DerOutputStream out) throws IOException {
|
---|
125 | out.putIA5String(name);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Convert the name into user readable string.
|
---|
130 | */
|
---|
131 | @Override
|
---|
132 | public String toString() {
|
---|
133 | return "DNSName: " + name;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Compares this name with another, for equality.
|
---|
138 | *
|
---|
139 | * @return true iff the names are equivalent
|
---|
140 | * according to RFC2459.
|
---|
141 | */
|
---|
142 | @Override
|
---|
143 | public boolean equals(Object obj) {
|
---|
144 | if (this == obj)
|
---|
145 | return true;
|
---|
146 |
|
---|
147 | if (!(obj instanceof DNSName))
|
---|
148 | return false;
|
---|
149 |
|
---|
150 | DNSName other = (DNSName) obj;
|
---|
151 |
|
---|
152 | // RFC2459 mandates that these names are
|
---|
153 | // not case-sensitive
|
---|
154 | return name.equalsIgnoreCase(other.name);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Returns the hash code value for this object.
|
---|
159 | *
|
---|
160 | * @return a hash code value for this object.
|
---|
161 | */
|
---|
162 | @Override
|
---|
163 | public int hashCode() {
|
---|
164 | return name.toUpperCase(Locale.ENGLISH).hashCode();
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Return type of constraint inputName places on this name:<ul>
|
---|
169 | * <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain).
|
---|
170 | * <li>NAME_MATCH = 0: input name matches name.
|
---|
171 | * <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming subtree)
|
---|
172 | * <li>NAME_WIDENS = 2: input name widens name (is higher in the naming subtree)
|
---|
173 | * <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type.
|
---|
174 | * </ul>. These results are used in checking NameConstraints during
|
---|
175 | * certification path verification.
|
---|
176 | * <p>
|
---|
177 | * RFC2459: DNS name restrictions are expressed as foo.bar.com. Any subdomain
|
---|
178 | * satisfies the name constraint. For example, www.foo.bar.com would
|
---|
179 | * satisfy the constraint but bigfoo.bar.com would not.
|
---|
180 | * <p>
|
---|
181 | * draft-ietf-pkix-new-part1-00.txt: DNS name restrictions are expressed as foo.bar.com.
|
---|
182 | * Any DNS name that
|
---|
183 | * can be constructed by simply adding to the left hand side of the name
|
---|
184 | * satisfies the name constraint. For example, www.foo.bar.com would
|
---|
185 | * satisfy the constraint but foo1.bar.com would not.
|
---|
186 | * <p>
|
---|
187 | * RFC1034: By convention, domain names can be stored with arbitrary case, but
|
---|
188 | * domain name comparisons for all present domain functions are done in a
|
---|
189 | * case-insensitive manner, assuming an ASCII character set, and a high
|
---|
190 | * order zero bit.
|
---|
191 | * <p>
|
---|
192 | * @param inputName to be checked for being constrained
|
---|
193 | * @return constraint type above
|
---|
194 | * @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
|
---|
195 | * not supported for this name type.
|
---|
196 | */
|
---|
197 | @Override
|
---|
198 | public int constrains(GeneralNameInterface inputName) {
|
---|
199 | int constraintType;
|
---|
200 | if (inputName == null)
|
---|
201 | constraintType = NAME_DIFF_TYPE;
|
---|
202 | else if (inputName.getType() != NAME_DNS)
|
---|
203 | constraintType = NAME_DIFF_TYPE;
|
---|
204 | else {
|
---|
205 | String inName =
|
---|
206 | (((DNSName) inputName).getName()).toLowerCase(Locale.ENGLISH);
|
---|
207 | String thisName = name.toLowerCase(Locale.ENGLISH);
|
---|
208 | if (inName.equals(thisName))
|
---|
209 | constraintType = NAME_MATCH;
|
---|
210 | else if (thisName.endsWith(inName)) {
|
---|
211 | int inNdx = thisName.lastIndexOf(inName);
|
---|
212 | if (thisName.charAt(inNdx-1) == '.')
|
---|
213 | constraintType = NAME_WIDENS;
|
---|
214 | else
|
---|
215 | constraintType = NAME_SAME_TYPE;
|
---|
216 | } else if (inName.endsWith(thisName)) {
|
---|
217 | int ndx = inName.lastIndexOf(thisName);
|
---|
218 | if (inName.charAt(ndx-1) == '.')
|
---|
219 | constraintType = NAME_NARROWS;
|
---|
220 | else
|
---|
221 | constraintType = NAME_SAME_TYPE;
|
---|
222 | } else {
|
---|
223 | constraintType = NAME_SAME_TYPE;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | return constraintType;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Return subtree depth of this name for purposes of determining
|
---|
231 | * NameConstraints minimum and maximum bounds and for calculating
|
---|
232 | * path lengths in name subtrees.
|
---|
233 | *
|
---|
234 | * @return distance of name from root
|
---|
235 | * @throws UnsupportedOperationException if not supported for this name type
|
---|
236 | */
|
---|
237 | @Override
|
---|
238 | public int subtreeDepth() {
|
---|
239 | String subtree = name;
|
---|
240 | int i = 1;
|
---|
241 |
|
---|
242 | /* count dots */
|
---|
243 | for (; subtree.lastIndexOf('.') >= 0; i++) {
|
---|
244 | subtree = subtree.substring(0, subtree.lastIndexOf('.'));
|
---|
245 | }
|
---|
246 |
|
---|
247 | return i;
|
---|
248 | }
|
---|
249 | }
|
---|