source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/DNSNameFix.java@ 12253

Last change on this file since 12253 was 12253, checked in by Don-vip, 7 years ago

findbugs - NM_SAME_SIMPLE_NAME_AS_SUPERCLASS

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
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 */
25package org.openstreetmap.josm.io.remotecontrol;
26
27import java.io.IOException;
28import java.lang.reflect.Field;
29
30import org.openstreetmap.josm.tools.Utils;
31
32/**
33 * This class implements the DNSName as required by the GeneralNames
34 * ASN.1 object.
35 * <p>
36 * [RFC2459] When the subjectAltName extension contains a domain name service
37 * label, the domain name MUST be stored in the dNSName (an IA5String).
38 * The name MUST be in the "preferred name syntax," as specified by RFC
39 * 1034 [RFC 1034]. Note that while upper and lower case letters are
40 * allowed in domain names, no signifigance is attached to the case. In
41 * addition, while the string " " is a legal domain name, subjectAltName
42 * extensions with a dNSName " " are not permitted. Finally, the use of
43 * the DNS representation for Internet mail addresses (wpolk.nist.gov
44 * instead of wpolk@nist.gov) is not permitted; such identities are to
45 * be encoded as rfc822Name.
46 *
47 * This class has been copied from OpenJDK8u repository and modified
48 * in order to fix Java bug 8016345:
49 * https://bugs.openjdk.java.net/browse/JDK-8016345
50 *
51 * It can be deleted after a migration to a Java release fixing this bug:
52 * https://bugs.openjdk.java.net/browse/JDK-8054380
53 * <p>
54 * @author Amit Kapoor
55 * @author Hemma Prafullchandra
56 * @author JOSM developers
57 * @since 7347
58 */
59public final class DNSNameFix extends sun.security.x509.DNSName {
60
61 private static final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
62 private static final String digitsAndHyphen = "0123456789-";
63 private static final String alphaDigitsAndHyphen = alpha + digitsAndHyphen;
64
65 /**
66 * Create the DNSName object with the specified name.
67 *
68 * @param name the DNSName.
69 * @throws IOException if the name is not a valid DNSName subjectAltName
70 */
71 public DNSNameFix(String name) throws IOException {
72 super("fake");
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 try {
98 Field fName = getClass().getSuperclass().getDeclaredField("name");
99 Utils.setObjectsAccessible(fName);
100 fName.set(this, name);
101 } catch (ReflectiveOperationException | SecurityException e) {
102 throw new IOException(e);
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.