Path: blob/master/src/java.base/share/classes/sun/security/ssl/KAKeyDerivation.java
41159 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.security.ssl;2526import javax.crypto.KeyAgreement;27import javax.crypto.SecretKey;28import javax.crypto.spec.SecretKeySpec;29import javax.net.ssl.SSLHandshakeException;30import java.io.IOException;31import java.security.GeneralSecurityException;32import java.security.PrivateKey;33import java.security.PublicKey;34import java.security.spec.AlgorithmParameterSpec;3536/**37* A common class for creating various KeyDerivation types.38*/39public class KAKeyDerivation implements SSLKeyDerivation {4041private final String algorithmName;42private final HandshakeContext context;43private final PrivateKey localPrivateKey;44private final PublicKey peerPublicKey;4546KAKeyDerivation(String algorithmName,47HandshakeContext context,48PrivateKey localPrivateKey,49PublicKey peerPublicKey) {50this.algorithmName = algorithmName;51this.context = context;52this.localPrivateKey = localPrivateKey;53this.peerPublicKey = peerPublicKey;54}5556@Override57public SecretKey deriveKey(String algorithm,58AlgorithmParameterSpec params) throws IOException {59if (!context.negotiatedProtocol.useTLS13PlusSpec()) {60return t12DeriveKey(algorithm, params);61} else {62return t13DeriveKey(algorithm, params);63}64}6566/**67* Handle the TLSv1-1.2 objects, which don't use the HKDF algorithms.68*/69private SecretKey t12DeriveKey(String algorithm,70AlgorithmParameterSpec params) throws IOException {71try {72KeyAgreement ka = KeyAgreement.getInstance(algorithmName);73ka.init(localPrivateKey);74ka.doPhase(peerPublicKey, true);75SecretKey preMasterSecret76= ka.generateSecret("TlsPremasterSecret");77SSLMasterKeyDerivation mskd78= SSLMasterKeyDerivation.valueOf(79context.negotiatedProtocol);80if (mskd == null) {81// unlikely82throw new SSLHandshakeException(83"No expected master key derivation for protocol: "84+ context.negotiatedProtocol.name);85}86SSLKeyDerivation kd = mskd.createKeyDerivation(87context, preMasterSecret);88return kd.deriveKey("MasterSecret", params);89} catch (GeneralSecurityException gse) {90throw (SSLHandshakeException) new SSLHandshakeException(91"Could not generate secret").initCause(gse);92}93}9495/**96* Handle the TLSv1.3 objects, which use the HKDF algorithms.97*/98private SecretKey t13DeriveKey(String algorithm,99AlgorithmParameterSpec params) throws IOException {100try {101KeyAgreement ka = KeyAgreement.getInstance(algorithmName);102ka.init(localPrivateKey);103ka.doPhase(peerPublicKey, true);104SecretKey sharedSecret105= ka.generateSecret("TlsPremasterSecret");106107CipherSuite.HashAlg hashAlg = context.negotiatedCipherSuite.hashAlg;108SSLKeyDerivation kd = context.handshakeKeyDerivation;109HKDF hkdf = new HKDF(hashAlg.name);110if (kd == null) { // No PSK is in use.111// If PSK is not in use Early Secret will still be112// HKDF-Extract(0, 0).113byte[] zeros = new byte[hashAlg.hashLength];114SecretKeySpec ikm115= new SecretKeySpec(zeros, "TlsPreSharedSecret");116SecretKey earlySecret117= hkdf.extract(zeros, ikm, "TlsEarlySecret");118kd = new SSLSecretDerivation(context, earlySecret);119}120121// derive salt secret122SecretKey saltSecret = kd.deriveKey("TlsSaltSecret", null);123124// derive handshake secret125return hkdf.extract(saltSecret, sharedSecret, algorithm);126} catch (GeneralSecurityException gse) {127throw (SSLHandshakeException) new SSLHandshakeException(128"Could not generate secret").initCause(gse);129}130}131}132133134