rsa_pmeth.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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 (tbslen != (size_t)EVP_MD_size(rctx->md)) {
  355. RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
  356. return -1;
  357. }
  358. if (rctx->pad_mode == RSA_X931_PADDING) {
  359. if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
  360. return 0;
  361. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  362. int ret;
  363. if (!setup_tbuf(rctx, ctx))
  364. return -1;
  365. ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  366. rsa, RSA_NO_PADDING);
  367. if (ret <= 0)
  368. return 0;
  369. ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
  370. rctx->md, rctx->mgf1md,
  371. rctx->tbuf, rctx->saltlen);
  372. if (ret <= 0)
  373. return 0;
  374. return 1;
  375. } else
  376. return -1;
  377. } else {
  378. if (!setup_tbuf(rctx, ctx))
  379. return -1;
  380. rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  381. rsa, rctx->pad_mode);
  382. if (rslen == 0)
  383. return 0;
  384. }
  385. if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
  386. return 0;
  387. return 1;
  388. }
  389. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
  390. unsigned char *out, size_t *outlen,
  391. const unsigned char *in, size_t inlen)
  392. {
  393. int ret;
  394. RSA_PKEY_CTX *rctx = ctx->data;
  395. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  396. int klen = RSA_size(ctx->pkey->pkey.rsa);
  397. if (!setup_tbuf(rctx, ctx))
  398. return -1;
  399. if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
  400. in, inlen,
  401. rctx->oaep_label,
  402. rctx->oaep_labellen,
  403. rctx->md, rctx->mgf1md))
  404. return -1;
  405. ret = RSA_public_encrypt(klen, rctx->tbuf, out,
  406. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  407. } else
  408. ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  409. rctx->pad_mode);
  410. if (ret < 0)
  411. return ret;
  412. *outlen = ret;
  413. return 1;
  414. }
  415. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
  416. unsigned char *out, size_t *outlen,
  417. const unsigned char *in, size_t inlen)
  418. {
  419. int ret;
  420. RSA_PKEY_CTX *rctx = ctx->data;
  421. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  422. if (!setup_tbuf(rctx, ctx))
  423. return -1;
  424. ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
  425. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  426. if (ret <= 0)
  427. return ret;
  428. ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
  429. ret, ret,
  430. rctx->oaep_label,
  431. rctx->oaep_labellen,
  432. rctx->md, rctx->mgf1md);
  433. } else
  434. ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  435. rctx->pad_mode);
  436. if (ret < 0)
  437. return ret;
  438. *outlen = ret;
  439. return 1;
  440. }
  441. static int check_padding_md(const EVP_MD *md, int padding)
  442. {
  443. if (!md)
  444. return 1;
  445. if (padding == RSA_NO_PADDING) {
  446. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
  447. return 0;
  448. }
  449. if (padding == RSA_X931_PADDING) {
  450. if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) {
  451. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
  452. return 0;
  453. }
  454. return 1;
  455. }
  456. return 1;
  457. }
  458. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  459. {
  460. RSA_PKEY_CTX *rctx = ctx->data;
  461. switch (type) {
  462. case EVP_PKEY_CTRL_RSA_PADDING:
  463. if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
  464. if (!check_padding_md(rctx->md, p1))
  465. return 0;
  466. if (p1 == RSA_PKCS1_PSS_PADDING) {
  467. if (!(ctx->operation &
  468. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
  469. goto bad_pad;
  470. if (!rctx->md)
  471. rctx->md = EVP_sha1();
  472. }
  473. if (p1 == RSA_PKCS1_OAEP_PADDING) {
  474. if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
  475. goto bad_pad;
  476. if (!rctx->md)
  477. rctx->md = EVP_sha1();
  478. }
  479. rctx->pad_mode = p1;
  480. return 1;
  481. }
  482. bad_pad:
  483. RSAerr(RSA_F_PKEY_RSA_CTRL,
  484. RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  485. return -2;
  486. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  487. *(int *)p2 = rctx->pad_mode;
  488. return 1;
  489. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  490. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  491. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  492. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
  493. return -2;
  494. }
  495. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
  496. *(int *)p2 = rctx->saltlen;
  497. else {
  498. if (p1 < -2)
  499. return -2;
  500. rctx->saltlen = p1;
  501. }
  502. return 1;
  503. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  504. if (p1 < 256) {
  505. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
  506. return -2;
  507. }
  508. rctx->nbits = p1;
  509. return 1;
  510. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  511. if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
  512. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
  513. return -2;
  514. }
  515. BN_free(rctx->pub_exp);
  516. rctx->pub_exp = p2;
  517. return 1;
  518. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  519. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  520. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  521. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  522. return -2;
  523. }
  524. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
  525. *(const EVP_MD **)p2 = rctx->md;
  526. else
  527. rctx->md = p2;
  528. return 1;
  529. case EVP_PKEY_CTRL_MD:
  530. if (!check_padding_md(p2, rctx->pad_mode))
  531. return 0;
  532. rctx->md = p2;
  533. return 1;
  534. case EVP_PKEY_CTRL_GET_MD:
  535. *(const EVP_MD **)p2 = rctx->md;
  536. return 1;
  537. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  538. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  539. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
  540. && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  541. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
  542. return -2;
  543. }
  544. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  545. if (rctx->mgf1md)
  546. *(const EVP_MD **)p2 = rctx->mgf1md;
  547. else
  548. *(const EVP_MD **)p2 = rctx->md;
  549. } else
  550. rctx->mgf1md = p2;
  551. return 1;
  552. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  553. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  554. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  555. return -2;
  556. }
  557. if (rctx->oaep_label)
  558. OPENSSL_free(rctx->oaep_label);
  559. if (p2 && p1 > 0) {
  560. rctx->oaep_label = p2;
  561. rctx->oaep_labellen = p1;
  562. } else {
  563. rctx->oaep_label = NULL;
  564. rctx->oaep_labellen = 0;
  565. }
  566. return 1;
  567. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  568. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  569. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  570. return -2;
  571. }
  572. *(unsigned char **)p2 = rctx->oaep_label;
  573. return rctx->oaep_labellen;
  574. case EVP_PKEY_CTRL_DIGESTINIT:
  575. case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
  576. case EVP_PKEY_CTRL_PKCS7_DECRYPT:
  577. case EVP_PKEY_CTRL_PKCS7_SIGN:
  578. return 1;
  579. #ifndef OPENSSL_NO_CMS
  580. case EVP_PKEY_CTRL_CMS_DECRYPT:
  581. case EVP_PKEY_CTRL_CMS_ENCRYPT:
  582. case EVP_PKEY_CTRL_CMS_SIGN:
  583. return 1;
  584. #endif
  585. case EVP_PKEY_CTRL_PEER_KEY:
  586. RSAerr(RSA_F_PKEY_RSA_CTRL,
  587. RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  588. return -2;
  589. default:
  590. return -2;
  591. }
  592. }
  593. static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
  594. const char *type, const char *value)
  595. {
  596. if (!value) {
  597. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
  598. return 0;
  599. }
  600. if (!strcmp(type, "rsa_padding_mode")) {
  601. int pm;
  602. if (!strcmp(value, "pkcs1"))
  603. pm = RSA_PKCS1_PADDING;
  604. else if (!strcmp(value, "sslv23"))
  605. pm = RSA_SSLV23_PADDING;
  606. else if (!strcmp(value, "none"))
  607. pm = RSA_NO_PADDING;
  608. else if (!strcmp(value, "oeap"))
  609. pm = RSA_PKCS1_OAEP_PADDING;
  610. else if (!strcmp(value, "oaep"))
  611. pm = RSA_PKCS1_OAEP_PADDING;
  612. else if (!strcmp(value, "x931"))
  613. pm = RSA_X931_PADDING;
  614. else if (!strcmp(value, "pss"))
  615. pm = RSA_PKCS1_PSS_PADDING;
  616. else {
  617. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
  618. return -2;
  619. }
  620. return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
  621. }
  622. if (!strcmp(type, "rsa_pss_saltlen")) {
  623. int saltlen;
  624. saltlen = atoi(value);
  625. return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
  626. }
  627. if (!strcmp(type, "rsa_keygen_bits")) {
  628. int nbits;
  629. nbits = atoi(value);
  630. return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
  631. }
  632. if (!strcmp(type, "rsa_keygen_pubexp")) {
  633. int ret;
  634. BIGNUM *pubexp = NULL;
  635. if (!BN_asc2bn(&pubexp, value))
  636. return 0;
  637. ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
  638. if (ret <= 0)
  639. BN_free(pubexp);
  640. return ret;
  641. }
  642. if (!strcmp(type, "rsa_mgf1_md")) {
  643. const EVP_MD *md;
  644. if (!(md = EVP_get_digestbyname(value))) {
  645. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
  646. return 0;
  647. }
  648. return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
  649. }
  650. if (!strcmp(type, "rsa_oaep_md")) {
  651. const EVP_MD *md;
  652. if (!(md = EVP_get_digestbyname(value))) {
  653. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
  654. return 0;
  655. }
  656. return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
  657. }
  658. if (!strcmp(type, "rsa_oaep_label")) {
  659. unsigned char *lab;
  660. long lablen;
  661. int ret;
  662. lab = string_to_hex(value, &lablen);
  663. if (!lab)
  664. return 0;
  665. ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
  666. if (ret <= 0)
  667. OPENSSL_free(lab);
  668. return ret;
  669. }
  670. return -2;
  671. }
  672. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  673. {
  674. RSA *rsa = NULL;
  675. RSA_PKEY_CTX *rctx = ctx->data;
  676. BN_GENCB *pcb, cb;
  677. int ret;
  678. if (!rctx->pub_exp) {
  679. rctx->pub_exp = BN_new();
  680. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
  681. return 0;
  682. }
  683. rsa = RSA_new();
  684. if (!rsa)
  685. return 0;
  686. if (ctx->pkey_gencb) {
  687. pcb = &cb;
  688. evp_pkey_set_cb_translate(pcb, ctx);
  689. } else
  690. pcb = NULL;
  691. ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
  692. if (ret > 0)
  693. EVP_PKEY_assign_RSA(pkey, rsa);
  694. else
  695. RSA_free(rsa);
  696. return ret;
  697. }
  698. const EVP_PKEY_METHOD rsa_pkey_meth = {
  699. EVP_PKEY_RSA,
  700. EVP_PKEY_FLAG_AUTOARGLEN,
  701. pkey_rsa_init,
  702. pkey_rsa_copy,
  703. pkey_rsa_cleanup,
  704. 0, 0,
  705. 0,
  706. pkey_rsa_keygen,
  707. 0,
  708. pkey_rsa_sign,
  709. 0,
  710. pkey_rsa_verify,
  711. 0,
  712. pkey_rsa_verifyrecover,
  713. 0, 0, 0, 0,
  714. 0,
  715. pkey_rsa_encrypt,
  716. 0,
  717. pkey_rsa_decrypt,
  718. 0, 0,
  719. pkey_rsa_ctrl,
  720. pkey_rsa_ctrl_str
  721. };