rsa.c 23 KB

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