jdk/src/share/classes/sun/security/pkcs11/P11KeyAgreement.java
author xuelei
Thu, 30 May 2013 22:02:43 -0700
changeset 17916 e02ddef88f77
parent 16080 0e6266b88242
permissions -rw-r--r--
8014618: Need to strip leading zeros in TlsPremasterSecret of DHKeyAgreement Reviewed-by: xuelei Contributed-by: Pasi Eronen <pe@iki.fi>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17916
e02ddef88f77 8014618: Need to strip leading zeros in TlsPremasterSecret of DHKeyAgreement
xuelei
parents: 16080
diff changeset
     2
 * Copyright (c) 2003, 2013, 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.*;
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 12201
diff changeset
    40
import sun.security.util.KeyUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * KeyAgreement implementation class. This class currently supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * DH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
final class P11KeyAgreement extends KeyAgreementSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // private key, if initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private P11Key privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // other sides public value ("y"), if doPhase() already called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private BigInteger publicValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // length of the secret to be derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int secretLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // KeyAgreement from SunJCE as fallback for > 2 party agreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private KeyAgreement multiPartyAgreement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    P11KeyAgreement(Token token, String algorithm, long mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected void engineInit(Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (key instanceof PrivateKey == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        ("Key must be instance of PrivateKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        privateKey = P11KeyFactory.convertKey(token, key, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        publicValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected void engineInit(Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            SecureRandom random) throws InvalidKeyException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        engineInit(key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected Key engineDoPhase(Key key, boolean lastPhase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throws InvalidKeyException, IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (privateKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new IllegalStateException("Not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (publicValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new IllegalStateException("Phase already executed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // PKCS#11 only allows key agreement between 2 parties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // JCE allows >= 2 parties. To support that case (for compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // and to pass JCK), fall back to SunJCE in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // NOTE that we initialize using the P11Key, which will fail if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // is sensitive/unextractable. However, this is not an issue in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // compatibility configuration, which is all we are targeting here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if ((multiPartyAgreement != null) || (lastPhase == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (multiPartyAgreement == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    multiPartyAgreement = KeyAgreement.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        ("DH", P11Util.getSunJceProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    multiPartyAgreement.init(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        ("Could not initialize multi party agreement", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return multiPartyAgreement.doPhase(key, lastPhase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if ((key instanceof PublicKey == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                || (key.getAlgorithm().equals(algorithm) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                ("Key must be a PublicKey with algorithm DH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        BigInteger p, g, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (key instanceof DHPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            DHPublicKey dhKey = (DHPublicKey)key;
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 12201
diff changeset
   138
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 12201
diff changeset
   139
            // validate the Diffie-Hellman public key
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 12201
diff changeset
   140
            KeyUtil.validate(dhKey);
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 12201
diff changeset
   141
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            y = dhKey.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            DHParameterSpec params = dhKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            p = params.getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            g = params.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // normally, DH PublicKeys will always implement DHPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // just in case not, attempt conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            P11DHKeyFactory kf = new P11DHKeyFactory(token, "DH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   151
                DHPublicKeySpec spec = kf.engineGetKeySpec(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   152
                        key, DHPublicKeySpec.class);
16080
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 12201
diff changeset
   153
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 12201
diff changeset
   154
                // validate the Diffie-Hellman public key
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 12201
diff changeset
   155
                KeyUtil.validate(spec);
