rsa_sign.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 "crypto/x509.h"
  16. #ifndef OPENSSL_NO_MD2
  17. # include <openssl/md2.h> /* uses MD2_DIGEST_LENGTH */
  18. #endif
  19. #ifndef OPENSSL_NO_MD5
  20. # include <openssl/md5.h> /* uses MD5_DIGEST_LENGTH */
  21. #endif
  22. #ifndef OPENSSL_NO_MDC2
  23. # include <openssl/mdc2.h> /* uses MDC2_DIGEST_LENGTH */
  24. #endif
  25. #include <openssl/sha.h> /* uses SHA???_DIGEST_LENGTH */
  26. #include "rsa_local.h"
  27. /*
  28. * The general purpose ASN1 code is not available inside the FIPS provider.
  29. * To remove the dependency RSASSA-PKCS1-v1_5 DigestInfo encodings can be
  30. * treated as a special case by pregenerating the required ASN1 encoding.
  31. * This encoding will also be shared by the default provider.
  32. *
  33. * The EMSA-PKCS1-v1_5 encoding method includes an ASN.1 value of type
  34. * DigestInfo, where the type DigestInfo has the syntax
  35. *
  36. * DigestInfo ::= SEQUENCE {
  37. * digestAlgorithm DigestAlgorithm,
  38. * digest OCTET STRING
  39. * }
  40. *
  41. * DigestAlgorithm ::= AlgorithmIdentifier {
  42. * {PKCS1-v1-5DigestAlgorithms}
  43. * }
  44. *
  45. * The AlgorithmIdentifier is a sequence containing the digest OID and
  46. * parameters (a value of type NULL).
  47. *
  48. * The ENCODE_DIGESTINFO_SHA() and ENCODE_DIGESTINFO_MD() macros define an
  49. * initialized array containing the DER encoded DigestInfo for the specified
  50. * SHA or MD digest. The content of the OCTET STRING is not included.
  51. * |name| is the digest name.
  52. * |n| is last byte in the encoded OID for the digest.
  53. * |sz| is the digest length in bytes. It must not be greater than 110.
  54. */
  55. #define ASN1_SEQUENCE 0x30
  56. #define ASN1_OCTET_STRING 0x04
  57. #define ASN1_NULL 0x05
  58. #define ASN1_OID 0x06
  59. /* SHA OIDs are of the form: (2 16 840 1 101 3 4 2 |n|) */
  60. #define ENCODE_DIGESTINFO_SHA(name, n, sz) \
  61. static const unsigned char digestinfo_##name##_der[] = { \
  62. ASN1_SEQUENCE, 0x11 + sz, \
  63. ASN1_SEQUENCE, 0x0d, \
  64. ASN1_OID, 0x09, 2 * 40 + 16, 0x86, 0x48, 1, 101, 3, 4, 2, n, \
  65. ASN1_NULL, 0x00, \
  66. ASN1_OCTET_STRING, sz \
  67. };
  68. /* MD2 and MD5 OIDs are of the form: (1 2 840 113549 2 |n|) */
  69. #define ENCODE_DIGESTINFO_MD(name, n, sz) \
  70. static const unsigned char digestinfo_##name##_der[] = { \
  71. ASN1_SEQUENCE, 0x10 + sz, \
  72. ASN1_SEQUENCE, 0x0c, \
  73. ASN1_OID, 0x08, 1 * 40 + 2, 0x86, 0x48, 0x86, 0xf7, 0x0d, 2, n, \
  74. ASN1_NULL, 0x00, \
  75. ASN1_OCTET_STRING, sz \
  76. };
  77. #ifndef FIPS_MODE
  78. # ifndef OPENSSL_NO_MD2
  79. ENCODE_DIGESTINFO_MD(md2, 0x02, MD2_DIGEST_LENGTH)
  80. # endif
  81. # ifndef OPENSSL_NO_MD5
  82. ENCODE_DIGESTINFO_MD(md5, 0x05, MD5_DIGEST_LENGTH)
  83. # endif
  84. # ifndef OPENSSL_NO_MDC2
  85. /* MDC-2 (2 5 8 3 101) */
  86. static const unsigned char digestinfo_mdc2_der[] = {
  87. ASN1_SEQUENCE, 0x0c + MDC2_DIGEST_LENGTH,
  88. ASN1_SEQUENCE, 0x08,
  89. ASN1_OID, 0x04, 2 * 40 + 5, 8, 3, 101,
  90. ASN1_NULL, 0x00,
  91. ASN1_OCTET_STRING, MDC2_DIGEST_LENGTH
  92. };
  93. # endif
  94. /* SHA-1 (1 3 14 3 2 26) */
  95. static const unsigned char digestinfo_sha1_der[] = {
  96. ASN1_SEQUENCE, 0x0d + SHA_DIGEST_LENGTH,
  97. ASN1_SEQUENCE, 0x09,
  98. ASN1_OID, 0x05, 1 * 40 + 3, 14, 3, 2, 26,
  99. ASN1_NULL, 0x00,
  100. ASN1_OCTET_STRING, SHA_DIGEST_LENGTH
  101. };
  102. #endif /* FIPS_MODE */
  103. ENCODE_DIGESTINFO_SHA(sha256, 0x01, SHA256_DIGEST_LENGTH)
  104. ENCODE_DIGESTINFO_SHA(sha384, 0x02, SHA384_DIGEST_LENGTH)
  105. ENCODE_DIGESTINFO_SHA(sha512, 0x03, SHA512_DIGEST_LENGTH)
  106. ENCODE_DIGESTINFO_SHA(sha224, 0x04, SHA224_DIGEST_LENGTH)
  107. ENCODE_DIGESTINFO_SHA(sha512_224, 0x05, SHA224_DIGEST_LENGTH)
  108. ENCODE_DIGESTINFO_SHA(sha512_256, 0x06, SHA256_DIGEST_LENGTH)
  109. ENCODE_DIGESTINFO_SHA(sha3_224, 0x07, SHA224_DIGEST_LENGTH)
  110. ENCODE_DIGESTINFO_SHA(sha3_256, 0x08, SHA256_DIGEST_LENGTH)
  111. ENCODE_DIGESTINFO_SHA(sha3_384, 0x09, SHA384_DIGEST_LENGTH)
  112. ENCODE_DIGESTINFO_SHA(sha3_512, 0x0a, SHA512_DIGEST_LENGTH)
  113. #define MD_CASE(name) \
  114. case NID_##name: \
  115. *len = sizeof(digestinfo_##name##_der); \
  116. return digestinfo_##name##_der;
  117. static const unsigned char *digestinfo_encoding(int nid, size_t *len)
  118. {
  119. switch (nid) {
  120. #ifndef FIPS_MODE
  121. # ifndef OPENSSL_NO_MDC2
  122. MD_CASE(mdc2)
  123. # endif
  124. # ifndef OPENSSL_NO_MD2
  125. MD_CASE(md2)
  126. # endif
  127. # ifndef OPENSSL_NO_MD5
  128. MD_CASE(md5)
  129. # endif
  130. MD_CASE(sha1)
  131. #endif /* FIPS_MODE */
  132. MD_CASE(sha224)
  133. MD_CASE(sha256)
  134. MD_CASE(sha384)
  135. MD_CASE(sha512)
  136. MD_CASE(sha512_224)
  137. MD_CASE(sha512_256)
  138. MD_CASE(sha3_224)
  139. MD_CASE(sha3_256)
  140. MD_CASE(sha3_384)
  141. MD_CASE(sha3_512)
  142. default:
  143. return NULL;
  144. }
  145. }
  146. /* Size of an SSL signature: MD5+SHA1 */
  147. #define SSL_SIG_LENGTH 36
  148. /*
  149. * Encodes a DigestInfo prefix of hash |type| and digest |m|, as
  150. * described in EMSA-PKCS1-v1_5-ENCODE, RFC 3447 section 9.2 step 2. This
  151. * encodes the DigestInfo (T and tLen) but does not add the padding.
  152. *
  153. * On success, it returns one and sets |*out| to a newly allocated buffer
  154. * containing the result and |*out_len| to its length. The caller must free
  155. * |*out| with OPENSSL_free(). Otherwise, it returns zero.
  156. */
  157. static int encode_pkcs1(unsigned char **out, size_t *out_len, int type,
  158. const unsigned char *m, size_t m_len)
  159. {
  160. size_t di_prefix_len, dig_info_len;
  161. const unsigned char *di_prefix;
  162. unsigned char *dig_info;
  163. if (type == NID_undef) {
  164. RSAerr(RSA_F_ENCODE_PKCS1, RSA_R_UNKNOWN_ALGORITHM_TYPE);
  165. return 0;
  166. }
  167. di_prefix = digestinfo_encoding(type, &di_prefix_len);
  168. if (di_prefix == NULL) {
  169. RSAerr(RSA_F_ENCODE_PKCS1,
  170. RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
  171. return 0;
  172. }
  173. dig_info_len = di_prefix_len + m_len;
  174. dig_info = OPENSSL_malloc(dig_info_len);
  175. if (dig_info == NULL) {
  176. RSAerr(RSA_F_ENCODE_PKCS1, ERR_R_MALLOC_FAILURE);
  177. return 0;
  178. }
  179. memcpy(dig_info, di_prefix, di_prefix_len);
  180. memcpy(dig_info + di_prefix_len, m, m_len);
  181. *out = dig_info;
  182. *out_len = dig_info_len;
  183. return 1;
  184. }
  185. int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
  186. unsigned char *sigret, unsigned int *siglen, RSA *rsa)
  187. {
  188. int encrypt_len, ret = 0;
  189. size_t encoded_len = 0;
  190. unsigned char *tmps = NULL;
  191. const unsigned char *encoded = NULL;
  192. if (rsa->meth->rsa_sign != NULL)
  193. return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);
  194. /* Compute the encoded digest. */
  195. if (type == NID_md5_sha1) {
  196. /*
  197. * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
  198. * earlier. It has no DigestInfo wrapper but otherwise is
  199. * RSASSA-PKCS1-v1_5.
  200. */
  201. if (m_len != SSL_SIG_LENGTH) {
  202. RSAerr(RSA_F_RSA_SIGN, RSA_R_INVALID_MESSAGE_LENGTH);
  203. return 0;
  204. }
  205. encoded_len = SSL_SIG_LENGTH;
  206. encoded = m;
  207. } else {
  208. if (!encode_pkcs1(&tmps, &encoded_len, type, m, m_len))
  209. goto err;
  210. encoded = tmps;
  211. }
  212. if (encoded_len + RSA_PKCS1_PADDING_SIZE > (size_t)RSA_size(rsa)) {
  213. RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
  214. goto err;
  215. }
  216. encrypt_len = RSA_private_encrypt((int)encoded_len, encoded, sigret, rsa,
  217. RSA_PKCS1_PADDING);
  218. if (encrypt_len <= 0)
  219. goto err;
  220. *siglen = encrypt_len;
  221. ret = 1;
  222. err:
  223. OPENSSL_clear_free(tmps, encoded_len);
  224. return ret;
  225. }
  226. /*
  227. * Verify an RSA signature in |sigbuf| using |rsa|.
  228. * |type| is the NID of the digest algorithm to use.
  229. * If |rm| is NULL, it verifies the signature for digest |m|, otherwise
  230. * it recovers the digest from the signature, writing the digest to |rm| and
  231. * the length to |*prm_len|.
  232. *
  233. * It returns one on successful verification or zero otherwise.
  234. */
  235. int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
  236. unsigned char *rm, size_t *prm_len,
  237. const unsigned char *sigbuf, size_t siglen, RSA *rsa)
  238. {
  239. int len, ret = 0;
  240. size_t decrypt_len, encoded_len = 0;
  241. unsigned char *decrypt_buf = NULL, *encoded = NULL;
  242. if (siglen != (size_t)RSA_size(rsa)) {
  243. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_WRONG_SIGNATURE_LENGTH);
  244. return 0;
  245. }
  246. /* Recover the encoded digest. */
  247. decrypt_buf = OPENSSL_malloc(siglen);
  248. if (decrypt_buf == NULL) {
  249. RSAerr(RSA_F_INT_RSA_VERIFY, ERR_R_MALLOC_FAILURE);
  250. goto err;
  251. }
  252. len = RSA_public_decrypt((int)siglen, sigbuf, decrypt_buf, rsa,
  253. RSA_PKCS1_PADDING);
  254. if (len <= 0)
  255. goto err;
  256. decrypt_len = len;
  257. if (type == NID_md5_sha1) {
  258. /*
  259. * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
  260. * earlier. It has no DigestInfo wrapper but otherwise is
  261. * RSASSA-PKCS1-v1_5.
  262. */
  263. if (decrypt_len != SSL_SIG_LENGTH) {
  264. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  265. goto err;
  266. }
  267. if (rm != NULL) {
  268. memcpy(rm, decrypt_buf, SSL_SIG_LENGTH);
  269. *prm_len = SSL_SIG_LENGTH;
  270. } else {
  271. if (m_len != SSL_SIG_LENGTH) {
  272. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_MESSAGE_LENGTH);
  273. goto err;
  274. }
  275. if (memcmp(decrypt_buf, m, SSL_SIG_LENGTH) != 0) {
  276. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  277. goto err;
  278. }
  279. }
  280. } else if (type == NID_mdc2 && decrypt_len == 2 + 16
  281. && decrypt_buf[0] == 0x04 && decrypt_buf[1] == 0x10) {
  282. /*
  283. * Oddball MDC2 case: signature can be OCTET STRING. check for correct
  284. * tag and length octets.
  285. */
  286. if (rm != NULL) {
  287. memcpy(rm, decrypt_buf + 2, 16);
  288. *prm_len = 16;
  289. } else {
  290. if (m_len != 16) {
  291. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_MESSAGE_LENGTH);
  292. goto err;
  293. }
  294. if (memcmp(m, decrypt_buf + 2, 16) != 0) {
  295. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  296. goto err;
  297. }
  298. }
  299. } else {
  300. /*
  301. * If recovering the digest, extract a digest-sized output from the end
  302. * of |decrypt_buf| for |encode_pkcs1|, then compare the decryption
  303. * output as in a standard verification.
  304. */
  305. if (rm != NULL) {
  306. const EVP_MD *md = EVP_get_digestbynid(type);
  307. if (md == NULL) {
  308. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_UNKNOWN_ALGORITHM_TYPE);
  309. goto err;
  310. }
  311. len = EVP_MD_size(md);
  312. if (len <= 0)
  313. goto err;
  314. m_len = (unsigned int)len;
  315. if (m_len > decrypt_len) {
  316. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
  317. goto err;
  318. }
  319. m = decrypt_buf + decrypt_len - m_len;
  320. }
  321. /* Construct the encoded digest and ensure it matches. */
  322. if (!encode_pkcs1(&encoded, &encoded_len, type, m, m_len))
  323. goto err;
  324. if (encoded_len != decrypt_len
  325. || memcmp(encoded, decrypt_buf, encoded_len) != 0) {
  326. RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
  327. goto err;
  328. }
  329. /* Output the recovered digest. */
  330. if (rm != NULL) {
  331. memcpy(rm, m, m_len);
  332. *prm_len = m_len;
  333. }
  334. }
  335. ret = 1;
  336. err:
  337. OPENSSL_clear_free(encoded, encoded_len);
  338. OPENSSL_clear_free(decrypt_buf, siglen);
  339. return ret;
  340. }
  341. int RSA_verify(int type, const unsigned char *m, unsigned int m_len,
  342. const unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
  343. {
  344. if (rsa->meth->rsa_verify != NULL)
  345. return rsa->meth->rsa_verify(type, m, m_len, sigbuf, siglen, rsa);
  346. return int_rsa_verify(type, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
  347. }