DHKeyFactory.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* DHKeyFactory.java -- DH key-factory JCE Adapter
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.jce.sig;
  32. import gnu.java.security.Registry;
  33. import gnu.javax.crypto.key.dh.DHKeyPairPKCS8Codec;
  34. import gnu.javax.crypto.key.dh.DHKeyPairX509Codec;
  35. import gnu.javax.crypto.key.dh.GnuDHPrivateKey;
  36. import gnu.javax.crypto.key.dh.GnuDHPublicKey;
  37. import java.math.BigInteger;
  38. import java.security.InvalidKeyException;
  39. import java.security.Key;
  40. import java.security.KeyFactorySpi;
  41. import java.security.PrivateKey;
  42. import java.security.PublicKey;
  43. import java.security.spec.InvalidKeySpecException;
  44. import java.security.spec.KeySpec;
  45. import java.security.spec.PKCS8EncodedKeySpec;
  46. import java.security.spec.X509EncodedKeySpec;
  47. import javax.crypto.interfaces.DHPrivateKey;
  48. import javax.crypto.interfaces.DHPublicKey;
  49. import javax.crypto.spec.DHPrivateKeySpec;
  50. import javax.crypto.spec.DHPublicKeySpec;
  51. /**
  52. * Implementation of a JCE Adapter for DH a key-factory.
  53. */
  54. public class DHKeyFactory
  55. extends KeyFactorySpi
  56. {
  57. // implicit 0-arguments constructor
  58. protected PublicKey engineGeneratePublic(KeySpec keySpec)
  59. throws InvalidKeySpecException
  60. {
  61. if (keySpec instanceof DHPublicKeySpec)
  62. {
  63. DHPublicKeySpec spec = (DHPublicKeySpec) keySpec;
  64. BigInteger p = spec.getP();
  65. BigInteger g = spec.getG();
  66. BigInteger y = spec.getY();
  67. return new GnuDHPublicKey(Registry.X509_ENCODING_ID, null, p, g, y);
  68. }
  69. if (keySpec instanceof X509EncodedKeySpec)
  70. {
  71. X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
  72. byte[] encoded = spec.getEncoded();
  73. PublicKey result;
  74. try
  75. {
  76. result = new DHKeyPairX509Codec().decodePublicKey(encoded);
  77. return result;
  78. }
  79. catch (RuntimeException x)
  80. {
  81. InvalidKeySpecException y = new InvalidKeySpecException();
  82. y.initCause(x);
  83. throw y;
  84. }
  85. }
  86. throw new InvalidKeySpecException("Unsupported (public) key specification");
  87. }
  88. protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
  89. throws InvalidKeySpecException
  90. {
  91. if (keySpec instanceof DHPrivateKeySpec)
  92. {
  93. DHPrivateKeySpec spec = (DHPrivateKeySpec) keySpec;
  94. BigInteger p = spec.getP();
  95. BigInteger g = spec.getG();
  96. BigInteger x = spec.getX();
  97. return new GnuDHPrivateKey(Registry.PKCS8_ENCODING_ID, null, p, g, x);
  98. }
  99. if (keySpec instanceof PKCS8EncodedKeySpec)
  100. {
  101. PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
  102. byte[] encoded = spec.getEncoded();
  103. PrivateKey result;
  104. try
  105. {
  106. result = new DHKeyPairPKCS8Codec().decodePrivateKey(encoded);
  107. return result;
  108. }
  109. catch (RuntimeException x)
  110. {
  111. InvalidKeySpecException y = new InvalidKeySpecException();
  112. y.initCause(x);
  113. throw y;
  114. }
  115. }
  116. throw new InvalidKeySpecException("Unsupported (private) key specification");
  117. }
  118. protected KeySpec engineGetKeySpec(Key key, Class keySpec)
  119. throws InvalidKeySpecException
  120. {
  121. if (key instanceof DHPublicKey)
  122. {
  123. if (keySpec.isAssignableFrom(DHPublicKeySpec.class))
  124. {
  125. DHPublicKey dssKey = (DHPublicKey) key;
  126. BigInteger p = dssKey.getParams().getP();
  127. BigInteger g = dssKey.getParams().getG();
  128. BigInteger y = dssKey.getY();
  129. return new DHPublicKeySpec(y, p, g);
  130. }
  131. if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
  132. {
  133. if (key instanceof GnuDHPublicKey)
  134. {
  135. GnuDHPublicKey dhKey = (GnuDHPublicKey) key;
  136. byte[] encoded = dhKey.getEncoded(Registry.X509_ENCODING_ID);
  137. return new X509EncodedKeySpec(encoded);
  138. }
  139. if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
  140. {
  141. byte[] encoded = key.getEncoded();
  142. return new X509EncodedKeySpec(encoded);
  143. }
  144. throw new InvalidKeySpecException(
  145. "Wrong key type or unsupported (public) key specification");
  146. }
  147. throw new InvalidKeySpecException("Unsupported (public) key specification");
  148. }
  149. if (key instanceof DHPrivateKey)
  150. {
  151. if (keySpec.isAssignableFrom(DHPrivateKeySpec.class))
  152. {
  153. DHPrivateKey dhKey = (DHPrivateKey) key;
  154. BigInteger p = dhKey.getParams().getP();
  155. BigInteger g = dhKey.getParams().getG();
  156. BigInteger x = dhKey.getX();
  157. return new DHPrivateKeySpec(x, p, g);
  158. }
  159. if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
  160. {
  161. if (key instanceof GnuDHPrivateKey)
  162. {
  163. GnuDHPrivateKey dhKey = (GnuDHPrivateKey) key;
  164. byte[] encoded = dhKey.getEncoded(Registry.PKCS8_ENCODING_ID);
  165. return new PKCS8EncodedKeySpec(encoded);
  166. }
  167. if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
  168. {
  169. byte[] encoded = key.getEncoded();
  170. return new PKCS8EncodedKeySpec(encoded);
  171. }
  172. throw new InvalidKeySpecException(
  173. "Wrong key type or unsupported (private) key specification");
  174. }
  175. throw new InvalidKeySpecException(
  176. "Unsupported (private) key specification");
  177. }
  178. throw new InvalidKeySpecException(
  179. "Wrong key type or unsupported key specification");
  180. }
  181. protected Key engineTranslateKey(Key key) throws InvalidKeyException
  182. {
  183. if ((key instanceof GnuDHPublicKey) || (key instanceof GnuDHPrivateKey))
  184. return key;
  185. if (key instanceof DHPublicKey)
  186. {
  187. DHPublicKey dsaKey = (DHPublicKey) key;
  188. BigInteger p = dsaKey.getParams().getP();
  189. BigInteger g = dsaKey.getParams().getG();
  190. BigInteger y = dsaKey.getY();
  191. return new GnuDHPublicKey(Registry.X509_ENCODING_ID, null, p, g, y);
  192. }
  193. if (key instanceof DHPrivateKey)
  194. {
  195. DHPrivateKey dsaKey = (DHPrivateKey) key;
  196. BigInteger p = dsaKey.getParams().getP();
  197. BigInteger g = dsaKey.getParams().getG();
  198. BigInteger x = dsaKey.getX();
  199. return new GnuDHPrivateKey(Registry.PKCS8_ENCODING_ID, null, p, g, x);
  200. }
  201. throw new InvalidKeyException("Wrong key type");
  202. }
  203. }