author | jjg |
Mon, 15 Aug 2011 11:48:20 -0700 | |
changeset 10336 | 0bb1999251f8 |
parent 5506 | 202f599c92aa |
child 11521 | d7698e6c5f51 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.pkcs11; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.math.BigInteger; |
|
30 |
import java.nio.ByteBuffer; |
|
31 |
||
32 |
import java.security.*; |
|
5155
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
33 |
import java.security.interfaces.*; |
2 | 34 |
import sun.nio.ch.DirectBuffer; |
35 |
||
36 |
import sun.security.util.*; |
|
37 |
import sun.security.x509.AlgorithmId; |
|
38 |
||
39 |
import sun.security.rsa.RSASignature; |
|
40 |
import sun.security.rsa.RSAPadding; |
|
41 |
||
42 |
import sun.security.pkcs11.wrapper.*; |
|
43 |
import static sun.security.pkcs11.wrapper.PKCS11Constants.*; |
|
44 |
||
45 |
/** |
|
46 |
* Signature implementation class. This class currently supports the |
|
47 |
* following algorithms: |
|
48 |
* |
|
49 |
* . DSA |
|
50 |
* . NONEwithDSA (RawDSA) |
|
51 |
* . SHA1withDSA |
|
52 |
* . RSA: |
|
53 |
* . MD2withRSA |
|
54 |
* . MD5withRSA |
|
55 |
* . SHA1withRSA |
|
56 |
* . SHA256withRSA |
|
57 |
* . SHA384withRSA |
|
58 |
* . SHA512withRSA |
|
59 |
* . ECDSA |
|
60 |
* . NONEwithECDSA |
|
61 |
* . SHA1withECDSA |
|
62 |
* . SHA256withECDSA |
|
63 |
* . SHA384withECDSA |
|
64 |
* . SHA512withECDSA |
|
65 |
* |
|
66 |
* Note that the underlying PKCS#11 token may support complete signature |
|
67 |
* algorithm (e.g. CKM_DSA_SHA1, CKM_MD5_RSA_PKCS), or it may just |
|
68 |
* implement the signature algorithm without hashing (e.g. CKM_DSA, CKM_PKCS), |
|
69 |
* or it may only implement the raw public key operation (CKM_RSA_X_509). |
|
70 |
* This class uses what is available and adds whatever extra processing |
|
71 |
* is needed. |
|
72 |
* |
|
73 |
* @author Andreas Sterbenz |
|
74 |
* @since 1.5 |
|
75 |
*/ |
|
76 |
final class P11Signature extends SignatureSpi { |
|
77 |
||
78 |
// token instance |
|
79 |
private final Token token; |
|
80 |
||
81 |
// algorithm name |
|
82 |
private final String algorithm; |
|
83 |
||
84 |
// name of the key algorithm, currently either RSA or DSA |
|
85 |
private final String keyAlgorithm; |
|
86 |
||
87 |
// mechanism id |
|
88 |
private final long mechanism; |
|
89 |
||
5155
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
90 |
// digest algorithm OID, if we encode RSA signature ourselves |
2 | 91 |
private final ObjectIdentifier digestOID; |
92 |
||
93 |
// type, one of T_* below |
|
94 |
private final int type; |
|
95 |
||
96 |
// key instance used, if init*() was called |
|
97 |
private P11Key p11Key; |
|
98 |
||
99 |
// message digest, if we do the digesting ourselves |
|
100 |
private final MessageDigest md; |
|
101 |
||
102 |
// associated session, if any |
|
103 |
private Session session; |
|
104 |
||
5155
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
105 |
// mode, one of M_* below |
2 | 106 |
private int mode; |
107 |
||
108 |
// flag indicating whether an operation is initialized |
|
109 |
private boolean initialized; |
|
110 |
||
111 |
// buffer, for update(byte) or DSA |
|
112 |
private final byte[] buffer; |
|
113 |
||
114 |
// total number of bytes processed in current operation |
|
115 |
private int bytesProcessed; |
|
116 |
||
117 |
// constant for signing mode |
|
118 |
private final static int M_SIGN = 1; |
|
119 |
// constant for verification mode |
|
120 |
private final static int M_VERIFY = 2; |
|
121 |
||
122 |
// constant for type digesting, we do the hashing ourselves |
|
123 |
private final static int T_DIGEST = 1; |
|
124 |
// constant for type update, token does everything |
|
125 |
private final static int T_UPDATE = 2; |
|
126 |
// constant for type raw, used with RawDSA and NONEwithECDSA only |
|
127 |
private final static int T_RAW = 3; |
|
128 |
||
129 |
// XXX PKCS#11 v2.20 says "should not be longer than 1024 bits", |
|
130 |
// but this is a little arbitrary |
|
131 |
private final static int RAW_ECDSA_MAX = 128; |
|
132 |
||
133 |
P11Signature(Token token, String algorithm, long mechanism) |
|
134 |
throws NoSuchAlgorithmException, PKCS11Exception { |
|
135 |
super(); |
|
136 |
this.token = token; |
|
137 |
this.algorithm = algorithm; |
|
138 |
this.mechanism = mechanism; |
|
5155
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
139 |
byte[] buffer = null; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
140 |
ObjectIdentifier digestOID = null; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
141 |
MessageDigest md = null; |
2 | 142 |
switch ((int)mechanism) { |
143 |
case (int)CKM_MD2_RSA_PKCS: |
|
144 |
case (int)CKM_MD5_RSA_PKCS: |
|
145 |
case (int)CKM_SHA1_RSA_PKCS: |
|
146 |
case (int)CKM_SHA256_RSA_PKCS: |
|
147 |
case (int)CKM_SHA384_RSA_PKCS: |
|
148 |
case (int)CKM_SHA512_RSA_PKCS: |
|
149 |
keyAlgorithm = "RSA"; |
|
150 |
type = T_UPDATE; |
|
151 |
buffer = new byte[1]; |
|
152 |
break; |
|
153 |
case (int)CKM_DSA_SHA1: |
|
154 |
keyAlgorithm = "DSA"; |
|
155 |
type = T_UPDATE; |
|
156 |
buffer = new byte[1]; |
|
157 |
break; |
|
158 |
case (int)CKM_ECDSA_SHA1: |
|
159 |
keyAlgorithm = "EC"; |
|
160 |
type = T_UPDATE; |
|
161 |
buffer = new byte[1]; |
|
162 |
break; |
|
163 |
case (int)CKM_DSA: |
|
164 |
keyAlgorithm = "DSA"; |
|
165 |
if (algorithm.equals("DSA")) { |
|
166 |
type = T_DIGEST; |
|
167 |
md = MessageDigest.getInstance("SHA-1"); |
|
168 |
} else if (algorithm.equals("RawDSA")) { |
|
169 |
type = T_RAW; |
|
170 |
buffer = new byte[20]; |
|
171 |
} else { |
|
172 |
throw new ProviderException(algorithm); |
|
173 |
} |
|
174 |
break; |
|
175 |
case (int)CKM_ECDSA: |
|
176 |
keyAlgorithm = "EC"; |
|
177 |
if (algorithm.equals("NONEwithECDSA")) { |
|
178 |
type = T_RAW; |
|
179 |
buffer = new byte[RAW_ECDSA_MAX]; |
|
180 |
} else { |
|
181 |
String digestAlg; |
|
182 |
if (algorithm.equals("SHA1withECDSA")) { |
|
183 |
digestAlg = "SHA-1"; |
|
184 |
} else if (algorithm.equals("SHA256withECDSA")) { |
|
185 |
digestAlg = "SHA-256"; |
|
186 |
} else if (algorithm.equals("SHA384withECDSA")) { |
|
187 |
digestAlg = "SHA-384"; |
|
188 |
} else if (algorithm.equals("SHA512withECDSA")) { |
|
189 |
digestAlg = "SHA-512"; |
|
190 |
} else { |
|
191 |
throw new ProviderException(algorithm); |
|
192 |
} |
|
193 |
type = T_DIGEST; |
|
194 |
md = MessageDigest.getInstance(digestAlg); |
|
195 |
} |
|
196 |
break; |
|
197 |
case (int)CKM_RSA_PKCS: |
|
198 |
case (int)CKM_RSA_X_509: |
|
199 |
keyAlgorithm = "RSA"; |
|
200 |
type = T_DIGEST; |
|
201 |
if (algorithm.equals("MD5withRSA")) { |
|
202 |
md = MessageDigest.getInstance("MD5"); |
|
203 |
digestOID = AlgorithmId.MD5_oid; |
|
204 |
} else if (algorithm.equals("SHA1withRSA")) { |
|
205 |
md = MessageDigest.getInstance("SHA-1"); |
|
206 |
digestOID = AlgorithmId.SHA_oid; |
|
207 |
} else if (algorithm.equals("MD2withRSA")) { |
|
208 |
md = MessageDigest.getInstance("MD2"); |
|
209 |
digestOID = AlgorithmId.MD2_oid; |
|
210 |
} else if (algorithm.equals("SHA256withRSA")) { |
|
211 |
md = MessageDigest.getInstance("SHA-256"); |
|
212 |
digestOID = AlgorithmId.SHA256_oid; |
|
213 |
} else if (algorithm.equals("SHA384withRSA")) { |
|
214 |
md = MessageDigest.getInstance("SHA-384"); |
|
215 |
digestOID = AlgorithmId.SHA384_oid; |
|
216 |
} else if (algorithm.equals("SHA512withRSA")) { |
|
217 |
md = MessageDigest.getInstance("SHA-512"); |
|
218 |
digestOID = AlgorithmId.SHA512_oid; |
|
219 |
} else { |
|
220 |
throw new ProviderException("Unknown signature: " + algorithm); |
|
221 |
} |
|
222 |
break; |
|
223 |
default: |
|
224 |
throw new ProviderException("Unknown mechanism: " + mechanism); |
|
225 |
} |
|
5155
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
226 |
this.buffer = buffer; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
227 |
this.digestOID = digestOID; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
228 |
this.md = md; |
2 | 229 |
} |
230 |
||
231 |
private void ensureInitialized() { |
|
232 |
token.ensureValid(); |
|
233 |
if (initialized == false) { |
|
234 |
initialize(); |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
private void cancelOperation() { |
|
239 |
token.ensureValid(); |
|
240 |
if (initialized == false) { |
|
241 |
return; |
|
242 |
} |
|
243 |
initialized = false; |
|
244 |
if ((session == null) || (token.explicitCancel == false)) { |
|
245 |
return; |
|
246 |
} |
|
247 |
if (session.hasObjects() == false) { |
|
248 |
session = token.killSession(session); |
|
249 |
return; |
|
250 |
} |
|
251 |
// "cancel" operation by finishing it |
|
252 |
// XXX make sure all this always works correctly |
|
253 |
if (mode == M_SIGN) { |
|
254 |
try { |
|
255 |
if (type == T_UPDATE) { |
|
256 |
token.p11.C_SignFinal(session.id(), 0); |
|
257 |
} else { |
|
258 |
byte[] digest; |
|
259 |
if (type == T_DIGEST) { |
|
260 |
digest = md.digest(); |
|
261 |
} else { // T_RAW |
|
262 |
digest = buffer; |
|
263 |
} |
|
264 |
token.p11.C_Sign(session.id(), digest); |
|
265 |
} |
|
266 |
} catch (PKCS11Exception e) { |
|
267 |
throw new ProviderException("cancel failed", e); |
|
268 |
} |
|
269 |
} else { // M_VERIFY |
|
270 |
try { |
|
271 |
byte[] signature; |
|
272 |
if (keyAlgorithm.equals("DSA")) { |
|
273 |
signature = new byte[40]; |
|
274 |
} else { |
|
275 |
signature = new byte[(p11Key.keyLength() + 7) >> 3]; |
|
276 |
} |
|
277 |
if (type == T_UPDATE) { |
|
278 |
token.p11.C_VerifyFinal(session.id(), signature); |
|
279 |
} else { |
|
280 |
byte[] digest; |
|
281 |
if (type == T_DIGEST) { |
|
282 |
digest = md.digest(); |
|
283 |
} else { // T_RAW |
|
284 |
digest = buffer; |
|
285 |
} |
|
286 |
token.p11.C_Verify(session.id(), digest, signature); |
|
287 |
} |
|
288 |
} catch (PKCS11Exception e) { |
|
289 |
// will fail since the signature is incorrect |
|
290 |
// XXX check error code |
|
291 |
} |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
// assumes current state is initialized == false |
|
296 |
private void initialize() { |
|
297 |
try { |
|
298 |
if (session == null) { |
|
299 |
session = token.getOpSession(); |
|
300 |
} |
|
301 |
if (mode == M_SIGN) { |
|
302 |
token.p11.C_SignInit(session.id(), |
|
303 |
new CK_MECHANISM(mechanism), p11Key.keyID); |
|
304 |
} else { |
|
305 |
token.p11.C_VerifyInit(session.id(), |
|
306 |
new CK_MECHANISM(mechanism), p11Key.keyID); |
|
307 |
} |
|
308 |
initialized = true; |
|
309 |
} catch (PKCS11Exception e) { |
|
310 |
throw new ProviderException("Initialization failed", e); |
|
311 |
} |
|
312 |
if (bytesProcessed != 0) { |
|
313 |
bytesProcessed = 0; |
|
314 |
if (md != null) { |
|
315 |
md.reset(); |
|
316 |
} |
|
317 |
} |
|
318 |
} |
|
319 |
||
5155
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
320 |
private void checkRSAKeyLength(int len) throws InvalidKeyException { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
321 |
RSAPadding padding; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
322 |
try { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
323 |
padding = RSAPadding.getInstance |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
324 |
(RSAPadding.PAD_BLOCKTYPE_1, (len + 7) >> 3); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
325 |
} catch (InvalidAlgorithmParameterException iape) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
326 |
throw new InvalidKeyException(iape.getMessage()); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
327 |
} |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
328 |
int maxDataSize = padding.getMaxDataSize(); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
329 |
int encodedLength; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
330 |
if (algorithm.equals("MD5withRSA") || |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
331 |
algorithm.equals("MD2withRSA")) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
332 |
encodedLength = 34; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
333 |
} else if (algorithm.equals("SHA1withRSA")) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
334 |
encodedLength = 35; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
335 |
} else if (algorithm.equals("SHA256withRSA")) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
336 |
encodedLength = 51; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
337 |
} else if (algorithm.equals("SHA384withRSA")) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
338 |
encodedLength = 67; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
339 |
} else if (algorithm.equals("SHA512withRSA")) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
340 |
encodedLength = 83; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
341 |
} else { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
342 |
throw new ProviderException("Unknown signature algo: " + algorithm); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
343 |
} |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
344 |
if (encodedLength > maxDataSize) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
345 |
throw new InvalidKeyException |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
346 |
("Key is too short for this signature algorithm"); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
347 |
} |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
348 |
} |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
349 |
|
2 | 350 |
// see JCA spec |
351 |
protected void engineInitVerify(PublicKey publicKey) |
|
352 |
throws InvalidKeyException { |
|
5155
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
353 |
if (publicKey == null) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
354 |
throw new InvalidKeyException("Key must not be null"); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
355 |
} |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
356 |
// Need to check RSA key length whenever a new key is set |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
357 |
if (keyAlgorithm.equals("RSA") && publicKey != p11Key) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
358 |
int keyLen; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
359 |
if (publicKey instanceof P11Key) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
360 |
keyLen = ((P11Key) publicKey).keyLength(); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
361 |
} else { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
362 |
keyLen = ((RSAKey) publicKey).getModulus().bitLength(); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
363 |
} |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
364 |
checkRSAKeyLength(keyLen); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
365 |
} |
2 | 366 |
cancelOperation(); |
367 |
mode = M_VERIFY; |
|
368 |
p11Key = P11KeyFactory.convertKey(token, publicKey, keyAlgorithm); |
|
369 |
initialize(); |
|
370 |
} |
|
371 |
||
372 |
// see JCA spec |
|
373 |
protected void engineInitSign(PrivateKey privateKey) |
|
374 |
throws InvalidKeyException { |
|
5155
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
375 |
if (privateKey == null) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
376 |
throw new InvalidKeyException("Key must not be null"); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
377 |
} |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
378 |
// Need to check RSA key length whenever a new key is set |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
379 |
if (keyAlgorithm.equals("RSA") && privateKey != p11Key) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
380 |
int keyLen; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
381 |
if (privateKey instanceof P11Key) { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
382 |
keyLen = ((P11Key) privateKey).keyLength; |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
383 |
} else { |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
384 |
keyLen = ((RSAKey) privateKey).getModulus().bitLength(); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
385 |
} |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
386 |
checkRSAKeyLength(keyLen); |
d70c73fdc68e
6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents:
2
diff
changeset
|
387 |
} |
2 | 388 |
cancelOperation(); |
389 |
mode = M_SIGN; |
|
390 |
p11Key = P11KeyFactory.convertKey(token, privateKey, keyAlgorithm); |
|
391 |
initialize(); |
|
392 |
} |
|
393 |
||
394 |
// see JCA spec |
|
395 |
protected void engineUpdate(byte b) throws SignatureException { |
|
396 |
ensureInitialized(); |
|
397 |
switch (type) { |
|
398 |
case T_UPDATE: |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
399 |
buffer[0] = b; |
2 | 400 |
engineUpdate(buffer, 0, 1); |
401 |
break; |
|
402 |
case T_DIGEST: |
|
403 |
md.update(b); |
|
404 |
bytesProcessed++; |
|
405 |
break; |
|
406 |
case T_RAW: |
|
407 |
if (bytesProcessed >= buffer.length) { |
|
408 |
bytesProcessed = buffer.length + 1; |
|
409 |
return; |
|
410 |
} |
|
411 |
buffer[bytesProcessed++] = b; |
|
412 |
break; |
|
413 |
default: |
|
414 |
throw new ProviderException("Internal error"); |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
// see JCA spec |
|
419 |
protected void engineUpdate(byte[] b, int ofs, int len) |
|
420 |
throws SignatureException { |
|
421 |
ensureInitialized(); |
|
422 |
if (len == 0) { |
|
423 |
return; |
|
424 |
} |
|
425 |
switch (type) { |
|
426 |
case T_UPDATE: |
|
427 |
try { |
|
428 |
if (mode == M_SIGN) { |
|
429 |
token.p11.C_SignUpdate(session.id(), 0, b, ofs, len); |
|
430 |
} else { |
|
431 |
token.p11.C_VerifyUpdate(session.id(), 0, b, ofs, len); |
|
432 |
} |
|
433 |
bytesProcessed += len; |
|
434 |
} catch (PKCS11Exception e) { |
|
435 |
throw new ProviderException(e); |
|
436 |
} |
|
437 |
break; |
|
438 |
case T_DIGEST: |
|
439 |
md.update(b, ofs, len); |
|
440 |
bytesProcessed += len; |
|
441 |
break; |
|
442 |
case T_RAW: |
|
443 |
if (bytesProcessed + len > buffer.length) { |
|
444 |
bytesProcessed = buffer.length + 1; |
|
445 |
return; |
|
446 |
} |
|
447 |
System.arraycopy(b, ofs, buffer, bytesProcessed, len); |
|
448 |
bytesProcessed += len; |
|
449 |
break; |
|
450 |
default: |
|
451 |
throw new ProviderException("Internal error"); |
|
452 |
} |
|
453 |
} |
|
454 |
||
455 |
// see JCA spec |
|
456 |
protected void engineUpdate(ByteBuffer byteBuffer) { |
|
457 |
ensureInitialized(); |
|
458 |
int len = byteBuffer.remaining(); |
|
459 |
if (len <= 0) { |
|
460 |
return; |
|
461 |
} |
|
462 |
switch (type) { |
|
463 |
case T_UPDATE: |
|
464 |
if (byteBuffer instanceof DirectBuffer == false) { |
|
465 |
// cannot do better than default impl |
|
466 |
super.engineUpdate(byteBuffer); |
|
467 |
return; |
|
468 |
} |
|
469 |
long addr = ((DirectBuffer)byteBuffer).address(); |
|
470 |
int ofs = byteBuffer.position(); |
|
471 |
try { |
|
472 |
if (mode == M_SIGN) { |
|
473 |
token.p11.C_SignUpdate |
|
474 |
(session.id(), addr + ofs, null, 0, len); |
|
475 |
} else { |
|
476 |
token.p11.C_VerifyUpdate |
|
477 |
(session.id(), addr + ofs, null, 0, len); |
|
478 |
} |
|
479 |
bytesProcessed += len; |
|
480 |
byteBuffer.position(ofs + len); |
|
481 |
} catch (PKCS11Exception e) { |
|
482 |
throw new ProviderException("Update failed", e); |
|
483 |
} |
|
484 |
break; |
|
485 |
case T_DIGEST: |
|
486 |
md.update(byteBuffer); |
|
487 |
bytesProcessed += len; |
|
488 |
break; |
|
489 |
case T_RAW: |
|
490 |
if (bytesProcessed + len > buffer.length) { |
|
491 |
bytesProcessed = buffer.length + 1; |
|
492 |
return; |
|
493 |
} |
|
494 |
byteBuffer.get(buffer, bytesProcessed, len); |
|
495 |
bytesProcessed += len; |
|
496 |
break; |
|
497 |
default: |
|
498 |
throw new ProviderException("Internal error"); |
|
499 |
} |
|
500 |
} |
|
501 |
||
502 |
// see JCA spec |
|
503 |
protected byte[] engineSign() throws SignatureException { |
|
504 |
ensureInitialized(); |
|
505 |
try { |
|
506 |
byte[] signature; |
|
507 |
if (type == T_UPDATE) { |
|
508 |
int len = keyAlgorithm.equals("DSA") ? 40 : 0; |
|
509 |
signature = token.p11.C_SignFinal(session.id(), len); |
|
510 |
} else { |
|
511 |
byte[] digest; |
|
512 |
if (type == T_DIGEST) { |
|
513 |
digest = md.digest(); |
|
514 |
} else { // T_RAW |
|
515 |
if (mechanism == CKM_DSA) { |
|
516 |
if (bytesProcessed != buffer.length) { |
|
517 |
throw new SignatureException |
|
518 |
("Data for RawDSA must be exactly 20 bytes long"); |
|
519 |
} |
|
520 |
digest = buffer; |
|
521 |
} else { // CKM_ECDSA |
|
522 |
if (bytesProcessed > buffer.length) { |
|
523 |
throw new SignatureException("Data for NONEwithECDSA" |
|
524 |
+ " must be at most " + RAW_ECDSA_MAX + " bytes long"); |
|
525 |
} |
|
526 |
digest = new byte[bytesProcessed]; |
|
527 |
System.arraycopy(buffer, 0, digest, 0, bytesProcessed); |
|
528 |
} |
|
529 |
} |
|
530 |
if (keyAlgorithm.equals("RSA") == false) { |
|
531 |
// DSA and ECDSA |
|
532 |
signature = token.p11.C_Sign(session.id(), digest); |
|
533 |
} else { // RSA |
|
534 |
byte[] data = encodeSignature(digest); |
|
535 |
if (mechanism == CKM_RSA_X_509) { |
|
536 |
data = pkcs1Pad(data); |
|
537 |
} |
|
538 |
signature = token.p11.C_Sign(session.id(), data); |
|
539 |
} |
|
540 |
} |
|
541 |
if (keyAlgorithm.equals("RSA") == false) { |
|
542 |
return dsaToASN1(signature); |
|
543 |
} else { |
|
544 |
return signature; |
|
545 |
} |
|
546 |
} catch (PKCS11Exception e) { |
|
547 |
throw new ProviderException(e); |
|
548 |
} finally { |
|
549 |
initialized = false; |
|
550 |
session = token.releaseSession(session); |
|
551 |
} |
|
552 |
} |
|
553 |
||
554 |
// see JCA spec |
|
555 |
protected boolean engineVerify(byte[] signature) throws SignatureException { |
|
556 |
ensureInitialized(); |
|
557 |
try { |
|
558 |
if (keyAlgorithm.equals("DSA")) { |
|
559 |
signature = asn1ToDSA(signature); |
|
560 |
} else if (keyAlgorithm.equals("EC")) { |
|
561 |
signature = asn1ToECDSA(signature); |
|
562 |
} |
|
563 |
if (type == T_UPDATE) { |
|
564 |
token.p11.C_VerifyFinal(session.id(), signature); |
|
565 |
} else { |
|
566 |
byte[] digest; |
|
567 |
if (type == T_DIGEST) { |
|
568 |
digest = md.digest(); |
|
569 |
} else { // T_RAW |
|
570 |
if (mechanism == CKM_DSA) { |
|
571 |
if (bytesProcessed != buffer.length) { |
|
572 |
throw new SignatureException |
|
573 |
("Data for RawDSA must be exactly 20 bytes long"); |
|
574 |
} |
|
575 |
digest = buffer; |
|
576 |
} else { |
|
577 |
if (bytesProcessed > buffer.length) { |
|
578 |
throw new SignatureException("Data for NONEwithECDSA" |
|
579 |
+ " must be at most " + RAW_ECDSA_MAX + " bytes long"); |
|
580 |
} |
|
581 |
digest = new byte[bytesProcessed]; |
|
582 |
System.arraycopy(buffer, 0, digest, 0, bytesProcessed); |
|
583 |
} |
|
584 |
} |
|
585 |
if (keyAlgorithm.equals("RSA") == false) { |
|
586 |
// DSA and ECDSA |
|
587 |
token.p11.C_Verify(session.id(), digest, signature); |
|
588 |
} else { // RSA |
|
589 |
byte[] data = encodeSignature(digest); |
|
590 |
if (mechanism == CKM_RSA_X_509) { |
|
591 |
data = pkcs1Pad(data); |
|
592 |
} |
|
593 |
token.p11.C_Verify(session.id(), data, signature); |
|
594 |
} |
|
595 |
} |
|
596 |
return true; |
|
597 |
} catch (PKCS11Exception e) { |
|
598 |
long errorCode = e.getErrorCode(); |
|
599 |
if (errorCode == CKR_SIGNATURE_INVALID) { |
|
600 |
return false; |
|
601 |
} |
|
602 |
if (errorCode == CKR_SIGNATURE_LEN_RANGE) { |
|
603 |
// return false rather than throwing an exception |
|
604 |
return false; |
|
605 |
} |
|
606 |
// ECF bug? |
|
607 |
if (errorCode == CKR_DATA_LEN_RANGE) { |
|
608 |
return false; |
|
609 |
} |
|
610 |
throw new ProviderException(e); |
|
611 |
} finally { |
|
612 |
// XXX we should not release the session if we abort above |
|
613 |
// before calling C_Verify |
|
614 |
initialized = false; |
|
615 |
session = token.releaseSession(session); |
|
616 |
} |
|
617 |
} |
|
618 |
||
619 |
private byte[] pkcs1Pad(byte[] data) { |
|
620 |
try { |
|
621 |
int len = (p11Key.keyLength() + 7) >> 3; |
|
622 |
RSAPadding padding = RSAPadding.getInstance |
|
623 |
(RSAPadding.PAD_BLOCKTYPE_1, len); |
|
624 |
byte[] padded = padding.pad(data); |
|
625 |
return padded; |
|
626 |
} catch (GeneralSecurityException e) { |
|
627 |
throw new ProviderException(e); |
|
628 |
} |
|
629 |
} |
|
630 |
||
631 |
private byte[] encodeSignature(byte[] digest) throws SignatureException { |
|
632 |
try { |
|
633 |
return RSASignature.encodeSignature(digestOID, digest); |
|
634 |
} catch (IOException e) { |
|
635 |
throw new SignatureException("Invalid encoding", e); |
|
636 |
} |
|
637 |
} |
|
638 |
||
639 |
// private static byte[] decodeSignature(byte[] signature) throws IOException { |
|
640 |
// return RSASignature.decodeSignature(digestOID, signature); |
|
641 |
// } |
|
642 |
||
643 |
// For DSA and ECDSA signatures, PKCS#11 represents them as a simple |
|
644 |
// byte array that contains the concatenation of r and s. |
|
645 |
// For DSA, r and s are always exactly 20 bytes long. |
|
646 |
// For ECDSA, r and s are of variable length, but we know that each |
|
647 |
// occupies half of the array. |
|
648 |
private static byte[] dsaToASN1(byte[] signature) { |
|
649 |
int n = signature.length >> 1; |
|
650 |
BigInteger r = new BigInteger(1, P11Util.subarray(signature, 0, n)); |
|
651 |
BigInteger s = new BigInteger(1, P11Util.subarray(signature, n, n)); |
|
652 |
try { |
|
653 |
DerOutputStream outseq = new DerOutputStream(100); |
|
654 |
outseq.putInteger(r); |
|
655 |
outseq.putInteger(s); |
|
656 |
DerValue result = new DerValue(DerValue.tag_Sequence, |
|
657 |
outseq.toByteArray()); |
|
658 |
return result.toByteArray(); |
|
659 |
} catch (java.io.IOException e) { |
|
660 |
throw new RuntimeException("Internal error", e); |
|
661 |
} |
|
662 |
} |
|
663 |
||
664 |
private static byte[] asn1ToDSA(byte[] signature) throws SignatureException { |
|
665 |
try { |
|
666 |
DerInputStream in = new DerInputStream(signature); |
|
667 |
DerValue[] values = in.getSequence(2); |
|
668 |
BigInteger r = values[0].getPositiveBigInteger(); |
|
669 |
BigInteger s = values[1].getPositiveBigInteger(); |
|
670 |
byte[] br = toByteArray(r, 20); |
|
671 |
byte[] bs = toByteArray(s, 20); |
|
672 |
if ((br == null) || (bs == null)) { |
|
673 |
throw new SignatureException("Out of range value for R or S"); |
|
674 |
} |
|
675 |
return P11Util.concat(br, bs); |
|
676 |
} catch (SignatureException e) { |
|
677 |
throw e; |
|
678 |
} catch (Exception e) { |
|
679 |
throw new SignatureException("invalid encoding for signature", e); |
|
680 |
} |
|
681 |
} |
|
682 |
||
683 |
private byte[] asn1ToECDSA(byte[] signature) throws SignatureException { |
|
684 |
try { |
|
685 |
DerInputStream in = new DerInputStream(signature); |
|
686 |
DerValue[] values = in.getSequence(2); |
|
687 |
BigInteger r = values[0].getPositiveBigInteger(); |
|
688 |
BigInteger s = values[1].getPositiveBigInteger(); |
|
689 |
// trim leading zeroes |
|
690 |
byte[] br = P11Util.trimZeroes(r.toByteArray()); |
|
691 |
byte[] bs = P11Util.trimZeroes(s.toByteArray()); |
|
692 |
int k = Math.max(br.length, bs.length); |
|
693 |
// r and s each occupy half the array |
|
694 |
byte[] res = new byte[k << 1]; |
|
695 |
System.arraycopy(br, 0, res, k - br.length, br.length); |
|
696 |
System.arraycopy(bs, 0, res, res.length - bs.length, bs.length); |
|
697 |
return res; |
|
698 |
} catch (Exception e) { |
|
699 |
throw new SignatureException("invalid encoding for signature", e); |
|
700 |
} |
|
701 |
} |
|
702 |
||
703 |
private static byte[] toByteArray(BigInteger bi, int len) { |
|
704 |
byte[] b = bi.toByteArray(); |
|
705 |
int n = b.length; |
|
706 |
if (n == len) { |
|
707 |
return b; |
|
708 |
} |
|
709 |
if ((n == len + 1) && (b[0] == 0)) { |
|
710 |
byte[] t = new byte[len]; |
|
711 |
System.arraycopy(b, 1, t, 0, len); |
|
712 |
return t; |
|
713 |
} |
|
714 |
if (n > len) { |
|
715 |
return null; |
|
716 |
} |
|
717 |
// must be smaller |
|
718 |
byte[] t = new byte[len]; |
|
719 |
System.arraycopy(b, 0, t, (len - n), n); |
|
720 |
return t; |
|
721 |
} |
|
722 |
||
723 |
// see JCA spec |
|
724 |
protected void engineSetParameter(String param, Object value) |
|
725 |
throws InvalidParameterException { |
|
726 |
throw new UnsupportedOperationException("setParameter() not supported"); |
|
727 |
} |
|
728 |
||
729 |
// see JCA spec |
|
730 |
protected Object engineGetParameter(String param) |
|
731 |
throws InvalidParameterException { |
|
732 |
throw new UnsupportedOperationException("getParameter() not supported"); |
|
733 |
} |
|
734 |
} |