src/java.base/share/classes/sun/security/ssl/ECDHCrypt.java
changeset 50768 68fa3d4026ea
parent 50767 356eaea05bf0
child 50769 1bf8f9840705
equal deleted inserted replaced
50767:356eaea05bf0 50768:68fa3d4026ea
     1 /*
       
     2  * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.security.ssl;
       
    27 
       
    28 import java.security.*;
       
    29 import java.security.interfaces.ECPublicKey;
       
    30 import java.security.spec.*;
       
    31 
       
    32 import java.util.EnumSet;
       
    33 import javax.crypto.SecretKey;
       
    34 import javax.crypto.KeyAgreement;
       
    35 import javax.net.ssl.SSLHandshakeException;
       
    36 
       
    37 /**
       
    38  * Helper class for the ECDH key exchange. It generates the appropriate
       
    39  * ephemeral keys as necessary and performs the actual shared secret derivation.
       
    40  *
       
    41  * @since   1.6
       
    42  * @author  Andreas Sterbenz
       
    43  */
       
    44 final class ECDHCrypt {
       
    45 
       
    46     // our private key
       
    47     private PrivateKey privateKey;
       
    48 
       
    49     // our public key
       
    50     private ECPublicKey publicKey;
       
    51 
       
    52     // Called by ServerHandshaker for static ECDH
       
    53     ECDHCrypt(PrivateKey privateKey, PublicKey publicKey) {
       
    54         this.privateKey = privateKey;
       
    55         this.publicKey = (ECPublicKey)publicKey;
       
    56     }
       
    57 
       
    58     // Called by ServerHandshaker for ephemeral ECDH
       
    59     ECDHCrypt(NamedGroup namedGroup, SecureRandom random) {
       
    60         try {
       
    61             KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("EC");
       
    62             ECGenParameterSpec params =
       
    63                     SupportedGroupsExtension.getECGenParamSpec(namedGroup);
       
    64             kpg.initialize(params, random);
       
    65             KeyPair kp = kpg.generateKeyPair();
       
    66             privateKey = kp.getPrivate();
       
    67             publicKey = (ECPublicKey)kp.getPublic();
       
    68         } catch (GeneralSecurityException e) {
       
    69             throw new RuntimeException("Could not generate ECDH keypair", e);
       
    70         }
       
    71     }
       
    72 
       
    73     // Called by ClientHandshaker with params it received from the server
       
    74     ECDHCrypt(ECParameterSpec params, SecureRandom random) {
       
    75         try {
       
    76             KeyPairGenerator kpg = JsseJce.getKeyPairGenerator("EC");
       
    77             kpg.initialize(params, random);
       
    78             KeyPair kp = kpg.generateKeyPair();
       
    79             privateKey = kp.getPrivate();
       
    80             publicKey = (ECPublicKey)kp.getPublic();
       
    81         } catch (GeneralSecurityException e) {
       
    82             throw new RuntimeException("Could not generate ECDH keypair", e);
       
    83         }
       
    84     }
       
    85 
       
    86     /**
       
    87      * Gets the public key of this end of the key exchange.
       
    88      */
       
    89     PublicKey getPublicKey() {
       
    90         return publicKey;
       
    91     }
       
    92 
       
    93     // called by ClientHandshaker with either the server's static or
       
    94     // ephemeral public key
       
    95     SecretKey getAgreedSecret(
       
    96             PublicKey peerPublicKey) throws SSLHandshakeException {
       
    97 
       
    98         try {
       
    99             KeyAgreement ka = JsseJce.getKeyAgreement("ECDH");
       
   100             ka.init(privateKey);
       
   101             ka.doPhase(peerPublicKey, true);
       
   102             return ka.generateSecret("TlsPremasterSecret");
       
   103         } catch (GeneralSecurityException e) {
       
   104             throw (SSLHandshakeException) new SSLHandshakeException(
       
   105                 "Could not generate secret").initCause(e);
       
   106         }
       
   107     }
       
   108 
       
   109     // called by ServerHandshaker
       
   110     SecretKey getAgreedSecret(
       
   111             byte[] encodedPoint) throws SSLHandshakeException {
       
   112 
       
   113         try {
       
   114             ECParameterSpec params = publicKey.getParams();
       
   115             ECPoint point =
       
   116                     JsseJce.decodePoint(encodedPoint, params.getCurve());
       
   117             KeyFactory kf = JsseJce.getKeyFactory("EC");
       
   118             ECPublicKeySpec spec = new ECPublicKeySpec(point, params);
       
   119             PublicKey peerPublicKey = kf.generatePublic(spec);
       
   120             return getAgreedSecret(peerPublicKey);
       
   121         } catch (GeneralSecurityException | java.io.IOException e) {
       
   122             throw (SSLHandshakeException) new SSLHandshakeException(
       
   123                 "Could not generate secret").initCause(e);
       
   124         }
       
   125     }
       
   126 
       
   127     // Check constraints of the specified EC public key.
       
   128     void checkConstraints(AlgorithmConstraints constraints,
       
   129             byte[] encodedPoint) throws SSLHandshakeException {
       
   130 
       
   131         try {
       
   132 
       
   133             ECParameterSpec params = publicKey.getParams();
       
   134             ECPoint point =
       
   135                     JsseJce.decodePoint(encodedPoint, params.getCurve());
       
   136             ECPublicKeySpec spec = new ECPublicKeySpec(point, params);
       
   137 
       
   138             KeyFactory kf = JsseJce.getKeyFactory("EC");
       
   139             ECPublicKey publicKey = (ECPublicKey)kf.generatePublic(spec);
       
   140 
       
   141             // check constraints of ECPublicKey
       
   142             if (!constraints.permits(
       
   143                     EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), publicKey)) {
       
   144                 throw new SSLHandshakeException(
       
   145                     "ECPublicKey does not comply to algorithm constraints");
       
   146             }
       
   147         } catch (GeneralSecurityException | java.io.IOException e) {
       
   148             throw (SSLHandshakeException) new SSLHandshakeException(
       
   149                     "Could not generate ECPublicKey").initCause(e);
       
   150         }
       
   151     }
       
   152 
       
   153 }