rsa.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. #include <openssl/rsa.h>
  57. #include <limits.h>
  58. #include <string.h>
  59. #include <openssl/bn.h>
  60. #include <openssl/digest.h>
  61. #include <openssl/engine.h>
  62. #include <openssl/err.h>
  63. #include <openssl/ex_data.h>
  64. #include <openssl/md5.h>
  65. #include <openssl/mem.h>
  66. #include <openssl/nid.h>
  67. #include <openssl/sha.h>
  68. #include <openssl/thread.h>
  69. #include "../bn/internal.h"
  70. #include "../delocate.h"
  71. #include "../../internal.h"
  72. #include "internal.h"
  73. DEFINE_STATIC_EX_DATA_CLASS(g_rsa_ex_data_class);
  74. RSA *RSA_new(void) { return RSA_new_method(NULL); }
  75. RSA *RSA_new_method(const ENGINE *engine) {
  76. RSA *rsa = OPENSSL_malloc(sizeof(RSA));
  77. if (rsa == NULL) {
  78. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  79. return NULL;
  80. }
  81. OPENSSL_memset(rsa, 0, sizeof(RSA));
  82. if (engine) {
  83. rsa->meth = ENGINE_get_RSA_method(engine);
  84. }
  85. if (rsa->meth == NULL) {
  86. rsa->meth = (RSA_METHOD *) RSA_default_method();
  87. }
  88. METHOD_ref(rsa->meth);
  89. rsa->references = 1;
  90. rsa->flags = rsa->meth->flags;
  91. CRYPTO_MUTEX_init(&rsa->lock);
  92. CRYPTO_new_ex_data(&rsa->ex_data);
  93. if (rsa->meth->init && !rsa->meth->init(rsa)) {
  94. CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data);
  95. CRYPTO_MUTEX_cleanup(&rsa->lock);
  96. METHOD_unref(rsa->meth);
  97. OPENSSL_free(rsa);
  98. return NULL;
  99. }
  100. return rsa;
  101. }
  102. void RSA_free(RSA *rsa) {
  103. unsigned u;
  104. if (rsa == NULL) {
  105. return;
  106. }
  107. if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) {
  108. return;
  109. }
  110. if (rsa->meth->finish) {
  111. rsa->meth->finish(rsa);
  112. }
  113. METHOD_unref(rsa->meth);
  114. CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data);
  115. BN_clear_free(rsa->n);
  116. BN_clear_free(rsa->e);
  117. BN_clear_free(rsa->d);
  118. BN_clear_free(rsa->p);
  119. BN_clear_free(rsa->q);
  120. BN_clear_free(rsa->dmp1);
  121. BN_clear_free(rsa->dmq1);
  122. BN_clear_free(rsa->iqmp);
  123. BN_MONT_CTX_free(rsa->mont_n);
  124. BN_MONT_CTX_free(rsa->mont_p);
  125. BN_MONT_CTX_free(rsa->mont_q);
  126. for (u = 0; u < rsa->num_blindings; u++) {
  127. BN_BLINDING_free(rsa->blindings[u]);
  128. }
  129. OPENSSL_free(rsa->blindings);
  130. OPENSSL_free(rsa->blindings_inuse);
  131. CRYPTO_MUTEX_cleanup(&rsa->lock);
  132. OPENSSL_free(rsa);
  133. }
  134. int RSA_up_ref(RSA *rsa) {
  135. CRYPTO_refcount_inc(&rsa->references);
  136. return 1;
  137. }
  138. unsigned RSA_bits(const RSA *rsa) { return BN_num_bits(rsa->n); }
  139. void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, const BIGNUM **out_e,
  140. const BIGNUM **out_d) {
  141. if (out_n != NULL) {
  142. *out_n = rsa->n;
  143. }
  144. if (out_e != NULL) {
  145. *out_e = rsa->e;
  146. }
  147. if (out_d != NULL) {
  148. *out_d = rsa->d;
  149. }
  150. }
  151. void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p,
  152. const BIGNUM **out_q) {
  153. if (out_p != NULL) {
  154. *out_p = rsa->p;
  155. }
  156. if (out_q != NULL) {
  157. *out_q = rsa->q;
  158. }
  159. }
  160. void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1,
  161. const BIGNUM **out_dmq1, const BIGNUM **out_iqmp) {
  162. if (out_dmp1 != NULL) {
  163. *out_dmp1 = rsa->dmp1;
  164. }
  165. if (out_dmq1 != NULL) {
  166. *out_dmq1 = rsa->dmq1;
  167. }
  168. if (out_iqmp != NULL) {
  169. *out_iqmp = rsa->iqmp;
  170. }
  171. }
  172. int RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d) {
  173. if ((rsa->n == NULL && n == NULL) ||
  174. (rsa->e == NULL && e == NULL)) {
  175. return 0;
  176. }
  177. if (n != NULL) {
  178. BN_free(rsa->n);
  179. rsa->n = n;
  180. }
  181. if (e != NULL) {
  182. BN_free(rsa->e);
  183. rsa->e = e;
  184. }
  185. if (d != NULL) {
  186. BN_free(rsa->d);
  187. rsa->d = d;
  188. }
  189. return 1;
  190. }
  191. int RSA_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q) {
  192. if ((rsa->p == NULL && p == NULL) ||
  193. (rsa->q == NULL && q == NULL)) {
  194. return 0;
  195. }
  196. if (p != NULL) {
  197. BN_free(rsa->p);
  198. rsa->p = p;
  199. }
  200. if (q != NULL) {
  201. BN_free(rsa->q);
  202. rsa->q = q;
  203. }
  204. return 1;
  205. }
  206. int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) {
  207. if ((rsa->dmp1 == NULL && dmp1 == NULL) ||
  208. (rsa->dmq1 == NULL && dmq1 == NULL) ||
  209. (rsa->iqmp == NULL && iqmp == NULL)) {
  210. return 0;
  211. }
  212. if (dmp1 != NULL) {
  213. BN_free(rsa->dmp1);
  214. rsa->dmp1 = dmp1;
  215. }
  216. if (dmq1 != NULL) {
  217. BN_free(rsa->dmq1);
  218. rsa->dmq1 = dmq1;
  219. }
  220. if (iqmp != NULL) {
  221. BN_free(rsa->iqmp);
  222. rsa->iqmp = iqmp;
  223. }
  224. return 1;
  225. }
  226. int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
  227. int padding) {
  228. size_t out_len;
  229. if (!RSA_encrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
  230. return -1;
  231. }
  232. if (out_len > INT_MAX) {
  233. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  234. return -1;
  235. }
  236. return out_len;
  237. }
  238. int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  239. const uint8_t *in, size_t in_len, int padding) {
  240. if (rsa->meth->sign_raw) {
  241. return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
  242. }
  243. return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
  244. }
  245. int RSA_private_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
  246. int padding) {
  247. size_t out_len;
  248. if (!RSA_sign_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
  249. return -1;
  250. }
  251. if (out_len > INT_MAX) {
  252. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  253. return -1;
  254. }
  255. return out_len;
  256. }
  257. int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  258. const uint8_t *in, size_t in_len, int padding) {
  259. if (rsa->meth->decrypt) {
  260. return rsa->meth->decrypt(rsa, out_len, out, max_out, in, in_len, padding);
  261. }
  262. return rsa_default_decrypt(rsa, out_len, out, max_out, in, in_len, padding);
  263. }
  264. int RSA_private_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
  265. int padding) {
  266. size_t out_len;
  267. if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
  268. return -1;
  269. }
  270. if (out_len > INT_MAX) {
  271. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  272. return -1;
  273. }
  274. return out_len;
  275. }
  276. int RSA_public_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
  277. int padding) {
  278. size_t out_len;
  279. if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
  280. return -1;
  281. }
  282. if (out_len > INT_MAX) {
  283. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  284. return -1;
  285. }
  286. return out_len;
  287. }
  288. unsigned RSA_size(const RSA *rsa) {
  289. if (rsa->meth->size) {
  290. return rsa->meth->size(rsa);
  291. }
  292. return rsa_default_size(rsa);
  293. }
  294. int RSA_is_opaque(const RSA *rsa) {
  295. return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE);
  296. }
  297. int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  298. CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
  299. int index;
  300. if (!CRYPTO_get_ex_new_index(g_rsa_ex_data_class_bss_get(), &index, argl,
  301. argp, free_func)) {
  302. return -1;
  303. }
  304. return index;
  305. }
  306. int RSA_set_ex_data(RSA *rsa, int idx, void *arg) {
  307. return CRYPTO_set_ex_data(&rsa->ex_data, idx, arg);
  308. }
  309. void *RSA_get_ex_data(const RSA *rsa, int idx) {
  310. return CRYPTO_get_ex_data(&rsa->ex_data, idx);
  311. }
  312. // SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
  313. // the length of an MD5 and SHA1 hash.
  314. static const unsigned SSL_SIG_LENGTH = 36;
  315. // pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
  316. // to be signed with PKCS#1.
  317. struct pkcs1_sig_prefix {
  318. // nid identifies the hash function.
  319. int nid;
  320. // hash_len is the expected length of the hash function.
  321. uint8_t hash_len;
  322. // len is the number of bytes of |bytes| which are valid.
  323. uint8_t len;
  324. // bytes contains the DER bytes.
  325. uint8_t bytes[19];
  326. };
  327. // kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
  328. // different hash functions.
  329. static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
  330. {
  331. NID_md5,
  332. MD5_DIGEST_LENGTH,
  333. 18,
  334. {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  335. 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
  336. },
  337. {
  338. NID_sha1,
  339. SHA_DIGEST_LENGTH,
  340. 15,
  341. {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  342. 0x00, 0x04, 0x14},
  343. },
  344. {
  345. NID_sha224,
  346. SHA224_DIGEST_LENGTH,
  347. 19,
  348. {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  349. 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
  350. },
  351. {
  352. NID_sha256,
  353. SHA256_DIGEST_LENGTH,
  354. 19,
  355. {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  356. 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
  357. },
  358. {
  359. NID_sha384,
  360. SHA384_DIGEST_LENGTH,
  361. 19,
  362. {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  363. 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
  364. },
  365. {
  366. NID_sha512,
  367. SHA512_DIGEST_LENGTH,
  368. 19,
  369. {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  370. 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
  371. },
  372. {
  373. NID_undef, 0, 0, {0},
  374. },
  375. };
  376. int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
  377. int *is_alloced, int hash_nid, const uint8_t *msg,
  378. size_t msg_len) {
  379. unsigned i;
  380. if (hash_nid == NID_md5_sha1) {
  381. // Special case: SSL signature, just check the length.
  382. if (msg_len != SSL_SIG_LENGTH) {
  383. OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  384. return 0;
  385. }
  386. *out_msg = (uint8_t*) msg;
  387. *out_msg_len = SSL_SIG_LENGTH;
  388. *is_alloced = 0;
  389. return 1;
  390. }
  391. for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
  392. const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
  393. if (sig_prefix->nid != hash_nid) {
  394. continue;
  395. }
  396. if (msg_len != sig_prefix->hash_len) {
  397. OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  398. return 0;
  399. }
  400. const uint8_t* prefix = sig_prefix->bytes;
  401. unsigned prefix_len = sig_prefix->len;
  402. unsigned signed_msg_len;
  403. uint8_t *signed_msg;
  404. signed_msg_len = prefix_len + msg_len;
  405. if (signed_msg_len < prefix_len) {
  406. OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG);
  407. return 0;
  408. }
  409. signed_msg = OPENSSL_malloc(signed_msg_len);
  410. if (!signed_msg) {
  411. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  412. return 0;
  413. }
  414. OPENSSL_memcpy(signed_msg, prefix, prefix_len);
  415. OPENSSL_memcpy(signed_msg + prefix_len, msg, msg_len);
  416. *out_msg = signed_msg;
  417. *out_msg_len = signed_msg_len;
  418. *is_alloced = 1;
  419. return 1;
  420. }
  421. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
  422. return 0;
  423. }
  424. int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
  425. unsigned *out_len, RSA *rsa) {
  426. const unsigned rsa_size = RSA_size(rsa);
  427. int ret = 0;
  428. uint8_t *signed_msg = NULL;
  429. size_t signed_msg_len = 0;
  430. int signed_msg_is_alloced = 0;
  431. size_t size_t_out_len;
  432. if (rsa->meth->sign) {
  433. return rsa->meth->sign(hash_nid, in, in_len, out, out_len, rsa);
  434. }
  435. if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
  436. &signed_msg_is_alloced, hash_nid, in, in_len) ||
  437. !RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
  438. signed_msg_len, RSA_PKCS1_PADDING)) {
  439. goto err;
  440. }
  441. *out_len = size_t_out_len;
  442. ret = 1;
  443. err:
  444. if (signed_msg_is_alloced) {
  445. OPENSSL_free(signed_msg);
  446. }
  447. return ret;
  448. }
  449. int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  450. const uint8_t *in, size_t in_len, const EVP_MD *md,
  451. const EVP_MD *mgf1_md, int salt_len) {
  452. if (in_len != EVP_MD_size(md)) {
  453. OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  454. return 0;
  455. }
  456. size_t padded_len = RSA_size(rsa);
  457. uint8_t *padded = OPENSSL_malloc(padded_len);
  458. if (padded == NULL) {
  459. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  460. return 0;
  461. }
  462. int ret =
  463. RSA_padding_add_PKCS1_PSS_mgf1(rsa, padded, in, md, mgf1_md, salt_len) &&
  464. RSA_sign_raw(rsa, out_len, out, max_out, padded, padded_len,
  465. RSA_NO_PADDING);
  466. OPENSSL_free(padded);
  467. return ret;
  468. }
  469. int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
  470. const uint8_t *sig, size_t sig_len, RSA *rsa) {
  471. if (rsa->n == NULL || rsa->e == NULL) {
  472. OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
  473. return 0;
  474. }
  475. const size_t rsa_size = RSA_size(rsa);
  476. uint8_t *buf = NULL;
  477. int ret = 0;
  478. uint8_t *signed_msg = NULL;
  479. size_t signed_msg_len = 0, len;
  480. int signed_msg_is_alloced = 0;
  481. if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
  482. OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  483. return 0;
  484. }
  485. buf = OPENSSL_malloc(rsa_size);
  486. if (!buf) {
  487. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  488. return 0;
  489. }
  490. if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
  491. RSA_PKCS1_PADDING)) {
  492. goto out;
  493. }
  494. if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
  495. &signed_msg_is_alloced, hash_nid, msg, msg_len)) {
  496. goto out;
  497. }
  498. // Check that no other information follows the hash value (FIPS 186-4 Section
  499. // 5.5) and it matches the expected hash.
  500. if (len != signed_msg_len || OPENSSL_memcmp(buf, signed_msg, len) != 0) {
  501. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
  502. goto out;
  503. }
  504. ret = 1;
  505. out:
  506. OPENSSL_free(buf);
  507. if (signed_msg_is_alloced) {
  508. OPENSSL_free(signed_msg);
  509. }
  510. return ret;
  511. }
  512. int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *msg, size_t msg_len,
  513. const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len,
  514. const uint8_t *sig, size_t sig_len) {
  515. if (msg_len != EVP_MD_size(md)) {
  516. OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  517. return 0;
  518. }
  519. size_t em_len = RSA_size(rsa);
  520. uint8_t *em = OPENSSL_malloc(em_len);
  521. if (em == NULL) {
  522. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  523. return 0;
  524. }
  525. int ret = 0;
  526. if (!RSA_verify_raw(rsa, &em_len, em, em_len, sig, sig_len, RSA_NO_PADDING)) {
  527. goto err;
  528. }
  529. if (em_len != RSA_size(rsa)) {
  530. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  531. goto err;
  532. }
  533. ret = RSA_verify_PKCS1_PSS_mgf1(rsa, msg, md, mgf1_md, em, salt_len);
  534. err:
  535. OPENSSL_free(em);
  536. return ret;
  537. }
  538. int RSA_check_key(const RSA *key) {
  539. BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp_times_q;
  540. BN_CTX *ctx;
  541. int ok = 0, has_crt_values;
  542. if (RSA_is_opaque(key)) {
  543. // Opaque keys can't be checked.
  544. return 1;
  545. }
  546. if ((key->p != NULL) != (key->q != NULL)) {
  547. OPENSSL_PUT_ERROR(RSA, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
  548. return 0;
  549. }
  550. if (!key->n || !key->e) {
  551. OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
  552. return 0;
  553. }
  554. if (!key->d || !key->p) {
  555. // For a public key, or without p and q, there's nothing that can be
  556. // checked.
  557. return 1;
  558. }
  559. ctx = BN_CTX_new();
  560. if (ctx == NULL) {
  561. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  562. return 0;
  563. }
  564. BN_init(&n);
  565. BN_init(&pm1);
  566. BN_init(&qm1);
  567. BN_init(&lcm);
  568. BN_init(&gcd);
  569. BN_init(&de);
  570. BN_init(&dmp1);
  571. BN_init(&dmq1);
  572. BN_init(&iqmp_times_q);
  573. if (!BN_mul(&n, key->p, key->q, ctx) ||
  574. // lcm = lcm(p, q)
  575. !BN_sub(&pm1, key->p, BN_value_one()) ||
  576. !BN_sub(&qm1, key->q, BN_value_one()) ||
  577. !BN_mul(&lcm, &pm1, &qm1, ctx) ||
  578. !BN_gcd(&gcd, &pm1, &qm1, ctx)) {
  579. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  580. goto out;
  581. }
  582. if (!BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
  583. !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
  584. // de = d*e mod lcm(p, q).
  585. !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
  586. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  587. goto out;
  588. }
  589. if (BN_cmp(&n, key->n) != 0) {
  590. OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q);
  591. goto out;
  592. }
  593. if (!BN_is_one(&de)) {
  594. OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1);
  595. goto out;
  596. }
  597. has_crt_values = key->dmp1 != NULL;
  598. if (has_crt_values != (key->dmq1 != NULL) ||
  599. has_crt_values != (key->iqmp != NULL)) {
  600. OPENSSL_PUT_ERROR(RSA, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
  601. goto out;
  602. }
  603. if (has_crt_values) {
  604. if (// dmp1 = d mod (p-1)
  605. !BN_mod(&dmp1, key->d, &pm1, ctx) ||
  606. // dmq1 = d mod (q-1)
  607. !BN_mod(&dmq1, key->d, &qm1, ctx) ||
  608. // iqmp = q^-1 mod p
  609. !BN_mod_mul(&iqmp_times_q, key->iqmp, key->q, key->p, ctx)) {
  610. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  611. goto out;
  612. }
  613. if (BN_cmp(&dmp1, key->dmp1) != 0 ||
  614. BN_cmp(&dmq1, key->dmq1) != 0 ||
  615. BN_cmp(key->iqmp, key->p) >= 0 ||
  616. !BN_is_one(&iqmp_times_q)) {
  617. OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT);
  618. goto out;
  619. }
  620. }
  621. ok = 1;
  622. out:
  623. BN_free(&n);
  624. BN_free(&pm1);
  625. BN_free(&qm1);
  626. BN_free(&lcm);
  627. BN_free(&gcd);
  628. BN_free(&de);
  629. BN_free(&dmp1);
  630. BN_free(&dmq1);
  631. BN_free(&iqmp_times_q);
  632. BN_CTX_free(ctx);
  633. return ok;
  634. }
  635. // This is the product of the 132 smallest odd primes, from 3 to 751.
  636. static const BN_ULONG kSmallFactorsLimbs[] = {
  637. TOBN(0xc4309333, 0x3ef4e3e1), TOBN(0x71161eb6, 0xcd2d655f),
  638. TOBN(0x95e2238c, 0x0bf94862), TOBN(0x3eb233d3, 0x24f7912b),
  639. TOBN(0x6b55514b, 0xbf26c483), TOBN(0x0a84d817, 0x5a144871),
  640. TOBN(0x77d12fee, 0x9b82210a), TOBN(0xdb5b93c2, 0x97f050b3),
  641. TOBN(0x4acad6b9, 0x4d6c026b), TOBN(0xeb7751f3, 0x54aec893),
  642. TOBN(0xdba53368, 0x36bc85c4), TOBN(0xd85a1b28, 0x7f5ec78e),
  643. TOBN(0x2eb072d8, 0x6b322244), TOBN(0xbba51112, 0x5e2b3aea),
  644. TOBN(0x36ed1a6c, 0x0e2486bf), TOBN(0x5f270460, 0xec0c5727),
  645. 0x000017b1
  646. };
  647. DEFINE_LOCAL_DATA(BIGNUM, g_small_factors) {
  648. out->d = (BN_ULONG *) kSmallFactorsLimbs;
  649. out->top = OPENSSL_ARRAY_SIZE(kSmallFactorsLimbs);
  650. out->dmax = out->top;
  651. out->neg = 0;
  652. out->flags = BN_FLG_STATIC_DATA;
  653. }
  654. int RSA_check_fips(RSA *key) {
  655. if (RSA_is_opaque(key)) {
  656. // Opaque keys can't be checked.
  657. OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED);
  658. return 0;
  659. }
  660. if (!RSA_check_key(key)) {
  661. return 0;
  662. }
  663. BN_CTX *ctx = BN_CTX_new();
  664. if (ctx == NULL) {
  665. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  666. return 0;
  667. }
  668. BIGNUM small_gcd;
  669. BN_init(&small_gcd);
  670. int ret = 1;
  671. // Perform partial public key validation of RSA keys (SP 800-89 5.3.3).
  672. enum bn_primality_result_t primality_result;
  673. if (BN_num_bits(key->e) <= 16 ||
  674. BN_num_bits(key->e) > 256 ||
  675. !BN_is_odd(key->n) ||
  676. !BN_is_odd(key->e) ||
  677. !BN_gcd(&small_gcd, key->n, g_small_factors(), ctx) ||
  678. !BN_is_one(&small_gcd) ||
  679. !BN_enhanced_miller_rabin_primality_test(&primality_result, key->n,
  680. BN_prime_checks, ctx, NULL) ||
  681. primality_result != bn_non_prime_power_composite) {
  682. OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED);
  683. ret = 0;
  684. }
  685. BN_free(&small_gcd);
  686. BN_CTX_free(ctx);
  687. if (!ret || key->d == NULL || key->p == NULL) {
  688. // On a failure or on only a public key, there's nothing else can be
  689. // checked.
  690. return ret;
  691. }
  692. // FIPS pairwise consistency test (FIPS 140-2 4.9.2). Per FIPS 140-2 IG,
  693. // section 9.9, it is not known whether |rsa| will be used for signing or
  694. // encryption, so either pair-wise consistency self-test is acceptable. We
  695. // perform a signing test.
  696. uint8_t data[32] = {0};
  697. unsigned sig_len = RSA_size(key);
  698. uint8_t *sig = OPENSSL_malloc(sig_len);
  699. if (sig == NULL) {
  700. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  701. return 0;
  702. }
  703. if (!RSA_sign(NID_sha256, data, sizeof(data), sig, &sig_len, key)) {
  704. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  705. ret = 0;
  706. goto cleanup;
  707. }
  708. #if defined(BORINGSSL_FIPS_BREAK_RSA_PWCT)
  709. data[0] = ~data[0];
  710. #endif
  711. if (!RSA_verify(NID_sha256, data, sizeof(data), sig, sig_len, key)) {
  712. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  713. ret = 0;
  714. }
  715. cleanup:
  716. OPENSSL_free(sig);
  717. return ret;
  718. }
  719. int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
  720. size_t len) {
  721. if (rsa->meth->private_transform) {
  722. return rsa->meth->private_transform(rsa, out, in, len);
  723. }
  724. return rsa_default_private_transform(rsa, out, in, len);
  725. }
  726. int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) {
  727. return 1;
  728. }