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