Path: blob/master/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyAgreement.java
41154 views
/*1* Copyright (c) 2003, 2021, 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*/2425package sun.security.pkcs11;2627import java.math.BigInteger;2829import java.security.*;30import java.security.spec.*;3132import javax.crypto.*;33import javax.crypto.interfaces.*;34import javax.crypto.spec.*;3536import static sun.security.pkcs11.TemplateManager.*;37import sun.security.pkcs11.wrapper.*;38import static sun.security.pkcs11.wrapper.PKCS11Constants.*;39import sun.security.util.KeyUtil;4041/**42* KeyAgreement implementation class. This class currently supports43* DH.44*45* @author Andreas Sterbenz46* @since 1.547*/48final class P11KeyAgreement extends KeyAgreementSpi {4950// token instance51private final Token token;5253// algorithm name54private final String algorithm;5556// mechanism id57private final long mechanism;5859// private key, if initialized60private P11Key privateKey;6162// other sides public value ("y"), if doPhase() already called63private BigInteger publicValue;6465// length of the secret to be derived66private int secretLen;6768// KeyAgreement from SunJCE as fallback for > 2 party agreement69private KeyAgreement multiPartyAgreement;7071private static class AllowKDF {7273private static final boolean VALUE = getValue();7475@SuppressWarnings("removal")76private static boolean getValue() {77return AccessController.doPrivileged(78(PrivilegedAction<Boolean>)79() -> Boolean.getBoolean("jdk.crypto.KeyAgreement.legacyKDF"));80}81}8283P11KeyAgreement(Token token, String algorithm, long mechanism) {84super();85this.token = token;86this.algorithm = algorithm;87this.mechanism = mechanism;88}8990// see JCE spec91protected void engineInit(Key key, SecureRandom random)92throws InvalidKeyException {93if (key instanceof PrivateKey == false) {94throw new InvalidKeyException95("Key must be instance of PrivateKey");96}97privateKey = P11KeyFactory.convertKey(token, key, algorithm);98publicValue = null;99multiPartyAgreement = null;100}101102// see JCE spec103protected void engineInit(Key key, AlgorithmParameterSpec params,104SecureRandom random) throws InvalidKeyException,105InvalidAlgorithmParameterException {106if (params != null) {107throw new InvalidAlgorithmParameterException108("Parameters not supported");109}110engineInit(key, random);111}112113// see JCE spec114protected Key engineDoPhase(Key key, boolean lastPhase)115throws InvalidKeyException, IllegalStateException {116if (privateKey == null) {117throw new IllegalStateException("Not initialized");118}119if (publicValue != null) {120throw new IllegalStateException("Phase already executed");121}122// PKCS#11 only allows key agreement between 2 parties123// JCE allows >= 2 parties. To support that case (for compatibility124// and to pass JCK), fall back to SunJCE in this case.125// NOTE that we initialize using the P11Key, which will fail if it126// is sensitive/unextractable. However, this is not an issue in the127// compatibility configuration, which is all we are targeting here.128if ((multiPartyAgreement != null) || (lastPhase == false)) {129if (multiPartyAgreement == null) {130try {131multiPartyAgreement = KeyAgreement.getInstance132("DH", P11Util.getSunJceProvider());133multiPartyAgreement.init(privateKey);134} catch (NoSuchAlgorithmException e) {135throw new InvalidKeyException136("Could not initialize multi party agreement", e);137}138}139return multiPartyAgreement.doPhase(key, lastPhase);140}141if ((key instanceof PublicKey == false)142|| (key.getAlgorithm().equals(algorithm) == false)) {143throw new InvalidKeyException144("Key must be a PublicKey with algorithm DH");145}146BigInteger p, g, y;147if (key instanceof DHPublicKey) {148DHPublicKey dhKey = (DHPublicKey)key;149150// validate the Diffie-Hellman public key151KeyUtil.validate(dhKey);152153y = dhKey.getY();154DHParameterSpec params = dhKey.getParams();155p = params.getP();156g = params.getG();157} else {158// normally, DH PublicKeys will always implement DHPublicKey159// just in case not, attempt conversion160P11DHKeyFactory kf = new P11DHKeyFactory(token, "DH");161try {162DHPublicKeySpec spec = kf.engineGetKeySpec(163key, DHPublicKeySpec.class);164165// validate the Diffie-Hellman public key166KeyUtil.validate(spec);167168y = spec.getY();169p = spec.getP();170g = spec.getG();171} catch (InvalidKeySpecException e) {172throw new InvalidKeyException("Could not obtain key values", e);173}174}175// if parameters of private key are accessible, verify that176// they match parameters of public key177// XXX p and g should always be readable, even if the key is sensitive178if (privateKey instanceof DHPrivateKey) {179DHPrivateKey dhKey = (DHPrivateKey)privateKey;180DHParameterSpec params = dhKey.getParams();181if ((p.equals(params.getP()) == false)182|| (g.equals(params.getG()) == false)) {183throw new InvalidKeyException184("PublicKey DH parameters must match PrivateKey DH parameters");185}186}187publicValue = y;188// length of the secret is length of key189secretLen = (p.bitLength() + 7) >> 3;190return null;191}192193// see JCE spec194protected byte[] engineGenerateSecret() throws IllegalStateException {195if (multiPartyAgreement != null) {196byte[] val = multiPartyAgreement.generateSecret();197multiPartyAgreement = null;198return val;199}200if ((privateKey == null) || (publicValue == null)) {201throw new IllegalStateException("Not initialized correctly");202}203Session session = null;204long privKeyID = privateKey.getKeyID();205try {206session = token.getOpSession();207CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {208new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),209new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_GENERIC_SECRET),210};211attributes = token.getAttributes212(O_GENERATE, CKO_SECRET_KEY, CKK_GENERIC_SECRET, attributes);213long keyID = token.p11.C_DeriveKey(session.id(),214new CK_MECHANISM(mechanism, publicValue), privKeyID,215attributes);216217attributes = new CK_ATTRIBUTE[] {218new CK_ATTRIBUTE(CKA_VALUE)219};220token.p11.C_GetAttributeValue(session.id(), keyID, attributes);221byte[] secret = attributes[0].getByteArray();222token.p11.C_DestroyObject(session.id(), keyID);223// Some vendors, e.g. NSS, trim off the leading 0x00 byte(s) from224// the generated secret. Thus, we need to check the secret length225// and trim/pad it so the returned value has the same length as226// the modulus size227if (secret.length == secretLen) {228return secret;229} else {230if (secret.length > secretLen) {231// Shouldn't happen; but check just in case232throw new ProviderException("generated secret is out-of-range");233}234byte[] newSecret = new byte[secretLen];235System.arraycopy(secret, 0, newSecret, secretLen - secret.length,236secret.length);237return newSecret;238}239} catch (PKCS11Exception e) {240throw new ProviderException("Could not derive key", e);241} finally {242privateKey.releaseKeyID();243publicValue = null;244token.releaseSession(session);245}246}247248// see JCE spec249protected int engineGenerateSecret(byte[] sharedSecret, int250offset) throws IllegalStateException, ShortBufferException {251if (multiPartyAgreement != null) {252int n = multiPartyAgreement.generateSecret(sharedSecret, offset);253multiPartyAgreement = null;254return n;255}256if (offset + secretLen > sharedSecret.length) {257throw new ShortBufferException("Need " + secretLen258+ " bytes, only " + (sharedSecret.length - offset) + " available");259}260byte[] secret = engineGenerateSecret();261System.arraycopy(secret, 0, sharedSecret, offset, secret.length);262return secret.length;263}264265// see JCE spec266protected SecretKey engineGenerateSecret(String algorithm)267throws IllegalStateException, NoSuchAlgorithmException,268InvalidKeyException {269if (multiPartyAgreement != null) {270SecretKey key = multiPartyAgreement.generateSecret(algorithm);271multiPartyAgreement = null;272return key;273}274if (algorithm == null) {275throw new NoSuchAlgorithmException("Algorithm must not be null");276}277278if (algorithm.equals("TlsPremasterSecret")) {279// For now, only perform native derivation for TlsPremasterSecret280// as that is required for FIPS compliance.281// For other algorithms, there are unresolved issues regarding282// how this should work in JCE plus a Solaris truncation bug.283// (bug not yet filed).284return nativeGenerateSecret(algorithm);285}286287if (!algorithm.equalsIgnoreCase("TlsPremasterSecret") &&288!AllowKDF.VALUE) {289290throw new NoSuchAlgorithmException("Unsupported secret key "291+ "algorithm: " + algorithm);292}293294byte[] secret = engineGenerateSecret();295// Maintain compatibility for SunJCE:296// verify secret length is sensible for algorithm / truncate297// return generated key itself if possible298int keyLen;299if (algorithm.equalsIgnoreCase("DES")) {300keyLen = 8;301} else if (algorithm.equalsIgnoreCase("DESede")) {302keyLen = 24;303} else if (algorithm.equalsIgnoreCase("Blowfish")) {304keyLen = Math.min(56, secret.length);305} else if (algorithm.equalsIgnoreCase("TlsPremasterSecret")) {306keyLen = secret.length;307} else {308throw new NoSuchAlgorithmException309("Unknown algorithm " + algorithm);310}311if (secret.length < keyLen) {312throw new InvalidKeyException("Secret too short");313}314if (algorithm.equalsIgnoreCase("DES") ||315algorithm.equalsIgnoreCase("DESede")) {316for (int i = 0; i < keyLen; i+=8) {317P11SecretKeyFactory.fixDESParity(secret, i);318}319}320return new SecretKeySpec(secret, 0, keyLen, algorithm);321}322323private SecretKey nativeGenerateSecret(String algorithm)324throws IllegalStateException, NoSuchAlgorithmException,325InvalidKeyException {326if ((privateKey == null) || (publicValue == null)) {327throw new IllegalStateException("Not initialized correctly");328}329long keyType = CKK_GENERIC_SECRET;330Session session = null;331long privKeyID = privateKey.getKeyID();332try {333session = token.getObjSession();334CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {335new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),336new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),337};338attributes = token.getAttributes339(O_GENERATE, CKO_SECRET_KEY, keyType, attributes);340long keyID = token.p11.C_DeriveKey(session.id(),341new CK_MECHANISM(mechanism, publicValue), privKeyID,342attributes);343CK_ATTRIBUTE[] lenAttributes = new CK_ATTRIBUTE[] {344new CK_ATTRIBUTE(CKA_VALUE_LEN),345};346token.p11.C_GetAttributeValue(session.id(), keyID, lenAttributes);347int keyLen = (int)lenAttributes[0].getLong();348SecretKey key = P11Key.secretKey349(session, keyID, algorithm, keyLen << 3, attributes);350if ("RAW".equals(key.getFormat())) {351// Workaround for Solaris bug 6318543.352// Strip leading zeroes ourselves if possible (key not sensitive).353// This should be removed once the Solaris fix is available354// as here we always retrieve the CKA_VALUE even for tokens355// that do not have that bug.356byte[] keyBytes = key.getEncoded();357byte[] newBytes = KeyUtil.trimZeroes(keyBytes);358if (keyBytes != newBytes) {359key = new SecretKeySpec(newBytes, algorithm);360}361}362return key;363} catch (PKCS11Exception e) {364throw new InvalidKeyException("Could not derive key", e);365} finally {366privateKey.releaseKeyID();367publicValue = null;368token.releaseSession(session);369}370}371372}373374375