/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLRSAKeyFactory.java

https://bitbucket.org/aways/android_libcore · Java · 202 lines · 166 code · 21 blank · 15 comment · 42 complexity · aa0d4969e25b56cf1c91d3488cdc559d MD5 · raw file

  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.harmony.xnet.provider.jsse;
  17. import java.math.BigInteger;
  18. import java.security.InvalidKeyException;
  19. import java.security.Key;
  20. import java.security.KeyFactorySpi;
  21. import java.security.PrivateKey;
  22. import java.security.PublicKey;
  23. import java.security.interfaces.RSAPrivateCrtKey;
  24. import java.security.interfaces.RSAPrivateKey;
  25. import java.security.interfaces.RSAPublicKey;
  26. import java.security.spec.InvalidKeySpecException;
  27. import java.security.spec.KeySpec;
  28. import java.security.spec.PKCS8EncodedKeySpec;
  29. import java.security.spec.RSAPrivateCrtKeySpec;
  30. import java.security.spec.RSAPrivateKeySpec;
  31. import java.security.spec.RSAPublicKeySpec;
  32. import java.security.spec.X509EncodedKeySpec;
  33. public class OpenSSLRSAKeyFactory<T, S> extends KeyFactorySpi {
  34. @Override
  35. protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
  36. if (keySpec instanceof RSAPublicKeySpec) {
  37. RSAPublicKeySpec rsaKeySpec = (RSAPublicKeySpec) keySpec;
  38. return new OpenSSLRSAPublicKey(rsaKeySpec);
  39. } else if (keySpec instanceof X509EncodedKeySpec) {
  40. X509EncodedKeySpec x509KeySpec = (X509EncodedKeySpec) keySpec;
  41. try {
  42. final OpenSSLKey key = new OpenSSLKey(
  43. NativeCrypto.d2i_PUBKEY(x509KeySpec.getEncoded()));
  44. return new OpenSSLRSAPublicKey(key);
  45. } catch (Exception e) {
  46. throw new InvalidKeySpecException(e);
  47. }
  48. }
  49. throw new InvalidKeySpecException("Must use RSAPublicKeySpec or X509EncodedKeySpec; was "
  50. + keySpec.getClass().getName());
  51. }
  52. @Override
  53. protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
  54. if (keySpec instanceof RSAPrivateCrtKeySpec) {
  55. RSAPrivateCrtKeySpec rsaKeySpec = (RSAPrivateCrtKeySpec) keySpec;
  56. return new OpenSSLRSAPrivateCrtKey(rsaKeySpec);
  57. } else if (keySpec instanceof RSAPrivateKeySpec) {
  58. RSAPrivateKeySpec rsaKeySpec = (RSAPrivateKeySpec) keySpec;
  59. return new OpenSSLRSAPrivateKey(rsaKeySpec);
  60. } else if (keySpec instanceof PKCS8EncodedKeySpec) {
  61. PKCS8EncodedKeySpec pkcs8KeySpec = (PKCS8EncodedKeySpec) keySpec;
  62. try {
  63. final OpenSSLKey key = new OpenSSLKey(
  64. NativeCrypto.d2i_PKCS8_PRIV_KEY_INFO(pkcs8KeySpec.getEncoded()));
  65. return OpenSSLRSAPrivateKey.getInstance(key);
  66. } catch (Exception e) {
  67. throw new InvalidKeySpecException(e);
  68. }
  69. }
  70. throw new InvalidKeySpecException("Must use RSAPublicKeySpec or PKCS8EncodedKeySpec; was "
  71. + keySpec.getClass().getName());
  72. }
  73. @Override
  74. protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
  75. throws InvalidKeySpecException {
  76. if (key == null) {
  77. throw new InvalidKeySpecException("key == null");
  78. }
  79. if (keySpec == null) {
  80. throw new InvalidKeySpecException("keySpec == null");
  81. }
  82. if (key instanceof RSAPublicKey) {
  83. RSAPublicKey rsaKey = (RSAPublicKey) key;
  84. if (RSAPublicKeySpec.class.equals(keySpec)) {
  85. BigInteger modulus = rsaKey.getModulus();
  86. BigInteger publicExponent = rsaKey.getPublicExponent();
  87. return (T) new RSAPublicKeySpec(modulus, publicExponent);
  88. } else if (X509EncodedKeySpec.class.equals(keySpec)) {
  89. return (T) new X509EncodedKeySpec(key.getEncoded());
  90. } else {
  91. throw new InvalidKeySpecException("Must be RSAPublicKeySpec or X509EncodedKeySpec");
  92. }
  93. } else if (key instanceof RSAPrivateCrtKey) {
  94. RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
  95. if (RSAPrivateKeySpec.class.equals(keySpec)) {
  96. BigInteger modulus = rsaKey.getModulus();
  97. BigInteger privateExponent = rsaKey.getPrivateExponent();
  98. return (T) new RSAPrivateKeySpec(modulus, privateExponent);
  99. } else if (RSAPrivateCrtKeySpec.class.equals(keySpec)) {
  100. BigInteger modulus = rsaKey.getModulus();
  101. BigInteger publicExponent = rsaKey.getPublicExponent();
  102. BigInteger privateExponent = rsaKey.getPrivateExponent();
  103. BigInteger primeP = rsaKey.getPrimeP();
  104. BigInteger primeQ = rsaKey.getPrimeQ();
  105. BigInteger primeExponentP = rsaKey.getPrimeExponentP();
  106. BigInteger primeExponentQ = rsaKey.getPrimeExponentQ();
  107. BigInteger crtCoefficient = rsaKey.getCrtCoefficient();
  108. return (T) new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent,
  109. primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient);
  110. } else if (PKCS8EncodedKeySpec.class.equals(keySpec)) {
  111. return (T) new PKCS8EncodedKeySpec(rsaKey.getEncoded());
  112. } else {
  113. throw new InvalidKeySpecException(
  114. "Must be RSAPrivateKeySpec or or RSAPrivateCrtKeySpec or PKCS8EncodedKeySpec");
  115. }
  116. } else if (key instanceof RSAPrivateKey) {
  117. RSAPrivateKey rsaKey = (RSAPrivateKey) key;
  118. if (RSAPrivateKeySpec.class.equals(keySpec)) {
  119. BigInteger modulus = rsaKey.getModulus();
  120. BigInteger privateExponent = rsaKey.getPrivateExponent();
  121. return (T) new RSAPrivateKeySpec(modulus, privateExponent);
  122. } else if (RSAPrivateCrtKeySpec.class.equals(keySpec)) {
  123. BigInteger modulus = rsaKey.getModulus();
  124. BigInteger privateExponent = rsaKey.getPrivateExponent();
  125. return (T) new RSAPrivateCrtKeySpec(modulus, null, privateExponent, null, null,
  126. null, null, null);
  127. } else if (PKCS8EncodedKeySpec.class.equals(keySpec)) {
  128. return (T) new PKCS8EncodedKeySpec(rsaKey.getEncoded());
  129. } else {
  130. throw new InvalidKeySpecException(
  131. "Must be RSAPrivateKeySpec or PKCS8EncodedKeySpec");
  132. }
  133. } else {
  134. throw new InvalidKeySpecException("Must be RSAPublicKey or RSAPrivateKey");
  135. }
  136. }
  137. @Override
  138. protected Key engineTranslateKey(Key key) throws InvalidKeyException {
  139. if (key == null) {
  140. throw new InvalidKeyException("key == null");
  141. }
  142. if (key instanceof RSAPublicKey) {
  143. RSAPublicKey rsaKey = (RSAPublicKey) key;
  144. try {
  145. return engineGeneratePublic(new RSAPublicKeySpec(rsaKey.getModulus(),
  146. rsaKey.getPublicExponent()));
  147. } catch (InvalidKeySpecException e) {
  148. throw new InvalidKeyException(e);
  149. }
  150. } else if (key instanceof RSAPrivateCrtKey) {
  151. RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
  152. BigInteger modulus = rsaKey.getModulus();
  153. BigInteger publicExponent = rsaKey.getPublicExponent();
  154. BigInteger privateExponent = rsaKey.getPrivateExponent();
  155. BigInteger primeP = rsaKey.getPrimeP();
  156. BigInteger primeQ = rsaKey.getPrimeQ();
  157. BigInteger primeExponentP = rsaKey.getPrimeExponentP();
  158. BigInteger primeExponentQ = rsaKey.getPrimeExponentQ();
  159. BigInteger crtCoefficient = rsaKey.getCrtCoefficient();
  160. try {
  161. return engineGeneratePrivate(new RSAPrivateCrtKeySpec(modulus, publicExponent,
  162. privateExponent, primeP, primeQ, primeExponentP, primeExponentQ,
  163. crtCoefficient));
  164. } catch (InvalidKeySpecException e) {
  165. throw new InvalidKeyException(e);
  166. }
  167. } else if (key instanceof RSAPrivateKey) {
  168. RSAPrivateKey rsaKey = (RSAPrivateKey) key;
  169. BigInteger modulus = rsaKey.getModulus();
  170. BigInteger privateExponent = rsaKey.getPrivateExponent();
  171. try {
  172. return engineGeneratePrivate(new RSAPrivateKeySpec(modulus, privateExponent));
  173. } catch (InvalidKeySpecException e) {
  174. throw new InvalidKeyException(e);
  175. }
  176. } else {
  177. throw new InvalidKeyException(
  178. "Key must be RSAPublicKey or RSAPrivateCrtKey or RSAPrivateKey");
  179. }
  180. }
  181. }