src/java.base/share/classes/sun/security/ssl/SupportedVersionsExtension.java
author xuelei
Fri, 08 Jun 2018 11:59:34 -0700
branchJDK-8145252-TLS13-branch
changeset 56708 25178bb3e8f5
parent 56704 c3ee22c3a0f6
child 56761 001e4e7c0a01
permissions -rw-r--r--
Rename Stringize to Stringizer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     1
/*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     4
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    10
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    15
 * accompanied this code).
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    16
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    20
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    23
 * questions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    24
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    25
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    26
package sun.security.ssl;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    27
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    28
import java.io.IOException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    29
import java.nio.ByteBuffer;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    30
import java.text.MessageFormat;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    31
import java.util.Locale;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    32
import javax.net.ssl.SSLProtocolException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    33
import static sun.security.ssl.SSLExtension.CH_SUPPORTED_VERSIONS;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    34
import sun.security.ssl.SSLExtension.ExtensionConsumer;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    35
import static sun.security.ssl.SSLExtension.HRR_SUPPORTED_VERSIONS;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    36
import static sun.security.ssl.SSLExtension.SH_SUPPORTED_VERSIONS;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    37
import sun.security.ssl.SSLExtension.SSLExtensionSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    38
import sun.security.ssl.SSLHandshake.HandshakeMessage;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    39
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    40
/**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    41
 * Pack of the "supported_versions" extensions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    42
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    43
final class SupportedVersionsExtension {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    44
    static final HandshakeProducer chNetworkProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    45
            new CHSupportedVersionsProducer();
56704
c3ee22c3a0f6 Minor nits and cleanup across SSLExtension classes
jnimeh
parents: 56542
diff changeset
    46
    static final ExtensionConsumer chOnLoadConsumer =
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    47
            new CHSupportedVersionsConsumer();
56708
25178bb3e8f5 Rename Stringize to Stringizer
xuelei
parents: 56704
diff changeset
    48
    static final SSLStringizer chStringizer =
25178bb3e8f5 Rename Stringize to Stringizer
xuelei
parents: 56704
diff changeset
    49
            new CHSupportedVersionsStringizer();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    50
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    51
    static final HandshakeProducer shNetworkProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    52
            new SHSupportedVersionsProducer();
56704
c3ee22c3a0f6 Minor nits and cleanup across SSLExtension classes
jnimeh
parents: 56542
diff changeset
    53
    static final ExtensionConsumer shOnLoadConsumer =
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    54
            new SHSupportedVersionsConsumer();
56708
25178bb3e8f5 Rename Stringize to Stringizer
xuelei
parents: 56704
diff changeset
    55
    static final SSLStringizer shStringizer =
25178bb3e8f5 Rename Stringize to Stringizer
xuelei
parents: 56704
diff changeset
    56
            new SHSupportedVersionsStringizer();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    57
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    58
    static final HandshakeProducer hrrNetworkProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    59
            new HRRSupportedVersionsProducer();
56704
c3ee22c3a0f6 Minor nits and cleanup across SSLExtension classes
jnimeh
parents: 56542
diff changeset
    60
    static final ExtensionConsumer hrrOnLoadConsumer =
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    61
            new HRRSupportedVersionsConsumer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    62
    static final HandshakeProducer hrrReproducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    63
            new HRRSupportedVersionsReproducer();
56708
25178bb3e8f5 Rename Stringize to Stringizer
xuelei
parents: 56704
diff changeset
    64
    static final SSLStringizer hrrStringizer =
25178bb3e8f5 Rename Stringize to Stringizer
xuelei
parents: 56704
diff changeset
    65
            new SHSupportedVersionsStringizer();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    66
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    67
     * The "supported_versions" extension in ClientHello.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    68
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    69
    static final class CHSupportedVersionsSpec implements SSLExtensionSpec {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    70
        final int[] requestedProtocols;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    71
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    72
        private CHSupportedVersionsSpec(int[] requestedProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    73
            this.requestedProtocols = requestedProtocols;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    74
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    75
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    76
        private CHSupportedVersionsSpec(ByteBuffer m) throws IOException  {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    77
            if (m.remaining() < 3) {        //  1: the length of the list
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    78
                                            // +2: one version at least
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    79
                throw new SSLProtocolException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    80
                    "Invalid supported_versions extension: insufficient data");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    81
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    82
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    83
            byte[] vbs = Record.getBytes8(m);   // Get the version bytes.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    84
            if (m.hasRemaining()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    85
                throw new SSLProtocolException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    86
                    "Invalid supported_versions extension: unknown extra data");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    87
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    88
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    89
            if (vbs == null || vbs.length == 0 || (vbs.length & 0x01) != 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    90
                throw new SSLProtocolException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    91
                    "Invalid supported_versions extension: incomplete data");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    92
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    93
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    94
            int[] protocols = new int[vbs.length >> 1];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    95
            for (int i = 0, j = 0; i < vbs.length;) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    96
                byte major = vbs[i++];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    97
                byte minor = vbs[i++];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    98
                protocols[j++] = ((major & 0xFF) << 8) | (minor & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    99
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   100
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   101
            this.requestedProtocols = protocols;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   102
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   103
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   104
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   105
        public String toString() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   106
            MessageFormat messageFormat = new MessageFormat(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   107
                "\"versions\": '['{0}']'", Locale.ENGLISH);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   108
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   109
            if (requestedProtocols == null || requestedProtocols.length == 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   110
                Object[] messageFields = {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   111
                        "<no supported version specified>"
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   112
                    };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   113
                return messageFormat.format(messageFields);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   114
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   115
                StringBuilder builder = new StringBuilder(512);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   116
                boolean isFirst = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   117
                for (int pv : requestedProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   118
                    if (isFirst) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   119
                        isFirst = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   120
                    } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   121
                        builder.append(", ");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   122
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   123
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   124
                    builder.append(ProtocolVersion.nameOf(pv));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   125
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   126
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   127
                Object[] messageFields = {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   128
                        builder.toString()
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   129
                    };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   130
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   131
                return messageFormat.format(messageFields);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   132
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   133
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   134
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   135
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   136
    private static final
56708
25178bb3e8f5 Rename Stringize to Stringizer
xuelei
parents: 56704
diff changeset
   137
            class CHSupportedVersionsStringizer implements SSLStringizer {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   138
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   139
        public String toString(ByteBuffer buffer) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   140
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   141
                return (new CHSupportedVersionsSpec(buffer)).toString();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   142
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   143
                // For debug logging only, so please swallow exceptions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   144
                return ioe.getMessage();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   145
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   146
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   147
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   148
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   149
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   150
     * Network data producer of a "supported_versions" extension in ClientHello.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   151
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   152
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   153
            class CHSupportedVersionsProducer implements HandshakeProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   154
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   155
        private CHSupportedVersionsProducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   156
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   157
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   158
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   159
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   160
        public byte[] produce(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   161
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   162
            // The producing happens in client side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   163
            ClientHandshakeContext chc = (ClientHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   164
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   165
            // Is it a supported and enabled extension?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   166
            if (!chc.sslConfig.isAvailable(CH_SUPPORTED_VERSIONS)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   167
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   168
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   169
                        "Ignore unavailable extension: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   170
                        CH_SUPPORTED_VERSIONS.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   171
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   172
                return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   173
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   174
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   175
            // Produce the extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   176
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   177
            // The activated protocols are used as the supported versions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   178
            int[] protocols = new int[chc.activeProtocols.size()];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   179
            int verLen = protocols.length * 2;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   180
            byte[] extData = new byte[verLen + 1];      // 1: versions length
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   181
            extData[0] = (byte)(verLen & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   182
            int i = 0, j = 1;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   183
            for (ProtocolVersion pv : chc.activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   184
                protocols[i++] = pv.id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   185
                extData[j++] = pv.major;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   186
                extData[j++] = pv.minor;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   187
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   188
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   189
            // Update the context.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   190
            chc.handshakeExtensions.put(CH_SUPPORTED_VERSIONS,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   191
                    new CHSupportedVersionsSpec(protocols));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   192
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   193
            return extData;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   194
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   195
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   196
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   197
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   198
     * Network data consumer of a "supported_versions" extension in ClientHello.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   199
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   200
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   201
            class CHSupportedVersionsConsumer implements ExtensionConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   202
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   203
        private CHSupportedVersionsConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   204
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   205
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   206
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   207
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   208
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   209
            HandshakeMessage message, ByteBuffer buffer) throws IOException {
56704
c3ee22c3a0f6 Minor nits and cleanup across SSLExtension classes
jnimeh
parents: 56542
diff changeset
   210
            // The consuming happens in server side only.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   211
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   212
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   213
            // Is it a supported and enabled extension?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   214
            if (!shc.sslConfig.isAvailable(CH_SUPPORTED_VERSIONS)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   215
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   216
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   217
                        "Ignore unavailable extension: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   218
                        CH_SUPPORTED_VERSIONS.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   219
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   220
                return;     // ignore the extension
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   221
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   222
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   223
            // Parse the extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   224
            CHSupportedVersionsSpec spec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   225
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   226
                spec = new CHSupportedVersionsSpec(buffer);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   227
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   228
                shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   229
                return;     // fatal() always throws, make the compiler happy.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   230
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   231
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   232
            // Update the context.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   233
            shc.handshakeExtensions.put(CH_SUPPORTED_VERSIONS, spec);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   234
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   235
            // No impact on session resumption.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   236
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   237
            // Note that the protocol version negotiation happens before the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   238
            // session resumption negotiation.  And the session resumption
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   239
            // negotiation depends on the negotiated protocol version.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   240
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   241
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   242
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   243
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   244
     * The "supported_versions" extension in ServerHello and HelloRetryRequest.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   245
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   246
    static final class SHSupportedVersionsSpec implements SSLExtensionSpec {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   247
        final int selectedVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   248
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   249
        private SHSupportedVersionsSpec(ProtocolVersion selectedVersion) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   250
            this.selectedVersion = selectedVersion.id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   251
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   252
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   253
        private SHSupportedVersionsSpec(ByteBuffer m) throws IOException  {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   254
            if (m.remaining() != 2) {       // 2: the selected version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   255
                throw new SSLProtocolException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   256
                    "Invalid supported_versions: insufficient data");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   257
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   258
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   259
            byte major = m.get();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   260
            byte minor = m.get();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   261
            this.selectedVersion = ((major & 0xFF) << 8) | (minor & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   262
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   263
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   264
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   265
        public String toString() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   266
            MessageFormat messageFormat = new MessageFormat(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   267
                "\"selected version\": '['{0}']'", Locale.ENGLISH);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   268
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   269
            Object[] messageFields = {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   270
                    ProtocolVersion.nameOf(selectedVersion)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   271
                };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   272
            return messageFormat.format(messageFields);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   273
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   274
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   275
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
    private static final
56708
25178bb3e8f5 Rename Stringize to Stringizer
xuelei
parents: 56704
diff changeset
   277
            class SHSupportedVersionsStringizer implements SSLStringizer {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   278
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   279
        public String toString(ByteBuffer buffer) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   280
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
                return (new SHSupportedVersionsSpec(buffer)).toString();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   283
                // For debug logging only, so please swallow exceptions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
                return ioe.getMessage();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   285
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   286
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   287
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   288
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   289
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   290
     * Network data producer of a "supported_versions" extension in ServerHello.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   291
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
            class SHSupportedVersionsProducer implements HandshakeProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   295
        private SHSupportedVersionsProducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   296
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   297
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   299
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
        public byte[] produce(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   301
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
            // The producing happens in server side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   305
            // In response to supported_versions request only
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   306
            CHSupportedVersionsSpec svs = (CHSupportedVersionsSpec)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   307
                    shc.handshakeExtensions.get(CH_SUPPORTED_VERSIONS);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   308
            if (svs == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   309
                // Unlikely, no key_share extension requested.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
                            "Ignore unavailable supported_versions extension");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
                return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   315
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   316
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   317
            // Is it a supported and enabled extension?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   318
            if (!shc.sslConfig.isAvailable(SH_SUPPORTED_VERSIONS)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   319
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
                        "Ignore unavailable extension: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   322
                        SH_SUPPORTED_VERSIONS.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   323
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   324
                return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   325
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   326
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   327
            // Produce the extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   328
            byte[] extData = new byte[2];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   329
            extData[0] = shc.negotiatedProtocol.major;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   330
            extData[1] = shc.negotiatedProtocol.minor;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   331
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   332
            // Update the context.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   333
            shc.handshakeExtensions.put(SH_SUPPORTED_VERSIONS,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   334
                    new SHSupportedVersionsSpec(shc.negotiatedProtocol));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   335
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   336
            return extData;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   337
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   338
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   339
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   340
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   341
     * Network data consumer of a "supported_versions" extension in ServerHello.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   342
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   343
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   344
            class SHSupportedVersionsConsumer implements ExtensionConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   345
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   346
        private SHSupportedVersionsConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   347
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   348
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   349
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   350
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   351
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   352
            HandshakeMessage message, ByteBuffer buffer) throws IOException {
56704
c3ee22c3a0f6 Minor nits and cleanup across SSLExtension classes
jnimeh
parents: 56542
diff changeset
   353
            // The consuming happens in client side only.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   354
            ClientHandshakeContext chc = (ClientHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   355
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   356
            // Is it a supported and enabled extension?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   357
            if (!chc.sslConfig.isAvailable(SH_SUPPORTED_VERSIONS)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   358
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   359
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   360
                        "Ignore unavailable extension: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   361
                        SH_SUPPORTED_VERSIONS.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   362
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   363
                return;     // ignore the extension
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   364
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   365
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   366
            // Parse the extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   367
            SHSupportedVersionsSpec spec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   368
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   369
                spec = new SHSupportedVersionsSpec(buffer);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   370
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   371
                chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   372
                return;     // fatal() always throws, make the compiler happy.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   373
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   374
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   375
            // Update the context.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   376
            chc.handshakeExtensions.put(SH_SUPPORTED_VERSIONS, spec);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   377
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   378
            // No impact on session resumption.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   379
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   380
            // Note that the protocol version negotiation happens before the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   381
            // session resumption negotiation.  And the session resumption
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   382
            // negotiation depends on the negotiated protocol version.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   383
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   384
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   385
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   386
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   387
     * Network data producer of a "supported_versions" extension in
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   388
     * HelloRetryRequest.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   389
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   390
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   391
            class HRRSupportedVersionsProducer implements HandshakeProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   392
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   393
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   394
        private HRRSupportedVersionsProducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   395
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   396
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   397
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   398
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   399
        public byte[] produce(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   400
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   401
            // The producing happens in server side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   402
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   403
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   404
            // Is it a supported and enabled extension?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   405
            if (!shc.sslConfig.isAvailable(HRR_SUPPORTED_VERSIONS)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   406
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   407
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   408
                        "Ignore unavailable extension: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   409
                        HRR_SUPPORTED_VERSIONS.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   410
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   411
                return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   412
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   413
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   414
            // Produce the extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   415
            byte[] extData = new byte[2];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   416
            extData[0] = shc.negotiatedProtocol.major;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   417
            extData[1] = shc.negotiatedProtocol.minor;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   418
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   419
            // Update the context.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   420
            shc.handshakeExtensions.put(HRR_SUPPORTED_VERSIONS,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   421
                    new SHSupportedVersionsSpec(shc.negotiatedProtocol));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   422
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   423
            return extData;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   424
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   425
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   426
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   427
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   428
     * Network data consumer of a "supported_versions" extension in
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   429
     * HelloRetryRequest.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   430
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   431
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   432
            class HRRSupportedVersionsConsumer implements ExtensionConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   433
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   434
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   435
        private HRRSupportedVersionsConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   436
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   437
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   438
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   439
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   440
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   441
            HandshakeMessage message, ByteBuffer buffer) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   442
56704
c3ee22c3a0f6 Minor nits and cleanup across SSLExtension classes
jnimeh
parents: 56542
diff changeset
   443
            // The consuming happens in client side only.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   444
            ClientHandshakeContext chc = (ClientHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   445
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   446
            // Is it a supported and enabled extension?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   447
            if (!chc.sslConfig.isAvailable(HRR_SUPPORTED_VERSIONS)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   448
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   449
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   450
                        "Ignore unavailable extension: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   451
                        HRR_SUPPORTED_VERSIONS.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   452
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   453
                return;     // ignore the extension
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   454
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   455
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   456
            // Parse the extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   457
            SHSupportedVersionsSpec spec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   458
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   459
                spec = new SHSupportedVersionsSpec(buffer);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   460
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   461
                chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   462
                return;     // fatal() always throws, make the compiler happy.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   463
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   464
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   465
            // Update the context.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   466
            chc.handshakeExtensions.put(HRR_SUPPORTED_VERSIONS, spec);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   467
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   468
            // No impact on session resumption.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   469
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   470
            // Note that the protocol version negotiation happens before the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   471
            // session resumption negotiation.  And the session resumption
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   472
            // negotiation depends on the negotiated protocol version.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   473
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   474
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   475
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   476
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   477
     * Network data producer of a "supported_versions" extension for stateless
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   478
     * HelloRetryRequest reconstruction.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   479
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   480
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   481
            class HRRSupportedVersionsReproducer implements HandshakeProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   482
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   483
        private HRRSupportedVersionsReproducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   484
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   485
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   486
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   487
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   488
        public byte[] produce(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   489
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   490
            // The producing happens in server side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   491
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   492
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   493
            // Is it a supported and enabled extension?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   494
            if (!shc.sslConfig.isAvailable(HRR_SUPPORTED_VERSIONS)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   495
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   496
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   497
                        "[Reprocude] Ignore unavailable extension: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   498
                        HRR_SUPPORTED_VERSIONS.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   499
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   500
                return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   501
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   502
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   503
            // Produce the extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   504
            byte[] extData = new byte[2];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   505
            extData[0] = shc.negotiatedProtocol.major;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   506
            extData[1] = shc.negotiatedProtocol.minor;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   507
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   508
            return extData;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   509
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   510
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   511
}