1 /* |
1 /* |
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
27 |
27 |
28 import java.io.IOException; |
28 import java.io.IOException; |
29 import java.math.BigInteger; |
29 import java.math.BigInteger; |
30 |
30 |
31 import java.security.*; |
31 import java.security.*; |
|
32 import java.security.spec.AlgorithmParameterSpec; |
32 import java.security.interfaces.*; |
33 import java.security.interfaces.*; |
33 |
34 |
34 import sun.security.util.*; |
35 import sun.security.util.*; |
|
36 import sun.security.x509.AlgorithmId; |
35 import sun.security.pkcs.PKCS8Key; |
37 import sun.security.pkcs.PKCS8Key; |
36 |
38 |
37 /** |
39 /** |
38 * Key implementation for RSA private keys, non-CRT form (modulus, private |
40 * RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in non-CRT |
39 * exponent only). For CRT private keys, see RSAPrivateCrtKeyImpl. We need |
41 * form (modulus, private exponent only). For CRT private keys, see |
40 * separate classes to ensure correct behavior in instanceof checks, etc. |
42 * RSAPrivateCrtKeyImpl. We need separate classes to ensure correct behavior |
|
43 * in instanceof checks, etc. |
41 * |
44 * |
42 * Note: RSA keys must be at least 512 bits long |
45 * Note: RSA keys must be at least 512 bits long |
43 * |
46 * |
44 * @see RSAPrivateCrtKeyImpl |
47 * @see RSAPrivateCrtKeyImpl |
45 * @see RSAKeyFactory |
48 * @see RSAKeyFactory |
52 private static final long serialVersionUID = -33106691987952810L; |
55 private static final long serialVersionUID = -33106691987952810L; |
53 |
56 |
54 private final BigInteger n; // modulus |
57 private final BigInteger n; // modulus |
55 private final BigInteger d; // private exponent |
58 private final BigInteger d; // private exponent |
56 |
59 |
|
60 // optional parameters associated with this RSA key |
|
61 // specified in the encoding of its AlgorithmId. |
|
62 // must be null for "RSA" keys. |
|
63 private final AlgorithmParameterSpec keyParams; |
|
64 |
57 /** |
65 /** |
58 * Construct a key from its components. Used by the |
66 * Construct a key from its components. Used by the |
59 * RSAKeyFactory and the RSAKeyPairGenerator. |
67 * RSAKeyFactory and the RSAKeyPairGenerator. |
60 */ |
68 */ |
61 RSAPrivateKeyImpl(BigInteger n, BigInteger d) throws InvalidKeyException { |
69 RSAPrivateKeyImpl(AlgorithmId rsaId, BigInteger n, BigInteger d) |
|
70 throws InvalidKeyException { |
|
71 RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), null); |
|
72 |
62 this.n = n; |
73 this.n = n; |
63 this.d = d; |
74 this.d = d; |
64 RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), null); |
75 this.keyParams = RSAUtil.getParamSpec(rsaId); |
|
76 |
65 // generate the encoding |
77 // generate the encoding |
66 algid = RSAPrivateCrtKeyImpl.rsaId; |
78 algid = rsaId; |
67 try { |
79 try { |
68 DerOutputStream out = new DerOutputStream(); |
80 DerOutputStream out = new DerOutputStream(); |
69 out.putInteger(0); // version must be 0 |
81 out.putInteger(0); // version must be 0 |
70 out.putInteger(n); |
82 out.putInteger(n); |
71 out.putInteger(0); |
83 out.putInteger(0); |
83 throw new InvalidKeyException(exc); |
95 throw new InvalidKeyException(exc); |
84 } |
96 } |
85 } |
97 } |
86 |
98 |
87 // see JCA doc |
99 // see JCA doc |
|
100 @Override |
88 public String getAlgorithm() { |
101 public String getAlgorithm() { |
89 return "RSA"; |
102 return algid.getName(); |
90 } |
103 } |
91 |
104 |
92 // see JCA doc |
105 // see JCA doc |
|
106 @Override |
93 public BigInteger getModulus() { |
107 public BigInteger getModulus() { |
94 return n; |
108 return n; |
95 } |
109 } |
96 |
110 |
97 // see JCA doc |
111 // see JCA doc |
|
112 @Override |
98 public BigInteger getPrivateExponent() { |
113 public BigInteger getPrivateExponent() { |
99 return d; |
114 return d; |
100 } |
115 } |
|
116 |
|
117 // see JCA doc |
|
118 @Override |
|
119 public AlgorithmParameterSpec getParams() { |
|
120 return keyParams; |
|
121 } |
|
122 |
|
123 // return a string representation of this key for debugging |
|
124 @Override |
|
125 public String toString() { |
|
126 return "Sun " + getAlgorithm() + " private key, " + n.bitLength() |
|
127 + " bits" + "\n params: " + keyParams + "\n modulus: " + n |
|
128 + "\n private exponent: " + d; |
|
129 } |
101 } |
130 } |