rsa_sign.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include "internal/cryptlib.h"
  59. #include <openssl/bn.h>
  60. #include <openssl/rsa.h>
  61. #include <openssl/objects.h>
  62. #include <openssl/x509.h>
  63. #include "rsa_locl.h"
  64. /* Size of an SSL signature: MD5+SHA1 */
  65. #define SSL_SIG_LENGTH 36
  66. int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
  67. unsigned char *sigret, unsigned int *siglen, RSA *rsa)
  68. {
  69. X509_SIG sig;
  70. ASN1_TYPE parameter;
  71. int i, j, ret = 1;
  72. unsigned char *p, *tmps = NULL;
  73. const unsigned char *s = NULL;
  74. X509_ALGOR algor;
  75. ASN1_OCTET_STRING digest;
  76. if (rsa->meth->rsa_sign) {
  77. return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);
  78. }
  79. /* Special case: SSL signature, just check the length */
  80. if (type == NID_md5_sha1) {
  81. if (m_len != SSL_SIG_LENGTH) {
  82. RSAerr(RSA_F_RSA_SIGN, RSA_R_INVALID_MESSAGE_LENGTH);
  83. return (0);
  84. }
  85. i = SSL_SIG_LENGTH;
  86. s = m;
  87. } else {
  88. sig.algor = &algor;
  89. sig.algor->algorithm = OBJ_nid2obj(type);
  90. if (sig.algor->algorithm == NULL) {
  91. RSAerr(RSA_F_RSA_SIGN, RSA_R_UNKNOWN_ALGORITHM_TYPE);
  92. return (0);
  93. }
  94. if (OBJ_length(sig.algor->algorithm) == 0) {
  95. RSAerr(RSA_F_RSA_SIGN,
  96. RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
  97. return (0);
  98. }
  99. parameter.type = V_ASN1_NULL;
  100. parameter.value.ptr = NULL;
  101. sig.algor->parameter = &parameter;
  102. sig.digest = &digest;
  103. sig.digest->data = (unsigned char *)m; /* TMP UGLY CAST */
  104. sig.digest->length = m_len;
  105. i = i2d_X509_SIG(&sig, NULL);
  106. }
  107. j = RSA_size(rsa);
  108. if (i > (j - RSA_PKCS1_PADDING_SIZE)) {
  109. RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
  110. return (0);
  111. }
  112. if (type != NID_md5_sha1) {
  113. tmps = OPENSSL_malloc((unsigned int)j + 1);
  114. if (tmps == NULL) {
  115. RSAerr(RSA_F_RSA_SIGN, ERR_R_MALLOC_FAILURE);
  116. return (0);
  117. }
  118. p = tmps;
  119. i2d_X509_SIG(&sig, &p);
  120. s = tmps;
  121. }
  122. i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);
  123. if (i <= 0)
  124. ret = 0;
  125. else
  126. *siglen = i;
  127. if (type != NID_md5_sha1)
  128. OPENSSL_clear_free(tmps, (unsigned int)j + 1);
  129. return (ret);
  130. }
  131. /*
  132. * Check DigestInfo structure does not contain extraneous data by reencoding
  133. * using DER and checking encoding against original.
  134. */
  135. static int rsa_check_digestinfo(X509_SIG *sig, const unsigned char *dinfo,
  136. int dinfolen)
  137. {
  138. unsigned char *der = NULL;
  139. int derlen;
  140. int ret = 0;
  141. derlen = i2d_X509_SIG(sig, &der);
  142. if (derlen <= 0)
  143. return 0;
  144. if (derlen == dinfolen && !memcmp(dinfo, der, derlen))
  145. ret = 1;
  146. OPENSSL_clear_free(der, derlen);
  147. return ret;
  148. }
  149. int int_rsa_verify(int dtype, const unsigned char *m,
  150. unsigned int m_len,
  151. unsigned char *rm, size_t *prm_len,
  152. const unsigned char *sigbuf, size_t siglen, RSA *rsa)
  153. {
  154. int i, ret = 0, sigtype;
  155. unsigned char *s;
  156. X509_SIG *sig = NULL;
  157. if (siglen != (unsigned int)RSA_size(rsa)) {
  158. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_WRONG_SIGNATURE_LENGTH);
  159. return (0);
  160. }
  161. if ((dtype == NID_md5_sha1) && rm) {
  162. i = RSA_public_decrypt((int)siglen,
  163. sigbuf, rm, rsa, RSA_PKCS1_PADDING);
  164. if (i <= 0)
  165. return 0;
  166. *prm_len = i;
  167. return 1;
  168. }
  169. s = OPENSSL_malloc((unsigned int)siglen);
  170. if (s == NULL) {
  171. RSAerr(RSA_F_INT_RSA_VERIFY, ERR_R_MALLOC_FAILURE);
  172. goto err;
  173. }
  174. if ((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH)) {
  175. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_MESSAGE_LENGTH);
  176. goto err;
  177. }
  178. i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING);
  179. if (i <= 0)
  180. goto err;
  181. /*
  182. * Oddball MDC2 case: signature can be OCTET STRING. check for correct
  183. * tag and length octets.
  184. */
  185. if (dtype == NID_mdc2 && i == 18 && s[0] == 0x04 && s[1] == 0x10) {
  186. if (rm) {
  187. memcpy(rm, s + 2, 16);
  188. *prm_len = 16;
  189. ret = 1;
  190. } else if (memcmp(m, s + 2, 16)) {
  191. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  192. } else {
  193. ret = 1;
  194. }
  195. } else if (dtype == NID_md5_sha1) {
  196. /* Special case: SSL signature */
  197. if ((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH))
  198. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  199. else
  200. ret = 1;
  201. } else {
  202. const unsigned char *p = s;
  203. sig = d2i_X509_SIG(NULL, &p, (long)i);
  204. if (sig == NULL)
  205. goto err;
  206. /* Excess data can be used to create forgeries */
  207. if (p != s + i || !rsa_check_digestinfo(sig, s, i)) {
  208. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  209. goto err;
  210. }
  211. /*
  212. * Parameters to the signature algorithm can also be used to create
  213. * forgeries
  214. */
  215. if (sig->algor->parameter
  216. && ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL) {
  217. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  218. goto err;
  219. }
  220. sigtype = OBJ_obj2nid(sig->algor->algorithm);
  221. #ifdef RSA_DEBUG
  222. /* put a backward compatibility flag in EAY */
  223. fprintf(stderr, "in(%s) expect(%s)\n", OBJ_nid2ln(sigtype),
  224. OBJ_nid2ln(dtype));
  225. #endif
  226. if (sigtype != dtype) {
  227. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_ALGORITHM_MISMATCH);
  228. goto err;
  229. }
  230. if (rm) {
  231. const EVP_MD *md;
  232. md = EVP_get_digestbynid(dtype);
  233. if (md && (EVP_MD_size(md) != sig->digest->length))
  234. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
  235. else {
  236. memcpy(rm, sig->digest->data, sig->digest->length);
  237. *prm_len = sig->digest->length;
  238. ret = 1;
  239. }
  240. } else if (((unsigned int)sig->digest->length != m_len) ||
  241. (memcmp(m, sig->digest->data, m_len) != 0)) {
  242. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  243. } else
  244. ret = 1;
  245. }
  246. err:
  247. X509_SIG_free(sig);
  248. OPENSSL_clear_free(s, (unsigned int)siglen);
  249. return (ret);
  250. }
  251. int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
  252. const unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
  253. {
  254. if (rsa->meth->rsa_verify) {
  255. return rsa->meth->rsa_verify(dtype, m, m_len, sigbuf, siglen, rsa);
  256. }
  257. return int_rsa_verify(dtype, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
  258. }