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