0N/A/*
2362N/A * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.pkcs11;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.interfaces.ECPublicKey;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/A
0N/Aimport javax.crypto.*;
0N/A
0N/Aimport static sun.security.pkcs11.TemplateManager.*;
0N/Aimport sun.security.pkcs11.wrapper.*;
0N/Aimport static sun.security.pkcs11.wrapper.PKCS11Constants.*;
0N/A
0N/A/**
0N/A * KeyAgreement implementation for ECDH.
0N/A *
0N/A * @author Andreas Sterbenz
0N/A * @since 1.6
0N/A */
0N/Afinal class P11ECDHKeyAgreement extends KeyAgreementSpi {
0N/A
0N/A // token instance
0N/A private final Token token;
0N/A
0N/A // algorithm name
0N/A private final String algorithm;
0N/A
0N/A // mechanism id
0N/A private final long mechanism;
0N/A
0N/A // private key, if initialized
0N/A private P11Key privateKey;
0N/A
0N/A // encoded public point, non-null between doPhase() and generateSecret() only
0N/A private byte[] publicValue;
0N/A
0N/A // length of the secret to be derived
0N/A private int secretLen;
0N/A
0N/A P11ECDHKeyAgreement(Token token, String algorithm, long mechanism) {
0N/A super();
0N/A this.token = token;
0N/A this.algorithm = algorithm;
0N/A this.mechanism = mechanism;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected void engineInit(Key key, SecureRandom random)
0N/A throws InvalidKeyException {
0N/A if (key instanceof PrivateKey == false) {
0N/A throw new InvalidKeyException
0N/A ("Key must be instance of PrivateKey");
0N/A }
0N/A privateKey = P11KeyFactory.convertKey(token, key, "EC");
0N/A publicValue = null;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected void engineInit(Key key, AlgorithmParameterSpec params,
0N/A SecureRandom random) throws InvalidKeyException,
0N/A InvalidAlgorithmParameterException {
0N/A if (params != null) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Parameters not supported");
0N/A }
0N/A engineInit(key, random);
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected Key engineDoPhase(Key key, boolean lastPhase)
0N/A throws InvalidKeyException, IllegalStateException {
0N/A if (privateKey == null) {
0N/A throw new IllegalStateException("Not initialized");
0N/A }
0N/A if (publicValue != null) {
0N/A throw new IllegalStateException("Phase already executed");
0N/A }
0N/A if (lastPhase == false) {
0N/A throw new IllegalStateException
0N/A ("Only two party agreement supported, lastPhase must be true");
0N/A }
0N/A if (key instanceof ECPublicKey == false) {
0N/A throw new InvalidKeyException
0N/A ("Key must be a PublicKey with algorithm EC");
0N/A }
0N/A ECPublicKey ecKey = (ECPublicKey)key;
0N/A int keyLenBits = ecKey.getParams().getCurve().getField().getFieldSize();
0N/A secretLen = (keyLenBits + 7) >> 3;
0N/A publicValue = P11ECKeyFactory.getEncodedPublicValue(ecKey);
0N/A return null;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected byte[] engineGenerateSecret() throws IllegalStateException {
0N/A if ((privateKey == null) || (publicValue == null)) {
0N/A throw new IllegalStateException("Not initialized correctly");
0N/A }
0N/A Session session = null;
0N/A try {
0N/A session = token.getOpSession();
0N/A CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
0N/A new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
0N/A new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_GENERIC_SECRET),
0N/A };
0N/A CK_ECDH1_DERIVE_PARAMS ckParams =
0N/A new CK_ECDH1_DERIVE_PARAMS(CKD_NULL, null, publicValue);
0N/A attributes = token.getAttributes
0N/A (O_GENERATE, CKO_SECRET_KEY, CKK_GENERIC_SECRET, attributes);
0N/A long keyID = token.p11.C_DeriveKey(session.id(),
0N/A new CK_MECHANISM(mechanism, ckParams), privateKey.keyID,
0N/A attributes);
0N/A attributes = new CK_ATTRIBUTE[] {
0N/A new CK_ATTRIBUTE(CKA_VALUE)
0N/A };
0N/A token.p11.C_GetAttributeValue(session.id(), keyID, attributes);
0N/A byte[] secret = attributes[0].getByteArray();
0N/A token.p11.C_DestroyObject(session.id(), keyID);
0N/A return secret;
0N/A } catch (PKCS11Exception e) {
0N/A throw new ProviderException("Could not derive key", e);
0N/A } finally {
0N/A publicValue = null;
0N/A token.releaseSession(session);
0N/A }
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected int engineGenerateSecret(byte[] sharedSecret, int
0N/A offset) throws IllegalStateException, ShortBufferException {
0N/A if (offset + secretLen > sharedSecret.length) {
0N/A throw new ShortBufferException("Need " + secretLen
0N/A + " bytes, only " + (sharedSecret.length - offset) + " available");
0N/A }
0N/A byte[] secret = engineGenerateSecret();
0N/A System.arraycopy(secret, 0, sharedSecret, offset, secret.length);
0N/A return secret.length;
0N/A }
0N/A
0N/A // see JCE spec
0N/A protected SecretKey engineGenerateSecret(String algorithm)
0N/A throws IllegalStateException, NoSuchAlgorithmException,
0N/A InvalidKeyException {
0N/A if (algorithm == null) {
0N/A throw new NoSuchAlgorithmException("Algorithm must not be null");
0N/A }
0N/A if (algorithm.equals("TlsPremasterSecret") == false) {
0N/A throw new NoSuchAlgorithmException
0N/A ("Only supported for algorithm TlsPremasterSecret");
0N/A }
0N/A return nativeGenerateSecret(algorithm);
0N/A }
0N/A
0N/A private SecretKey nativeGenerateSecret(String algorithm)
0N/A throws IllegalStateException, NoSuchAlgorithmException,
0N/A InvalidKeyException {
0N/A if ((privateKey == null) || (publicValue == null)) {
0N/A throw new IllegalStateException("Not initialized correctly");
0N/A }
0N/A long keyType = CKK_GENERIC_SECRET;
0N/A Session session = null;
0N/A try {
0N/A session = token.getObjSession();
0N/A CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
0N/A new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
0N/A new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),
0N/A };
0N/A CK_ECDH1_DERIVE_PARAMS ckParams =
0N/A new CK_ECDH1_DERIVE_PARAMS(CKD_NULL, null, publicValue);
0N/A attributes = token.getAttributes
0N/A (O_GENERATE, CKO_SECRET_KEY, keyType, attributes);
0N/A long keyID = token.p11.C_DeriveKey(session.id(),
0N/A new CK_MECHANISM(mechanism, ckParams), privateKey.keyID,
0N/A attributes);
0N/A CK_ATTRIBUTE[] lenAttributes = new CK_ATTRIBUTE[] {
0N/A new CK_ATTRIBUTE(CKA_VALUE_LEN),
0N/A };
0N/A token.p11.C_GetAttributeValue(session.id(), keyID, lenAttributes);
0N/A int keyLen = (int)lenAttributes[0].getLong();
0N/A SecretKey key = P11Key.secretKey
0N/A (session, keyID, algorithm, keyLen << 3, attributes);
0N/A return key;
0N/A } catch (PKCS11Exception e) {
0N/A throw new InvalidKeyException("Could not derive key", e);
0N/A } finally {
0N/A publicValue = null;
0N/A token.releaseSession(session);
0N/A }
0N/A }
0N/A
0N/A}