author | darcy |
Wed, 09 Oct 2019 09:57:41 -0700 | |
changeset 58519 | 6e017b301287 |
parent 57950 | 4612a3cfb927 |
child 58679 | 9c3209ff7550 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
50204
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, 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.rsa; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.math.BigInteger; |
|
30 |
||
31 |
import java.security.*; |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
32 |
import java.security.spec.AlgorithmParameterSpec; |
2 | 33 |
import java.security.interfaces.*; |
34 |
||
35 |
import sun.security.util.*; |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
36 |
import sun.security.x509.AlgorithmId; |
2 | 37 |
import sun.security.pkcs.PKCS8Key; |
38 |
||
39 |
/** |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
40 |
* RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in non-CRT |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
41 |
* form (modulus, private exponent only). For CRT private keys, see |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
42 |
* RSAPrivateCrtKeyImpl. We need separate classes to ensure correct behavior |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
43 |
* in instanceof checks, etc. |
2 | 44 |
* |
45 |
* Note: RSA keys must be at least 512 bits long |
|
46 |
* |
|
47 |
* @see RSAPrivateCrtKeyImpl |
|
48 |
* @see RSAKeyFactory |
|
49 |
* |
|
50 |
* @since 1.5 |
|
51 |
* @author Andreas Sterbenz |
|
52 |
*/ |
|
53 |
public final class RSAPrivateKeyImpl extends PKCS8Key implements RSAPrivateKey { |
|
54 |
||
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
50204
diff
changeset
|
55 |
@java.io.Serial |
2 | 56 |
private static final long serialVersionUID = -33106691987952810L; |
57 |
||
58 |
private final BigInteger n; // modulus |
|
59 |
private final BigInteger d; // private exponent |
|
60 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
61 |
// optional parameters associated with this RSA key |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
62 |
// specified in the encoding of its AlgorithmId. |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
63 |
// must be null for "RSA" keys. |
58519
6e017b301287
8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents:
57950
diff
changeset
|
64 |
@SuppressWarnings("serial") // Not statically typed as Serializable |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
65 |
private final AlgorithmParameterSpec keyParams; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
66 |
|
2 | 67 |
/** |
68 |
* Construct a key from its components. Used by the |
|
69 |
* RSAKeyFactory and the RSAKeyPairGenerator. |
|
70 |
*/ |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
71 |
RSAPrivateKeyImpl(AlgorithmId rsaId, BigInteger n, BigInteger d) |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
72 |
throws InvalidKeyException { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
73 |
RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), null); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
74 |
|
2 | 75 |
this.n = n; |
76 |
this.d = d; |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
77 |
this.keyParams = RSAUtil.getParamSpec(rsaId); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
78 |
|
2 | 79 |
// generate the encoding |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
80 |
algid = rsaId; |
2 | 81 |
try { |
82 |
DerOutputStream out = new DerOutputStream(); |
|
83 |
out.putInteger(0); // version must be 0 |
|
84 |
out.putInteger(n); |
|
85 |
out.putInteger(0); |
|
86 |
out.putInteger(d); |
|
87 |
out.putInteger(0); |
|
88 |
out.putInteger(0); |
|
89 |
out.putInteger(0); |
|
90 |
out.putInteger(0); |
|
91 |
out.putInteger(0); |
|
92 |
DerValue val = |
|
93 |
new DerValue(DerValue.tag_Sequence, out.toByteArray()); |
|
94 |
key = val.toByteArray(); |
|
95 |
} catch (IOException exc) { |
|
96 |
// should never occur |
|
97 |
throw new InvalidKeyException(exc); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
// see JCA doc |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
102 |
@Override |
2 | 103 |
public String getAlgorithm() { |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
104 |
return algid.getName(); |
2 | 105 |
} |
106 |
||
107 |
// see JCA doc |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
108 |
@Override |
2 | 109 |
public BigInteger getModulus() { |
110 |
return n; |
|
111 |
} |
|
112 |
||
113 |
// see JCA doc |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
114 |
@Override |
2 | 115 |
public BigInteger getPrivateExponent() { |
116 |
return d; |
|
117 |
} |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
118 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
119 |
// see JCA doc |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
120 |
@Override |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
121 |
public AlgorithmParameterSpec getParams() { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
122 |
return keyParams; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
123 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
124 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
125 |
// return a string representation of this key for debugging |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
126 |
@Override |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
127 |
public String toString() { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
128 |
return "Sun " + getAlgorithm() + " private key, " + n.bitLength() |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
129 |
+ " bits" + "\n params: " + keyParams + "\n modulus: " + n |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
130 |
+ "\n private exponent: " + d; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
131 |
} |
2 | 132 |
} |