src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/CKeyPairGenerator.java
author herrick
Mon, 30 Sep 2019 19:33:13 -0400
branchJDK-8200758-branch
changeset 58417 67ffaf3a2b75
parent 53006 4debb3321e65
permissions -rw-r--r--
8231280: Linux packages produced by jpackage should have correct dependencies Submitted-by: asemenyuk Reviewed-by: herrick
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
     2
 * Copyright (c) 2005, 2018, 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: 2596
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: 2596
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: 2596
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
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.mscapi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.UUID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.RSAKeyGenParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    33
import sun.security.rsa.RSAKeyFactory;
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47216
diff changeset
    34
import static sun.security.util.SecurityProviderConstants.DEF_RSA_KEY_SIZE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * RSA keypair generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Standard algorithm, minimum key length is 512 bit, maximum is 16,384.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Generates a private key that is exportable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    44
public abstract class CKeyPairGenerator extends KeyPairGeneratorSpi {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    46
    protected String keyAlg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    48
    public CKeyPairGenerator(String keyAlg) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    49
        this.keyAlg = keyAlg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    52
    public static class RSA extends CKeyPairGenerator {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    53
        public RSA() {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    54
            super("RSA");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    55
            // initialize to default in case the app does not call initialize()
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    56
            initialize(DEF_RSA_KEY_SIZE, null);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    57
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    59
        // Supported by Microsoft Base, Strong and Enhanced Cryptographic Providers
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    60
        static final int KEY_SIZE_MIN = 512; // disallow MSCAPI min. of 384
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    61
        static final int KEY_SIZE_MAX = 16384;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    62
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    63
        // size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    64
        private int keySize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    66
        // initialize the generator. See JCA doc
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    67
        // random is always ignored
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    68
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    69
        public void initialize(int keySize, SecureRandom random) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    70
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    71
            try {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    72
                RSAKeyFactory.checkKeyLengths(keySize, null,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    73
                        KEY_SIZE_MIN, KEY_SIZE_MAX);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    74
            } catch (InvalidKeyException e) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    75
                throw new InvalidParameterException(e.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    78
            this.keySize = keySize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    80
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    81
        // second initialize method. See JCA doc
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    82
        // random and exponent are always ignored
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    83
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    84
        public void initialize(AlgorithmParameterSpec params, SecureRandom random)
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    85
                throws InvalidAlgorithmParameterException {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    86
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    87
            int tmpSize;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    88
            if (params == null) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    89
                tmpSize = DEF_RSA_KEY_SIZE;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    90
            } else if (params instanceof RSAKeyGenParameterSpec) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    91
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    92
                if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    93
                    throw new InvalidAlgorithmParameterException
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    94
                            ("Exponent parameter is not supported");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    95
                }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    96
                tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    97
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    98
            } else {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
    99
                throw new InvalidAlgorithmParameterException
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   100
                        ("Params must be an instance of RSAKeyGenParameterSpec");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   101
            }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   102
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   103
            try {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   104
                RSAKeyFactory.checkKeyLengths(tmpSize, null,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   105
                        KEY_SIZE_MIN, KEY_SIZE_MAX);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   106
            } catch (InvalidKeyException e) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   107
                throw new InvalidAlgorithmParameterException(
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   108
                        "Invalid Key sizes", e);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   109
            }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   110
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   111
            this.keySize = tmpSize;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   112
        }
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   113
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   114
        // generate the keypair. See JCA doc
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   115
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   116
        public KeyPair generateKeyPair() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   118
            try {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   119
                // Generate each keypair in a unique key container
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   120
                CKeyPair keys =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   121
                        generateCKeyPair(keyAlg, keySize,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   122
                                "{" + UUID.randomUUID().toString() + "}");
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   123
                return new KeyPair(keys.getPublic(), keys.getPrivate());
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   124
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   125
            } catch (KeyException e) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   126
                throw new ProviderException(e);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   127
            }
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   128
        }
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   129
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   130
        private static native CKeyPair generateCKeyPair(String alg, int keySize,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47421
diff changeset
   131
                String keyContainerName) throws KeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}