jdk/src/share/classes/sun/security/pkcs11/P11KeyAgreement.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 12201 d77ed23f4992
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * KeyAgreement implementation class. This class currently supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * DH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
final class P11KeyAgreement extends KeyAgreementSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // private key, if initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private P11Key privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // other sides public value ("y"), if doPhase() already called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private BigInteger publicValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // length of the secret to be derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int secretLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // KeyAgreement from SunJCE as fallback for > 2 party agreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private KeyAgreement multiPartyAgreement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    P11KeyAgreement(Token token, String algorithm, long mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected void engineInit(Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (key instanceof PrivateKey == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        ("Key must be instance of PrivateKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        privateKey = P11KeyFactory.convertKey(token, key, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        publicValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected void engineInit(Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            SecureRandom random) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        engineInit(key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected Key engineDoPhase(Key key, boolean lastPhase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throws InvalidKeyException, IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (privateKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new IllegalStateException("Not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (publicValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new IllegalStateException("Phase already executed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // PKCS#11 only allows key agreement between 2 parties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // JCE allows >= 2 parties. To support that case (for compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // and to pass JCK), fall back to SunJCE in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // NOTE that we initialize using the P11Key, which will fail if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // is sensitive/unextractable. However, this is not an issue in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // compatibility configuration, which is all we are targeting here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if ((multiPartyAgreement != null) || (lastPhase == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (multiPartyAgreement == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    multiPartyAgreement = KeyAgreement.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        ("DH", P11Util.getSunJceProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    multiPartyAgreement.init(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        ("Could not initialize multi party agreement", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return multiPartyAgreement.doPhase(key, lastPhase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if ((key instanceof PublicKey == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                || (key.getAlgorithm().equals(algorithm) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                ("Key must be a PublicKey with algorithm DH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        BigInteger p, g, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (key instanceof DHPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            DHPublicKey dhKey = (DHPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            y = dhKey.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            DHParameterSpec params = dhKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            p = params.getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            g = params.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // normally, DH PublicKeys will always implement DHPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // just in case not, attempt conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            P11DHKeyFactory kf = new P11DHKeyFactory(token, "DH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   146
                DHPublicKeySpec spec = kf.engineGetKeySpec(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   147
                        key, DHPublicKeySpec.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                y = spec.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                p = spec.getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                g = spec.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                throw new InvalidKeyException("Could not obtain key values", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // if parameters of private key are accessible, verify that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // they match parameters of public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // XXX p and g should always be readable, even if the key is sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (privateKey instanceof DHPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            DHPrivateKey dhKey = (DHPrivateKey)privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            DHParameterSpec params = dhKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if ((p.equals(params.getP()) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                || (g.equals(params.getG()) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                ("PublicKey DH parameters must match PrivateKey DH parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        publicValue = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // length of the secret is length of key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        secretLen = (p.bitLength() + 7) >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected byte[] engineGenerateSecret() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (multiPartyAgreement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            byte[] val = multiPartyAgreement.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if ((privateKey == null) || (publicValue == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new IllegalStateException("Not initialized correctly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_GENERIC_SECRET),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                (O_GENERATE, CKO_SECRET_KEY, CKK_GENERIC_SECRET, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            long keyID = token.p11.C_DeriveKey(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                new CK_MECHANISM(mechanism, publicValue), privateKey.keyID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                new CK_ATTRIBUTE(CKA_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            token.p11.C_GetAttributeValue(session.id(), keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            byte[] secret = attributes[0].getByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            token.p11.C_DestroyObject(session.id(), keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // trim leading 0x00 bytes per JCE convention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return P11Util.trimZeroes(secret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new ProviderException("Could not derive key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            publicValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    protected int engineGenerateSecret(byte[] sharedSecret, int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            offset) throws IllegalStateException, ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (multiPartyAgreement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            int n = multiPartyAgreement.generateSecret(sharedSecret, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (offset + secretLen > sharedSecret.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new ShortBufferException("Need " + secretLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                + " bytes, only " + (sharedSecret.length - offset) + " available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        byte[] secret = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        System.arraycopy(secret, 0, sharedSecret, offset, secret.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    protected SecretKey engineGenerateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (multiPartyAgreement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            SecretKey key = multiPartyAgreement.generateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (algorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new NoSuchAlgorithmException("Algorithm must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (algorithm.equals("TlsPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            // For now, only perform native derivation for TlsPremasterSecret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // as that is required for FIPS compliance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // For other algorithms, there are unresolved issues regarding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // how this should work in JCE plus a Solaris truncation bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            // (bug not yet filed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return nativeGenerateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        byte[] secret = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // Maintain compatibility for SunJCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // verify secret length is sensible for algorithm / truncate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // return generated key itself if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int keyLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (algorithm.equalsIgnoreCase("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            keyLen = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } else if (algorithm.equalsIgnoreCase("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            keyLen = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else if (algorithm.equalsIgnoreCase("Blowfish")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            keyLen = Math.min(56, secret.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } else if (algorithm.equalsIgnoreCase("TlsPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            keyLen = secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                ("Unknown algorithm " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (secret.length < keyLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw new InvalidKeyException("Secret too short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (algorithm.equalsIgnoreCase("DES") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            algorithm.equalsIgnoreCase("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                for (int i = 0; i < keyLen; i+=8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    P11SecretKeyFactory.fixDESParity(secret, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return new SecretKeySpec(secret, 0, keyLen, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private SecretKey nativeGenerateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if ((privateKey == null) || (publicValue == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new IllegalStateException("Not initialized correctly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        long keyType = CKK_GENERIC_SECRET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                (O_GENERATE, CKO_SECRET_KEY, keyType, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            long keyID = token.p11.C_DeriveKey(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                new CK_MECHANISM(mechanism, publicValue), privateKey.keyID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            CK_ATTRIBUTE[] lenAttributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                new CK_ATTRIBUTE(CKA_VALUE_LEN),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            token.p11.C_GetAttributeValue(session.id(), keyID, lenAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            int keyLen = (int)lenAttributes[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            SecretKey key = P11Key.secretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        (session, keyID, algorithm, keyLen << 3, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if ("RAW".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                // Workaround for Solaris bug 6318543.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                // Strip leading zeroes ourselves if possible (key not sensitive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                // This should be removed once the Solaris fix is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                // as here we always retrieve the CKA_VALUE even for tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                // that do not have that bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                byte[] keyBytes = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                byte[] newBytes = P11Util.trimZeroes(keyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                if (keyBytes != newBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    key = new SecretKeySpec(newBytes, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            throw new InvalidKeyException("Could not derive key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            publicValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
}