jdk/src/java.base/share/classes/sun/security/ssl/HandshakeMessage.java
author xuelei
Wed, 10 May 2017 23:40:46 +0000
changeset 45064 b1b45177051b
parent 41471 18c0f074ed97
child 46060 cbd5a7843b0b
permissions -rw-r--r--
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS Reviewed-by: valeriep, jnimeh, apetcher
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: 41471
diff changeset
     2
 * Copyright (c) 1996, 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: 4236
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: 4236
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: 4236
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.KeyGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.crypto.SecretKey;
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
    44
import javax.crypto.spec.DHPublicKeySpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.internal.spec.TlsPrfParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.ssl.CipherSuite.*;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    50
import static sun.security.ssl.CipherSuite.PRF.*;
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
    51
import sun.security.util.KeyUtil;
41471
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 39563
diff changeset
    52
import sun.security.util.MessageDigestSpi2;
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
    53
import sun.security.provider.certpath.OCSPResponse;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Many data structures are involved in the handshake messages.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * classes are used as structures, with public data members.  They are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * not visible outside the SSL package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Handshake messages all have a common header format, and they are all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * encoded in a "handshake data" SSL record substream.  The base class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * here (HandshakeMessage) provides a common framework and records the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * SSL record type of the particular handshake message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * This file contains subclasses for all the basic handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * All handshake messages know how to encode and decode themselves on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * SSL streams; this facilitates using the same code on SSL client and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * server sides, although they don't send and receive the same messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Messages also know how to print themselves, which is quite handy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * for debugging.  They always identify their type, and can optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * dump all of their content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    76
public abstract class HandshakeMessage {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    78
    /* Class and subclass dynamic debugging support */
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    79
    public static final Debug debug = Debug.getInstance("ssl");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // enum HandshakeType:
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    82
    static final byte   ht_hello_request          = 0;      // RFC 5246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    83
    static final byte   ht_client_hello           = 1;      // RFC 5246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    84
    static final byte   ht_server_hello           = 2;      // RFC 5246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    85
    static final byte   ht_hello_verify_request   = 3;      // RFC 6347
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    86
    static final byte   ht_new_session_ticket     = 4;      // RFC 4507
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    87
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    88
    static final byte   ht_certificate            = 11;     // RFC 5246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    89
    static final byte   ht_server_key_exchange    = 12;     // RFC 5246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    90
    static final byte   ht_certificate_request    = 13;     // RFC 5246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    91
    static final byte   ht_server_hello_done      = 14;     // RFC 5246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    92
    static final byte   ht_certificate_verify     = 15;     // RFC 5246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    93
    static final byte   ht_client_key_exchange    = 16;     // RFC 5246
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    95
    static final byte   ht_finished               = 20;     // RFC 5246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    96
    static final byte   ht_certificate_url        = 21;     // RFC 6066
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    97
    static final byte   ht_certificate_status     = 22;     // RFC 6066
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    98
    static final byte   ht_supplemental_data      = 23;     // RFC 4680
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
    99
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   100
    static final byte   ht_not_applicable         = -1;     // N/A
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   102
    /*
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   103
     * SSL 3.0 MAC padding constants.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   104
     * Also used by CertificateVerify and Finished during the handshake.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   105
     */
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   106
    static final byte[] MD5_pad1 = genPad(0x36, 48);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   107
    static final byte[] MD5_pad2 = genPad(0x5c, 48);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   109
    static final byte[] SHA_pad1 = genPad(0x36, 40);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   110
    static final byte[] SHA_pad2 = genPad(0x5c, 40);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   111
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   112
    // default constructor
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   113
    HandshakeMessage() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   114
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Utility method to convert a BigInteger to a byte array in unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * format as needed in the handshake messages. BigInteger uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * 2's complement format, i.e. it prepends an extra zero if the MSB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * is set. We remove that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static byte[] toByteArray(BigInteger bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        byte[] b = bi.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if ((b.length > 1) && (b[0] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            int n = b.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            byte[] newarray = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            System.arraycopy(b, 1, newarray, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            b = newarray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static byte[] genPad(int b, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        byte[] padding = new byte[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Arrays.fill(padding, (byte)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Write a handshake message on the (handshake) output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * This is just a four byte header followed by the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * NOTE that huge messages -- notably, ones with huge cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * chains -- are handled correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    final void write(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int len = messageLength();
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 10336
diff changeset
   148
        if (len >= Record.OVERFLOW_OF_INT24) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new SSLException("Handshake message too big"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                + ", type = " + messageType() + ", len = " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        s.write(messageType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        s.putInt24(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        send(s);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   155
        s.complete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Subclasses implement these methods so those kinds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * messages can be emitted.  Base class delegates to subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    abstract int  messageType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    abstract int  messageLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    abstract void send(HandshakeOutStream s) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Write a descriptive message on the output stream; for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    abstract void print(PrintStream p) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
// NOTE:  the rest of these classes are nested within this one, and are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
// imported by other classes in this package.  There are a few other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
// handshake message classes, not neatly nested here because of current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
// licensing requirement for native (RSA) methods.  They belong here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
// but those native methods complicate things a lot!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * HelloRequest ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * Server can ask the client to initiate a new handshake, e.g. to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * session parameters after a connection has been (re)established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 */
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   186
static final class HelloRequest extends HandshakeMessage {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   187
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    int messageType() { return ht_hello_request; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    HelloRequest() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    HelloRequest(HandshakeInStream in) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // nothing in this message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   197
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    int messageLength() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   200
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    void send(HandshakeOutStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // nothing in this messaage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   206
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    void print(PrintStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        out.println("*** HelloRequest (empty)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   214
/*
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   215
 * HelloVerifyRequest ... SERVER --> CLIENT  [DTLS only]
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   216
 *
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   217
 * The definition of HelloVerifyRequest is as follows:
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   218
 *
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   219
 *     struct {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   220
 *       ProtocolVersion server_version;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   221
 *       opaque cookie<0..2^8-1>;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   222
 *     } HelloVerifyRequest;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   223
 *
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   224
 * For DTLS protocols, once the client has transmitted the ClientHello message,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   225
 * it expects to see a HelloVerifyRequest from the server.  However, if the
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   226
 * server's message is lost, the client knows that either the ClientHello or
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   227
 * the HelloVerifyRequest has been lost and retransmits. [RFC 6347]
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   228
 */
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   229
static final class HelloVerifyRequest extends HandshakeMessage {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   230
    ProtocolVersion     protocolVersion;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   231
    byte[]              cookie;         // 1 to 2^8 - 1 bytes
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   232
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   233
    HelloVerifyRequest(HelloCookieManager helloCookieManager,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   234
            ClientHello clientHelloMsg) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   235
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   236
        this.protocolVersion = clientHelloMsg.protocolVersion;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   237
        this.cookie = helloCookieManager.getCookie(clientHelloMsg);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   238
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   239
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   240
    HelloVerifyRequest(
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   241
            HandshakeInStream input, int messageLength) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   242
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   243
        this.protocolVersion =
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   244
                ProtocolVersion.valueOf(input.getInt8(), input.getInt8());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   245
        this.cookie = input.getBytes8();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   246
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   247
        // Is it a valid cookie?
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   248
        HelloCookieManager.checkCookie(protocolVersion, cookie);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   249
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   250
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   251
    @Override
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   252
    int messageType() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   253
        return ht_hello_verify_request;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   254
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   255
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   256
    @Override
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   257
    int messageLength() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   258
        return 2 + cookie.length;       // 2: the length of protocolVersion
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   259
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   260
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   261
    @Override
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   262
    void send(HandshakeOutStream hos) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   263
        hos.putInt8(protocolVersion.major);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   264
        hos.putInt8(protocolVersion.minor);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   265
        hos.putBytes8(cookie);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   266
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   267
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   268
    @Override
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   269
    void print(PrintStream out) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   270
        out.println("*** HelloVerifyRequest");
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   271
        if (debug != null && Debug.isOn("verbose")) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   272
            out.println("server_version: " + protocolVersion);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   273
            Debug.println(out, "cookie", cookie);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   274
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   275
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   276
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * ClientHello ... CLIENT --> SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * Client initiates handshake by telling server what it wants, and what it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * can support (prioritized by what's first in the ciphe suite list).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * By RFC2246:7.4.1.2 it's explicitly anticipated that this message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * will have more data added at the end ... e.g. what CAs the client trusts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 * Until we know how to parse it, we will just read what we know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 * about, and let our caller handle the jumps over unknown data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 */
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   289
static final class ClientHello extends HandshakeMessage {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   291
    ProtocolVersion             protocolVersion;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   292
    RandomCookie                clnt_random;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   293
    SessionId                   sessionId;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   294
    byte[]                      cookie;                     // DTLS only
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   295
    private CipherSuiteList     cipherSuites;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   296
    private final boolean       isDTLS;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   297
    byte[]                      compression_methods;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    HelloExtensions extensions = new HelloExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
   301
    private static final byte[]  NULL_COMPRESSION = new byte[] {0};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   303
    ClientHello(SecureRandom generator, ProtocolVersion protocolVersion,
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   304
            SessionId sessionId, CipherSuiteList cipherSuites,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   305
            boolean isDTLS) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   307
        this.isDTLS = isDTLS;
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   308
        this.protocolVersion = protocolVersion;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   309
        this.sessionId = sessionId;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   310
        this.cipherSuites = cipherSuites;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   311
        if (isDTLS) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   312
            this.cookie = new byte[0];
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   313
        } else {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   314
            this.cookie = null;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   315
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   317
        clnt_random = new RandomCookie(generator);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   318
        compression_methods = NULL_COMPRESSION;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   321
    ClientHello(HandshakeInStream s,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   322
            int messageLength, boolean isDTLS) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   323
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   324
        this.isDTLS = isDTLS;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   325
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        protocolVersion = ProtocolVersion.valueOf(s.getInt8(), s.getInt8());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        clnt_random = new RandomCookie(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        sessionId = new SessionId(s.getBytes8());
28565
48712ca501c1 8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents: 27957
diff changeset
   329
        sessionId.checkLength(protocolVersion);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   330
        if (isDTLS) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   331
            cookie = s.getBytes8();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   332
        } else {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   333
            cookie = null;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   334
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   335
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        cipherSuites = new CipherSuiteList(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        compression_methods = s.getBytes8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (messageLength() != messageLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            extensions = new HelloExtensions(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   343
    CipherSuiteList getCipherSuites() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   344
        return cipherSuites;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   345
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   346
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   347
    // add renegotiation_info extension
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   348
    void addRenegotiationInfoExtension(byte[] clientVerifyData) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   349
        HelloExtension renegotiationInfo = new RenegotiationInfoExtension(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   350
                    clientVerifyData, new byte[0]);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   351
        extensions.add(renegotiationInfo);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   352
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   353
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   354
    // add server_name extension
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 14004
diff changeset
   355
    void addSNIExtension(List<SNIServerName> serverNames) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   356
        try {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 14004
diff changeset
   357
            extensions.add(new ServerNameExtension(serverNames));
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   358
        } catch (IOException ioe) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   359
            // ignore the exception and return
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   360
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   361
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   362
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   363
    // add signature_algorithm extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   364
    void addSignatureAlgorithmsExtension(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   365
            Collection<SignatureAndHashAlgorithm> algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   366
        HelloExtension signatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   367
                new SignatureAlgorithmsExtension(algorithms);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   368
        extensions.add(signatureAlgorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   369
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   370
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   371
    void addMFLExtension(int maximumPacketSize) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   372
        HelloExtension maxFragmentLength =
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   373
                new MaxFragmentLengthExtension(maximumPacketSize);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   374
        extensions.add(maxFragmentLength);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   375
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   376
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   377
    void updateHelloCookie(MessageDigest cookieDigest) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   378
        //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   379
        // Just use HandshakeOutStream to compute the hello verify cookie.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   380
        // Not actually used to output handshake message records.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   381
        //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   382
        HandshakeOutStream hos = new HandshakeOutStream(null);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   383
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   384
        try {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   385
            send(hos, false);    // Do not count hello verify cookie.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   386
        } catch (IOException ioe) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   387
            // unlikely to happen
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   388
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   389
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   390
        cookieDigest.update(hos.toByteArray());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   391
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   392
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   393
    // Add status_request extension type
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   394
    void addCertStatusRequestExtension() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   395
        extensions.add(new CertStatusReqExtension(StatusRequestType.OCSP,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   396
                new OCSPStatusRequest()));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   397
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   398
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   399
    // Add status_request_v2 extension type
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   400
    void addCertStatusReqListV2Extension() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   401
        // Create a default OCSPStatusRequest that we can use for both
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   402
        // OCSP_MULTI and OCSP request list items.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   403
        OCSPStatusRequest osr = new OCSPStatusRequest();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   404
        List<CertStatusReqItemV2> itemList = new ArrayList<>(2);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   405
        itemList.add(new CertStatusReqItemV2(StatusRequestType.OCSP_MULTI,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   406
                osr));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   407
        itemList.add(new CertStatusReqItemV2(StatusRequestType.OCSP, osr));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   408
        extensions.add(new CertStatusReqListV2Extension(itemList));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   409
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   410
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32649
diff changeset
   411
    // add application_layer_protocol_negotiation extension
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32649
diff changeset
   412
    void addALPNExtension(String[] applicationProtocols) throws SSLException {
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32649
diff changeset
   413
        extensions.add(new ALPNExtension(applicationProtocols));
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32649
diff changeset
   414
    }
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32649
diff changeset
   415
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   416
    @Override
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   417
    int messageType() { return ht_client_hello; }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   418
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   419
    @Override
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   420
    int messageLength() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   421
        /*
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   422
         * Add fixed size parts of each field...
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   423
         * version + random + session + cipher + compress
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   424
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   425
        return (2 + 32 + 1 + 2 + 1
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   426
            + sessionId.length()                /* ... + variable parts */
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   427
            + (isDTLS ? (1 + cookie.length) : 0)
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   428
            + (cipherSuites.size() * 2)
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   429
            + compression_methods.length)
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   430
            + extensions.length();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   431
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   432
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   433
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    void send(HandshakeOutStream s) throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   435
        send(s, true);  // Count hello verify cookie.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   438
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        s.println("*** ClientHello, " + protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (debug != null && Debug.isOn("verbose")) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   443
            s.print("RandomCookie:  ");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   444
            clnt_random.print(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            s.print("Session ID:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            s.println(sessionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   449
            if (isDTLS) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   450
                Debug.println(s, "cookie", cookie);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   451
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   452
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            s.println("Cipher Suites: " + cipherSuites);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            Debug.println(s, "Compression Methods", compression_methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            extensions.print(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            s.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   460
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   461
    private void send(HandshakeOutStream s,
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   462
            boolean computeCookie) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   463
        s.putInt8(protocolVersion.major);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   464
        s.putInt8(protocolVersion.minor);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   465
        clnt_random.send(s);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   466
        s.putBytes8(sessionId.getId());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   467
        if (isDTLS && computeCookie) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   468
            s.putBytes8(cookie);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   469
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   470
        cipherSuites.send(s);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   471
        s.putBytes8(compression_methods);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   472
        extensions.send(s);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   473
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
   474
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
 * ServerHello ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
 * Server chooses protocol options from among those it supports and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
 * client supports.  Then it sends the basic session descriptive parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
 * back to the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
class ServerHello extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
{
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   487
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    int messageType() { return ht_server_hello; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    ProtocolVersion     protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    RandomCookie        svr_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    SessionId           sessionId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    CipherSuite         cipherSuite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    byte                compression_method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    HelloExtensions extensions = new HelloExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    ServerHello() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   501
    ServerHello(HandshakeInStream input, int messageLength)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   502
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        protocolVersion = ProtocolVersion.valueOf(input.getInt8(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                                                  input.getInt8());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        svr_random = new RandomCookie(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        sessionId = new SessionId(input.getBytes8());
28565
48712ca501c1 8044860: Vectors and fixed length fields should be verified for allowed sizes.
jnimeh
parents: 27957
diff changeset
   507
        sessionId.checkLength(protocolVersion);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        cipherSuite = CipherSuite.valueOf(input.getInt8(), input.getInt8());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        compression_method = (byte)input.getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (messageLength() != messageLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            extensions = new HelloExtensions(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   515
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    int messageLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        // almost fixed size, except session ID and extensions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        //      major + minor = 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        //      random = 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        //      session ID len field = 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        //      cipher suite + compression = 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        //      extensions: if present, 2 + length of extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return 38 + sessionId.length() + extensions.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   527
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    void send(HandshakeOutStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        s.putInt8(protocolVersion.major);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        s.putInt8(protocolVersion.minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        svr_random.send(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        s.putBytes8(sessionId.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        s.putInt8(cipherSuite.id >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        s.putInt8(cipherSuite.id & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        s.putInt8(compression_method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        extensions.send(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   540
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    void print(PrintStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        s.println("*** ServerHello, " + protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (debug != null && Debug.isOn("verbose")) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   546
            s.print("RandomCookie:  ");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   547
            svr_random.print(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            s.print("Session ID:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            s.println(sessionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            s.println("Cipher Suite: " + cipherSuite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            s.println("Compression Method: " + compression_method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            extensions.print(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            s.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
 * CertificateMsg ... send by both CLIENT and SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
 * Each end of a connection may need to pass its certificate chain to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
 * the other end.  Such chains are intended to validate an identity with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
 * reference to some certifying authority.  Examples include companies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
 * like Verisign, or financial institutions.  There's some control over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
 * the certifying authorities which are sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
 * NOTE: that these messages might be huge, taking many handshake records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
 * Up to 2^48 bytes of certificate may be sent, in records of at most 2^14
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
 * bytes each ... up to 2^32 records sent on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
class CertificateMsg extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
{
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   577
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    int messageType() { return ht_certificate; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    private X509Certificate[] chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    private List<byte[]> encodedChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private int messageLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    CertificateMsg(X509Certificate[] certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        chain = certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    CertificateMsg(HandshakeInStream input) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        int chainLen = input.getInt24();
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   592
        List<Certificate> v = new ArrayList<>(4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        CertificateFactory cf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        while (chainLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            byte[] cert = input.getBytes24();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            chainLen -= (3 + cert.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                if (cf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    cf = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                v.add(cf.generateCertificate(new ByteArrayInputStream(cert)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            } catch (CertificateException e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   604
                throw (SSLProtocolException)new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   605
                    e.getMessage()).initCause(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        chain = v.toArray(new X509Certificate[v.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   612
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (encodedChain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            messageLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            encodedChain = new ArrayList<byte[]>(chain.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                for (X509Certificate cert : chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    byte[] b = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    encodedChain.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    messageLength += b.length + 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            } catch (CertificateEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                encodedChain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                throw new RuntimeException("Could not encode certificates", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return messageLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   631
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        s.putInt24(messageLength() - 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        for (byte[] b : encodedChain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            s.putBytes24(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   639
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        s.println("*** Certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 28565
diff changeset
   643
        if (chain.length == 0) {
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 28565
diff changeset
   644
            s.println("<Empty>");
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 28565
diff changeset
   645
        } else if (debug != null && Debug.isOn("verbose")) {
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 28565
diff changeset
   646
            for (int i = 0; i < chain.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                s.println("chain [" + i + "] = " + chain[i]);
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 28565
diff changeset
   648
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 28565
diff changeset
   650
        s.println("***");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    X509Certificate[] getCertificateChain() {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   654
        return chain.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
/*
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   659
 * CertificateStatus ... SERVER --> CLIENT
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   660
 *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   661
 * When a ClientHello asserting the status_request or status_request_v2
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   662
 * extensions is accepted by the server, it will fetch and return one
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   663
 * or more status responses in this handshake message.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   664
 *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   665
 * NOTE: Like the Certificate handshake message, this can potentially
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   666
 * be a very large message both due to the size of multiple status
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   667
 * responses and the certificate chains that are often attached to them.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   668
 * Up to 2^24 bytes of status responses may be sent, possibly fragmented
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   669
 * over multiple TLS records.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   670
 */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   671
static final class CertificateStatus extends HandshakeMessage
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   672
{
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   673
    private final StatusRequestType statusType;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   674
    private int encodedResponsesLen;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   675
    private int messageLength = -1;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   676
    private List<byte[]> encodedResponses;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   677
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   678
    @Override
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   679
    int messageType() { return ht_certificate_status; }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   680
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   681
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   682
     * Create a CertificateStatus message from the certificates and their
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   683
     * respective OCSP responses
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   684
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   685
     * @param type an indication of the type of response (OCSP or OCSP_MULTI)
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   686
     * @param responses a {@code List} of OCSP responses in DER-encoded form.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   687
     *      For the OCSP type, only the first entry in the response list is
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   688
     *      used, and must correspond to the end-entity certificate sent to the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   689
     *      peer.  Zero-length or null values for the response data are not
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   690
     *      allowed for the OCSP type.  For the OCSP_MULTI type, each entry in
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   691
     *      the list should match its corresponding certificate sent in the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   692
     *      Server Certificate message.  Where an OCSP response does not exist,
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   693
     *      either a zero-length array or a null value should be used.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   694
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   695
     * @throws SSLException if an unsupported StatusRequestType or invalid
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   696
     *      OCSP response data is provided.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   697
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   698
    CertificateStatus(StatusRequestType type, X509Certificate[] chain,
36442
ce858cc19004 8132942: ServerHandshaker should not throw SSLHandshakeException when CertificateStatus constructor is called with invalid arguments
jnimeh
parents: 35298
diff changeset
   699
            Map<X509Certificate, byte[]> responses) {
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   700
        statusType = type;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   701
        encodedResponsesLen = 0;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   702
        encodedResponses = new ArrayList<>(chain.length);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   703
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   704
        Objects.requireNonNull(chain, "Null chain not allowed");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   705
        Objects.requireNonNull(responses, "Null responses not allowed");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   706
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   707
        if (statusType == StatusRequestType.OCSP) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   708
            // Just get the response for the end-entity certificate
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   709
            byte[] respDER = responses.get(chain[0]);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   710
            if (respDER != null && respDER.length > 0) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   711
                encodedResponses.add(respDER);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   712
                encodedResponsesLen = 3 + respDER.length;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   713
            } else {
36442
ce858cc19004 8132942: ServerHandshaker should not throw SSLHandshakeException when CertificateStatus constructor is called with invalid arguments
jnimeh
parents: 35298
diff changeset
   714
                throw new IllegalArgumentException("Zero-length or null " +
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   715
                        "OCSP Response");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   716
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   717
        } else if (statusType == StatusRequestType.OCSP_MULTI) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   718
            for (X509Certificate cert : chain) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   719
                byte[] respDER = responses.get(cert);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   720
                if (respDER != null) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   721
                    encodedResponses.add(respDER);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   722
                    encodedResponsesLen += (respDER.length + 3);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   723
                } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   724
                    // If we cannot find a response for a given certificate
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   725
                    // then use a zero-length placeholder.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   726
                    encodedResponses.add(new byte[0]);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   727
                    encodedResponsesLen += 3;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   728
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   729
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   730
        } else {
36442
ce858cc19004 8132942: ServerHandshaker should not throw SSLHandshakeException when CertificateStatus constructor is called with invalid arguments
jnimeh
parents: 35298
diff changeset
   731
            throw new IllegalArgumentException(
ce858cc19004 8132942: ServerHandshaker should not throw SSLHandshakeException when CertificateStatus constructor is called with invalid arguments
jnimeh
parents: 35298
diff changeset
   732
                    "Unsupported StatusResponseType: " + statusType);
32032
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   733
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   734
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   735
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   736
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   737
     * Decode the CertificateStatus handshake message coming from a
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   738
     * {@code HandshakeInputStream}.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   739
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   740
     * @param input the {@code HandshakeInputStream} containing the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   741
     * CertificateStatus message bytes.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   742
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   743
     * @throws SSLHandshakeException if a zero-length response is found in the
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   744
     * OCSP response type, or an unsupported response type is detected.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   745
     * @throws IOException if a decoding error occurs.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   746
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   747
    CertificateStatus(HandshakeInStream input) throws IOException {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   748
        encodedResponsesLen = 0;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   749
        encodedResponses = new ArrayList<>();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   750
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   751
        statusType = StatusRequestType.get(input.getInt8());
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   752
        if (statusType == StatusRequestType.OCSP) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   753
            byte[] respDER = input.getBytes24();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   754
            // Convert the incoming bytes to a OCSPResponse strucutre
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   755
            if (respDER.length > 0) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   756
                encodedResponses.add(respDER);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   757
                encodedResponsesLen = 3 + respDER.length;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   758
            } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   759
                throw new SSLHandshakeException("Zero-length OCSP Response");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   760
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   761
        } else if (statusType == StatusRequestType.OCSP_MULTI) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   762
            int respListLen = input.getInt24();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   763
            encodedResponsesLen = respListLen;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   764
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   765
            // Add each OCSP reponse into the array list in the order
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   766
            // we receive them off the wire.  A zero-length array is
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   767
            // allowed for ocsp_multi, and means that a response for
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   768
            // a given certificate is not available.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   769
            while (respListLen > 0) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   770
                byte[] respDER = input.getBytes24();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   771
                encodedResponses.add(respDER);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   772
                respListLen -= (respDER.length + 3);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   773
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   774
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   775
            if (respListLen != 0) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   776
                throw new SSLHandshakeException(
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   777
                        "Bad OCSP response list length");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   778
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   779
        } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   780
            throw new SSLHandshakeException("Unsupported StatusResponseType: " +
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   781
                    statusType);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   782
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   783
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   784
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   785
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   786
     * Get the length of the CertificateStatus message.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   787
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   788
     * @return the length of the message in bytes.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   789
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   790
    @Override
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   791
    int messageLength() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   792
        int len = 1;            // Length + Status type
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   793
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   794
        if (messageLength == -1) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   795
            if (statusType == StatusRequestType.OCSP) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   796
                len += encodedResponsesLen;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   797
            } else if (statusType == StatusRequestType.OCSP_MULTI) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   798
                len += 3 + encodedResponsesLen;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   799
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   800
            messageLength = len;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   801
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   802
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   803
        return messageLength;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   804
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   805
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   806
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   807
     * Encode the CertificateStatus handshake message and place it on a
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   808
     * {@code HandshakeOutputStream}.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   809
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   810
     * @param s the HandshakeOutputStream that will the message bytes.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   811
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   812
     * @throws IOException if an encoding error occurs.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   813
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   814
    @Override
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   815
    void send(HandshakeOutStream s) throws IOException {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   816
        s.putInt8(statusType.id);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   817
        if (statusType == StatusRequestType.OCSP) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   818
            s.putBytes24(encodedResponses.get(0));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   819
        } else if (statusType == StatusRequestType.OCSP_MULTI) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   820
            s.putInt24(encodedResponsesLen);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   821
            for (byte[] respBytes : encodedResponses) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   822
                if (respBytes != null) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   823
                    s.putBytes24(respBytes);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   824
                } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   825
                    s.putBytes24(null);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   826
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   827
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   828
        } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   829
            // It is highly unlikely that we will fall into this section of
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   830
            // the code.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   831
            throw new SSLHandshakeException("Unsupported status_type: " +
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   832
                    statusType.id);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   833
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   834
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   835
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   836
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   837
     * Display a human-readable representation of the CertificateStatus message.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   838
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   839
     * @param s the PrintStream used to display the message data.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   840
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   841
     * @throws IOException if any errors occur while parsing the OCSP response
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   842
     * bytes into a readable form.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   843
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   844
    @Override
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   845
    void print(PrintStream s) throws IOException {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   846
        s.println("*** CertificateStatus");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   847
        if (debug != null && Debug.isOn("verbose")) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   848
            s.println("Type: " + statusType);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   849
            if (statusType == StatusRequestType.OCSP) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   850
                OCSPResponse oResp = new OCSPResponse(encodedResponses.get(0));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   851
                s.println(oResp);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   852
            } else if (statusType == StatusRequestType.OCSP_MULTI) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   853
                int numResponses = encodedResponses.size();
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   854
                s.println(numResponses +
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   855
                        (numResponses == 1 ? " entry:" : " entries:"));
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   856
                for (byte[] respDER : encodedResponses) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   857
                    if (respDER.length > 0) {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   858
                        OCSPResponse oResp = new OCSPResponse(respDER);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   859
                        s.println(oResp);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   860
                    } else {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   861
                        s.println("<Zero-length entry>");
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   862
                    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   863
                }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   864
            }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   865
        }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   866
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   867
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   868
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   869
     * Get the type of CertificateStatus message
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   870
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   871
     * @return the {@code StatusRequestType} for this CertificateStatus
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   872
     *      message.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   873
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   874
    StatusRequestType getType() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   875
        return statusType;
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   876
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   877
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   878
    /**
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   879
     * Get the list of non-zero length OCSP responses.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   880
     * The responses returned in this list can be used to map to
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   881
     * {@code X509Certificate} objects provided by the peer and
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   882
     * provided to a {@code PKIXRevocationChecker}.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   883
     *
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   884
     * @return an unmodifiable List of zero or more byte arrays, each one
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   885
     *      consisting of a single status response.
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   886
     */
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   887
    List<byte[]> getResponses() {
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   888
        return Collections.unmodifiableList(encodedResponses);
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   889
    }
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   890
}
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   891
22badc53802f 8046321: OCSP Stapling for TLS
jnimeh
parents: 31695
diff changeset
   892
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
 * ServerKeyExchange ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
 * The cipher suite selected, when combined with the certificate exchanged,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
 * implies one of several different kinds of key exchange.  Most current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
 * cipher suites require the server to send more than its certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
 * The primary exceptions are when a server sends an encryption-capable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
 * RSA public key in its cert, to be used with RSA (or RSA_export) key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
 * exchange; and when a server sends its Diffie-Hellman cert.  Those kinds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
 * of key exchange do not require a ServerKeyExchange message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
 * Key exchange can be viewed as having three modes, which are explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
 * for the Diffie-Hellman flavors and poorly specified for RSA ones:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
 *      - "Ephemeral" keys.  Here, a "temporary" key is allocated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
 *        server, and signed.  Diffie-Hellman keys signed using RSA or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
 *        DSS are ephemeral (DHE flavor).  RSA keys get used to do the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
 *        thing, to cut the key size down to 512 bits (export restrictions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
 *        or for signing-only RSA certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
 *      - Anonymity.  Here no server certificate is sent, only the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
 *        key of the server.  This case is subject to man-in-the-middle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
 *        attacks.  This can be done with Diffie-Hellman keys (DH_anon) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
 *        with RSA keys, but is only used in SSLv3 for DH_anon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
 *      - "Normal" case.  Here a server certificate is sent, and the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
 *        key there is used directly in exchanging the premaster secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
 *        For example, Diffie-Hellman "DH" flavor, and any RSA flavor with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
 *        only 512 bit keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
 * If a server certificate is sent, there is no anonymity.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
 * when a certificate is sent, ephemeral keys may still be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
 * exchange the premaster secret.  That's how RSA_EXPORT often works,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
 * as well as how the DHE_* flavors work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
 */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
   928
abstract static class ServerKeyExchange extends HandshakeMessage
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
{
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   930
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    int messageType() { return ht_server_key_exchange; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
 * Using RSA for Key Exchange:  exchange a session key that's not as big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
 * as the signing-only key.  Used for export applications, since exported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
 * RSA encryption keys can't be bigger than 512 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
 * This is never used when keys are 512 bits or smaller, and isn't used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
 * on "US Domestic" ciphers in any case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
class RSA_ServerKeyExchange extends ServerKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
{
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
   946
    private byte[] rsa_modulus;     // 1 to 2^16 - 1 bytes
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
   947
    private byte[] rsa_exponent;    // 1 to 2^16 - 1 bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    private Signature signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    private byte[] signatureBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * Hash the nonces and the ephemeral RSA public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
   955
    private void updateSignature(byte[] clntNonce, byte[] svrNonce)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        int tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        signature.update(clntNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        signature.update(svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        tmp = rsa_modulus.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        signature.update((byte)(tmp >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        signature.update((byte)(tmp & 0x0ff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        signature.update(rsa_modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        tmp = rsa_exponent.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        signature.update((byte)(tmp >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        signature.update((byte)(tmp & 0x0ff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        signature.update(rsa_exponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * Construct an RSA server key exchange message, using data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * known _only_ to the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * The client knows the public key corresponding to this private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * key, from the Certificate message sent previously.  To comply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * with US export regulations we use short RSA keys ... either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * long term ones in the server's X509 cert, or else ephemeral
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * ones sent using this message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    RSA_ServerKeyExchange(PublicKey ephemeralKey, PrivateKey privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            RandomCookie clntNonce, RandomCookie svrNonce, SecureRandom sr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        RSAPublicKeySpec rsaKey = JsseJce.getRSAPublicKeySpec(ephemeralKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        rsa_modulus = toByteArray(rsaKey.getModulus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        rsa_exponent = toByteArray(rsaKey.getPublicExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        signature = RSASignature.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        signature.initSign(privateKey, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        updateSignature(clntNonce.random_bytes, svrNonce.random_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        signatureBytes = signature.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * Parse an RSA server key exchange message, using data known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * to the client (and, in some situations, eavesdroppers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    RSA_ServerKeyExchange(HandshakeInStream input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            throws IOException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        signature = RSASignature.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        rsa_modulus = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        rsa_exponent = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        signatureBytes = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * Get the ephemeral RSA public key that will be used in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * SSL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    PublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            KeyFactory kfac = JsseJce.getKeyFactory("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            // modulus and exponent are always positive
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1017
            RSAPublicKeySpec kspec = new RSAPublicKeySpec(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1018
                new BigInteger(1, rsa_modulus),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1019
                new BigInteger(1, rsa_exponent));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            return kfac.generatePublic(kspec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Verify the signed temporary key using the hashes computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * from it and the two nonces.  This is called by clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * with "exportable" RSA flavors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    boolean verify(PublicKey certifiedKey, RandomCookie clntNonce,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            RandomCookie svrNonce) throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        signature.initVerify(certifiedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        updateSignature(clntNonce.random_bytes, svrNonce.random_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        return signature.verify(signatureBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1038
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        return 6 + rsa_modulus.length + rsa_exponent.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
               + signatureBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1044
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        s.putBytes16(rsa_modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        s.putBytes16(rsa_exponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        s.putBytes16(signatureBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1051
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        s.println("*** RSA ServerKeyExchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            Debug.println(s, "RSA Modulus", rsa_modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            Debug.println(s, "RSA Public Exponent", rsa_exponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
 * Using Diffie-Hellman algorithm for key exchange.  All we really need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
 * do is securely get Diffie-Hellman keys (using the same P, G parameters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
 * to our peer, then we automatically have a shared secret without need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
 * to exchange any more data.  (D-H only solutions, such as SKIP, could
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
 * eliminate key exchange negotiations and get faster connection setup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
 * But they still need a signature algorithm like DSS/DSA to support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
 * trusted distribution of keys without relying on unscalable physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
 * key distribution systems.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
 * This class supports several DH-based key exchange algorithms, though
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
 * perhaps eventually each deserves its own class.  Notably, this has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
 * basic support for DH_anon and its DHE_DSS and DHE_RSA signed variants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
class DH_ServerKeyExchange extends ServerKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    // Fix message encoding, see 4348279
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  1081
    private static final boolean dhKeyExchangeFix =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        Debug.getBooleanProperty("com.sun.net.ssl.dhKeyExchangeFix", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1084
    private byte[]                dh_p;        // 1 to 2^16 - 1 bytes
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1085
    private byte[]                dh_g;        // 1 to 2^16 - 1 bytes
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1086
    private byte[]                dh_Ys;       // 1 to 2^16 - 1 bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1088
    private byte[]                signature;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1090
    // protocol version being established using this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1091
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1092
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1093
    // the preferable signature algorithm used by this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1094
    private SignatureAndHashAlgorithm preferableSignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1095
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * Construct from initialized DH key object, for DH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1100
    DH_ServerKeyExchange(DHCrypt obj, ProtocolVersion protocolVersion) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1101
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1102
        this.preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1103
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1104
        // The DH key has been validated in the constructor of DHCrypt.
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1105
        setValues(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        signature = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * Construct from initialized DH key object and the key associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * with the cert chain which was sent ... for DHE_DSS and DHE_RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * key exchange.  (Constructor called by server.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1114
    DH_ServerKeyExchange(DHCrypt obj, PrivateKey key, byte[] clntNonce,
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1115
            byte[] svrNonce, SecureRandom sr,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1116
            SignatureAndHashAlgorithm signAlgorithm,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1117
            ProtocolVersion protocolVersion) throws GeneralSecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1119
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1120
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1121
        // The DH key has been validated in the constructor of DHCrypt.
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1122
        setValues(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        Signature sig;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1125
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1126
            this.preferableSignatureAlgorithm = signAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1127
            sig = JsseJce.getSignature(signAlgorithm.getAlgorithmName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1129
            this.preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1130
            if (key.getAlgorithm().equals("DSA")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1131
                sig = JsseJce.getSignature(JsseJce.SIGNATURE_DSA);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1132
            } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1133
                sig = RSASignature.getInstance();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1134
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1136
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        sig.initSign(key, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        signature = sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * Construct a DH_ServerKeyExchange message from an input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * stream, as if sent from server to client for use with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * DH_anon key exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1147
    DH_ServerKeyExchange(HandshakeInStream input,
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1148
            ProtocolVersion protocolVersion)
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1149
            throws IOException, GeneralSecurityException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1150
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1151
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1152
        this.preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1153
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        dh_p = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        dh_g = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        dh_Ys = input.getBytes16();
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1157
        KeyUtil.validate(new DHPublicKeySpec(new BigInteger(1, dh_Ys),
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1158
                                             new BigInteger(1, dh_p),
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1159
                                             new BigInteger(1, dh_g)));
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1160
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        signature = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * Construct a DH_ServerKeyExchange message from an input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * and a certificate, as if sent from server to client for use with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * DHE_DSS or DHE_RSA key exchange.  (Called by client.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    DH_ServerKeyExchange(HandshakeInStream input, PublicKey publicKey,
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1170
            byte[] clntNonce, byte[] svrNonce, int messageSize,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1171
            Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1172
            ProtocolVersion protocolVersion)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            throws IOException, GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1175
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1176
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1177
        // read params: ServerDHParams
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        dh_p = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        dh_g = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        dh_Ys = input.getBytes16();
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1181
        KeyUtil.validate(new DHPublicKeySpec(new BigInteger(1, dh_Ys),
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1182
                                             new BigInteger(1, dh_p),
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 10336
diff changeset
  1183
                                             new BigInteger(1, dh_g)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1185
        // read the signature and hash algorithm
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1186
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1187
            int hash = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1188
            int signature = input.getInt8();    // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1189
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1190
            preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1191
                SignatureAndHashAlgorithm.valueOf(hash, signature, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1192
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1193
            // Is it a local supported signature algorithm?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1194
            if (!localSupportedSignAlgs.contains(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1195
                    preferableSignatureAlgorithm)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1196
                throw new SSLHandshakeException(
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1197
                    "Unsupported SignatureAndHashAlgorithm in " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1198
                    "ServerKeyExchange message: " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1199
                    preferableSignatureAlgorithm);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1200
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1201
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1202
            this.preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1203
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1204
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1205
        // read the signature
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1206
        byte[] signature;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        if (dhKeyExchangeFix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            signature = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            messageSize -= (dh_p.length + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            messageSize -= (dh_g.length + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            messageSize -= (dh_Ys.length + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            signature = new byte[messageSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            input.read(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        Signature sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        String algorithm = publicKey.getAlgorithm();
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1220
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1221
            sig = JsseJce.getSignature(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1222
                        preferableSignatureAlgorithm.getAlgorithmName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1224
                switch (algorithm) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1225
                    case "DSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1226
                        sig = JsseJce.getSignature(JsseJce.SIGNATURE_DSA);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1227
                        break;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1228
                    case "RSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1229
                        sig = RSASignature.getInstance();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1230
                        break;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1231
                    default:
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1232
                        throw new SSLKeyException(
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1233
                            "neither an RSA or a DSA key: " + algorithm);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1234
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        sig.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        if (sig.verify(signature) == false ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            throw new SSLKeyException("Server D-H key verification failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1245
    /* Return the Diffie-Hellman modulus */
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1246
    BigInteger getModulus() {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1247
        return new BigInteger(1, dh_p);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1248
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1249
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1250
    /* Return the Diffie-Hellman base/generator */
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1251
    BigInteger getBase() {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1252
        return new BigInteger(1, dh_g);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1253
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1254
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1255
    /* Return the server's Diffie-Hellman public key */
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1256
    BigInteger getServerPublicKey() {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1257
        return new BigInteger(1, dh_Ys);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1258
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1259
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1260
    /*
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1261
     * Update sig with nonces and Diffie-Hellman public key.
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1262
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1263
    private void updateSignature(Signature sig, byte[] clntNonce,
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1264
            byte[] svrNonce) throws SignatureException {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1265
        int tmp;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1266
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1267
        sig.update(clntNonce);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1268
        sig.update(svrNonce);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1269
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1270
        tmp = dh_p.length;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1271
        sig.update((byte)(tmp >> 8));
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1272
        sig.update((byte)(tmp & 0x0ff));
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1273
        sig.update(dh_p);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1274
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1275
        tmp = dh_g.length;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1276
        sig.update((byte)(tmp >> 8));
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1277
        sig.update((byte)(tmp & 0x0ff));
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1278
        sig.update(dh_g);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1279
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1280
        tmp = dh_Ys.length;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1281
        sig.update((byte)(tmp >> 8));
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1282
        sig.update((byte)(tmp & 0x0ff));
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1283
        sig.update(dh_Ys);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1284
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1285
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1286
    private void setValues(DHCrypt obj) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1287
        dh_p = toByteArray(obj.getModulus());
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1288
        dh_g = toByteArray(obj.getBase());
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1289
        dh_Ys = toByteArray(obj.getPublicKey());
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1290
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1291
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1292
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        int temp = 6;   // overhead for p, g, y(s) values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        temp += dh_p.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        temp += dh_g.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        temp += dh_Ys.length;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1299
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        if (signature != null) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1301
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1302
                temp += SignatureAndHashAlgorithm.sizeInRecord();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1303
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1304
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            temp += signature.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            if (dhKeyExchangeFix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                temp += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1310
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        return temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1314
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        s.putBytes16(dh_p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        s.putBytes16(dh_g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        s.putBytes16(dh_Ys);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1319
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        if (signature != null) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1321
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1322
                s.putInt8(preferableSignatureAlgorithm.getHashValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1323
                s.putInt8(preferableSignatureAlgorithm.getSignatureValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1324
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1325
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            if (dhKeyExchangeFix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                s.putBytes16(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                s.write(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1334
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        s.println("*** Diffie-Hellman ServerKeyExchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            Debug.println(s, "DH Modulus", dh_p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            Debug.println(s, "DH Base", dh_g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            Debug.println(s, "Server DH Public Key", dh_Ys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            if (signature == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                s.println("Anonymous");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            } else {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1346
                if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1347
                    s.println("Signature Algorithm " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1348
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1349
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1350
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                s.println("Signed with a DSA or RSA public key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
 * ECDH server key exchange message. Sent by the server for ECDHE and ECDH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
 * ciphersuites to communicate its ephemeral public key (including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
 * EC domain parameters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
 * We support named curves only, no explicitly encoded curves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
static final
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1365
class ECDH_ServerKeyExchange extends ServerKeyExchange {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    // constants for ECCurveType
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  1368
    private static final int CURVE_EXPLICIT_PRIME = 1;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  1369
    private static final int CURVE_EXPLICIT_CHAR2 = 2;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  1370
    private static final int CURVE_NAMED_CURVE    = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1372
    // id of the named group we are using
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1373
    private int groupId;
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1374
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    // encoded public point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    private byte[] pointBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    // signature bytes (or null if anonymous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    private byte[] signatureBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    // public key object encapsulated in this message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    private ECPublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1384
    // protocol version being established using this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1385
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1386
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1387
    // the preferable signature algorithm used by this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1388
    private SignatureAndHashAlgorithm preferableSignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1389
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    ECDH_ServerKeyExchange(ECDHCrypt obj, PrivateKey privateKey,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1391
            byte[] clntNonce, byte[] svrNonce, SecureRandom sr,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1392
            SignatureAndHashAlgorithm signAlgorithm,
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1393
            ProtocolVersion protocolVersion)
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1394
            throws SSLHandshakeException, GeneralSecurityException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1395
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1396
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1397
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        publicKey = (ECPublicKey)obj.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        ECParameterSpec params = publicKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        ECPoint point = publicKey.getW();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        pointBytes = JsseJce.encodePoint(point, params.getCurve());
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1402
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1403
        NamedGroup namedGroup = NamedGroup.valueOf(params);
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1404
        if ((namedGroup == null) || (namedGroup.oid == null) ){
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1405
            // unlikely
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1406
            throw new SSLHandshakeException(
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1407
                "Unnamed EC parameter spec: " + params);
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1408
        }
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1409
        groupId = namedGroup.id;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        if (privateKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            // ECDH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1416
        Signature sig;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1417
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1418
            this.preferableSignatureAlgorithm = signAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1419
            sig = JsseJce.getSignature(signAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1420
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1421
            sig = getSignature(privateKey.getAlgorithm());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1422
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1423
        sig.initSign(privateKey);  // where is the SecureRandom?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        signatureBytes = sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * Parse an ECDH server key exchange message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    ECDH_ServerKeyExchange(HandshakeInStream input, PublicKey signingKey,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1433
            byte[] clntNonce, byte[] svrNonce,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1434
            Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1435
            ProtocolVersion protocolVersion)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            throws IOException, GeneralSecurityException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1437
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1438
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1439
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1440
        // read params: ServerECDHParams
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        int curveType = input.getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        ECParameterSpec parameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        // These parsing errors should never occur as we negotiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        // the supported curves during the exchange of the Hello messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        if (curveType == CURVE_NAMED_CURVE) {
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1446
            groupId = input.getInt16();
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1447
            NamedGroup namedGroup = NamedGroup.valueOf(groupId);
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1448
            if (namedGroup == null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1449
                throw new SSLHandshakeException(
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1450
                    "Unknown named group ID: " + groupId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            }
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1452
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1453
            if (!SupportedGroupsExtension.supports(namedGroup)) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1454
                throw new SSLHandshakeException(
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1455
                    "Unsupported named group: " + namedGroup);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            }
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1457
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1458
            if (namedGroup.oid == null) {
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1459
                throw new SSLHandshakeException(
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1460
                    "Unknown named EC curve: " + namedGroup);
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1461
            }
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1462
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1463
            parameters = JsseJce.getECParameterSpec(namedGroup.oid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            if (parameters == null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1465
                throw new SSLHandshakeException(
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1466
                    "No supported EC parameter for named group: " + namedGroup);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1469
            throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1470
                "Unsupported ECCurveType: " + curveType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        pointBytes = input.getBytes8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        ECPoint point = JsseJce.decodePoint(pointBytes, parameters.getCurve());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        KeyFactory factory = JsseJce.getKeyFactory("EC");
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1476
        publicKey = (ECPublicKey)factory.generatePublic(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1477
            new ECPublicKeySpec(point, parameters));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        if (signingKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            // ECDH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1484
        // read the signature and hash algorithm
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1485
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1486
            int hash = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1487
            int signature = input.getInt8();    // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1488
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1489
            preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1490
                SignatureAndHashAlgorithm.valueOf(hash, signature, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1491
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1492
            // Is it a local supported signature algorithm?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1493
            if (!localSupportedSignAlgs.contains(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1494
                    preferableSignatureAlgorithm)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1495
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1496
                        "Unsupported SignatureAndHashAlgorithm in " +
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1497
                        "ServerKeyExchange message: " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1498
                        preferableSignatureAlgorithm);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1499
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1500
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1501
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1502
        // read the signature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        signatureBytes = input.getBytes16();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1504
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1505
        // verify the signature
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1506
        Signature sig;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1507
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1508
            sig = JsseJce.getSignature(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1509
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1510
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1511
            sig = getSignature(signingKey.getAlgorithm());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1512
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        sig.initVerify(signingKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        if (sig.verify(signatureBytes) == false ) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1518
            throw new SSLKeyException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1519
                "Invalid signature on ECDH server key exchange message");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * Get the ephemeral EC public key encapsulated in this message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    ECPublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1530
    private static Signature getSignature(String keyAlgorithm)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1531
            throws NoSuchAlgorithmException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1532
            switch (keyAlgorithm) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1533
                case "EC":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1534
                    return JsseJce.getSignature(JsseJce.SIGNATURE_ECDSA);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1535
                case "RSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1536
                    return RSASignature.getInstance();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1537
                default:
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1538
                    throw new NoSuchAlgorithmException(
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1539
                        "neither an RSA or a EC key : " + keyAlgorithm);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1540
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1543
    private void updateSignature(Signature sig, byte[] clntNonce,
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1544
            byte[] svrNonce) throws SignatureException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        sig.update(clntNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        sig.update(svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        sig.update((byte)CURVE_NAMED_CURVE);
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1549
        sig.update((byte)(groupId >> 8));
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1550
        sig.update((byte)groupId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        sig.update((byte)pointBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        sig.update(pointBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1555
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    int messageLength() {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1557
        int sigLen = 0;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1558
        if (signatureBytes != null) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1559
            sigLen = 2 + signatureBytes.length;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1560
            if (protocolVersion.useTLS12PlusSpec()) {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1561
                sigLen += SignatureAndHashAlgorithm.sizeInRecord();
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1562
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1563
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1564
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        return 4 + pointBytes.length + sigLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1568
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        s.putInt8(CURVE_NAMED_CURVE);
45064
b1b45177051b 8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents: 41471
diff changeset
  1571
        s.putInt16(groupId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        s.putBytes8(pointBytes);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1573
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        if (signatureBytes != null) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1575
            if (protocolVersion.useTLS12PlusSpec()) {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1576
                s.putInt8(preferableSignatureAlgorithm.getHashValue());
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1577
                s.putInt8(preferableSignatureAlgorithm.getSignatureValue());
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1578
            }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1579
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            s.putBytes16(signatureBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1584
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        s.println("*** ECDH ServerKeyExchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        if (debug != null && Debug.isOn("verbose")) {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1589
            if (signatureBytes == null) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1590
                s.println("Anonymous");
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1591
            } else {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1592
                if (protocolVersion.useTLS12PlusSpec()) {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1593
                    s.println("Signature Algorithm " +
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1594
                            preferableSignatureAlgorithm.getAlgorithmName());
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1595
                }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1596
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1597
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            s.println("Server key: " + publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
static final class DistinguishedName {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * DER encoded distinguished name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * TLS requires that its not longer than 65535 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1609
    byte[] name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    DistinguishedName(HandshakeInStream input) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        name = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    DistinguishedName(X500Principal dn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        name = dn.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    X500Principal getX500Principal() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            return new X500Principal(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        } catch (IllegalArgumentException e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1623
            throw (SSLProtocolException)new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1624
                e.getMessage()).initCause(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        return 2 + name.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    void send(HandshakeOutStream output) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        output.putBytes16(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    void print(PrintStream output) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        X500Principal principal = new X500Principal(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        output.println("<" + principal.toString() + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
 * CertificateRequest ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
 * Authenticated servers may ask clients to authenticate themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
 * in turn, using this message.
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1647
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1648
 * Prior to TLS 1.2, the structure of the message is defined as:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1649
 *     struct {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1650
 *         ClientCertificateType certificate_types<1..2^8-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1651
 *         DistinguishedName certificate_authorities<0..2^16-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1652
 *     } CertificateRequest;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1653
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1654
 * In TLS 1.2, the structure is changed to:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1655
 *     struct {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1656
 *         ClientCertificateType certificate_types<1..2^8-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1657
 *         SignatureAndHashAlgorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1658
 *           supported_signature_algorithms<2^16-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1659
 *         DistinguishedName certificate_authorities<0..2^16-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1660
 *     } CertificateRequest;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1661
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
class CertificateRequest extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    // enum ClientCertificateType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    static final int   cct_rsa_sign = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    static final int   cct_dss_sign = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
    static final int   cct_rsa_fixed_dh = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    static final int   cct_dss_fixed_dh = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    // The existance of these two values is a bug in the SSL specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    // They are never used in the protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    static final int   cct_rsa_ephemeral_dh = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    static final int   cct_dss_ephemeral_dh = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    // From RFC 4492 (ECC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    static final int    cct_ecdsa_sign       = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
    static final int    cct_rsa_fixed_ecdh   = 65;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    static final int    cct_ecdsa_fixed_ecdh = 66;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  1682
    private static final byte[] TYPES_NO_ECC = { cct_rsa_sign, cct_dss_sign };
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  1683
    private static final byte[] TYPES_ECC =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        { cct_rsa_sign, cct_dss_sign, cct_ecdsa_sign };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1686
    byte[]                types;               // 1 to 255 types
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1687
    DistinguishedName[]   authorities;         // 3 to 2^16 - 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        // ... "3" because that's the smallest DER-encoded X500 DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1690
    // protocol version being established using this CertificateRequest message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1691
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1692
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1693
    // supported_signature_algorithms for TLS 1.2 or later
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1694
    private Collection<SignatureAndHashAlgorithm> algorithms;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1695
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1696
    // length of supported_signature_algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1697
    private int algorithmsLen;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1698
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1699
    CertificateRequest(X509Certificate[] ca, KeyExchange keyExchange,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1700
            Collection<SignatureAndHashAlgorithm> signAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1701
            ProtocolVersion protocolVersion) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1702
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1703
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1704
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        // always use X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        authorities = new DistinguishedName[ca.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        for (int i = 0; i < ca.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            X500Principal x500Principal = ca[i].getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            authorities[i] = new DistinguishedName(x500Principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        // we support RSA, DSS, and ECDSA client authentication and they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        // can be used with all ciphersuites. If this changes, the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        // needs to be adapted to take keyExchange into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        // We only request ECDSA client auth if we have ECC crypto available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        this.types = JsseJce.isEcAvailable() ? TYPES_ECC : TYPES_NO_ECC;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1716
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1717
        // Use supported_signature_algorithms for TLS 1.2 or later.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1718
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1719
            if (signAlgs == null || signAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1720
                throw new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1721
                        "No supported signature algorithms");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1722
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1723
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1724
            algorithms = new ArrayList<SignatureAndHashAlgorithm>(signAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1725
            algorithmsLen =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1726
                SignatureAndHashAlgorithm.sizeInRecord() * algorithms.size();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1727
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1728
            algorithms = new ArrayList<SignatureAndHashAlgorithm>();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1729
            algorithmsLen = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1730
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1733
    CertificateRequest(HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1734
            ProtocolVersion protocolVersion) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1735
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1736
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1737
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1738
        // Read the certificate_types.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        types = input.getBytes8();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1740
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1741
        // Read the supported_signature_algorithms for TLS 1.2 or later.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1742
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1743
            algorithmsLen = input.getInt16();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1744
            if (algorithmsLen < 2) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1745
                throw new SSLProtocolException(
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1746
                    "Invalid supported_signature_algorithms field: " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1747
                    algorithmsLen);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1748
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1749
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1750
            algorithms = new ArrayList<SignatureAndHashAlgorithm>();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1751
            int remains = algorithmsLen;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1752
            int sequence = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1753
            while (remains > 1) {    // needs at least two bytes
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1754
                int hash = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1755
                int signature = input.getInt8();    // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1756
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1757
                SignatureAndHashAlgorithm algorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1758
                    SignatureAndHashAlgorithm.valueOf(hash, signature,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1759
                                                                ++sequence);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1760
                algorithms.add(algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1761
                remains -= 2;  // one byte for hash, one byte for signature
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1762
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1763
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1764
            if (remains != 0) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1765
                throw new SSLProtocolException(
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1766
                    "Invalid supported_signature_algorithms field. remains: " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1767
                    remains);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1768
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1769
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1770
            algorithms = new ArrayList<SignatureAndHashAlgorithm>();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1771
            algorithmsLen = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1772
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1773
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1774
        // read the certificate_authorities
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        int len = input.getInt16();
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
  1776
        ArrayList<DistinguishedName> v = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        while (len >= 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            DistinguishedName dn = new DistinguishedName(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            v.add(dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            len -= dn.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        if (len != 0) {
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1784
            throw new SSLProtocolException(
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1785
                "Bad CertificateRequest DN length: " + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        authorities = v.toArray(new DistinguishedName[v.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    X500Principal[] getAuthorities() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        X500Principal[] ret = new X500Principal[authorities.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        for (int i = 0; i < authorities.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            ret[i] = authorities[i].getX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1799
    Collection<SignatureAndHashAlgorithm> getSignAlgorithms() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1800
        return algorithms;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1801
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1802
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1803
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1804
    int messageType() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1805
        return ht_certificate_request;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1806
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1808
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1809
    int messageLength() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1810
        int len = 1 + types.length + 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1811
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1812
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1813
            len += algorithmsLen + 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1814
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1815
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1816
        for (int i = 0; i < authorities.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            len += authorities[i].length();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1818
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1819
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1823
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1824
    void send(HandshakeOutStream output) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1825
        // put certificate_types
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1826
        output.putBytes8(types);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1828
        // put supported_signature_algorithms
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1829
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1830
            output.putInt16(algorithmsLen);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1831
            for (SignatureAndHashAlgorithm algorithm : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1832
                output.putInt8(algorithm.getHashValue());      // hash
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1833
                output.putInt8(algorithm.getSignatureValue()); // signature
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1834
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1835
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1836
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1837
        // put certificate_authorities
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1838
        int len = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1839
        for (int i = 0; i < authorities.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            len += authorities[i].length();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1841
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        output.putInt16(len);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1844
        for (int i = 0; i < authorities.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            authorities[i].send(output);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1846
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1849
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1850
    void print(PrintStream s) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        s.println("*** CertificateRequest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            s.print("Cert Types: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            for (int i = 0; i < types.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                switch (types[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                  case cct_rsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                    s.print("RSA"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                  case cct_dss_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                    s.print("DSS"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                  case cct_rsa_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                    s.print("Fixed DH (RSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                  case cct_dss_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                    s.print("Fixed DH (DSS sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                  case cct_rsa_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                    s.print("Ephemeral DH (RSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                  case cct_dss_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                    s.print("Ephemeral DH (DSS sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                  case cct_ecdsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                    s.print("ECDSA"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                  case cct_rsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                    s.print("Fixed ECDH (RSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                  case cct_ecdsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                    s.print("Fixed ECDH (ECDSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                  default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                    s.print("Type-" + (types[i] & 0xff)); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                if (i != types.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                    s.print(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            s.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1884
            if (protocolVersion.useTLS12PlusSpec()) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 16100
diff changeset
  1885
                StringBuilder sb = new StringBuilder();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1886
                boolean opened = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1887
                for (SignatureAndHashAlgorithm signAlg : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1888
                    if (opened) {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27804
diff changeset
  1889
                        sb.append(", ").append(signAlg.getAlgorithmName());
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1890
                    } else {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 16100
diff changeset
  1891
                        sb.append(signAlg.getAlgorithmName());
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1892
                        opened = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1893
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1894
                }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 16100
diff changeset
  1895
                s.println("Supported Signature Algorithms: " + sb);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1896
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1897
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            s.println("Cert Authorities:");
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1899
            if (authorities.length == 0) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1900
                s.println("<Empty>");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1901
            } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1902
                for (int i = 0; i < authorities.length; i++) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1903
                    authorities[i].print(s);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1904
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1905
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
 * ServerHelloDone ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
 * When server's done sending its messages in response to the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
 * "hello" (e.g. its own hello, certificate, key exchange message, perhaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
 * client certificate request) it sends this message to flag that it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
 * done that part of the handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
class ServerHelloDone extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
{
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1922
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    int messageType() { return ht_server_hello_done; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    ServerHelloDone() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    ServerHelloDone(HandshakeInStream input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        // nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1932
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
    int messageLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1938
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    void send(HandshakeOutStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        // nothing to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1944
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    void print(PrintStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        s.println("*** ServerHelloDone");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
 * CertificateVerify ... CLIENT --> SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
 * Sent after client sends signature-capable certificates (e.g. not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
 * Diffie-Hellman) to verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
static final class CertificateVerify extends HandshakeMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1960
    // the signature bytes
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1961
    private byte[] signature;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
35298
9f93cbce8c44 8144773: Further reduce use of MD5
xuelei
parents: 34380
diff changeset
  1963
    // protocol version being established using this CertificateVerify message
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1964
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1965
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1966
    // the preferable signature algorithm used by this CertificateVerify message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1967
    private SignatureAndHashAlgorithm preferableSignatureAlgorithm = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     * Create an RSA or DSA signed certificate verify message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1972
    CertificateVerify(ProtocolVersion protocolVersion,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1973
            HandshakeHash handshakeHash, PrivateKey privateKey,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1974
            SecretKey masterSecret, SecureRandom sr,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1975
            SignatureAndHashAlgorithm signAlgorithm)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1976
            throws GeneralSecurityException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1977
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1978
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1979
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        String algorithm = privateKey.getAlgorithm();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1981
        Signature sig = null;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1982
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1983
            this.preferableSignatureAlgorithm = signAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1984
            sig = JsseJce.getSignature(signAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1985
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1986
            sig = getSignature(protocolVersion, algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1987
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        sig.initSign(privateKey, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        updateSignature(sig, protocolVersion, handshakeHash, algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                        masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        signature = sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    // Unmarshal the signed data from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    //
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1997
    CertificateVerify(HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1998
            Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1999
            ProtocolVersion protocolVersion) throws IOException  {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2000
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2001
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2002
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2003
        // read the signature and hash algorithm
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2004
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2005
            int hashAlg = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2006
            int signAlg = input.getInt8();         // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2007
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2008
            preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2009
                SignatureAndHashAlgorithm.valueOf(hashAlg, signAlg, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2010
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2011
            // Is it a local supported signature algorithm?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2012
            if (!localSupportedSignAlgs.contains(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2013
                    preferableSignatureAlgorithm)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2014
                throw new SSLHandshakeException(
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  2015
                    "Unsupported SignatureAndHashAlgorithm in " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  2016
                    "CertificateVerify message: " + preferableSignatureAlgorithm);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2017
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2018
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2019
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2020
        // read the signature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        signature = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    /*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2025
     * Get the preferable signature algorithm used by this message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2026
     */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2027
    SignatureAndHashAlgorithm getPreferableSignatureAlgorithm() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2028
        return preferableSignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2029
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2030
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2031
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * Verify a certificate verify message. Return the result of verification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * if there is a problem throw a GeneralSecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    boolean verify(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            HandshakeHash handshakeHash, PublicKey publicKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            SecretKey masterSecret) throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        String algorithm = publicKey.getAlgorithm();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2039
        Signature sig = null;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2040
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2041
            sig = JsseJce.getSignature(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2042
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2043
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2044
            sig = getSignature(protocolVersion, algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2045
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        sig.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        updateSignature(sig, protocolVersion, handshakeHash, algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                        masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        return sig.verify(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * Get the Signature object appropriate for verification using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * given signature algorithm and protocol version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    private static Signature getSignature(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            String algorithm) throws GeneralSecurityException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2058
            switch (algorithm) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2059
                case "RSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2060
                    return RSASignature.getInternalInstance();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2061
                case "DSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2062
                    return JsseJce.getSignature(JsseJce.SIGNATURE_RAWDSA);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2063
                case "EC":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2064
                    return JsseJce.getSignature(JsseJce.SIGNATURE_RAWECDSA);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2065
                default:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2066
                    throw new SignatureException("Unrecognized algorithm: "
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2067
                        + algorithm);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2068
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * Update the Signature with the data appropriate for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * signature algorithm and protocol version so that the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * ready for signing or verifying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    private static void updateSignature(Signature sig,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            HandshakeHash handshakeHash, String algorithm, SecretKey masterKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            throws SignatureException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2080
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        if (algorithm.equals("RSA")) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2082
            if (!protocolVersion.useTLS12PlusSpec()) {  // TLS1.1-
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2083
                MessageDigest md5Clone = handshakeHash.getMD5Clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2084
                MessageDigest shaClone = handshakeHash.getSHAClone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2085
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2086
                if (!protocolVersion.useTLS10PlusSpec()) {  // SSLv3
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2087
                    updateDigest(md5Clone, MD5_pad1, MD5_pad2, masterKey);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2088
                    updateDigest(shaClone, SHA_pad1, SHA_pad2, masterKey);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2089
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2090
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2091
                // The signature must be an instance of RSASignature, need
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2092
                // to use these hashes directly.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2093
                RSASignature.setHashes(sig, md5Clone, shaClone);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2094
            } else {  // TLS1.2+
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2095
                sig.update(handshakeHash.getAllHandshakeMessages());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        } else { // DSA, ECDSA
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2098
            if (!protocolVersion.useTLS12PlusSpec()) {  // TLS1.1-
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2099
                MessageDigest shaClone = handshakeHash.getSHAClone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2100
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2101
                if (!protocolVersion.useTLS10PlusSpec()) {  // SSLv3
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2102
                    updateDigest(shaClone, SHA_pad1, SHA_pad2, masterKey);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2103
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2104
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2105
                sig.update(shaClone.digest());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2106
            } else {  // TLS1.2+
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2107
                sig.update(handshakeHash.getAllHandshakeMessages());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * Update the MessageDigest for SSLv3 certificate verify or finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * message calculation. The digest must already have been updated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * all preceding handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * Used by the Finished class as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2118
    private static void updateDigest(MessageDigest md,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2119
            byte[] pad1, byte[] pad2,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            SecretKey masterSecret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        // Digest the key bytes if available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        // Otherwise (sensitive key), try digesting the key directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        // That is currently only implemented in SunPKCS11 using a private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        // reflection API, so we avoid that if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        byte[] keyBytes = "RAW".equals(masterSecret.getFormat())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                        ? masterSecret.getEncoded() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        if (keyBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            md.update(keyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            digestKey(md, masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        md.update(pad1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        byte[] temp = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        if (keyBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
            md.update(keyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
            digestKey(md, masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        md.update(pad2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        md.update(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    private static void digestKey(MessageDigest md, SecretKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
        try {
41471
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 39563
diff changeset
  2146
            if (md instanceof MessageDigestSpi2) {
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 39563
diff changeset
  2147
                ((MessageDigestSpi2)md).engineUpdate(key);
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 39563
diff changeset
  2148
            } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2149
                throw new Exception(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2150
                    "Digest does not support implUpdate(SecretKey)");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        } catch (Exception e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2153
            throw new RuntimeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2154
                "Could not obtain encoded key and "
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2155
                + "MessageDigest cannot digest key", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2159
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2160
    int messageType() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2161
        return ht_certificate_verify;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2164
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2165
    int messageLength() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2166
        int temp = 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2167
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2168
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2169
            temp += SignatureAndHashAlgorithm.sizeInRecord();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2170
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2171
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2172
        return temp + signature.length;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2173
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2174
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2175
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
    void send(HandshakeOutStream s) throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2177
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2178
            s.putInt8(preferableSignatureAlgorithm.getHashValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2179
            s.putInt8(preferableSignatureAlgorithm.getSignatureValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2180
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2181
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        s.putBytes16(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2185
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        s.println("*** CertificateVerify");
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2188
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2189
        if (debug != null && Debug.isOn("verbose")) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2190
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2191
                s.println("Signature Algorithm " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2192
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2193
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2194
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
 * FINISHED ... sent by both CLIENT and SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
 * This is the FINISHED message as defined in the SSL and TLS protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
 * Both protocols define this handshake message slightly differently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
 * This class supports both formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
 * When handshaking is finished, each side sends a "change_cipher_spec"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
 * record, then immediately sends a "finished" handshake message prepared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
 * according to the newly adopted cipher spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
 * NOTE that until this is sent, no application data may be passed, unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
 * some non-default cipher suite has already been set up on this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
 * connection (e.g. a previous handshake arranged one).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
static final class Finished extends HandshakeMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
    // constant for a Finished message sent by the client
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  2217
    static final int CLIENT = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
    // constant for a Finished message sent by the server
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  2220
    static final int SERVER = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
    // enum Sender:  "CLNT" and "SRVR"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
    private static final byte[] SSL_CLIENT = { 0x43, 0x4C, 0x4E, 0x54 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
    private static final byte[] SSL_SERVER = { 0x53, 0x52, 0x56, 0x52 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     * Contents of the finished message ("checksum"). For TLS, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
     * is 12 bytes long, for SSLv3 36 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
    private byte[] verifyData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
    /*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2233
     * Current cipher suite we are negotiating.  TLS 1.2 has
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2234
     * ciphersuite-defined PRF algorithms.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2235
     */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2236
    private ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2237
    private CipherSuite cipherSuite;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2238
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2239
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * Create a finished message to send to the remote peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
    Finished(ProtocolVersion protocolVersion, HandshakeHash handshakeHash,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2243
            int sender, SecretKey master, CipherSuite cipherSuite) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2244
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2245
        this.cipherSuite = cipherSuite;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2246
        verifyData = getFinished(handshakeHash, sender, master);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     * Constructor that reads FINISHED message from stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2252
    Finished(ProtocolVersion protocolVersion, HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2253
            CipherSuite cipherSuite) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2254
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2255
        this.cipherSuite = cipherSuite;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2256
        int msgLen = protocolVersion.useTLS10PlusSpec() ?  12 : 36;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        verifyData = new byte[msgLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        input.read(verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     * Verify that the hashes here are what would have been produced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     * according to a given set of inputs.  This is used to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     * both client and server are fully in sync, and that the handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     * computations have been successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2267
    boolean verify(HandshakeHash handshakeHash, int sender, SecretKey master) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2268
        byte[] myFinished = getFinished(handshakeHash, sender, master);
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 31538
diff changeset
  2269
        return MessageDigest.isEqual(myFinished, verifyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     * Perform the actual finished message calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2275
    private byte[] getFinished(HandshakeHash handshakeHash,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2276
            int sender, SecretKey masterKey) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
        byte[] sslLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
        String tlsLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
        if (sender == CLIENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
            sslLabel = SSL_CLIENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            tlsLabel = "client finished";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        } else if (sender == SERVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
            sslLabel = SSL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            tlsLabel = "server finished";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            throw new RuntimeException("Invalid sender: " + sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2288
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2289
        if (protocolVersion.useTLS10PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2290
            // TLS 1.0+
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            try {
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  2292
                byte[] seed;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2293
                String prfAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2294
                PRF prf;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2295
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2296
                // Get the KeyGenerator alg and calculate the seed.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2297
                if (protocolVersion.useTLS12PlusSpec()) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2298
                    // TLS 1.2+ or DTLS 1.2+
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2299
                    seed = handshakeHash.getFinishedHash();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2300
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2301
                    prfAlg = "SunTls12Prf";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2302
                    prf = cipherSuite.prfAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2303
                } else {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2304
                    // TLS 1.0/1.1, DTLS 1.0
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2305
                    MessageDigest md5Clone = handshakeHash.getMD5Clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2306
                    MessageDigest shaClone = handshakeHash.getSHAClone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2307
                    seed = new byte[36];
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2308
                    md5Clone.digest(seed, 0, 16);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2309
                    shaClone.digest(seed, 16, 20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2311
                    prfAlg = "SunTlsPrf";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2312
                    prf = P_NONE;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2313
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2314
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2315
                String prfHashAlg = prf.getPRFHashAlg();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2316
                int prfHashLength = prf.getPRFHashLength();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2317
                int prfBlockSize = prf.getPRFBlockSize();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2318
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2319
                /*
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2320
                 * RFC 5246/7.4.9 says that finished messages can
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2321
                 * be ciphersuite-specific in both length/PRF hash
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2322
                 * algorithm.  If we ever run across a different
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2323
                 * length, this call will need to be updated.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2324
                 */
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
  2325
                @SuppressWarnings("deprecation")
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2326
                TlsPrfParameterSpec spec = new TlsPrfParameterSpec(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2327
                    masterKey, tlsLabel, seed, 12,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2328
                    prfHashAlg, prfHashLength, prfBlockSize);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2329
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2330
                KeyGenerator kg = JsseJce.getKeyGenerator(prfAlg);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2331
                kg.init(spec);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2332
                SecretKey prfKey = kg.generateKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                if ("RAW".equals(prfKey.getFormat()) == false) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2334
                    throw new ProviderException(
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  2335
                        "Invalid PRF output, format must be RAW. " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  2336
                        "Format received: " + prfKey.getFormat());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                byte[] finished = prfKey.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                return finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                throw new RuntimeException("PRF failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            // SSLv3
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2345
            MessageDigest md5Clone = handshakeHash.getMD5Clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2346
            MessageDigest shaClone = handshakeHash.getSHAClone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            updateDigest(md5Clone, sslLabel, MD5_pad1, MD5_pad2, masterKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            updateDigest(shaClone, sslLabel, SHA_pad1, SHA_pad2, masterKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            byte[] finished = new byte[36];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                md5Clone.digest(finished, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                shaClone.digest(finished, 16, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                // cannot occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                throw new RuntimeException("Digest failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            return finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * Update the MessageDigest for SSLv3 finished message calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * The digest must already have been updated with all preceding handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * messages. This operation is almost identical to the certificate verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * hash, reuse that code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
    private static void updateDigest(MessageDigest md, byte[] sender,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            byte[] pad1, byte[] pad2, SecretKey masterSecret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        md.update(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        CertificateVerify.updateDigest(md, pad1, pad2, masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2373
    // get the verify_data of the finished message
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2374
    byte[] getVerifyData() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2375
        return verifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2376
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2377
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2378
    @Override
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2379
    int messageType() { return ht_finished; }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2380
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2381
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        return verifyData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2386
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
    void send(HandshakeOutStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        out.write(verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2391
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        s.println("*** Finished");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            Debug.println(s, "verify_data", verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            s.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
// END of nested classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
}