jdk/src/java.base/share/classes/sun/security/ssl/HandshakeMessage.java
author ascarpino
Wed, 08 Feb 2017 12:08:28 -0800
changeset 43701 fe8c324ba97c
parent 41471 18c0f074ed97
child 45064 b1b45177051b
permissions -rw-r--r--
8160655: Fix denyAfter and usage types for security properties Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36442
ce858cc19004 8132942: ServerHandshaker should not throw SSLHandshakeException when CertificateStatus constructor is called with invalid arguments
jnimeh
parents: 35298
diff changeset
     2
 * Copyright (c) 1996, 2016, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    // id of the curve we are using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    private int curveId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    // encoded public point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    private byte[] pointBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    // signature bytes (or null if anonymous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    private byte[] signatureBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    // public key object encapsulated in this message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    private ECPublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1383
    // protocol version being established using this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1384
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1385
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1386
    // the preferable signature algorithm used by this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1387
    private SignatureAndHashAlgorithm preferableSignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1388
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    ECDH_ServerKeyExchange(ECDHCrypt obj, PrivateKey privateKey,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1390
            byte[] clntNonce, byte[] svrNonce, SecureRandom sr,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1391
            SignatureAndHashAlgorithm signAlgorithm,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1392
            ProtocolVersion protocolVersion) throws GeneralSecurityException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1393
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1394
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1395
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        publicKey = (ECPublicKey)obj.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        ECParameterSpec params = publicKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        ECPoint point = publicKey.getW();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        pointBytes = JsseJce.encodePoint(point, params.getCurve());
37814
844e590a011a 8156502: Use short name of SupportedEllipticCurvesExtension.java
xuelei
parents: 36952
diff changeset
  1400
        curveId = EllipticCurvesExtension.getCurveIndex(params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        if (privateKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            // ECDH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1407
        Signature sig;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1408
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1409
            this.preferableSignatureAlgorithm = signAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1410
            sig = JsseJce.getSignature(signAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1411
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1412
            sig = getSignature(privateKey.getAlgorithm());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1413
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1414
        sig.initSign(privateKey);  // where is the SecureRandom?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        signatureBytes = sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * Parse an ECDH server key exchange message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    ECDH_ServerKeyExchange(HandshakeInStream input, PublicKey signingKey,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1424
            byte[] clntNonce, byte[] svrNonce,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1425
            Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1426
            ProtocolVersion protocolVersion)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            throws IOException, GeneralSecurityException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1428
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1429
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1430
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1431
        // read params: ServerECDHParams
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        int curveType = input.getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        ECParameterSpec parameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        // These parsing errors should never occur as we negotiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        // the supported curves during the exchange of the Hello messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        if (curveType == CURVE_NAMED_CURVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            curveId = input.getInt16();
37814
844e590a011a 8156502: Use short name of SupportedEllipticCurvesExtension.java
xuelei
parents: 36952
diff changeset
  1438
            if (!EllipticCurvesExtension.isSupported(curveId)) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1439
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1440
                    "Unsupported curveId: " + curveId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
37814
844e590a011a 8156502: Use short name of SupportedEllipticCurvesExtension.java
xuelei
parents: 36952
diff changeset
  1442
            String curveOid = EllipticCurvesExtension.getCurveOid(curveId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            if (curveOid == null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1444
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1445
                    "Unknown named curve: " + curveId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            parameters = JsseJce.getECParameterSpec(curveOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            if (parameters == null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1449
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1450
                    "Unsupported curve: " + curveOid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1453
            throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1454
                "Unsupported ECCurveType: " + curveType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        pointBytes = input.getBytes8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        ECPoint point = JsseJce.decodePoint(pointBytes, parameters.getCurve());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        KeyFactory factory = JsseJce.getKeyFactory("EC");
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1460
        publicKey = (ECPublicKey)factory.generatePublic(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1461
            new ECPublicKeySpec(point, parameters));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        if (signingKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            // ECDH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1468
        // read the signature and hash algorithm
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1469
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1470
            int hash = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1471
            int signature = input.getInt8();    // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1472
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1473
            preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1474
                SignatureAndHashAlgorithm.valueOf(hash, signature, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1475
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1476
            // Is it a local supported signature algorithm?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1477
            if (!localSupportedSignAlgs.contains(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1478
                    preferableSignatureAlgorithm)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1479
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1480
                        "Unsupported SignatureAndHashAlgorithm in " +
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1481
                        "ServerKeyExchange message: " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1482
                        preferableSignatureAlgorithm);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1483
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1484
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1485
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1486
        // read the signature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        signatureBytes = input.getBytes16();
7043
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
        // verify the signature
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1490
        Signature sig;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1491
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1492
            sig = JsseJce.getSignature(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1493
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1494
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1495
            sig = getSignature(signingKey.getAlgorithm());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1496
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        sig.initVerify(signingKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        if (sig.verify(signatureBytes) == false ) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1502
            throw new SSLKeyException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1503
                "Invalid signature on ECDH server key exchange message");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * Get the ephemeral EC public key encapsulated in this message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    ECPublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1514
    private static Signature getSignature(String keyAlgorithm)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1515
            throws NoSuchAlgorithmException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1516
            switch (keyAlgorithm) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1517
                case "EC":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1518
                    return JsseJce.getSignature(JsseJce.SIGNATURE_ECDSA);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1519
                case "RSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1520
                    return RSASignature.getInstance();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  1521
                default:
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1522
                    throw new NoSuchAlgorithmException(
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1523
                        "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
  1524
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1527
    private void updateSignature(Signature sig, byte[] clntNonce,
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1528
            byte[] svrNonce) throws SignatureException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        sig.update(clntNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        sig.update(svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        sig.update((byte)CURVE_NAMED_CURVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        sig.update((byte)(curveId >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        sig.update((byte)curveId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        sig.update((byte)pointBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        sig.update(pointBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1539
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    int messageLength() {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1541
        int sigLen = 0;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1542
        if (signatureBytes != null) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1543
            sigLen = 2 + signatureBytes.length;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1544
            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
  1545
                sigLen += SignatureAndHashAlgorithm.sizeInRecord();
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1546
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1547
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1548
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        return 4 + pointBytes.length + sigLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1552
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        s.putInt8(CURVE_NAMED_CURVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        s.putInt16(curveId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        s.putBytes8(pointBytes);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1557
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        if (signatureBytes != null) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1559
            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
  1560
                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
  1561
                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
  1562
            }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1563
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            s.putBytes16(signatureBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        }
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 print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        s.println("*** ECDH ServerKeyExchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        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
  1573
            if (signatureBytes == null) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1574
                s.println("Anonymous");
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1575
            } else {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1576
                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
  1577
                    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
  1578
                            preferableSignatureAlgorithm.getAlgorithmName());
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1579
                }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1580
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1581
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            s.println("Server key: " + publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
static final class DistinguishedName {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * DER encoded distinguished name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * TLS requires that its not longer than 65535 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1593
    byte[] name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    DistinguishedName(HandshakeInStream input) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        name = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    DistinguishedName(X500Principal dn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        name = dn.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
    X500Principal getX500Principal() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            return new X500Principal(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        } catch (IllegalArgumentException e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1607
            throw (SSLProtocolException)new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1608
                e.getMessage()).initCause(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        return 2 + name.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    void send(HandshakeOutStream output) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        output.putBytes16(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    void print(PrintStream output) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        X500Principal principal = new X500Principal(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        output.println("<" + principal.toString() + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
 * CertificateRequest ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
 * Authenticated servers may ask clients to authenticate themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
 * in turn, using this message.
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1631
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1632
 * 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
  1633
 *     struct {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1634
 *         ClientCertificateType certificate_types<1..2^8-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1635
 *         DistinguishedName certificate_authorities<0..2^16-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1636
 *     } CertificateRequest;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1637
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1638
 * In TLS 1.2, the structure is changed to:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1639
 *     struct {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1640
 *         ClientCertificateType certificate_types<1..2^8-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1641
 *         SignatureAndHashAlgorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1642
 *           supported_signature_algorithms<2^16-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1643
 *         DistinguishedName certificate_authorities<0..2^16-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1644
 *     } CertificateRequest;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1645
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
class CertificateRequest extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    // enum ClientCertificateType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    static final int   cct_rsa_sign = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
    static final int   cct_dss_sign = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    static final int   cct_rsa_fixed_dh = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    static final int   cct_dss_fixed_dh = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    // The existance of these two values is a bug in the SSL specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    // They are never used in the protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    static final int   cct_rsa_ephemeral_dh = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    static final int   cct_dss_ephemeral_dh = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    // From RFC 4492 (ECC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    static final int    cct_ecdsa_sign       = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    static final int    cct_rsa_fixed_ecdh   = 65;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    static final int    cct_ecdsa_fixed_ecdh = 66;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  1666
    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
  1667
    private static final byte[] TYPES_ECC =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        { cct_rsa_sign, cct_dss_sign, cct_ecdsa_sign };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1670
    byte[]                types;               // 1 to 255 types
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1671
    DistinguishedName[]   authorities;         // 3 to 2^16 - 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        // ... "3" because that's the smallest DER-encoded X500 DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1674
    // protocol version being established using this CertificateRequest message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1675
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1676
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1677
    // supported_signature_algorithms for TLS 1.2 or later
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1678
    private Collection<SignatureAndHashAlgorithm> algorithms;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1679
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1680
    // length of supported_signature_algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1681
    private int algorithmsLen;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1682
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  1683
    CertificateRequest(X509Certificate[] ca, KeyExchange keyExchange,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1684
            Collection<SignatureAndHashAlgorithm> signAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1685
            ProtocolVersion protocolVersion) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1686
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1687
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1688
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        // always use X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        authorities = new DistinguishedName[ca.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        for (int i = 0; i < ca.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            X500Principal x500Principal = ca[i].getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            authorities[i] = new DistinguishedName(x500Principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        // we support RSA, DSS, and ECDSA client authentication and they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        // can be used with all ciphersuites. If this changes, the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        // needs to be adapted to take keyExchange into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        // We only request ECDSA client auth if we have ECC crypto available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        this.types = JsseJce.isEcAvailable() ? TYPES_ECC : TYPES_NO_ECC;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1700
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1701
        // Use supported_signature_algorithms for TLS 1.2 or later.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1702
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1703
            if (signAlgs == null || signAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1704
                throw new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1705
                        "No supported signature algorithms");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1706
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1707
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1708
            algorithms = new ArrayList<SignatureAndHashAlgorithm>(signAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1709
            algorithmsLen =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1710
                SignatureAndHashAlgorithm.sizeInRecord() * algorithms.size();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1711
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1712
            algorithms = new ArrayList<SignatureAndHashAlgorithm>();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1713
            algorithmsLen = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1714
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1717
    CertificateRequest(HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1718
            ProtocolVersion protocolVersion) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1719
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1720
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1721
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1722
        // Read the certificate_types.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        types = input.getBytes8();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1724
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1725
        // Read the supported_signature_algorithms for TLS 1.2 or later.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1726
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1727
            algorithmsLen = input.getInt16();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1728
            if (algorithmsLen < 2) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1729
                throw new SSLProtocolException(
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1730
                    "Invalid supported_signature_algorithms field: " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1731
                    algorithmsLen);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1732
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1733
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1734
            algorithms = new ArrayList<SignatureAndHashAlgorithm>();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1735
            int remains = algorithmsLen;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1736
            int sequence = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1737
            while (remains > 1) {    // needs at least two bytes
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1738
                int hash = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1739
                int signature = input.getInt8();    // signature algorithm
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
                SignatureAndHashAlgorithm algorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1742
                    SignatureAndHashAlgorithm.valueOf(hash, signature,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1743
                                                                ++sequence);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1744
                algorithms.add(algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1745
                remains -= 2;  // one byte for hash, one byte for signature
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1746
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1747
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1748
            if (remains != 0) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1749
                throw new SSLProtocolException(
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1750
                    "Invalid supported_signature_algorithms field. remains: " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1751
                    remains);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1752
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1753
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1754
            algorithms = new ArrayList<SignatureAndHashAlgorithm>();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1755
            algorithmsLen = 0;
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
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1758
        // read the certificate_authorities
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        int len = input.getInt16();
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
  1760
        ArrayList<DistinguishedName> v = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        while (len >= 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            DistinguishedName dn = new DistinguishedName(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            v.add(dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            len -= dn.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        if (len != 0) {
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1768
            throw new SSLProtocolException(
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1769
                "Bad CertificateRequest DN length: " + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        authorities = v.toArray(new DistinguishedName[v.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    X500Principal[] getAuthorities() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        X500Principal[] ret = new X500Principal[authorities.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        for (int i = 0; i < authorities.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            ret[i] = authorities[i].getX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1783
    Collection<SignatureAndHashAlgorithm> getSignAlgorithms() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1784
        return algorithms;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1785
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1786
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1787
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1788
    int messageType() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1789
        return ht_certificate_request;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1790
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1792
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1793
    int messageLength() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1794
        int len = 1 + types.length + 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1795
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1796
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1797
            len += algorithmsLen + 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1798
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1799
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1800
        for (int i = 0; i < authorities.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            len += authorities[i].length();
7043
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1807
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1808
    void send(HandshakeOutStream output) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1809
        // put certificate_types
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1810
        output.putBytes8(types);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1812
        // put supported_signature_algorithms
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1813
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1814
            output.putInt16(algorithmsLen);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1815
            for (SignatureAndHashAlgorithm algorithm : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1816
                output.putInt8(algorithm.getHashValue());      // hash
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1817
                output.putInt8(algorithm.getSignatureValue()); // signature
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
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1820
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1821
        // put certificate_authorities
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1822
        int len = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1823
        for (int i = 0; i < authorities.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            len += authorities[i].length();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1825
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        output.putInt16(len);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1828
        for (int i = 0; i < authorities.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            authorities[i].send(output);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1830
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1833
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1834
    void print(PrintStream s) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        s.println("*** CertificateRequest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            s.print("Cert Types: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            for (int i = 0; i < types.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                switch (types[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                  case cct_rsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                    s.print("RSA"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                  case cct_dss_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                    s.print("DSS"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                  case cct_rsa_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                    s.print("Fixed DH (RSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                  case cct_dss_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                    s.print("Fixed DH (DSS sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                  case cct_rsa_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                    s.print("Ephemeral DH (RSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                  case cct_dss_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                    s.print("Ephemeral DH (DSS sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                  case cct_ecdsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                    s.print("ECDSA"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                  case cct_rsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                    s.print("Fixed ECDH (RSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                  case cct_ecdsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                    s.print("Fixed ECDH (ECDSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                  default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                    s.print("Type-" + (types[i] & 0xff)); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                if (i != types.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                    s.print(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            s.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1868
            if (protocolVersion.useTLS12PlusSpec()) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 16100
diff changeset
  1869
                StringBuilder sb = new StringBuilder();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1870
                boolean opened = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1871
                for (SignatureAndHashAlgorithm signAlg : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1872
                    if (opened) {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 27804
diff changeset
  1873
                        sb.append(", ").append(signAlg.getAlgorithmName());
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1874
                    } else {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 16100
diff changeset
  1875
                        sb.append(signAlg.getAlgorithmName());
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1876
                        opened = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1877
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1878
                }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 16100
diff changeset
  1879
                s.println("Supported Signature Algorithms: " + sb);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1880
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1881
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            s.println("Cert Authorities:");
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1883
            if (authorities.length == 0) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1884
                s.println("<Empty>");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1885
            } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1886
                for (int i = 0; i < authorities.length; i++) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1887
                    authorities[i].print(s);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1888
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1889
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
 * ServerHelloDone ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
 * When server's done sending its messages in response to the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
 * "hello" (e.g. its own hello, certificate, key exchange message, perhaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
 * client certificate request) it sends this message to flag that it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
 * done that part of the handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
class ServerHelloDone extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
{
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1906
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    int messageType() { return ht_server_hello_done; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    ServerHelloDone() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    ServerHelloDone(HandshakeInStream input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        // nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1916
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    int messageLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    }
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
    void send(HandshakeOutStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        // nothing to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1928
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
    void print(PrintStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        s.println("*** ServerHelloDone");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
 * CertificateVerify ... CLIENT --> SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
 * Sent after client sends signature-capable certificates (e.g. not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
 * Diffie-Hellman) to verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
static final class CertificateVerify extends HandshakeMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1944
    // the signature bytes
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1945
    private byte[] signature;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
35298
9f93cbce8c44 8144773: Further reduce use of MD5
xuelei
parents: 34380
diff changeset
  1947
    // protocol version being established using this CertificateVerify message
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1948
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1949
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1950
    // the preferable signature algorithm used by this CertificateVerify message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1951
    private SignatureAndHashAlgorithm preferableSignatureAlgorithm = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * Create an RSA or DSA signed certificate verify message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1956
    CertificateVerify(ProtocolVersion protocolVersion,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1957
            HandshakeHash handshakeHash, PrivateKey privateKey,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1958
            SecretKey masterSecret, SecureRandom sr,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1959
            SignatureAndHashAlgorithm signAlgorithm)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1960
            throws GeneralSecurityException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1961
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1962
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1963
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        String algorithm = privateKey.getAlgorithm();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1965
        Signature sig = null;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1966
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1967
            this.preferableSignatureAlgorithm = signAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1968
            sig = JsseJce.getSignature(signAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1969
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1970
            sig = getSignature(protocolVersion, algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1971
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        sig.initSign(privateKey, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        updateSignature(sig, protocolVersion, handshakeHash, algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                        masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        signature = sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    // Unmarshal the signed data from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    //
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1981
    CertificateVerify(HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1982
            Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1983
            ProtocolVersion protocolVersion) throws IOException  {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1984
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1985
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1986
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1987
        // read the signature and hash algorithm
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  1988
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1989
            int hashAlg = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1990
            int signAlg = input.getInt8();         // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1991
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1992
            preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1993
                SignatureAndHashAlgorithm.valueOf(hashAlg, signAlg, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1994
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1995
            // Is it a local supported signature algorithm?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1996
            if (!localSupportedSignAlgs.contains(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1997
                    preferableSignatureAlgorithm)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1998
                throw new SSLHandshakeException(
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  1999
                    "Unsupported SignatureAndHashAlgorithm in " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  2000
                    "CertificateVerify message: " + preferableSignatureAlgorithm);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2001
            }
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
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2004
        // read the signature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        signature = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    /*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2009
     * Get the preferable signature algorithm used by this message
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
    SignatureAndHashAlgorithm getPreferableSignatureAlgorithm() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2012
        return preferableSignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2013
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2014
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2015
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * Verify a certificate verify message. Return the result of verification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * if there is a problem throw a GeneralSecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    boolean verify(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            HandshakeHash handshakeHash, PublicKey publicKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
            SecretKey masterSecret) throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        String algorithm = publicKey.getAlgorithm();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2023
        Signature sig = null;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2024
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2025
            sig = JsseJce.getSignature(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2026
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2027
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2028
            sig = getSignature(protocolVersion, algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2029
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        sig.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        updateSignature(sig, protocolVersion, handshakeHash, algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                        masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        return sig.verify(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * Get the Signature object appropriate for verification using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * given signature algorithm and protocol version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
    private static Signature getSignature(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            String algorithm) throws GeneralSecurityException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2042
            switch (algorithm) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2043
                case "RSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2044
                    return RSASignature.getInternalInstance();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2045
                case "DSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2046
                    return JsseJce.getSignature(JsseJce.SIGNATURE_RAWDSA);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2047
                case "EC":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2048
                    return JsseJce.getSignature(JsseJce.SIGNATURE_RAWECDSA);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2049
                default:
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2050
                    throw new SignatureException("Unrecognized algorithm: "
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2051
                        + algorithm);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8991
diff changeset
  2052
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * Update the Signature with the data appropriate for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * signature algorithm and protocol version so that the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * ready for signing or verifying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
    private static void updateSignature(Signature sig,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
            ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            HandshakeHash handshakeHash, String algorithm, SecretKey masterKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
            throws SignatureException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2064
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        if (algorithm.equals("RSA")) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2066
            if (!protocolVersion.useTLS12PlusSpec()) {  // TLS1.1-
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2067
                MessageDigest md5Clone = handshakeHash.getMD5Clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2068
                MessageDigest shaClone = handshakeHash.getSHAClone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2069
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2070
                if (!protocolVersion.useTLS10PlusSpec()) {  // SSLv3
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2071
                    updateDigest(md5Clone, MD5_pad1, MD5_pad2, masterKey);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2072
                    updateDigest(shaClone, SHA_pad1, SHA_pad2, masterKey);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2073
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2074
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2075
                // The signature must be an instance of RSASignature, need
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2076
                // to use these hashes directly.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2077
                RSASignature.setHashes(sig, md5Clone, shaClone);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2078
            } else {  // TLS1.2+
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2079
                sig.update(handshakeHash.getAllHandshakeMessages());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        } else { // DSA, ECDSA
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 shaClone = handshakeHash.getSHAClone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2084
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2085
                if (!protocolVersion.useTLS10PlusSpec()) {  // SSLv3
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2086
                    updateDigest(shaClone, SHA_pad1, SHA_pad2, masterKey);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2087
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2088
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2089
                sig.update(shaClone.digest());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2090
            } else {  // TLS1.2+
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2091
                sig.update(handshakeHash.getAllHandshakeMessages());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * Update the MessageDigest for SSLv3 certificate verify or finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * message calculation. The digest must already have been updated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * all preceding handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * Used by the Finished class as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2102
    private static void updateDigest(MessageDigest md,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2103
            byte[] pad1, byte[] pad2,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            SecretKey masterSecret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        // Digest the key bytes if available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        // Otherwise (sensitive key), try digesting the key directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        // That is currently only implemented in SunPKCS11 using a private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        // reflection API, so we avoid that if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        byte[] keyBytes = "RAW".equals(masterSecret.getFormat())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                        ? masterSecret.getEncoded() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        if (keyBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            md.update(keyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            digestKey(md, masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        md.update(pad1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        byte[] temp = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        if (keyBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            md.update(keyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
            digestKey(md, masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        md.update(pad2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        md.update(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
    private static void digestKey(MessageDigest md, SecretKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        try {
41471
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 39563
diff changeset
  2130
            if (md instanceof MessageDigestSpi2) {
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 39563
diff changeset
  2131
                ((MessageDigestSpi2)md).engineUpdate(key);
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 39563
diff changeset
  2132
            } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2133
                throw new Exception(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2134
                    "Digest does not support implUpdate(SecretKey)");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        } catch (Exception e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2137
            throw new RuntimeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2138
                "Could not obtain encoded key and "
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2139
                + "MessageDigest cannot digest key", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2143
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2144
    int messageType() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2145
        return ht_certificate_verify;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2148
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2149
    int messageLength() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2150
        int temp = 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2151
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2152
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2153
            temp += SignatureAndHashAlgorithm.sizeInRecord();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2154
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2155
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2156
        return temp + signature.length;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2157
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2158
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2159
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
    void send(HandshakeOutStream s) throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2161
        if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2162
            s.putInt8(preferableSignatureAlgorithm.getHashValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2163
            s.putInt8(preferableSignatureAlgorithm.getSignatureValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2164
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2165
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        s.putBytes16(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2169
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        s.println("*** CertificateVerify");
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2172
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2173
        if (debug != null && Debug.isOn("verbose")) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2174
            if (protocolVersion.useTLS12PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2175
                s.println("Signature Algorithm " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2176
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2177
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2178
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
 * FINISHED ... sent by both CLIENT and SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
 * This is the FINISHED message as defined in the SSL and TLS protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
 * Both protocols define this handshake message slightly differently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
 * This class supports both formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
 * When handshaking is finished, each side sends a "change_cipher_spec"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
 * record, then immediately sends a "finished" handshake message prepared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
 * according to the newly adopted cipher spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
 * NOTE that until this is sent, no application data may be passed, unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
 * some non-default cipher suite has already been set up on this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
 * connection (e.g. a previous handshake arranged one).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
static final class Finished extends HandshakeMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
    // constant for a Finished message sent by the client
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  2201
    static final int CLIENT = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    // constant for a Finished message sent by the server
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32032
diff changeset
  2204
    static final int SERVER = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
    // enum Sender:  "CLNT" and "SRVR"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    private static final byte[] SSL_CLIENT = { 0x43, 0x4C, 0x4E, 0x54 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
    private static final byte[] SSL_SERVER = { 0x53, 0x52, 0x56, 0x52 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * Contents of the finished message ("checksum"). For TLS, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * is 12 bytes long, for SSLv3 36 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
    private byte[] verifyData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
    /*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2217
     * Current cipher suite we are negotiating.  TLS 1.2 has
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2218
     * ciphersuite-defined PRF algorithms.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2219
     */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2220
    private ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2221
    private CipherSuite cipherSuite;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2222
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2223
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     * Create a finished message to send to the remote peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
    Finished(ProtocolVersion protocolVersion, HandshakeHash handshakeHash,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2227
            int sender, SecretKey master, CipherSuite cipherSuite) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2228
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2229
        this.cipherSuite = cipherSuite;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2230
        verifyData = getFinished(handshakeHash, sender, master);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     * Constructor that reads FINISHED message from stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2236
    Finished(ProtocolVersion protocolVersion, HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2237
            CipherSuite cipherSuite) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2238
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2239
        this.cipherSuite = cipherSuite;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2240
        int msgLen = protocolVersion.useTLS10PlusSpec() ?  12 : 36;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        verifyData = new byte[msgLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        input.read(verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * Verify that the hashes here are what would have been produced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * according to a given set of inputs.  This is used to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * both client and server are fully in sync, and that the handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * computations have been successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2251
    boolean verify(HandshakeHash handshakeHash, int sender, SecretKey master) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2252
        byte[] myFinished = getFinished(handshakeHash, sender, master);
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 31538
diff changeset
  2253
        return MessageDigest.isEqual(myFinished, verifyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * Perform the actual finished message calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2259
    private byte[] getFinished(HandshakeHash handshakeHash,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2260
            int sender, SecretKey masterKey) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        byte[] sslLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        String tlsLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        if (sender == CLIENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            sslLabel = SSL_CLIENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            tlsLabel = "client finished";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        } else if (sender == SERVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            sslLabel = SSL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            tlsLabel = "server finished";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            throw new RuntimeException("Invalid sender: " + sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2272
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2273
        if (protocolVersion.useTLS10PlusSpec()) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2274
            // TLS 1.0+
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            try {
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
  2276
                byte[] seed;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2277
                String prfAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2278
                PRF prf;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2279
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2280
                // Get the KeyGenerator alg and calculate the seed.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2281
                if (protocolVersion.useTLS12PlusSpec()) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2282
                    // TLS 1.2+ or DTLS 1.2+
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2283
                    seed = handshakeHash.getFinishedHash();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2284
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2285
                    prfAlg = "SunTls12Prf";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2286
                    prf = cipherSuite.prfAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2287
                } else {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29264
diff changeset
  2288
                    // TLS 1.0/1.1, DTLS 1.0
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2289
                    MessageDigest md5Clone = handshakeHash.getMD5Clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2290
                    MessageDigest shaClone = handshakeHash.getSHAClone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2291
                    seed = new byte[36];
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2292
                    md5Clone.digest(seed, 0, 16);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2293
                    shaClone.digest(seed, 16, 20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2295
                    prfAlg = "SunTlsPrf";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2296
                    prf = P_NONE;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2297
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2298
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2299
                String prfHashAlg = prf.getPRFHashAlg();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2300
                int prfHashLength = prf.getPRFHashLength();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2301
                int prfBlockSize = prf.getPRFBlockSize();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2302
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2303
                /*
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2304
                 * RFC 5246/7.4.9 says that finished messages can
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2305
                 * be ciphersuite-specific in both length/PRF hash
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2306
                 * algorithm.  If we ever run across a different
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2307
                 * length, this call will need to be updated.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2308
                 */
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
  2309
                @SuppressWarnings("deprecation")
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2310
                TlsPrfParameterSpec spec = new TlsPrfParameterSpec(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2311
                    masterKey, tlsLabel, seed, 12,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2312
                    prfHashAlg, prfHashLength, prfBlockSize);
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
                KeyGenerator kg = JsseJce.getKeyGenerator(prfAlg);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2315
                kg.init(spec);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2316
                SecretKey prfKey = kg.generateKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                if ("RAW".equals(prfKey.getFormat()) == false) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2318
                    throw new ProviderException(
36952
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  2319
                        "Invalid PRF output, format must be RAW. " +
4500612ce068 8153531: Improve exception messaging for RSAClientKeyExchange
coffeys
parents: 36442
diff changeset
  2320
                        "Format received: " + prfKey.getFormat());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                byte[] finished = prfKey.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                return finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                throw new RuntimeException("PRF failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            // SSLv3
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2329
            MessageDigest md5Clone = handshakeHash.getMD5Clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2330
            MessageDigest shaClone = handshakeHash.getSHAClone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            updateDigest(md5Clone, sslLabel, MD5_pad1, MD5_pad2, masterKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            updateDigest(shaClone, sslLabel, SHA_pad1, SHA_pad2, masterKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            byte[] finished = new byte[36];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                md5Clone.digest(finished, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                shaClone.digest(finished, 16, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                // cannot occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                throw new RuntimeException("Digest failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
            return finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
     * Update the MessageDigest for SSLv3 finished message calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
     * The digest must already have been updated with all preceding handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
     * messages. This operation is almost identical to the certificate verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     * hash, reuse that code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
    private static void updateDigest(MessageDigest md, byte[] sender,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            byte[] pad1, byte[] pad2, SecretKey masterSecret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        md.update(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        CertificateVerify.updateDigest(md, pad1, pad2, masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2357
    // get the verify_data of the finished message
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2358
    byte[] getVerifyData() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2359
        return verifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2360
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2361
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2362
    @Override
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2363
    int messageType() { return ht_finished; }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2364
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2365
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        return verifyData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2370
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    void send(HandshakeOutStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        out.write(verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  2375
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        s.println("*** Finished");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            Debug.println(s, "verify_data", verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            s.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
// END of nested classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
}