author | jboes |
Fri, 20 Sep 2019 11:07:52 +0100 | |
changeset 58242 | 94bb65cb37d3 |
parent 54483 | ac20c3bdc55d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53972
43c2ab1bdfd3
8215430: Remove the internal package com.sun.net.ssl
xuelei
parents:
53018
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package 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.IllegalBlockSizeException; |
|
41 |
import javax.crypto.BadPaddingException; |
|
42 |
import javax.crypto.NoSuchPaddingException; |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
43 |
import jdk.internal.access.JavaSecuritySignatureAccess; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
44 |
import jdk.internal.access.SharedSecrets; |
2 | 45 |
|
46 |
import sun.security.util.Debug; |
|
47 |
import sun.security.jca.*; |
|
48 |
import sun.security.jca.GetInstance.Instance; |
|
49 |
||
50 |
/** |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
51 |
* The Signature class is used to provide applications the functionality |
2 | 52 |
* of a digital signature algorithm. Digital signatures are used for |
53 |
* authentication and integrity assurance of digital data. |
|
54 |
* |
|
55 |
* <p> The signature algorithm can be, among others, the NIST standard |
|
46053
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
56 |
* DSA, using DSA and SHA-256. The DSA algorithm using the |
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
57 |
* SHA-256 message digest algorithm can be specified as {@code SHA256withDSA}. |
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
58 |
* In the case of RSA the signing algorithm could be specified as, for example, |
6f7e93cb432a
8169080: Improve documentation examples for crypto applications
wetmore
parents:
45434
diff
changeset
|
59 |
* {@code SHA256withRSA}. |
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= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
110 |
* "{@docRoot}/../specs/security/standard-names.html#signature-algorithms"> |
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
111 |
* Signature section</a> of the |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
112 |
* Java Security Standard Algorithm Names Specification. |
8152
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 |
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
45118
diff
changeset
|
117 |
* @since 1.1 |
2 | 118 |
* |
119 |
*/ |
|
120 |
||
121 |
public abstract class Signature extends SignatureSpi { |
|
122 |
||
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
123 |
static { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
124 |
SharedSecrets.setJavaSecuritySignatureAccess( |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
125 |
new JavaSecuritySignatureAccess() { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
126 |
@Override |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
127 |
public void initVerify(Signature s, PublicKey publicKey, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
128 |
AlgorithmParameterSpec params) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
129 |
throws InvalidKeyException, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
130 |
InvalidAlgorithmParameterException { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
131 |
s.initVerify(publicKey, params); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
132 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
133 |
@Override |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
134 |
public void initVerify(Signature s, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
135 |
java.security.cert.Certificate certificate, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
136 |
AlgorithmParameterSpec params) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
137 |
throws InvalidKeyException, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
138 |
InvalidAlgorithmParameterException { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
139 |
s.initVerify(certificate, params); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
140 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
141 |
@Override |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
142 |
public void initSign(Signature s, PrivateKey privateKey, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
143 |
AlgorithmParameterSpec params, SecureRandom random) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
144 |
throws InvalidKeyException, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
145 |
InvalidAlgorithmParameterException { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
146 |
s.initSign(privateKey, params, random); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
147 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
148 |
}); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
149 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
150 |
|
2 | 151 |
private static final Debug debug = |
152 |
Debug.getInstance("jca", "Signature"); |
|
153 |
||
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
154 |
private static final Debug pdebug = |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
155 |
Debug.getInstance("provider", "Provider"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
156 |
private static final boolean skipDebug = |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
157 |
Debug.isOn("engine=") && !Debug.isOn("signature"); |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
158 |
|
2 | 159 |
/* |
160 |
* The algorithm for this signature object. |
|
161 |
* This value is used to map an OID to the particular algorithm. |
|
162 |
* The mapping is done in AlgorithmObject.algOID(String algorithm) |
|
163 |
*/ |
|
164 |
private String algorithm; |
|
165 |
||
166 |
// The provider |
|
167 |
Provider provider; |
|
168 |
||
169 |
/** |
|
170 |
* Possible {@link #state} value, signifying that |
|
171 |
* this signature object has not yet been initialized. |
|
172 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32647
diff
changeset
|
173 |
protected static final int UNINITIALIZED = 0; |
2 | 174 |
|
175 |
/** |
|
176 |
* Possible {@link #state} value, signifying that |
|
177 |
* this signature object has been initialized for signing. |
|
178 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32647
diff
changeset
|
179 |
protected static final int SIGN = 2; |
2 | 180 |
|
181 |
/** |
|
182 |
* Possible {@link #state} value, signifying that |
|
183 |
* this signature object has been initialized for verification. |
|
184 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32647
diff
changeset
|
185 |
protected static final int VERIFY = 3; |
2 | 186 |
|
187 |
/** |
|
188 |
* Current state of this signature object. |
|
189 |
*/ |
|
190 |
protected int state = UNINITIALIZED; |
|
191 |
||
192 |
/** |
|
193 |
* Creates a Signature object for the specified algorithm. |
|
194 |
* |
|
195 |
* @param algorithm the standard string name of the algorithm. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
196 |
* See the Signature section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
197 |
* "{@docRoot}/../specs/security/standard-names.html#signature-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
198 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 199 |
* for information about standard algorithm names. |
200 |
*/ |
|
201 |
protected Signature(String algorithm) { |
|
202 |
this.algorithm = algorithm; |
|
203 |
} |
|
204 |
||
205 |
// name of the special signature alg |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32647
diff
changeset
|
206 |
private static final String RSA_SIGNATURE = "NONEwithRSA"; |
2 | 207 |
|
208 |
// name of the equivalent cipher alg |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32647
diff
changeset
|
209 |
private static final String RSA_CIPHER = "RSA/ECB/PKCS1Padding"; |
2 | 210 |
|
211 |
// all the services we need to lookup for compatibility with Cipher |
|
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
37348
diff
changeset
|
212 |
private static final List<ServiceId> rsaIds = List.of( |
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
37348
diff
changeset
|
213 |
new ServiceId("Signature", "NONEwithRSA"), |
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
37348
diff
changeset
|
214 |
new ServiceId("Cipher", "RSA/ECB/PKCS1Padding"), |
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
37348
diff
changeset
|
215 |
new ServiceId("Cipher", "RSA/ECB"), |
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
37348
diff
changeset
|
216 |
new ServiceId("Cipher", "RSA//PKCS1Padding"), |
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
37348
diff
changeset
|
217 |
new ServiceId("Cipher", "RSA")); |
2 | 218 |
|
219 |
/** |
|
220 |
* Returns a Signature object that implements the specified signature |
|
221 |
* algorithm. |
|
222 |
* |
|
223 |
* <p> This method traverses the list of registered security Providers, |
|
224 |
* starting with the most preferred Provider. |
|
225 |
* A new Signature object encapsulating the |
|
226 |
* SignatureSpi implementation from the first |
|
227 |
* Provider that supports the specified algorithm is returned. |
|
228 |
* |
|
229 |
* <p> Note that the list of registered providers may be retrieved via |
|
230 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
231 |
* |
|
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
232 |
* @implNote |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
233 |
* The JDK Reference Implementation additionally uses the |
37348
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
234 |
* {@code jdk.security.provider.preferred} |
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
235 |
* {@link Security#getProperty(String) Security} property to determine |
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
236 |
* the preferred provider order for the specified algorithm. This |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
237 |
* may be different than the order of providers returned by |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
238 |
* {@link Security#getProviders() Security.getProviders()}. |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
32649
diff
changeset
|
239 |
* |
2 | 240 |
* @param algorithm the standard name of the algorithm requested. |
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
241 |
* See the Signature section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
242 |
* "{@docRoot}/../specs/security/standard-names.html#signature-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
243 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 244 |
* for information about standard algorithm names. |
245 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
246 |
* @return the new {@code Signature} object |
2 | 247 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
248 |
* @throws NoSuchAlgorithmException if no {@code Provider} supports a |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
249 |
* {@code Signature} implementation for the |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
250 |
* specified algorithm |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
251 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
252 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 253 |
* |
254 |
* @see Provider |
|
255 |
*/ |
|
256 |
public static Signature getInstance(String algorithm) |
|
257 |
throws NoSuchAlgorithmException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
258 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 259 |
List<Service> list; |
260 |
if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) { |
|
261 |
list = GetInstance.getServices(rsaIds); |
|
262 |
} else { |
|
263 |
list = GetInstance.getServices("Signature", algorithm); |
|
264 |
} |
|
265 |
Iterator<Service> t = list.iterator(); |
|
266 |
if (t.hasNext() == false) { |
|
267 |
throw new NoSuchAlgorithmException |
|
268 |
(algorithm + " Signature not available"); |
|
269 |
} |
|
270 |
// try services until we find an Spi or a working Signature subclass |
|
271 |
NoSuchAlgorithmException failure; |
|
272 |
do { |
|
273 |
Service s = t.next(); |
|
274 |
if (isSpi(s)) { |
|
275 |
return new Delegate(s, t, algorithm); |
|
276 |
} else { |
|
277 |
// must be a subclass of Signature, disable dynamic selection |
|
278 |
try { |
|
279 |
Instance instance = |
|
280 |
GetInstance.getInstance(s, SignatureSpi.class); |
|
281 |
return getInstance(instance, algorithm); |
|
282 |
} catch (NoSuchAlgorithmException e) { |
|
283 |
failure = e; |
|
284 |
} |
|
285 |
} |
|
286 |
} while (t.hasNext()); |
|
287 |
throw failure; |
|
288 |
} |
|
289 |
||
290 |
private static Signature getInstance(Instance instance, String algorithm) { |
|
291 |
Signature sig; |
|
292 |
if (instance.impl instanceof Signature) { |
|
293 |
sig = (Signature)instance.impl; |
|
18181
4f0a36582461
8014620: Signature.getAlgorithm return null in special case
youdwei
parents:
12875
diff
changeset
|
294 |
sig.algorithm = algorithm; |
2 | 295 |
} else { |
296 |
SignatureSpi spi = (SignatureSpi)instance.impl; |
|
297 |
sig = new Delegate(spi, algorithm); |
|
298 |
} |
|
299 |
sig.provider = instance.provider; |
|
300 |
return sig; |
|
301 |
} |
|
302 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32647
diff
changeset
|
303 |
private static final Map<String,Boolean> signatureInfo; |
2 | 304 |
|
305 |
static { |
|
30033
b9c86c17164a
8078468: Update security libraries to use diamond with anonymous classes
darcy
parents:
28059
diff
changeset
|
306 |
signatureInfo = new ConcurrentHashMap<>(); |
2 | 307 |
Boolean TRUE = Boolean.TRUE; |
308 |
// pre-initialize with values for our SignatureSpi implementations |
|
309 |
signatureInfo.put("sun.security.provider.DSA$RawDSA", TRUE); |
|
310 |
signatureInfo.put("sun.security.provider.DSA$SHA1withDSA", TRUE); |
|
311 |
signatureInfo.put("sun.security.rsa.RSASignature$MD2withRSA", TRUE); |
|
312 |
signatureInfo.put("sun.security.rsa.RSASignature$MD5withRSA", TRUE); |
|
313 |
signatureInfo.put("sun.security.rsa.RSASignature$SHA1withRSA", TRUE); |
|
314 |
signatureInfo.put("sun.security.rsa.RSASignature$SHA256withRSA", TRUE); |
|
315 |
signatureInfo.put("sun.security.rsa.RSASignature$SHA384withRSA", TRUE); |
|
316 |
signatureInfo.put("sun.security.rsa.RSASignature$SHA512withRSA", TRUE); |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
48080
diff
changeset
|
317 |
signatureInfo.put("sun.security.rsa.RSAPSSSignature", TRUE); |
2 | 318 |
signatureInfo.put("sun.security.pkcs11.P11Signature", TRUE); |
319 |
} |
|
320 |
||
321 |
private static boolean isSpi(Service s) { |
|
322 |
if (s.getType().equals("Cipher")) { |
|
323 |
// must be a CipherSpi, which we can wrap with the CipherAdapter |
|
324 |
return true; |
|
325 |
} |
|
326 |
String className = s.getClassName(); |
|
327 |
Boolean result = signatureInfo.get(className); |
|
328 |
if (result == null) { |
|
329 |
try { |
|
330 |
Object instance = s.newInstance(null); |
|
331 |
// Signature extends SignatureSpi |
|
332 |
// so it is a "real" Spi if it is an |
|
333 |
// instance of SignatureSpi but not Signature |
|
334 |
boolean r = (instance instanceof SignatureSpi) |
|
335 |
&& (instance instanceof Signature == false); |
|
336 |
if ((debug != null) && (r == false)) { |
|
337 |
debug.println("Not a SignatureSpi " + className); |
|
338 |
debug.println("Delayed provider selection may not be " |
|
339 |
+ "available for algorithm " + s.getAlgorithm()); |
|
340 |
} |
|
341 |
result = Boolean.valueOf(r); |
|
342 |
signatureInfo.put(className, result); |
|
343 |
} catch (Exception e) { |
|
344 |
// something is wrong, assume not an SPI |
|
345 |
return false; |
|
346 |
} |
|
347 |
} |
|
348 |
return result.booleanValue(); |
|
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Returns a Signature object that implements the specified signature |
|
353 |
* algorithm. |
|
354 |
* |
|
355 |
* <p> A new Signature object encapsulating the |
|
356 |
* SignatureSpi implementation from the specified provider |
|
357 |
* is returned. The specified provider must be registered |
|
358 |
* in the security provider list. |
|
359 |
* |
|
360 |
* <p> Note that the list of registered providers may be retrieved via |
|
361 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
362 |
* |
|
363 |
* @param algorithm the name of the algorithm requested. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
364 |
* See the Signature section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
365 |
* "{@docRoot}/../specs/security/standard-names.html#signature-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
366 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 367 |
* for information about standard algorithm names. |
368 |
* |
|
369 |
* @param provider the name of the provider. |
|
370 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
371 |
* @return the new {@code Signature} object |
2 | 372 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
373 |
* @throws IllegalArgumentException if the provider name is {@code null} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
374 |
* or empty |
2 | 375 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
376 |
* @throws NoSuchAlgorithmException if a {@code SignatureSpi} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
377 |
* implementation for the specified algorithm is not |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
378 |
* available from the specified provider |
2 | 379 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
380 |
* @throws NoSuchProviderException if the specified provider is not |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
381 |
* registered in the security provider list |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
382 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
383 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 384 |
* |
385 |
* @see Provider |
|
386 |
*/ |
|
387 |
public static Signature getInstance(String algorithm, String provider) |
|
388 |
throws NoSuchAlgorithmException, NoSuchProviderException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
389 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 390 |
if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) { |
391 |
// exception compatibility with existing code |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
51229
diff
changeset
|
392 |
if (provider == null || provider.isEmpty()) { |
2 | 393 |
throw new IllegalArgumentException("missing provider"); |
394 |
} |
|
395 |
Provider p = Security.getProvider(provider); |
|
396 |
if (p == null) { |
|
397 |
throw new NoSuchProviderException |
|
398 |
("no such provider: " + provider); |
|
399 |
} |
|
400 |
return getInstanceRSA(p); |
|
401 |
} |
|
402 |
Instance instance = GetInstance.getInstance |
|
403 |
("Signature", SignatureSpi.class, algorithm, provider); |
|
404 |
return getInstance(instance, algorithm); |
|
405 |
} |
|
406 |
||
407 |
/** |
|
408 |
* Returns a Signature object that implements the specified |
|
409 |
* signature algorithm. |
|
410 |
* |
|
411 |
* <p> A new Signature object encapsulating the |
|
412 |
* SignatureSpi implementation from the specified Provider |
|
413 |
* object is returned. Note that the specified Provider object |
|
414 |
* does not have to be registered in the provider list. |
|
415 |
* |
|
416 |
* @param algorithm the name of the algorithm requested. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
417 |
* See the Signature section in the <a href= |
45118
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
418 |
* "{@docRoot}/../specs/security/standard-names.html#signature-algorithms"> |
e4258d800b54
8178278: Move Standard Algorithm Names document to specs directory
ihse
parents:
42691
diff
changeset
|
419 |
* Java Security Standard Algorithm Names Specification</a> |
2 | 420 |
* for information about standard algorithm names. |
421 |
* |
|
422 |
* @param provider the provider. |
|
423 |
* |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
424 |
* @return the new {@code Signature} object |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
425 |
* |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
426 |
* @throws IllegalArgumentException if the provider is {@code null} |
2 | 427 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
428 |
* @throws NoSuchAlgorithmException if a {@code SignatureSpi} |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
429 |
* implementation for the specified algorithm is not available |
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
430 |
* from the specified {@code Provider} object |
2 | 431 |
* |
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
432 |
* @throws NullPointerException if {@code algorithm} is {@code null} |
2 | 433 |
* |
434 |
* @see Provider |
|
435 |
* |
|
436 |
* @since 1.4 |
|
437 |
*/ |
|
438 |
public static Signature getInstance(String algorithm, Provider provider) |
|
439 |
throws NoSuchAlgorithmException { |
|
41826
b35ee9b35b09
4985694: Incomplete spec for most of the getInstances
wetmore
parents:
41482
diff
changeset
|
440 |
Objects.requireNonNull(algorithm, "null algorithm name"); |
2 | 441 |
if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) { |
442 |
// exception compatibility with existing code |
|
443 |
if (provider == null) { |
|
444 |
throw new IllegalArgumentException("missing provider"); |
|
445 |
} |
|
446 |
return getInstanceRSA(provider); |
|
447 |
} |
|
448 |
Instance instance = GetInstance.getInstance |
|
449 |
("Signature", SignatureSpi.class, algorithm, provider); |
|
450 |
return getInstance(instance, algorithm); |
|
451 |
} |
|
452 |
||
453 |
// return an implementation for NONEwithRSA, which is a special case |
|
454 |
// because of the Cipher.RSA/ECB/PKCS1Padding compatibility wrapper |
|
455 |
private static Signature getInstanceRSA(Provider p) |
|
456 |
throws NoSuchAlgorithmException { |
|
457 |
// try Signature first |
|
458 |
Service s = p.getService("Signature", RSA_SIGNATURE); |
|
459 |
if (s != null) { |
|
460 |
Instance instance = GetInstance.getInstance(s, SignatureSpi.class); |
|
461 |
return getInstance(instance, RSA_SIGNATURE); |
|
462 |
} |
|
463 |
// check Cipher |
|
464 |
try { |
|
465 |
Cipher c = Cipher.getInstance(RSA_CIPHER, p); |
|
466 |
return new Delegate(new CipherAdapter(c), RSA_SIGNATURE); |
|
467 |
} catch (GeneralSecurityException e) { |
|
468 |
// throw Signature style exception message to avoid confusion, |
|
469 |
// but append Cipher exception as cause |
|
470 |
throw new NoSuchAlgorithmException("no such algorithm: " |
|
471 |
+ RSA_SIGNATURE + " for provider " + p.getName(), e); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
/** |
|
476 |
* Returns the provider of this signature object. |
|
477 |
* |
|
478 |
* @return the provider of this signature object |
|
479 |
*/ |
|
480 |
public final Provider getProvider() { |
|
481 |
chooseFirstProvider(); |
|
482 |
return this.provider; |
|
483 |
} |
|
484 |
||
42691
42b035e0bda9
8165751: NPE hit with java.security.debug=provider
mullan
parents:
41826
diff
changeset
|
485 |
private String getProviderName() { |
42b035e0bda9
8165751: NPE hit with java.security.debug=provider
mullan
parents:
41826
diff
changeset
|
486 |
return (provider == null) ? "(no provider)" : provider.getName(); |
42b035e0bda9
8165751: NPE hit with java.security.debug=provider
mullan
parents:
41826
diff
changeset
|
487 |
} |
42b035e0bda9
8165751: NPE hit with java.security.debug=provider
mullan
parents:
41826
diff
changeset
|
488 |
|
2 | 489 |
void chooseFirstProvider() { |
490 |
// empty, overridden in Delegate |
|
491 |
} |
|
492 |
||
493 |
/** |
|
494 |
* Initializes this object for verification. If this method is called |
|
495 |
* again with a different argument, it negates the effect |
|
496 |
* of this call. |
|
497 |
* |
|
498 |
* @param publicKey the public key of the identity whose signature is |
|
499 |
* going to be verified. |
|
500 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
501 |
* @throws InvalidKeyException if the key is invalid. |
2 | 502 |
*/ |
503 |
public final void initVerify(PublicKey publicKey) |
|
504 |
throws InvalidKeyException { |
|
505 |
engineInitVerify(publicKey); |
|
506 |
state = VERIFY; |
|
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
507 |
|
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
508 |
if (!skipDebug && pdebug != null) { |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
509 |
pdebug.println("Signature." + algorithm + |
42691
42b035e0bda9
8165751: NPE hit with java.security.debug=provider
mullan
parents:
41826
diff
changeset
|
510 |
" verification algorithm from: " + getProviderName()); |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
511 |
} |
2 | 512 |
} |
513 |
||
514 |
/** |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
515 |
* Initialize this object for verification. If this method is called |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
516 |
* again with different arguments, it negates the effect |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
517 |
* of this call. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
518 |
* |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
519 |
* @param publicKey the public key of the identity whose signature is |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
520 |
* going to be verified. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
521 |
* @param params the parameters used for verifying this signature. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
522 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
523 |
* @throws InvalidKeyException if the key is invalid. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
524 |
* @throws InvalidAlgorithmParameterException if the params is invalid. |
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
525 |
*/ |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
526 |
final void initVerify(PublicKey publicKey, AlgorithmParameterSpec params) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
527 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
528 |
engineInitVerify(publicKey, params); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
529 |
state = VERIFY; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
530 |
|
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
531 |
if (!skipDebug && pdebug != null) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
532 |
pdebug.println("Signature." + algorithm + |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
533 |
" verification algorithm from: " + getProviderName()); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
534 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
535 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
536 |
|
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
537 |
private static PublicKey getPublicKeyFromCert(Certificate cert) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
538 |
throws InvalidKeyException { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
539 |
// If the certificate is of type X509Certificate, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
540 |
// we should check whether it has a Key Usage |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
541 |
// extension marked as critical. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
542 |
//if (cert instanceof java.security.cert.X509Certificate) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
543 |
if (cert instanceof X509Certificate) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
544 |
// Check whether the cert has a key usage extension |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
545 |
// marked as a critical extension. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
546 |
// The OID for KeyUsage extension is 2.5.29.15. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
547 |
X509Certificate c = (X509Certificate)cert; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
548 |
Set<String> critSet = c.getCriticalExtensionOIDs(); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
549 |
|
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
550 |
if (critSet != null && !critSet.isEmpty() |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
551 |
&& critSet.contains("2.5.29.15")) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
552 |
boolean[] keyUsageInfo = c.getKeyUsage(); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
553 |
// keyUsageInfo[0] is for digitalSignature. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
554 |
if ((keyUsageInfo != null) && (keyUsageInfo[0] == false)) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
555 |
throw new InvalidKeyException("Wrong key usage"); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
556 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
557 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
558 |
return cert.getPublicKey(); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
559 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
560 |
|
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
561 |
/** |
2 | 562 |
* Initializes this object for verification, using the public key from |
563 |
* the given certificate. |
|
564 |
* <p>If the certificate is of type X.509 and has a <i>key usage</i> |
|
565 |
* extension field marked as critical, and the value of the <i>key usage</i> |
|
566 |
* extension field implies that the public key in |
|
567 |
* the certificate and its corresponding private key are not |
|
568 |
* 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
|
569 |
* {@code InvalidKeyException} is thrown. |
2 | 570 |
* |
571 |
* @param certificate the certificate of the identity whose signature is |
|
572 |
* going to be verified. |
|
573 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
574 |
* @throws InvalidKeyException if the public key in the certificate |
2 | 575 |
* is not encoded properly or does not include required parameter |
576 |
* information or cannot be used for digital signature purposes. |
|
577 |
* @since 1.3 |
|
578 |
*/ |
|
579 |
public final void initVerify(Certificate certificate) |
|
580 |
throws InvalidKeyException { |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
581 |
engineInitVerify(getPublicKeyFromCert(certificate)); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
582 |
state = VERIFY; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
583 |
|
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
584 |
if (!skipDebug && pdebug != null) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
585 |
pdebug.println("Signature." + algorithm + |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
586 |
" verification algorithm from: " + getProviderName()); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
587 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
588 |
} |
2 | 589 |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
590 |
/** |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
591 |
* Initializes this object for verification, using the public key from |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
592 |
* the given certificate. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
593 |
* <p>If the certificate is of type X.509 and has a <i>key usage</i> |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
594 |
* extension field marked as critical, and the value of the <i>key usage</i> |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
595 |
* extension field implies that the public key in |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
596 |
* the certificate and its corresponding private key are not |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
597 |
* supposed to be used for digital signatures, an |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
598 |
* {@code InvalidKeyException} is thrown. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
599 |
* |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
600 |
* @param certificate the certificate of the identity whose signature is |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
601 |
* going to be verified. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
602 |
* @param params the parameters used for verifying this signature. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
603 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
604 |
* @throws InvalidKeyException if the public key in the certificate |
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
605 |
* is not encoded properly or does not include required parameter |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
606 |
* information or cannot be used for digital signature purposes. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
607 |
* @throws InvalidAlgorithmParameterException if the params is invalid. |
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
608 |
* |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
609 |
* @since 13 |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
610 |
*/ |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
611 |
final void initVerify(Certificate certificate, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
612 |
AlgorithmParameterSpec params) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
613 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
614 |
engineInitVerify(getPublicKeyFromCert(certificate), params); |
2 | 615 |
state = VERIFY; |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
616 |
|
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
617 |
if (!skipDebug && pdebug != null) { |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
618 |
pdebug.println("Signature." + algorithm + |
42691
42b035e0bda9
8165751: NPE hit with java.security.debug=provider
mullan
parents:
41826
diff
changeset
|
619 |
" verification algorithm from: " + getProviderName()); |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
620 |
} |
2 | 621 |
} |
622 |
||
623 |
/** |
|
624 |
* Initialize this object for signing. If this method is called |
|
625 |
* again with a different argument, it negates the effect |
|
626 |
* of this call. |
|
627 |
* |
|
628 |
* @param privateKey the private key of the identity whose signature |
|
629 |
* is going to be generated. |
|
630 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
631 |
* @throws InvalidKeyException if the key is invalid. |
2 | 632 |
*/ |
633 |
public final void initSign(PrivateKey privateKey) |
|
634 |
throws InvalidKeyException { |
|
635 |
engineInitSign(privateKey); |
|
636 |
state = SIGN; |
|
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
637 |
|
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
638 |
if (!skipDebug && pdebug != null) { |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
639 |
pdebug.println("Signature." + algorithm + |
42691
42b035e0bda9
8165751: NPE hit with java.security.debug=provider
mullan
parents:
41826
diff
changeset
|
640 |
" signing algorithm from: " + getProviderName()); |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
641 |
} |
2 | 642 |
} |
643 |
||
644 |
/** |
|
645 |
* Initialize this object for signing. If this method is called |
|
646 |
* again with a different argument, it negates the effect |
|
647 |
* of this call. |
|
648 |
* |
|
649 |
* @param privateKey the private key of the identity whose signature |
|
650 |
* is going to be generated. |
|
651 |
* |
|
652 |
* @param random the source of randomness for this signature. |
|
653 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
654 |
* @throws InvalidKeyException if the key is invalid. |
2 | 655 |
*/ |
656 |
public final void initSign(PrivateKey privateKey, SecureRandom random) |
|
657 |
throws InvalidKeyException { |
|
658 |
engineInitSign(privateKey, random); |
|
659 |
state = SIGN; |
|
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
660 |
|
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
661 |
if (!skipDebug && pdebug != null) { |
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
662 |
pdebug.println("Signature." + algorithm + |
42691
42b035e0bda9
8165751: NPE hit with java.security.debug=provider
mullan
parents:
41826
diff
changeset
|
663 |
" signing algorithm from: " + getProviderName()); |
26736
5a93000b26cd
8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents:
25859
diff
changeset
|
664 |
} |
2 | 665 |
} |
666 |
||
667 |
/** |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
668 |
* Initialize this object for signing. If this method is called |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
669 |
* again with different arguments, it negates the effect |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
670 |
* of this call. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
671 |
* |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
672 |
* @param privateKey the private key of the identity whose signature |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
673 |
* is going to be generated. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
674 |
* @param params the parameters used for generating signature. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
675 |
* @param random the source of randomness for this signature. |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
676 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
677 |
* @throws InvalidKeyException if the key is invalid. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
678 |
* @throws InvalidAlgorithmParameterException if the params is invalid |
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
679 |
*/ |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
680 |
final void initSign(PrivateKey privateKey, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
681 |
AlgorithmParameterSpec params, SecureRandom random) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
682 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
683 |
engineInitSign(privateKey, params, random); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
684 |
state = SIGN; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
685 |
|
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
686 |
if (!skipDebug && pdebug != null) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
687 |
pdebug.println("Signature." + algorithm + |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
688 |
" signing algorithm from: " + getProviderName()); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
689 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
690 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
691 |
|
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
692 |
/** |
2 | 693 |
* Returns the signature bytes of all the data updated. |
694 |
* The format of the signature depends on the underlying |
|
695 |
* signature scheme. |
|
696 |
* |
|
697 |
* <p>A call to this method resets this signature object to the state |
|
698 |
* 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
|
699 |
* call to {@code initSign(PrivateKey)}. That is, the object is |
2 | 700 |
* 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
|
701 |
* 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
|
702 |
* {@code sign}. |
2 | 703 |
* |
704 |
* @return the signature bytes of the signing operation's result. |
|
705 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
706 |
* @throws SignatureException if this signature object is not |
2 | 707 |
* initialized properly or if this signature algorithm is unable to |
708 |
* process the input data provided. |
|
709 |
*/ |
|
710 |
public final byte[] sign() throws SignatureException { |
|
711 |
if (state == SIGN) { |
|
712 |
return engineSign(); |
|
713 |
} |
|
714 |
throw new SignatureException("object not initialized for " + |
|
715 |
"signing"); |
|
716 |
} |
|
717 |
||
718 |
/** |
|
719 |
* 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
|
720 |
* 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
|
721 |
* {@code offset}. |
2 | 722 |
* The format of the signature depends on the underlying |
723 |
* signature scheme. |
|
724 |
* |
|
725 |
* <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
|
726 |
* was in after a call to one of the {@code initSign} methods) and |
2 | 727 |
* can be reused to generate further signatures with the same private key. |
728 |
* |
|
729 |
* @param outbuf buffer for the signature result. |
|
730 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
731 |
* @param offset offset into {@code outbuf} where the signature is |
2 | 732 |
* stored. |
733 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
734 |
* @param len number of bytes within {@code outbuf} allotted for the |
2 | 735 |
* signature. |
736 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
737 |
* @return the number of bytes placed into {@code outbuf}. |
2 | 738 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
739 |
* @throws SignatureException if this signature object is not |
27077 | 740 |
* initialized properly, if this signature algorithm is unable to |
741 |
* process the input data provided, or if {@code len} is less |
|
742 |
* than the actual signature length. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
743 |
* @throws IllegalArgumentException if {@code outbuf} is {@code null}, |
27077 | 744 |
* or {@code offset} or {@code len} is less than 0, or the sum of |
745 |
* {@code offset} and {@code len} is greater than the length of |
|
746 |
* {@code outbuf}. |
|
2 | 747 |
* |
748 |
* @since 1.2 |
|
749 |
*/ |
|
750 |
public final int sign(byte[] outbuf, int offset, int len) |
|
751 |
throws SignatureException { |
|
752 |
if (outbuf == null) { |
|
753 |
throw new IllegalArgumentException("No output buffer given"); |
|
754 |
} |
|
27077 | 755 |
if (offset < 0 || len < 0) { |
756 |
throw new IllegalArgumentException("offset or len is less than 0"); |
|
757 |
} |
|
2 | 758 |
if (outbuf.length - offset < len) { |
759 |
throw new IllegalArgumentException |
|
760 |
("Output buffer too small for specified offset and length"); |
|
761 |
} |
|
762 |
if (state != SIGN) { |
|
763 |
throw new SignatureException("object not initialized for " + |
|
764 |
"signing"); |
|
765 |
} |
|
766 |
return engineSign(outbuf, offset, len); |
|
767 |
} |
|
768 |
||
769 |
/** |
|
770 |
* Verifies the passed-in signature. |
|
771 |
* |
|
772 |
* <p>A call to this method resets this signature object to the state |
|
773 |
* 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
|
774 |
* call to {@code initVerify(PublicKey)}. That is, the object is |
2 | 775 |
* 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
|
776 |
* whose public key was specified in the call to {@code initVerify}. |
2 | 777 |
* |
778 |
* @param signature the signature bytes to be verified. |
|
779 |
* |
|
780 |
* @return true if the signature was verified, false if not. |
|
781 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
782 |
* @throws SignatureException if this signature object is not |
2 | 783 |
* initialized properly, the passed-in signature is improperly |
784 |
* encoded or of the wrong type, if this signature algorithm is unable to |
|
785 |
* process the input data provided, etc. |
|
786 |
*/ |
|
787 |
public final boolean verify(byte[] signature) throws SignatureException { |
|
788 |
if (state == VERIFY) { |
|
789 |
return engineVerify(signature); |
|
790 |
} |
|
791 |
throw new SignatureException("object not initialized for " + |
|
792 |
"verification"); |
|
793 |
} |
|
794 |
||
795 |
/** |
|
796 |
* Verifies the passed-in signature in the specified array |
|
797 |
* of bytes, starting at the specified offset. |
|
798 |
* |
|
799 |
* <p>A call to this method resets this signature object to the state |
|
800 |
* 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
|
801 |
* call to {@code initVerify(PublicKey)}. That is, the object is |
2 | 802 |
* 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
|
803 |
* whose public key was specified in the call to {@code initVerify}. |
2 | 804 |
* |
805 |
* |
|
806 |
* @param signature the signature bytes to be verified. |
|
807 |
* @param offset the offset to start from in the array of bytes. |
|
808 |
* @param length the number of bytes to use, starting at offset. |
|
809 |
* |
|
810 |
* @return true if the signature was verified, false if not. |
|
811 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
812 |
* @throws SignatureException if this signature object is not |
2 | 813 |
* initialized properly, the passed-in signature is improperly |
814 |
* encoded or of the wrong type, if this signature algorithm is unable to |
|
815 |
* process the input data provided, etc. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
816 |
* @throws IllegalArgumentException if the {@code signature} |
51229
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
817 |
* byte array is {@code null}, or the {@code offset} or {@code length} |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
818 |
* 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
|
819 |
* {@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
|
820 |
* {@code signature} byte array. |
2 | 821 |
* @since 1.4 |
822 |
*/ |
|
823 |
public final boolean verify(byte[] signature, int offset, int length) |
|
824 |
throws SignatureException { |
|
825 |
if (state == VERIFY) { |
|
27077 | 826 |
if (signature == null) { |
827 |
throw new IllegalArgumentException("signature is null"); |
|
828 |
} |
|
829 |
if (offset < 0 || length < 0) { |
|
830 |
throw new IllegalArgumentException |
|
831 |
("offset or length is less than 0"); |
|
832 |
} |
|
833 |
if (signature.length - offset < length) { |
|
834 |
throw new IllegalArgumentException |
|
835 |
("signature too small for specified offset and length"); |
|
2 | 836 |
} |
837 |
||
838 |
return engineVerify(signature, offset, length); |
|
839 |
} |
|
840 |
throw new SignatureException("object not initialized for " + |
|
841 |
"verification"); |
|
842 |
} |
|
843 |
||
844 |
/** |
|
845 |
* Updates the data to be signed or verified by a byte. |
|
846 |
* |
|
847 |
* @param b the byte to use for the update. |
|
848 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
849 |
* @throws SignatureException if this signature object is not |
2 | 850 |
* initialized properly. |
851 |
*/ |
|
852 |
public final void update(byte b) throws SignatureException { |
|
853 |
if (state == VERIFY || state == SIGN) { |
|
854 |
engineUpdate(b); |
|
855 |
} else { |
|
856 |
throw new SignatureException("object not initialized for " |
|
857 |
+ "signature or verification"); |
|
858 |
} |
|
859 |
} |
|
860 |
||
861 |
/** |
|
862 |
* Updates the data to be signed or verified, using the specified |
|
863 |
* array of bytes. |
|
864 |
* |
|
865 |
* @param data the byte array to use for the update. |
|
866 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
867 |
* @throws SignatureException if this signature object is not |
2 | 868 |
* initialized properly. |
869 |
*/ |
|
870 |
public final void update(byte[] data) throws SignatureException { |
|
871 |
update(data, 0, data.length); |
|
872 |
} |
|
873 |
||
874 |
/** |
|
875 |
* Updates the data to be signed or verified, using the specified |
|
876 |
* array of bytes, starting at the specified offset. |
|
877 |
* |
|
878 |
* @param data the array of bytes. |
|
879 |
* @param off the offset to start from in the array of bytes. |
|
880 |
* @param len the number of bytes to use, starting at offset. |
|
881 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
882 |
* @throws SignatureException if this signature object is not |
27077 | 883 |
* initialized properly. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
884 |
* @throws IllegalArgumentException if {@code data} is {@code null}, |
27077 | 885 |
* or {@code off} or {@code len} is less than 0, or the sum of |
886 |
* {@code off} and {@code len} is greater than the length of |
|
887 |
* {@code data}. |
|
2 | 888 |
*/ |
889 |
public final void update(byte[] data, int off, int len) |
|
890 |
throws SignatureException { |
|
891 |
if (state == SIGN || state == VERIFY) { |
|
27077 | 892 |
if (data == null) { |
893 |
throw new IllegalArgumentException("data is null"); |
|
894 |
} |
|
895 |
if (off < 0 || len < 0) { |
|
896 |
throw new IllegalArgumentException("off or len is less than 0"); |
|
897 |
} |
|
898 |
if (data.length - off < len) { |
|
899 |
throw new IllegalArgumentException |
|
900 |
("data too small for specified offset and length"); |
|
901 |
} |
|
2 | 902 |
engineUpdate(data, off, len); |
903 |
} else { |
|
904 |
throw new SignatureException("object not initialized for " |
|
905 |
+ "signature or verification"); |
|
906 |
} |
|
907 |
} |
|
908 |
||
909 |
/** |
|
910 |
* 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
|
911 |
* 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
|
912 |
* starting at {@code data.position()}. |
2 | 913 |
* Upon return, the buffer's position will be equal to its limit; |
914 |
* its limit will not have changed. |
|
915 |
* |
|
916 |
* @param data the ByteBuffer |
|
917 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
918 |
* @throws SignatureException if this signature object is not |
2 | 919 |
* initialized properly. |
920 |
* @since 1.5 |
|
921 |
*/ |
|
922 |
public final void update(ByteBuffer data) throws SignatureException { |
|
923 |
if ((state != SIGN) && (state != VERIFY)) { |
|
924 |
throw new SignatureException("object not initialized for " |
|
925 |
+ "signature or verification"); |
|
926 |
} |
|
927 |
if (data == null) { |
|
928 |
throw new NullPointerException(); |
|
929 |
} |
|
930 |
engineUpdate(data); |
|
931 |
} |
|
932 |
||
933 |
/** |
|
934 |
* Returns the name of the algorithm for this signature object. |
|
935 |
* |
|
936 |
* @return the name of the algorithm for this signature object. |
|
937 |
*/ |
|
938 |
public final String getAlgorithm() { |
|
939 |
return this.algorithm; |
|
940 |
} |
|
941 |
||
942 |
/** |
|
943 |
* Returns a string representation of this signature object, |
|
944 |
* providing information that includes the state of the object |
|
945 |
* and the name of the algorithm used. |
|
946 |
* |
|
947 |
* @return a string representation of this signature object. |
|
948 |
*/ |
|
949 |
public String toString() { |
|
950 |
String initState = ""; |
|
951 |
switch (state) { |
|
952 |
case UNINITIALIZED: |
|
953 |
initState = "<not initialized>"; |
|
954 |
break; |
|
955 |
case VERIFY: |
|
956 |
initState = "<initialized for verifying>"; |
|
957 |
break; |
|
958 |
case SIGN: |
|
959 |
initState = "<initialized for signing>"; |
|
960 |
break; |
|
961 |
} |
|
962 |
return "Signature object: " + getAlgorithm() + initState; |
|
963 |
} |
|
964 |
||
965 |
/** |
|
966 |
* Sets the specified algorithm parameter to the specified value. |
|
967 |
* This method supplies a general-purpose mechanism through |
|
968 |
* which it is possible to set the various parameters of this object. |
|
969 |
* A parameter may be any settable parameter for the algorithm, such as |
|
970 |
* a parameter size, or a source of random bits for signature generation |
|
971 |
* (if appropriate), or an indication of whether or not to perform |
|
972 |
* a specific but optional computation. A uniform algorithm-specific |
|
973 |
* naming scheme for each parameter is desirable but left unspecified |
|
974 |
* at this time. |
|
975 |
* |
|
976 |
* @param param the string identifier of the parameter. |
|
977 |
* @param value the parameter value. |
|
978 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
979 |
* @throws InvalidParameterException if {@code param} is an |
2 | 980 |
* invalid parameter for this signature algorithm engine, |
981 |
* the parameter is already set |
|
982 |
* and cannot be set again, a security exception occurs, and so on. |
|
983 |
* |
|
984 |
* @see #getParameter |
|
985 |
* |
|
986 |
* @deprecated Use |
|
987 |
* {@link #setParameter(java.security.spec.AlgorithmParameterSpec) |
|
988 |
* setParameter}. |
|
989 |
*/ |
|
990 |
@Deprecated |
|
991 |
public final void setParameter(String param, Object value) |
|
992 |
throws InvalidParameterException { |
|
993 |
engineSetParameter(param, value); |
|
994 |
} |
|
995 |
||
996 |
/** |
|
997 |
* Initializes this signature engine with the specified parameter set. |
|
998 |
* |
|
999 |
* @param params the parameters |
|
1000 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
1001 |
* @throws InvalidAlgorithmParameterException if the given parameters |
2 | 1002 |
* are inappropriate for this signature engine |
1003 |
* |
|
1004 |
* @see #getParameters |
|
1005 |
*/ |
|
1006 |
public final void setParameter(AlgorithmParameterSpec params) |
|
1007 |
throws InvalidAlgorithmParameterException { |
|
1008 |
engineSetParameter(params); |
|
1009 |
} |
|
1010 |
||
1011 |
/** |
|
1012 |
* Returns the parameters used with this signature object. |
|
1013 |
* |
|
51229
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
1014 |
* <p> If this signature has been previously initialized with parameters |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
1015 |
* (by calling the {@code setParameter} method), this method returns |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
1016 |
* the same parameters. If this signature has not been initialized with |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
1017 |
* parameters, this method may return a combination of default and |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
1018 |
* randomly generated parameter values if the underlying |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
1019 |
* signature implementation supports it and can successfully generate |
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
1020 |
* them. Otherwise, {@code null} is returned. |
2 | 1021 |
* |
51229
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
1022 |
* @return the parameters used with this signature, or {@code null} |
2 | 1023 |
* |
1024 |
* @see #setParameter(AlgorithmParameterSpec) |
|
1025 |
* @since 1.4 |
|
1026 |
*/ |
|
1027 |
public final AlgorithmParameters getParameters() { |
|
1028 |
return engineGetParameters(); |
|
1029 |
} |
|
1030 |
||
1031 |
/** |
|
1032 |
* Gets the value of the specified algorithm parameter. This method |
|
1033 |
* supplies a general-purpose mechanism through which it is possible to |
|
1034 |
* get the various parameters of this object. A parameter may be any |
|
1035 |
* settable parameter for the algorithm, such as a parameter size, or |
|
1036 |
* a source of random bits for signature generation (if appropriate), |
|
1037 |
* or an indication of whether or not to perform a specific but optional |
|
1038 |
* computation. A uniform algorithm-specific naming scheme for each |
|
1039 |
* parameter is desirable but left unspecified at this time. |
|
1040 |
* |
|
1041 |
* @param param the string name of the parameter. |
|
1042 |
* |
|
51229
17b7d7034e8e
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents:
50204
diff
changeset
|
1043 |
* @return the object that represents the parameter value, or {@code null} if |
2 | 1044 |
* there is none. |
1045 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
1046 |
* @throws InvalidParameterException if {@code param} is an invalid |
2 | 1047 |
* parameter for this engine, or another exception occurs while |
1048 |
* trying to get this parameter. |
|
1049 |
* |
|
1050 |
* @see #setParameter(String, Object) |
|
1051 |
* |
|
1052 |
* @deprecated |
|
1053 |
*/ |
|
1054 |
@Deprecated |
|
1055 |
public final Object getParameter(String param) |
|
1056 |
throws InvalidParameterException { |
|
1057 |
return engineGetParameter(param); |
|
1058 |
} |
|
1059 |
||
1060 |
/** |
|
1061 |
* Returns a clone if the implementation is cloneable. |
|
1062 |
* |
|
1063 |
* @return a clone if the implementation is cloneable. |
|
1064 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
1065 |
* @throws CloneNotSupportedException if this is called |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
1066 |
* on an implementation that does not support {@code Cloneable}. |
2 | 1067 |
*/ |
1068 |
public Object clone() throws CloneNotSupportedException { |
|
1069 |
if (this instanceof Cloneable) { |
|
1070 |
return super.clone(); |
|
1071 |
} else { |
|
1072 |
throw new CloneNotSupportedException(); |
|
1073 |
} |
|
1074 |
} |
|
1075 |
||
1076 |
/* |
|
1077 |
* The following class allows providers to extend from SignatureSpi |
|
1078 |
* rather than from Signature. It represents a Signature with an |
|
1079 |
* encapsulated, provider-supplied SPI object (of type SignatureSpi). |
|
1080 |
* If the provider implementation is an instance of SignatureSpi, the |
|
1081 |
* getInstance() methods above return an instance of this class, with |
|
1082 |
* the SPI object encapsulated. |
|
1083 |
* |
|
1084 |
* Note: All SPI methods from the original Signature class have been |
|
1085 |
* moved up the hierarchy into a new class (SignatureSpi), which has |
|
1086 |
* been interposed in the hierarchy between the API (Signature) |
|
1087 |
* and its original parent (Object). |
|
1088 |
*/ |
|
1089 |
||
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
1090 |
@SuppressWarnings("deprecation") |
2 | 1091 |
private static class Delegate extends Signature { |
1092 |
||
1093 |
// The provider implementation (delegate) |
|
1094 |
// filled in once the provider is selected |
|
1095 |
private SignatureSpi sigSpi; |
|
1096 |
||
1097 |
// lock for mutex during provider selection |
|
1098 |
private final Object lock; |
|
1099 |
||
1100 |
// next service to try in provider selection |
|
1101 |
// null once provider is selected |
|
1102 |
private Service firstService; |
|
1103 |
||
1104 |
// remaining services to try in provider selection |
|
1105 |
// null once provider is selected |
|
1106 |
private Iterator<Service> serviceIterator; |
|
1107 |
||
1108 |
// constructor |
|
1109 |
Delegate(SignatureSpi sigSpi, String algorithm) { |
|
1110 |
super(algorithm); |
|
1111 |
this.sigSpi = sigSpi; |
|
1112 |
this.lock = null; // no lock needed |
|
1113 |
} |
|
1114 |
||
1115 |
// used with delayed provider selection |
|
1116 |
Delegate(Service service, |
|
1117 |
Iterator<Service> iterator, String algorithm) { |
|
1118 |
super(algorithm); |
|
1119 |
this.firstService = service; |
|
1120 |
this.serviceIterator = iterator; |
|
1121 |
this.lock = new Object(); |
|
1122 |
} |
|
1123 |
||
1124 |
/** |
|
1125 |
* Returns a clone if the delegate is cloneable. |
|
1126 |
* |
|
1127 |
* @return a clone if the delegate is cloneable. |
|
1128 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54483
diff
changeset
|
1129 |
* @throws CloneNotSupportedException if this is called on a |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18181
diff
changeset
|
1130 |
* delegate that does not support {@code Cloneable}. |
2 | 1131 |
*/ |
1132 |
public Object clone() throws CloneNotSupportedException { |
|
1133 |
chooseFirstProvider(); |
|
1134 |
if (sigSpi instanceof Cloneable) { |
|
1135 |
SignatureSpi sigSpiClone = (SignatureSpi)sigSpi.clone(); |
|
1136 |
// Because 'algorithm' and 'provider' are private |
|
1137 |
// members of our supertype, we must perform a cast to |
|
1138 |
// access them. |
|
1139 |
Signature that = |
|
1140 |
new Delegate(sigSpiClone, ((Signature)this).algorithm); |
|
1141 |
that.provider = ((Signature)this).provider; |
|
1142 |
return that; |
|
1143 |
} else { |
|
1144 |
throw new CloneNotSupportedException(); |
|
1145 |
} |
|
1146 |
} |
|
1147 |
||
1148 |
private static SignatureSpi newInstance(Service s) |
|
1149 |
throws NoSuchAlgorithmException { |
|
1150 |
if (s.getType().equals("Cipher")) { |
|
1151 |
// must be NONEwithRSA |
|
1152 |
try { |
|
1153 |
Cipher c = Cipher.getInstance(RSA_CIPHER, s.getProvider()); |
|
1154 |
return new CipherAdapter(c); |
|
1155 |
} catch (NoSuchPaddingException e) { |
|
1156 |
throw new NoSuchAlgorithmException(e); |
|
1157 |
} |
|
1158 |
} else { |
|
1159 |
Object o = s.newInstance(null); |
|
1160 |
if (o instanceof SignatureSpi == false) { |
|
1161 |
throw new NoSuchAlgorithmException |
|
1162 |
("Not a SignatureSpi: " + o.getClass().getName()); |
|
1163 |
} |
|
1164 |
return (SignatureSpi)o; |
|
1165 |
} |
|
1166 |
} |
|
1167 |
||
1168 |
// max number of debug warnings to print from chooseFirstProvider() |
|
1169 |
private static int warnCount = 10; |
|
1170 |
||
1171 |
/** |
|
1172 |
* Choose the Spi from the first provider available. Used if |
|
1173 |
* delayed provider selection is not possible because initSign()/ |
|
1174 |
* initVerify() is not the first method called. |
|
1175 |
*/ |
|
1176 |
void chooseFirstProvider() { |
|
1177 |
if (sigSpi != null) { |
|
1178 |
return; |
|
1179 |
} |
|
1180 |
synchronized (lock) { |
|
1181 |
if (sigSpi != null) { |
|
1182 |
return; |
|
1183 |
} |
|
1184 |
if (debug != null) { |
|
1185 |
int w = --warnCount; |
|
1186 |
if (w >= 0) { |
|
1187 |
debug.println("Signature.init() not first method " |
|
1188 |
+ "called, disabling delayed provider selection"); |
|
1189 |
if (w == 0) { |
|
1190 |
debug.println("Further warnings of this type will " |
|
1191 |
+ "be suppressed"); |
|
1192 |
} |
|
48080
18a4438eb690
8185855: Debug exception stacks should be clearer
coffeys
parents:
47216
diff
changeset
|
1193 |
new Exception("Debug call trace").printStackTrace(); |
2 | 1194 |
} |
1195 |
} |
|
1196 |
Exception lastException = null; |
|
1197 |
while ((firstService != null) || serviceIterator.hasNext()) { |
|
1198 |
Service s; |
|
1199 |
if (firstService != null) { |
|
1200 |
s = firstService; |
|
1201 |
firstService = null; |
|
1202 |
} else { |
|
1203 |
s = serviceIterator.next(); |
|
1204 |
} |
|
1205 |
if (isSpi(s) == false) { |
|
1206 |
continue; |
|
1207 |
} |
|
1208 |
try { |
|
1209 |
sigSpi = newInstance(s); |
|
1210 |
provider = s.getProvider(); |
|
1211 |
// not needed any more |
|
1212 |
firstService = null; |
|
1213 |
serviceIterator = null; |
|
1214 |
return; |
|
1215 |
} catch (NoSuchAlgorithmException e) { |
|
1216 |
lastException = e; |
|
1217 |
} |
|
1218 |
} |
|
1219 |
ProviderException e = new ProviderException |
|
1220 |
("Could not construct SignatureSpi instance"); |
|
1221 |
if (lastException != null) { |
|
1222 |
e.initCause(lastException); |
|
1223 |
} |
|
1224 |
throw e; |
|
1225 |
} |
|
1226 |
} |
|
1227 |
||
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1228 |
// Used by engineSetParameter/engineInitSign/engineInitVerify() to |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1229 |
// find the right provider with the supplied key, parameters, random source |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1230 |
private void chooseProvider(int type, Key key, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1231 |
AlgorithmParameterSpec params, SecureRandom random) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1232 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
2 | 1233 |
synchronized (lock) { |
1234 |
if (sigSpi != null) { |
|
1235 |
return; |
|
1236 |
} |
|
1237 |
Exception lastException = null; |
|
1238 |
while ((firstService != null) || serviceIterator.hasNext()) { |
|
1239 |
Service s; |
|
1240 |
if (firstService != null) { |
|
1241 |
s = firstService; |
|
1242 |
firstService = null; |
|
1243 |
} else { |
|
1244 |
s = serviceIterator.next(); |
|
1245 |
} |
|
1246 |
// if provider says it does not support this key, ignore it |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1247 |
if (key != null && s.supportsParameter(key) == false) { |
2 | 1248 |
continue; |
1249 |
} |
|
1250 |
// if instance is not a SignatureSpi, ignore it |
|
1251 |
if (isSpi(s) == false) { |
|
1252 |
continue; |
|
1253 |
} |
|
1254 |
try { |
|
1255 |
SignatureSpi spi = newInstance(s); |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1256 |
tryOperation(spi, type, key, params, random); |
2 | 1257 |
provider = s.getProvider(); |
1258 |
sigSpi = spi; |
|
1259 |
firstService = null; |
|
1260 |
serviceIterator = null; |
|
1261 |
return; |
|
1262 |
} catch (Exception e) { |
|
1263 |
// NoSuchAlgorithmException from newInstance() |
|
1264 |
// InvalidKeyException from init() |
|
1265 |
// RuntimeException (ProviderException) from init() |
|
1266 |
if (lastException == null) { |
|
1267 |
lastException = e; |
|
1268 |
} |
|
1269 |
} |
|
1270 |
} |
|
1271 |
// no working provider found, fail |
|
1272 |
if (lastException instanceof InvalidKeyException) { |
|
1273 |
throw (InvalidKeyException)lastException; |
|
1274 |
} |
|
1275 |
if (lastException instanceof RuntimeException) { |
|
1276 |
throw (RuntimeException)lastException; |
|
1277 |
} |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1278 |
if (lastException instanceof InvalidAlgorithmParameterException) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1279 |
throw (InvalidAlgorithmParameterException)lastException; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1280 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1281 |
|
2 | 1282 |
String k = (key != null) ? key.getClass().getName() : "(null)"; |
1283 |
throw new InvalidKeyException |
|
1284 |
("No installed provider supports this key: " |
|
1285 |
+ k, lastException); |
|
1286 |
} |
|
1287 |
} |
|
1288 |
||
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1289 |
private static final int I_PUB = 1; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1290 |
private static final int I_PRIV = 2; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1291 |
private static final int I_PRIV_SR = 3; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1292 |
private static final int I_PUB_PARAM = 4; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1293 |
private static final int I_PRIV_PARAM_SR = 5; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1294 |
private static final int S_PARAM = 6; |
2 | 1295 |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1296 |
private void tryOperation(SignatureSpi spi, int type, Key key, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1297 |
AlgorithmParameterSpec params, SecureRandom random) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1298 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
2 | 1299 |
switch (type) { |
1300 |
case I_PUB: |
|
1301 |
spi.engineInitVerify((PublicKey)key); |
|
1302 |
break; |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1303 |
case I_PUB_PARAM: |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1304 |
spi.engineInitVerify((PublicKey)key, params); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1305 |
break; |
2 | 1306 |
case I_PRIV: |
1307 |
spi.engineInitSign((PrivateKey)key); |
|
1308 |
break; |
|
1309 |
case I_PRIV_SR: |
|
1310 |
spi.engineInitSign((PrivateKey)key, random); |
|
1311 |
break; |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1312 |
case I_PRIV_PARAM_SR: |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1313 |
spi.engineInitSign((PrivateKey)key, params, random); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1314 |
break; |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1315 |
case S_PARAM: |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1316 |
spi.engineSetParameter(params); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1317 |
break; |
2 | 1318 |
default: |
1319 |
throw new AssertionError("Internal error: " + type); |
|
1320 |
} |
|
1321 |
} |
|
1322 |
||
1323 |
protected void engineInitVerify(PublicKey publicKey) |
|
1324 |
throws InvalidKeyException { |
|
1325 |
if (sigSpi != null) { |
|
1326 |
sigSpi.engineInitVerify(publicKey); |
|
1327 |
} else { |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1328 |
try { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1329 |
chooseProvider(I_PUB, publicKey, null, null); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1330 |
} catch (InvalidAlgorithmParameterException iape) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1331 |
// should not happen, re-throw as IKE just in case |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1332 |
throw new InvalidKeyException(iape); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1333 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1334 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1335 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1336 |
|
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1337 |
void engineInitVerify(PublicKey publicKey, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1338 |
AlgorithmParameterSpec params) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1339 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1340 |
if (sigSpi != null) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1341 |
sigSpi.engineInitVerify(publicKey, params); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1342 |
} else { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1343 |
chooseProvider(I_PUB_PARAM, publicKey, params, null); |
2 | 1344 |
} |
1345 |
} |
|
1346 |
||
1347 |
protected void engineInitSign(PrivateKey privateKey) |
|
1348 |
throws InvalidKeyException { |
|
1349 |
if (sigSpi != null) { |
|
1350 |
sigSpi.engineInitSign(privateKey); |
|
1351 |
} else { |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1352 |
try { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1353 |
chooseProvider(I_PRIV, privateKey, null, null); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1354 |
} catch (InvalidAlgorithmParameterException iape) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1355 |
// should not happen, re-throw as IKE just in case |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1356 |
throw new InvalidKeyException(iape); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1357 |
} |
2 | 1358 |
} |
1359 |
} |
|
1360 |
||
1361 |
protected void engineInitSign(PrivateKey privateKey, SecureRandom sr) |
|
1362 |
throws InvalidKeyException { |
|
1363 |
if (sigSpi != null) { |
|
1364 |
sigSpi.engineInitSign(privateKey, sr); |
|
1365 |
} else { |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1366 |
try { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1367 |
chooseProvider(I_PRIV_SR, privateKey, null, sr); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1368 |
} catch (InvalidAlgorithmParameterException iape) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1369 |
// should not happen, re-throw as IKE just in case |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1370 |
throw new InvalidKeyException(iape); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1371 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1372 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1373 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1374 |
|
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1375 |
void engineInitSign(PrivateKey privateKey, |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1376 |
AlgorithmParameterSpec params, SecureRandom sr) |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1377 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1378 |
if (sigSpi != null) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1379 |
sigSpi.engineInitSign(privateKey, params, sr); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1380 |
} else { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1381 |
chooseProvider(I_PRIV_PARAM_SR, privateKey, params, sr); |
2 | 1382 |
} |
1383 |
} |
|
1384 |
||
1385 |
protected void engineUpdate(byte b) throws SignatureException { |
|
1386 |
chooseFirstProvider(); |
|
1387 |
sigSpi.engineUpdate(b); |
|
1388 |
} |
|
1389 |
||
1390 |
protected void engineUpdate(byte[] b, int off, int len) |
|
1391 |
throws SignatureException { |
|
1392 |
chooseFirstProvider(); |
|
1393 |
sigSpi.engineUpdate(b, off, len); |
|
1394 |
} |
|
1395 |
||
1396 |
protected void engineUpdate(ByteBuffer data) { |
|
1397 |
chooseFirstProvider(); |
|
1398 |
sigSpi.engineUpdate(data); |
|
1399 |
} |
|
1400 |
||
1401 |
protected byte[] engineSign() throws SignatureException { |
|
1402 |
chooseFirstProvider(); |
|
1403 |
return sigSpi.engineSign(); |
|
1404 |
} |
|
1405 |
||
1406 |
protected int engineSign(byte[] outbuf, int offset, int len) |
|
1407 |
throws SignatureException { |
|
1408 |
chooseFirstProvider(); |
|
1409 |
return sigSpi.engineSign(outbuf, offset, len); |
|
1410 |
} |
|
1411 |
||
1412 |
protected boolean engineVerify(byte[] sigBytes) |
|
1413 |
throws SignatureException { |
|
1414 |
chooseFirstProvider(); |
|
1415 |
return sigSpi.engineVerify(sigBytes); |
|
1416 |
} |
|
1417 |
||
1418 |
protected boolean engineVerify(byte[] sigBytes, int offset, int length) |
|
1419 |
throws SignatureException { |
|
1420 |
chooseFirstProvider(); |
|
1421 |
return sigSpi.engineVerify(sigBytes, offset, length); |
|
1422 |
} |
|
1423 |
||
1424 |
protected void engineSetParameter(String param, Object value) |
|
1425 |
throws InvalidParameterException { |
|
1426 |
chooseFirstProvider(); |
|
1427 |
sigSpi.engineSetParameter(param, value); |
|
1428 |
} |
|
1429 |
||
1430 |
protected void engineSetParameter(AlgorithmParameterSpec params) |
|
1431 |
throws InvalidAlgorithmParameterException { |
|
54483
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1432 |
if (sigSpi != null) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1433 |
sigSpi.engineSetParameter(params); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1434 |
} else { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1435 |
try { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1436 |
chooseProvider(S_PARAM, null, params, null); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1437 |
} catch (InvalidKeyException ike) { |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1438 |
// should never happen, rethrow just in case |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1439 |
throw new InvalidAlgorithmParameterException(ike); |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1440 |
} |
ac20c3bdc55d
8216039: TLS with BC and RSASSA-PSS breaks ECDHServerKeyExchange
valeriep
parents:
53972
diff
changeset
|
1441 |
} |
2 | 1442 |
} |
1443 |
||
1444 |
protected Object engineGetParameter(String param) |
|
1445 |
throws InvalidParameterException { |
|
1446 |
chooseFirstProvider(); |
|
1447 |
return sigSpi.engineGetParameter(param); |
|
1448 |
} |
|
1449 |
||
1450 |
protected AlgorithmParameters engineGetParameters() { |
|
1451 |
chooseFirstProvider(); |
|
1452 |
return sigSpi.engineGetParameters(); |
|
1453 |
} |
|
1454 |
} |
|
1455 |
||
1456 |
// adapter for RSA/ECB/PKCS1Padding ciphers |
|
10709
d865c9f21240
7092375: Security Libraries don't build with javac -Werror
xuelei
parents:
9035
diff
changeset
|
1457 |
@SuppressWarnings("deprecation") |
2 | 1458 |
private static class CipherAdapter extends SignatureSpi { |
1459 |
||
1460 |
private final Cipher cipher; |
|
1461 |
||
1462 |
private ByteArrayOutputStream data; |
|
1463 |
||
1464 |
CipherAdapter(Cipher cipher) { |
|
1465 |
this.cipher = cipher; |
|
1466 |
} |
|
1467 |
||
1468 |
protected void engineInitVerify(PublicKey publicKey) |
|
1469 |
throws InvalidKeyException { |
|
1470 |
cipher.init(Cipher.DECRYPT_MODE, publicKey); |
|
1471 |
if (data == null) { |
|
1472 |
data = new ByteArrayOutputStream(128); |
|
1473 |
} else { |
|
1474 |
data.reset(); |
|
1475 |
} |
|
1476 |
} |
|
1477 |
||
1478 |
protected void engineInitSign(PrivateKey privateKey) |
|
1479 |
throws InvalidKeyException { |
|
1480 |
cipher.init(Cipher.ENCRYPT_MODE, privateKey); |
|
1481 |
data = null; |
|
1482 |
} |
|
1483 |
||
1484 |
protected void engineInitSign(PrivateKey privateKey, |
|
1485 |
SecureRandom random) throws InvalidKeyException { |
|
1486 |
cipher.init(Cipher.ENCRYPT_MODE, privateKey, random); |
|
1487 |
data = null; |
|
1488 |
} |
|
1489 |
||
1490 |
protected void engineUpdate(byte b) throws SignatureException { |
|
1491 |
engineUpdate(new byte[] {b}, 0, 1); |
|
1492 |
} |
|
1493 |
||
1494 |
protected void engineUpdate(byte[] b, int off, int len) |
|
1495 |
throws SignatureException { |
|
1496 |
if (data != null) { |
|
1497 |
data.write(b, off, len); |
|
1498 |
return; |
|
1499 |
} |
|
1500 |
byte[] out = cipher.update(b, off, len); |
|
1501 |
if ((out != null) && (out.length != 0)) { |
|
1502 |
throw new SignatureException |
|
1503 |
("Cipher unexpectedly returned data"); |
|
1504 |
} |
|
1505 |
} |
|
1506 |
||
1507 |
protected byte[] engineSign() throws SignatureException { |
|
1508 |
try { |
|
1509 |
return cipher.doFinal(); |
|
1510 |
} catch (IllegalBlockSizeException e) { |
|
1511 |
throw new SignatureException("doFinal() failed", e); |
|
1512 |
} catch (BadPaddingException e) { |
|
1513 |
throw new SignatureException("doFinal() failed", e); |
|
1514 |
} |
|
1515 |
} |
|
1516 |
||
1517 |
protected boolean engineVerify(byte[] sigBytes) |
|
1518 |
throws SignatureException { |
|
1519 |
try { |
|
1520 |
byte[] out = cipher.doFinal(sigBytes); |
|
1521 |
byte[] dataBytes = data.toByteArray(); |
|
1522 |
data.reset(); |
|
31695 | 1523 |
return MessageDigest.isEqual(out, dataBytes); |
2 | 1524 |
} catch (BadPaddingException e) { |
1525 |
// e.g. wrong public key used |
|
1526 |
// return false rather than throwing exception |
|
1527 |
return false; |
|
1528 |
} catch (IllegalBlockSizeException e) { |
|
1529 |
throw new SignatureException("doFinal() failed", e); |
|
1530 |
} |
|
1531 |
} |
|
1532 |
||
1533 |
protected void engineSetParameter(String param, Object value) |
|
1534 |
throws InvalidParameterException { |
|
1535 |
throw new InvalidParameterException("Parameters not supported"); |
|
1536 |
} |
|
1537 |
||
1538 |
protected Object engineGetParameter(String param) |
|
1539 |
throws InvalidParameterException { |
|
1540 |
throw new InvalidParameterException("Parameters not supported"); |
|
1541 |
} |
|
1542 |
||
1543 |
} |
|
1544 |
||
1545 |
} |