src/java.base/share/classes/sun/security/ssl/HelloExtensions.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45064 jdk/src/java.base/share/classes/sun/security/ssl/HelloExtensions.java@b1b45177051b
child 48225 718669e6b375
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 37814
diff changeset
     2
 * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1579
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1579
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1579
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1579
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1579
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 9035
diff changeset
    31
import javax.net.ssl.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This file contains all the classes relevant to TLS Extensions for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * ClientHello and ServerHello messages. The extension mechanism and
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32032
diff changeset
    36
 * several extensions are defined in RFC 6066. Additional extensions are
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32032
diff changeset
    37
 * defined in the ECC RFC 4492 and the ALPN extension is defined in RFC 7301.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Currently, only the two ECC extensions are fully supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The classes contained in this file are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *  . HelloExtensions: a List of extensions as used in the client hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      and server hello messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  . ExtensionType: an enum style class for the extension type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  . HelloExtension: abstract base class for all extensions. All subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      must be immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *  . UnknownExtension: used to represent all parsed extensions that we do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      explicitly support.
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 6856
diff changeset
    50
 *  . ServerNameExtension: the server_name extension.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 6856
diff changeset
    51
 *  . SignatureAlgorithmsExtension: the signature_algorithms extension.
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 37814
diff changeset
    52
 *  . SupportedGroupsExtension: the supported groups extension.
37814
844e590a011a 8156502: Use short name of SupportedEllipticCurvesExtension.java
xuelei
parents: 34380
diff changeset
    53
 *  . EllipticPointFormatsExtension: the ECC supported point formats
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      (compressed/uncompressed) extension.
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32032
diff changeset
    55
 *  . ALPNExtension: the application_layer_protocol_negotiation extension.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
final class HelloExtensions {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private List<HelloExtension> extensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int encodedLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    HelloExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        extensions = Collections.emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    HelloExtensions(HandshakeInStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        int len = s.getInt16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        extensions = new ArrayList<HelloExtension>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        encodedLength = len + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            int type = s.getInt16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            int extlen = s.getInt16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            ExtensionType extType = ExtensionType.get(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            HelloExtension extension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (extType == ExtensionType.EXT_SERVER_NAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                extension = new ServerNameExtension(s, extlen);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 6856
diff changeset
    80
            } else if (extType == ExtensionType.EXT_SIGNATURE_ALGORITHMS) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 6856
diff changeset
    81
                extension = new SignatureAlgorithmsExtension(s, extlen);
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 37814
diff changeset
    82
            } else if (extType == ExtensionType.EXT_SUPPORTED_GROUPS) {
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 37814
diff changeset
    83
                extension = new SupportedGroupsExtension(s, extlen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            } else if (extType == ExtensionType.EXT_EC_POINT_FORMATS) {
37814
844e590a011a 8156502: Use short name of SupportedEllipticCurvesExtension.java
xuelei
parents: 34380
diff changeset
    85
                extension = new EllipticPointFormatsExtension(s, extlen);
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    86
            } else if (extType == ExtensionType.EXT_RENEGOTIATION_INFO) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    87
                extension = new RenegotiationInfoExtension(s, extlen);
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32032
diff changeset
    88
            } else if (extType == ExtensionType.EXT_ALPN) {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32032
diff changeset
    89
                extension = new ALPNExtension(s, extlen);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    90
            } else if (extType == ExtensionType.EXT_MAX_FRAGMENT_LENGTH) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    91
                extension = new MaxFragmentLengthExtension(s, extlen);
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30904
diff changeset
    92
            } else if (extType == ExtensionType.EXT_STATUS_REQUEST) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30904
diff changeset
    93
                extension = new CertStatusReqExtension(s, extlen);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30904
diff changeset
    94
            } else if (extType == ExtensionType.EXT_STATUS_REQUEST_V2) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 30904
diff changeset
    95
                extension = new CertStatusReqListV2Extension(s, extlen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                extension = new UnknownExtension(s, extlen, extType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            extensions.add(extension);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            len -= extlen + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (len != 0) {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   103
            throw new SSLProtocolException(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   104
                        "Error parsing extensions: extra data");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // Return the List of extensions. Must not be modified by the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    List<HelloExtension> list() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return extensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    void add(HelloExtension ext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (extensions.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            extensions = new ArrayList<HelloExtension>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        extensions.add(ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        encodedLength = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    HelloExtension get(ExtensionType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (HelloExtension ext : extensions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (ext.type == type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return ext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (encodedLength >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return encodedLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (extensions.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            encodedLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            encodedLength = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            for (HelloExtension ext : extensions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                encodedLength += ext.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return encodedLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int length = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        s.putInt16(length - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (HelloExtension ext : extensions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            ext.send(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        for (HelloExtension ext : extensions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            s.println(ext.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}