author | lana |
Thu, 25 Jan 2018 20:56:49 +0000 | |
changeset 48708 | 46a2e41ebe59 |
parent 47421 | f9e03aef3a49 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.mscapi; |
|
27 |
||
28 |
import java.util.UUID; |
|
29 |
import java.security.*; |
|
30 |
import java.security.spec.AlgorithmParameterSpec; |
|
31 |
import java.security.spec.RSAKeyGenParameterSpec; |
|
32 |
||
2596 | 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 | 35 |
|
36 |
/** |
|
37 |
* RSA keypair generator. |
|
38 |
* |
|
39 |
* Standard algorithm, minimum key length is 512 bit, maximum is 16,384. |
|
40 |
* Generates a private key that is exportable. |
|
41 |
* |
|
42 |
* @since 1.6 |
|
43 |
*/ |
|
44 |
public final class RSAKeyPairGenerator extends KeyPairGeneratorSpi { |
|
45 |
||
46 |
// Supported by Microsoft Base, Strong and Enhanced Cryptographic Providers |
|
2596 | 47 |
static final int KEY_SIZE_MIN = 512; // disallow MSCAPI min. of 384 |
48 |
static final int KEY_SIZE_MAX = 16384; |
|
2 | 49 |
|
50 |
// size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX |
|
51 |
private int keySize; |
|
52 |
||
53 |
public RSAKeyPairGenerator() { |
|
54 |
// initialize to default in case the app does not call initialize() |
|
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
55 |
initialize(DEF_RSA_KEY_SIZE, null); |
2 | 56 |
} |
57 |
||
58 |
// initialize the generator. See JCA doc |
|
59 |
// random is always ignored |
|
60 |
public void initialize(int keySize, SecureRandom random) { |
|
61 |
||
2596 | 62 |
try { |
63 |
RSAKeyFactory.checkKeyLengths(keySize, null, |
|
64 |
KEY_SIZE_MIN, KEY_SIZE_MAX); |
|
65 |
} catch (InvalidKeyException e) { |
|
66 |
throw new InvalidParameterException(e.getMessage()); |
|
67 |
} |
|
68 |
||
69 |
this.keySize = keySize; |
|
2 | 70 |
} |
71 |
||
72 |
// second initialize method. See JCA doc |
|
73 |
// random and exponent are always ignored |
|
74 |
public void initialize(AlgorithmParameterSpec params, SecureRandom random) |
|
75 |
throws InvalidAlgorithmParameterException { |
|
76 |
||
2596 | 77 |
int tmpSize; |
2 | 78 |
if (params == null) { |
47421
f9e03aef3a49
8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents:
47216
diff
changeset
|
79 |
tmpSize = DEF_RSA_KEY_SIZE; |
2 | 80 |
} else if (params instanceof RSAKeyGenParameterSpec) { |
81 |
||
82 |
if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) { |
|
83 |
throw new InvalidAlgorithmParameterException |
|
84 |
("Exponent parameter is not supported"); |
|
85 |
} |
|
2596 | 86 |
tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize(); |
2 | 87 |
|
88 |
} else { |
|
89 |
throw new InvalidAlgorithmParameterException |
|
90 |
("Params must be an instance of RSAKeyGenParameterSpec"); |
|
91 |
} |
|
2596 | 92 |
|
93 |
try { |
|
94 |
RSAKeyFactory.checkKeyLengths(tmpSize, null, |
|
95 |
KEY_SIZE_MIN, KEY_SIZE_MAX); |
|
96 |
} catch (InvalidKeyException e) { |
|
97 |
throw new InvalidAlgorithmParameterException( |
|
98 |
"Invalid Key sizes", e); |
|
99 |
} |
|
100 |
||
101 |
this.keySize = tmpSize; |
|
2 | 102 |
} |
103 |
||
104 |
// generate the keypair. See JCA doc |
|
105 |
public KeyPair generateKeyPair() { |
|
106 |
||
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
107 |
try { |
2 | 108 |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
109 |
// Generate each keypair in a unique key container |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
110 |
RSAKeyPair keys = |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
111 |
generateRSAKeyPair(keySize, |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
112 |
"{" + UUID.randomUUID().toString() + "}"); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
113 |
|
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
114 |
return new KeyPair(keys.getPublic(), keys.getPrivate()); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
115 |
|
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
116 |
} catch (KeyException e) { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
117 |
throw new ProviderException(e); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
118 |
} |
2 | 119 |
} |
120 |
||
121 |
private static native RSAKeyPair generateRSAKeyPair(int keySize, |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
122 |
String keyContainerName) throws KeyException; |
2 | 123 |
} |