jdk/src/share/classes/sun/security/ssl/HandshakeMessage.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 8991 7df5283fd3b8
child 10336 0bb1999251f8
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
     2
 * Copyright (c) 1996, 2011, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.internal.spec.TlsPrfParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.security.ssl.CipherSuite.*;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    49
import static sun.security.ssl.CipherSuite.PRF.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Many data structures are involved in the handshake messages.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * classes are used as structures, with public data members.  They are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * not visible outside the SSL package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Handshake messages all have a common header format, and they are all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * encoded in a "handshake data" SSL record substream.  The base class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * here (HandshakeMessage) provides a common framework and records the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * SSL record type of the particular handshake message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * This file contains subclasses for all the basic handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * All handshake messages know how to encode and decode themselves on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * SSL streams; this facilitates using the same code on SSL client and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * server sides, although they don't send and receive the same messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Messages also know how to print themselves, which is quite handy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * for debugging.  They always identify their type, and can optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * dump all of their content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    72
public abstract class HandshakeMessage {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    HandshakeMessage() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // enum HandshakeType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static final byte   ht_hello_request = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static final byte   ht_client_hello = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static final byte   ht_server_hello = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final byte   ht_certificate = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final byte   ht_server_key_exchange = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static final byte   ht_certificate_request = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static final byte   ht_server_hello_done = 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static final byte   ht_certificate_verify = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static final byte   ht_client_key_exchange = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static final byte   ht_finished = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* Class and subclass dynamic debugging support */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    91
    public static final Debug debug = Debug.getInstance("ssl");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Utility method to convert a BigInteger to a byte array in unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * format as needed in the handshake messages. BigInteger uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * 2's complement format, i.e. it prepends an extra zero if the MSB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * is set. We remove that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static byte[] toByteArray(BigInteger bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        byte[] b = bi.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if ((b.length > 1) && (b[0] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            int n = b.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            byte[] newarray = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.arraycopy(b, 1, newarray, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            b = newarray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * SSL 3.0 MAC padding constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Also used by CertificateVerify and Finished during the handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static final byte[] MD5_pad1 = genPad(0x36, 48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static final byte[] MD5_pad2 = genPad(0x5c, 48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static final byte[] SHA_pad1 = genPad(0x36, 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static final byte[] SHA_pad2 = genPad(0x5c, 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static byte[] genPad(int b, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        byte[] padding = new byte[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Arrays.fill(padding, (byte)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Write a handshake message on the (handshake) output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * This is just a four byte header followed by the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * NOTE that huge messages -- notably, ones with huge cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * chains -- are handled correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    final void write(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int len = messageLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (len > (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new SSLException("Handshake message too big"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                + ", type = " + messageType() + ", len = " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        s.write(messageType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        s.putInt24(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        send(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Subclasses implement these methods so those kinds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * messages can be emitted.  Base class delegates to subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    abstract int  messageType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    abstract int  messageLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    abstract void send(HandshakeOutStream s) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Write a descriptive message on the output stream; for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    abstract void print(PrintStream p) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
// NOTE:  the rest of these classes are nested within this one, and are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
// imported by other classes in this package.  There are a few other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
// handshake message classes, not neatly nested here because of current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
// licensing requirement for native (RSA) methods.  They belong here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
// but those native methods complicate things a lot!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * HelloRequest ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * Server can ask the client to initiate a new handshake, e.g. to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * session parameters after a connection has been (re)established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 */
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   172
static final class HelloRequest extends HandshakeMessage {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    int messageType() { return ht_hello_request; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    HelloRequest() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    HelloRequest(HandshakeInStream in) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // nothing in this message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    int messageLength() { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    void send(HandshakeOutStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // nothing in this messaage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    void print(PrintStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        out.println("*** HelloRequest (empty)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * ClientHello ... CLIENT --> SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * Client initiates handshake by telling server what it wants, and what it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * can support (prioritized by what's first in the ciphe suite list).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * By RFC2246:7.4.1.2 it's explicitly anticipated that this message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * will have more data added at the end ... e.g. what CAs the client trusts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * Until we know how to parse it, we will just read what we know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * about, and let our caller handle the jumps over unknown data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 */
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   208
static final class ClientHello extends HandshakeMessage {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    ProtocolVersion     protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    RandomCookie        clnt_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    SessionId           sessionId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private CipherSuiteList    cipherSuites;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    byte[]              compression_methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    HelloExtensions extensions = new HelloExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private final static byte[]  NULL_COMPRESSION = new byte[] {0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   220
    ClientHello(SecureRandom generator, ProtocolVersion protocolVersion,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   221
            SessionId sessionId, CipherSuiteList cipherSuites) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   223
        this.protocolVersion = protocolVersion;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   224
        this.sessionId = sessionId;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   225
        this.cipherSuites = cipherSuites;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (cipherSuites.containsEC()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            extensions.add(SupportedEllipticCurvesExtension.DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            extensions.add(SupportedEllipticPointFormatsExtension.DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   232
        clnt_random = new RandomCookie(generator);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   233
        compression_methods = NULL_COMPRESSION;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    ClientHello(HandshakeInStream s, int messageLength) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        protocolVersion = ProtocolVersion.valueOf(s.getInt8(), s.getInt8());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        clnt_random = new RandomCookie(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        sessionId = new SessionId(s.getBytes8());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        cipherSuites = new CipherSuiteList(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        compression_methods = s.getBytes8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (messageLength() != messageLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            extensions = new HelloExtensions(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   247
    CipherSuiteList getCipherSuites() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   248
        return cipherSuites;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   249
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   250
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   251
    // add renegotiation_info extension
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   252
    void addRenegotiationInfoExtension(byte[] clientVerifyData) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   253
        HelloExtension renegotiationInfo = new RenegotiationInfoExtension(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   254
                    clientVerifyData, new byte[0]);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   255
        extensions.add(renegotiationInfo);
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   256
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   257
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   258
    // add server_name extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   259
    void addServerNameIndicationExtension(String hostname) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   260
        // We would have checked that the hostname ia a FQDN.
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   261
        ArrayList<String> hostnames = new ArrayList<>(1);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   262
        hostnames.add(hostname);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   263
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   264
        try {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   265
            extensions.add(new ServerNameExtension(hostnames));
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   266
        } catch (IOException ioe) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   267
            // ignore the exception and return
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   268
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   269
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   270
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   271
    // add signature_algorithm extension
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   272
    void addSignatureAlgorithmsExtension(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   273
            Collection<SignatureAndHashAlgorithm> algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   274
        HelloExtension signatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   275
                new SignatureAlgorithmsExtension(algorithms);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   276
        extensions.add(signatureAlgorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   277
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   278
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   279
    @Override
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   280
    int messageType() { return ht_client_hello; }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   281
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   282
    @Override
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   283
    int messageLength() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   284
        /*
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   285
         * Add fixed size parts of each field...
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   286
         * version + random + session + cipher + compress
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   287
         */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   288
        return (2 + 32 + 1 + 2 + 1
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   289
            + sessionId.length()                /* ... + variable parts */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   290
            + (cipherSuites.size() * 2)
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   291
            + compression_methods.length)
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   292
            + extensions.length();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   293
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   294
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   295
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        s.putInt8(protocolVersion.major);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        s.putInt8(protocolVersion.minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        clnt_random.send(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        s.putBytes8(sessionId.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        cipherSuites.send(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        s.putBytes8(compression_methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        extensions.send(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   306
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        s.println("*** ClientHello, " + protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (debug != null && Debug.isOn("verbose")) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   311
            s.print("RandomCookie:  ");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   312
            clnt_random.print(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            s.print("Session ID:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            s.println(sessionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            s.println("Cipher Suites: " + cipherSuites);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            Debug.println(s, "Compression Methods", compression_methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            extensions.print(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            s.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 * ServerHello ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * Server chooses protocol options from among those it supports and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * client supports.  Then it sends the basic session descriptive parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * back to the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
class ServerHello extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    int messageType() { return ht_server_hello; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    ProtocolVersion     protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    RandomCookie        svr_random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    SessionId           sessionId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    CipherSuite         cipherSuite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    byte                compression_method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    HelloExtensions extensions = new HelloExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    ServerHello() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   349
    ServerHello(HandshakeInStream input, int messageLength)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   350
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        protocolVersion = ProtocolVersion.valueOf(input.getInt8(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                                  input.getInt8());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        svr_random = new RandomCookie(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        sessionId = new SessionId(input.getBytes8());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        cipherSuite = CipherSuite.valueOf(input.getInt8(), input.getInt8());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        compression_method = (byte)input.getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (messageLength() != messageLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            extensions = new HelloExtensions(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    int messageLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // almost fixed size, except session ID and extensions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        //      major + minor = 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        //      random = 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        //      session ID len field = 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        //      cipher suite + compression = 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        //      extensions: if present, 2 + length of extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return 38 + sessionId.length() + extensions.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    void send(HandshakeOutStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        s.putInt8(protocolVersion.major);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        s.putInt8(protocolVersion.minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        svr_random.send(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        s.putBytes8(sessionId.getId());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        s.putInt8(cipherSuite.id >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        s.putInt8(cipherSuite.id & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        s.putInt8(compression_method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        extensions.send(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    void print(PrintStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        s.println("*** ServerHello, " + protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (debug != null && Debug.isOn("verbose")) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   390
            s.print("RandomCookie:  ");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   391
            svr_random.print(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            s.print("Session ID:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            s.println(sessionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            s.println("Cipher Suite: " + cipherSuite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            s.println("Compression Method: " + compression_method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            extensions.print(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            s.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
 * CertificateMsg ... send by both CLIENT and SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
 * Each end of a connection may need to pass its certificate chain to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 * the other end.  Such chains are intended to validate an identity with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
 * reference to some certifying authority.  Examples include companies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
 * like Verisign, or financial institutions.  There's some control over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
 * the certifying authorities which are sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
 * NOTE: that these messages might be huge, taking many handshake records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
 * Up to 2^48 bytes of certificate may be sent, in records of at most 2^14
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
 * bytes each ... up to 2^32 records sent on the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
class CertificateMsg extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    int messageType() { return ht_certificate; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    private X509Certificate[] chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private List<byte[]> encodedChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    private int messageLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    CertificateMsg(X509Certificate[] certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        chain = certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    CertificateMsg(HandshakeInStream input) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        int chainLen = input.getInt24();
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   437
        List<Certificate> v = new ArrayList<>(4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        CertificateFactory cf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        while (chainLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            byte[] cert = input.getBytes24();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            chainLen -= (3 + cert.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if (cf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    cf = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                v.add(cf.generateCertificate(new ByteArrayInputStream(cert)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            } catch (CertificateException e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   449
                throw (SSLProtocolException)new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   450
                    e.getMessage()).initCause(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        chain = v.toArray(new X509Certificate[v.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (encodedChain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            messageLength = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            encodedChain = new ArrayList<byte[]>(chain.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                for (X509Certificate cert : chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    byte[] b = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    encodedChain.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    messageLength += b.length + 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            } catch (CertificateEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                encodedChain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                throw new RuntimeException("Could not encode certificates", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return messageLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        s.putInt24(messageLength() - 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        for (byte[] b : encodedChain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            s.putBytes24(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        s.println("*** Certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            for (int i = 0; i < chain.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                s.println("chain [" + i + "] = " + chain[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            s.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    X509Certificate[] getCertificateChain() {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   493
        return chain.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
 * ServerKeyExchange ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
 * The cipher suite selected, when combined with the certificate exchanged,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
 * implies one of several different kinds of key exchange.  Most current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
 * cipher suites require the server to send more than its certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
 * The primary exceptions are when a server sends an encryption-capable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
 * RSA public key in its cert, to be used with RSA (or RSA_export) key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
 * exchange; and when a server sends its Diffie-Hellman cert.  Those kinds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
 * of key exchange do not require a ServerKeyExchange message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
 * Key exchange can be viewed as having three modes, which are explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
 * for the Diffie-Hellman flavors and poorly specified for RSA ones:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
 *      - "Ephemeral" keys.  Here, a "temporary" key is allocated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
 *        server, and signed.  Diffie-Hellman keys signed using RSA or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
 *        DSS are ephemeral (DHE flavor).  RSA keys get used to do the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
 *        thing, to cut the key size down to 512 bits (export restrictions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
 *        or for signing-only RSA certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
 *      - Anonymity.  Here no server certificate is sent, only the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
 *        key of the server.  This case is subject to man-in-the-middle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
 *        attacks.  This can be done with Diffie-Hellman keys (DH_anon) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
 *        with RSA keys, but is only used in SSLv3 for DH_anon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
 *      - "Normal" case.  Here a server certificate is sent, and the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
 *        key there is used directly in exchanging the premaster secret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
 *        For example, Diffie-Hellman "DH" flavor, and any RSA flavor with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
 *        only 512 bit keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
 * If a server certificate is sent, there is no anonymity.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
 * when a certificate is sent, ephemeral keys may still be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
 * exchange the premaster secret.  That's how RSA_EXPORT often works,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
 * as well as how the DHE_* flavors work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
static abstract class ServerKeyExchange extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    int messageType() { return ht_server_key_exchange; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
 * Using RSA for Key Exchange:  exchange a session key that's not as big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
 * as the signing-only key.  Used for export applications, since exported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
 * RSA encryption keys can't be bigger than 512 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
 * This is never used when keys are 512 bits or smaller, and isn't used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
 * on "US Domestic" ciphers in any case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
class RSA_ServerKeyExchange extends ServerKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    private byte rsa_modulus[];     // 1 to 2^16 - 1 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    private byte rsa_exponent[];    // 1 to 2^16 - 1 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    private Signature signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    private byte[] signatureBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Hash the nonces and the ephemeral RSA public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    private void updateSignature(byte clntNonce[], byte svrNonce[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        int tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        signature.update(clntNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        signature.update(svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        tmp = rsa_modulus.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        signature.update((byte)(tmp >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        signature.update((byte)(tmp & 0x0ff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        signature.update(rsa_modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        tmp = rsa_exponent.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        signature.update((byte)(tmp >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        signature.update((byte)(tmp & 0x0ff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        signature.update(rsa_exponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Construct an RSA server key exchange message, using data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * known _only_ to the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * The client knows the public key corresponding to this private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * key, from the Certificate message sent previously.  To comply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * with US export regulations we use short RSA keys ... either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * long term ones in the server's X509 cert, or else ephemeral
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * ones sent using this message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    RSA_ServerKeyExchange(PublicKey ephemeralKey, PrivateKey privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            RandomCookie clntNonce, RandomCookie svrNonce, SecureRandom sr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        RSAPublicKeySpec rsaKey = JsseJce.getRSAPublicKeySpec(ephemeralKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        rsa_modulus = toByteArray(rsaKey.getModulus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        rsa_exponent = toByteArray(rsaKey.getPublicExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        signature = RSASignature.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        signature.initSign(privateKey, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        updateSignature(clntNonce.random_bytes, svrNonce.random_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        signatureBytes = signature.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Parse an RSA server key exchange message, using data known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * to the client (and, in some situations, eavesdroppers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    RSA_ServerKeyExchange(HandshakeInStream input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            throws IOException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        signature = RSASignature.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        rsa_modulus = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        rsa_exponent = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        signatureBytes = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Get the ephemeral RSA public key that will be used in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * SSL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    PublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            KeyFactory kfac = JsseJce.getKeyFactory("RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            // modulus and exponent are always positive
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   621
            RSAPublicKeySpec kspec = new RSAPublicKeySpec(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   622
                new BigInteger(1, rsa_modulus),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   623
                new BigInteger(1, rsa_exponent));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            return kfac.generatePublic(kspec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Verify the signed temporary key using the hashes computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * from it and the two nonces.  This is called by clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * with "exportable" RSA flavors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    boolean verify(PublicKey certifiedKey, RandomCookie clntNonce,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            RandomCookie svrNonce) throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        signature.initVerify(certifiedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        updateSignature(clntNonce.random_bytes, svrNonce.random_bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return signature.verify(signatureBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return 6 + rsa_modulus.length + rsa_exponent.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
               + signatureBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        s.putBytes16(rsa_modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        s.putBytes16(rsa_exponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        s.putBytes16(signatureBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        s.println("*** RSA ServerKeyExchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            Debug.println(s, "RSA Modulus", rsa_modulus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            Debug.println(s, "RSA Public Exponent", rsa_exponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
 * Using Diffie-Hellman algorithm for key exchange.  All we really need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
 * do is securely get Diffie-Hellman keys (using the same P, G parameters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
 * to our peer, then we automatically have a shared secret without need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
 * to exchange any more data.  (D-H only solutions, such as SKIP, could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
 * eliminate key exchange negotiations and get faster connection setup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
 * But they still need a signature algorithm like DSS/DSA to support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
 * trusted distribution of keys without relying on unscalable physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
 * key distribution systems.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
 * This class supports several DH-based key exchange algorithms, though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
 * perhaps eventually each deserves its own class.  Notably, this has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
 * basic support for DH_anon and its DHE_DSS and DHE_RSA signed variants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
class DH_ServerKeyExchange extends ServerKeyExchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    // Fix message encoding, see 4348279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    private final static boolean dhKeyExchangeFix =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        Debug.getBooleanProperty("com.sun.net.ssl.dhKeyExchangeFix", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    private byte                dh_p [];        // 1 to 2^16 - 1 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    private byte                dh_g [];        // 1 to 2^16 - 1 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    private byte                dh_Ys [];       // 1 to 2^16 - 1 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    private byte                signature [];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   691
    // protocol version being established using this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   692
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   693
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   694
    // the preferable signature algorithm used by this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   695
    private SignatureAndHashAlgorithm preferableSignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   696
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * Construct from initialized DH key object, for DH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   701
    DH_ServerKeyExchange(DHCrypt obj, ProtocolVersion protocolVersion) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   702
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   703
        this.preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   704
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   705
        setValues(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        signature = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Construct from initialized DH key object and the key associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * with the cert chain which was sent ... for DHE_DSS and DHE_RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * key exchange.  (Constructor called by server.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    DH_ServerKeyExchange(DHCrypt obj, PrivateKey key, byte clntNonce[],
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   715
            byte svrNonce[], SecureRandom sr,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   716
            SignatureAndHashAlgorithm signAlgorithm,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   717
            ProtocolVersion protocolVersion) throws GeneralSecurityException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   719
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   720
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   721
        setValues(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        Signature sig;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   724
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   725
            this.preferableSignatureAlgorithm = signAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   726
            sig = JsseJce.getSignature(signAlgorithm.getAlgorithmName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   728
            this.preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   729
            if (key.getAlgorithm().equals("DSA")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   730
                sig = JsseJce.getSignature(JsseJce.SIGNATURE_DSA);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   731
            } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   732
                sig = RSASignature.getInstance();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   733
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   735
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        sig.initSign(key, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        signature = sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * Construct a DH_ServerKeyExchange message from an input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * stream, as if sent from server to client for use with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * DH_anon key exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   746
    DH_ServerKeyExchange(HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   747
            ProtocolVersion protocolVersion) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   748
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   749
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   750
        this.preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   751
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        dh_p = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        dh_g = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        dh_Ys = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        signature = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * Construct a DH_ServerKeyExchange message from an input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * and a certificate, as if sent from server to client for use with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * DHE_DSS or DHE_RSA key exchange.  (Called by client.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    DH_ServerKeyExchange(HandshakeInStream input, PublicKey publicKey,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   764
            byte clntNonce[], byte svrNonce[], int messageSize,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   765
            Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   766
            ProtocolVersion protocolVersion)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            throws IOException, GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   769
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   770
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   771
        // read params: ServerDHParams
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        dh_p = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        dh_g = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        dh_Ys = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   776
        // read the signature and hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   777
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   778
            int hash = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   779
            int signature = input.getInt8();    // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   780
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   781
            preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   782
                SignatureAndHashAlgorithm.valueOf(hash, signature, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   783
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   784
            // Is it a local supported signature algorithm?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   785
            if (!localSupportedSignAlgs.contains(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   786
                    preferableSignatureAlgorithm)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   787
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   788
                        "Unsupported SignatureAndHashAlgorithm in " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   789
                        "ServerKeyExchange message");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   790
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   791
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   792
            this.preferableSignatureAlgorithm = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   793
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   794
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   795
        // read the signature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        byte signature[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (dhKeyExchangeFix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            signature = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            messageSize -= (dh_p.length + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            messageSize -= (dh_g.length + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            messageSize -= (dh_Ys.length + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            signature = new byte[messageSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            input.read(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        Signature sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        String algorithm = publicKey.getAlgorithm();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   810
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   811
            sig = JsseJce.getSignature(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   812
                        preferableSignatureAlgorithm.getAlgorithmName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   814
            if (algorithm.equals("DSA")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   815
                sig = JsseJce.getSignature(JsseJce.SIGNATURE_DSA);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   816
            } else if (algorithm.equals("RSA")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   817
                sig = RSASignature.getInstance();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   818
            } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   819
                throw new SSLKeyException("neither an RSA or a DSA key");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   820
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        sig.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (sig.verify(signature) == false ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            throw new SSLKeyException("Server D-H key verification failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   831
    /* 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
   832
    BigInteger getModulus() {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   833
        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
   834
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   835
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   836
    /* 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
   837
    BigInteger getBase() {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   838
        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
   839
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   840
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   841
    /* 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
   842
    BigInteger getServerPublicKey() {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   843
        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
   844
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   845
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   846
    /*
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   847
     * 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
   848
     */
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   849
    private void updateSignature(Signature sig, byte clntNonce[],
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   850
            byte svrNonce[]) throws SignatureException {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   851
        int tmp;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   852
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   853
        sig.update(clntNonce);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   854
        sig.update(svrNonce);
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   855
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   856
        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
   857
        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
   858
        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
   859
        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
   860
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   861
        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
   862
        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
   863
        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
   864
        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
   865
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   866
        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
   867
        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
   868
        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
   869
        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
   870
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   871
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   872
    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
   873
        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
   874
        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
   875
        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
   876
    }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   877
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        int temp = 6;   // overhead for p, g, y(s) values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        temp += dh_p.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        temp += dh_g.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        temp += dh_Ys.length;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   884
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        if (signature != null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   886
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   887
                temp += SignatureAndHashAlgorithm.sizeInRecord();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   888
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   889
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            temp += signature.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if (dhKeyExchangeFix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                temp += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   895
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        return temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        s.putBytes16(dh_p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        s.putBytes16(dh_g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        s.putBytes16(dh_Ys);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   903
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        if (signature != null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   905
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   906
                s.putInt8(preferableSignatureAlgorithm.getHashValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   907
                s.putInt8(preferableSignatureAlgorithm.getSignatureValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   908
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   909
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            if (dhKeyExchangeFix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                s.putBytes16(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                s.write(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        s.println("*** Diffie-Hellman ServerKeyExchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            Debug.println(s, "DH Modulus", dh_p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            Debug.println(s, "DH Base", dh_g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            Debug.println(s, "Server DH Public Key", dh_Ys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            if (signature == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                s.println("Anonymous");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   929
                if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   930
                    s.println("Signature Algorithm " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   931
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   932
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   933
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                s.println("Signed with a DSA or RSA public key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
 * ECDH server key exchange message. Sent by the server for ECDHE and ECDH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
 * ciphersuites to communicate its ephemeral public key (including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
 * EC domain parameters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
 * We support named curves only, no explicitly encoded curves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
static final
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
   948
class ECDH_ServerKeyExchange extends ServerKeyExchange {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    // constants for ECCurveType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    private final static int CURVE_EXPLICIT_PRIME = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    private final static int CURVE_EXPLICIT_CHAR2 = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    private final static int CURVE_NAMED_CURVE    = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    // id of the curve we are using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    private int curveId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    // encoded public point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    private byte[] pointBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    // signature bytes (or null if anonymous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    private byte[] signatureBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    // public key object encapsulated in this message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    private ECPublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   966
    // protocol version being established using this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   967
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   968
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   969
    // the preferable signature algorithm used by this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   970
    private SignatureAndHashAlgorithm preferableSignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   971
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    ECDH_ServerKeyExchange(ECDHCrypt obj, PrivateKey privateKey,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   973
            byte[] clntNonce, byte[] svrNonce, SecureRandom sr,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   974
            SignatureAndHashAlgorithm signAlgorithm,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   975
            ProtocolVersion protocolVersion) throws GeneralSecurityException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   976
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   977
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   978
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        publicKey = (ECPublicKey)obj.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        ECParameterSpec params = publicKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        ECPoint point = publicKey.getW();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        pointBytes = JsseJce.encodePoint(point, params.getCurve());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        curveId = SupportedEllipticCurvesExtension.getCurveIndex(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        if (privateKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            // ECDH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   990
        Signature sig;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   991
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   992
            this.preferableSignatureAlgorithm = signAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   993
            sig = JsseJce.getSignature(signAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   994
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   995
            sig = getSignature(privateKey.getAlgorithm());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   996
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   997
        sig.initSign(privateKey);  // where is the SecureRandom?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        signatureBytes = sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * Parse an ECDH server key exchange message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    ECDH_ServerKeyExchange(HandshakeInStream input, PublicKey signingKey,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1007
            byte[] clntNonce, byte[] svrNonce,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1008
            Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1009
            ProtocolVersion protocolVersion)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            throws IOException, GeneralSecurityException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1011
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1012
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1013
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1014
        // read params: ServerECDHParams
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        int curveType = input.getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        ECParameterSpec parameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        // These parsing errors should never occur as we negotiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        // the supported curves during the exchange of the Hello messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if (curveType == CURVE_NAMED_CURVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            curveId = input.getInt16();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1021
            if (SupportedEllipticCurvesExtension.isSupported(curveId)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1022
                    == false) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1023
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1024
                    "Unsupported curveId: " + curveId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1026
            String curveOid =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1027
                SupportedEllipticCurvesExtension.getCurveOid(curveId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            if (curveOid == null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1029
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1030
                    "Unknown named curve: " + curveId);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            parameters = JsseJce.getECParameterSpec(curveOid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            if (parameters == null) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1034
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1035
                    "Unsupported curve: " + curveOid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        } else {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1038
            throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1039
                "Unsupported ECCurveType: " + curveType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        pointBytes = input.getBytes8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        ECPoint point = JsseJce.decodePoint(pointBytes, parameters.getCurve());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        KeyFactory factory = JsseJce.getKeyFactory("EC");
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1045
        publicKey = (ECPublicKey)factory.generatePublic(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1046
            new ECPublicKeySpec(point, parameters));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        if (signingKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            // ECDH_anon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1053
        // read the signature and hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1054
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1055
            int hash = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1056
            int signature = input.getInt8();    // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1057
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1058
            preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1059
                SignatureAndHashAlgorithm.valueOf(hash, signature, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1060
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1061
            // Is it a local supported signature algorithm?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1062
            if (!localSupportedSignAlgs.contains(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1063
                    preferableSignatureAlgorithm)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1064
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1065
                        "Unsupported SignatureAndHashAlgorithm in " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1066
                        "ServerKeyExchange message");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1067
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1068
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1069
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1070
        // read the signature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        signatureBytes = input.getBytes16();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1072
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1073
        // verify the signature
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1074
        Signature sig;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1075
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1076
            sig = JsseJce.getSignature(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1077
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1078
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1079
            sig = getSignature(signingKey.getAlgorithm());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1080
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        sig.initVerify(signingKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        updateSignature(sig, clntNonce, svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        if (sig.verify(signatureBytes) == false ) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1086
            throw new SSLKeyException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1087
                "Invalid signature on ECDH server key exchange message");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * Get the ephemeral EC public key encapsulated in this message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    ECPublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1098
    private static Signature getSignature(String keyAlgorithm)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1099
            throws NoSuchAlgorithmException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        if (keyAlgorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            return JsseJce.getSignature(JsseJce.SIGNATURE_ECDSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        } else if (keyAlgorithm.equals("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            return RSASignature.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            throw new NoSuchAlgorithmException("neither an RSA or a EC key");
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
    private void updateSignature(Signature sig, byte clntNonce[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            byte svrNonce[]) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        sig.update(clntNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        sig.update(svrNonce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        sig.update((byte)CURVE_NAMED_CURVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        sig.update((byte)(curveId >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        sig.update((byte)curveId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        sig.update((byte)pointBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        sig.update(pointBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    int messageLength() {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1122
        int sigLen = 0;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1123
        if (signatureBytes != null) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1124
            sigLen = 2 + signatureBytes.length;
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1125
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1126
                sigLen += SignatureAndHashAlgorithm.sizeInRecord();
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1127
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1128
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1129
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return 4 + pointBytes.length + sigLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    void send(HandshakeOutStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        s.putInt8(CURVE_NAMED_CURVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        s.putInt16(curveId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        s.putBytes8(pointBytes);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1137
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        if (signatureBytes != null) {
8991
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1139
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1140
                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
  1141
                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
  1142
            }
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1143
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            s.putBytes16(signatureBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        s.println("*** ECDH ServerKeyExchange");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        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
  1152
            if (signatureBytes == null) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1153
                s.println("Anonymous");
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1154
            } else {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1155
                if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1156
                    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
  1157
                            preferableSignatureAlgorithm.getAlgorithmName());
7df5283fd3b8 7027797: take care of ECDH_anon/DH_anon server key exchange for TLS 1.2
xuelei
parents: 7990
diff changeset
  1158
                }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1159
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1160
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            s.println("Server key: " + publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
static final class DistinguishedName {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * DER encoded distinguished name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * TLS requires that its not longer than 65535 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    byte name[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    DistinguishedName(HandshakeInStream input) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        name = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    DistinguishedName(X500Principal dn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        name = dn.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    X500Principal getX500Principal() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            return new X500Principal(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        } catch (IllegalArgumentException e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1186
            throw (SSLProtocolException)new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1187
                e.getMessage()).initCause(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        return 2 + name.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    void send(HandshakeOutStream output) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        output.putBytes16(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    void print(PrintStream output) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        X500Principal principal = new X500Principal(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        output.println("<" + principal.toString() + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
 * CertificateRequest ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
 * Authenticated servers may ask clients to authenticate themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
 * in turn, using this message.
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1210
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1211
 * 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
  1212
 *     struct {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1213
 *         ClientCertificateType certificate_types<1..2^8-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1214
 *         DistinguishedName certificate_authorities<0..2^16-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1215
 *     } CertificateRequest;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1216
 *
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1217
 * In TLS 1.2, the structure is changed to:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1218
 *     struct {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1219
 *         ClientCertificateType certificate_types<1..2^8-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1220
 *         SignatureAndHashAlgorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1221
 *           supported_signature_algorithms<2^16-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1222
 *         DistinguishedName certificate_authorities<0..2^16-1>;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1223
 *     } CertificateRequest;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1224
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
class CertificateRequest extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    // enum ClientCertificateType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    static final int   cct_rsa_sign = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    static final int   cct_dss_sign = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    static final int   cct_rsa_fixed_dh = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    static final int   cct_dss_fixed_dh = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    // The existance of these two values is a bug in the SSL specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    // They are never used in the protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    static final int   cct_rsa_ephemeral_dh = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    static final int   cct_dss_ephemeral_dh = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    // From RFC 4492 (ECC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    static final int    cct_ecdsa_sign       = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    static final int    cct_rsa_fixed_ecdh   = 65;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    static final int    cct_ecdsa_fixed_ecdh = 66;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    private final static byte[] TYPES_NO_ECC = { cct_rsa_sign, cct_dss_sign };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    private final static byte[] TYPES_ECC =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        { cct_rsa_sign, cct_dss_sign, cct_ecdsa_sign };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    byte                types [];               // 1 to 255 types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    DistinguishedName   authorities [];         // 3 to 2^16 - 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        // ... "3" because that's the smallest DER-encoded X500 DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1253
    // protocol version being established using this CertificateRequest message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1254
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1255
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1256
    // supported_signature_algorithms for TLS 1.2 or later
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1257
    private Collection<SignatureAndHashAlgorithm> algorithms;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1258
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1259
    // length of supported_signature_algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1260
    private int algorithmsLen;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1261
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1262
    CertificateRequest(X509Certificate ca[], KeyExchange keyExchange,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1263
            Collection<SignatureAndHashAlgorithm> signAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1264
            ProtocolVersion protocolVersion) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1265
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1266
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1267
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        // always use X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        authorities = new DistinguishedName[ca.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        for (int i = 0; i < ca.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            X500Principal x500Principal = ca[i].getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            authorities[i] = new DistinguishedName(x500Principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        // we support RSA, DSS, and ECDSA client authentication and they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        // can be used with all ciphersuites. If this changes, the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        // needs to be adapted to take keyExchange into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        // We only request ECDSA client auth if we have ECC crypto available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        this.types = JsseJce.isEcAvailable() ? TYPES_ECC : TYPES_NO_ECC;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1279
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1280
        // Use supported_signature_algorithms for TLS 1.2 or later.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1281
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1282
            if (signAlgs == null || signAlgs.isEmpty()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1283
                throw new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1284
                        "No supported signature algorithms");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1285
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1286
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1287
            algorithms = new ArrayList<SignatureAndHashAlgorithm>(signAlgs);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1288
            algorithmsLen =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1289
                SignatureAndHashAlgorithm.sizeInRecord() * algorithms.size();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1290
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1291
            algorithms = new ArrayList<SignatureAndHashAlgorithm>();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1292
            algorithmsLen = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1293
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1296
    CertificateRequest(HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1297
            ProtocolVersion protocolVersion) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1298
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1299
        this.protocolVersion = protocolVersion;
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
        // Read the certificate_types.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        types = input.getBytes8();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1303
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1304
        // Read the supported_signature_algorithms for TLS 1.2 or later.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1305
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1306
            algorithmsLen = input.getInt16();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1307
            if (algorithmsLen < 2) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1308
                throw new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1309
                        "Invalid supported_signature_algorithms field");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1310
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1311
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1312
            algorithms = new ArrayList<SignatureAndHashAlgorithm>();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1313
            int remains = algorithmsLen;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1314
            int sequence = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1315
            while (remains > 1) {    // needs at least two bytes
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1316
                int hash = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1317
                int signature = input.getInt8();    // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1318
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1319
                SignatureAndHashAlgorithm algorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1320
                    SignatureAndHashAlgorithm.valueOf(hash, signature,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1321
                                                                ++sequence);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1322
                algorithms.add(algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1323
                remains -= 2;  // one byte for hash, one byte for signature
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1324
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1325
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1326
            if (remains != 0) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1327
                throw new SSLProtocolException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1328
                        "Invalid supported_signature_algorithms field");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1329
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1330
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1331
            algorithms = new ArrayList<SignatureAndHashAlgorithm>();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1332
            algorithmsLen = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1333
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1334
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1335
        // read the certificate_authorities
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        int len = input.getInt16();
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
  1337
        ArrayList<DistinguishedName> v = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        while (len >= 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            DistinguishedName dn = new DistinguishedName(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            v.add(dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            len -= dn.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            throw new SSLProtocolException("Bad CertificateRequest DN length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        authorities = v.toArray(new DistinguishedName[v.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    X500Principal[] getAuthorities() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        X500Principal[] ret = new X500Principal[authorities.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        for (int i = 0; i < authorities.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            ret[i] = authorities[i].getX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1359
    Collection<SignatureAndHashAlgorithm> getSignAlgorithms() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1360
        return algorithms;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1361
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1362
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1363
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1364
    int messageType() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1365
        return ht_certificate_request;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1366
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1368
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1369
    int messageLength() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1370
        int len = 1 + types.length + 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1371
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1372
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1373
            len += algorithmsLen + 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1374
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1375
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1376
        for (int i = 0; i < authorities.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            len += authorities[i].length();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1378
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1379
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1383
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1384
    void send(HandshakeOutStream output) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1385
        // put certificate_types
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1386
        output.putBytes8(types);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1388
        // put supported_signature_algorithms
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1389
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1390
            output.putInt16(algorithmsLen);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1391
            for (SignatureAndHashAlgorithm algorithm : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1392
                output.putInt8(algorithm.getHashValue());      // hash
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1393
                output.putInt8(algorithm.getSignatureValue()); // signature
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1394
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1395
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1396
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1397
        // put certificate_authorities
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1398
        int len = 0;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1399
        for (int i = 0; i < authorities.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            len += authorities[i].length();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1401
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        output.putInt16(len);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1404
        for (int i = 0; i < authorities.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            authorities[i].send(output);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1406
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1409
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1410
    void print(PrintStream s) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        s.println("*** CertificateRequest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            s.print("Cert Types: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            for (int i = 0; i < types.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                switch (types[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                  case cct_rsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                    s.print("RSA"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                  case cct_dss_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                    s.print("DSS"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                  case cct_rsa_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                    s.print("Fixed DH (RSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                  case cct_dss_fixed_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                    s.print("Fixed DH (DSS sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                  case cct_rsa_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                    s.print("Ephemeral DH (RSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                  case cct_dss_ephemeral_dh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                    s.print("Ephemeral DH (DSS sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                  case cct_ecdsa_sign:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                    s.print("ECDSA"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                  case cct_rsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                    s.print("Fixed ECDH (RSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                  case cct_ecdsa_fixed_ecdh:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                    s.print("Fixed ECDH (ECDSA sig)"); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                  default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                    s.print("Type-" + (types[i] & 0xff)); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                if (i != types.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                    s.print(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            s.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1444
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1445
                StringBuffer buffer = new StringBuffer();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1446
                boolean opened = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1447
                for (SignatureAndHashAlgorithm signAlg : algorithms) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1448
                    if (opened) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1449
                        buffer.append(", " + signAlg.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1450
                    } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1451
                        buffer.append(signAlg.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1452
                        opened = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1453
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1454
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1455
                s.println("Supported Signature Algorithms: " + buffer);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1456
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1457
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            s.println("Cert Authorities:");
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1459
            if (authorities.length == 0) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1460
                s.println("<Empty>");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1461
            } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1462
                for (int i = 0; i < authorities.length; i++) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1463
                    authorities[i].print(s);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1464
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1465
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
 * ServerHelloDone ... SERVER --> CLIENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
 * When server's done sending its messages in response to the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
 * "hello" (e.g. its own hello, certificate, key exchange message, perhaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
 * client certificate request) it sends this message to flag that it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
 * done that part of the handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
static final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
class ServerHelloDone extends HandshakeMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    int messageType() { return ht_server_hello_done; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    ServerHelloDone() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    ServerHelloDone(HandshakeInStream input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        // nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    int messageLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    void send(HandshakeOutStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        // nothing to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    void print(PrintStream s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        s.println("*** ServerHelloDone");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
 * CertificateVerify ... CLIENT --> SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
 * Sent after client sends signature-capable certificates (e.g. not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
 * Diffie-Hellman) to verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
static final class CertificateVerify extends HandshakeMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1516
    // the signature bytes
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1517
    private byte[] signature;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1519
    // protocol version being established using this ServerKeyExchange message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1520
    ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1521
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1522
    // the preferable signature algorithm used by this CertificateVerify message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1523
    private SignatureAndHashAlgorithm preferableSignatureAlgorithm = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * Create an RSA or DSA signed certificate verify message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1528
    CertificateVerify(ProtocolVersion protocolVersion,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1529
            HandshakeHash handshakeHash, PrivateKey privateKey,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1530
            SecretKey masterSecret, SecureRandom sr,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1531
            SignatureAndHashAlgorithm signAlgorithm)
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1532
            throws GeneralSecurityException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1533
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1534
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1535
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        String algorithm = privateKey.getAlgorithm();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1537
        Signature sig = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1538
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1539
            this.preferableSignatureAlgorithm = signAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1540
            sig = JsseJce.getSignature(signAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1541
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1542
            sig = getSignature(protocolVersion, algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1543
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        sig.initSign(privateKey, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        updateSignature(sig, protocolVersion, handshakeHash, algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                        masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        signature = sig.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    // Unmarshal the signed data from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    //
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1553
    CertificateVerify(HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1554
            Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1555
            ProtocolVersion protocolVersion) throws IOException  {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1556
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1557
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1558
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1559
        // read the signature and hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1560
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1561
            int hashAlg = input.getInt8();         // hash algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1562
            int signAlg = input.getInt8();         // signature algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1563
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1564
            preferableSignatureAlgorithm =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1565
                SignatureAndHashAlgorithm.valueOf(hashAlg, signAlg, 0);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1566
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1567
            // Is it a local supported signature algorithm?
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1568
            if (!localSupportedSignAlgs.contains(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1569
                    preferableSignatureAlgorithm)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1570
                throw new SSLHandshakeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1571
                        "Unsupported SignatureAndHashAlgorithm in " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1572
                        "ServerKeyExchange message");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1573
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1574
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1575
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1576
        // read the signature
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        signature = input.getBytes16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    /*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1581
     * Get the preferable signature algorithm used by this message
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1582
     */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1583
    SignatureAndHashAlgorithm getPreferableSignatureAlgorithm() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1584
        return preferableSignatureAlgorithm;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1585
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1586
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1587
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * Verify a certificate verify message. Return the result of verification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * if there is a problem throw a GeneralSecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    boolean verify(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            HandshakeHash handshakeHash, PublicKey publicKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            SecretKey masterSecret) throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        String algorithm = publicKey.getAlgorithm();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1595
        Signature sig = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1596
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1597
            sig = JsseJce.getSignature(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1598
                        preferableSignatureAlgorithm.getAlgorithmName());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1599
        } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1600
            sig = getSignature(protocolVersion, algorithm);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1601
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        sig.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        updateSignature(sig, protocolVersion, handshakeHash, algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                        masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        return sig.verify(signature);
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
     * Get the Signature object appropriate for verification using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * given signature algorithm and protocol version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    private static Signature getSignature(ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            String algorithm) throws GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        if (algorithm.equals("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            return RSASignature.getInternalInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        } else if (algorithm.equals("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            return JsseJce.getSignature(JsseJce.SIGNATURE_RAWDSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        } else if (algorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            return JsseJce.getSignature(JsseJce.SIGNATURE_RAWECDSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            throw new SignatureException("Unrecognized algorithm: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * Update the Signature with the data appropriate for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * signature algorithm and protocol version so that the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * ready for signing or verifying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    private static void updateSignature(Signature sig,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            ProtocolVersion protocolVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            HandshakeHash handshakeHash, String algorithm, SecretKey masterKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            throws SignatureException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1635
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        if (algorithm.equals("RSA")) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1637
            if (protocolVersion.v < ProtocolVersion.TLS12.v) { // TLS1.1-
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1638
                MessageDigest md5Clone = handshakeHash.getMD5Clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1639
                MessageDigest shaClone = handshakeHash.getSHAClone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1640
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1641
                if (protocolVersion.v < ProtocolVersion.TLS10.v) { // SSLv3
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1642
                    updateDigest(md5Clone, MD5_pad1, MD5_pad2, masterKey);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1643
                    updateDigest(shaClone, SHA_pad1, SHA_pad2, masterKey);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1644
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1645
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1646
                // The signature must be an instance of RSASignature, need
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1647
                // to use these hashes directly.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1648
                RSASignature.setHashes(sig, md5Clone, shaClone);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1649
            } else {  // TLS1.2+
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1650
                sig.update(handshakeHash.getAllHandshakeMessages());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        } else { // DSA, ECDSA
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1653
            if (protocolVersion.v < ProtocolVersion.TLS12.v) { // TLS1.1-
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1654
                MessageDigest shaClone = handshakeHash.getSHAClone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1655
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1656
                if (protocolVersion.v < ProtocolVersion.TLS10.v) { // SSLv3
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1657
                    updateDigest(shaClone, SHA_pad1, SHA_pad2, masterKey);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1658
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1659
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1660
                sig.update(shaClone.digest());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1661
            } else {  // TLS1.2+
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1662
                sig.update(handshakeHash.getAllHandshakeMessages());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * Update the MessageDigest for SSLv3 certificate verify or finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * message calculation. The digest must already have been updated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * all preceding handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * Used by the Finished class as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1673
    private static void updateDigest(MessageDigest md,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1674
            byte[] pad1, byte[] pad2,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            SecretKey masterSecret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        // Digest the key bytes if available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        // Otherwise (sensitive key), try digesting the key directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        // That is currently only implemented in SunPKCS11 using a private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        // reflection API, so we avoid that if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        byte[] keyBytes = "RAW".equals(masterSecret.getFormat())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                        ? masterSecret.getEncoded() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        if (keyBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            md.update(keyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            digestKey(md, masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        md.update(pad1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        byte[] temp = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        if (keyBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            md.update(keyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            digestKey(md, masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        md.update(pad2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        md.update(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    private final static Class delegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    private final static Field spiField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            delegate = Class.forName("java.security.MessageDigest$Delegate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            spiField = delegate.getDeclaredField("digestSpi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            throw new RuntimeException("Reflection failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        makeAccessible(spiField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    private static void makeAccessible(final AccessibleObject o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                o.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    // ConcurrentHashMap does not allow null values, use this marker object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    private final static Object NULL_OBJECT = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    // cache Method objects per Spi class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
    // Note that this will prevent the Spi classes from being GC'd. We assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    // that is not a problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    private final static Map<Class,Object> methodCache =
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
  1728
                                        new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    private static void digestKey(MessageDigest md, SecretKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            // Verify that md is implemented via MessageDigestSpi, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            // via JDK 1.1 style MessageDigest subclassing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            if (md.getClass() != delegate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                throw new Exception("Digest is not a MessageDigestSpi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            MessageDigestSpi spi = (MessageDigestSpi)spiField.get(md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            Class<?> clazz = spi.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            Object r = methodCache.get(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                    r = clazz.getDeclaredMethod("implUpdate", SecretKey.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                    makeAccessible((Method)r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                    r = NULL_OBJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                methodCache.put(clazz, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            if (r == NULL_OBJECT) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1750
                throw new Exception(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1751
                    "Digest does not support implUpdate(SecretKey)");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            Method update = (Method)r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            update.invoke(spi, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        } catch (Exception e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1756
            throw new RuntimeException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1757
                "Could not obtain encoded key and "
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1758
                + "MessageDigest cannot digest key", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1762
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1763
    int messageType() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1764
        return ht_certificate_verify;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1767
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1768
    int messageLength() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1769
        int temp = 2;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1770
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1771
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1772
            temp += SignatureAndHashAlgorithm.sizeInRecord();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1773
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1774
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1775
        return temp + signature.length;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1776
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1777
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1778
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    void send(HandshakeOutStream s) throws IOException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1780
        if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1781
            s.putInt8(preferableSignatureAlgorithm.getHashValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1782
            s.putInt8(preferableSignatureAlgorithm.getSignatureValue());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1783
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1784
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        s.putBytes16(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1788
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        s.println("*** CertificateVerify");
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1791
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1792
        if (debug != null && Debug.isOn("verbose")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1793
            if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1794
                s.println("Signature Algorithm " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1795
                        preferableSignatureAlgorithm.getAlgorithmName());
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
 * FINISHED ... sent by both CLIENT and SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
 * This is the FINISHED message as defined in the SSL and TLS protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
 * Both protocols define this handshake message slightly differently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
 * This class supports both formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
 * When handshaking is finished, each side sends a "change_cipher_spec"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
 * record, then immediately sends a "finished" handshake message prepared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
 * according to the newly adopted cipher spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
 * NOTE that until this is sent, no application data may be passed, unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
 * some non-default cipher suite has already been set up on this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
 * connection (e.g. a previous handshake arranged one).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
static final class Finished extends HandshakeMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    // constant for a Finished message sent by the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    final static int CLIENT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
    // constant for a Finished message sent by the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    final static int SERVER = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    // enum Sender:  "CLNT" and "SRVR"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    private static final byte[] SSL_CLIENT = { 0x43, 0x4C, 0x4E, 0x54 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    private static final byte[] SSL_SERVER = { 0x53, 0x52, 0x56, 0x52 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * Contents of the finished message ("checksum"). For TLS, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * is 12 bytes long, for SSLv3 36 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
    private byte[] verifyData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
    /*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1836
     * Current cipher suite we are negotiating.  TLS 1.2 has
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1837
     * ciphersuite-defined PRF algorithms.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1838
     */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1839
    private ProtocolVersion protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1840
    private CipherSuite cipherSuite;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1841
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1842
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * Create a finished message to send to the remote peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    Finished(ProtocolVersion protocolVersion, HandshakeHash handshakeHash,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1846
            int sender, SecretKey master, CipherSuite cipherSuite) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1847
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1848
        this.cipherSuite = cipherSuite;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1849
        verifyData = getFinished(handshakeHash, sender, master);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * Constructor that reads FINISHED message from stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1855
    Finished(ProtocolVersion protocolVersion, HandshakeInStream input,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1856
            CipherSuite cipherSuite) throws IOException {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1857
        this.protocolVersion = protocolVersion;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1858
        this.cipherSuite = cipherSuite;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        int msgLen = (protocolVersion.v >= ProtocolVersion.TLS10.v) ? 12 : 36;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        verifyData = new byte[msgLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        input.read(verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * Verify that the hashes here are what would have been produced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * according to a given set of inputs.  This is used to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * both client and server are fully in sync, and that the handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * computations have been successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1870
    boolean verify(HandshakeHash handshakeHash, int sender, SecretKey master) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1871
        byte[] myFinished = getFinished(handshakeHash, sender, master);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        return Arrays.equals(myFinished, verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * Perform the actual finished message calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1878
    private byte[] getFinished(HandshakeHash handshakeHash,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1879
            int sender, SecretKey masterKey) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        byte[] sslLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        String tlsLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        if (sender == CLIENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            sslLabel = SSL_CLIENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            tlsLabel = "client finished";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        } else if (sender == SERVER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            sslLabel = SSL_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            tlsLabel = "server finished";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            throw new RuntimeException("Invalid sender: " + sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1891
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        if (protocolVersion.v >= ProtocolVersion.TLS10.v) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1893
            // TLS 1.0+
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1895
                byte [] seed;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1896
                String prfAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1897
                PRF prf;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1898
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1899
                // Get the KeyGenerator alg and calculate the seed.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1900
                if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1901
                    // TLS 1.2
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1902
                    seed = handshakeHash.getFinishedHash();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1903
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1904
                    prfAlg = "SunTls12Prf";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1905
                    prf = cipherSuite.prfAlg;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1906
                } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1907
                    // TLS 1.0/1.1
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1908
                    MessageDigest md5Clone = handshakeHash.getMD5Clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1909
                    MessageDigest shaClone = handshakeHash.getSHAClone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1910
                    seed = new byte[36];
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1911
                    md5Clone.digest(seed, 0, 16);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1912
                    shaClone.digest(seed, 16, 20);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1914
                    prfAlg = "SunTlsPrf";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1915
                    prf = P_NONE;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1916
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1917
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1918
                String prfHashAlg = prf.getPRFHashAlg();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1919
                int prfHashLength = prf.getPRFHashLength();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1920
                int prfBlockSize = prf.getPRFBlockSize();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1921
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1922
                /*
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1923
                 * RFC 5246/7.4.9 says that finished messages can
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1924
                 * be ciphersuite-specific in both length/PRF hash
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1925
                 * algorithm.  If we ever run across a different
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1926
                 * length, this call will need to be updated.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1927
                 */
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1928
                TlsPrfParameterSpec spec = new TlsPrfParameterSpec(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1929
                    masterKey, tlsLabel, seed, 12,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1930
                    prfHashAlg, prfHashLength, prfBlockSize);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1931
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1932
                KeyGenerator kg = JsseJce.getKeyGenerator(prfAlg);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1933
                kg.init(spec);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1934
                SecretKey prfKey = kg.generateKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                if ("RAW".equals(prfKey.getFormat()) == false) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1936
                    throw new ProviderException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1937
                        "Invalid PRF output, format must be RAW");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                byte[] finished = prfKey.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                return finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                throw new RuntimeException("PRF failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            // SSLv3
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1946
            MessageDigest md5Clone = handshakeHash.getMD5Clone();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1947
            MessageDigest shaClone = handshakeHash.getSHAClone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            updateDigest(md5Clone, sslLabel, MD5_pad1, MD5_pad2, masterKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            updateDigest(shaClone, sslLabel, SHA_pad1, SHA_pad2, masterKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            byte[] finished = new byte[36];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                md5Clone.digest(finished, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                shaClone.digest(finished, 16, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                // cannot occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                throw new RuntimeException("Digest failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            return finished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * Update the MessageDigest for SSLv3 finished message calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * The digest must already have been updated with all preceding handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * messages. This operation is almost identical to the certificate verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * hash, reuse that code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
    private static void updateDigest(MessageDigest md, byte[] sender,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            byte[] pad1, byte[] pad2, SecretKey masterSecret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        md.update(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        CertificateVerify.updateDigest(md, pad1, pad2, masterSecret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1974
    // get the verify_data of the finished message
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1975
    byte[] getVerifyData() {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1976
        return verifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1977
    }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1978
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1979
    @Override
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1980
    int messageType() { return ht_finished; }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1981
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1982
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        return verifyData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1987
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    void send(HandshakeOutStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        out.write(verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1992
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        s.println("*** Finished");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            Debug.println(s, "verify_data", verifyData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            s.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
// END of nested classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
}