author | vinnie |
Thu, 25 Sep 2014 12:24:19 +0100 | |
changeset 26736 | 5a93000b26cd |
parent 25859 | 3317bb8137f4 |
child 27077 | c943ca4a3380 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1996, 2014, 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 java.security; |
|
27 |
||
28 |
import java.security.spec.AlgorithmParameterSpec; |
|
29 |
import java.util.*; |
|
30 |
import java.util.concurrent.ConcurrentHashMap; |
|
31 |
import java.io.*; |
|
32 |
import java.security.cert.Certificate; |
|
33 |
import java.security.cert.X509Certificate; |
|
34 |
||
35 |
import java.nio.ByteBuffer; |
|
36 |
||
37 |
import java.security.Provider.Service; |
|
38 |
||
39 |
import javax.crypto.Cipher; |
|
40 |
import javax.crypto.CipherSpi; |
|
41 |
import javax.crypto.IllegalBlockSizeException; |
|
42 |
import javax.crypto.BadPaddingException; |
|
43 |
import javax.crypto.NoSuchPaddingException; |
|
44 |
||
45 |
import sun.security.util.Debug; |
|
46 |
import sun.security.jca.*; |
|
47 |
import sun.security.jca.GetInstance.Instance; |
|
48 |
||
49 |
/** |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
50 |
* The Signature class is used to provide applications the functionality |
2 | 51 |
* of a digital signature algorithm. Digital signatures are used for |
52 |
* authentication and integrity assurance of digital data. |
|
53 |
* |
|
54 |
* <p> The signature algorithm can be, among others, the NIST standard |
|
55 |
* DSA, using DSA and SHA-1. The DSA algorithm using the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
56 |
* SHA-1 message digest algorithm can be specified as {@code SHA1withDSA}. |
2 | 57 |
* In the case of RSA, there are multiple choices for the message digest |
58 |
* algorithm, so the signing algorithm could be specified as, for example, |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
59 |
* {@code MD2withRSA}, {@code MD5withRSA}, or {@code SHA1withRSA}. |
2 | 60 |
* The algorithm name must be specified, as there is no default. |
61 |
* |
|
62 |
* <p> A Signature object can be used to generate and verify digital |
|
63 |
* signatures. |
|
64 |
* |
|
65 |
* <p> There are three phases to the use of a Signature object for |
|
66 |
* either signing data or verifying a signature:<ol> |
|
67 |
* |
|
68 |
* <li>Initialization, with either |
|
69 |
* |
|
70 |
* <ul> |
|
71 |
* |
|
72 |
* <li>a public key, which initializes the signature for |
|
73 |
* verification (see {@link #initVerify(PublicKey) initVerify}), or |
|
74 |
* |
|
75 |
* <li>a private key (and optionally a Secure Random Number Generator), |
|
76 |
* which initializes the signature for signing |
|
77 |
* (see {@link #initSign(PrivateKey)} |
|
78 |
* and {@link #initSign(PrivateKey, SecureRandom)}). |
|
79 |
* |
|
21334 | 80 |
* </ul> |
2 | 81 |
* |
21334 | 82 |
* <li>Updating |
2 | 83 |
* |
84 |
* <p>Depending on the type of initialization, this will update the |
|
85 |
* bytes to be signed or verified. See the |
|
21334 | 86 |
* {@link #update(byte) update} methods. |
2 | 87 |
* |
88 |
* <li>Signing or Verifying a signature on all updated bytes. See the |
|
89 |
* {@link #sign() sign} methods and the {@link #verify(byte[]) verify} |
|
90 |
* method. |
|
91 |
* |
|
92 |
* </ol> |
|
93 |
* |
|
94 |
* <p>Note that this class is abstract and extends from |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
95 |
* {@code SignatureSpi} for historical reasons. |
2 | 96 |
* Application developers should only take notice of the methods defined in |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
97 |
* this {@code Signature} class; all the methods in |
2 | 98 |
* the superclass are intended for cryptographic service providers who wish to |
99 |
* supply their own implementations of digital signature algorithms. |
|
100 |
* |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
101 |
* <p> Every implementation of the Java platform is required to support the |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
102 |
* following standard {@code Signature} algorithms: |
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
103 |
* <ul> |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
104 |
* <li>{@code SHA1withDSA}</li> |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
105 |
* <li>{@code SHA1withRSA}</li> |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
106 |
* <li>{@code SHA256withRSA}</li> |
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
107 |
* </ul> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
108 |
* These algorithms are described in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
109 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
110 |
* Signature section</a> of the |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
111 |
* Java Cryptography Architecture Standard Algorithm Name Documentation. |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
112 |
* Consult the release documentation for your implementation to see if any |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
113 |
* other algorithms are supported. |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
114 |
* |
2 | 115 |
* @author Benjamin Renaud |
116 |
* |
|
117 |
*/ |
|
118 |
||
119 |
public abstract class Signature extends SignatureSpi { |
|
120 |
||
121 |
private static final Debug debug = |
|
122 |
Debug.getInstance("jca", "Signature"); |
|
123 |
||
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
124 |
private static final Debug pdebug = |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
125 |
Debug.getInstance("provider", "Provider"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
126 |
private static final boolean skipDebug = |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
127 |
Debug.isOn("engine=") && !Debug.isOn("signature"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
128 |
|
2 | 129 |
/* |
130 |
* The algorithm for this signature object. |
|
131 |
* This value is used to map an OID to the particular algorithm. |
|
132 |
* The mapping is done in AlgorithmObject.algOID(String algorithm) |
|
133 |
*/ |
|
134 |
private String algorithm; |
|
135 |
||
136 |
// The provider |
|
137 |
Provider provider; |
|
138 |
||
139 |
/** |
|
140 |
* Possible {@link #state} value, signifying that |
|
141 |
* this signature object has not yet been initialized. |
|
142 |
*/ |
|
143 |
protected final static int UNINITIALIZED = 0; |
|
144 |
||
145 |
/** |
|
146 |
* Possible {@link #state} value, signifying that |
|
147 |
* this signature object has been initialized for signing. |
|
148 |
*/ |
|
149 |
protected final static int SIGN = 2; |
|
150 |
||
151 |
/** |
|
152 |
* Possible {@link #state} value, signifying that |
|
153 |
* this signature object has been initialized for verification. |
|
154 |
*/ |
|
155 |
protected final static int VERIFY = 3; |
|
156 |
||
157 |
/** |
|
158 |
* Current state of this signature object. |
|
159 |
*/ |
|
160 |
protected int state = UNINITIALIZED; |
|
161 |
||
162 |
/** |
|
163 |
* Creates a Signature object for the specified algorithm. |
|
164 |
* |
|
165 |
* @param algorithm the standard string name of the algorithm. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
166 |
* See the Signature section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
167 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
168 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 169 |
* for information about standard algorithm names. |
170 |
*/ |
|
171 |
protected Signature(String algorithm) { |
|
172 |
this.algorithm = algorithm; |
|
173 |
} |
|
174 |
||
175 |
// name of the special signature alg |
|
176 |
private final static String RSA_SIGNATURE = "NONEwithRSA"; |
|
177 |
||
178 |
// name of the equivalent cipher alg |
|
179 |
private final static String RSA_CIPHER = "RSA/ECB/PKCS1Padding"; |
|
180 |
||
181 |
// all the services we need to lookup for compatibility with Cipher |
|
182 |
private final static List<ServiceId> rsaIds = Arrays.asList( |
|
183 |
new ServiceId[] { |
|
184 |
new ServiceId("Signature", "NONEwithRSA"), |
|
185 |
new ServiceId("Cipher", "RSA/ECB/PKCS1Padding"), |
|
186 |
new ServiceId("Cipher", "RSA/ECB"), |
|
187 |
new ServiceId("Cipher", "RSA//PKCS1Padding"), |
|
188 |
new ServiceId("Cipher", "RSA"), |
|
189 |
} |
|
190 |
); |
|
191 |
||
192 |
/** |
|
193 |
* Returns a Signature object that implements the specified signature |
|
194 |
* algorithm. |
|
195 |
* |
|
196 |
* <p> This method traverses the list of registered security Providers, |
|
197 |
* starting with the most preferred Provider. |
|
198 |
* A new Signature object encapsulating the |
|
199 |
* SignatureSpi implementation from the first |
|
200 |
* Provider that supports the specified algorithm is returned. |
|
201 |
* |
|
202 |
* <p> Note that the list of registered providers may be retrieved via |
|
203 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
204 |
* |
|
205 |
* @param algorithm the standard name of the algorithm requested. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
206 |
* See the Signature section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
207 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
208 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 209 |
* for information about standard algorithm names. |
210 |
* |
|
211 |
* @return the new Signature object. |
|
212 |
* |
|
213 |
* @exception NoSuchAlgorithmException if no Provider supports a |
|
214 |
* Signature implementation for the |
|
215 |
* specified algorithm. |
|
216 |
* |
|
217 |
* @see Provider |
|
218 |
*/ |
|
219 |
public static Signature getInstance(String algorithm) |
|
220 |
throws NoSuchAlgorithmException { |
|
221 |
List<Service> list; |
|
222 |
if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) { |
|
223 |
list = GetInstance.getServices(rsaIds); |
|
224 |
} else { |
|
225 |
list = GetInstance.getServices("Signature", algorithm); |
|
226 |
} |
|
227 |
Iterator<Service> t = list.iterator(); |
|
228 |
if (t.hasNext() == false) { |
|
229 |
throw new NoSuchAlgorithmException |
|
230 |
(algorithm + " Signature not available"); |
|
231 |
} |
|
232 |
// try services until we find an Spi or a working Signature subclass |
|
233 |
NoSuchAlgorithmException failure; |
|
234 |
do { |
|
235 |
Service s = t.next(); |
|
236 |
if (isSpi(s)) { |
|
237 |
return new Delegate(s, t, algorithm); |
|
238 |
} else { |
|
239 |
// must be a subclass of Signature, disable dynamic selection |
|
240 |
try { |
|
241 |
Instance instance = |
|
242 |
GetInstance.getInstance(s, SignatureSpi.class); |
|
243 |
return getInstance(instance, algorithm); |
|
244 |
} catch (NoSuchAlgorithmException e) { |
|
245 |
failure = e; |
|
246 |
} |
|
247 |
} |
|
248 |
} while (t.hasNext()); |
|
249 |
throw failure; |
|
250 |
} |
|
251 |
||
252 |
private static Signature getInstance(Instance instance, String algorithm) { |
|
253 |
Signature sig; |
|
254 |
if (instance.impl instanceof Signature) { |
|
255 |
sig = (Signature)instance.impl; |
|
18181
4f0a36582461
8014620: Signature.getAlgorithm return null in special case
youdwei
parents:
12875
diff
changeset
|
256 |
sig.algorithm = algorithm; |
2 | 257 |
} else { |
258 |
SignatureSpi spi = (SignatureSpi)instance.impl; |
|
259 |
sig = new Delegate(spi, algorithm); |
|
260 |
} |
|
261 |
sig.provider = instance.provider; |
|
262 |
return sig; |
|
263 |
} |
|
264 |
||
265 |
private final static Map<String,Boolean> signatureInfo; |
|
266 |
||
267 |
static { |
|
268 |
signatureInfo = new ConcurrentHashMap<String,Boolean>(); |
|
269 |
Boolean TRUE = Boolean.TRUE; |
|
270 |
// pre-initialize with values for our SignatureSpi implementations |
|
271 |
signatureInfo.put("sun.security.provider.DSA$RawDSA", TRUE); |
|
272 |
signatureInfo.put("sun.security.provider.DSA$SHA1withDSA", TRUE); |
|
273 |
signatureInfo.put("sun.security.rsa.RSASignature$MD2withRSA", TRUE); |
|
274 |
signatureInfo.put("sun.security.rsa.RSASignature$MD5withRSA", TRUE); |
|
275 |
signatureInfo.put("sun.security.rsa.RSASignature$SHA1withRSA", TRUE); |
|
276 |
signatureInfo.put("sun.security.rsa.RSASignature$SHA256withRSA", TRUE); |
|
277 |
signatureInfo.put("sun.security.rsa.RSASignature$SHA384withRSA", TRUE); |
|
278 |
signatureInfo.put("sun.security.rsa.RSASignature$SHA512withRSA", TRUE); |
|
279 |
signatureInfo.put("com.sun.net.ssl.internal.ssl.RSASignature", TRUE); |
|
280 |
signatureInfo.put("sun.security.pkcs11.P11Signature", TRUE); |
|
281 |
} |
|
282 |
||
283 |
private static boolean isSpi(Service s) { |
|
284 |
if (s.getType().equals("Cipher")) { |
|
285 |
// must be a CipherSpi, which we can wrap with the CipherAdapter |
|
286 |
return true; |
|
287 |
} |
|
288 |
String className = s.getClassName(); |
|
289 |
Boolean result = signatureInfo.get(className); |
|
290 |
if (result == null) { |
|
291 |
try { |
|
292 |
Object instance = s.newInstance(null); |
|
293 |
// Signature extends SignatureSpi |
|
294 |
// so it is a "real" Spi if it is an |
|
295 |
// instance of SignatureSpi but not Signature |
|
296 |
boolean r = (instance instanceof SignatureSpi) |
|
297 |
&& (instance instanceof Signature == false); |
|
298 |
if ((debug != null) && (r == false)) { |
|
299 |
debug.println("Not a SignatureSpi " + className); |
|
300 |
debug.println("Delayed provider selection may not be " |
|
301 |
+ "available for algorithm " + s.getAlgorithm()); |
|
302 |
} |
|
303 |
result = Boolean.valueOf(r); |
|
304 |
signatureInfo.put(className, result); |
|
305 |
} catch (Exception e) { |
|
306 |
// something is wrong, assume not an SPI |
|
307 |
return false; |
|
308 |
} |
|
309 |
} |
|
310 |
return result.booleanValue(); |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Returns a Signature object that implements the specified signature |
|
315 |
* algorithm. |
|
316 |
* |
|
317 |
* <p> A new Signature object encapsulating the |
|
318 |
* SignatureSpi implementation from the specified provider |
|
319 |
* is returned. The specified provider must be registered |
|
320 |
* in the security provider list. |
|
321 |
* |
|
322 |
* <p> Note that the list of registered providers may be retrieved via |
|
323 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
324 |
* |
|
325 |
* @param algorithm the name of the algorithm requested. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
326 |
* See the Signature section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
327 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
328 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 329 |
* for information about standard algorithm names. |
330 |
* |
|
331 |
* @param provider the name of the provider. |
|
332 |
* |
|
333 |
* @return the new Signature object. |
|
334 |
* |
|
335 |
* @exception NoSuchAlgorithmException if a SignatureSpi |
|
336 |
* implementation for the specified algorithm is not |
|
337 |
* available from the specified provider. |
|
338 |
* |
|
339 |
* @exception NoSuchProviderException if the specified provider is not |
|
340 |
* registered in the security provider list. |
|
341 |
* |
|
342 |
* @exception IllegalArgumentException if the provider name is null |
|
343 |
* or empty. |
|
344 |
* |
|
345 |
* @see Provider |
|
346 |
*/ |
|
347 |
public static Signature getInstance(String algorithm, String provider) |
|
348 |
throws NoSuchAlgorithmException, NoSuchProviderException { |
|
349 |
if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) { |
|
350 |
// exception compatibility with existing code |
|
351 |
if ((provider == null) || (provider.length() == 0)) { |
|
352 |
throw new IllegalArgumentException("missing provider"); |
|
353 |
} |
|
354 |
Provider p = Security.getProvider(provider); |
|
355 |
if (p == null) { |
|
356 |
throw new NoSuchProviderException |
|
357 |
("no such provider: " + provider); |
|
358 |
} |
|
359 |
return getInstanceRSA(p); |
|
360 |
} |
|
361 |
Instance instance = GetInstance.getInstance |
|
362 |
("Signature", SignatureSpi.class, algorithm, provider); |
|
363 |
return getInstance(instance, algorithm); |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Returns a Signature object that implements the specified |
|
368 |
* signature algorithm. |
|
369 |
* |
|
370 |
* <p> A new Signature object encapsulating the |
|
371 |
* SignatureSpi implementation from the specified Provider |
|
372 |
* object is returned. Note that the specified Provider object |
|
373 |
* does not have to be registered in the provider list. |
|
374 |
* |
|
375 |
* @param algorithm the name of the algorithm requested. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
376 |
* See the Signature section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
377 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
378 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 379 |
* for information about standard algorithm names. |
380 |
* |
|
381 |
* @param provider the provider. |
|
382 |
* |
|
383 |
* @return the new Signature object. |
|
384 |
* |
|
385 |
* @exception NoSuchAlgorithmException if a SignatureSpi |
|
386 |
* implementation for the specified algorithm is not available |
|
387 |
* from the specified Provider object. |
|
388 |
* |
|
389 |
* @exception IllegalArgumentException if the provider is null. |
|
390 |
* |
|
391 |
* @see Provider |
|
392 |
* |
|
393 |
* @since 1.4 |
|
394 |
*/ |
|
395 |
public static Signature getInstance(String algorithm, Provider provider) |
|
396 |
throws NoSuchAlgorithmException { |
|
397 |
if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) { |
|
398 |
// exception compatibility with existing code |
|
399 |
if (provider == null) { |
|
400 |
throw new IllegalArgumentException("missing provider"); |
|
401 |
} |
|
402 |
return getInstanceRSA(provider); |
|
403 |
} |
|
404 |
Instance instance = GetInstance.getInstance |
|
405 |
("Signature", SignatureSpi.class, algorithm, provider); |
|
406 |
return getInstance(instance, algorithm); |
|
407 |
} |
|
408 |
||
409 |
// return an implementation for NONEwithRSA, which is a special case |
|
410 |
// because of the Cipher.RSA/ECB/PKCS1Padding compatibility wrapper |
|
411 |
private static Signature getInstanceRSA(Provider p) |
|
412 |
throws NoSuchAlgorithmException { |
|
413 |
// try Signature first |
|
414 |
Service s = p.getService("Signature", RSA_SIGNATURE); |
|
415 |
if (s != null) { |
|
416 |
Instance instance = GetInstance.getInstance(s, SignatureSpi.class); |
|
417 |
return getInstance(instance, RSA_SIGNATURE); |
|
418 |
} |
|
419 |
// check Cipher |
|
420 |
try { |
|
421 |
Cipher c = Cipher.getInstance(RSA_CIPHER, p); |
|
422 |
return new Delegate(new CipherAdapter(c), RSA_SIGNATURE); |
|
423 |
} catch (GeneralSecurityException e) { |
|
424 |
// throw Signature style exception message to avoid confusion, |
|
425 |
// but append Cipher exception as cause |
|
426 |
throw new NoSuchAlgorithmException("no such algorithm: " |
|
427 |
+ RSA_SIGNATURE + " for provider " + p.getName(), e); |
|
428 |
} |
|
429 |
} |
|
430 |
||
431 |
/** |
|
432 |
* Returns the provider of this signature object. |
|
433 |
* |
|
434 |
* @return the provider of this signature object |
|
435 |
*/ |
|
436 |
public final Provider getProvider() { |
|
437 |
chooseFirstProvider(); |
|
438 |
return this.provider; |
|
439 |
} |
|
440 |
||
441 |
void chooseFirstProvider() { |
|
442 |
// empty, overridden in Delegate |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* Initializes this object for verification. If this method is called |
|
447 |
* again with a different argument, it negates the effect |
|
448 |
* of this call. |
|
449 |
* |
|
450 |
* @param publicKey the public key of the identity whose signature is |
|
451 |
* going to be verified. |
|
452 |
* |
|
453 |
* @exception InvalidKeyException if the key is invalid. |
|
454 |
*/ |
|
455 |
public final void initVerify(PublicKey publicKey) |
|
456 |
throws InvalidKeyException { |
|
457 |
engineInitVerify(publicKey); |
|
458 |
state = VERIFY; |
|
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
459 |
|
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
460 |
if (!skipDebug && pdebug != null) { |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
461 |
pdebug.println("Signature." + algorithm + |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
462 |
" verification algorithm from: " + this.provider.getName()); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
463 |
} |
2 | 464 |
} |
465 |
||
466 |
/** |
|
467 |
* Initializes this object for verification, using the public key from |
|
468 |
* the given certificate. |
|
469 |
* <p>If the certificate is of type X.509 and has a <i>key usage</i> |
|
470 |
* extension field marked as critical, and the value of the <i>key usage</i> |
|
471 |
* extension field implies that the public key in |
|
472 |
* the certificate and its corresponding private key are not |
|
473 |
* supposed to be used for digital signatures, an |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
474 |
* {@code InvalidKeyException} is thrown. |
2 | 475 |
* |
476 |
* @param certificate the certificate of the identity whose signature is |
|
477 |
* going to be verified. |
|
478 |
* |
|
479 |
* @exception InvalidKeyException if the public key in the certificate |
|
480 |
* is not encoded properly or does not include required parameter |
|
481 |
* information or cannot be used for digital signature purposes. |
|
482 |
* @since 1.3 |
|
483 |
*/ |
|
484 |
public final void initVerify(Certificate certificate) |
|
485 |
throws InvalidKeyException { |
|
486 |
// If the certificate is of type X509Certificate, |
|
487 |
// we should check whether it has a Key Usage |
|
488 |
// extension marked as critical. |
|
489 |
if (certificate instanceof java.security.cert.X509Certificate) { |
|
490 |
// Check whether the cert has a key usage extension |
|
491 |
// marked as a critical extension. |
|
492 |
// The OID for KeyUsage extension is 2.5.29.15. |
|
493 |
X509Certificate cert = (X509Certificate)certificate; |
|
494 |
Set<String> critSet = cert.getCriticalExtensionOIDs(); |
|
495 |
||
496 |
if (critSet != null && !critSet.isEmpty() |
|
497 |
&& critSet.contains("2.5.29.15")) { |
|
498 |
boolean[] keyUsageInfo = cert.getKeyUsage(); |
|
499 |
// keyUsageInfo[0] is for digitalSignature. |
|
500 |
if ((keyUsageInfo != null) && (keyUsageInfo[0] == false)) |
|
501 |
throw new InvalidKeyException("Wrong key usage"); |
|
502 |
} |
|
503 |
} |
|
504 |
||
505 |
PublicKey publicKey = certificate.getPublicKey(); |
|
506 |
engineInitVerify(publicKey); |
|
507 |
state = VERIFY; |
|
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
508 |
|
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
509 |
if (!skipDebug && pdebug != null) { |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
510 |
pdebug.println("Signature." + algorithm + |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
511 |
" verification algorithm from: " + this.provider.getName()); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
512 |
} |
2 | 513 |
} |
514 |
||
515 |
/** |
|
516 |
* Initialize this object for signing. If this method is called |
|
517 |
* again with a different argument, it negates the effect |
|
518 |
* of this call. |
|
519 |
* |
|
520 |
* @param privateKey the private key of the identity whose signature |
|
521 |
* is going to be generated. |
|
522 |
* |
|
523 |
* @exception InvalidKeyException if the key is invalid. |
|
524 |
*/ |
|
525 |
public final void initSign(PrivateKey privateKey) |
|
526 |
throws InvalidKeyException { |
|
527 |
engineInitSign(privateKey); |
|
528 |
state = SIGN; |
|
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
529 |
|
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
530 |
if (!skipDebug && pdebug != null) { |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
531 |
pdebug.println("Signature." + algorithm + |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
532 |
" signing algorithm from: " + this.provider.getName()); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
533 |
} |
2 | 534 |
} |
535 |
||
536 |
/** |
|
537 |
* Initialize this object for signing. If this method is called |
|
538 |
* again with a different argument, it negates the effect |
|
539 |
* of this call. |
|
540 |
* |
|
541 |
* @param privateKey the private key of the identity whose signature |
|
542 |
* is going to be generated. |
|
543 |
* |
|
544 |
* @param random the source of randomness for this signature. |
|
545 |
* |
|
546 |
* @exception InvalidKeyException if the key is invalid. |
|
547 |
*/ |
|
548 |
public final void initSign(PrivateKey privateKey, SecureRandom random) |
|
549 |
throws InvalidKeyException { |
|
550 |
engineInitSign(privateKey, random); |
|
551 |
state = SIGN; |
|
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
552 |
|
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
553 |
if (!skipDebug && pdebug != null) { |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
554 |
pdebug.println("Signature." + algorithm + |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
555 |
" signing algorithm from: " + this.provider.getName()); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
556 |
} |
2 | 557 |
} |
558 |
||
559 |
/** |
|
560 |
* Returns the signature bytes of all the data updated. |
|
561 |
* The format of the signature depends on the underlying |
|
562 |
* signature scheme. |
|
563 |
* |
|
564 |
* <p>A call to this method resets this signature object to the state |
|
565 |
* it was in when previously initialized for signing via a |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
566 |
* call to {@code initSign(PrivateKey)}. That is, the object is |
2 | 567 |
* reset and available to generate another signature from the same |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
568 |
* signer, if desired, via new calls to {@code update} and |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
569 |
* {@code sign}. |
2 | 570 |
* |
571 |
* @return the signature bytes of the signing operation's result. |
|
572 |
* |
|
573 |
* @exception SignatureException if this signature object is not |
|
574 |
* initialized properly or if this signature algorithm is unable to |
|
575 |
* process the input data provided. |
|
576 |
*/ |
|
577 |
public final byte[] sign() throws SignatureException { |
|
578 |
if (state == SIGN) { |
|
579 |
return engineSign(); |
|
580 |
} |
|
581 |
throw new SignatureException("object not initialized for " + |
|
582 |
"signing"); |
|
583 |
} |
|
584 |
||
585 |
/** |
|
586 |
* Finishes the signature operation and stores the resulting signature |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
587 |
* bytes in the provided buffer {@code outbuf}, starting at |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
588 |
* {@code offset}. |
2 | 589 |
* The format of the signature depends on the underlying |
590 |
* signature scheme. |
|
591 |
* |
|
592 |
* <p>This signature object is reset to its initial state (the state it |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
593 |
* was in after a call to one of the {@code initSign} methods) and |
2 | 594 |
* can be reused to generate further signatures with the same private key. |
595 |
* |
|
596 |
* @param outbuf buffer for the signature result. |
|
597 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
598 |
* @param offset offset into {@code outbuf} where the signature is |
2 | 599 |
* stored. |
600 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
601 |
* @param len number of bytes within {@code outbuf} allotted for the |
2 | 602 |
* signature. |
603 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
604 |
* @return the number of bytes placed into {@code outbuf}. |
2 | 605 |
* |
606 |
* @exception SignatureException if this signature object is not |
|
607 |
* initialized properly, if this signature algorithm is unable to |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
608 |
* process the input data provided, or if {@code len} is less |
2 | 609 |
* than the actual signature length. |
610 |
* |
|
611 |
* @since 1.2 |
|
612 |
*/ |
|
613 |
public final int sign(byte[] outbuf, int offset, int len) |
|
614 |
throws SignatureException { |
|
615 |
if (outbuf == null) { |
|
616 |
throw new IllegalArgumentException("No output buffer given"); |
|
617 |
} |
|
618 |
if (outbuf.length - offset < len) { |
|
619 |
throw new IllegalArgumentException |
|
620 |
("Output buffer too small for specified offset and length"); |
|
621 |
} |
|
622 |
if (state != SIGN) { |
|
623 |
throw new SignatureException("object not initialized for " + |
|
624 |
"signing"); |
|
625 |
} |
|
626 |
return engineSign(outbuf, offset, len); |
|
627 |
} |
|
628 |
||
629 |
/** |
|
630 |
* Verifies the passed-in signature. |
|
631 |
* |
|
632 |
* <p>A call to this method resets this signature object to the state |
|
633 |
* it was in when previously initialized for verification via a |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
634 |
* call to {@code initVerify(PublicKey)}. That is, the object is |
2 | 635 |
* reset and available to verify another signature from the identity |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
636 |
* whose public key was specified in the call to {@code initVerify}. |
2 | 637 |
* |
638 |
* @param signature the signature bytes to be verified. |
|
639 |
* |
|
640 |
* @return true if the signature was verified, false if not. |
|
641 |
* |
|
642 |
* @exception SignatureException if this signature object is not |
|
643 |
* initialized properly, the passed-in signature is improperly |
|
644 |
* encoded or of the wrong type, if this signature algorithm is unable to |
|
645 |
* process the input data provided, etc. |
|
646 |
*/ |
|
647 |
public final boolean verify(byte[] signature) throws SignatureException { |
|
648 |
if (state == VERIFY) { |
|
649 |
return engineVerify(signature); |
|
650 |
} |
|
651 |
throw new SignatureException("object not initialized for " + |
|
652 |
"verification"); |
|
653 |
} |
|
654 |
||
655 |
/** |
|
656 |
* Verifies the passed-in signature in the specified array |
|
657 |
* of bytes, starting at the specified offset. |
|
658 |
* |
|
659 |
* <p>A call to this method resets this signature object to the state |
|
660 |
* it was in when previously initialized for verification via a |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
661 |
* call to {@code initVerify(PublicKey)}. That is, the object is |
2 | 662 |
* reset and available to verify another signature from the identity |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
663 |
* whose public key was specified in the call to {@code initVerify}. |
2 | 664 |
* |
665 |
* |
|
666 |
* @param signature the signature bytes to be verified. |
|
667 |
* @param offset the offset to start from in the array of bytes. |
|
668 |
* @param length the number of bytes to use, starting at offset. |
|
669 |
* |
|
670 |
* @return true if the signature was verified, false if not. |
|
671 |
* |
|
672 |
* @exception SignatureException if this signature object is not |
|
673 |
* initialized properly, the passed-in signature is improperly |
|
674 |
* encoded or of the wrong type, if this signature algorithm is unable to |
|
675 |
* process the input data provided, etc. |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
676 |
* @exception IllegalArgumentException if the {@code signature} |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
677 |
* byte array is null, or the {@code offset} or {@code length} |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
678 |
* is less than 0, or the sum of the {@code offset} and |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
679 |
* {@code length} is greater than the length of the |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
680 |
* {@code signature} byte array. |
2 | 681 |
* @since 1.4 |
682 |
*/ |
|
683 |
public final boolean verify(byte[] signature, int offset, int length) |
|
684 |
throws SignatureException { |
|
685 |
if (state == VERIFY) { |
|
686 |
if ((signature == null) || (offset < 0) || (length < 0) || |
|
12875
43a1cd4cfc32
7172149: ArrayIndexOutOfBoundsException from Signature.verify
xuelei
parents:
10709
diff
changeset
|
687 |
(length > signature.length - offset)) { |
2 | 688 |
throw new IllegalArgumentException("Bad arguments"); |
689 |
} |
|
690 |
||
691 |
return engineVerify(signature, offset, length); |
|
692 |
} |
|
693 |
throw new SignatureException("object not initialized for " + |
|
694 |
"verification"); |
|
695 |
} |
|
696 |
||
697 |
/** |
|
698 |
* Updates the data to be signed or verified by a byte. |
|
699 |
* |
|
700 |
* @param b the byte to use for the update. |
|
701 |
* |
|
702 |
* @exception SignatureException if this signature object is not |
|
703 |
* initialized properly. |
|
704 |
*/ |
|
705 |
public final void update(byte b) throws SignatureException { |
|
706 |
if (state == VERIFY || state == SIGN) { |
|
707 |
engineUpdate(b); |
|
708 |
} else { |
|
709 |
throw new SignatureException("object not initialized for " |
|
710 |
+ "signature or verification"); |
|
711 |
} |
|
712 |
} |
|
713 |
||
714 |
/** |
|
715 |
* Updates the data to be signed or verified, using the specified |
|
716 |
* array of bytes. |
|
717 |
* |
|
718 |
* @param data the byte array to use for the update. |
|
719 |
* |
|
720 |
* @exception SignatureException if this signature object is not |
|
721 |
* initialized properly. |
|
722 |
*/ |
|
723 |
public final void update(byte[] data) throws SignatureException { |
|
724 |
update(data, 0, data.length); |
|
725 |
} |
|
726 |
||
727 |
/** |
|
728 |
* Updates the data to be signed or verified, using the specified |
|
729 |
* array of bytes, starting at the specified offset. |
|
730 |
* |
|
731 |
* @param data the array of bytes. |
|
732 |
* @param off the offset to start from in the array of bytes. |
|
733 |
* @param len the number of bytes to use, starting at offset. |
|
734 |
* |
|
735 |
* @exception SignatureException if this signature object is not |
|
736 |
* initialized properly. |
|
737 |
*/ |
|
738 |
public final void update(byte[] data, int off, int len) |
|
739 |
throws SignatureException { |
|
740 |
if (state == SIGN || state == VERIFY) { |
|
741 |
engineUpdate(data, off, len); |
|
742 |
} else { |
|
743 |
throw new SignatureException("object not initialized for " |
|
744 |
+ "signature or verification"); |
|
745 |
} |
|
746 |
} |
|
747 |
||
748 |
/** |
|
749 |
* Updates the data to be signed or verified using the specified |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
750 |
* ByteBuffer. Processes the {@code data.remaining()} bytes |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
751 |
* starting at at {@code data.position()}. |
2 | 752 |
* Upon return, the buffer's position will be equal to its limit; |
753 |
* its limit will not have changed. |
|
754 |
* |
|
755 |
* @param data the ByteBuffer |
|
756 |
* |
|
757 |
* @exception SignatureException if this signature object is not |
|
758 |
* initialized properly. |
|
759 |
* @since 1.5 |
|
760 |
*/ |
|
761 |
public final void update(ByteBuffer data) throws SignatureException { |
|
762 |
if ((state != SIGN) && (state != VERIFY)) { |
|
763 |
throw new SignatureException("object not initialized for " |
|
764 |
+ "signature or verification"); |
|
765 |
} |
|
766 |
if (data == null) { |
|
767 |
throw new NullPointerException(); |
|
768 |
} |
|
769 |
engineUpdate(data); |
|
770 |
} |
|
771 |
||
772 |
/** |
|
773 |
* Returns the name of the algorithm for this signature object. |
|
774 |
* |
|
775 |
* @return the name of the algorithm for this signature object. |
|
776 |
*/ |
|
777 |
public final String getAlgorithm() { |
|
778 |
return this.algorithm; |
|
779 |
} |
|
780 |
||
781 |
/** |
|
782 |
* Returns a string representation of this signature object, |
|
783 |
* providing information that includes the state of the object |
|
784 |
* and the name of the algorithm used. |
|
785 |
* |
|
786 |
* @return a string representation of this signature object. |
|
787 |
*/ |
|
788 |
public String toString() { |
|
789 |
String initState = ""; |
|
790 |
switch (state) { |
|
791 |
case UNINITIALIZED: |
|
792 |
initState = "<not initialized>"; |
|
793 |
break; |
|
794 |
case VERIFY: |
|
795 |
initState = "<initialized for verifying>"; |
|
796 |
break; |
|
797 |
case SIGN: |
|
798 |
initState = "<initialized for signing>"; |
|
799 |
break; |
|
800 |
} |
|
801 |
return "Signature object: " + getAlgorithm() + initState; |
|
802 |
} |
|
803 |
||
804 |
/** |
|
805 |
* Sets the specified algorithm parameter to the specified value. |
|
806 |
* This method supplies a general-purpose mechanism through |
|
807 |
* which it is possible to set the various parameters of this object. |
|
808 |
* A parameter may be any settable parameter for the algorithm, such as |
|
809 |
* a parameter size, or a source of random bits for signature generation |
|
810 |
* (if appropriate), or an indication of whether or not to perform |
|
811 |
* a specific but optional computation. A uniform algorithm-specific |
|
812 |
* naming scheme for each parameter is desirable but left unspecified |
|
813 |
* at this time. |
|
814 |
* |
|
815 |
* @param param the string identifier of the parameter. |
|
816 |
* @param value the parameter value. |
|
817 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
818 |
* @exception InvalidParameterException if {@code param} is an |
2 | 819 |
* invalid parameter for this signature algorithm engine, |
820 |
* the parameter is already set |
|
821 |
* and cannot be set again, a security exception occurs, and so on. |
|
822 |
* |
|
823 |
* @see #getParameter |
|
824 |
* |
|
825 |
* @deprecated Use |
|
826 |
* {@link #setParameter(java.security.spec.AlgorithmParameterSpec) |
|
827 |
* setParameter}. |
|
828 |
*/ |
|
829 |
@Deprecated |
|
830 |
public final void setParameter(String param, Object value) |
|
831 |
throws InvalidParameterException { |
|
832 |
engineSetParameter(param, value); |
|
833 |
} |
|
834 |
||
835 |
/** |
|
836 |
* Initializes this signature engine with the specified parameter set. |
|
837 |
* |
|
838 |
* @param params the parameters |
|
839 |
* |
|
840 |
* @exception InvalidAlgorithmParameterException if the given parameters |
|
841 |
* are inappropriate for this signature engine |
|
842 |
* |
|
843 |
* @see #getParameters |
|
844 |
*/ |
|
845 |
public final void setParameter(AlgorithmParameterSpec params) |
|
846 |
throws InvalidAlgorithmParameterException { |
|
847 |
engineSetParameter(params); |
|
848 |
} |
|
849 |
||
850 |
/** |
|
851 |
* Returns the parameters used with this signature object. |
|
852 |
* |
|
853 |
* <p>The returned parameters may be the same that were used to initialize |
|
854 |
* this signature, or may contain a combination of default and randomly |
|
855 |
* generated parameter values used by the underlying signature |
|
856 |
* implementation if this signature requires algorithm parameters but |
|
857 |
* was not initialized with any. |
|
858 |
* |
|
859 |
* @return the parameters used with this signature, or null if this |
|
860 |
* signature does not use any parameters. |
|
861 |
* |
|
862 |
* @see #setParameter(AlgorithmParameterSpec) |
|
863 |
* @since 1.4 |
|
864 |
*/ |
|
865 |
public final AlgorithmParameters getParameters() { |
|
866 |
return engineGetParameters(); |
|
867 |
} |
|
868 |
||
869 |
/** |
|
870 |
* Gets the value of the specified algorithm parameter. This method |
|
871 |
* supplies a general-purpose mechanism through which it is possible to |
|
872 |
* get the various parameters of this object. A parameter may be any |
|
873 |
* settable parameter for the algorithm, such as a parameter size, or |
|
874 |
* a source of random bits for signature generation (if appropriate), |
|
875 |
* or an indication of whether or not to perform a specific but optional |
|
876 |
* computation. A uniform algorithm-specific naming scheme for each |
|
877 |
* parameter is desirable but left unspecified at this time. |
|
878 |
* |
|
879 |
* @param param the string name of the parameter. |
|
880 |
* |
|
881 |
* @return the object that represents the parameter value, or null if |
|
882 |
* there is none. |
|
883 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
884 |
* @exception InvalidParameterException if {@code param} is an invalid |
2 | 885 |
* parameter for this engine, or another exception occurs while |
886 |
* trying to get this parameter. |
|
887 |
* |
|
888 |
* @see #setParameter(String, Object) |
|
889 |
* |
|
890 |
* @deprecated |
|
891 |
*/ |
|
892 |
@Deprecated |
|
893 |
public final Object getParameter(String param) |
|
894 |
throws InvalidParameterException { |
|
895 |
return engineGetParameter(param); |
|
896 |
} |
|
897 |
||
898 |
/** |
|
899 |
* Returns a clone if the implementation is cloneable. |
|
900 |
* |
|
901 |
* @return a clone if the implementation is cloneable. |
|
902 |
* |
|
903 |
* @exception CloneNotSupportedException if this is called |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
904 |
* on an implementation that does not support {@code Cloneable}. |
2 | 905 |
*/ |
906 |
public Object clone() throws CloneNotSupportedException { |
|
907 |
if (this instanceof Cloneable) { |
|
908 |
return super.clone(); |
|
909 |
} else { |
|
910 |
throw new CloneNotSupportedException(); |
|
911 |
} |
|
912 |
} |
|
913 |
||
914 |
/* |
|
915 |
* The following class allows providers to extend from SignatureSpi |
|
916 |
* rather than from Signature. It represents a Signature with an |
|
917 |
* encapsulated, provider-supplied SPI object (of type SignatureSpi). |
|
918 |
* If the provider implementation is an instance of SignatureSpi, the |
|
919 |
* getInstance() methods above return an instance of this class, with |
|
920 |
* the SPI object encapsulated. |
|
921 |
* |
|
922 |
* Note: All SPI methods from the original Signature class have been |
|
923 |
* moved up the hierarchy into a new class (SignatureSpi), which has |
|
924 |
* been interposed in the hierarchy between the API (Signature) |
|
925 |
* and its original parent (Object). |
|
926 |
*/ |
|
927 |
||
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
928 |
@SuppressWarnings("deprecation") |
2 | 929 |
private static class Delegate extends Signature { |
930 |
||
931 |
// The provider implementation (delegate) |
|
932 |
// filled in once the provider is selected |
|
933 |
private SignatureSpi sigSpi; |
|
934 |
||
935 |
// lock for mutex during provider selection |
|
936 |
private final Object lock; |
|
937 |
||
938 |
// next service to try in provider selection |
|
939 |
// null once provider is selected |
|
940 |
private Service firstService; |
|
941 |
||
942 |
// remaining services to try in provider selection |
|
943 |
// null once provider is selected |
|
944 |
private Iterator<Service> serviceIterator; |
|
945 |
||
946 |
// constructor |
|
947 |
Delegate(SignatureSpi sigSpi, String algorithm) { |
|
948 |
super(algorithm); |
|
949 |
this.sigSpi = sigSpi; |
|
950 |
this.lock = null; // no lock needed |
|
951 |
} |
|
952 |
||
953 |
// used with delayed provider selection |
|
954 |
Delegate(Service service, |
|
955 |
Iterator<Service> iterator, String algorithm) { |
|
956 |
super(algorithm); |
|
957 |
this.firstService = service; |
|
958 |
this.serviceIterator = iterator; |
|
959 |
this.lock = new Object(); |
|
960 |
} |
|
961 |
||
962 |
/** |
|
963 |
* Returns a clone if the delegate is cloneable. |
|
964 |
* |
|
965 |
* @return a clone if the delegate is cloneable. |
|
966 |
* |
|
967 |
* @exception CloneNotSupportedException if this is called on a |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
968 |
* delegate that does not support {@code Cloneable}. |
2 | 969 |
*/ |
970 |
public Object clone() throws CloneNotSupportedException { |
|
971 |
chooseFirstProvider(); |
|
972 |
if (sigSpi instanceof Cloneable) { |
|
973 |
SignatureSpi sigSpiClone = (SignatureSpi)sigSpi.clone(); |
|
974 |
// Because 'algorithm' and 'provider' are private |
|
975 |
// members of our supertype, we must perform a cast to |
|
976 |
// access them. |
|
977 |
Signature that = |
|
978 |
new Delegate(sigSpiClone, ((Signature)this).algorithm); |
|
979 |
that.provider = ((Signature)this).provider; |
|
980 |
return that; |
|
981 |
} else { |
|
982 |
throw new CloneNotSupportedException(); |
|
983 |
} |
|
984 |
} |
|
985 |
||
986 |
private static SignatureSpi newInstance(Service s) |
|
987 |
throws NoSuchAlgorithmException { |
|
988 |
if (s.getType().equals("Cipher")) { |
|
989 |
// must be NONEwithRSA |
|
990 |
try { |
|
991 |
Cipher c = Cipher.getInstance(RSA_CIPHER, s.getProvider()); |
|
992 |
return new CipherAdapter(c); |
|
993 |
} catch (NoSuchPaddingException e) { |
|
994 |
throw new NoSuchAlgorithmException(e); |
|
995 |
} |
|
996 |
} else { |
|
997 |
Object o = s.newInstance(null); |
|
998 |
if (o instanceof SignatureSpi == false) { |
|
999 |
throw new NoSuchAlgorithmException |
|
1000 |
("Not a SignatureSpi: " + o.getClass().getName()); |
|
1001 |
} |
|
1002 |
return (SignatureSpi)o; |
|
1003 |
} |
|
1004 |
} |
|
1005 |
||
1006 |
// max number of debug warnings to print from chooseFirstProvider() |
|
1007 |
private static int warnCount = 10; |
|
1008 |
||
1009 |
/** |
|
1010 |
* Choose the Spi from the first provider available. Used if |
|
1011 |
* delayed provider selection is not possible because initSign()/ |
|
1012 |
* initVerify() is not the first method called. |
|
1013 |
*/ |
|
1014 |
void chooseFirstProvider() { |
|
1015 |
if (sigSpi != null) { |
|
1016 |
return; |
|
1017 |
} |
|
1018 |
synchronized (lock) { |
|
1019 |
if (sigSpi != null) { |
|
1020 |
return; |
|
1021 |
} |
|
1022 |
if (debug != null) { |
|
1023 |
int w = --warnCount; |
|
1024 |
if (w >= 0) { |
|
1025 |
debug.println("Signature.init() not first method " |
|
1026 |
+ "called, disabling delayed provider selection"); |
|
1027 |
if (w == 0) { |
|
1028 |
debug.println("Further warnings of this type will " |
|
1029 |
+ "be suppressed"); |
|
1030 |
} |
|
1031 |
new Exception("Call trace").printStackTrace(); |
|
1032 |
} |
|
1033 |
} |
|
1034 |
Exception lastException = null; |
|
1035 |
while ((firstService != null) || serviceIterator.hasNext()) { |
|
1036 |
Service s; |
|
1037 |
if (firstService != null) { |
|
1038 |
s = firstService; |
|
1039 |
firstService = null; |
|
1040 |
} else { |
|
1041 |
s = serviceIterator.next(); |
|
1042 |
} |
|
1043 |
if (isSpi(s) == false) { |
|
1044 |
continue; |
|
1045 |
} |
|
1046 |
try { |
|
1047 |
sigSpi = newInstance(s); |
|
1048 |
provider = s.getProvider(); |
|
1049 |
// not needed any more |
|
1050 |
firstService = null; |
|
1051 |
serviceIterator = null; |
|
1052 |
return; |
|
1053 |
} catch (NoSuchAlgorithmException e) { |
|
1054 |
lastException = e; |
|
1055 |
} |
|
1056 |
} |
|
1057 |
ProviderException e = new ProviderException |
|
1058 |
("Could not construct SignatureSpi instance"); |
|
1059 |
if (lastException != null) { |
|
1060 |
e.initCause(lastException); |
|
1061 |
} |
|
1062 |
throw e; |
|
1063 |
} |
|
1064 |
} |
|
1065 |
||
1066 |
private void chooseProvider(int type, Key key, SecureRandom random) |
|
1067 |
throws InvalidKeyException { |
|
1068 |
synchronized (lock) { |
|
1069 |
if (sigSpi != null) { |
|
1070 |
init(sigSpi, type, key, random); |
|
1071 |
return; |
|
1072 |
} |
|
1073 |
Exception lastException = null; |
|
1074 |
while ((firstService != null) || serviceIterator.hasNext()) { |
|
1075 |
Service s; |
|
1076 |
if (firstService != null) { |
|
1077 |
s = firstService; |
|
1078 |
firstService = null; |
|
1079 |
} else { |
|
1080 |
s = serviceIterator.next(); |
|
1081 |
} |
|
1082 |
// if provider says it does not support this key, ignore it |
|
1083 |
if (s.supportsParameter(key) == false) { |
|
1084 |
continue; |
|
1085 |
} |
|
1086 |
// if instance is not a SignatureSpi, ignore it |
|
1087 |
if (isSpi(s) == false) { |
|
1088 |
continue; |
|
1089 |
} |
|
1090 |
try { |
|
1091 |
SignatureSpi spi = newInstance(s); |
|
1092 |
init(spi, type, key, random); |
|
1093 |
provider = s.getProvider(); |
|
1094 |
sigSpi = spi; |
|
1095 |
firstService = null; |
|
1096 |
serviceIterator = null; |
|
1097 |
return; |
|
1098 |
} catch (Exception e) { |
|
1099 |
// NoSuchAlgorithmException from newInstance() |
|
1100 |
// InvalidKeyException from init() |
|
1101 |
// RuntimeException (ProviderException) from init() |
|
1102 |
if (lastException == null) { |
|
1103 |
lastException = e; |
|
1104 |
} |
|
1105 |
} |
|
1106 |
} |
|
1107 |
// no working provider found, fail |
|
1108 |
if (lastException instanceof InvalidKeyException) { |
|
1109 |
throw (InvalidKeyException)lastException; |
|
1110 |
} |
|
1111 |
if (lastException instanceof RuntimeException) { |
|
1112 |
throw (RuntimeException)lastException; |
|
1113 |
} |
|
1114 |
String k = (key != null) ? key.getClass().getName() : "(null)"; |
|
1115 |
throw new InvalidKeyException |
|
1116 |
("No installed provider supports this key: " |
|
1117 |
+ k, lastException); |
|
1118 |
} |
|
1119 |
} |
|
1120 |
||
1121 |
private final static int I_PUB = 1; |
|
1122 |
private final static int I_PRIV = 2; |
|
1123 |
private final static int I_PRIV_SR = 3; |
|
1124 |
||
1125 |
private void init(SignatureSpi spi, int type, Key key, |
|
1126 |
SecureRandom random) throws InvalidKeyException { |
|
1127 |
switch (type) { |
|
1128 |
case I_PUB: |
|
1129 |
spi.engineInitVerify((PublicKey)key); |
|
1130 |
break; |
|
1131 |
case I_PRIV: |
|
1132 |
spi.engineInitSign((PrivateKey)key); |
|
1133 |
break; |
|
1134 |
case I_PRIV_SR: |
|
1135 |
spi.engineInitSign((PrivateKey)key, random); |
|
1136 |
break; |
|
1137 |
default: |
|
1138 |
throw new AssertionError("Internal error: " + type); |
|
1139 |
} |
|
1140 |
} |
|
1141 |
||
1142 |
protected void engineInitVerify(PublicKey publicKey) |
|
1143 |
throws InvalidKeyException { |
|
1144 |
if (sigSpi != null) { |
|
1145 |
sigSpi.engineInitVerify(publicKey); |
|
1146 |
} else { |
|
1147 |
chooseProvider(I_PUB, publicKey, null); |
|
1148 |
} |
|
1149 |
} |
|
1150 |
||
1151 |
protected void engineInitSign(PrivateKey privateKey) |
|
1152 |
throws InvalidKeyException { |
|
1153 |
if (sigSpi != null) { |
|
1154 |
sigSpi.engineInitSign(privateKey); |
|
1155 |
} else { |
|
1156 |
chooseProvider(I_PRIV, privateKey, null); |
|
1157 |
} |
|
1158 |
} |
|
1159 |
||
1160 |
protected void engineInitSign(PrivateKey privateKey, SecureRandom sr) |
|
1161 |
throws InvalidKeyException { |
|
1162 |
if (sigSpi != null) { |
|
1163 |
sigSpi.engineInitSign(privateKey, sr); |
|
1164 |
} else { |
|
1165 |
chooseProvider(I_PRIV_SR, privateKey, sr); |
|
1166 |
} |
|
1167 |
} |
|
1168 |
||
1169 |
protected void engineUpdate(byte b) throws SignatureException { |
|
1170 |
chooseFirstProvider(); |
|
1171 |
sigSpi.engineUpdate(b); |
|
1172 |
} |
|
1173 |
||
1174 |
protected void engineUpdate(byte[] b, int off, int len) |
|
1175 |
throws SignatureException { |
|
1176 |
chooseFirstProvider(); |
|
1177 |
sigSpi.engineUpdate(b, off, len); |
|
1178 |
} |
|
1179 |
||
1180 |
protected void engineUpdate(ByteBuffer data) { |
|
1181 |
chooseFirstProvider(); |
|
1182 |
sigSpi.engineUpdate(data); |
|
1183 |
} |
|
1184 |
||
1185 |
protected byte[] engineSign() throws SignatureException { |
|
1186 |
chooseFirstProvider(); |
|
1187 |
return sigSpi.engineSign(); |
|
1188 |
} |
|
1189 |
||
1190 |
protected int engineSign(byte[] outbuf, int offset, int len) |
|
1191 |
throws SignatureException { |
|
1192 |
chooseFirstProvider(); |
|
1193 |
return sigSpi.engineSign(outbuf, offset, len); |
|
1194 |
} |
|
1195 |
||
1196 |
protected boolean engineVerify(byte[] sigBytes) |
|
1197 |
throws SignatureException { |
|
1198 |
chooseFirstProvider(); |
|
1199 |
return sigSpi.engineVerify(sigBytes); |
|
1200 |
} |
|
1201 |
||
1202 |
protected boolean engineVerify(byte[] sigBytes, int offset, int length) |
|
1203 |
throws SignatureException { |
|
1204 |
chooseFirstProvider(); |
|
1205 |
return sigSpi.engineVerify(sigBytes, offset, length); |
|
1206 |
} |
|
1207 |
||
1208 |
protected void engineSetParameter(String param, Object value) |
|
1209 |
throws InvalidParameterException { |
|
1210 |
chooseFirstProvider(); |
|
1211 |
sigSpi.engineSetParameter(param, value); |
|
1212 |
} |
|
1213 |
||
1214 |
protected void engineSetParameter(AlgorithmParameterSpec params) |
|
1215 |
throws InvalidAlgorithmParameterException { |
|
1216 |
chooseFirstProvider(); |
|
1217 |
sigSpi.engineSetParameter(params); |
|
1218 |
} |
|
1219 |
||
1220 |
protected Object engineGetParameter(String param) |
|
1221 |
throws InvalidParameterException { |
|
1222 |
chooseFirstProvider(); |
|
1223 |
return sigSpi.engineGetParameter(param); |
|
1224 |
} |
|
1225 |
||
1226 |
protected AlgorithmParameters engineGetParameters() { |
|
1227 |
chooseFirstProvider(); |
|
1228 |
return sigSpi.engineGetParameters(); |
|
1229 |
} |
|
1230 |
} |
|
1231 |
||
1232 |
// adapter for RSA/ECB/PKCS1Padding ciphers |
|
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
1233 |
@SuppressWarnings("deprecation") |
2 | 1234 |
private static class CipherAdapter extends SignatureSpi { |
1235 |
||
1236 |
private final Cipher cipher; |
|
1237 |
||
1238 |
private ByteArrayOutputStream data; |
|
1239 |
||
1240 |
CipherAdapter(Cipher cipher) { |
|
1241 |
this.cipher = cipher; |
|
1242 |
} |
|
1243 |
||
1244 |
protected void engineInitVerify(PublicKey publicKey) |
|
1245 |
throws InvalidKeyException { |
|
1246 |
cipher.init(Cipher.DECRYPT_MODE, publicKey); |
|
1247 |
if (data == null) { |
|
1248 |
data = new ByteArrayOutputStream(128); |
|
1249 |
} else { |
|
1250 |
data.reset(); |
|
1251 |
} |
|
1252 |
} |
|
1253 |
||
1254 |
protected void engineInitSign(PrivateKey privateKey) |
|
1255 |
throws InvalidKeyException { |
|
1256 |
cipher.init(Cipher.ENCRYPT_MODE, privateKey); |
|
1257 |
data = null; |
|
1258 |
} |
|
1259 |
||
1260 |
protected void engineInitSign(PrivateKey privateKey, |
|
1261 |
SecureRandom random) throws InvalidKeyException { |
|
1262 |
cipher.init(Cipher.ENCRYPT_MODE, privateKey, random); |
|
1263 |
data = null; |
|
1264 |
} |
|
1265 |
||
1266 |
protected void engineUpdate(byte b) throws SignatureException { |
|
1267 |
engineUpdate(new byte[] {b}, 0, 1); |
|
1268 |
} |
|
1269 |
||
1270 |
protected void engineUpdate(byte[] b, int off, int len) |
|
1271 |
throws SignatureException { |
|
1272 |
if (data != null) { |
|
1273 |
data.write(b, off, len); |
|
1274 |
return; |
|
1275 |
} |
|
1276 |
byte[] out = cipher.update(b, off, len); |
|
1277 |
if ((out != null) && (out.length != 0)) { |
|
1278 |
throw new SignatureException |
|
1279 |
("Cipher unexpectedly returned data"); |
|
1280 |
} |
|
1281 |
} |
|
1282 |
||
1283 |
protected byte[] engineSign() throws SignatureException { |
|
1284 |
try { |
|
1285 |
return cipher.doFinal(); |
|
1286 |
} catch (IllegalBlockSizeException e) { |
|
1287 |
throw new SignatureException("doFinal() failed", e); |
|
1288 |
} catch (BadPaddingException e) { |
|
1289 |
throw new SignatureException("doFinal() failed", e); |
|
1290 |
} |
|
1291 |
} |
|
1292 |
||
1293 |
protected boolean engineVerify(byte[] sigBytes) |
|
1294 |
throws SignatureException { |
|
1295 |
try { |
|
1296 |
byte[] out = cipher.doFinal(sigBytes); |
|
1297 |
byte[] dataBytes = data.toByteArray(); |
|
1298 |
data.reset(); |
|
1299 |
return Arrays.equals(out, dataBytes); |
|
1300 |
} catch (BadPaddingException e) { |
|
1301 |
// e.g. wrong public key used |
|
1302 |
// return false rather than throwing exception |
|
1303 |
return false; |
|
1304 |
} catch (IllegalBlockSizeException e) { |
|
1305 |
throw new SignatureException("doFinal() failed", e); |
|
1306 |
} |
|
1307 |
} |
|
1308 |
||
1309 |
protected void engineSetParameter(String param, Object value) |
|
1310 |
throws InvalidParameterException { |
|
1311 |
throw new InvalidParameterException("Parameters not supported"); |
|
1312 |
} |
|
1313 |
||
1314 |
protected Object engineGetParameter(String param) |
|
1315 |
throws InvalidParameterException { |
|
1316 |
throw new InvalidParameterException("Parameters not supported"); |
|
1317 |
} |
|
1318 |
||
1319 |
} |
|
1320 |
||
1321 |
} |