Path: blob/master/src/java.base/share/classes/javax/net/ssl/SNIServerName.java
41159 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.net.ssl;2627import java.util.Arrays;28import java.util.HexFormat;2930/**31* Instances of this class represent a server name in a Server Name32* Indication (SNI) extension.33* <P>34* The SNI extension is a feature that extends the SSL/TLS/DTLS protocols to35* indicate what server name the client is attempting to connect to during36* handshaking. See section 3, "Server Name Indication", of <A37* HREF="http://www.ietf.org/rfc/rfc6066.txt">TLS Extensions (RFC 6066)</A>.38* <P>39* {@code SNIServerName} objects are immutable. Subclasses should not provide40* methods that can change the state of an instance once it has been created.41*42* @see SSLParameters#getServerNames()43* @see SSLParameters#setServerNames(List)44*45* @since 1.846*/47public abstract class SNIServerName {4849// the type of the server name50private final int type;5152// the encoded value of the server name53private final byte[] encoded;5455/**56* Creates an {@code SNIServerName} using the specified name type and57* encoded value.58* <P>59* Note that the {@code encoded} byte array is cloned to protect against60* subsequent modification.61*62* @param type63* the type of the server name64* @param encoded65* the encoded value of the server name66*67* @throws IllegalArgumentException if {@code type} is not in the range68* of 0 to 255, inclusive.69* @throws NullPointerException if {@code encoded} is null70*/71protected SNIServerName(int type, byte[] encoded) {72if (type < 0) {73throw new IllegalArgumentException(74"Server name type cannot be less than zero");75} else if (type > 255) {76throw new IllegalArgumentException(77"Server name type cannot be greater than 255");78}79this.type = type;8081if (encoded == null) {82throw new NullPointerException(83"Server name encoded value cannot be null");84}85this.encoded = encoded.clone();86}878889/**90* Returns the name type of this server name.91*92* @return the name type of this server name93*/94public final int getType() {95return type;96}9798/**99* Returns a copy of the encoded server name value of this server name.100*101* @return a copy of the encoded server name value of this server name102*/103public final byte[] getEncoded() {104return encoded.clone();105}106107/**108* Indicates whether some other object is "equal to" this server name.109*110* @return true if, and only if, {@code other} is of the same class111* of this object, and has the same name type and112* encoded value as this server name.113*/114@Override115public boolean equals(Object other) {116if (this == other) {117return true;118}119120if (this.getClass() != other.getClass()) {121return false;122}123124SNIServerName that = (SNIServerName)other;125return (this.type == that.type) &&126Arrays.equals(this.encoded, that.encoded);127}128129/**130* Returns a hash code value for this server name.131* <P>132* The hash code value is generated using the name type and encoded133* value of this server name.134*135* @return a hash code value for this server name.136*/137@Override138public int hashCode() {139int result = 17; // 17/31: prime number to decrease collisions140result = 31 * result + type;141result = 31 * result + Arrays.hashCode(encoded);142143return result;144}145146/**147* Returns a string representation of this server name, including the server148* name type and the encoded server name value in this149* {@code SNIServerName} object.150* <P>151* The exact details of the representation are unspecified and subject152* to change, but the following may be regarded as typical:153* <pre>154* "type={@literal <name type>}, value={@literal <name value>}"155* </pre>156* <P>157* In this class, the format of "{@literal <name type>}" is158* "[LITERAL] (INTEGER)", where the optional "LITERAL" is the literal159* name, and INTEGER is the integer value of the name type. The format160* of "{@literal <name value>}" is "XX:...:XX", where "XX" is the161* hexadecimal digit representation of a byte value. For example, a162* returned value of an pseudo server name may look like:163* <pre>164* "type=(31), value=77:77:77:2E:65:78:61:6D:70:6C:65:2E:63:6E"165* </pre>166* or167* <pre>168* "type=host_name (0), value=77:77:77:2E:65:78:61:6D:70:6C:65:2E:63:6E"169* </pre>170*171* <P>172* Please NOTE that the exact details of the representation are unspecified173* and subject to change, and subclasses may override the method with174* their own formats.175*176* @return a string representation of this server name177*/178@Override179public String toString() {180if (type == StandardConstants.SNI_HOST_NAME) {181return "type=host_name (0), value=" + toHexString(encoded);182} else {183return "type=(" + type + "), value=" + toHexString(encoded);184}185}186187// convert byte array to hex string188private static String toHexString(byte[] bytes) {189if (bytes.length == 0) {190return "(empty)";191}192return HexFormat.ofDelimiter(":").withUpperCase().formatHex(bytes);193}194}195196197198