jdk/src/java.base/share/classes/sun/security/ssl/ECDHCrypt.java
author xuelei
Wed, 23 Mar 2016 12:25:08 +0000
changeset 36661 044bf6a5474a
parent 31712 e4d5230193da
child 39563 1449ed425710
permissions -rw-r--r--
8149017: Delayed provider selection broken in RSA client key exchange Reviewed-by: coffeys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31712
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
     2
 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.interfaces.ECPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
31712
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
    32
import java.util.EnumSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.KeyAgreement;
25807
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 23010
diff changeset
    35
import javax.net.ssl.SSLHandshakeException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Helper class for the ECDH key exchange. It generates the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * ephemeral keys as necessary and performs the actual shared secret derivation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
final class ECDHCrypt {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // our private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private PrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // our public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private ECPublicKey publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // Called by ServerHandshaker for static ECDH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    ECDHCrypt(PrivateKey privateKey, PublicKey publicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.privateKey = privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.publicKey = (ECPublicKey)publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Called by ServerHandshaker for ephemeral ECDH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    ECDHCrypt(String curveName, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            ECGenParameterSpec params = new ECGenParameterSpec(curveName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            kpg.initialize(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            privateKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            publicKey = (ECPublicKey)kp.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new RuntimeException("Could not generate DH keypair", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Called by ClientHandshaker with params it received from the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    ECDHCrypt(ECParameterSpec params, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            kpg.initialize(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            privateKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            publicKey = (ECPublicKey)kp.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new RuntimeException("Could not generate DH keypair", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Gets the public key of this end of the key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    PublicKey getPublicKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
31712
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
    92
    // called by ClientHandshaker with either the server's static or
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
    93
    // ephemeral public key
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
    94
    SecretKey getAgreedSecret(
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
    95
            PublicKey peerPublicKey) throws SSLHandshakeException {
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
    96
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            KeyAgreement ka = JsseJce.getKeyAgreement("ECDH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            ka.init(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            ka.doPhase(peerPublicKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return ka.generateSecret("TlsPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (GeneralSecurityException e) {
25807
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 23010
diff changeset
   103
            throw (SSLHandshakeException) new SSLHandshakeException(
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 23010
diff changeset
   104
                "Could not generate secret").initCause(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // called by ServerHandshaker
31712
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   109
    SecretKey getAgreedSecret(
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   110
            byte[] encodedPoint) throws SSLHandshakeException {
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            ECParameterSpec params = publicKey.getParams();
31712
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   114
            ECPoint point =
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   115
                    JsseJce.decodePoint(encodedPoint, params.getCurve());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            KeyFactory kf = JsseJce.getKeyFactory("EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            ECPublicKeySpec spec = new ECPublicKeySpec(point, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            PublicKey peerPublicKey = kf.generatePublic(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return getAgreedSecret(peerPublicKey);
25807
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 23010
diff changeset
   120
        } catch (GeneralSecurityException | java.io.IOException e) {
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 23010
diff changeset
   121
            throw (SSLHandshakeException) new SSLHandshakeException(
6c325960c9ee 8042982: Unexpected RuntimeExceptions being thrown by SSLEngine
robm
parents: 23010
diff changeset
   122
                "Could not generate secret").initCause(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
31712
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   126
    // Check constraints of the specified EC public key.
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   127
    void checkConstraints(AlgorithmConstraints constraints,
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   128
            byte[] encodedPoint) throws SSLHandshakeException {
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   129
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   130
        try {
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   131
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   132
            ECParameterSpec params = publicKey.getParams();
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   133
            ECPoint point =
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   134
                    JsseJce.decodePoint(encodedPoint, params.getCurve());
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   135
            ECPublicKeySpec spec = new ECPublicKeySpec(point, params);
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   136
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   137
            KeyFactory kf = JsseJce.getKeyFactory("EC");
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   138
            ECPublicKey publicKey = (ECPublicKey)kf.generatePublic(spec);
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   139
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   140
            // check constraints of ECPublicKey
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   141
            if (!constraints.permits(
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   142
                    EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), publicKey)) {
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   143
                throw new SSLHandshakeException(
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   144
                    "ECPublicKey does not comply to algorithm constraints");
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   145
            }
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   146
        } catch (GeneralSecurityException | java.io.IOException e) {
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   147
            throw (SSLHandshakeException) new SSLHandshakeException(
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   148
                    "Could not generate ECPublicKey").initCause(e);
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   149
        }
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   150
    }
e4d5230193da 8076328: Enforce key exchange constraints
xuelei
parents: 25859
diff changeset
   151
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}