author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 19375 | jdk/src/share/classes/sun/security/rsa/RSASignature.java@cdfdb9c0590e |
child 29915 | 88af03f531f0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19375
cdfdb9c0590e
8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents:
12685
diff
changeset
|
2 |
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.rsa; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.util.Arrays; |
|
31 |
||
32 |
import java.security.*; |
|
33 |
import java.security.interfaces.*; |
|
34 |
||
35 |
import sun.security.util.*; |
|
36 |
import sun.security.x509.AlgorithmId; |
|
37 |
||
38 |
/** |
|
39 |
* PKCS#1 RSA signatures with the various message digest algorithms. |
|
40 |
* This file contains an abstract base class with all the logic plus |
|
41 |
* a nested static class for each of the message digest algorithms |
|
12685 | 42 |
* (see end of the file). We support MD2, MD5, SHA-1, SHA-224, SHA-256, |
43 |
* SHA-384, and SHA-512. |
|
2 | 44 |
* |
45 |
* @since 1.5 |
|
46 |
* @author Andreas Sterbenz |
|
47 |
*/ |
|
48 |
public abstract class RSASignature extends SignatureSpi { |
|
49 |
||
50 |
// we sign an ASN.1 SEQUENCE of AlgorithmId and digest |
|
7043 | 51 |
// it has the form 30:xx:30:xx:[digestOID]:05:00:04:xx:[digest] |
2 | 52 |
// this means the encoded length is (8 + digestOID.length + digest.length) |
53 |
private static final int baseLength = 8; |
|
54 |
||
55 |
// object identifier for the message digest algorithm used |
|
56 |
private final ObjectIdentifier digestOID; |
|
57 |
||
58 |
// length of the encoded signature blob |
|
59 |
private final int encodedLength; |
|
60 |
||
61 |
// message digest implementation we use |
|
62 |
private final MessageDigest md; |
|
63 |
// flag indicating whether the digest is reset |
|
64 |
private boolean digestReset; |
|
65 |
||
66 |
// private key, if initialized for signing |
|
67 |
private RSAPrivateKey privateKey; |
|
68 |
// public key, if initialized for verifying |
|
69 |
private RSAPublicKey publicKey; |
|
70 |
||
71 |
// padding to use, set when the initSign/initVerify is called |
|
72 |
private RSAPadding padding; |
|
73 |
||
74 |
/** |
|
75 |
* Construct a new RSASignature. Used by subclasses. |
|
76 |
*/ |
|
77 |
RSASignature(String algorithm, ObjectIdentifier digestOID, int oidLength) { |
|
78 |
this.digestOID = digestOID; |
|
79 |
try { |
|
80 |
md = MessageDigest.getInstance(algorithm); |
|
81 |
} catch (NoSuchAlgorithmException e) { |
|
82 |
throw new ProviderException(e); |
|
83 |
} |
|
84 |
digestReset = true; |
|
85 |
encodedLength = baseLength + oidLength + md.getDigestLength(); |
|
86 |
} |
|
87 |
||
88 |
// initialize for verification. See JCA doc |
|
89 |
protected void engineInitVerify(PublicKey publicKey) |
|
90 |
throws InvalidKeyException { |
|
91 |
RSAPublicKey rsaKey = (RSAPublicKey)RSAKeyFactory.toRSAKey(publicKey); |
|
92 |
this.privateKey = null; |
|
93 |
this.publicKey = rsaKey; |
|
94 |
initCommon(rsaKey, null); |
|
95 |
} |
|
96 |
||
97 |
// initialize for signing. See JCA doc |
|
98 |
protected void engineInitSign(PrivateKey privateKey) |
|
99 |
throws InvalidKeyException { |
|
100 |
engineInitSign(privateKey, null); |
|
101 |
} |
|
102 |
||
103 |
// initialize for signing. See JCA doc |
|
104 |
protected void engineInitSign(PrivateKey privateKey, SecureRandom random) |
|
105 |
throws InvalidKeyException { |
|
7043 | 106 |
RSAPrivateKey rsaKey = |
107 |
(RSAPrivateKey)RSAKeyFactory.toRSAKey(privateKey); |
|
2 | 108 |
this.privateKey = rsaKey; |
109 |
this.publicKey = null; |
|
110 |
initCommon(rsaKey, random); |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Init code common to sign and verify. |
|
115 |
*/ |
|
116 |
private void initCommon(RSAKey rsaKey, SecureRandom random) |
|
117 |
throws InvalidKeyException { |
|
118 |
resetDigest(); |
|
119 |
int keySize = RSACore.getByteLength(rsaKey); |
|
120 |
try { |
|
121 |
padding = RSAPadding.getInstance |
|
122 |
(RSAPadding.PAD_BLOCKTYPE_1, keySize, random); |
|
123 |
} catch (InvalidAlgorithmParameterException iape) { |
|
124 |
throw new InvalidKeyException(iape.getMessage()); |
|
125 |
} |
|
126 |
int maxDataSize = padding.getMaxDataSize(); |
|
127 |
if (encodedLength > maxDataSize) { |
|
128 |
throw new InvalidKeyException |
|
129 |
("Key is too short for this signature algorithm"); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Reset the message digest if it is not already reset. |
|
135 |
*/ |
|
136 |
private void resetDigest() { |
|
137 |
if (digestReset == false) { |
|
138 |
md.reset(); |
|
139 |
digestReset = true; |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Return the message digest value. |
|
145 |
*/ |
|
146 |
private byte[] getDigestValue() { |
|
147 |
digestReset = true; |
|
148 |
return md.digest(); |
|
149 |
} |
|
150 |
||
151 |
// update the signature with the plaintext data. See JCA doc |
|
152 |
protected void engineUpdate(byte b) throws SignatureException { |
|
153 |
md.update(b); |
|
154 |
digestReset = false; |
|
155 |
} |
|
156 |
||
157 |
// update the signature with the plaintext data. See JCA doc |
|
158 |
protected void engineUpdate(byte[] b, int off, int len) |
|
159 |
throws SignatureException { |
|
160 |
md.update(b, off, len); |
|
161 |
digestReset = false; |
|
162 |
} |
|
163 |
||
164 |
// update the signature with the plaintext data. See JCA doc |
|
165 |
protected void engineUpdate(ByteBuffer b) { |
|
166 |
md.update(b); |
|
167 |
digestReset = false; |
|
168 |
} |
|
169 |
||
170 |
// sign the data and return the signature. See JCA doc |
|
171 |
protected byte[] engineSign() throws SignatureException { |
|
172 |
byte[] digest = getDigestValue(); |
|
173 |
try { |
|
174 |
byte[] encoded = encodeSignature(digestOID, digest); |
|
175 |
byte[] padded = padding.pad(encoded); |
|
176 |
byte[] encrypted = RSACore.rsa(padded, privateKey); |
|
177 |
return encrypted; |
|
178 |
} catch (GeneralSecurityException e) { |
|
179 |
throw new SignatureException("Could not sign data", e); |
|
180 |
} catch (IOException e) { |
|
181 |
throw new SignatureException("Could not encode data", e); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
// verify the data and return the result. See JCA doc |
|
186 |
protected boolean engineVerify(byte[] sigBytes) throws SignatureException { |
|
7526
78a87adede1e
6896700: Validation of signatures succeed when it should fail
weijun
parents:
7043
diff
changeset
|
187 |
if (sigBytes.length != RSACore.getByteLength(publicKey)) { |
78a87adede1e
6896700: Validation of signatures succeed when it should fail
weijun
parents:
7043
diff
changeset
|
188 |
throw new SignatureException("Signature length not correct: got " + |
78a87adede1e
6896700: Validation of signatures succeed when it should fail
weijun
parents:
7043
diff
changeset
|
189 |
sigBytes.length + " but was expecting " + |
78a87adede1e
6896700: Validation of signatures succeed when it should fail
weijun
parents:
7043
diff
changeset
|
190 |
RSACore.getByteLength(publicKey)); |
78a87adede1e
6896700: Validation of signatures succeed when it should fail
weijun
parents:
7043
diff
changeset
|
191 |
} |
2 | 192 |
byte[] digest = getDigestValue(); |
193 |
try { |
|
194 |
byte[] decrypted = RSACore.rsa(sigBytes, publicKey); |
|
195 |
byte[] unpadded = padding.unpad(decrypted); |
|
196 |
byte[] decodedDigest = decodeSignature(digestOID, unpadded); |
|
197 |
return Arrays.equals(digest, decodedDigest); |
|
198 |
} catch (javax.crypto.BadPaddingException e) { |
|
199 |
// occurs if the app has used the wrong RSA public key |
|
200 |
// or if sigBytes is invalid |
|
201 |
// return false rather than propagating the exception for |
|
202 |
// compatibility/ease of use |
|
203 |
return false; |
|
204 |
} catch (IOException e) { |
|
205 |
throw new SignatureException("Signature encoding error", e); |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
/** |
|
210 |
* Encode the digest, return the to-be-signed data. |
|
211 |
* Also used by the PKCS#11 provider. |
|
212 |
*/ |
|
213 |
public static byte[] encodeSignature(ObjectIdentifier oid, byte[] digest) |
|
214 |
throws IOException { |
|
215 |
DerOutputStream out = new DerOutputStream(); |
|
216 |
new AlgorithmId(oid).encode(out); |
|
217 |
out.putOctetString(digest); |
|
7043 | 218 |
DerValue result = |
219 |
new DerValue(DerValue.tag_Sequence, out.toByteArray()); |
|
2 | 220 |
return result.toByteArray(); |
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Decode the signature data. Verify that the object identifier matches |
|
225 |
* and return the message digest. |
|
226 |
*/ |
|
227 |
public static byte[] decodeSignature(ObjectIdentifier oid, byte[] signature) |
|
228 |
throws IOException { |
|
229 |
DerInputStream in = new DerInputStream(signature); |
|
230 |
DerValue[] values = in.getSequence(2); |
|
231 |
if ((values.length != 2) || (in.available() != 0)) { |
|
232 |
throw new IOException("SEQUENCE length error"); |
|
233 |
} |
|
234 |
AlgorithmId algId = AlgorithmId.parse(values[0]); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7526
diff
changeset
|
235 |
if (algId.getOID().equals((Object)oid) == false) { |
7043 | 236 |
throw new IOException("ObjectIdentifier mismatch: " |
237 |
+ algId.getOID()); |
|
2 | 238 |
} |
239 |
if (algId.getEncodedParams() != null) { |
|
240 |
throw new IOException("Unexpected AlgorithmId parameters"); |
|
241 |
} |
|
242 |
byte[] digest = values[1].getOctetString(); |
|
243 |
return digest; |
|
244 |
} |
|
245 |
||
246 |
// set parameter, not supported. See JCA doc |
|
19375
cdfdb9c0590e
8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents:
12685
diff
changeset
|
247 |
@Deprecated |
2 | 248 |
protected void engineSetParameter(String param, Object value) |
249 |
throws InvalidParameterException { |
|
250 |
throw new UnsupportedOperationException("setParameter() not supported"); |
|
251 |
} |
|
252 |
||
253 |
// get parameter, not supported. See JCA doc |
|
19375
cdfdb9c0590e
8022461: Fix lint warnings in sun.security.{provider,rsa,x509}
juh
parents:
12685
diff
changeset
|
254 |
@Deprecated |
2 | 255 |
protected Object engineGetParameter(String param) |
256 |
throws InvalidParameterException { |
|
257 |
throw new UnsupportedOperationException("getParameter() not supported"); |
|
258 |
} |
|
259 |
||
260 |
// Nested class for MD2withRSA signatures |
|
261 |
public static final class MD2withRSA extends RSASignature { |
|
262 |
public MD2withRSA() { |
|
263 |
super("MD2", AlgorithmId.MD2_oid, 10); |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
// Nested class for MD5withRSA signatures |
|
268 |
public static final class MD5withRSA extends RSASignature { |
|
269 |
public MD5withRSA() { |
|
270 |
super("MD5", AlgorithmId.MD5_oid, 10); |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
// Nested class for SHA1withRSA signatures |
|
275 |
public static final class SHA1withRSA extends RSASignature { |
|
276 |
public SHA1withRSA() { |
|
277 |
super("SHA-1", AlgorithmId.SHA_oid, 7); |
|
278 |
} |
|
279 |
} |
|
280 |
||
12685 | 281 |
// Nested class for SHA224withRSA signatures |
282 |
public static final class SHA224withRSA extends RSASignature { |
|
283 |
public SHA224withRSA() { |
|
284 |
super("SHA-224", AlgorithmId.SHA224_oid, 11); |
|
285 |
} |
|
286 |
} |
|
287 |
||
2 | 288 |
// Nested class for SHA256withRSA signatures |
289 |
public static final class SHA256withRSA extends RSASignature { |
|
290 |
public SHA256withRSA() { |
|
291 |
super("SHA-256", AlgorithmId.SHA256_oid, 11); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
// Nested class for SHA384withRSA signatures |
|
296 |
public static final class SHA384withRSA extends RSASignature { |
|
297 |
public SHA384withRSA() { |
|
298 |
super("SHA-384", AlgorithmId.SHA384_oid, 11); |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
// Nested class for SHA512withRSA signatures |
|
303 |
public static final class SHA512withRSA extends RSASignature { |
|
304 |
public SHA512withRSA() { |
|
305 |
super("SHA-512", AlgorithmId.SHA512_oid, 11); |
|
306 |
} |
|
307 |
} |
|
308 |
||
309 |
} |