author | weijun |
Thu, 13 Jun 2019 23:53:21 +0800 | |
changeset 55380 | 22b3b7983ada |
parent 54827 | 01fa7f06f806 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54827 | 2 |
* Copyright (c) 2005, 2019, 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; |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
29 |
import java.security.*; |
53007 | 30 |
import java.security.interfaces.ECPublicKey; |
53006 | 31 |
import java.security.interfaces.RSAPublicKey; |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
32 |
import java.security.spec.AlgorithmParameterSpec; |
2596 | 33 |
import java.math.BigInteger; |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
34 |
import java.security.spec.MGF1ParameterSpec; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
35 |
import java.security.spec.PSSParameterSpec; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
36 |
import java.util.Locale; |
2596 | 37 |
|
38 |
import sun.security.rsa.RSAKeyFactory; |
|
53007 | 39 |
import sun.security.util.ECUtil; |
40 |
import sun.security.util.KeyUtil; |
|
2 | 41 |
|
42 |
/** |
|
53007 | 43 |
* Signature implementation. |
2 | 44 |
* |
45 |
* Objects should be instantiated by calling Signature.getInstance() using the |
|
46 |
* following algorithm names: |
|
47 |
* |
|
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
48 |
* . "NONEwithRSA" |
2 | 49 |
* . "SHA1withRSA" |
9524 | 50 |
* . "SHA256withRSA" |
51 |
* . "SHA384withRSA" |
|
52 |
* . "SHA512withRSA" |
|
2 | 53 |
* . "MD5withRSA" |
54 |
* . "MD2withRSA" |
|
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
55 |
* . "RSASSA-PSS" |
53007 | 56 |
* . "SHA1withECDSA" |
57 |
* . "SHA224withECDSA" |
|
58 |
* . "SHA256withECDSA" |
|
59 |
* . "SHA384withECDSA" |
|
60 |
* . "SHA512withECDSA" |
|
2 | 61 |
* |
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
62 |
* NOTE: RSA keys must be at least 512 bits long. |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
63 |
* |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
64 |
* NOTE: NONEwithRSA must be supplied with a pre-computed message digest. |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
65 |
* Only the following digest algorithms are supported: MD5, SHA-1, |
12700
9c9ad46c7c40
7169496: Problem with the SHA-224 support for SunMSCAPI provider
valeriep
parents:
12685
diff
changeset
|
66 |
* SHA-256, SHA-384, SHA-512 and a special-purpose digest |
12685 | 67 |
* algorithm which is a concatenation of SHA-1 and MD5 digests. |
2 | 68 |
* |
69 |
* @since 1.6 |
|
70 |
* @author Stanley Man-Kit Ho |
|
71 |
*/ |
|
53006 | 72 |
abstract class CSignature extends SignatureSpi { |
73 |
// private key algorithm name |
|
74 |
protected String keyAlgorithm; |
|
75 |
||
2 | 76 |
// message digest implementation we use |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
77 |
protected MessageDigest messageDigest; |
2 | 78 |
|
9524 | 79 |
// message digest name |
53006 | 80 |
protected String messageDigestAlgorithm; |
9524 | 81 |
|
82 |
// flag indicating whether the digest has been reset |
|
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
83 |
protected boolean needsReset; |
2 | 84 |
|
85 |
// the signing key |
|
53006 | 86 |
protected CPrivateKey privateKey = null; |
2 | 87 |
|
88 |
// the verification key |
|
53006 | 89 |
protected CPublicKey publicKey = null; |
2 | 90 |
|
9524 | 91 |
/** |
53006 | 92 |
* Constructs a new CSignature. Used by subclasses. |
9524 | 93 |
*/ |
53006 | 94 |
CSignature(String keyName, String digestName) { |
2 | 95 |
|
53006 | 96 |
this.keyAlgorithm = keyName; |
97 |
if (digestName != null) { |
|
98 |
try { |
|
99 |
messageDigest = MessageDigest.getInstance(digestName); |
|
100 |
// Get the digest's canonical name |
|
101 |
messageDigestAlgorithm = messageDigest.getAlgorithm(); |
|
102 |
} catch (NoSuchAlgorithmException e) { |
|
103 |
throw new ProviderException(e); |
|
104 |
} |
|
105 |
} else { |
|
106 |
messageDigest = null; |
|
107 |
messageDigestAlgorithm = null; |
|
2 | 108 |
} |
109 |
needsReset = false; |
|
110 |
} |
|
111 |
||
53006 | 112 |
static class RSA extends CSignature { |
113 |
||
114 |
public RSA(String digestAlgorithm) { |
|
115 |
super("RSA", digestAlgorithm); |
|
116 |
} |
|
117 |
||
118 |
// initialize for signing. See JCA doc |
|
119 |
@Override |
|
120 |
protected void engineInitSign(PrivateKey key) throws InvalidKeyException { |
|
55380
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
121 |
if (key == null) { |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
122 |
throw new InvalidKeyException("Key cannot be null"); |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
123 |
} |
53007 | 124 |
if ((key instanceof CPrivateKey) == false |
125 |
|| !key.getAlgorithm().equalsIgnoreCase("RSA")) { |
|
54827 | 126 |
throw new InvalidKeyException("Key type not supported: " |
127 |
+ key.getClass() + " " + key.getAlgorithm()); |
|
53006 | 128 |
} |
129 |
privateKey = (CPrivateKey) key; |
|
130 |
||
131 |
// Check against the local and global values to make sure |
|
132 |
// the sizes are ok. Round up to nearest byte. |
|
133 |
RSAKeyFactory.checkKeyLengths(((privateKey.length() + 7) & ~7), |
|
134 |
null, CKeyPairGenerator.RSA.KEY_SIZE_MIN, |
|
135 |
CKeyPairGenerator.RSA.KEY_SIZE_MAX); |
|
136 |
||
137 |
this.publicKey = null; |
|
138 |
resetDigest(); |
|
139 |
} |
|
140 |
||
141 |
// initialize for signing. See JCA doc |
|
142 |
@Override |
|
143 |
protected void engineInitVerify(PublicKey key) throws InvalidKeyException { |
|
55380
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
144 |
if (key == null) { |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
145 |
throw new InvalidKeyException("Key cannot be null"); |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
146 |
} |
53006 | 147 |
// This signature accepts only RSAPublicKey |
148 |
if ((key instanceof RSAPublicKey) == false) { |
|
55380
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
149 |
throw new InvalidKeyException("Key type not supported: " |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
150 |
+ key.getClass()); |
53006 | 151 |
} |
152 |
||
153 |
||
154 |
if ((key instanceof CPublicKey) == false) { |
|
155 |
||
156 |
// convert key to MSCAPI format |
|
157 |
java.security.interfaces.RSAPublicKey rsaKey = |
|
158 |
(java.security.interfaces.RSAPublicKey) key; |
|
159 |
||
160 |
BigInteger modulus = rsaKey.getModulus(); |
|
161 |
BigInteger exponent = rsaKey.getPublicExponent(); |
|
162 |
||
163 |
// Check against the local and global values to make sure |
|
164 |
// the sizes are ok. Round up to the nearest byte. |
|
165 |
RSAKeyFactory.checkKeyLengths(((modulus.bitLength() + 7) & ~7), |
|
166 |
exponent, -1, CKeyPairGenerator.RSA.KEY_SIZE_MAX); |
|
167 |
||
168 |
byte[] modulusBytes = modulus.toByteArray(); |
|
169 |
byte[] exponentBytes = exponent.toByteArray(); |
|
170 |
||
171 |
// Adjust key length due to sign bit |
|
172 |
int keyBitLength = (modulusBytes[0] == 0) |
|
173 |
? (modulusBytes.length - 1) * 8 |
|
174 |
: modulusBytes.length * 8; |
|
175 |
||
176 |
byte[] keyBlob = generatePublicKeyBlob( |
|
177 |
keyBitLength, modulusBytes, exponentBytes); |
|
178 |
||
179 |
try { |
|
180 |
publicKey = importPublicKey("RSA", keyBlob, keyBitLength); |
|
181 |
||
182 |
} catch (KeyStoreException e) { |
|
183 |
throw new InvalidKeyException(e); |
|
184 |
} |
|
185 |
||
186 |
} else { |
|
187 |
publicKey = (CPublicKey) key; |
|
188 |
} |
|
189 |
||
190 |
this.privateKey = null; |
|
191 |
resetDigest(); |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Returns the signature bytes of all the data |
|
196 |
* updated so far. |
|
197 |
* The format of the signature depends on the underlying |
|
198 |
* signature scheme. |
|
199 |
* |
|
200 |
* @return the signature bytes of the signing operation's result. |
|
201 |
* |
|
202 |
* @exception SignatureException if the engine is not |
|
203 |
* initialized properly or if this signature algorithm is unable to |
|
204 |
* process the input data provided. |
|
205 |
*/ |
|
206 |
@Override |
|
207 |
protected byte[] engineSign() throws SignatureException { |
|
208 |
||
209 |
byte[] hash = getDigestValue(); |
|
210 |
||
54827 | 211 |
if (privateKey.getHCryptKey() == 0) { |
212 |
return signCngHash(1, hash, hash.length, |
|
213 |
0, |
|
214 |
this instanceof NONEwithRSA ? null : messageDigestAlgorithm, |
|
215 |
privateKey.getHCryptProvider(), 0); |
|
216 |
} else { |
|
217 |
// Omit the hash OID when generating a NONEwithRSA signature |
|
218 |
boolean noHashOID = this instanceof NONEwithRSA; |
|
219 |
// Sign hash using MS Crypto APIs |
|
220 |
byte[] result = signHash(noHashOID, hash, hash.length, |
|
53006 | 221 |
messageDigestAlgorithm, privateKey.getHCryptProvider(), |
222 |
privateKey.getHCryptKey()); |
|
223 |
||
54827 | 224 |
// Convert signature array from little endian to big endian |
225 |
return convertEndianArray(result); |
|
226 |
} |
|
53006 | 227 |
} |
228 |
||
229 |
/** |
|
230 |
* Verifies the passed-in signature. |
|
231 |
* |
|
232 |
* @param sigBytes the signature bytes to be verified. |
|
233 |
* |
|
234 |
* @return true if the signature was verified, false if not. |
|
235 |
* |
|
236 |
* @exception SignatureException if the engine is not |
|
237 |
* initialized properly, the passed-in signature is improperly |
|
238 |
* encoded or of the wrong type, if this signature algorithm is unable to |
|
239 |
* process the input data provided, etc. |
|
240 |
*/ |
|
241 |
@Override |
|
242 |
protected boolean engineVerify(byte[] sigBytes) |
|
243 |
throws SignatureException { |
|
244 |
byte[] hash = getDigestValue(); |
|
245 |
||
54827 | 246 |
if (publicKey.getHCryptKey() == 0) { |
247 |
return verifyCngSignedHash( |
|
248 |
1, hash, hash.length, |
|
249 |
sigBytes, sigBytes.length, |
|
250 |
0, |
|
251 |
messageDigestAlgorithm, |
|
252 |
publicKey.getHCryptProvider(), |
|
253 |
0); |
|
254 |
} else { |
|
255 |
return verifySignedHash(hash, hash.length, |
|
256 |
messageDigestAlgorithm, convertEndianArray(sigBytes), |
|
257 |
sigBytes.length, publicKey.getHCryptProvider(), |
|
258 |
publicKey.getHCryptKey()); |
|
259 |
} |
|
53006 | 260 |
} |
261 |
||
262 |
/** |
|
263 |
* Generates a public-key BLOB from a key's components. |
|
264 |
*/ |
|
265 |
// used by CRSACipher |
|
266 |
static native byte[] generatePublicKeyBlob( |
|
267 |
int keyBitLength, byte[] modulus, byte[] publicExponent) |
|
268 |
throws InvalidKeyException; |
|
269 |
||
270 |
} |
|
271 |
||
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
272 |
// Nested class for NONEwithRSA signatures |
53006 | 273 |
public static final class NONEwithRSA extends RSA { |
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
274 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
275 |
// the longest supported digest is 512 bits (SHA-512) |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
276 |
private static final int RAW_RSA_MAX = 64; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
277 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
278 |
private final byte[] precomputedDigest; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
279 |
private int offset = 0; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
280 |
|
53006 | 281 |
public NONEwithRSA() { |
282 |
super(null); |
|
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
283 |
precomputedDigest = new byte[RAW_RSA_MAX]; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
284 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
285 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
286 |
// Stores the precomputed message digest value. |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
287 |
@Override |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
288 |
protected void engineUpdate(byte b) throws SignatureException { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
289 |
if (offset >= precomputedDigest.length) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
290 |
offset = RAW_RSA_MAX + 1; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
291 |
return; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
292 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
293 |
precomputedDigest[offset++] = b; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
294 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
295 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
296 |
// Stores the precomputed message digest value. |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
297 |
@Override |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
298 |
protected void engineUpdate(byte[] b, int off, int len) |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
299 |
throws SignatureException { |
51216 | 300 |
if (len > (precomputedDigest.length - offset)) { |
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
301 |
offset = RAW_RSA_MAX + 1; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
302 |
return; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
303 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
304 |
System.arraycopy(b, off, precomputedDigest, offset, len); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
305 |
offset += len; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
306 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
307 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
308 |
// Stores the precomputed message digest value. |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
309 |
@Override |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
310 |
protected void engineUpdate(ByteBuffer byteBuffer) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
311 |
int len = byteBuffer.remaining(); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
312 |
if (len <= 0) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
313 |
return; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
314 |
} |
51216 | 315 |
if (len > (precomputedDigest.length - offset)) { |
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
316 |
offset = RAW_RSA_MAX + 1; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
317 |
return; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
318 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
319 |
byteBuffer.get(precomputedDigest, offset, len); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
320 |
offset += len; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
321 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
322 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
323 |
@Override |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
324 |
protected void resetDigest(){ |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
325 |
offset = 0; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
326 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
327 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
328 |
// Returns the precomputed message digest value. |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
329 |
@Override |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
330 |
protected byte[] getDigestValue() throws SignatureException { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
331 |
if (offset > RAW_RSA_MAX) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
332 |
throw new SignatureException("Message digest is too long"); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
333 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
334 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
335 |
// Determine the digest algorithm from the digest length |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
336 |
if (offset == 20) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
337 |
setDigestName("SHA1"); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
338 |
} else if (offset == 36) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
339 |
setDigestName("SHA1+MD5"); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
340 |
} else if (offset == 32) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
341 |
setDigestName("SHA-256"); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
342 |
} else if (offset == 48) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
343 |
setDigestName("SHA-384"); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
344 |
} else if (offset == 64) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
345 |
setDigestName("SHA-512"); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
346 |
} else if (offset == 16) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
347 |
setDigestName("MD5"); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
348 |
} else { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
349 |
throw new SignatureException( |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
350 |
"Message digest length is not supported"); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
351 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
352 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
353 |
byte[] result = new byte[offset]; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
354 |
System.arraycopy(precomputedDigest, 0, result, 0, offset); |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
355 |
offset = 0; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
356 |
|
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
357 |
return result; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
358 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
359 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
360 |
|
53006 | 361 |
public static final class SHA1withRSA extends RSA { |
362 |
public SHA1withRSA() { |
|
2 | 363 |
super("SHA1"); |
364 |
} |
|
365 |
} |
|
366 |
||
53006 | 367 |
public static final class SHA256withRSA extends RSA { |
368 |
public SHA256withRSA() { |
|
9524 | 369 |
super("SHA-256"); |
370 |
} |
|
371 |
} |
|
372 |
||
53006 | 373 |
public static final class SHA384withRSA extends RSA { |
374 |
public SHA384withRSA() { |
|
9524 | 375 |
super("SHA-384"); |
376 |
} |
|
377 |
} |
|
378 |
||
53006 | 379 |
public static final class SHA512withRSA extends RSA { |
380 |
public SHA512withRSA() { |
|
9524 | 381 |
super("SHA-512"); |
382 |
} |
|
383 |
} |
|
384 |
||
53006 | 385 |
public static final class MD5withRSA extends RSA { |
386 |
public MD5withRSA() { |
|
2 | 387 |
super("MD5"); |
388 |
} |
|
389 |
} |
|
390 |
||
53006 | 391 |
public static final class MD2withRSA extends RSA { |
392 |
public MD2withRSA() { |
|
2 | 393 |
super("MD2"); |
394 |
} |
|
395 |
} |
|
396 |
||
53007 | 397 |
public static final class SHA1withECDSA extends ECDSA { |
398 |
public SHA1withECDSA() { |
|
399 |
super("SHA-1"); |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
public static final class SHA224withECDSA extends ECDSA { |
|
404 |
public SHA224withECDSA() { |
|
405 |
super("SHA-224"); |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
public static final class SHA256withECDSA extends ECDSA { |
|
410 |
public SHA256withECDSA() { |
|
411 |
super("SHA-256"); |
|
412 |
} |
|
413 |
} |
|
414 |
||
415 |
public static final class SHA384withECDSA extends ECDSA { |
|
416 |
public SHA384withECDSA() { |
|
417 |
super("SHA-384"); |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
public static final class SHA512withECDSA extends ECDSA { |
|
422 |
public SHA512withECDSA() { |
|
423 |
super("SHA-512"); |
|
424 |
} |
|
425 |
} |
|
426 |
||
427 |
static class ECDSA extends CSignature { |
|
428 |
||
429 |
public ECDSA(String messageDigestAlgorithm) { |
|
430 |
super("EC", messageDigestAlgorithm); |
|
431 |
} |
|
432 |
||
433 |
// initialize for signing. See JCA doc |
|
434 |
@Override |
|
435 |
protected void engineInitSign(PrivateKey key) throws InvalidKeyException { |
|
55380
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
436 |
if (key == null) { |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
437 |
throw new InvalidKeyException("Key cannot be null"); |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
438 |
} |
53007 | 439 |
if ((key instanceof CPrivateKey) == false |
440 |
|| !key.getAlgorithm().equalsIgnoreCase("EC")) { |
|
55380
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
441 |
throw new InvalidKeyException("Key type not supported: " |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
442 |
+ key.getClass() + " " + key.getAlgorithm()); |
53007 | 443 |
} |
444 |
privateKey = (CPrivateKey) key; |
|
445 |
||
446 |
this.publicKey = null; |
|
447 |
resetDigest(); |
|
448 |
} |
|
449 |
||
450 |
// initialize for signing. See JCA doc |
|
451 |
@Override |
|
452 |
protected void engineInitVerify(PublicKey key) throws InvalidKeyException { |
|
55380
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
453 |
if (key == null) { |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
454 |
throw new InvalidKeyException("Key cannot be null"); |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
455 |
} |
53007 | 456 |
// This signature accepts only ECPublicKey |
457 |
if ((key instanceof ECPublicKey) == false) { |
|
55380
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
458 |
throw new InvalidKeyException("Key type not supported: " |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
459 |
+ key.getClass()); |
53007 | 460 |
} |
461 |
||
462 |
if ((key instanceof CPublicKey) == false) { |
|
463 |
try { |
|
464 |
publicKey = importECPublicKey("EC", |
|
465 |
CKey.generateECBlob(key), |
|
466 |
KeyUtil.getKeySize(key)); |
|
467 |
} catch (KeyStoreException e) { |
|
468 |
throw new InvalidKeyException(e); |
|
469 |
} |
|
470 |
} else { |
|
471 |
publicKey = (CPublicKey) key; |
|
472 |
} |
|
473 |
||
474 |
this.privateKey = null; |
|
475 |
resetDigest(); |
|
476 |
} |
|
477 |
||
478 |
@Override |
|
479 |
protected byte[] engineSign() throws SignatureException { |
|
480 |
byte[] hash = getDigestValue(); |
|
481 |
byte[] raw = signCngHash(0, hash, hash.length, |
|
482 |
0, |
|
483 |
null, |
|
484 |
privateKey.getHCryptProvider(), 0); |
|
485 |
return ECUtil.encodeSignature(raw); |
|
486 |
} |
|
487 |
||
488 |
@Override |
|
489 |
protected boolean engineVerify(byte[] sigBytes) throws SignatureException { |
|
490 |
byte[] hash = getDigestValue(); |
|
491 |
sigBytes = ECUtil.decodeSignature(sigBytes); |
|
492 |
return verifyCngSignedHash( |
|
493 |
0, |
|
494 |
hash, hash.length, |
|
495 |
sigBytes, sigBytes.length, |
|
496 |
0, |
|
497 |
null, |
|
498 |
publicKey.getHCryptProvider(), |
|
499 |
0 |
|
500 |
); |
|
501 |
} |
|
502 |
} |
|
503 |
||
53006 | 504 |
public static final class PSS extends RSA { |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
505 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
506 |
private PSSParameterSpec pssParams = null; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
507 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
508 |
// Workaround: Cannot import raw public key to CNG. This signature |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
509 |
// will be used for verification if key is not from MSCAPI. |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
510 |
private Signature fallbackSignature; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
511 |
|
53006 | 512 |
public PSS() { |
513 |
super(null); |
|
514 |
} |
|
515 |
||
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
516 |
@Override |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
517 |
protected void engineInitSign(PrivateKey key) throws InvalidKeyException { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
518 |
super.engineInitSign(key); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
519 |
fallbackSignature = null; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
520 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
521 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
522 |
@Override |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
523 |
protected void engineInitVerify(PublicKey key) throws InvalidKeyException { |
55380
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
524 |
if (key == null) { |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
525 |
throw new InvalidKeyException("Key cannot be null"); |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
526 |
} |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
527 |
// This signature accepts only RSAPublicKey |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
528 |
if ((key instanceof java.security.interfaces.RSAPublicKey) == false) { |
55380
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
529 |
throw new InvalidKeyException("Key type not supported: " |
22b3b7983ada
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents:
54827
diff
changeset
|
530 |
+ key.getClass()); |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
531 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
532 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
533 |
this.privateKey = null; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
534 |
|
53006 | 535 |
if (key instanceof CPublicKey) { |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
536 |
fallbackSignature = null; |
53006 | 537 |
publicKey = (CPublicKey) key; |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
538 |
} else { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
539 |
if (fallbackSignature == null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
540 |
try { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
541 |
fallbackSignature = Signature.getInstance( |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
542 |
"RSASSA-PSS", "SunRsaSign"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
543 |
} catch (NoSuchAlgorithmException | NoSuchProviderException e) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
544 |
throw new InvalidKeyException("Invalid key", e); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
545 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
546 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
547 |
fallbackSignature.initVerify(key); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
548 |
if (pssParams != null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
549 |
try { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
550 |
fallbackSignature.setParameter(pssParams); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
551 |
} catch (InvalidAlgorithmParameterException e) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
552 |
throw new InvalidKeyException("Invalid params", e); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
553 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
554 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
555 |
publicKey = null; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
556 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
557 |
resetDigest(); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
558 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
559 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
560 |
@Override |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
561 |
protected void engineUpdate(byte b) throws SignatureException { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
562 |
ensureInit(); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
563 |
if (fallbackSignature != null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
564 |
fallbackSignature.update(b); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
565 |
} else { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
566 |
messageDigest.update(b); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
567 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
568 |
needsReset = true; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
569 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
570 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
571 |
@Override |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
572 |
protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
573 |
ensureInit(); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
574 |
if (fallbackSignature != null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
575 |
fallbackSignature.update(b, off, len); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
576 |
} else { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
577 |
messageDigest.update(b, off, len); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
578 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
579 |
needsReset = true; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
580 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
581 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
582 |
@Override |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
583 |
protected void engineUpdate(ByteBuffer input) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
584 |
try { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
585 |
ensureInit(); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
586 |
} catch (SignatureException se) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
587 |
// hack for working around API bug |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
588 |
throw new RuntimeException(se.getMessage()); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
589 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
590 |
if (fallbackSignature != null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
591 |
try { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
592 |
fallbackSignature.update(input); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
593 |
} catch (SignatureException se) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
594 |
// hack for working around API bug |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
595 |
throw new RuntimeException(se.getMessage()); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
596 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
597 |
} else { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
598 |
messageDigest.update(input); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
599 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
600 |
needsReset = true; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
601 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
602 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
603 |
@Override |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
604 |
protected byte[] engineSign() throws SignatureException { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
605 |
ensureInit(); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
606 |
byte[] hash = getDigestValue(); |
53007 | 607 |
return signCngHash(2, hash, hash.length, |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
608 |
pssParams.getSaltLength(), |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
609 |
((MGF1ParameterSpec) |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
610 |
pssParams.getMGFParameters()).getDigestAlgorithm(), |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
611 |
privateKey.getHCryptProvider(), privateKey.getHCryptKey()); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
612 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
613 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
614 |
@Override |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
615 |
protected boolean engineVerify(byte[] sigBytes) throws SignatureException { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
616 |
ensureInit(); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
617 |
if (fallbackSignature != null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
618 |
needsReset = false; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
619 |
return fallbackSignature.verify(sigBytes); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
620 |
} else { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
621 |
byte[] hash = getDigestValue(); |
53006 | 622 |
return verifyCngSignedHash( |
53007 | 623 |
2, hash, hash.length, |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
624 |
sigBytes, sigBytes.length, |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
625 |
pssParams.getSaltLength(), |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
626 |
((MGF1ParameterSpec) |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
627 |
pssParams.getMGFParameters()).getDigestAlgorithm(), |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
628 |
publicKey.getHCryptProvider(), |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
629 |
publicKey.getHCryptKey() |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
630 |
); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
631 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
632 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
633 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
634 |
@Override |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
635 |
protected void engineSetParameter(AlgorithmParameterSpec params) |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
636 |
throws InvalidAlgorithmParameterException { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
637 |
if (needsReset) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
638 |
throw new ProviderException |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
639 |
("Cannot set parameters during operations"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
640 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
641 |
this.pssParams = validateSigParams(params); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
642 |
if (fallbackSignature != null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
643 |
fallbackSignature.setParameter(params); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
644 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
645 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
646 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
647 |
@Override |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
648 |
protected AlgorithmParameters engineGetParameters() { |
51229
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
51216
diff
changeset
|
649 |
AlgorithmParameters ap = null; |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
51216
diff
changeset
|
650 |
if (this.pssParams != null) { |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
51216
diff
changeset
|
651 |
try { |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
51216
diff
changeset
|
652 |
ap = AlgorithmParameters.getInstance("RSASSA-PSS"); |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
51216
diff
changeset
|
653 |
ap.init(this.pssParams); |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
51216
diff
changeset
|
654 |
} catch (GeneralSecurityException gse) { |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
51216
diff
changeset
|
655 |
throw new ProviderException(gse.getMessage()); |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
51216
diff
changeset
|
656 |
} |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
657 |
} |
51229
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
51216
diff
changeset
|
658 |
return ap; |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
659 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
660 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
661 |
private void ensureInit() throws SignatureException { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
662 |
if (this.privateKey == null && this.publicKey == null |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
663 |
&& fallbackSignature == null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
664 |
throw new SignatureException("Missing key"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
665 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
666 |
if (this.pssParams == null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
667 |
// Parameters are required for signature verification |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
668 |
throw new SignatureException |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
669 |
("Parameters required for RSASSA-PSS signatures"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
670 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
671 |
if (fallbackSignature == null && messageDigest == null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
672 |
// This could happen if initVerify(softKey), setParameter(), |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
673 |
// and initSign() were called. No messageDigest. Create it. |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
674 |
try { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
675 |
messageDigest = MessageDigest |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
676 |
.getInstance(pssParams.getDigestAlgorithm()); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
677 |
} catch (NoSuchAlgorithmException e) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
678 |
throw new SignatureException(e); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
679 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
680 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
681 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
682 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
683 |
/** |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
684 |
* Validate the specified Signature PSS parameters. |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
685 |
*/ |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
686 |
private PSSParameterSpec validateSigParams(AlgorithmParameterSpec p) |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
687 |
throws InvalidAlgorithmParameterException { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
688 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
689 |
if (p == null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
690 |
throw new InvalidAlgorithmParameterException |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
691 |
("Parameters cannot be null"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
692 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
693 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
694 |
if (!(p instanceof PSSParameterSpec)) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
695 |
throw new InvalidAlgorithmParameterException |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
696 |
("parameters must be type PSSParameterSpec"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
697 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
698 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
699 |
// no need to validate again if same as current signature parameters |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
700 |
PSSParameterSpec params = (PSSParameterSpec) p; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
701 |
if (params == this.pssParams) return params; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
702 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
703 |
// now sanity check the parameter values |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
704 |
if (!(params.getMGFAlgorithm().equalsIgnoreCase("MGF1"))) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
705 |
throw new InvalidAlgorithmParameterException("Only supports MGF1"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
706 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
707 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
708 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
709 |
if (params.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
710 |
throw new InvalidAlgorithmParameterException |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
711 |
("Only supports TrailerFieldBC(1)"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
712 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
713 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
714 |
AlgorithmParameterSpec algSpec = params.getMGFParameters(); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
715 |
if (!(algSpec instanceof MGF1ParameterSpec)) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
716 |
throw new InvalidAlgorithmParameterException |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
717 |
("Only support MGF1ParameterSpec"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
718 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
719 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
720 |
MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)algSpec; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
721 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
722 |
String msgHashAlg = params.getDigestAlgorithm() |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
723 |
.toLowerCase(Locale.ROOT).replaceAll("-", ""); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
724 |
if (msgHashAlg.equals("sha")) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
725 |
msgHashAlg = "sha1"; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
726 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
727 |
String mgf1HashAlg = mgfSpec.getDigestAlgorithm() |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
728 |
.toLowerCase(Locale.ROOT).replaceAll("-", ""); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
729 |
if (mgf1HashAlg.equals("sha")) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
730 |
mgf1HashAlg = "sha1"; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
731 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
732 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
733 |
if (!mgf1HashAlg.equals(msgHashAlg)) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
734 |
throw new InvalidAlgorithmParameterException |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
735 |
("MGF1 hash must be the same as message hash"); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
736 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
737 |
|
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
738 |
return params; |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
739 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
740 |
} |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
741 |
|
53006 | 742 |
/** |
53007 | 743 |
* Sign hash using CNG API with HCRYPTKEY. |
744 |
* @param type 0 no padding, 1, pkcs1, 2, pss |
|
53006 | 745 |
*/ |
746 |
native static byte[] signCngHash( |
|
53007 | 747 |
int type, byte[] hash, |
748 |
int hashSize, int saltLength, String hashAlgorithm, |
|
53006 | 749 |
long hCryptProv, long nCryptKey) |
750 |
throws SignatureException; |
|
2 | 751 |
|
53006 | 752 |
/** |
53007 | 753 |
* Verify a signed hash using CNG API with HCRYPTKEY. |
754 |
* @param type 0 no padding, 1, pkcs1, 2, pss |
|
53006 | 755 |
*/ |
756 |
private native static boolean verifyCngSignedHash( |
|
53007 | 757 |
int type, byte[] hash, int hashSize, |
758 |
byte[] signature, int signatureSize, |
|
53006 | 759 |
int saltLength, String hashAlgorithm, |
760 |
long hCryptProv, long hKey) throws SignatureException; |
|
9524 | 761 |
|
762 |
/** |
|
763 |
* Resets the message digest if needed. |
|
764 |
*/ |
|
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
765 |
protected void resetDigest() { |
2 | 766 |
if (needsReset) { |
50715
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
767 |
if (messageDigest != null) { |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
768 |
messageDigest.reset(); |
46492a773912
8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents:
50204
diff
changeset
|
769 |
} |
2 | 770 |
needsReset = false; |
771 |
} |
|
772 |
} |
|
773 |
||
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
774 |
protected byte[] getDigestValue() throws SignatureException { |
9524 | 775 |
needsReset = false; |
776 |
return messageDigest.digest(); |
|
777 |
} |
|
778 |
||
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
779 |
protected void setDigestName(String name) { |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
780 |
messageDigestAlgorithm = name; |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
781 |
} |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
782 |
|
2 | 783 |
/** |
784 |
* Updates the data to be signed or verified |
|
785 |
* using the specified byte. |
|
786 |
* |
|
787 |
* @param b the byte to use for the update. |
|
788 |
* |
|
789 |
* @exception SignatureException if the engine is not initialized |
|
790 |
* properly. |
|
791 |
*/ |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
792 |
@Override |
53006 | 793 |
protected void engineUpdate(byte b) throws SignatureException { |
2 | 794 |
messageDigest.update(b); |
795 |
needsReset = true; |
|
796 |
} |
|
797 |
||
798 |
/** |
|
799 |
* Updates the data to be signed or verified, using the |
|
800 |
* specified array of bytes, starting at the specified offset. |
|
801 |
* |
|
802 |
* @param b the array of bytes |
|
803 |
* @param off the offset to start from in the array of bytes |
|
804 |
* @param len the number of bytes to use, starting at offset |
|
805 |
* |
|
806 |
* @exception SignatureException if the engine is not initialized |
|
807 |
* properly |
|
808 |
*/ |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
809 |
@Override |
2 | 810 |
protected void engineUpdate(byte[] b, int off, int len) |
53006 | 811 |
throws SignatureException { |
2 | 812 |
messageDigest.update(b, off, len); |
813 |
needsReset = true; |
|
814 |
} |
|
815 |
||
816 |
/** |
|
817 |
* Updates the data to be signed or verified, using the |
|
818 |
* specified ByteBuffer. |
|
819 |
* |
|
820 |
* @param input the ByteBuffer |
|
821 |
*/ |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
822 |
@Override |
53006 | 823 |
protected void engineUpdate(ByteBuffer input) { |
2 | 824 |
messageDigest.update(input); |
825 |
needsReset = true; |
|
826 |
} |
|
827 |
||
828 |
/** |
|
829 |
* Convert array from big endian to little endian, or vice versa. |
|
830 |
*/ |
|
53006 | 831 |
private static byte[] convertEndianArray(byte[] byteArray) { |
2 | 832 |
if (byteArray == null || byteArray.length == 0) |
833 |
return byteArray; |
|
834 |
||
835 |
byte [] retval = new byte[byteArray.length]; |
|
836 |
||
837 |
// make it big endian |
|
838 |
for (int i=0;i < byteArray.length;i++) |
|
839 |
retval[i] = byteArray[byteArray.length - i - 1]; |
|
840 |
||
841 |
return retval; |
|
842 |
} |
|
843 |
||
844 |
/** |
|
845 |
* Sign hash using Microsoft Crypto API with HCRYPTKEY. |
|
846 |
* The returned data is in little-endian. |
|
847 |
*/ |
|
9533
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
848 |
private native static byte[] signHash(boolean noHashOID, byte[] hash, |
13cc5e8eb9f1
6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents:
9524
diff
changeset
|
849 |
int hashSize, String hashAlgorithm, long hCryptProv, long hCryptKey) |
2 | 850 |
throws SignatureException; |
851 |
||
852 |
/** |
|
853 |
* Verify a signed hash using Microsoft Crypto API with HCRYPTKEY. |
|
854 |
*/ |
|
855 |
private native static boolean verifySignedHash(byte[] hash, int hashSize, |
|
856 |
String hashAlgorithm, byte[] signature, int signatureSize, |
|
857 |
long hCryptProv, long hCryptKey) throws SignatureException; |
|
858 |
||
859 |
/** |
|
860 |
* Sets the specified algorithm parameter to the specified |
|
861 |
* value. This method supplies a general-purpose mechanism through |
|
862 |
* which it is possible to set the various parameters of this object. |
|
863 |
* A parameter may be any settable parameter for the algorithm, such as |
|
864 |
* a parameter size, or a source of random bits for signature generation |
|
865 |
* (if appropriate), or an indication of whether or not to perform |
|
866 |
* a specific but optional computation. A uniform algorithm-specific |
|
867 |
* naming scheme for each parameter is desirable but left unspecified |
|
868 |
* at this time. |
|
869 |
* |
|
870 |
* @param param the string identifier of the parameter. |
|
871 |
* |
|
872 |
* @param value the parameter value. |
|
873 |
* |
|
874 |
* @exception InvalidParameterException if <code>param</code> is an |
|
875 |
* invalid parameter for this signature algorithm engine, |
|
876 |
* the parameter is already set |
|
877 |
* and cannot be set again, a security exception occurs, and so on. |
|
878 |
* |
|
879 |
* @deprecated Replaced by {@link |
|
880 |
* #engineSetParameter(java.security.spec.AlgorithmParameterSpec) |
|
881 |
* engineSetParameter}. |
|
882 |
*/ |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
883 |
@Override |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9533
diff
changeset
|
884 |
@Deprecated |
2 | 885 |
protected void engineSetParameter(String param, Object value) |
53006 | 886 |
throws InvalidParameterException { |
2 | 887 |
throw new InvalidParameterException("Parameter not supported"); |
888 |
} |
|
889 |
||
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
890 |
/** |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
891 |
* Sets this signature engine with the specified algorithm parameter. |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
892 |
* |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
893 |
* @param params the parameters |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
894 |
* |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
895 |
* @exception InvalidAlgorithmParameterException if the given |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
896 |
* parameter is invalid |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
897 |
*/ |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
898 |
@Override |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
899 |
protected void engineSetParameter(AlgorithmParameterSpec params) |
53006 | 900 |
throws InvalidAlgorithmParameterException { |
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
901 |
if (params != null) { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
902 |
throw new InvalidAlgorithmParameterException("No parameter accepted"); |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
903 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
904 |
} |
2 | 905 |
|
906 |
/** |
|
907 |
* Gets the value of the specified algorithm parameter. |
|
908 |
* This method supplies a general-purpose mechanism through which it |
|
909 |
* is possible to get the various parameters of this object. A parameter |
|
910 |
* may be any settable parameter for the algorithm, such as a parameter |
|
911 |
* size, or a source of random bits for signature generation (if |
|
912 |
* appropriate), or an indication of whether or not to perform a |
|
913 |
* specific but optional computation. A uniform algorithm-specific |
|
914 |
* naming scheme for each parameter is desirable but left unspecified |
|
915 |
* at this time. |
|
916 |
* |
|
917 |
* @param param the string name of the parameter. |
|
918 |
* |
|
919 |
* @return the object that represents the parameter value, or null if |
|
920 |
* there is none. |
|
921 |
* |
|
922 |
* @exception InvalidParameterException if <code>param</code> is an |
|
923 |
* invalid parameter for this engine, or another exception occurs while |
|
924 |
* trying to get this parameter. |
|
925 |
* |
|
926 |
* @deprecated |
|
927 |
*/ |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
928 |
@Override |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9533
diff
changeset
|
929 |
@Deprecated |
2 | 930 |
protected Object engineGetParameter(String param) |
53006 | 931 |
throws InvalidParameterException { |
2 | 932 |
throw new InvalidParameterException("Parameter not supported"); |
933 |
} |
|
934 |
||
935 |
/** |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
936 |
* Gets the algorithm parameter from this signature engine. |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
937 |
* |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
938 |
* @return the parameter, or null if no parameter is used. |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
939 |
*/ |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
940 |
@Override |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
941 |
protected AlgorithmParameters engineGetParameters() { |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
942 |
return null; |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
943 |
} |
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
944 |
|
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
945 |
/** |
2 | 946 |
* Imports a public-key BLOB. |
947 |
*/ |
|
53006 | 948 |
// used by CRSACipher |
949 |
static native CPublicKey importPublicKey( |
|
950 |
String alg, byte[] keyBlob, int keySize) throws KeyStoreException; |
|
53007 | 951 |
|
952 |
static native CPublicKey importECPublicKey( |
|
953 |
String alg, byte[] keyBlob, int keySize) throws KeyStoreException; |
|
2 | 954 |
} |