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