src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyGenerator.java
author mbalao
Tue, 15 Jan 2019 19:24:07 -0300
changeset 53340 142b179dd60e
parent 47216 71c04702a3d5
child 53945 1f0b00fe27ed
permissions -rw-r--r--
8217088: Disable JDK-6913047 fix (SunPKCS11 memory leak) after JDK-8216597 (SIGBUS error in getNativeKeyInfo) Summary: Disable JDK-6913047 fix (SunPKCS11 memory leak) temporarily until JDK-8216597 (SIGBUS error in getNativeKeyInfo) is fixed. Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 290
diff changeset
     2
 * Copyright (c) 2003, 2008, 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: 290
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: 290
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: 290
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 290
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 290
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.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * KeyGenerator implementation class. This class currently supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * DES, DESede, AES, ARCFOUR, and Blowfish.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
final class P11KeyGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // raw key size in bits, e.g. 64 for DES. Always valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // bits of entropy in the key, e.g. 56 for DES. Always valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int significantKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // keyType (CKK_*), needed for TemplateManager call only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private long keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // for determining if both 112 and 168 bits of DESede key lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean supportBothKeySizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    68
    /**
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    69
     * Utility method for checking if the specified key size is valid
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    70
     * and within the supported range. Return the significant key size
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    71
     * upon successful validation.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    72
     * @param keyGenMech the PKCS#11 key generation mechanism.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    73
     * @param keySize the to-be-checked key size for this mechanism.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    74
     * @param token token which provides this mechanism.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    75
     * @return the significant key size (in bits) corresponding to the
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    76
     * specified key size.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    77
     * @throws InvalidParameterException if the specified key size is invalid.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    78
     * @throws ProviderException if this mechanism isn't supported by SunPKCS11
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    79
     * or underlying native impl.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    80
     */
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    81
    static int checkKeySize(long keyGenMech, int keySize, Token token)
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    82
        throws InvalidAlgorithmParameterException, ProviderException {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    83
        int sigKeySize;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    84
        switch ((int)keyGenMech) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    85
            case (int)CKM_DES_KEY_GEN:
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    86
                if ((keySize != 64) && (keySize != 56)) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    87
                    throw new InvalidAlgorithmParameterException
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    88
                            ("DES key length must be 56 bits");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    89
                }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    90
                sigKeySize = 56;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    91
                break;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    92
            case (int)CKM_DES2_KEY_GEN:
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    93
            case (int)CKM_DES3_KEY_GEN:
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    94
                if ((keySize == 112) || (keySize == 128)) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    95
                    sigKeySize = 112;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    96
                } else if ((keySize == 168) || (keySize == 192)) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    97
                    sigKeySize = 168;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    98
                } else {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
    99
                    throw new InvalidAlgorithmParameterException
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   100
                            ("DESede key length must be 112, or 168 bits");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   101
                }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   102
                break;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   103
            default:
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   104
                // Handle all variable-key-length algorithms here
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   105
                CK_MECHANISM_INFO info = null;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   106
                try {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   107
                    info = token.getMechanismInfo(keyGenMech);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   108
                } catch (PKCS11Exception p11e) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   109
                    // Should never happen
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   110
                    throw new ProviderException
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   111
                            ("Cannot retrieve mechanism info", p11e);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   112
                }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   113
                if (info == null) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   114
                    // XXX Unable to retrieve the supported key length from
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   115
                    // the underlying native impl. Skip the checking for now.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   116
                    return keySize;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   117
                }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   118
                // PKCS#11 defines these to be in number of bytes except for
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   119
                // RC4 which is in bits. However, some PKCS#11 impls still use
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   120
                // bytes for all mechs, e.g. NSS. We try to detect this
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   121
                // inconsistency if the minKeySize seems unreasonably small.
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   122
                int minKeySize = (int)info.ulMinKeySize;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   123
                int maxKeySize = (int)info.ulMaxKeySize;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   124
                if (keyGenMech != CKM_RC4_KEY_GEN || minKeySize < 8) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   125
                    minKeySize = (int)info.ulMinKeySize << 3;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   126
                    maxKeySize = (int)info.ulMaxKeySize << 3;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   127
                }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   128
                // Explicitly disallow keys shorter than 40-bits for security
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   129
                if (minKeySize < 40) minKeySize = 40;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   130
                if (keySize < minKeySize || keySize > maxKeySize) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   131
                    throw new InvalidAlgorithmParameterException
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   132
                            ("Key length must be between " + minKeySize +
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   133
                            " and " + maxKeySize + " bits");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   134
                }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   135
                if (keyGenMech == CKM_AES_KEY_GEN) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   136
                    if ((keySize != 128) && (keySize != 192) &&
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   137
                        (keySize != 256)) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   138
                        throw new InvalidAlgorithmParameterException
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   139
                                ("AES key length must be " + minKeySize +
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   140
                                (maxKeySize >= 192? ", 192":"") +
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   141
                                (maxKeySize >= 256? ", or 256":"") + " bits");
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   142
                    }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   143
                }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   144
                sigKeySize = keySize;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   145
        }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   146
        return sigKeySize;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   147
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    P11KeyGenerator(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (this.mechanism == CKM_DES3_KEY_GEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            /* Given the current lookup order specified in SunPKCS11.java,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
               if CKM_DES2_KEY_GEN is used to construct this object, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
               means that CKM_DES3_KEY_GEN is disabled or unsupported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            supportBothKeySizes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                (token.provider.config.isEnabled(CKM_DES2_KEY_GEN) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                 (token.getMechanismInfo(CKM_DES2_KEY_GEN) != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        setDefaultKeySize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // set default keysize and also initialize keyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private void setDefaultKeySize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        switch ((int)mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        case (int)CKM_DES_KEY_GEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            keySize = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            keyType = CKK_DES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        case (int)CKM_DES2_KEY_GEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            keySize = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            keyType = CKK_DES2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        case (int)CKM_DES3_KEY_GEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            keySize = 192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            keyType = CKK_DES3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        case (int)CKM_AES_KEY_GEN:
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   184
            keySize = 128;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            keyType = CKK_AES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        case (int)CKM_RC4_KEY_GEN:
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   188
            keySize = 128;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            keyType = CKK_RC4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        case (int)CKM_BLOWFISH_KEY_GEN:
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   192
            keySize = 128;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            keyType = CKK_BLOWFISH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new ProviderException("Unknown mechanism " + mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   198
        try {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   199
            significantKeySize = checkKeySize(mechanism, keySize, token);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   200
        } catch (InvalidAlgorithmParameterException iape) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   201
            throw new ProviderException("Unsupported default key size", iape);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        setDefaultKeySize();
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 void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                ("AlgorithmParameterSpec not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    protected void engineInit(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        token.ensureValid();
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   221
        int newSignificantKeySize;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   222
        try {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   223
            newSignificantKeySize = checkKeySize(mechanism, keySize, token);
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   224
        } catch (InvalidAlgorithmParameterException iape) {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   225
            throw (InvalidParameterException)
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   226
                    (new InvalidParameterException().initCause(iape));
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   227
        }
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   228
        if ((mechanism == CKM_DES2_KEY_GEN) ||
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   229
            (mechanism == CKM_DES3_KEY_GEN))  {
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   230
            long newMechanism = (newSignificantKeySize == 112 ?
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   231
                CKM_DES2_KEY_GEN : CKM_DES3_KEY_GEN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (mechanism != newMechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (supportBothKeySizes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    mechanism = newMechanism;
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   235
                    // Adjust keyType to reflect the mechanism change
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   236
                    keyType = (mechanism == CKM_DES2_KEY_GEN ?
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   237
                        CKK_DES2 : CKK_DES3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    throw new InvalidParameterException
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   240
                            ("Only " + significantKeySize +
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   241
                             "-bit DESede is supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
290
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   245
        this.keySize = keySize;
519d4185fbe2 6572331: regression: cipher.wrap operation fails with CKR_ATTRIBUTE_VALUE_INVALID
valeriep
parents: 2
diff changeset
   246
        this.significantKeySize = newSignificantKeySize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            session = token.getObjSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            CK_ATTRIBUTE[] attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            switch ((int)keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            case (int)CKK_DES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            case (int)CKK_DES2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            case (int)CKK_DES3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                // fixed length, do not specify CKA_VALUE_LEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                attributes = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    new CK_ATTRIBUTE(CKA_VALUE_LEN, keySize >> 3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            attributes = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                (O_GENERATE, CKO_SECRET_KEY, keyType, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            long keyID = token.p11.C_GenerateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                (session.id(), new CK_MECHANISM(mechanism), attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return P11Key.secretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                (session, keyID, algorithm, significantKeySize, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new ProviderException("Could not generate key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}