jdk/src/share/classes/sun/security/pkcs11/P11KeyAgreement.java
author valeriep
Tue, 20 Mar 2012 15:06:13 -0700
changeset 12201 d77ed23f4992
parent 10336 0bb1999251f8
child 16080 0e6266b88242
permissions -rw-r--r--
7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11 Summary: Always return the secret in the same length as the modulus. Reviewed-by: wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
     2
 * Copyright (c) 2003, 2012, 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);
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   201
            // 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
   202
            // 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
   203
            // 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
   204
            // 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
   205
            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
   206
                return secret;
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   207
            } else {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   208
                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
   209
                    // 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
   210
                    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
   211
                }
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   212
                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
   213
                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
   214
                    secret.length);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   215
                return newSecret;
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 10336
diff changeset
   216
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new ProviderException("Could not derive key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            publicValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    protected int engineGenerateSecret(byte[] sharedSecret, int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            offset) throws IllegalStateException, ShortBufferException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (multiPartyAgreement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            int n = multiPartyAgreement.generateSecret(sharedSecret, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (offset + secretLen > sharedSecret.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new ShortBufferException("Need " + secretLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                + " bytes, only " + (sharedSecret.length - offset) + " available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        byte[] secret = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        System.arraycopy(secret, 0, sharedSecret, offset, secret.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    protected SecretKey engineGenerateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (multiPartyAgreement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            SecretKey key = multiPartyAgreement.generateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            multiPartyAgreement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (algorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new NoSuchAlgorithmException("Algorithm must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (algorithm.equals("TlsPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // For now, only perform native derivation for TlsPremasterSecret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // as that is required for FIPS compliance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // For other algorithms, there are unresolved issues regarding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            // how this should work in JCE plus a Solaris truncation bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // (bug not yet filed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return nativeGenerateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        byte[] secret = engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // Maintain compatibility for SunJCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // verify secret length is sensible for algorithm / truncate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // return generated key itself if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        int keyLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (algorithm.equalsIgnoreCase("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            keyLen = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else if (algorithm.equalsIgnoreCase("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            keyLen = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else if (algorithm.equalsIgnoreCase("Blowfish")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            keyLen = Math.min(56, secret.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } else if (algorithm.equalsIgnoreCase("TlsPremasterSecret")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            keyLen = secret.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                ("Unknown algorithm " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (secret.length < keyLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw new InvalidKeyException("Secret too short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (algorithm.equalsIgnoreCase("DES") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            algorithm.equalsIgnoreCase("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                for (int i = 0; i < keyLen; i+=8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    P11SecretKeyFactory.fixDESParity(secret, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return new SecretKeySpec(secret, 0, keyLen, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private SecretKey nativeGenerateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if ((privateKey == null) || (publicValue == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new IllegalStateException("Not initialized correctly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        long keyType = CKK_GENERIC_SECRET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                new CK_ATTRIBUTE(CKA_KEY_TYPE, keyType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                (O_GENERATE, CKO_SECRET_KEY, keyType, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            long keyID = token.p11.C_DeriveKey(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                new CK_MECHANISM(mechanism, publicValue), privateKey.keyID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            CK_ATTRIBUTE[] lenAttributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                new CK_ATTRIBUTE(CKA_VALUE_LEN),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            token.p11.C_GetAttributeValue(session.id(), keyID, lenAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            int keyLen = (int)lenAttributes[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            SecretKey key = P11Key.secretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        (session, keyID, algorithm, keyLen << 3, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if ("RAW".equals(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                // Workaround for Solaris bug 6318543.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                // Strip leading zeroes ourselves if possible (key not sensitive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                // This should be removed once the Solaris fix is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                // as here we always retrieve the CKA_VALUE even for tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // that do not have that bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                byte[] keyBytes = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                byte[] newBytes = P11Util.trimZeroes(keyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (keyBytes != newBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    key = new SecretKeySpec(newBytes, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw new InvalidKeyException("Could not derive key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            publicValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
}