src/java.base/share/classes/javax/net/ssl/SNIServerName.java
author darcy
Thu, 29 Aug 2019 16:31:34 -0700
changeset 57956 e0b8b019d2f5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8229997: Apply java.io.Serial annotations in java.base Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
     1
/*
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
     4
 *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    10
 *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    15
 * accompanied this code).
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    16
 *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    20
 *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    23
 * questions.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    24
 */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    25
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    26
package javax.net.ssl;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    27
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    28
import java.util.Arrays;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    29
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    30
/**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    31
 * Instances of this class represent a server name in a Server Name
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    32
 * Indication (SNI) extension.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    33
 * <P>
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    34
 * The SNI extension is a feature that extends the SSL/TLS/DTLS protocols to
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    35
 * indicate what server name the client is attempting to connect to during
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    36
 * handshaking.  See section 3, "Server Name Indication", of <A
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    37
 * HREF="http://www.ietf.org/rfc/rfc6066.txt">TLS Extensions (RFC 6066)</A>.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    38
 * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    39
 * {@code SNIServerName} objects are immutable.  Subclasses should not provide
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    40
 * methods that can change the state of an instance once it has been created.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    41
 *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    42
 * @see SSLParameters#getServerNames()
14663
49b7de969579 8001751: Javadoc warnings in JSSE code
xuelei
parents: 14194
diff changeset
    43
 * @see SSLParameters#setServerNames(List)
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    44
 *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    45
 * @since 1.8
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    46
 */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    47
public abstract class SNIServerName {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    48
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    49
    // the type of the server name
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    50
    private final int type;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    51
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    52
    // the encoded value of the server name
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    53
    private final byte[] encoded;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    54
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    55
    // the hex digitals
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    56
    private static final char[] HEXES = "0123456789ABCDEF".toCharArray();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    57
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    58
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    59
     * Creates an {@code SNIServerName} using the specified name type and
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    60
     * encoded value.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    61
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    62
     * Note that the {@code encoded} byte array is cloned to protect against
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    63
     * subsequent modification.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    64
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    65
     * @param  type
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    66
     *         the type of the server name
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    67
     * @param  encoded
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    68
     *         the encoded value of the server name
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    69
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    70
     * @throws IllegalArgumentException if {@code type} is not in the range
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    71
     *         of 0 to 255, inclusive.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    72
     * @throws NullPointerException if {@code encoded} is null
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    73
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    74
    protected SNIServerName(int type, byte[] encoded) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    75
        if (type < 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    76
            throw new IllegalArgumentException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    77
                "Server name type cannot be less than zero");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    78
        } else if (type > 255) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    79
            throw new IllegalArgumentException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    80
                "Server name type cannot be greater than 255");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    81
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    82
        this.type = type;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    83
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    84
        if (encoded == null) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    85
            throw new NullPointerException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    86
                "Server name encoded value cannot be null");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    87
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    88
        this.encoded = encoded.clone();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    89
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    90
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    91
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    92
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    93
     * Returns the name type of this server name.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    94
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    95
     * @return the name type of this server name
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    96
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    97
    public final int getType() {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    98
        return type;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    99
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   100
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   101
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   102
     * Returns a copy of the encoded server name value of this server name.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   103
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   104
     * @return a copy of the encoded server name value of this server name
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   105
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   106
    public final byte[] getEncoded() {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   107
        return encoded.clone();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   108
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   109
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   110
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   111
     * Indicates whether some other object is "equal to" this server name.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   112
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   113
     * @return true if, and only if, {@code other} is of the same class
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   114
     *         of this object, and has the same name type and
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   115
     *         encoded value as this server name.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   116
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   117
    @Override
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   118
    public boolean equals(Object other) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   119
        if (this == other) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   120
            return true;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   121
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   122
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   123
        if (this.getClass() != other.getClass()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   124
            return false;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   125
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   126
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   127
        SNIServerName that = (SNIServerName)other;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   128
        return (this.type == that.type) &&
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   129
                    Arrays.equals(this.encoded, that.encoded);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   130
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   131
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   132
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   133
     * Returns a hash code value for this server name.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   134
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   135
     * The hash code value is generated using the name type and encoded
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   136
     * value of this server name.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   137
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   138
     * @return a hash code value for this server name.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   139
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   140
    @Override
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   141
    public int hashCode() {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   142
        int result = 17;    // 17/31: prime number to decrease collisions
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   143
        result = 31 * result + type;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   144
        result = 31 * result + Arrays.hashCode(encoded);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   145
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   146
        return result;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   147
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   148
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   149
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   150
     * Returns a string representation of this server name, including the server
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   151
     * name type and the encoded server name value in this
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   152
     * {@code SNIServerName} object.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   153
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   154
     * The exact details of the representation are unspecified and subject
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   155
     * to change, but the following may be regarded as typical:
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   156
     * <pre>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   157
     *     "type={@literal <name type>}, value={@literal <name value>}"
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   158
     * </pre>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   159
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   160
     * In this class, the format of "{@literal <name type>}" is
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   161
     * "[LITERAL] (INTEGER)", where the optional "LITERAL" is the literal
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   162
     * name, and INTEGER is the integer value of the name type.  The format
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   163
     * of "{@literal <name value>}" is "XX:...:XX", where "XX" is the
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   164
     * hexadecimal digit representation of a byte value. For example, a
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   165
     * returned value of an pseudo server name may look like:
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   166
     * <pre>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   167
     *     "type=(31), value=77:77:77:2E:65:78:61:6D:70:6C:65:2E:63:6E"
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   168
     * </pre>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   169
     * or
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   170
     * <pre>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   171
     *     "type=host_name (0), value=77:77:77:2E:65:78:61:6D:70:6C:65:2E:63:6E"
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   172
     * </pre>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   173
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   174
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   175
     * Please NOTE that the exact details of the representation are unspecified
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   176
     * and subject to change, and subclasses may override the method with
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   177
     * their own formats.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   178
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   179
     * @return a string representation of this server name
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   180
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   181
    @Override
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   182
    public String toString() {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   183
        if (type == StandardConstants.SNI_HOST_NAME) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   184
            return "type=host_name (0), value=" + toHexString(encoded);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   185
        } else {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   186
            return "type=(" + type + "), value=" + toHexString(encoded);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   187
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   188
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   189
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   190
    // convert byte array to hex string
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   191
    private static String toHexString(byte[] bytes) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   192
        if (bytes.length == 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   193
            return "(empty)";
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   194
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   195
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   196
        StringBuilder sb = new StringBuilder(bytes.length * 3 - 1);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   197
        boolean isInitial = true;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   198
        for (byte b : bytes) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   199
            if (isInitial) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   200
                isInitial = false;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   201
            } else {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   202
                sb.append(':');
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   203
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   204
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   205
            int k = b & 0xFF;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   206
            sb.append(HEXES[k >>> 4]);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   207
            sb.append(HEXES[k & 0xF]);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   208
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   209
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   210
        return sb.toString();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   211
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   212
}
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   213