0e6266b88242 7192392: Better validation of client keys
xuelei
parents: 12201
diff changeset
   156
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                y = spec.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                p = spec.getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                g = spec.getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                throw new InvalidKeyException("Could not obtain key values", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // if parameters of private key are accessible, verify that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // they match parameters of public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // XXX p and g should always be readable, even if the key is sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (privateKey instanceof DHPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            DHPrivateKey dhKey = (DHPrivateKey)privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            DHParameterSpec params = dhKey.getParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if ((p.equals(params.getP()) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                || (g.equals(params.getG()) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                ("PublicKey DH parameters must match PrivateKey DH parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        publicValue = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // length of the secret is length of key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        secretLen = (p.bitLength() + 7) >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected byte[] engineGenerateSecret() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (multiPartyAgreement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            byte[] val = multiPartyAgreement.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if ((privateKey == null) || (publicValue == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new IllegalStateException("Not initialized correctly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_GENERIC_SECRET),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                (O_GENERATE, CKO_SECRET_KEY, CKK_GENERIC_SECRET, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            long keyID = token.p11.C_DeriveKey(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                new CK_MECHANISM(mechanism, publicValue), privateKey.keyID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                new CK_ATTRIBUTE(CKA_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            token.p11.C_GetAttributeValue(session.id(), keyID, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            byte[] secret = attributes[0].getByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            token.p11.C_DestroyObject(session.id(), keyID);
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   210
            // Some vendors, e.g. NSS, trim off the leading 0x00 byte(s) from
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   211
            // the generated secret. Thus, we need to check the secret length
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   212
            // and trim/pad it so the returned value has the same length as
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   213
            // the modulus size
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   214
            if (secret.length == secretLen) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   215
                return secret;
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   216
            } else {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   217
                if (secret.length > secretLen) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   218
                    // Shouldn't happen; but check just in case
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   219
                    throw new ProviderException("generated secret is out-of-range");
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   220
                }
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   221
                byte[] newSecret = new byte[secretLen];
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   222
                System.arraycopy(secret, 0, newSecret, secretLen - secret.length,
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   223
                    secret.length);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   224
                return newSecret;
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   225
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new ProviderException("Could not derive key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            publicValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    protected int engineGenerateSecret(byte[] sharedSecret, int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            offset) throws IllegalStateException, ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (multiPartyAgreement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int n = multiPartyAgreement.generateSecret(sharedSecret, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (offset + secretLen > sharedSecret.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new ShortBufferException("Need " + secretLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                + " bytes, only " + (sharedSecret.length - offset) + " available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        byte[] secret = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        System.arraycopy(secret, 0, sharedSecret, offset, secret.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    protected SecretKey engineGenerateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (multiPartyAgreement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            SecretKey key = multiPartyAgreement.generateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (algorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throw new NoSuchAlgorithmException("Algorithm must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (algorithm.equals("TlsPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // For now, only perform native derivation for TlsPremasterSecret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // as that is required for FIPS compliance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            // For other algorithms, there are unresolved issues regarding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // how this should work in JCE plus a Solaris truncation bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // (bug not yet filed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return nativeGenerateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        byte[] secret = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // Maintain compatibility for SunJCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // verify secret length is sensible for algorithm / truncate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // return generated key itself if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int keyLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (algorithm.equalsIgnoreCase("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            keyLen = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } else if (algorithm.equalsIgnoreCase("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            keyLen = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } else if (algorithm.equalsIgnoreCase("Blowfish")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            keyLen = Math.min(56, secret.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } else if (algorithm.equalsIgnoreCase("TlsPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            keyLen = secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                ("Unknown algorithm " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (secret.length < keyLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new InvalidKeyException("Secret too short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (algorithm.equalsIgnoreCase("DES") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            algorithm.equalsIgnoreCase("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                for (int i = 0; i < keyLen; i+=8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    P11SecretKeyFactory.fixDESParity(secret, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return new SecretKeySpec(secret, 0, keyLen, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private SecretKey nativeGenerateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if ((privateKey == null) || (publicValue == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw new IllegalStateException("Not initialized correctly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        long keyType = CKK_GENERIC_SECRET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                (O_GENERATE, CKO_SECRET_KEY, keyType, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            long keyID = token.p11.C_DeriveKey(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                new CK_MECHANISM(mechanism, publicValue), privateKey.keyID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            CK_ATTRIBUTE[] lenAttributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                new CK_ATTRIBUTE(CKA_VALUE_LEN),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            token.p11.C_GetAttributeValue(session.id(), keyID, lenAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            int keyLen = (int)lenAttributes[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            SecretKey key = P11Key.secretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        (session, keyID, algorithm, keyLen << 3, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if ("RAW".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                // Workaround for Solaris bug 6318543.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                // Strip leading zeroes ourselves if possible (key not sensitive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                // This should be removed once the Solaris fix is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                // as here we always retrieve the CKA_VALUE even for tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // that do not have that bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                byte[] keyBytes = key.getEncoded();
17916
e02ddef88f77 8014618: Need to strip leading zeros in TlsPremasterSecret of DHKeyAgreement
xuelei
parents: 16080
diff changeset
   333
                byte[] newBytes = KeyUtil.trimZeroes(keyBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if (keyBytes != newBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    key = new SecretKeySpec(newBytes, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            throw new InvalidKeyException("Could not derive key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            publicValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}