rsa_sign.c 14 KB

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