author | vinnie |
Thu, 21 Apr 2011 19:05:29 +0100 | |
changeset 9508 | 310b4f6c8e61 |
parent 9505 | 3bbc6ff8a905 |
child 9524 | 8417d0e74ac5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
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.nio.ByteBuffer; |
|
29 |
import java.security.PublicKey; |
|
30 |
import java.security.PrivateKey; |
|
31 |
import java.security.InvalidKeyException; |
|
32 |
import java.security.InvalidParameterException; |
|
33 |
import java.security.InvalidAlgorithmParameterException; |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
34 |
import java.security.KeyStoreException; |
2 | 35 |
import java.security.NoSuchAlgorithmException; |
36 |
import java.security.ProviderException; |
|
37 |
import java.security.MessageDigest; |
|
38 |
import java.security.SecureRandom; |
|
39 |
import java.security.Signature; |
|
40 |
import java.security.SignatureSpi; |
|
41 |
import java.security.SignatureException; |
|
2596 | 42 |
import java.math.BigInteger; |
43 |
||
44 |
import sun.security.rsa.RSAKeyFactory; |
|
2 | 45 |
|
46 |
/** |
|
47 |
* RSA signature implementation. Supports RSA signing using PKCS#1 v1.5 padding. |
|
48 |
* |
|
49 |
* Objects should be instantiated by calling Signature.getInstance() using the |
|
50 |
* following algorithm names: |
|
51 |
* |
|
52 |
* . "SHA1withRSA" |
|
53 |
* . "MD5withRSA" |
|
54 |
* . "MD2withRSA" |
|
55 |
* |
|
56 |
* Note: RSA keys must be at least 512 bits long |
|
57 |
* |
|
58 |
* @since 1.6 |
|
59 |
* @author Stanley Man-Kit Ho |
|
60 |
*/ |
|
61 |
abstract class RSASignature extends java.security.SignatureSpi |
|
62 |
{ |
|
63 |
// message digest implementation we use |
|
64 |
private final MessageDigest messageDigest; |
|
65 |
||
66 |
// flag indicating whether the digest is reset |
|
67 |
private boolean needsReset; |
|
68 |
||
69 |
// the signing key |
|
70 |
private Key privateKey = null; |
|
71 |
||
72 |
// the verification key |
|
73 |
private Key publicKey = null; |
|
74 |
||
75 |
||
76 |
RSASignature(String digestName) { |
|
77 |
||
78 |
try { |
|
79 |
messageDigest = MessageDigest.getInstance(digestName); |
|
80 |
||
81 |
} catch (NoSuchAlgorithmException e) { |
|
82 |
throw new ProviderException(e); |
|
83 |
} |
|
84 |
||
85 |
needsReset = false; |
|
86 |
} |
|
87 |
||
88 |
public static final class SHA1 extends RSASignature { |
|
89 |
public SHA1() { |
|
90 |
super("SHA1"); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
public static final class MD5 extends RSASignature { |
|
95 |
public MD5() { |
|
96 |
super("MD5"); |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
public static final class MD2 extends RSASignature { |
|
101 |
public MD2() { |
|
102 |
super("MD2"); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Initializes this signature object with the specified |
|
108 |
* public key for verification operations. |
|
109 |
* |
|
110 |
* @param publicKey the public key of the identity whose signature is |
|
111 |
* going to be verified. |
|
112 |
* |
|
113 |
* @exception InvalidKeyException if the key is improperly |
|
114 |
* encoded, parameters are missing, and so on. |
|
115 |
*/ |
|
116 |
protected void engineInitVerify(PublicKey key) |
|
117 |
throws InvalidKeyException |
|
118 |
{ |
|
119 |
// This signature accepts only RSAPublicKey |
|
120 |
if ((key instanceof java.security.interfaces.RSAPublicKey) == false) { |
|
121 |
throw new InvalidKeyException("Key type not supported"); |
|
122 |
} |
|
123 |
||
124 |
java.security.interfaces.RSAPublicKey rsaKey = |
|
125 |
(java.security.interfaces.RSAPublicKey) key; |
|
126 |
||
127 |
if ((key instanceof sun.security.mscapi.RSAPublicKey) == false) { |
|
128 |
||
129 |
// convert key to MSCAPI format |
|
130 |
||
2596 | 131 |
BigInteger modulus = rsaKey.getModulus(); |
132 |
BigInteger exponent = rsaKey.getPublicExponent(); |
|
133 |
||
134 |
// Check against the local and global values to make sure |
|
135 |
// the sizes are ok. Round up to the nearest byte. |
|
136 |
RSAKeyFactory.checkKeyLengths(((modulus.bitLength() + 7) & ~7), |
|
137 |
exponent, -1, RSAKeyPairGenerator.KEY_SIZE_MAX); |
|
138 |
||
139 |
byte[] modulusBytes = modulus.toByteArray(); |
|
140 |
byte[] exponentBytes = exponent.toByteArray(); |
|
2 | 141 |
|
142 |
// Adjust key length due to sign bit |
|
143 |
int keyBitLength = (modulusBytes[0] == 0) |
|
144 |
? (modulusBytes.length - 1) * 8 |
|
145 |
: modulusBytes.length * 8; |
|
146 |
||
147 |
byte[] keyBlob = generatePublicKeyBlob( |
|
2596 | 148 |
keyBitLength, modulusBytes, exponentBytes); |
2 | 149 |
|
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
150 |
try { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
151 |
publicKey = importPublicKey(keyBlob, keyBitLength); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
152 |
|
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
153 |
} catch (KeyStoreException e) { |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
154 |
throw new InvalidKeyException(e); |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
155 |
} |
2 | 156 |
|
157 |
} else { |
|
158 |
publicKey = (sun.security.mscapi.RSAPublicKey) key; |
|
159 |
} |
|
160 |
||
161 |
if (needsReset) { |
|
162 |
messageDigest.reset(); |
|
163 |
needsReset = false; |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* Initializes this signature object with the specified |
|
169 |
* private key for signing operations. |
|
170 |
* |
|
171 |
* @param privateKey the private key of the identity whose signature |
|
172 |
* will be generated. |
|
173 |
* |
|
174 |
* @exception InvalidKeyException if the key is improperly |
|
175 |
* encoded, parameters are missing, and so on. |
|
176 |
*/ |
|
177 |
protected void engineInitSign(PrivateKey key) |
|
178 |
throws InvalidKeyException |
|
179 |
{ |
|
180 |
// This signature accepts only RSAPrivateKey |
|
181 |
if ((key instanceof sun.security.mscapi.RSAPrivateKey) == false) { |
|
182 |
throw new InvalidKeyException("Key type not supported"); |
|
183 |
} |
|
184 |
privateKey = (sun.security.mscapi.RSAPrivateKey) key; |
|
185 |
||
2596 | 186 |
// Check against the local and global values to make sure |
187 |
// the sizes are ok. Round up to nearest byte. |
|
188 |
RSAKeyFactory.checkKeyLengths(((privateKey.bitLength() + 7) & ~7), |
|
189 |
null, RSAKeyPairGenerator.KEY_SIZE_MIN, |
|
190 |
RSAKeyPairGenerator.KEY_SIZE_MAX); |
|
2 | 191 |
|
192 |
if (needsReset) { |
|
193 |
messageDigest.reset(); |
|
194 |
needsReset = false; |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Updates the data to be signed or verified |
|
200 |
* using the specified byte. |
|
201 |
* |
|
202 |
* @param b the byte to use for the update. |
|
203 |
* |
|
204 |
* @exception SignatureException if the engine is not initialized |
|
205 |
* properly. |
|
206 |
*/ |
|
207 |
protected void engineUpdate(byte b) throws SignatureException |
|
208 |
{ |
|
209 |
messageDigest.update(b); |
|
210 |
needsReset = true; |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Updates the data to be signed or verified, using the |
|
215 |
* specified array of bytes, starting at the specified offset. |
|
216 |
* |
|
217 |
* @param b the array of bytes |
|
218 |
* @param off the offset to start from in the array of bytes |
|
219 |
* @param len the number of bytes to use, starting at offset |
|
220 |
* |
|
221 |
* @exception SignatureException if the engine is not initialized |
|
222 |
* properly |
|
223 |
*/ |
|
224 |
protected void engineUpdate(byte[] b, int off, int len) |
|
225 |
throws SignatureException |
|
226 |
{ |
|
227 |
messageDigest.update(b, off, len); |
|
228 |
needsReset = true; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Updates the data to be signed or verified, using the |
|
233 |
* specified ByteBuffer. |
|
234 |
* |
|
235 |
* @param input the ByteBuffer |
|
236 |
*/ |
|
237 |
protected void engineUpdate(ByteBuffer input) |
|
238 |
{ |
|
239 |
messageDigest.update(input); |
|
240 |
needsReset = true; |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Returns the signature bytes of all the data |
|
245 |
* updated so far. |
|
246 |
* The format of the signature depends on the underlying |
|
247 |
* signature scheme. |
|
248 |
* |
|
249 |
* @return the signature bytes of the signing operation's result. |
|
250 |
* |
|
251 |
* @exception SignatureException if the engine is not |
|
252 |
* initialized properly or if this signature algorithm is unable to |
|
253 |
* process the input data provided. |
|
254 |
*/ |
|
255 |
protected byte[] engineSign() throws SignatureException { |
|
256 |
||
257 |
byte[] hash = messageDigest.digest(); |
|
258 |
needsReset = false; |
|
259 |
||
260 |
// Sign hash using MS Crypto APIs |
|
261 |
||
262 |
byte[] result = signHash(hash, hash.length, |
|
263 |
messageDigest.getAlgorithm(), privateKey.getHCryptProvider(), |
|
264 |
privateKey.getHCryptKey()); |
|
265 |
||
266 |
// Convert signature array from little endian to big endian |
|
267 |
return convertEndianArray(result); |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* Convert array from big endian to little endian, or vice versa. |
|
272 |
*/ |
|
273 |
private byte[] convertEndianArray(byte[] byteArray) |
|
274 |
{ |
|
275 |
if (byteArray == null || byteArray.length == 0) |
|
276 |
return byteArray; |
|
277 |
||
278 |
byte [] retval = new byte[byteArray.length]; |
|
279 |
||
280 |
// make it big endian |
|
281 |
for (int i=0;i < byteArray.length;i++) |
|
282 |
retval[i] = byteArray[byteArray.length - i - 1]; |
|
283 |
||
284 |
return retval; |
|
285 |
} |
|
286 |
||
287 |
/** |
|
288 |
* Sign hash using Microsoft Crypto API with HCRYPTKEY. |
|
289 |
* The returned data is in little-endian. |
|
290 |
*/ |
|
291 |
private native static byte[] signHash(byte[] hash, int hashSize, |
|
292 |
String hashAlgorithm, long hCryptProv, long hCryptKey) |
|
293 |
throws SignatureException; |
|
294 |
||
295 |
/** |
|
296 |
* Verify a signed hash using Microsoft Crypto API with HCRYPTKEY. |
|
297 |
*/ |
|
298 |
private native static boolean verifySignedHash(byte[] hash, int hashSize, |
|
299 |
String hashAlgorithm, byte[] signature, int signatureSize, |
|
300 |
long hCryptProv, long hCryptKey) throws SignatureException; |
|
301 |
||
302 |
/** |
|
303 |
* Verifies the passed-in signature. |
|
304 |
* |
|
305 |
* @param sigBytes the signature bytes to be verified. |
|
306 |
* |
|
307 |
* @return true if the signature was verified, false if not. |
|
308 |
* |
|
309 |
* @exception SignatureException if the engine is not |
|
310 |
* initialized properly, the passed-in signature is improperly |
|
311 |
* encoded or of the wrong type, if this signature algorithm is unable to |
|
312 |
* process the input data provided, etc. |
|
313 |
*/ |
|
314 |
protected boolean engineVerify(byte[] sigBytes) |
|
315 |
throws SignatureException |
|
316 |
{ |
|
317 |
byte[] hash = messageDigest.digest(); |
|
318 |
needsReset = false; |
|
319 |
||
320 |
return verifySignedHash(hash, hash.length, |
|
321 |
messageDigest.getAlgorithm(), convertEndianArray(sigBytes), |
|
322 |
sigBytes.length, publicKey.getHCryptProvider(), |
|
323 |
publicKey.getHCryptKey()); |
|
324 |
} |
|
325 |
||
326 |
/** |
|
327 |
* Sets the specified algorithm parameter to the specified |
|
328 |
* value. This method supplies a general-purpose mechanism through |
|
329 |
* which it is possible to set the various parameters of this object. |
|
330 |
* A parameter may be any settable parameter for the algorithm, such as |
|
331 |
* a parameter size, or a source of random bits for signature generation |
|
332 |
* (if appropriate), or an indication of whether or not to perform |
|
333 |
* a specific but optional computation. A uniform algorithm-specific |
|
334 |
* naming scheme for each parameter is desirable but left unspecified |
|
335 |
* at this time. |
|
336 |
* |
|
337 |
* @param param the string identifier of the parameter. |
|
338 |
* |
|
339 |
* @param value the parameter value. |
|
340 |
* |
|
341 |
* @exception InvalidParameterException if <code>param</code> is an |
|
342 |
* invalid parameter for this signature algorithm engine, |
|
343 |
* the parameter is already set |
|
344 |
* and cannot be set again, a security exception occurs, and so on. |
|
345 |
* |
|
346 |
* @deprecated Replaced by {@link |
|
347 |
* #engineSetParameter(java.security.spec.AlgorithmParameterSpec) |
|
348 |
* engineSetParameter}. |
|
349 |
*/ |
|
350 |
protected void engineSetParameter(String param, Object value) |
|
351 |
throws InvalidParameterException |
|
352 |
{ |
|
353 |
throw new InvalidParameterException("Parameter not supported"); |
|
354 |
} |
|
355 |
||
356 |
||
357 |
/** |
|
358 |
* Gets the value of the specified algorithm parameter. |
|
359 |
* This method supplies a general-purpose mechanism through which it |
|
360 |
* is possible to get the various parameters of this object. A parameter |
|
361 |
* may be any settable parameter for the algorithm, such as a parameter |
|
362 |
* size, or a source of random bits for signature generation (if |
|
363 |
* appropriate), or an indication of whether or not to perform a |
|
364 |
* specific but optional computation. A uniform algorithm-specific |
|
365 |
* naming scheme for each parameter is desirable but left unspecified |
|
366 |
* at this time. |
|
367 |
* |
|
368 |
* @param param the string name of the parameter. |
|
369 |
* |
|
370 |
* @return the object that represents the parameter value, or null if |
|
371 |
* there is none. |
|
372 |
* |
|
373 |
* @exception InvalidParameterException if <code>param</code> is an |
|
374 |
* invalid parameter for this engine, or another exception occurs while |
|
375 |
* trying to get this parameter. |
|
376 |
* |
|
377 |
* @deprecated |
|
378 |
*/ |
|
379 |
protected Object engineGetParameter(String param) |
|
380 |
throws InvalidParameterException |
|
381 |
{ |
|
382 |
throw new InvalidParameterException("Parameter not supported"); |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Generates a public-key BLOB from a key's components. |
|
387 |
*/ |
|
9505
3bbc6ff8a905
6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
5506
diff
changeset
|
388 |
// used by RSACipher |
3bbc6ff8a905
6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
5506
diff
changeset
|
389 |
static native byte[] generatePublicKeyBlob( |
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
390 |
int keyBitLength, byte[] modulus, byte[] publicExponent) |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
391 |
throws InvalidKeyException; |
2 | 392 |
|
393 |
/** |
|
394 |
* Imports a public-key BLOB. |
|
395 |
*/ |
|
9505
3bbc6ff8a905
6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
5506
diff
changeset
|
396 |
// used by RSACipher |
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
397 |
static native RSAPublicKey importPublicKey(byte[] keyBlob, int keySize) |
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
398 |
throws KeyStoreException; |
2 | 399 |
} |