jdk/test/javax/net/ssl/templates/SSLExplorer.java
author sundar
Mon, 16 May 2016 14:50:43 +0530
changeset 37951 ce2744a0f1a7
parent 34687 d302ed125dc9
permissions -rw-r--r--
8156914: jlink API minor cleanups Reviewed-by: mchung
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
/*
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
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
import java.nio.ByteBuffer;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    27
import java.nio.BufferUnderflowException;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    28
import java.io.IOException;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    29
import javax.net.ssl.*;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    30
import java.util.*;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    31
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    32
/**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    33
 * Instances of this class acts as an explorer of the network data of an
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    34
 * SSL/TLS connection.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    35
 */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    36
public final class SSLExplorer {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    37
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    38
    // Private constructor prevents construction outside this class.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    39
    private SSLExplorer() {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    40
    }
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
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    43
     * The header size of TLS/SSL records.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    44
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    45
     * The value of this constant is {@value}.
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 final static int RECORD_HEADER_SIZE = 0x05;
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
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    50
     * Returns the required number of bytes in the {@code source}
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    51
     * {@link ByteBuffer} necessary to explore SSL/TLS connection.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    52
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    53
     * This method tries to parse as few bytes as possible from
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    54
     * {@code source} byte buffer to get the length of an
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    55
     * SSL/TLS record.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    56
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    57
     * This method accesses the {@code source} parameter in read-only
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    58
     * mode, and does not update the buffer's properties such as capacity,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    59
     * limit, position, and mark values.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    60
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    61
     * @param  source
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    62
     *         a {@link ByteBuffer} containing
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    63
     *         inbound or outbound network data for an SSL/TLS connection.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    64
     * @throws BufferUnderflowException if less than {@code RECORD_HEADER_SIZE}
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    65
     *         bytes remaining in {@code source}
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    66
     * @return the required size in byte to explore an SSL/TLS connection
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    67
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    68
    public final static int getRequiredSize(ByteBuffer source) {
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
        ByteBuffer input = source.duplicate();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    71
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    72
        // Do we have a complete header?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    73
        if (input.remaining() < RECORD_HEADER_SIZE) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    74
            throw new BufferUnderflowException();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    75
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    76
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    77
        // Is it a handshake message?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    78
        byte firstByte = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    79
        byte secondByte = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    80
        byte thirdByte = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    81
        if ((firstByte & 0x80) != 0 && thirdByte == 0x01) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    82
            // looks like a V2ClientHello
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    83
            // return (((firstByte & 0x7F) << 8) | (secondByte & 0xFF)) + 2;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    84
            return RECORD_HEADER_SIZE;   // Only need the header fields
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    85
        } else {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    86
            return (((input.get() & 0xFF) << 8) | (input.get() & 0xFF)) + 5;
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
    }
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
     * Returns the required number of bytes in the {@code source} byte array
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    92
     * necessary to explore SSL/TLS connection.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    93
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    94
     * This method tries to parse as few bytes as possible from
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    95
     * {@code source} byte array to get the length of an
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    96
     * SSL/TLS record.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    97
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    98
     * @param  source
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
    99
     *         a byte array containing inbound or outbound network data for
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   100
     *         an SSL/TLS connection.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   101
     * @param  offset
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   102
     *         the start offset in array {@code source} at which the
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   103
     *         network data is read from.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   104
     * @param  length
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   105
     *         the maximum number of bytes to read.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   106
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   107
     * @throws BufferUnderflowException if less than {@code RECORD_HEADER_SIZE}
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   108
     *         bytes remaining in {@code source}
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   109
     * @return the required size in byte to explore an SSL/TLS connection
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
    public final static int getRequiredSize(byte[] source,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   112
            int offset, int length) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   113
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   114
        ByteBuffer byteBuffer =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   115
            ByteBuffer.wrap(source, offset, length).asReadOnlyBuffer();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   116
        return getRequiredSize(byteBuffer);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   117
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   118
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   119
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   120
     * Launch and explore the security capabilities from byte buffer.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   121
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   122
     * This method tries to parse as few records as possible from
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   123
     * {@code source} byte buffer to get the {@link SSLCapabilities}
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   124
     * of an SSL/TLS connection.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   125
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   126
     * Please NOTE that this method must be called before any handshaking
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   127
     * occurs.  The behavior of this method is not defined in this release
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   128
     * if the handshake has begun, or has completed.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   129
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   130
     * This method accesses the {@code source} parameter in read-only
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   131
     * mode, and does not update the buffer's properties such as capacity,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   132
     * limit, position, and mark values.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   133
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   134
     * @param  source
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   135
     *         a {@link ByteBuffer} containing
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   136
     *         inbound or outbound network data for an SSL/TLS connection.
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
     * @throws IOException on network data error
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   139
     * @throws BufferUnderflowException if not enough source bytes available
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   140
     *         to make a complete exploration.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   141
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   142
     * @return the explored {@link SSLCapabilities} of the SSL/TLS
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   143
     *         connection
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   144
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   145
    public final static SSLCapabilities explore(ByteBuffer source)
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   146
            throws IOException {
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
        ByteBuffer input = source.duplicate();
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
        // Do we have a complete header?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   151
        if (input.remaining() < RECORD_HEADER_SIZE) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   152
            throw new BufferUnderflowException();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   153
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   154
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   155
        // Is it a handshake message?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   156
        byte firstByte = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   157
        byte secondByte = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   158
        byte thirdByte = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   159
        if ((firstByte & 0x80) != 0 && thirdByte == 0x01) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   160
            // looks like a V2ClientHello
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   161
            return exploreV2HelloRecord(input,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   162
                                    firstByte, secondByte, thirdByte);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   163
        } else if (firstByte == 22) {   // 22: handshake record
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   164
            return exploreTLSRecord(input,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   165
                                    firstByte, secondByte, thirdByte);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   166
        } else {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   167
            throw new SSLException("Not handshake record");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   168
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   169
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   170
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   171
    /**
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   172
     * Launch and explore the security capabilities from byte array.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   173
     * <P>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   174
     * Please NOTE that this method must be called before any handshaking
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   175
     * occurs.  The behavior of this method is not defined in this release
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   176
     * if the handshake has begun, or has completed.  Once handshake has
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   177
     * begun, or has completed, the security capabilities can not and
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   178
     * should not be launched with this method.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   179
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   180
     * @param  source
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   181
     *         a byte array containing inbound or outbound network data for
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   182
     *         an SSL/TLS connection.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   183
     * @param  offset
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   184
     *         the start offset in array {@code source} at which the
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   185
     *         network data is read from.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   186
     * @param  length
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   187
     *         the maximum number of bytes to read.
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
     * @throws IOException on network data error
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   190
     * @throws BufferUnderflowException if not enough source bytes available
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   191
     *         to make a complete exploration.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   192
     * @return the explored {@link SSLCapabilities} of the SSL/TLS
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   193
     *         connection
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
     * @see #explore(ByteBuffer)
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   196
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   197
    public final static SSLCapabilities explore(byte[] source,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   198
            int offset, int length) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   199
        ByteBuffer byteBuffer =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   200
            ByteBuffer.wrap(source, offset, length).asReadOnlyBuffer();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   201
        return explore(byteBuffer);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   202
    }
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
     * uint8 V2CipherSpec[3];
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   206
     * struct {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   207
     *     uint16 msg_length;         // The highest bit MUST be 1;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   208
     *                                // the remaining bits contain the length
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   209
     *                                // of the following data in bytes.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   210
     *     uint8 msg_type;            // MUST be 1
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   211
     *     Version version;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   212
     *     uint16 cipher_spec_length; // It cannot be zero and MUST be a
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   213
     *                                // multiple of the V2CipherSpec length.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   214
     *     uint16 session_id_length;  // This field MUST be empty.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   215
     *     uint16 challenge_length;   // SHOULD use a 32-byte challenge
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   216
     *     V2CipherSpec cipher_specs[V2ClientHello.cipher_spec_length];
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   217
     *     opaque session_id[V2ClientHello.session_id_length];
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   218
     *     opaque challenge[V2ClientHello.challenge_length;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   219
     * } V2ClientHello;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   220
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   221
    private static SSLCapabilities exploreV2HelloRecord(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   222
            ByteBuffer input, byte firstByte, byte secondByte,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   223
            byte thirdByte) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   224
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   225
        // We only need the header. We have already had enough source bytes.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   226
        // int recordLength = (firstByte & 0x7F) << 8) | (secondByte & 0xFF);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   227
        try {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   228
            // Is it a V2ClientHello?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   229
            if (thirdByte != 0x01) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   230
                throw new SSLException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   231
                        "Unsupported or Unrecognized SSL record");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   232
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   233
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   234
            // What's the hello version?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   235
            byte helloVersionMajor = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   236
            byte helloVersionMinor = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   237
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   238
            // 0x00: major version of SSLv20
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   239
            // 0x02: minor version of SSLv20
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   240
            //
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   241
            // SNIServerName is an extension, SSLv20 doesn't support extension.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   242
            return new SSLCapabilitiesImpl((byte)0x00, (byte)0x02,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   243
                        helloVersionMajor, helloVersionMinor,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   244
                        Collections.<SNIServerName>emptyList());
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   245
        } catch (BufferUnderflowException bufe) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   246
            throw new SSLProtocolException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   247
                        "Invalid handshake record");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   248
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   249
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   250
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   251
    /*
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   252
     * struct {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   253
     *     uint8 major;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   254
     *     uint8 minor;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   255
     * } ProtocolVersion;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   256
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   257
     * enum {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   258
     *     change_cipher_spec(20), alert(21), handshake(22),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   259
     *     application_data(23), (255)
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   260
     * } ContentType;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   261
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   262
     * struct {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   263
     *     ContentType type;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   264
     *     ProtocolVersion version;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   265
     *     uint16 length;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   266
     *     opaque fragment[TLSPlaintext.length];
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   267
     * } TLSPlaintext;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   268
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   269
    private static SSLCapabilities exploreTLSRecord(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   270
            ByteBuffer input, byte firstByte, byte secondByte,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   271
            byte thirdByte) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   272
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   273
        // Is it a handshake message?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   274
        if (firstByte != 22) {        // 22: handshake record
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   275
            throw new SSLException("Not handshake record");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   276
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   277
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   278
        // We need the record version to construct SSLCapabilities.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   279
        byte recordMajorVersion = secondByte;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   280
        byte recordMinorVersion = thirdByte;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   281
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   282
        // Is there enough data for a full record?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   283
        int recordLength = getInt16(input);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   284
        if (recordLength > input.remaining()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   285
            throw new BufferUnderflowException();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   286
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   287
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   288
        // We have already had enough source bytes.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   289
        try {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   290
            return exploreHandshake(input,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   291
                recordMajorVersion, recordMinorVersion, recordLength);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   292
        } catch (BufferUnderflowException bufe) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   293
            throw new SSLProtocolException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   294
                        "Invalid handshake record");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   295
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   296
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   297
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   298
    /*
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   299
     * enum {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   300
     *     hello_request(0), client_hello(1), server_hello(2),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   301
     *     certificate(11), server_key_exchange (12),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   302
     *     certificate_request(13), server_hello_done(14),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   303
     *     certificate_verify(15), client_key_exchange(16),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   304
     *     finished(20)
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   305
     *     (255)
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   306
     * } HandshakeType;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   307
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   308
     * struct {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   309
     *     HandshakeType msg_type;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   310
     *     uint24 length;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   311
     *     select (HandshakeType) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   312
     *         case hello_request:       HelloRequest;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   313
     *         case client_hello:        ClientHello;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   314
     *         case server_hello:        ServerHello;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   315
     *         case certificate:         Certificate;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   316
     *         case server_key_exchange: ServerKeyExchange;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   317
     *         case certificate_request: CertificateRequest;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   318
     *         case server_hello_done:   ServerHelloDone;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   319
     *         case certificate_verify:  CertificateVerify;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   320
     *         case client_key_exchange: ClientKeyExchange;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   321
     *         case finished:            Finished;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   322
     *     } body;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   323
     * } Handshake;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   324
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   325
    private static SSLCapabilities exploreHandshake(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   326
            ByteBuffer input, byte recordMajorVersion,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   327
            byte recordMinorVersion, int recordLength) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   328
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   329
        // What is the handshake type?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   330
        byte handshakeType = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   331
        if (handshakeType != 0x01) {   // 0x01: client_hello message
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   332
            throw new IllegalStateException("Not initial handshaking");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   333
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   334
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   335
        // What is the handshake body length?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   336
        int handshakeLength = getInt24(input);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   337
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   338
        // Theoretically, a single handshake message might span multiple
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   339
        // records, but in practice this does not occur.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   340
        if (handshakeLength > (recordLength - 4)) { // 4: handshake header size
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   341
            throw new SSLException("Handshake message spans multiple records");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   342
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   343
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   344
        input = input.duplicate();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   345
        input.limit(handshakeLength + input.position());
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   346
        return exploreClientHello(input,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   347
                                    recordMajorVersion, recordMinorVersion);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   348
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   349
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   350
    /*
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   351
     * struct {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   352
     *     uint32 gmt_unix_time;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   353
     *     opaque random_bytes[28];
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   354
     * } Random;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   355
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   356
     * opaque SessionID<0..32>;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   357
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   358
     * uint8 CipherSuite[2];
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   359
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   360
     * enum { null(0), (255) } CompressionMethod;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   361
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   362
     * struct {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   363
     *     ProtocolVersion client_version;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   364
     *     Random random;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   365
     *     SessionID session_id;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   366
     *     CipherSuite cipher_suites<2..2^16-2>;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   367
     *     CompressionMethod compression_methods<1..2^8-1>;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   368
     *     select (extensions_present) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   369
     *         case false:
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   370
     *             struct {};
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   371
     *         case true:
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   372
     *             Extension extensions<0..2^16-1>;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   373
     *     };
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   374
     * } ClientHello;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   375
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   376
    private static SSLCapabilities exploreClientHello(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   377
            ByteBuffer input,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   378
            byte recordMajorVersion,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   379
            byte recordMinorVersion) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   380
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   381
        List<SNIServerName> snList = Collections.<SNIServerName>emptyList();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   382
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   383
        // client version
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   384
        byte helloMajorVersion = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   385
        byte helloMinorVersion = input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   386
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   387
        // ignore random
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   388
        int position = input.position();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   389
        input.position(position + 32);  // 32: the length of Random
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   390
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   391
        // ignore session id
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   392
        ignoreByteVector8(input);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   393
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   394
        // ignore cipher_suites
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   395
        ignoreByteVector16(input);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   396
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   397
        // ignore compression methods
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   398
        ignoreByteVector8(input);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   399
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   400
        if (input.remaining() > 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   401
            snList = exploreExtensions(input);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   402
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   403
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   404
        return new SSLCapabilitiesImpl(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   405
                recordMajorVersion, recordMinorVersion,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   406
                helloMajorVersion, helloMinorVersion, snList);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   407
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   408
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   409
    /*
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   410
     * struct {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   411
     *     ExtensionType extension_type;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   412
     *     opaque extension_data<0..2^16-1>;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   413
     * } Extension;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   414
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   415
     * enum {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   416
     *     server_name(0), max_fragment_length(1),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   417
     *     client_certificate_url(2), trusted_ca_keys(3),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   418
     *     truncated_hmac(4), status_request(5), (65535)
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   419
     * } ExtensionType;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   420
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   421
    private static List<SNIServerName> exploreExtensions(ByteBuffer input)
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   422
            throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   423
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   424
        int length = getInt16(input);           // length of extensions
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   425
        while (length > 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   426
            int extType = getInt16(input);      // extenson type
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   427
            int extLen = getInt16(input);       // length of extension data
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   428
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   429
            if (extType == 0x00) {      // 0x00: type of server name indication
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   430
                return exploreSNIExt(input, extLen);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   431
            } else {                    // ignore other extensions
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   432
                ignoreByteVector(input, extLen);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   433
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   434
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   435
            length -= extLen + 4;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   436
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   437
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   438
        return Collections.<SNIServerName>emptyList();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   439
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   440
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   441
    /*
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   442
     * struct {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   443
     *     NameType name_type;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   444
     *     select (name_type) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   445
     *         case host_name: HostName;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   446
     *     } name;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   447
     * } ServerName;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   448
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   449
     * enum {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   450
     *     host_name(0), (255)
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   451
     * } NameType;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   452
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   453
     * opaque HostName<1..2^16-1>;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   454
     *
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   455
     * struct {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   456
     *     ServerName server_name_list<1..2^16-1>
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   457
     * } ServerNameList;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   458
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   459
    private static List<SNIServerName> exploreSNIExt(ByteBuffer input,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   460
            int extLen) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   461
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   462
        Map<Integer, SNIServerName> sniMap = new LinkedHashMap<>();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   463
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   464
        int remains = extLen;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   465
        if (extLen >= 2) {     // "server_name" extension in ClientHello
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   466
            int listLen = getInt16(input);     // length of server_name_list
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   467
            if (listLen == 0 || listLen + 2 != extLen) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   468
                throw new SSLProtocolException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   469
                    "Invalid server name indication extension");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   470
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   471
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   472
            remains -= 2;     // 0x02: the length field of server_name_list
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   473
            while (remains > 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   474
                int code = getInt8(input);      // name_type
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   475
                int snLen = getInt16(input);    // length field of server name
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   476
                if (snLen > remains) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   477
                    throw new SSLProtocolException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   478
                        "Not enough data to fill declared vector size");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   479
                }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   480
                byte[] encoded = new byte[snLen];
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   481
                input.get(encoded);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   482
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   483
                SNIServerName serverName;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   484
                switch (code) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   485
                    case StandardConstants.SNI_HOST_NAME:
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   486
                        if (encoded.length == 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   487
                            throw new SSLProtocolException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   488
                                "Empty HostName in server name indication");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   489
                        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   490
                        serverName = new SNIHostName(encoded);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   491
                        break;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   492
                    default:
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   493
                        serverName = new UnknownServerName(code, encoded);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   494
                }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   495
                // check for duplicated server name type
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   496
                if (sniMap.put(serverName.getType(), serverName) != null) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   497
                    throw new SSLProtocolException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   498
                            "Duplicated server name of type " +
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   499
                            serverName.getType());
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   500
                }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   501
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   502
                remains -= encoded.length + 3;  // NameType: 1 byte
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   503
                                                // HostName length: 2 bytes
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   504
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   505
        } else if (extLen == 0) {     // "server_name" extension in ServerHello
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   506
            throw new SSLProtocolException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   507
                        "Not server name indication extension in client");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   508
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   509
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   510
        if (remains != 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   511
            throw new SSLProtocolException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   512
                        "Invalid server name indication extension");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   513
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   514
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   515
        return Collections.<SNIServerName>unmodifiableList(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   516
                                            new ArrayList<>(sniMap.values()));
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   517
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   518
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   519
    private static int getInt8(ByteBuffer input) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   520
        return input.get();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   521
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   522
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   523
    private static int getInt16(ByteBuffer input) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   524
        return ((input.get() & 0xFF) << 8) | (input.get() & 0xFF);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   525
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   526
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   527
    private static int getInt24(ByteBuffer input) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   528
        return ((input.get() & 0xFF) << 16) | ((input.get() & 0xFF) << 8) |
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   529
                (input.get() & 0xFF);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   530
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   531
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   532
    private static void ignoreByteVector8(ByteBuffer input) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   533
        ignoreByteVector(input, getInt8(input));
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   534
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   535
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   536
    private static void ignoreByteVector16(ByteBuffer input) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   537
        ignoreByteVector(input, getInt16(input));
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   538
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   539
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   540
    private static void ignoreByteVector24(ByteBuffer input) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   541
        ignoreByteVector(input, getInt24(input));
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   542
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   543
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   544
    private static void ignoreByteVector(ByteBuffer input, int length) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   545
        if (length != 0) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   546
            int position = input.position();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   547
            input.position(position + length);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   548
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   549
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   550
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   551
    private static class UnknownServerName extends SNIServerName {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   552
        UnknownServerName(int code, byte[] encoded) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   553
            super(code, encoded);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   554
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   555
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   556
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   557
    private static final class SSLCapabilitiesImpl extends SSLCapabilities {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   558
        private final static Map<Integer, String> versionMap = new HashMap<>(5);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   559
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   560
        private final String recordVersion;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   561
        private final String helloVersion;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   562
        List<SNIServerName> sniNames;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   563
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   564
        static {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   565
            versionMap.put(0x0002, "SSLv2Hello");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   566
            versionMap.put(0x0300, "SSLv3");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   567
            versionMap.put(0x0301, "TLSv1");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   568
            versionMap.put(0x0302, "TLSv1.1");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   569
            versionMap.put(0x0303, "TLSv1.2");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   570
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   571
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   572
        SSLCapabilitiesImpl(byte recordMajorVersion, byte recordMinorVersion,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   573
                byte helloMajorVersion, byte helloMinorVersion,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   574
                List<SNIServerName> sniNames) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   575
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   576
            int version = (recordMajorVersion << 8) | recordMinorVersion;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   577
            this.recordVersion = versionMap.get(version) != null ?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   578
                        versionMap.get(version) :
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   579
                        unknownVersion(recordMajorVersion, recordMinorVersion);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   580
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   581
            version = (helloMajorVersion << 8) | helloMinorVersion;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   582
            this.helloVersion = versionMap.get(version) != null ?
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   583
                        versionMap.get(version) :
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   584
                        unknownVersion(helloMajorVersion, helloMinorVersion);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   585
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   586
            this.sniNames = sniNames;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   587
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   588
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   589
        @Override
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   590
        public String getRecordVersion() {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   591
            return recordVersion;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   592
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   593
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   594
        @Override
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   595
        public String getHelloVersion() {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   596
            return helloVersion;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   597
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   598
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   599
        @Override
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   600
        public List<SNIServerName> getServerNames() {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   601
            if (!sniNames.isEmpty()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   602
                return Collections.<SNIServerName>unmodifiableList(sniNames);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   603
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   604
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   605
            return sniNames;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   606
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   607
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   608
        private static String unknownVersion(byte major, byte minor) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   609
            return "Unknown-" + ((int)major) + "." + ((int)minor);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   610
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   611
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   612
}
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
diff changeset
   613