rsa_sign.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/bn.h>
  12. #include <openssl/rsa.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/x509.h>
  15. #include "internal/x509_int.h"
  16. #include "rsa_locl.h"
  17. /* Size of an SSL signature: MD5+SHA1 */
  18. #define SSL_SIG_LENGTH 36
  19. /*
  20. * encode_pkcs1 encodes a DigestInfo prefix of hash |type| and digest |m|, as
  21. * described in EMSA-PKCS1-v1_5-ENCODE, RFC 3447 section 9.2 step 2. This
  22. * encodes the DigestInfo (T and tLen) but does not add the padding.
  23. *
  24. * On success, it returns one and sets |*out| to a newly allocated buffer
  25. * containing the result and |*out_len| to its length. The caller must free
  26. * |*out| with |OPENSSL_free|. Otherwise, it returns zero.
  27. */
  28. static int encode_pkcs1(unsigned char **out, int *out_len, int type,
  29. const unsigned char *m, unsigned int m_len)
  30. {
  31. X509_SIG sig;
  32. X509_ALGOR algor;
  33. ASN1_TYPE parameter;
  34. ASN1_OCTET_STRING digest;
  35. uint8_t *der = NULL;
  36. int len;
  37. sig.algor = &algor;
  38. sig.algor->algorithm = OBJ_nid2obj(type);
  39. if (sig.algor->algorithm == NULL) {
  40. RSAerr(RSA_F_ENCODE_PKCS1, RSA_R_UNKNOWN_ALGORITHM_TYPE);
  41. return 0;
  42. }
  43. if (OBJ_length(sig.algor->algorithm) == 0) {
  44. RSAerr(RSA_F_ENCODE_PKCS1,
  45. RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
  46. return 0;
  47. }
  48. parameter.type = V_ASN1_NULL;
  49. parameter.value.ptr = NULL;
  50. sig.algor->parameter = &parameter;
  51. sig.digest = &digest;
  52. sig.digest->data = (unsigned char *)m;
  53. sig.digest->length = m_len;
  54. len = i2d_X509_SIG(&sig, &der);
  55. if (len < 0)
  56. return 0;
  57. *out = der;
  58. *out_len = len;
  59. return 1;
  60. }
  61. int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
  62. unsigned char *sigret, unsigned int *siglen, RSA *rsa)
  63. {
  64. int encrypt_len, encoded_len = 0, ret = 0;
  65. unsigned char *tmps = NULL;
  66. const unsigned char *encoded = NULL;
  67. if (rsa->meth->rsa_sign) {
  68. return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);
  69. }
  70. /* Compute the encoded digest. */
  71. if (type == NID_md5_sha1) {
  72. /*
  73. * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
  74. * earlier. It has no DigestInfo wrapper but otherwise is
  75. * RSASSA-PKCS1-v1_5.
  76. */
  77. if (m_len != SSL_SIG_LENGTH) {
  78. RSAerr(RSA_F_RSA_SIGN, RSA_R_INVALID_MESSAGE_LENGTH);
  79. return 0;
  80. }
  81. encoded_len = SSL_SIG_LENGTH;
  82. encoded = m;
  83. } else {
  84. if (!encode_pkcs1(&tmps, &encoded_len, type, m, m_len))
  85. goto err;
  86. encoded = tmps;
  87. }
  88. if (encoded_len > RSA_size(rsa) - RSA_PKCS1_PADDING_SIZE) {
  89. RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
  90. goto err;
  91. }
  92. encrypt_len = RSA_private_encrypt(encoded_len, encoded, sigret, rsa,
  93. RSA_PKCS1_PADDING);
  94. if (encrypt_len <= 0)
  95. goto err;
  96. *siglen = encrypt_len;
  97. ret = 1;
  98. err:
  99. OPENSSL_clear_free(tmps, (size_t)encoded_len);
  100. return ret;
  101. }
  102. /*
  103. * int_rsa_verify verifies an RSA signature in |sigbuf| using |rsa|. It may be
  104. * called in two modes. If |rm| is NULL, it verifies the signature for digest
  105. * |m|. Otherwise, it recovers the digest from the signature, writing the digest
  106. * to |rm| and the length to |*prm_len|. |type| is the NID of the digest
  107. * algorithm to use. It returns one on successful verification and zero
  108. * otherwise.
  109. */
  110. int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
  111. unsigned char *rm, size_t *prm_len,
  112. const unsigned char *sigbuf, size_t siglen, RSA *rsa)
  113. {
  114. int decrypt_len, ret = 0, encoded_len = 0;
  115. unsigned char *decrypt_buf = NULL, *encoded = NULL;
  116. if (siglen != (size_t)RSA_size(rsa)) {
  117. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_WRONG_SIGNATURE_LENGTH);
  118. return 0;
  119. }
  120. /* Recover the encoded digest. */
  121. decrypt_buf = OPENSSL_malloc(siglen);
  122. if (decrypt_buf == NULL) {
  123. RSAerr(RSA_F_INT_RSA_VERIFY, ERR_R_MALLOC_FAILURE);
  124. goto err;
  125. }
  126. decrypt_len = RSA_public_decrypt((int)siglen, sigbuf, decrypt_buf, rsa,
  127. RSA_PKCS1_PADDING);
  128. if (decrypt_len <= 0)
  129. goto err;
  130. if (type == NID_md5_sha1) {
  131. /*
  132. * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
  133. * earlier. It has no DigestInfo wrapper but otherwise is
  134. * RSASSA-PKCS1-v1_5.
  135. */
  136. if (decrypt_len != SSL_SIG_LENGTH) {
  137. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  138. goto err;
  139. }
  140. if (rm != NULL) {
  141. memcpy(rm, decrypt_buf, SSL_SIG_LENGTH);
  142. *prm_len = SSL_SIG_LENGTH;
  143. } else {
  144. if (m_len != SSL_SIG_LENGTH) {
  145. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_MESSAGE_LENGTH);
  146. goto err;
  147. }
  148. if (memcmp(decrypt_buf, m, SSL_SIG_LENGTH) != 0) {
  149. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  150. goto err;
  151. }
  152. }
  153. } else if (type == NID_mdc2 && decrypt_len == 2 + 16
  154. && decrypt_buf[0] == 0x04 && decrypt_buf[1] == 0x10) {
  155. /*
  156. * Oddball MDC2 case: signature can be OCTET STRING. check for correct
  157. * tag and length octets.
  158. */
  159. if (rm != NULL) {
  160. memcpy(rm, decrypt_buf + 2, 16);
  161. *prm_len = 16;
  162. } else {
  163. if (m_len != 16) {
  164. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_MESSAGE_LENGTH);
  165. goto err;
  166. }
  167. if (memcmp(m, decrypt_buf + 2, 16) != 0) {
  168. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  169. goto err;
  170. }
  171. }
  172. } else {
  173. /*
  174. * If recovering the digest, extract a digest-sized output from the end
  175. * of |decrypt_buf| for |encode_pkcs1|, then compare the decryption
  176. * output as in a standard verification.
  177. */
  178. if (rm != NULL) {
  179. const EVP_MD *md = EVP_get_digestbynid(type);
  180. if (md == NULL) {
  181. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_UNKNOWN_ALGORITHM_TYPE);
  182. goto err;
  183. }
  184. m_len = EVP_MD_size(md);
  185. if (m_len > (size_t)decrypt_len) {
  186. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
  187. goto err;
  188. }
  189. m = decrypt_buf + decrypt_len - m_len;
  190. }
  191. /* Construct the encoded digest and ensure it matches. */
  192. if (!encode_pkcs1(&encoded, &encoded_len, type, m, m_len))
  193. goto err;
  194. if (encoded_len != decrypt_len
  195. || memcmp(encoded, decrypt_buf, encoded_len) != 0) {
  196. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  197. goto err;
  198. }
  199. /* Output the recovered digest. */
  200. if (rm != NULL) {
  201. memcpy(rm, m, m_len);
  202. *prm_len = m_len;
  203. }
  204. }
  205. ret = 1;
  206. err:
  207. OPENSSL_clear_free(encoded, (size_t)encoded_len);
  208. OPENSSL_clear_free(decrypt_buf, siglen);
  209. return ret;
  210. }
  211. int RSA_verify(int type, const unsigned char *m, unsigned int m_len,
  212. const unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
  213. {
  214. if (rsa->meth->rsa_verify) {
  215. return rsa->meth->rsa_verify(type, m, m_len, sigbuf, siglen, rsa);
  216. }
  217. return int_rsa_verify(type, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
  218. }