rsa_pmeth.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /* crypto/rsa/rsa_pmeth.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2006.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2006 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. * licensing@OpenSSL.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  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 = 1024;
  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_ctx(EVP_PKEY_CTX *ctx)
  171. {
  172. RSA_PKEY_CTX *rctx = ctx->data;
  173. RSA *rsa = ctx->pkey->pkey.rsa;
  174. int rv = -1;
  175. if (!FIPS_mode())
  176. return 0;
  177. if (rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
  178. rv = 0;
  179. if (!(rsa->meth->flags & RSA_FLAG_FIPS_METHOD) && rv)
  180. return -1;
  181. if (rctx->md) {
  182. const EVP_MD *fmd;
  183. fmd = FIPS_get_digestbynid(EVP_MD_type(rctx->md));
  184. if (!fmd || !(fmd->flags & EVP_MD_FLAG_FIPS))
  185. return rv;
  186. }
  187. if (rctx->mgf1md && !(rctx->mgf1md->flags & EVP_MD_FLAG_FIPS)) {
  188. const EVP_MD *fmd;
  189. fmd = FIPS_get_digestbynid(EVP_MD_type(rctx->mgf1md));
  190. if (!fmd || !(fmd->flags & EVP_MD_FLAG_FIPS))
  191. return rv;
  192. }
  193. return 1;
  194. }
  195. #endif
  196. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
  197. size_t *siglen, const unsigned char *tbs,
  198. size_t tbslen)
  199. {
  200. int ret;
  201. RSA_PKEY_CTX *rctx = ctx->data;
  202. RSA *rsa = ctx->pkey->pkey.rsa;
  203. #ifdef OPENSSL_FIPS
  204. ret = pkey_fips_check_ctx(ctx);
  205. if (ret < 0) {
  206. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
  207. return -1;
  208. }
  209. #endif
  210. if (rctx->md) {
  211. if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
  212. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
  213. return -1;
  214. }
  215. #ifdef OPENSSL_FIPS
  216. if (ret > 0) {
  217. unsigned int slen;
  218. ret = FIPS_rsa_sign_digest(rsa, tbs, tbslen, rctx->md,
  219. rctx->pad_mode,
  220. rctx->saltlen,
  221. rctx->mgf1md, sig, &slen);
  222. if (ret > 0)
  223. *siglen = slen;
  224. else
  225. *siglen = 0;
  226. return ret;
  227. }
  228. #endif
  229. if (EVP_MD_type(rctx->md) == NID_mdc2) {
  230. unsigned int sltmp;
  231. if (rctx->pad_mode != RSA_PKCS1_PADDING)
  232. return -1;
  233. ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
  234. tbs, tbslen, sig, &sltmp, rsa);
  235. if (ret <= 0)
  236. return ret;
  237. ret = sltmp;
  238. } else if (rctx->pad_mode == RSA_X931_PADDING) {
  239. if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
  240. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
  241. return -1;
  242. }
  243. if (!setup_tbuf(rctx, ctx)) {
  244. RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
  245. return -1;
  246. }
  247. memcpy(rctx->tbuf, tbs, tbslen);
  248. rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
  249. ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
  250. sig, rsa, RSA_X931_PADDING);
  251. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  252. unsigned int sltmp;
  253. ret = RSA_sign(EVP_MD_type(rctx->md),
  254. tbs, tbslen, sig, &sltmp, rsa);
  255. if (ret <= 0)
  256. return ret;
  257. ret = sltmp;
  258. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  259. if (!setup_tbuf(rctx, ctx))
  260. return -1;
  261. if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
  262. rctx->tbuf, tbs,
  263. rctx->md, rctx->mgf1md,
  264. rctx->saltlen))
  265. return -1;
  266. ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
  267. sig, rsa, RSA_NO_PADDING);
  268. } else
  269. return -1;
  270. } else
  271. ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
  272. rctx->pad_mode);
  273. if (ret < 0)
  274. return ret;
  275. *siglen = ret;
  276. return 1;
  277. }
  278. static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
  279. unsigned char *rout, size_t *routlen,
  280. const unsigned char *sig, size_t siglen)
  281. {
  282. int ret;
  283. RSA_PKEY_CTX *rctx = ctx->data;
  284. if (rctx->md) {
  285. if (rctx->pad_mode == RSA_X931_PADDING) {
  286. if (!setup_tbuf(rctx, ctx))
  287. return -1;
  288. ret = RSA_public_decrypt(siglen, sig,
  289. rctx->tbuf, ctx->pkey->pkey.rsa,
  290. RSA_X931_PADDING);
  291. if (ret < 1)
  292. return 0;
  293. ret--;
  294. if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
  295. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  296. RSA_R_ALGORITHM_MISMATCH);
  297. return 0;
  298. }
  299. if (ret != EVP_MD_size(rctx->md)) {
  300. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  301. RSA_R_INVALID_DIGEST_LENGTH);
  302. return 0;
  303. }
  304. if (rout)
  305. memcpy(rout, rctx->tbuf, ret);
  306. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  307. size_t sltmp;
  308. ret = int_rsa_verify(EVP_MD_type(rctx->md),
  309. NULL, 0, rout, &sltmp,
  310. sig, siglen, ctx->pkey->pkey.rsa);
  311. if (ret <= 0)
  312. return 0;
  313. ret = sltmp;
  314. } else
  315. return -1;
  316. } else
  317. ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
  318. rctx->pad_mode);
  319. if (ret < 0)
  320. return ret;
  321. *routlen = ret;
  322. return 1;
  323. }
  324. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
  325. const unsigned char *sig, size_t siglen,
  326. const unsigned char *tbs, size_t tbslen)
  327. {
  328. RSA_PKEY_CTX *rctx = ctx->data;
  329. RSA *rsa = ctx->pkey->pkey.rsa;
  330. size_t rslen;
  331. #ifdef OPENSSL_FIPS
  332. int rv;
  333. rv = pkey_fips_check_ctx(ctx);
  334. if (rv < 0) {
  335. RSAerr(RSA_F_PKEY_RSA_VERIFY,
  336. RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
  337. return -1;
  338. }
  339. #endif
  340. if (rctx->md) {
  341. #ifdef OPENSSL_FIPS
  342. if (rv > 0) {
  343. return FIPS_rsa_verify_digest(rsa,
  344. tbs, tbslen,
  345. rctx->md,
  346. rctx->pad_mode,
  347. rctx->saltlen,
  348. rctx->mgf1md, sig, siglen);
  349. }
  350. #endif
  351. if (rctx->pad_mode == RSA_PKCS1_PADDING)
  352. return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
  353. sig, siglen, rsa);
  354. if (rctx->pad_mode == RSA_X931_PADDING) {
  355. if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
  356. return 0;
  357. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  358. int ret;
  359. if (!setup_tbuf(rctx, ctx))
  360. return -1;
  361. ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  362. rsa, RSA_NO_PADDING);
  363. if (ret <= 0)
  364. return 0;
  365. ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
  366. rctx->md, rctx->mgf1md,
  367. rctx->tbuf, rctx->saltlen);
  368. if (ret <= 0)
  369. return 0;
  370. return 1;
  371. } else
  372. return -1;
  373. } else {
  374. if (!setup_tbuf(rctx, ctx))
  375. return -1;
  376. rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  377. rsa, rctx->pad_mode);
  378. if (rslen == 0)
  379. return 0;
  380. }
  381. if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
  382. return 0;
  383. return 1;
  384. }
  385. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
  386. unsigned char *out, size_t *outlen,
  387. const unsigned char *in, size_t inlen)
  388. {
  389. int ret;
  390. RSA_PKEY_CTX *rctx = ctx->data;
  391. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  392. int klen = RSA_size(ctx->pkey->pkey.rsa);
  393. if (!setup_tbuf(rctx, ctx))
  394. return -1;
  395. if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
  396. in, inlen,
  397. rctx->oaep_label,
  398. rctx->oaep_labellen,
  399. rctx->md, rctx->mgf1md))
  400. return -1;
  401. ret = RSA_public_encrypt(klen, rctx->tbuf, out,
  402. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  403. } else
  404. ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  405. rctx->pad_mode);
  406. if (ret < 0)
  407. return ret;
  408. *outlen = ret;
  409. return 1;
  410. }
  411. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
  412. unsigned char *out, size_t *outlen,
  413. const unsigned char *in, size_t inlen)
  414. {
  415. int ret;
  416. RSA_PKEY_CTX *rctx = ctx->data;
  417. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  418. int i;
  419. if (!setup_tbuf(rctx, ctx))
  420. return -1;
  421. ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
  422. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  423. if (ret <= 0)
  424. return ret;
  425. for (i = 0; i < ret; i++) {
  426. if (rctx->tbuf[i])
  427. break;
  428. }
  429. ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
  430. ret - i, ret,
  431. rctx->oaep_label,
  432. rctx->oaep_labellen,
  433. rctx->md, rctx->mgf1md);
  434. } else
  435. ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  436. rctx->pad_mode);
  437. if (ret < 0)
  438. return ret;
  439. *outlen = ret;
  440. return 1;
  441. }
  442. static int check_padding_md(const EVP_MD *md, int padding)
  443. {
  444. if (!md)
  445. return 1;
  446. if (padding == RSA_NO_PADDING) {
  447. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
  448. return 0;
  449. }
  450. if (padding == RSA_X931_PADDING) {
  451. if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) {
  452. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
  453. return 0;
  454. }
  455. return 1;
  456. }
  457. return 1;
  458. }
  459. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  460. {
  461. RSA_PKEY_CTX *rctx = ctx->data;
  462. switch (type) {
  463. case EVP_PKEY_CTRL_RSA_PADDING:
  464. if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
  465. if (!check_padding_md(rctx->md, p1))
  466. return 0;
  467. if (p1 == RSA_PKCS1_PSS_PADDING) {
  468. if (!(ctx->operation &
  469. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
  470. goto bad_pad;
  471. if (!rctx->md)
  472. rctx->md = EVP_sha1();
  473. }
  474. if (p1 == RSA_PKCS1_OAEP_PADDING) {
  475. if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
  476. goto bad_pad;
  477. if (!rctx->md)
  478. rctx->md = EVP_sha1();
  479. }
  480. rctx->pad_mode = p1;
  481. return 1;
  482. }
  483. bad_pad:
  484. RSAerr(RSA_F_PKEY_RSA_CTRL,
  485. RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  486. return -2;
  487. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  488. *(int *)p2 = rctx->pad_mode;
  489. return 1;
  490. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  491. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  492. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  493. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
  494. return -2;
  495. }
  496. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
  497. *(int *)p2 = rctx->saltlen;
  498. else {
  499. if (p1 < -2)
  500. return -2;
  501. rctx->saltlen = p1;
  502. }
  503. return 1;
  504. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  505. if (p1 < 256) {
  506. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
  507. return -2;
  508. }
  509. rctx->nbits = p1;
  510. return 1;
  511. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  512. if (!p2)
  513. return -2;
  514. BN_free(rctx->pub_exp);
  515. rctx->pub_exp = p2;
  516. return 1;
  517. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  518. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  519. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  520. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  521. return -2;
  522. }
  523. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
  524. *(const EVP_MD **)p2 = rctx->md;
  525. else
  526. rctx->md = p2;
  527. return 1;
  528. case EVP_PKEY_CTRL_MD:
  529. if (!check_padding_md(p2, rctx->pad_mode))
  530. return 0;
  531. rctx->md = p2;
  532. return 1;
  533. case EVP_PKEY_CTRL_GET_MD:
  534. *(const EVP_MD **)p2 = rctx->md;
  535. return 1;
  536. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  537. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  538. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
  539. && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  540. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
  541. return -2;
  542. }
  543. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  544. if (rctx->mgf1md)
  545. *(const EVP_MD **)p2 = rctx->mgf1md;
  546. else
  547. *(const EVP_MD **)p2 = rctx->md;
  548. } else
  549. rctx->mgf1md = p2;
  550. return 1;
  551. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  552. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  553. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  554. return -2;
  555. }
  556. if (rctx->oaep_label)
  557. OPENSSL_free(rctx->oaep_label);
  558. if (p2 && p1 > 0) {
  559. rctx->oaep_label = p2;
  560. rctx->oaep_labellen = p1;
  561. } else {
  562. rctx->oaep_label = NULL;
  563. rctx->oaep_labellen = 0;
  564. }
  565. return 1;
  566. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  567. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  568. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  569. return -2;
  570. }
  571. *(unsigned char **)p2 = rctx->oaep_label;
  572. return rctx->oaep_labellen;
  573. case EVP_PKEY_CTRL_DIGESTINIT:
  574. case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
  575. case EVP_PKEY_CTRL_PKCS7_DECRYPT:
  576. case EVP_PKEY_CTRL_PKCS7_SIGN:
  577. return 1;
  578. #ifndef OPENSSL_NO_CMS
  579. case EVP_PKEY_CTRL_CMS_DECRYPT:
  580. case EVP_PKEY_CTRL_CMS_ENCRYPT:
  581. case EVP_PKEY_CTRL_CMS_SIGN:
  582. return 1;
  583. #endif
  584. case EVP_PKEY_CTRL_PEER_KEY:
  585. RSAerr(RSA_F_PKEY_RSA_CTRL,
  586. RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  587. return -2;
  588. default:
  589. return -2;
  590. }
  591. }
  592. static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
  593. const char *type, const char *value)
  594. {
  595. if (!value) {
  596. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
  597. return 0;
  598. }
  599. if (!strcmp(type, "rsa_padding_mode")) {
  600. int pm;
  601. if (!strcmp(value, "pkcs1"))
  602. pm = RSA_PKCS1_PADDING;
  603. else if (!strcmp(value, "sslv23"))
  604. pm = RSA_SSLV23_PADDING;
  605. else if (!strcmp(value, "none"))
  606. pm = RSA_NO_PADDING;
  607. else if (!strcmp(value, "oeap"))
  608. pm = RSA_PKCS1_OAEP_PADDING;
  609. else if (!strcmp(value, "oaep"))
  610. pm = RSA_PKCS1_OAEP_PADDING;
  611. else if (!strcmp(value, "x931"))
  612. pm = RSA_X931_PADDING;
  613. else if (!strcmp(value, "pss"))
  614. pm = RSA_PKCS1_PSS_PADDING;
  615. else {
  616. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
  617. return -2;
  618. }
  619. return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
  620. }
  621. if (!strcmp(type, "rsa_pss_saltlen")) {
  622. int saltlen;
  623. saltlen = atoi(value);
  624. return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
  625. }
  626. if (!strcmp(type, "rsa_keygen_bits")) {
  627. int nbits;
  628. nbits = atoi(value);
  629. return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
  630. }
  631. if (!strcmp(type, "rsa_keygen_pubexp")) {
  632. int ret;
  633. BIGNUM *pubexp = NULL;
  634. if (!BN_asc2bn(&pubexp, value))
  635. return 0;
  636. ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
  637. if (ret <= 0)
  638. BN_free(pubexp);
  639. return ret;
  640. }
  641. if (!strcmp(type, "rsa_mgf1_md")) {
  642. const EVP_MD *md;
  643. if (!(md = EVP_get_digestbyname(value))) {
  644. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
  645. return 0;
  646. }
  647. return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
  648. }
  649. if (!strcmp(type, "rsa_oaep_md")) {
  650. const EVP_MD *md;
  651. if (!(md = EVP_get_digestbyname(value))) {
  652. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
  653. return 0;
  654. }
  655. return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
  656. }
  657. if (!strcmp(type, "rsa_oaep_label")) {
  658. unsigned char *lab;
  659. long lablen;
  660. int ret;
  661. lab = string_to_hex(value, &lablen);
  662. if (!lab)
  663. return 0;
  664. ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
  665. if (ret <= 0)
  666. OPENSSL_free(lab);
  667. return ret;
  668. }
  669. return -2;
  670. }
  671. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  672. {
  673. RSA *rsa = NULL;
  674. RSA_PKEY_CTX *rctx = ctx->data;
  675. BN_GENCB *pcb, cb;
  676. int ret;
  677. if (!rctx->pub_exp) {
  678. rctx->pub_exp = BN_new();
  679. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
  680. return 0;
  681. }
  682. rsa = RSA_new();
  683. if (!rsa)
  684. return 0;
  685. if (ctx->pkey_gencb) {
  686. pcb = &cb;
  687. evp_pkey_set_cb_translate(pcb, ctx);
  688. } else
  689. pcb = NULL;
  690. ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
  691. if (ret > 0)
  692. EVP_PKEY_assign_RSA(pkey, rsa);
  693. else
  694. RSA_free(rsa);
  695. return ret;
  696. }
  697. const EVP_PKEY_METHOD rsa_pkey_meth = {
  698. EVP_PKEY_RSA,
  699. EVP_PKEY_FLAG_AUTOARGLEN,
  700. pkey_rsa_init,
  701. pkey_rsa_copy,
  702. pkey_rsa_cleanup,
  703. 0, 0,
  704. 0,
  705. pkey_rsa_keygen,
  706. 0,
  707. pkey_rsa_sign,
  708. 0,
  709. pkey_rsa_verify,
  710. 0,
  711. pkey_rsa_verifyrecover,
  712. 0, 0, 0, 0,
  713. 0,
  714. pkey_rsa_encrypt,
  715. 0,
  716. pkey_rsa_decrypt,
  717. 0, 0,
  718. pkey_rsa_ctrl,
  719. pkey_rsa_ctrl_str
  720. };