1 /* |
1 /* |
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
28 import java.io.IOException; |
28 import java.io.IOException; |
29 import java.nio.ByteBuffer; |
29 import java.nio.ByteBuffer; |
30 |
30 |
31 import java.security.*; |
31 import java.security.*; |
32 import java.security.interfaces.*; |
32 import java.security.interfaces.*; |
33 |
33 import java.security.spec.AlgorithmParameterSpec; |
|
34 |
|
35 import sun.security.rsa.RSAUtil.KeyType; |
34 import sun.security.util.*; |
36 import sun.security.util.*; |
35 import sun.security.x509.AlgorithmId; |
37 import sun.security.x509.AlgorithmId; |
36 |
38 |
37 /** |
39 /** |
38 * PKCS#1 RSA signatures with the various message digest algorithms. |
40 * PKCS#1 v1.5 RSA signatures with the various message digest algorithms. |
39 * This file contains an abstract base class with all the logic plus |
41 * This file contains an abstract base class with all the logic plus |
40 * a nested static class for each of the message digest algorithms |
42 * a nested static class for each of the message digest algorithms |
41 * (see end of the file). We support MD2, MD5, SHA-1, SHA-224, SHA-256, |
43 * (see end of the file). We support MD2, MD5, SHA-1, SHA-224, SHA-256, |
42 * SHA-384, and SHA-512. |
44 * SHA-384, SHA-512, SHA-512/224, and SHA-512/256. |
43 * |
45 * |
44 * @since 1.5 |
46 * @since 1.5 |
45 * @author Andreas Sterbenz |
47 * @author Andreas Sterbenz |
46 */ |
48 */ |
47 public abstract class RSASignature extends SignatureSpi { |
49 public abstract class RSASignature extends SignatureSpi { |
83 digestReset = true; |
85 digestReset = true; |
84 encodedLength = baseLength + oidLength + md.getDigestLength(); |
86 encodedLength = baseLength + oidLength + md.getDigestLength(); |
85 } |
87 } |
86 |
88 |
87 // initialize for verification. See JCA doc |
89 // initialize for verification. See JCA doc |
|
90 @Override |
88 protected void engineInitVerify(PublicKey publicKey) |
91 protected void engineInitVerify(PublicKey publicKey) |
89 throws InvalidKeyException { |
92 throws InvalidKeyException { |
90 RSAPublicKey rsaKey = (RSAPublicKey)RSAKeyFactory.toRSAKey(publicKey); |
93 RSAPublicKey rsaKey = (RSAPublicKey)RSAKeyFactory.toRSAKey(publicKey); |
91 this.privateKey = null; |
94 this.privateKey = null; |
92 this.publicKey = rsaKey; |
95 this.publicKey = rsaKey; |
93 initCommon(rsaKey, null); |
96 initCommon(rsaKey, null); |
94 } |
97 } |
95 |
98 |
96 // initialize for signing. See JCA doc |
99 // initialize for signing. See JCA doc |
|
100 @Override |
97 protected void engineInitSign(PrivateKey privateKey) |
101 protected void engineInitSign(PrivateKey privateKey) |
98 throws InvalidKeyException { |
102 throws InvalidKeyException { |
99 engineInitSign(privateKey, null); |
103 engineInitSign(privateKey, null); |
100 } |
104 } |
101 |
105 |
102 // initialize for signing. See JCA doc |
106 // initialize for signing. See JCA doc |
|
107 @Override |
103 protected void engineInitSign(PrivateKey privateKey, SecureRandom random) |
108 protected void engineInitSign(PrivateKey privateKey, SecureRandom random) |
104 throws InvalidKeyException { |
109 throws InvalidKeyException { |
105 RSAPrivateKey rsaKey = |
110 RSAPrivateKey rsaKey = |
106 (RSAPrivateKey)RSAKeyFactory.toRSAKey(privateKey); |
111 (RSAPrivateKey)RSAKeyFactory.toRSAKey(privateKey); |
107 this.privateKey = rsaKey; |
112 this.privateKey = rsaKey; |
112 /** |
117 /** |
113 * Init code common to sign and verify. |
118 * Init code common to sign and verify. |
114 */ |
119 */ |
115 private void initCommon(RSAKey rsaKey, SecureRandom random) |
120 private void initCommon(RSAKey rsaKey, SecureRandom random) |
116 throws InvalidKeyException { |
121 throws InvalidKeyException { |
|
122 try { |
|
123 RSAUtil.checkParamsAgainstType(KeyType.RSA, rsaKey.getParams()); |
|
124 } catch (ProviderException e) { |
|
125 throw new InvalidKeyException("Invalid key for RSA signatures", e); |
|
126 } |
117 resetDigest(); |
127 resetDigest(); |
118 int keySize = RSACore.getByteLength(rsaKey); |
128 int keySize = RSACore.getByteLength(rsaKey); |
119 try { |
129 try { |
120 padding = RSAPadding.getInstance |
130 padding = RSAPadding.getInstance |
121 (RSAPadding.PAD_BLOCKTYPE_1, keySize, random); |
131 (RSAPadding.PAD_BLOCKTYPE_1, keySize, random); |
146 digestReset = true; |
156 digestReset = true; |
147 return md.digest(); |
157 return md.digest(); |
148 } |
158 } |
149 |
159 |
150 // update the signature with the plaintext data. See JCA doc |
160 // update the signature with the plaintext data. See JCA doc |
|
161 @Override |
151 protected void engineUpdate(byte b) throws SignatureException { |
162 protected void engineUpdate(byte b) throws SignatureException { |
152 md.update(b); |
163 md.update(b); |
153 digestReset = false; |
164 digestReset = false; |
154 } |
165 } |
155 |
166 |
156 // update the signature with the plaintext data. See JCA doc |
167 // update the signature with the plaintext data. See JCA doc |
|
168 @Override |
157 protected void engineUpdate(byte[] b, int off, int len) |
169 protected void engineUpdate(byte[] b, int off, int len) |
158 throws SignatureException { |
170 throws SignatureException { |
159 md.update(b, off, len); |
171 md.update(b, off, len); |
160 digestReset = false; |
172 digestReset = false; |
161 } |
173 } |
162 |
174 |
163 // update the signature with the plaintext data. See JCA doc |
175 // update the signature with the plaintext data. See JCA doc |
|
176 @Override |
164 protected void engineUpdate(ByteBuffer b) { |
177 protected void engineUpdate(ByteBuffer b) { |
165 md.update(b); |
178 md.update(b); |
166 digestReset = false; |
179 digestReset = false; |
167 } |
180 } |
168 |
181 |
169 // sign the data and return the signature. See JCA doc |
182 // sign the data and return the signature. See JCA doc |
|
183 @Override |
170 protected byte[] engineSign() throws SignatureException { |
184 protected byte[] engineSign() throws SignatureException { |
|
185 if (privateKey == null) { |
|
186 throw new SignatureException("Missing private key"); |
|
187 } |
171 byte[] digest = getDigestValue(); |
188 byte[] digest = getDigestValue(); |
172 try { |
189 try { |
173 byte[] encoded = encodeSignature(digestOID, digest); |
190 byte[] encoded = encodeSignature(digestOID, digest); |
174 byte[] padded = padding.pad(encoded); |
191 byte[] padded = padding.pad(encoded); |
175 byte[] encrypted = RSACore.rsa(padded, privateKey, true); |
192 byte[] encrypted = RSACore.rsa(padded, privateKey, true); |
181 } |
198 } |
182 } |
199 } |
183 |
200 |
184 // verify the data and return the result. See JCA doc |
201 // verify the data and return the result. See JCA doc |
185 // should be reset to the state after engineInitVerify call. |
202 // should be reset to the state after engineInitVerify call. |
|
203 @Override |
186 protected boolean engineVerify(byte[] sigBytes) throws SignatureException { |
204 protected boolean engineVerify(byte[] sigBytes) throws SignatureException { |
|
205 if (publicKey == null) { |
|
206 throw new SignatureException("Missing public key"); |
|
207 } |
187 try { |
208 try { |
188 if (sigBytes.length != RSACore.getByteLength(publicKey)) { |
209 if (sigBytes.length != RSACore.getByteLength(publicKey)) { |
189 throw new SignatureException("Signature length not correct: got " + |
210 throw new SignatureException("Signature length not correct: got " + |
190 sigBytes.length + " but was expecting " + |
211 sigBytes.length + " but was expecting " + |
191 RSACore.getByteLength(publicKey)); |
212 RSACore.getByteLength(publicKey)); |
246 return digest; |
267 return digest; |
247 } |
268 } |
248 |
269 |
249 // set parameter, not supported. See JCA doc |
270 // set parameter, not supported. See JCA doc |
250 @Deprecated |
271 @Deprecated |
|
272 @Override |
251 protected void engineSetParameter(String param, Object value) |
273 protected void engineSetParameter(String param, Object value) |
252 throws InvalidParameterException { |
274 throws InvalidParameterException { |
253 throw new UnsupportedOperationException("setParameter() not supported"); |
275 throw new UnsupportedOperationException("setParameter() not supported"); |
254 } |
276 } |
255 |
277 |
|
278 // See JCA doc |
|
279 @Override |
|
280 protected void engineSetParameter(AlgorithmParameterSpec params) |
|
281 throws InvalidAlgorithmParameterException { |
|
282 if (params != null) { |
|
283 throw new InvalidAlgorithmParameterException("No parameters accepted"); |
|
284 } |
|
285 } |
|
286 |
256 // get parameter, not supported. See JCA doc |
287 // get parameter, not supported. See JCA doc |
257 @Deprecated |
288 @Deprecated |
|
289 @Override |
258 protected Object engineGetParameter(String param) |
290 protected Object engineGetParameter(String param) |
259 throws InvalidParameterException { |
291 throws InvalidParameterException { |
260 throw new UnsupportedOperationException("getParameter() not supported"); |
292 throw new UnsupportedOperationException("getParameter() not supported"); |
|
293 } |
|
294 |
|
295 // See JCA doc |
|
296 @Override |
|
297 protected AlgorithmParameters engineGetParameters() { |
|
298 return null; |
261 } |
299 } |
262 |
300 |
263 // Nested class for MD2withRSA signatures |
301 // Nested class for MD2withRSA signatures |
264 public static final class MD2withRSA extends RSASignature { |
302 public static final class MD2withRSA extends RSASignature { |
265 public MD2withRSA() { |
303 public MD2withRSA() { |
307 public SHA512withRSA() { |
345 public SHA512withRSA() { |
308 super("SHA-512", AlgorithmId.SHA512_oid, 11); |
346 super("SHA-512", AlgorithmId.SHA512_oid, 11); |
309 } |
347 } |
310 } |
348 } |
311 |
349 |
|
350 // Nested class for SHA512/224withRSA signatures |
|
351 public static final class SHA512_224withRSA extends RSASignature { |
|
352 public SHA512_224withRSA() { |
|
353 super("SHA-512/224", AlgorithmId.SHA512_224_oid, 11); |
|
354 } |
|
355 } |
|
356 |
|
357 // Nested class for SHA512/256withRSA signatures |
|
358 public static final class SHA512_256withRSA extends RSASignature { |
|
359 public SHA512_256withRSA() { |
|
360 super("SHA-512/256", AlgorithmId.SHA512_256_oid, 11); |
|
361 } |
|
362 } |
312 } |
363 } |