author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 44260 | jdk/src/java.base/share/classes/sun/security/rsa/RSAPublicKeyImpl.java@dd947f766e11 |
child 48567 | c4de888db380 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
44260
dd947f766e11
8175251: Failed to load RSA private key from pkcs12
valeriep
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2003, 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.rsa; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.math.BigInteger; |
|
30 |
||
31 |
import java.security.*; |
|
32 |
import java.security.interfaces.*; |
|
33 |
||
34 |
import sun.security.util.*; |
|
35 |
import sun.security.x509.X509Key; |
|
36 |
||
37 |
/** |
|
38 |
* Key implementation for RSA public keys. |
|
39 |
* |
|
40 |
* Note: RSA keys must be at least 512 bits long |
|
41 |
* |
|
42 |
* @see RSAPrivateCrtKeyImpl |
|
43 |
* @see RSAKeyFactory |
|
44 |
* |
|
45 |
* @since 1.5 |
|
46 |
* @author Andreas Sterbenz |
|
47 |
*/ |
|
48 |
public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey { |
|
49 |
||
50 |
private static final long serialVersionUID = 2644735423591199609L; |
|
51 |
||
52 |
private BigInteger n; // modulus |
|
53 |
private BigInteger e; // public exponent |
|
54 |
||
55 |
/** |
|
56 |
* Construct a key from its components. Used by the |
|
57 |
* RSAKeyFactory and the RSAKeyPairGenerator. |
|
58 |
*/ |
|
2596 | 59 |
public RSAPublicKeyImpl(BigInteger n, BigInteger e) |
60 |
throws InvalidKeyException { |
|
2 | 61 |
this.n = n; |
62 |
this.e = e; |
|
2596 | 63 |
RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e); |
2 | 64 |
// generate the encoding |
65 |
algid = RSAPrivateCrtKeyImpl.rsaId; |
|
66 |
try { |
|
67 |
DerOutputStream out = new DerOutputStream(); |
|
68 |
out.putInteger(n); |
|
69 |
out.putInteger(e); |
|
19375
cdfdb9c0590e
8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents:
5506
diff
changeset
|
70 |
byte[] keyArray = |
cdfdb9c0590e
8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents:
5506
diff
changeset
|
71 |
new DerValue(DerValue.tag_Sequence, |
cdfdb9c0590e
8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents:
5506
diff
changeset
|
72 |
out.toByteArray()).toByteArray(); |
cdfdb9c0590e
8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents:
5506
diff
changeset
|
73 |
setKey(new BitArray(keyArray.length*8, keyArray)); |
2 | 74 |
} catch (IOException exc) { |
75 |
// should never occur |
|
76 |
throw new InvalidKeyException(exc); |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Construct a key from its encoding. Used by RSAKeyFactory. |
|
82 |
*/ |
|
83 |
public RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException { |
|
84 |
decode(encoded); |
|
2596 | 85 |
RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e); |
2 | 86 |
} |
87 |
||
88 |
// see JCA doc |
|
89 |
public String getAlgorithm() { |
|
90 |
return "RSA"; |
|
91 |
} |
|
92 |
||
93 |
// see JCA doc |
|
94 |
public BigInteger getModulus() { |
|
95 |
return n; |
|
96 |
} |
|
97 |
||
98 |
// see JCA doc |
|
99 |
public BigInteger getPublicExponent() { |
|
100 |
return e; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Parse the key. Called by X509Key. |
|
105 |
*/ |
|
106 |
protected void parseKeyBits() throws InvalidKeyException { |
|
107 |
try { |
|
19375
cdfdb9c0590e
8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents:
5506
diff
changeset
|
108 |
DerInputStream in = new DerInputStream(getKey().toByteArray()); |
2 | 109 |
DerValue derValue = in.getDerValue(); |
110 |
if (derValue.tag != DerValue.tag_Sequence) { |
|
111 |
throw new IOException("Not a SEQUENCE"); |
|
112 |
} |
|
113 |
DerInputStream data = derValue.data; |
|
44260
dd947f766e11
8175251: Failed to load RSA private key from pkcs12
valeriep
parents:
25859
diff
changeset
|
114 |
n = data.getPositiveBigInteger(); |
dd947f766e11
8175251: Failed to load RSA private key from pkcs12
valeriep
parents:
25859
diff
changeset
|
115 |
e = data.getPositiveBigInteger(); |
2 | 116 |
if (derValue.data.available() != 0) { |
117 |
throw new IOException("Extra data available"); |
|
118 |
} |
|
119 |
} catch (IOException e) { |
|
120 |
throw new InvalidKeyException("Invalid RSA public key", e); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
// return a string representation of this key for debugging |
|
125 |
public String toString() { |
|
126 |
return "Sun RSA public key, " + n.bitLength() + " bits\n modulus: " |
|
127 |
+ n + "\n public exponent: " + e; |
|
128 |
} |
|
129 |
||
130 |
protected Object writeReplace() throws java.io.ObjectStreamException { |
|
131 |
return new KeyRep(KeyRep.Type.PUBLIC, |
|
132 |
getAlgorithm(), |
|
133 |
getFormat(), |
|
134 |
getEncoded()); |
|
135 |
} |
|
136 |
} |