rsa_sign.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright 1995-2020 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. #include <openssl/x509.h>
  20. #include "crypto/x509.h"
  21. #ifndef OPENSSL_NO_MD2
  22. # include <openssl/md2.h> /* uses MD2_DIGEST_LENGTH */
  23. #endif
  24. #ifndef OPENSSL_NO_MD4
  25. # include <openssl/md4.h> /* uses MD4_DIGEST_LENGTH */
  26. #endif
  27. #ifndef OPENSSL_NO_MD5
  28. # include <openssl/md5.h> /* uses MD5_DIGEST_LENGTH */
  29. #endif
  30. #ifndef OPENSSL_NO_MDC2
  31. # include <openssl/mdc2.h> /* uses MDC2_DIGEST_LENGTH */
  32. #endif
  33. #ifndef OPENSSL_NO_RMD160
  34. # include <openssl/ripemd.h> /* uses RIPEMD160_DIGEST_LENGTH */
  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. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  246. return 0;
  247. }
  248. memcpy(dig_info, di_prefix, di_prefix_len);
  249. memcpy(dig_info + di_prefix_len, m, m_len);
  250. *out = dig_info;
  251. *out_len = dig_info_len;
  252. return 1;
  253. }
  254. int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
  255. unsigned char *sigret, unsigned int *siglen, RSA *rsa)
  256. {
  257. int encrypt_len, ret = 0;
  258. size_t encoded_len = 0;
  259. unsigned char *tmps = NULL;
  260. const unsigned char *encoded = NULL;
  261. #ifndef FIPS_MODULE
  262. if (rsa->meth->rsa_sign != NULL)
  263. return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);
  264. #endif /* FIPS_MODULE */
  265. /* Compute the encoded digest. */
  266. if (type == NID_md5_sha1) {
  267. /*
  268. * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
  269. * earlier. It has no DigestInfo wrapper but otherwise is
  270. * RSASSA-PKCS1-v1_5.
  271. */
  272. if (m_len != SSL_SIG_LENGTH) {
  273. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  274. return 0;
  275. }
  276. encoded_len = SSL_SIG_LENGTH;
  277. encoded = m;
  278. } else {
  279. if (!encode_pkcs1(&tmps, &encoded_len, type, m, m_len))
  280. goto err;
  281. encoded = tmps;
  282. }
  283. if (encoded_len + RSA_PKCS1_PADDING_SIZE > (size_t)RSA_size(rsa)) {
  284. ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
  285. goto err;
  286. }
  287. encrypt_len = RSA_private_encrypt((int)encoded_len, encoded, sigret, rsa,
  288. RSA_PKCS1_PADDING);
  289. if (encrypt_len <= 0)
  290. goto err;
  291. *siglen = encrypt_len;
  292. ret = 1;
  293. err:
  294. OPENSSL_clear_free(tmps, encoded_len);
  295. return ret;
  296. }
  297. /*
  298. * Verify an RSA signature in |sigbuf| using |rsa|.
  299. * |type| is the NID of the digest algorithm to use.
  300. * If |rm| is NULL, it verifies the signature for digest |m|, otherwise
  301. * it recovers the digest from the signature, writing the digest to |rm| and
  302. * the length to |*prm_len|.
  303. *
  304. * It returns one on successful verification or zero otherwise.
  305. */
  306. int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
  307. unsigned char *rm, size_t *prm_len,
  308. const unsigned char *sigbuf, size_t siglen, RSA *rsa)
  309. {
  310. int len, ret = 0;
  311. size_t decrypt_len, encoded_len = 0;
  312. unsigned char *decrypt_buf = NULL, *encoded = NULL;
  313. if (siglen != (size_t)RSA_size(rsa)) {
  314. ERR_raise(ERR_LIB_RSA, RSA_R_WRONG_SIGNATURE_LENGTH);
  315. return 0;
  316. }
  317. /* Recover the encoded digest. */
  318. decrypt_buf = OPENSSL_malloc(siglen);
  319. if (decrypt_buf == NULL) {
  320. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  321. goto err;
  322. }
  323. len = RSA_public_decrypt((int)siglen, sigbuf, decrypt_buf, rsa,
  324. RSA_PKCS1_PADDING);
  325. if (len <= 0)
  326. goto err;
  327. decrypt_len = len;
  328. #ifndef FIPS_MODULE
  329. if (type == NID_md5_sha1) {
  330. /*
  331. * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
  332. * earlier. It has no DigestInfo wrapper but otherwise is
  333. * RSASSA-PKCS1-v1_5.
  334. */
  335. if (decrypt_len != SSL_SIG_LENGTH) {
  336. ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
  337. goto err;
  338. }
  339. if (rm != NULL) {
  340. memcpy(rm, decrypt_buf, SSL_SIG_LENGTH);
  341. *prm_len = SSL_SIG_LENGTH;
  342. } else {
  343. if (m_len != SSL_SIG_LENGTH) {
  344. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  345. goto err;
  346. }
  347. if (memcmp(decrypt_buf, m, SSL_SIG_LENGTH) != 0) {
  348. ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
  349. goto err;
  350. }
  351. }
  352. } else if (type == NID_mdc2 && decrypt_len == 2 + 16
  353. && decrypt_buf[0] == 0x04 && decrypt_buf[1] == 0x10) {
  354. /*
  355. * Oddball MDC2 case: signature can be OCTET STRING. check for correct
  356. * tag and length octets.
  357. */
  358. if (rm != NULL) {
  359. memcpy(rm, decrypt_buf + 2, 16);
  360. *prm_len = 16;
  361. } else {
  362. if (m_len != 16) {
  363. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  364. goto err;
  365. }
  366. if (memcmp(m, decrypt_buf + 2, 16) != 0) {
  367. ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
  368. goto err;
  369. }
  370. }
  371. } else
  372. #endif /* FIPS_MODULE */
  373. {
  374. /*
  375. * If recovering the digest, extract a digest-sized output from the end
  376. * of |decrypt_buf| for |encode_pkcs1|, then compare the decryption
  377. * output as in a standard verification.
  378. */
  379. if (rm != NULL) {
  380. len = digest_sz_from_nid(type);
  381. if (len <= 0)
  382. goto err;
  383. m_len = (unsigned int)len;
  384. if (m_len > decrypt_len) {
  385. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
  386. goto err;
  387. }
  388. m = decrypt_buf + decrypt_len - m_len;
  389. }
  390. /* Construct the encoded digest and ensure it matches. */
  391. if (!encode_pkcs1(&encoded, &encoded_len, type, m, m_len))
  392. goto err;
  393. if (encoded_len != decrypt_len
  394. || memcmp(encoded, decrypt_buf, encoded_len) != 0) {
  395. ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
  396. goto err;
  397. }
  398. /* Output the recovered digest. */
  399. if (rm != NULL) {
  400. memcpy(rm, m, m_len);
  401. *prm_len = m_len;
  402. }
  403. }
  404. ret = 1;
  405. err:
  406. OPENSSL_clear_free(encoded, encoded_len);
  407. OPENSSL_clear_free(decrypt_buf, siglen);
  408. return ret;
  409. }
  410. int RSA_verify(int type, const unsigned char *m, unsigned int m_len,
  411. const unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
  412. {
  413. if (rsa->meth->rsa_verify != NULL)
  414. return rsa->meth->rsa_verify(type, m, m_len, sigbuf, siglen, rsa);
  415. return int_rsa_verify(type, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
  416. }