2
0

rsa_pmeth.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /* crypto/rsa/rsa_pmeth.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project
  4. * 2006.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2006-2019 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * [email protected].
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/asn1t.h>
  62. #include <openssl/x509.h>
  63. #include <openssl/rsa.h>
  64. #include <openssl/bn.h>
  65. #include <openssl/evp.h>
  66. #include <openssl/x509v3.h>
  67. #ifndef OPENSSL_NO_CMS
  68. # include <openssl/cms.h>
  69. #endif
  70. #ifdef OPENSSL_FIPS
  71. # include <openssl/fips.h>
  72. #endif
  73. #include "evp_locl.h"
  74. #include "rsa_locl.h"
  75. /* RSA pkey context structure */
  76. typedef struct {
  77. /* Key gen parameters */
  78. int nbits;
  79. BIGNUM *pub_exp;
  80. /* Keygen callback info */
  81. int gentmp[2];
  82. /* RSA padding mode */
  83. int pad_mode;
  84. /* message digest */
  85. const EVP_MD *md;
  86. /* message digest for MGF1 */
  87. const EVP_MD *mgf1md;
  88. /* PSS salt length */
  89. int saltlen;
  90. /* Temp buffer */
  91. unsigned char *tbuf;
  92. /* OAEP label */
  93. unsigned char *oaep_label;
  94. size_t oaep_labellen;
  95. } RSA_PKEY_CTX;
  96. static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
  97. {
  98. RSA_PKEY_CTX *rctx;
  99. rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
  100. if (!rctx)
  101. return 0;
  102. rctx->nbits = 2048;
  103. rctx->pub_exp = NULL;
  104. rctx->pad_mode = RSA_PKCS1_PADDING;
  105. rctx->md = NULL;
  106. rctx->mgf1md = NULL;
  107. rctx->tbuf = NULL;
  108. rctx->saltlen = -2;
  109. rctx->oaep_label = NULL;
  110. rctx->oaep_labellen = 0;
  111. ctx->data = rctx;
  112. ctx->keygen_info = rctx->gentmp;
  113. ctx->keygen_info_count = 2;
  114. return 1;
  115. }
  116. static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  117. {
  118. RSA_PKEY_CTX *dctx, *sctx;
  119. if (!pkey_rsa_init(dst))
  120. return 0;
  121. sctx = src->data;
  122. dctx = dst->data;
  123. dctx->nbits = sctx->nbits;
  124. if (sctx->pub_exp) {
  125. dctx->pub_exp = BN_dup(sctx->pub_exp);
  126. if (!dctx->pub_exp)
  127. return 0;
  128. }
  129. dctx->pad_mode = sctx->pad_mode;
  130. dctx->md = sctx->md;
  131. dctx->mgf1md = sctx->mgf1md;
  132. if (sctx->oaep_label) {
  133. if (dctx->oaep_label)
  134. OPENSSL_free(dctx->oaep_label);
  135. dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
  136. if (!dctx->oaep_label)
  137. return 0;
  138. dctx->oaep_labellen = sctx->oaep_labellen;
  139. }
  140. return 1;
  141. }
  142. static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
  143. {
  144. if (ctx->tbuf)
  145. return 1;
  146. ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
  147. if (!ctx->tbuf)
  148. return 0;
  149. return 1;
  150. }
  151. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
  152. {
  153. RSA_PKEY_CTX *rctx = ctx->data;
  154. if (rctx) {
  155. if (rctx->pub_exp)
  156. BN_free(rctx->pub_exp);
  157. if (rctx->tbuf)
  158. OPENSSL_free(rctx->tbuf);
  159. if (rctx->oaep_label)
  160. OPENSSL_free(rctx->oaep_label);
  161. OPENSSL_free(rctx);
  162. }
  163. }
  164. #ifdef OPENSSL_FIPS
  165. /*
  166. * FIP checker. Return value indicates status of context parameters: 1 :
  167. * redirect to FIPS. 0 : don't redirect to FIPS. -1 : illegal operation in
  168. * FIPS mode.
  169. */
  170. static int pkey_fips_check_rsa(const RSA *rsa, const EVP_MD **pmd,
  171. const EVP_MD **pmgf1md)
  172. {
  173. int rv = -1;
  174. if (!FIPS_mode())
  175. return 0;
  176. if (rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
  177. rv = 0;
  178. if (!(rsa->meth->flags & RSA_FLAG_FIPS_METHOD) && rv)
  179. return -1;
  180. if (*pmd != NULL) {
  181. *pmd = FIPS_get_digestbynid(EVP_MD_type(*pmd));
  182. if (*pmd == NULL || !((*pmd)->flags & EVP_MD_FLAG_FIPS))
  183. return rv;
  184. }
  185. if (*pmgf1md != NULL) {
  186. *pmgf1md = FIPS_get_digestbynid(EVP_MD_type(*pmgf1md));
  187. if (*pmgf1md == NULL || !((*pmgf1md)->flags & EVP_MD_FLAG_FIPS))
  188. return rv;
  189. }
  190. return 1;
  191. }
  192. #endif
  193. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
  194. size_t *siglen, const unsigned char *tbs,
  195. size_t tbslen)
  196. {
  197. int ret;
  198. RSA_PKEY_CTX *rctx = ctx->data;
  199. RSA *rsa = ctx->pkey->pkey.rsa;
  200. const EVP_MD *md = rctx->md;
  201. const EVP_MD *mgf1md = rctx->mgf1md;
  202. #ifdef OPENSSL_FIPS
  203. ret = pkey_fips_check_rsa(rsa, &md, &mgf1md);
  204. if (ret < 0) {
  205. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
  206. return -1;
  207. }
  208. #endif
  209. if (md != NULL) {
  210. if (tbslen != (size_t)EVP_MD_size(md)) {
  211. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
  212. return -1;
  213. }
  214. #ifdef OPENSSL_FIPS
  215. if (ret > 0) {
  216. unsigned int slen;
  217. ret = FIPS_rsa_sign_digest(rsa, tbs, tbslen, md, rctx->pad_mode,
  218. rctx->saltlen, mgf1md, sig, &slen);
  219. if (ret > 0)
  220. *siglen = slen;
  221. else
  222. *siglen = 0;
  223. return ret;
  224. }
  225. #endif
  226. if (EVP_MD_type(md) == NID_mdc2) {
  227. unsigned int sltmp;
  228. if (rctx->pad_mode != RSA_PKCS1_PADDING)
  229. return -1;
  230. ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2, tbs, tbslen, sig, &sltmp,
  231. rsa);
  232. if (ret <= 0)
  233. return ret;
  234. ret = sltmp;
  235. } else if (rctx->pad_mode == RSA_X931_PADDING) {
  236. if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
  237. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
  238. return -1;
  239. }
  240. if (!setup_tbuf(rctx, ctx)) {
  241. RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
  242. return -1;
  243. }
  244. memcpy(rctx->tbuf, tbs, tbslen);
  245. rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(md));
  246. ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
  247. sig, rsa, RSA_X931_PADDING);
  248. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  249. unsigned int sltmp;
  250. ret = RSA_sign(EVP_MD_type(md), tbs, tbslen, sig, &sltmp, rsa);
  251. if (ret <= 0)
  252. return ret;
  253. ret = sltmp;
  254. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  255. if (!setup_tbuf(rctx, ctx))
  256. return -1;
  257. if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf, tbs,
  258. md, mgf1md, rctx->saltlen))
  259. return -1;
  260. ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
  261. sig, rsa, RSA_NO_PADDING);
  262. } else
  263. return -1;
  264. } else
  265. ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
  266. rctx->pad_mode);
  267. if (ret < 0)
  268. return ret;
  269. *siglen = ret;
  270. return 1;
  271. }
  272. static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
  273. unsigned char *rout, size_t *routlen,
  274. const unsigned char *sig, size_t siglen)
  275. {
  276. int ret;
  277. RSA_PKEY_CTX *rctx = ctx->data;
  278. if (rctx->md) {
  279. if (rctx->pad_mode == RSA_X931_PADDING) {
  280. if (!setup_tbuf(rctx, ctx))
  281. return -1;
  282. ret = RSA_public_decrypt(siglen, sig,
  283. rctx->tbuf, ctx->pkey->pkey.rsa,
  284. RSA_X931_PADDING);
  285. if (ret < 1)
  286. return 0;
  287. ret--;
  288. if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
  289. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  290. RSA_R_ALGORITHM_MISMATCH);
  291. return 0;
  292. }
  293. if (ret != EVP_MD_size(rctx->md)) {
  294. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  295. RSA_R_INVALID_DIGEST_LENGTH);
  296. return 0;
  297. }
  298. if (rout)
  299. memcpy(rout, rctx->tbuf, ret);
  300. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  301. size_t sltmp;
  302. ret = int_rsa_verify(EVP_MD_type(rctx->md),
  303. NULL, 0, rout, &sltmp,
  304. sig, siglen, ctx->pkey->pkey.rsa);
  305. if (ret <= 0)
  306. return 0;
  307. ret = sltmp;
  308. } else
  309. return -1;
  310. } else
  311. ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
  312. rctx->pad_mode);
  313. if (ret < 0)
  314. return ret;
  315. *routlen = ret;
  316. return 1;
  317. }
  318. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
  319. const unsigned char *sig, size_t siglen,
  320. const unsigned char *tbs, size_t tbslen)
  321. {
  322. RSA_PKEY_CTX *rctx = ctx->data;
  323. RSA *rsa = ctx->pkey->pkey.rsa;
  324. const EVP_MD *md = rctx->md;
  325. const EVP_MD *mgf1md = rctx->mgf1md;
  326. size_t rslen;
  327. #ifdef OPENSSL_FIPS
  328. int rv = pkey_fips_check_rsa(rsa, &md, &mgf1md);
  329. if (rv < 0) {
  330. RSAerr(RSA_F_PKEY_RSA_VERIFY,
  331. RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
  332. return -1;
  333. }
  334. #endif
  335. if (md != NULL) {
  336. #ifdef OPENSSL_FIPS
  337. if (rv > 0) {
  338. return FIPS_rsa_verify_digest(rsa, tbs, tbslen, md, rctx->pad_mode,
  339. rctx->saltlen, mgf1md, sig, siglen);
  340. }
  341. #endif
  342. if (rctx->pad_mode == RSA_PKCS1_PADDING)
  343. return RSA_verify(EVP_MD_type(md), tbs, tbslen,
  344. sig, siglen, rsa);
  345. if (tbslen != (size_t)EVP_MD_size(md)) {
  346. RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
  347. return -1;
  348. }
  349. if (rctx->pad_mode == RSA_X931_PADDING) {
  350. if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
  351. return 0;
  352. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  353. int ret;
  354. if (!setup_tbuf(rctx, ctx))
  355. return -1;
  356. ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  357. rsa, RSA_NO_PADDING);
  358. if (ret <= 0)
  359. return 0;
  360. ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, md, mgf1md,
  361. rctx->tbuf, rctx->saltlen);
  362. if (ret <= 0)
  363. return 0;
  364. return 1;
  365. } else
  366. return -1;
  367. } else {
  368. if (!setup_tbuf(rctx, ctx))
  369. return -1;
  370. rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  371. rsa, rctx->pad_mode);
  372. if (rslen == 0)
  373. return 0;
  374. }
  375. if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
  376. return 0;
  377. return 1;
  378. }
  379. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
  380. unsigned char *out, size_t *outlen,
  381. const unsigned char *in, size_t inlen)
  382. {
  383. int ret;
  384. RSA_PKEY_CTX *rctx = ctx->data;
  385. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  386. int klen = RSA_size(ctx->pkey->pkey.rsa);
  387. if (!setup_tbuf(rctx, ctx))
  388. return -1;
  389. if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
  390. in, inlen,
  391. rctx->oaep_label,
  392. rctx->oaep_labellen,
  393. rctx->md, rctx->mgf1md))
  394. return -1;
  395. ret = RSA_public_encrypt(klen, rctx->tbuf, out,
  396. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  397. } else
  398. ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  399. rctx->pad_mode);
  400. if (ret < 0)
  401. return ret;
  402. *outlen = ret;
  403. return 1;
  404. }
  405. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
  406. unsigned char *out, size_t *outlen,
  407. const unsigned char *in, size_t inlen)
  408. {
  409. int ret;
  410. RSA_PKEY_CTX *rctx = ctx->data;
  411. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  412. if (!setup_tbuf(rctx, ctx))
  413. return -1;
  414. ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
  415. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  416. if (ret <= 0)
  417. return ret;
  418. ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
  419. ret, ret,
  420. rctx->oaep_label,
  421. rctx->oaep_labellen,
  422. rctx->md, rctx->mgf1md);
  423. } else
  424. ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  425. rctx->pad_mode);
  426. if (ret < 0)
  427. return ret;
  428. *outlen = ret;
  429. return 1;
  430. }
  431. static int check_padding_md(const EVP_MD *md, int padding)
  432. {
  433. if (!md)
  434. return 1;
  435. if (padding == RSA_NO_PADDING) {
  436. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
  437. return 0;
  438. }
  439. if (padding == RSA_X931_PADDING) {
  440. if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) {
  441. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
  442. return 0;
  443. }
  444. return 1;
  445. }
  446. return 1;
  447. }
  448. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  449. {
  450. RSA_PKEY_CTX *rctx = ctx->data;
  451. switch (type) {
  452. case EVP_PKEY_CTRL_RSA_PADDING:
  453. if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
  454. if (!check_padding_md(rctx->md, p1))
  455. return 0;
  456. if (p1 == RSA_PKCS1_PSS_PADDING) {
  457. if (!(ctx->operation &
  458. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
  459. goto bad_pad;
  460. if (!rctx->md)
  461. rctx->md = EVP_sha1();
  462. }
  463. if (p1 == RSA_PKCS1_OAEP_PADDING) {
  464. if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
  465. goto bad_pad;
  466. if (!rctx->md)
  467. rctx->md = EVP_sha1();
  468. }
  469. rctx->pad_mode = p1;
  470. return 1;
  471. }
  472. bad_pad:
  473. RSAerr(RSA_F_PKEY_RSA_CTRL,
  474. RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  475. return -2;
  476. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  477. *(int *)p2 = rctx->pad_mode;
  478. return 1;
  479. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  480. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  481. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  482. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
  483. return -2;
  484. }
  485. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
  486. *(int *)p2 = rctx->saltlen;
  487. else {
  488. if (p1 < -2)
  489. return -2;
  490. rctx->saltlen = p1;
  491. }
  492. return 1;
  493. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  494. if (p1 < 256) {
  495. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
  496. return -2;
  497. }
  498. rctx->nbits = p1;
  499. return 1;
  500. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  501. if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
  502. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
  503. return -2;
  504. }
  505. BN_free(rctx->pub_exp);
  506. rctx->pub_exp = p2;
  507. return 1;
  508. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  509. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  510. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  511. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  512. return -2;
  513. }
  514. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
  515. *(const EVP_MD **)p2 = rctx->md;
  516. else
  517. rctx->md = p2;
  518. return 1;
  519. case EVP_PKEY_CTRL_MD:
  520. if (!check_padding_md(p2, rctx->pad_mode))
  521. return 0;
  522. rctx->md = p2;
  523. return 1;
  524. case EVP_PKEY_CTRL_GET_MD:
  525. *(const EVP_MD **)p2 = rctx->md;
  526. return 1;
  527. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  528. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  529. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
  530. && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  531. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
  532. return -2;
  533. }
  534. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  535. if (rctx->mgf1md)
  536. *(const EVP_MD **)p2 = rctx->mgf1md;
  537. else
  538. *(const EVP_MD **)p2 = rctx->md;
  539. } else
  540. rctx->mgf1md = p2;
  541. return 1;
  542. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  543. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  544. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  545. return -2;
  546. }
  547. if (rctx->oaep_label)
  548. OPENSSL_free(rctx->oaep_label);
  549. if (p2 && p1 > 0) {
  550. rctx->oaep_label = p2;
  551. rctx->oaep_labellen = p1;
  552. } else {
  553. rctx->oaep_label = NULL;
  554. rctx->oaep_labellen = 0;
  555. }
  556. return 1;
  557. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  558. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  559. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  560. return -2;
  561. }
  562. *(unsigned char **)p2 = rctx->oaep_label;
  563. return rctx->oaep_labellen;
  564. case EVP_PKEY_CTRL_DIGESTINIT:
  565. case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
  566. case EVP_PKEY_CTRL_PKCS7_DECRYPT:
  567. case EVP_PKEY_CTRL_PKCS7_SIGN:
  568. return 1;
  569. #ifndef OPENSSL_NO_CMS
  570. case EVP_PKEY_CTRL_CMS_DECRYPT:
  571. case EVP_PKEY_CTRL_CMS_ENCRYPT:
  572. case EVP_PKEY_CTRL_CMS_SIGN:
  573. return 1;
  574. #endif
  575. case EVP_PKEY_CTRL_PEER_KEY:
  576. RSAerr(RSA_F_PKEY_RSA_CTRL,
  577. RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  578. return -2;
  579. default:
  580. return -2;
  581. }
  582. }
  583. static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
  584. const char *type, const char *value)
  585. {
  586. if (!value) {
  587. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
  588. return 0;
  589. }
  590. if (!strcmp(type, "rsa_padding_mode")) {
  591. int pm;
  592. if (!strcmp(value, "pkcs1"))
  593. pm = RSA_PKCS1_PADDING;
  594. else if (!strcmp(value, "sslv23"))
  595. pm = RSA_SSLV23_PADDING;
  596. else if (!strcmp(value, "none"))
  597. pm = RSA_NO_PADDING;
  598. else if (!strcmp(value, "oeap"))
  599. pm = RSA_PKCS1_OAEP_PADDING;
  600. else if (!strcmp(value, "oaep"))
  601. pm = RSA_PKCS1_OAEP_PADDING;
  602. else if (!strcmp(value, "x931"))
  603. pm = RSA_X931_PADDING;
  604. else if (!strcmp(value, "pss"))
  605. pm = RSA_PKCS1_PSS_PADDING;
  606. else {
  607. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
  608. return -2;
  609. }
  610. return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
  611. }
  612. if (!strcmp(type, "rsa_pss_saltlen")) {
  613. int saltlen;
  614. saltlen = atoi(value);
  615. return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
  616. }
  617. if (!strcmp(type, "rsa_keygen_bits")) {
  618. int nbits;
  619. nbits = atoi(value);
  620. return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
  621. }
  622. if (!strcmp(type, "rsa_keygen_pubexp")) {
  623. int ret;
  624. BIGNUM *pubexp = NULL;
  625. if (!BN_asc2bn(&pubexp, value))
  626. return 0;
  627. ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
  628. if (ret <= 0)
  629. BN_free(pubexp);
  630. return ret;
  631. }
  632. if (!strcmp(type, "rsa_mgf1_md")) {
  633. const EVP_MD *md;
  634. if (!(md = EVP_get_digestbyname(value))) {
  635. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
  636. return 0;
  637. }
  638. return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
  639. }
  640. if (!strcmp(type, "rsa_oaep_md")) {
  641. const EVP_MD *md;
  642. if (!(md = EVP_get_digestbyname(value))) {
  643. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
  644. return 0;
  645. }
  646. return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
  647. }
  648. if (!strcmp(type, "rsa_oaep_label")) {
  649. unsigned char *lab;
  650. long lablen;
  651. int ret;
  652. lab = string_to_hex(value, &lablen);
  653. if (!lab)
  654. return 0;
  655. ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
  656. if (ret <= 0)
  657. OPENSSL_free(lab);
  658. return ret;
  659. }
  660. return -2;
  661. }
  662. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  663. {
  664. RSA *rsa = NULL;
  665. RSA_PKEY_CTX *rctx = ctx->data;
  666. BN_GENCB *pcb, cb;
  667. int ret;
  668. if (!rctx->pub_exp) {
  669. rctx->pub_exp = BN_new();
  670. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
  671. return 0;
  672. }
  673. rsa = RSA_new();
  674. if (!rsa)
  675. return 0;
  676. if (ctx->pkey_gencb) {
  677. pcb = &cb;
  678. evp_pkey_set_cb_translate(pcb, ctx);
  679. } else
  680. pcb = NULL;
  681. ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
  682. if (ret > 0)
  683. EVP_PKEY_assign_RSA(pkey, rsa);
  684. else
  685. RSA_free(rsa);
  686. return ret;
  687. }
  688. const EVP_PKEY_METHOD rsa_pkey_meth = {
  689. EVP_PKEY_RSA,
  690. EVP_PKEY_FLAG_AUTOARGLEN,
  691. pkey_rsa_init,
  692. pkey_rsa_copy,
  693. pkey_rsa_cleanup,
  694. 0, 0,
  695. 0,
  696. pkey_rsa_keygen,
  697. 0,
  698. pkey_rsa_sign,
  699. 0,
  700. pkey_rsa_verify,
  701. 0,
  702. pkey_rsa_verifyrecover,
  703. 0, 0, 0, 0,
  704. 0,
  705. pkey_rsa_encrypt,
  706. 0,
  707. pkey_rsa_decrypt,
  708. 0, 0,
  709. pkey_rsa_ctrl,
  710. pkey_rsa_ctrl_str
  711. };