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