Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/pkcs11/P11KeyAgreement.java
38919 views
/*1* Copyright (c) 2003, 2018, 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();7475private static boolean getValue() {76return AccessController.doPrivileged(77(PrivilegedAction<Boolean>)78() -> Boolean.getBoolean("jdk.crypto.KeyAgreement.legacyKDF"));79}80}8182P11KeyAgreement(Token token, String algorithm, long mechanism) {83super();84this.token = token;85this.algorithm = algorithm;86this.mechanism = mechanism;87}8889// see JCE spec90protected void engineInit(Key key, SecureRandom random)91throws InvalidKeyException {92if (key instanceof PrivateKey == false) {93throw new InvalidKeyException94("Key must be instance of PrivateKey");95}96privateKey = P11KeyFactory.convertKey(token, key, algorithm);97publicValue = null;98multiPartyAgreement = null;99}100101// see JCE spec102protected void engineInit(Key key, AlgorithmParameterSpec params,103SecureRandom random) throws InvalidKeyException,104InvalidAlgorithmParameterException {105if (params != null) {106throw new InvalidAlgorithmParameterException107("Parameters not supported");108}109engineInit(key, random);110}111112// see JCE spec113protected Key engineDoPhase(Key key, boolean lastPhase)114throws InvalidKeyException, IllegalStateException {115if (privateKey == null) {116throw new IllegalStateException("Not initialized");117}118if (publicValue != null) {119throw new IllegalStateException("Phase already executed");120}121// PKCS#11 only allows key agreement between 2 parties122// JCE allows >= 2 parties. To support that case (for compatibility123// and to pass JCK), fall back to SunJCE in this case.124// NOTE that we initialize using the P11Key, which will fail if it125// is sensitive/unextractable. However, this is not an issue in the126// compatibility configuration, which is all we are targeting here.127if ((multiPartyAgreement != null) || (lastPhase == false)) {128if (multiPartyAgreement == null) {129try {130multiPartyAgreement = KeyAgreement.getInstance131("DH", P11Util.getSunJceProvider());132multiPartyAgreement.init(privateKey);133} catch (NoSuchAlgorithmException e) {134throw new InvalidKeyException135("Could not initialize multi party agreement", e);136}137}138return multiPartyAgreement.doPhase(key, lastPhase);139}140if ((key instanceof PublicKey == false)141|| (key.getAlgorithm().equals(algorithm) == false)) {142throw new InvalidKeyException143("Key must be a PublicKey with algorithm DH");144}145BigInteger p, g, y;146if (key instanceof DHPublicKey) {147DHPublicKey dhKey = (DHPublicKey)key;148149// validate the Diffie-Hellman public key150KeyUtil.validate(dhKey);151152y = dhKey.getY();153DHParameterSpec params = dhKey.getParams();154p = params.getP();155g = params.getG();156} else {157// normally, DH PublicKeys will always implement DHPublicKey158// just in case not, attempt conversion159P11DHKeyFactory kf = new P11DHKeyFactory(token, "DH");160try {161DHPublicKeySpec spec = kf.engineGetKeySpec(162key, DHPublicKeySpec.class);163164// validate the Diffie-Hellman public key165KeyUtil.validate(spec);166167y = spec.getY();168p = spec.getP();169g = spec.getG();170} catch (InvalidKeySpecException e) {171throw new InvalidKeyException("Could not obtain key values", e);172}173}174// if parameters of private key are accessible, verify that175// they match parameters of public key176// XXX p and g should always be readable, even if the key is sensitive177if (privateKey instanceof DHPrivateKey) {178DHPrivateKey dhKey = (DHPrivateKey)privateKey;179DHParameterSpec params = dhKey.getParams();180if ((p.equals(params.getP()) == false)181|| (g.equals(params.getG()) == false)) {182throw new InvalidKeyException183("PublicKey DH parameters must match PrivateKey DH parameters");184}185}186publicValue = y;187// length of the secret is length of key188secretLen = (p.bitLength() + 7) >> 3;189return null;190}191192// see JCE spec193protected byte[] engineGenerateSecret() throws IllegalStateException {194if (multiPartyAgreement != null) {195byte[] val = multiPartyAgreement.generateSecret();196multiPartyAgreement = null;197return val;198}199if ((privateKey == null) || (publicValue == null)) {200throw new IllegalStateException("Not initialized correctly");201}202Session session = null;203long privKeyID = privateKey.getKeyID();204try {205session = token.getOpSession();206CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {207new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),208new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_GENERIC_SECRET),209};210attributes = token.getAttributes211(O_GENERATE, CKO_SECRET_KEY, CKK_GENERIC_SECRET, attributes);212long keyID = token.p11.C_DeriveKey(session.id(),213new CK_MECHANISM(mechanism, publicValue), privKeyID,214attributes);215216attributes = new CK_ATTRIBUTE[] {217new CK_ATTRIBUTE(CKA_VALUE)218};219token.p11.C_GetAttributeValue(session.id(), keyID, attributes);220byte[] secret = attributes[0].getByteArray();221token.p11.C_DestroyObject(session.id(), keyID);222// Some vendors, e.g. NSS, trim off the leading 0x00 byte(s) from223// the generated secret. Thus, we need to check the secret length224// and trim/pad it so the returned value has the same length as225// the modulus size226if (secret.length == secretLen) {227return secret;228} else {229if (secret.length > secretLen) {230// Shouldn't happen; but check just in case231throw new ProviderException("generated secret is out-of-range");232}233byte[] newSecret = new byte[secretLen];234System.arraycopy(secret, 0, newSecret, secretLen - secret.length,235secret.length);236return newSecret;237}238} catch (PKCS11Exception e) {239throw new ProviderException("Could not derive key", e);240} finally {241privateKey.releaseKeyID();242publicValue = null;243token.releaseSession(session);244}245}246247// see JCE spec248protected int engineGenerateSecret(byte[] sharedSecret, int249offset) throws IllegalStateException, ShortBufferException {250if (multiPartyAgreement != null) {251int n = multiPartyAgreement.generateSecret(sharedSecret, offset);252multiPartyAgreement = null;253return n;254}255if (offset + secretLen > sharedSecret.length) {256throw new ShortBufferException("Need " + secretLen257+ " bytes, only " + (sharedSecret.length - offset) + " available");258}259byte[] secret = engineGenerateSecret();260System.arraycopy(secret, 0, sharedSecret, offset, secret.length);261return secret.length;262}263264// see JCE spec265protected SecretKey engineGenerateSecret(String algorithm)266throws IllegalStateException, NoSuchAlgorithmException,267InvalidKeyException {268if (multiPartyAgreement != null) {269SecretKey key = multiPartyAgreement.generateSecret(algorithm);270multiPartyAgreement = null;271return key;272}273if (algorithm == null) {274throw new NoSuchAlgorithmException("Algorithm must not be null");275}276277if (algorithm.equals("TlsPremasterSecret")) {278// For now, only perform native derivation for TlsPremasterSecret279// as that is required for FIPS compliance.280// For other algorithms, there are unresolved issues regarding281// how this should work in JCE plus a Solaris truncation bug.282// (bug not yet filed).283return nativeGenerateSecret(algorithm);284}285286if (!algorithm.equalsIgnoreCase("TlsPremasterSecret") &&287!AllowKDF.VALUE) {288289throw new NoSuchAlgorithmException("Unsupported secret key "290+ "algorithm: " + algorithm);291}292293byte[] secret = engineGenerateSecret();294// Maintain compatibility for SunJCE:295// verify secret length is sensible for algorithm / truncate296// return generated key itself if possible297int keyLen;298if (algorithm.equalsIgnoreCase("DES")) {299keyLen = 8;300} else if (algorithm.equalsIgnoreCase("DESede")) {301keyLen = 24;302} else if (algorithm.equalsIgnoreCase("Blowfish")) {303keyLen = Math.min(56, secret.length);304} else if (algorithm.equalsIgnoreCase("TlsPremasterSecret")) {305keyLen = secret.length;306} else {307throw new NoSuchAlgorithmException308("Unknown algorithm " + algorithm);309}310if (secret.length < keyLen) {311throw new InvalidKeyException("Secret too short");312}313if (algorithm.equalsIgnoreCase("DES") ||314algorithm.equalsIgnoreCase("DESede")) {315for (int i = 0; i < keyLen; i+=8) {316P11SecretKeyFactory.fixDESParity(secret, i);317}318}319return new SecretKeySpec(secret, 0, keyLen, algorithm);320}321322private SecretKey nativeGenerateSecret(String algorithm)323throws IllegalStateException, NoSuchAlgorithmException,324InvalidKeyException {325if ((privateKey == null) || (publicValue == null)) {326throw new IllegalStateException("Not initialized correctly");327}328long keyType = CKK_GENERIC_SECRET;329Session session = null;330long privKeyID = privateKey.getKeyID();331try {332session = token.getObjSession();333CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {334new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),335new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),336};337attributes = token.getAttributes338(O_GENERATE, CKO_SECRET_KEY, keyType, attributes);339long keyID = token.p11.C_DeriveKey(session.id(),340new CK_MECHANISM(mechanism, publicValue), privKeyID,341attributes);342CK_ATTRIBUTE[] lenAttributes = new CK_ATTRIBUTE[] {343new CK_ATTRIBUTE(CKA_VALUE_LEN),344};345token.p11.C_GetAttributeValue(session.id(), keyID, lenAttributes);346int keyLen = (int)lenAttributes[0].getLong();347SecretKey key = P11Key.secretKey348(session, keyID, algorithm, keyLen << 3, attributes);349if ("RAW".equals(key.getFormat())) {350// Workaround for Solaris bug 6318543.351// Strip leading zeroes ourselves if possible (key not sensitive).352// This should be removed once the Solaris fix is available353// as here we always retrieve the CKA_VALUE even for tokens354// that do not have that bug.355byte[] keyBytes = key.getEncoded();356byte[] newBytes = KeyUtil.trimZeroes(keyBytes);357if (keyBytes != newBytes) {358key = new SecretKeySpec(newBytes, algorithm);359}360}361return key;362} catch (PKCS11Exception e) {363throw new InvalidKeyException("Could not derive key", e);364} finally {365privateKey.releaseKeyID();366publicValue = null;367token.releaseSession(session);368}369}370371}372373374