1 /* |
1 /* |
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2014, 2015, 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 |
35 import java.security.InvalidKeyException; |
35 import java.security.InvalidKeyException; |
36 import java.security.Key; |
36 import java.security.Key; |
37 import java.security.PublicKey; |
37 import java.security.PublicKey; |
38 import java.security.PrivateKey; |
38 import java.security.PrivateKey; |
39 import java.security.spec.RSAPrivateCrtKeySpec; |
39 import java.security.spec.RSAPrivateCrtKeySpec; |
|
40 import java.security.spec.RSAPrivateKeySpec; |
40 import java.security.spec.RSAPublicKeySpec; |
41 import java.security.spec.RSAPublicKeySpec; |
41 import java.security.interfaces.RSAKey; |
42 import java.security.interfaces.RSAKey; |
42 import java.security.interfaces.RSAPrivateCrtKey; |
43 import java.security.interfaces.RSAPrivateCrtKey; |
|
44 import java.security.interfaces.RSAPrivateKey; |
43 import java.security.interfaces.RSAPublicKey; |
45 import java.security.interfaces.RSAPublicKey; |
44 |
46 |
45 import java.security.KeyFactory; |
47 import java.security.KeyFactory; |
46 import java.security.NoSuchAlgorithmException; |
48 import java.security.NoSuchAlgorithmException; |
47 import java.security.SecureRandom; |
49 import java.security.SecureRandom; |
203 boolean doEncrypt = (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE); |
205 boolean doEncrypt = (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE); |
204 |
206 |
205 // Make sure the proper opmode uses the proper key |
207 // Make sure the proper opmode uses the proper key |
206 if (doEncrypt && (!(newKey instanceof RSAPublicKey))) { |
208 if (doEncrypt && (!(newKey instanceof RSAPublicKey))) { |
207 throw new InvalidKeyException("RSAPublicKey required for encryption"); |
209 throw new InvalidKeyException("RSAPublicKey required for encryption"); |
208 } else if (!doEncrypt && (!(newKey instanceof RSAPrivateCrtKey))) { |
210 } else if (!doEncrypt && (!(newKey instanceof RSAPrivateKey))) { |
209 throw new InvalidKeyException("RSAPrivateCrtKey required for decryption"); |
211 throw new InvalidKeyException("RSAPrivateKey required for decryption"); |
210 } |
212 } |
211 |
213 |
212 NativeKey nativeKey = null; |
214 NativeKey nativeKey = null; |
213 // Check keyList cache for a nativeKey |
215 // Check keyList cache for a nativeKey |
214 nativeKey = keyList.get(newKey); |
216 nativeKey = keyList.get(newKey); |
221 (new RSAPublicKeySpec(publicKey.getModulus(), publicKey.getPublicExponent())); |
223 (new RSAPublicKeySpec(publicKey.getModulus(), publicKey.getPublicExponent())); |
222 } catch (InvalidKeySpecException ikse) { |
224 } catch (InvalidKeySpecException ikse) { |
223 throw new InvalidKeyException(ikse); |
225 throw new InvalidKeyException(ikse); |
224 } |
226 } |
225 } else { |
227 } else { |
226 RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) newKey; |
|
227 try { |
228 try { |
228 nativeKey = (NativeKey) keyFactory.engineGeneratePrivate |
229 if (newKey instanceof RSAPrivateCrtKey) { |
229 (new RSAPrivateCrtKeySpec(privateKey.getModulus(), |
230 RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) newKey; |
230 privateKey.getPublicExponent(), |
231 nativeKey = (NativeKey) keyFactory.engineGeneratePrivate |
231 privateKey.getPrivateExponent(), |
232 (new RSAPrivateCrtKeySpec(privateKey.getModulus(), |
232 privateKey.getPrimeP(), |
233 privateKey.getPublicExponent(), |
233 privateKey.getPrimeQ(), |
234 privateKey.getPrivateExponent(), |
234 privateKey.getPrimeExponentP(), |
235 privateKey.getPrimeP(), |
235 privateKey.getPrimeExponentQ(), |
236 privateKey.getPrimeQ(), |
236 privateKey.getCrtCoefficient())); |
237 privateKey.getPrimeExponentP(), |
|
238 privateKey.getPrimeExponentQ(), |
|
239 privateKey.getCrtCoefficient())); |
|
240 } else if (newKey instanceof RSAPrivateKey) { |
|
241 RSAPrivateKey privateKey = (RSAPrivateKey) newKey; |
|
242 nativeKey = (NativeKey) keyFactory.engineGeneratePrivate |
|
243 (new RSAPrivateKeySpec(privateKey.getModulus(), |
|
244 privateKey.getPrivateExponent())); |
|
245 } else { |
|
246 throw new InvalidKeyException("Unsupported type of RSAPrivateKey"); |
|
247 } |
237 } catch (InvalidKeySpecException ikse) { |
248 } catch (InvalidKeySpecException ikse) { |
238 throw new InvalidKeyException(ikse); |
249 throw new InvalidKeyException(ikse); |
239 } |
250 } |
240 } |
251 } |
241 |
252 |