SSLRSASignatureImpl.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* SSLRSASignatureImpl.java -- SSL/TLS RSA implementation.
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is a 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 of the License, or (at
  7. your option) 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; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. 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.net.ssl.provider;
  32. import gnu.classpath.debug.Component;
  33. import gnu.classpath.debug.SystemLogger;
  34. import gnu.java.security.sig.rsa.RSA;
  35. import java.math.BigInteger;
  36. import java.security.InvalidKeyException;
  37. import java.security.InvalidParameterException;
  38. import java.security.MessageDigest;
  39. import java.security.NoSuchAlgorithmException;
  40. import java.security.PrivateKey;
  41. import java.security.PublicKey;
  42. import java.security.SignatureException;
  43. import java.security.SignatureSpi;
  44. import java.security.interfaces.RSAPrivateKey;
  45. import java.security.interfaces.RSAPublicKey;
  46. import java.util.Arrays;
  47. /**
  48. * An implementation of of the RSA signature algorithm; this is an RSA
  49. * encrypted MD5 hash followed by a SHA-1 hash.
  50. *
  51. * @author Casey Marshall (csm@gnu.org)
  52. */
  53. public class SSLRSASignatureImpl extends SignatureSpi
  54. {
  55. private static final SystemLogger logger = SystemLogger.SYSTEM;
  56. private RSAPublicKey pubkey;
  57. private RSAPrivateKey privkey;
  58. private final MessageDigest md5, sha;
  59. private boolean initSign = false;
  60. private boolean initVerify = false;
  61. public SSLRSASignatureImpl() throws NoSuchAlgorithmException
  62. {
  63. md5 = MessageDigest.getInstance("MD5");
  64. sha = MessageDigest.getInstance("SHA-1");
  65. }
  66. /* (non-Javadoc)
  67. * @see java.security.SignatureSpi#engineInitVerify(java.security.PublicKey)
  68. */
  69. @Override protected void engineInitVerify(PublicKey publicKey)
  70. throws InvalidKeyException
  71. {
  72. try
  73. {
  74. pubkey = (RSAPublicKey) publicKey;
  75. initVerify = true;
  76. initSign = false;
  77. privkey = null;
  78. }
  79. catch (ClassCastException cce)
  80. {
  81. throw new InvalidKeyException(cce);
  82. }
  83. }
  84. /* (non-Javadoc)
  85. * @see java.security.SignatureSpi#engineInitSign(java.security.PrivateKey)
  86. */
  87. @Override protected void engineInitSign(PrivateKey privateKey)
  88. throws InvalidKeyException
  89. {
  90. try
  91. {
  92. privkey = (RSAPrivateKey) privateKey;
  93. initSign = true;
  94. initVerify = false;
  95. pubkey = null;
  96. }
  97. catch (ClassCastException cce)
  98. {
  99. throw new InvalidKeyException(cce);
  100. }
  101. }
  102. /* (non-Javadoc)
  103. * @see java.security.SignatureSpi#engineUpdate(byte)
  104. */
  105. @Override protected void engineUpdate(byte b) throws SignatureException
  106. {
  107. if (!initSign && !initVerify)
  108. throw new IllegalStateException("not initialized");
  109. if (Debug.DEBUG)
  110. logger.log(Component.SSL_HANDSHAKE, "SSL/RSA update 0x{0}",
  111. Util.formatInt(b & 0xFF, 16, 2));
  112. md5.update(b);
  113. sha.update(b);
  114. }
  115. /* (non-Javadoc)
  116. * @see java.security.SignatureSpi#engineUpdate(byte[], int, int)
  117. */
  118. @Override protected void engineUpdate(byte[] b, int off, int len)
  119. throws SignatureException
  120. {
  121. if (!initSign && !initVerify)
  122. throw new IllegalStateException("not initialized");
  123. if (Debug.DEBUG)
  124. logger.log(Component.SSL_HANDSHAKE, "SSL/RSA update\n{0}",
  125. Util.hexDump(b, off, len, ">> "));
  126. md5.update(b, off, len);
  127. sha.update(b, off, len);
  128. }
  129. /* (non-Javadoc)
  130. * @see java.security.SignatureSpi#engineSign()
  131. */
  132. @Override protected byte[] engineSign() throws SignatureException
  133. {
  134. // FIXME we need to add RSA blinding to this, somehow.
  135. if (!initSign)
  136. throw new SignatureException("not initialized for signing");
  137. // Pad the hash results with RSA block type 1.
  138. final int k = (privkey.getModulus().bitLength() + 7) >>> 3;
  139. final byte[] d = Util.concat(md5.digest(), sha.digest());
  140. if (k - 11 < d.length)
  141. throw new SignatureException("message too long");
  142. final byte[] eb = new byte[k];
  143. eb[0] = 0x00;
  144. eb[1] = 0x01;
  145. for (int i = 2; i < k - d.length - 1; i++)
  146. eb[i] = (byte) 0xFF;
  147. System.arraycopy(d, 0, eb, k - d.length, d.length);
  148. BigInteger EB = new BigInteger(eb);
  149. // Private-key encrypt the padded hashes.
  150. BigInteger EM = RSA.sign(privkey, EB);
  151. return Util.trim(EM);
  152. }
  153. /* (non-Javadoc)
  154. * @see java.security.SignatureSpi#engineVerify(byte[])
  155. */
  156. @Override protected boolean engineVerify(byte[] sigBytes)
  157. throws SignatureException
  158. {
  159. if (!initVerify)
  160. throw new SignatureException("not initialized for verifying");
  161. // Public-key decrypt the signature representative.
  162. BigInteger EM = new BigInteger(1, (byte[]) sigBytes);
  163. BigInteger EB = RSA.verify(pubkey, EM);
  164. // Unpad the decrypted message.
  165. int i = 0;
  166. final byte[] eb = EB.toByteArray();
  167. if (eb[0] == 0x00)
  168. {
  169. for (i = 0; i < eb.length && eb[i] == 0x00; i++)
  170. ;
  171. }
  172. else if (eb[0] == 0x01)
  173. {
  174. for (i = 1; i < eb.length && eb[i] != 0x00; i++)
  175. {
  176. if (eb[i] != (byte) 0xFF)
  177. {
  178. throw new SignatureException("bad padding");
  179. }
  180. }
  181. i++;
  182. }
  183. else
  184. {
  185. throw new SignatureException("decryption failed");
  186. }
  187. byte[] d1 = Util.trim(eb, i, eb.length - i);
  188. byte[] d2 = Util.concat(md5.digest(), sha.digest());
  189. if (Debug.DEBUG)
  190. logger.logv(Component.SSL_HANDSHAKE, "SSL/RSA d1:{0} d2:{1}",
  191. Util.toHexString(d1, ':'), Util.toHexString(d2, ':'));
  192. return Arrays.equals(d1, d2);
  193. }
  194. /* (non-Javadoc)
  195. * @see java.security.SignatureSpi#engineSetParameter(java.lang.String, java.lang.Object)
  196. */
  197. @Override protected void engineSetParameter(String param, Object value)
  198. throws InvalidParameterException
  199. {
  200. throw new InvalidParameterException("parameters not supported");
  201. }
  202. /* (non-Javadoc)
  203. * @see java.security.SignatureSpi#engineGetParameter(java.lang.String)
  204. */
  205. @Override protected Object engineGetParameter(String param)
  206. throws InvalidParameterException
  207. {
  208. throw new InvalidParameterException("parameters not supported");
  209. }
  210. }