author | sundar |
Mon, 31 Aug 2015 17:51:02 +0530 | |
changeset 32434 | 769b3d81ae69 |
parent 25859 | 3317bb8137f4 |
child 40389 | c6df8bba0b71 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9505
3bbc6ff8a905
6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2011, 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.math.BigInteger; |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
29 |
import java.security.KeyException; |
2 | 30 |
import java.security.KeyRep; |
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
31 |
import java.security.ProviderException; |
2 | 32 |
|
33 |
import sun.security.rsa.RSAPublicKeyImpl; |
|
34 |
||
35 |
/** |
|
36 |
* The handle for an RSA public key using the Microsoft Crypto API. |
|
37 |
* |
|
38 |
* @since 1.6 |
|
39 |
*/ |
|
40 |
class RSAPublicKey extends Key implements java.security.interfaces.RSAPublicKey |
|
41 |
{ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
42 |
private static final long serialVersionUID = -2289561342425825391L; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
43 |
|
2 | 44 |
private byte[] publicKeyBlob = null; |
45 |
private byte[] encoding = null; |
|
46 |
private BigInteger modulus = null; |
|
47 |
private BigInteger exponent = null; |
|
48 |
||
49 |
/** |
|
50 |
* Construct an RSAPublicKey object. |
|
51 |
*/ |
|
52 |
RSAPublicKey(long hCryptProv, long hCryptKey, int keyLength) |
|
53 |
{ |
|
54 |
super(hCryptProv, hCryptKey, keyLength); |
|
55 |
} |
|
56 |
||
57 |
/** |
|
58 |
* Returns the standard algorithm name for this key. For |
|
59 |
* example, "RSA" would indicate that this key is a RSA key. |
|
60 |
* See Appendix A in the <a href= |
|
61 |
* "../../../guide/security/CryptoSpec.html#AppA"> |
|
62 |
* Java Cryptography Architecture API Specification & Reference </a> |
|
63 |
* for information about standard algorithm names. |
|
64 |
* |
|
65 |
* @return the name of the algorithm associated with this key. |
|
66 |
*/ |
|
67 |
public String getAlgorithm() |
|
68 |
{ |
|
69 |
return "RSA"; |
|
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* Returns a printable description of the key. |
|
74 |
*/ |
|
75 |
public String toString() |
|
76 |
{ |
|
77 |
StringBuffer sb = new StringBuffer(); |
|
78 |
||
79 |
sb.append("RSAPublicKey [size=").append(keyLength) |
|
80 |
.append(" bits, type=").append(getKeyType(hCryptKey)) |
|
81 |
.append(", container=").append(getContainerName(hCryptProv)) |
|
82 |
.append("]\n modulus: ").append(getModulus()) |
|
83 |
.append("\n public exponent: ").append(getPublicExponent()); |
|
84 |
||
85 |
return sb.toString(); |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Returns the public exponent. |
|
90 |
*/ |
|
91 |
public BigInteger getPublicExponent() { |
|
92 |
||
93 |
if (exponent == null) { |
|
94 |
||
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
95 |
try { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
96 |
publicKeyBlob = getPublicKeyBlob(hCryptKey); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
97 |
exponent = new BigInteger(1, getExponent(publicKeyBlob)); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
98 |
|
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
99 |
} catch (KeyException e) { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
100 |
throw new ProviderException(e); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
101 |
} |
2 | 102 |
} |
103 |
||
104 |
return exponent; |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Returns the modulus. |
|
109 |
*/ |
|
110 |
public BigInteger getModulus() { |
|
111 |
||
112 |
if (modulus == null) { |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
113 |
|
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
114 |
try { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
115 |
publicKeyBlob = getPublicKeyBlob(hCryptKey); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
116 |
modulus = new BigInteger(1, getModulus(publicKeyBlob)); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
117 |
|
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
118 |
} catch (KeyException e) { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
119 |
throw new ProviderException(e); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
120 |
} |
2 | 121 |
} |
122 |
||
123 |
return modulus; |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Returns the name of the primary encoding format of this key, |
|
128 |
* or null if this key does not support encoding. |
|
129 |
* The primary encoding format is |
|
130 |
* named in terms of the appropriate ASN.1 data format, if an |
|
131 |
* ASN.1 specification for this key exists. |
|
132 |
* For example, the name of the ASN.1 data format for public |
|
133 |
* keys is <I>SubjectPublicKeyInfo</I>, as |
|
134 |
* defined by the X.509 standard; in this case, the returned format is |
|
135 |
* <code>"X.509"</code>. Similarly, |
|
136 |
* the name of the ASN.1 data format for private keys is |
|
137 |
* <I>PrivateKeyInfo</I>, |
|
138 |
* as defined by the PKCS #8 standard; in this case, the returned format is |
|
139 |
* <code>"PKCS#8"</code>. |
|
140 |
* |
|
141 |
* @return the primary encoding format of the key. |
|
142 |
*/ |
|
143 |
public String getFormat() |
|
144 |
{ |
|
145 |
return "X.509"; |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Returns the key in its primary encoding format, or null |
|
150 |
* if this key does not support encoding. |
|
151 |
* |
|
152 |
* @return the encoded key, or null if the key does not support |
|
153 |
* encoding. |
|
154 |
*/ |
|
155 |
public byte[] getEncoded() |
|
156 |
{ |
|
157 |
if (encoding == null) { |
|
158 |
||
159 |
try { |
|
160 |
encoding = new RSAPublicKeyImpl(getModulus(), |
|
161 |
getPublicExponent()).getEncoded(); |
|
162 |
||
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
163 |
} catch (KeyException e) { |
2 | 164 |
// ignore |
165 |
} |
|
166 |
} |
|
167 |
return encoding; |
|
168 |
} |
|
169 |
||
170 |
protected Object writeReplace() throws java.io.ObjectStreamException { |
|
171 |
return new KeyRep(KeyRep.Type.PUBLIC, |
|
172 |
getAlgorithm(), |
|
173 |
getFormat(), |
|
174 |
getEncoded()); |
|
175 |
} |
|
176 |
||
177 |
/* |
|
178 |
* Returns the Microsoft CryptoAPI representation of the key. |
|
179 |
*/ |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
180 |
private native byte[] getPublicKeyBlob(long hCryptKey) throws KeyException; |
2 | 181 |
|
182 |
/* |
|
183 |
* Returns the key's public exponent (in big-endian 2's complement format). |
|
184 |
*/ |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
185 |
private native byte[] getExponent(byte[] keyBlob) throws KeyException; |
2 | 186 |
|
187 |
/* |
|
188 |
* Returns the key's modulus (in big-endian 2's complement format). |
|
189 |
*/ |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
190 |
private native byte[] getModulus(byte[] keyBlob) throws KeyException; |
2 | 191 |
} |