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