jdk/src/java.base/share/classes/sun/security/ssl/DHClientKeyExchange.java
author ascarpino
Wed, 08 Feb 2017 12:08:28 -0800
changeset 43701 fe8c324ba97c
parent 31538 0981099a3e54
permissions -rw-r--r--
8160655: Fix denyAfter and usage types for security properties Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2012, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.math.BigInteger;
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    32
import javax.net.ssl.SSLHandshakeException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Message used by clients to send their Diffie-Hellman public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * keys to servers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
final class DHClientKeyExchange extends HandshakeMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    42
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    int messageType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        return ht_client_key_exchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * This value may be empty if it was included in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * client's certificate ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
    51
    private byte[] dh_Yc;               // 1 to 2^16 -1 bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    BigInteger getClientPublicKey() {
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    54
        return dh_Yc == null ? null : new BigInteger(1, dh_Yc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Either pass the client's public key explicitly (because it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * using DHE or DH_anon), or implicitly (the public key was in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * certificate).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    DHClientKeyExchange(BigInteger publicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        dh_Yc = toByteArray(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    DHClientKeyExchange() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        dh_Yc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Get the client's public key either explicitly or implicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * (It's ugly to have an empty record be sent in the latter case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * but that's what the protocol spec requires.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    DHClientKeyExchange(HandshakeInStream input) throws IOException {
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    76
        if (input.available() >= 2) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    77
            dh_Yc = input.getBytes16();
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    78
        } else {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    79
            // currently, we don't support cipher suites that requires
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    80
            // implicit public key of client.
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    81
            throw new SSLHandshakeException(
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    82
                    "Unsupported implicit client DiffieHellman public key");
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    83
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    86
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    int messageLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (dh_Yc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return dh_Yc.length + 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
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    95
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    void send(HandshakeOutStream s) throws IOException {
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    97
        if (dh_Yc != null && dh_Yc.length != 0) {
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    98
            s.putBytes16(dh_Yc);
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 5506
diff changeset
    99
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   102
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    void print(PrintStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        s.println("*** ClientKeyExchange, DH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (debug != null && Debug.isOn("verbose")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            Debug.println(s, "DH Public key", dh_Yc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}