1 /*
2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
159 if (doCancel) {
160 UcryptoProvider.debug("Resource: free Signature Ctxt " + this.id);
161 NativeRSASignature.nativeFinal(id, sign, null, 0, 0);
162 } else {
163 UcryptoProvider.debug("Resource: stop tracking Signature Ctxt " + this.id);
164 }
165 } finally {
166 this.clear();
167 }
168 }
169 }
170
171 NativeRSASignature(UcryptoMech mech, int encodedLen)
172 throws NoSuchAlgorithmException {
173 this.mech = mech;
174 this.encodedLen = encodedLen;
175 this.keyFactory = new NativeRSAKeyFactory();
176 }
177
178 // deprecated but abstract
179 @SuppressWarnings("deprecation")
180 protected Object engineGetParameter(String param) throws InvalidParameterException {
181 throw new UnsupportedOperationException("getParameter() not supported");
182 }
183
184 @Override
185 protected synchronized void engineInitSign(PrivateKey privateKey)
186 throws InvalidKeyException {
187 if (privateKey == null) {
188 throw new InvalidKeyException("Key must not be null");
189 }
190 NativeKey newKey = key;
191 int newSigLength = sigLength;
192 // Need to check RSA key length whenever a new private key is set
193 if (privateKey != key) {
194 if (!(privateKey instanceof RSAPrivateKey)) {
195 throw new InvalidKeyException("RSAPrivateKey required. " +
196 "Received: " + privateKey.getClass().getName());
197 }
198 RSAPrivateKey rsaPrivKey = (RSAPrivateKey) privateKey;
199 BigInteger mod = rsaPrivKey.getModulus();
200 newSigLength = checkRSAKeyLength(mod);
201 BigInteger pe = rsaPrivKey.getPrivateExponent();
202 try {
203 if (rsaPrivKey instanceof RSAPrivateCrtKey) {
204 RSAPrivateCrtKey rsaPrivCrtKey = (RSAPrivateCrtKey) rsaPrivKey;
234 // Need to check RSA key length whenever a new public key is set
235 if (publicKey != key) {
236 if (publicKey instanceof RSAPublicKey) {
237 BigInteger mod = ((RSAPublicKey) publicKey).getModulus();
238 newSigLength = checkRSAKeyLength(mod);
239 try {
240 newKey = (NativeKey) keyFactory.engineGeneratePublic
241 (new RSAPublicKeySpec(mod, ((RSAPublicKey) publicKey).getPublicExponent()));
242 } catch (InvalidKeySpecException ikse) {
243 throw new InvalidKeyException(ikse);
244 }
245 } else {
246 throw new InvalidKeyException("RSAPublicKey required. " +
247 "Received: " + publicKey.getClass().getName());
248 }
249 }
250 init(false, newKey, newSigLength);
251 }
252
253 // deprecated but abstract
254 @SuppressWarnings("deprecation")
255 protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
256 throw new UnsupportedOperationException("setParameter() not supported");
257 }
258
259 @Override
260 protected synchronized byte[] engineSign() throws SignatureException {
261 try {
262 byte[] sig = new byte[sigLength];
263 int rv = doFinal(sig, 0, sigLength);
264 if (rv < 0) {
265 throw new SignatureException(new UcryptoException(-rv));
266 }
267 return sig;
268 } finally {
269 // doFinal should already be called, no need to cancel
270 reset(false);
271 }
272 }
273
274 @Override
275 protected synchronized int engineSign(byte[] outbuf, int offset, int len)
276 throws SignatureException {
|
1 /*
2 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
159 if (doCancel) {
160 UcryptoProvider.debug("Resource: free Signature Ctxt " + this.id);
161 NativeRSASignature.nativeFinal(id, sign, null, 0, 0);
162 } else {
163 UcryptoProvider.debug("Resource: stop tracking Signature Ctxt " + this.id);
164 }
165 } finally {
166 this.clear();
167 }
168 }
169 }
170
171 NativeRSASignature(UcryptoMech mech, int encodedLen)
172 throws NoSuchAlgorithmException {
173 this.mech = mech;
174 this.encodedLen = encodedLen;
175 this.keyFactory = new NativeRSAKeyFactory();
176 }
177
178 // deprecated but abstract
179 @Override
180 @SuppressWarnings("deprecation")
181 protected Object engineGetParameter(String param) throws InvalidParameterException {
182 throw new UnsupportedOperationException("getParameter() not supported");
183 }
184
185 @Override
186 protected AlgorithmParameters engineGetParameters() {
187 return null;
188 }
189
190 @Override
191 protected synchronized void engineInitSign(PrivateKey privateKey)
192 throws InvalidKeyException {
193 if (privateKey == null) {
194 throw new InvalidKeyException("Key must not be null");
195 }
196 NativeKey newKey = key;
197 int newSigLength = sigLength;
198 // Need to check RSA key length whenever a new private key is set
199 if (privateKey != key) {
200 if (!(privateKey instanceof RSAPrivateKey)) {
201 throw new InvalidKeyException("RSAPrivateKey required. " +
202 "Received: " + privateKey.getClass().getName());
203 }
204 RSAPrivateKey rsaPrivKey = (RSAPrivateKey) privateKey;
205 BigInteger mod = rsaPrivKey.getModulus();
206 newSigLength = checkRSAKeyLength(mod);
207 BigInteger pe = rsaPrivKey.getPrivateExponent();
208 try {
209 if (rsaPrivKey instanceof RSAPrivateCrtKey) {
210 RSAPrivateCrtKey rsaPrivCrtKey = (RSAPrivateCrtKey) rsaPrivKey;
240 // Need to check RSA key length whenever a new public key is set
241 if (publicKey != key) {
242 if (publicKey instanceof RSAPublicKey) {
243 BigInteger mod = ((RSAPublicKey) publicKey).getModulus();
244 newSigLength = checkRSAKeyLength(mod);
245 try {
246 newKey = (NativeKey) keyFactory.engineGeneratePublic
247 (new RSAPublicKeySpec(mod, ((RSAPublicKey) publicKey).getPublicExponent()));
248 } catch (InvalidKeySpecException ikse) {
249 throw new InvalidKeyException(ikse);
250 }
251 } else {
252 throw new InvalidKeyException("RSAPublicKey required. " +
253 "Received: " + publicKey.getClass().getName());
254 }
255 }
256 init(false, newKey, newSigLength);
257 }
258
259 // deprecated but abstract
260 @Override
261 @SuppressWarnings("deprecation")
262 protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
263 throw new UnsupportedOperationException("setParameter() not supported");
264 }
265
266 @Override
267 protected void engineSetParameter(AlgorithmParameterSpec params)
268 throws InvalidAlgorithmParameterException {
269 if (params != null) {
270 throw new InvalidAlgorithmParameterException("No parameter accepted");
271 }
272 }
273
274 @Override
275 protected synchronized byte[] engineSign() throws SignatureException {
276 try {
277 byte[] sig = new byte[sigLength];
278 int rv = doFinal(sig, 0, sigLength);
279 if (rv < 0) {
280 throw new SignatureException(new UcryptoException(-rv));
281 }
282 return sig;
283 } finally {
284 // doFinal should already be called, no need to cancel
285 reset(false);
286 }
287 }
288
289 @Override
290 protected synchronized int engineSign(byte[] outbuf, int offset, int len)
291 throws SignatureException {
|