author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 53006 | 4debb3321e65 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53006 | 2 |
* Copyright (c) 2005, 2018, 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 |
*/ |
|
53006 | 44 |
public abstract class CKeyPairGenerator extends KeyPairGeneratorSpi { |
2 | 45 |
|
53006 | 46 |
protected String keyAlg; |
2 | 47 |
|
53006 | 48 |
public CKeyPairGenerator(String keyAlg) { |
49 |
this.keyAlg = keyAlg; |
|
2 | 50 |
} |
51 |
||
53006 | 52 |
public static class RSA extends CKeyPairGenerator { |
53 |
public RSA() { |
|
54 |
super("RSA"); |
|
55 |
// initialize to default in case the app does not call initialize() |
|
56 |
initialize(DEF_RSA_KEY_SIZE, null); |
|
57 |
} |
|
2 | 58 |
|
53006 | 59 |
// Supported by Microsoft Base, Strong and Enhanced Cryptographic Providers |
60 |
static final int KEY_SIZE_MIN = 512; // disallow MSCAPI min. of 384 |
|
61 |
static final int KEY_SIZE_MAX = 16384; |
|
62 |
||
63 |
// size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX |
|
64 |
private int keySize; |
|
2 | 65 |
|
53006 | 66 |
// initialize the generator. See JCA doc |
67 |
// random is always ignored |
|
68 |
@Override |
|
69 |
public void initialize(int keySize, SecureRandom random) { |
|
70 |
||
71 |
try { |
|
72 |
RSAKeyFactory.checkKeyLengths(keySize, null, |
|
73 |
KEY_SIZE_MIN, KEY_SIZE_MAX); |
|
74 |
} catch (InvalidKeyException e) { |
|
75 |
throw new InvalidParameterException(e.getMessage()); |
|
2 | 76 |
} |
77 |
||
53006 | 78 |
this.keySize = keySize; |
2 | 79 |
} |
2596 | 80 |
|
53006 | 81 |
// second initialize method. See JCA doc |
82 |
// random and exponent are always ignored |
|
83 |
@Override |
|
84 |
public void initialize(AlgorithmParameterSpec params, SecureRandom random) |
|
85 |
throws InvalidAlgorithmParameterException { |
|
86 |
||
87 |
int tmpSize; |
|
88 |
if (params == null) { |
|
89 |
tmpSize = DEF_RSA_KEY_SIZE; |
|
90 |
} else if (params instanceof RSAKeyGenParameterSpec) { |
|
91 |
||
92 |
if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) { |
|
93 |
throw new InvalidAlgorithmParameterException |
|
94 |
("Exponent parameter is not supported"); |
|
95 |
} |
|
96 |
tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize(); |
|
97 |
||
98 |
} else { |
|
99 |
throw new InvalidAlgorithmParameterException |
|
100 |
("Params must be an instance of RSAKeyGenParameterSpec"); |
|
101 |
} |
|
102 |
||
103 |
try { |
|
104 |
RSAKeyFactory.checkKeyLengths(tmpSize, null, |
|
105 |
KEY_SIZE_MIN, KEY_SIZE_MAX); |
|
106 |
} catch (InvalidKeyException e) { |
|
107 |
throw new InvalidAlgorithmParameterException( |
|
108 |
"Invalid Key sizes", e); |
|
109 |
} |
|
110 |
||
111 |
this.keySize = tmpSize; |
|
2596 | 112 |
} |
113 |
||
53006 | 114 |
// generate the keypair. See JCA doc |
115 |
@Override |
|
116 |
public KeyPair generateKeyPair() { |
|
2 | 117 |
|
53006 | 118 |
try { |
119 |
// Generate each keypair in a unique key container |
|
120 |
CKeyPair keys = |
|
121 |
generateCKeyPair(keyAlg, keySize, |
|
122 |
"{" + UUID.randomUUID().toString() + "}"); |
|
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 | 125 |
} catch (KeyException e) { |
126 |
throw new ProviderException(e); |
|
127 |
} |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
5506
diff
changeset
|
128 |
} |
53006 | 129 |
|
130 |
private static native CKeyPair generateCKeyPair(String alg, int keySize, |
|
131 |
String keyContainerName) throws KeyException; |
|
2 | 132 |
} |
133 |
} |