Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/net/ssl/SNIServerName.java
38918 views
/*1* Copyright (c) 2012, 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;2829/**30* Instances of this class represent a server name in a Server Name31* Indication (SNI) extension.32* <P>33* The SNI extension is a feature that extends the SSL/TLS protocols to34* indicate what server name the client is attempting to connect to during35* handshaking. See section 3, "Server Name Indication", of <A36* HREF="http://www.ietf.org/rfc/rfc6066.txt">TLS Extensions (RFC 6066)</A>.37* <P>38* {@code SNIServerName} objects are immutable. Subclasses should not provide39* methods that can change the state of an instance once it has been created.40*41* @see SSLParameters#getServerNames()42* @see SSLParameters#setServerNames(List)43*44* @since 1.845*/46public abstract class SNIServerName {4748// the type of the server name49private final int type;5051// the encoded value of the server name52private final byte[] encoded;5354// the hex digitals55private static final char[] HEXES = "0123456789ABCDEF".toCharArray();5657/**58* Creates an {@code SNIServerName} using the specified name type and59* encoded value.60* <P>61* Note that the {@code encoded} byte array is cloned to protect against62* subsequent modification.63*64* @param type65* the type of the server name66* @param encoded67* the encoded value of the server name68*69* @throws IllegalArgumentException if {@code type} is not in the range70* of 0 to 255, inclusive.71* @throws NullPointerException if {@code encoded} is null72*/73protected SNIServerName(int type, byte[] encoded) {74if (type < 0) {75throw new IllegalArgumentException(76"Server name type cannot be less than zero");77} else if (type > 255) {78throw new IllegalArgumentException(79"Server name type cannot be greater than 255");80}81this.type = type;8283if (encoded == null) {84throw new NullPointerException(85"Server name encoded value cannot be null");86}87this.encoded = encoded.clone();88}899091/**92* Returns the name type of this server name.93*94* @return the name type of this server name95*/96public final int getType() {97return type;98}99100/**101* Returns a copy of the encoded server name value of this server name.102*103* @return a copy of the encoded server name value of this server name104*/105public final byte[] getEncoded() {106return encoded.clone();107}108109/**110* Indicates whether some other object is "equal to" this server name.111*112* @return true if, and only if, {@code other} is of the same class113* of this object, and has the same name type and114* encoded value as this server name.115*/116@Override117public boolean equals(Object other) {118if (this == other) {119return true;120}121122if (this.getClass() != other.getClass()) {123return false;124}125126SNIServerName that = (SNIServerName)other;127return (this.type == that.type) &&128Arrays.equals(this.encoded, that.encoded);129}130131/**132* Returns a hash code value for this server name.133* <P>134* The hash code value is generated using the name type and encoded135* value of this server name.136*137* @return a hash code value for this server name.138*/139@Override140public int hashCode() {141int result = 17; // 17/31: prime number to decrease collisions142result = 31 * result + type;143result = 31 * result + Arrays.hashCode(encoded);144145return result;146}147148/**149* Returns a string representation of this server name, including the server150* name type and the encoded server name value in this151* {@code SNIServerName} object.152* <P>153* The exact details of the representation are unspecified and subject154* to change, but the following may be regarded as typical:155* <pre>156* "type={@literal <name type>}, value={@literal <name value>}"157* </pre>158* <P>159* In this class, the format of "{@literal <name type>}" is160* "[LITERAL] (INTEGER)", where the optional "LITERAL" is the literal161* name, and INTEGER is the integer value of the name type. The format162* of "{@literal <name value>}" is "XX:...:XX", where "XX" is the163* hexadecimal digit representation of a byte value. For example, a164* returned value of an pseudo server name may look like:165* <pre>166* "type=(31), value=77:77:77:2E:65:78:61:6D:70:6C:65:2E:63:6E"167* </pre>168* or169* <pre>170* "type=host_name (0), value=77:77:77:2E:65:78:61:6D:70:6C:65:2E:63:6E"171* </pre>172*173* <P>174* Please NOTE that the exact details of the representation are unspecified175* and subject to change, and subclasses may override the method with176* their own formats.177*178* @return a string representation of this server name179*/180@Override181public String toString() {182if (type == StandardConstants.SNI_HOST_NAME) {183return "type=host_name (0), value=" + toHexString(encoded);184} else {185return "type=(" + type + "), value=" + toHexString(encoded);186}187}188189// convert byte array to hex string190private static String toHexString(byte[] bytes) {191if (bytes.length == 0) {192return "(empty)";193}194195StringBuilder sb = new StringBuilder(bytes.length * 3 - 1);196boolean isInitial = true;197for (byte b : bytes) {198if (isInitial) {199isInitial = false;200} else {201sb.append(':');202}203204int k = b & 0xFF;205sb.append(HEXES[k >>> 4]);206sb.append(HEXES[k & 0xF]);207}208209return sb.toString();210}211}212213214215