rsa_pmeth.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 "internal/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. #include "internal/evp_int.h"
  71. #include "rsa_locl.h"
  72. /* RSA pkey context structure */
  73. typedef struct {
  74. /* Key gen parameters */
  75. int nbits;
  76. BIGNUM *pub_exp;
  77. /* Keygen callback info */
  78. int gentmp[2];
  79. /* RSA padding mode */
  80. int pad_mode;
  81. /* message digest */
  82. const EVP_MD *md;
  83. /* message digest for MGF1 */
  84. const EVP_MD *mgf1md;
  85. /* PSS salt length */
  86. int saltlen;
  87. /* Temp buffer */
  88. unsigned char *tbuf;
  89. /* OAEP label */
  90. unsigned char *oaep_label;
  91. size_t oaep_labellen;
  92. } RSA_PKEY_CTX;
  93. static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
  94. {
  95. RSA_PKEY_CTX *rctx;
  96. rctx = OPENSSL_zalloc(sizeof(*rctx));
  97. if (rctx == NULL)
  98. return 0;
  99. rctx->nbits = 1024;
  100. rctx->pad_mode = RSA_PKCS1_PADDING;
  101. rctx->saltlen = -2;
  102. ctx->data = rctx;
  103. ctx->keygen_info = rctx->gentmp;
  104. ctx->keygen_info_count = 2;
  105. return 1;
  106. }
  107. static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  108. {
  109. RSA_PKEY_CTX *dctx, *sctx;
  110. if (!pkey_rsa_init(dst))
  111. return 0;
  112. sctx = src->data;
  113. dctx = dst->data;
  114. dctx->nbits = sctx->nbits;
  115. if (sctx->pub_exp) {
  116. dctx->pub_exp = BN_dup(sctx->pub_exp);
  117. if (!dctx->pub_exp)
  118. return 0;
  119. }
  120. dctx->pad_mode = sctx->pad_mode;
  121. dctx->md = sctx->md;
  122. dctx->mgf1md = sctx->mgf1md;
  123. if (sctx->oaep_label) {
  124. OPENSSL_free(dctx->oaep_label);
  125. dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
  126. if (!dctx->oaep_label)
  127. return 0;
  128. dctx->oaep_labellen = sctx->oaep_labellen;
  129. }
  130. return 1;
  131. }
  132. static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
  133. {
  134. if (ctx->tbuf)
  135. return 1;
  136. ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
  137. if (ctx->tbuf == NULL)
  138. return 0;
  139. return 1;
  140. }
  141. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
  142. {
  143. RSA_PKEY_CTX *rctx = ctx->data;
  144. if (rctx) {
  145. BN_free(rctx->pub_exp);
  146. OPENSSL_free(rctx->tbuf);
  147. OPENSSL_free(rctx->oaep_label);
  148. OPENSSL_free(rctx);
  149. }
  150. }
  151. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
  152. size_t *siglen, const unsigned char *tbs,
  153. size_t tbslen)
  154. {
  155. int ret;
  156. RSA_PKEY_CTX *rctx = ctx->data;
  157. RSA *rsa = ctx->pkey->pkey.rsa;
  158. if (rctx->md) {
  159. if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
  160. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
  161. return -1;
  162. }
  163. if (EVP_MD_type(rctx->md) == NID_mdc2) {
  164. unsigned int sltmp;
  165. if (rctx->pad_mode != RSA_PKCS1_PADDING)
  166. return -1;
  167. ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
  168. tbs, tbslen, sig, &sltmp, rsa);
  169. if (ret <= 0)
  170. return ret;
  171. ret = sltmp;
  172. } else if (rctx->pad_mode == RSA_X931_PADDING) {
  173. if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
  174. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
  175. return -1;
  176. }
  177. if (!setup_tbuf(rctx, ctx)) {
  178. RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
  179. return -1;
  180. }
  181. memcpy(rctx->tbuf, tbs, tbslen);
  182. rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
  183. ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
  184. sig, rsa, RSA_X931_PADDING);
  185. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  186. unsigned int sltmp;
  187. ret = RSA_sign(EVP_MD_type(rctx->md),
  188. tbs, tbslen, sig, &sltmp, rsa);
  189. if (ret <= 0)
  190. return ret;
  191. ret = sltmp;
  192. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  193. if (!setup_tbuf(rctx, ctx))
  194. return -1;
  195. if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
  196. rctx->tbuf, tbs,
  197. rctx->md, rctx->mgf1md,
  198. rctx->saltlen))
  199. return -1;
  200. ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
  201. sig, rsa, RSA_NO_PADDING);
  202. } else
  203. return -1;
  204. } else
  205. ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
  206. rctx->pad_mode);
  207. if (ret < 0)
  208. return ret;
  209. *siglen = ret;
  210. return 1;
  211. }
  212. static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
  213. unsigned char *rout, size_t *routlen,
  214. const unsigned char *sig, size_t siglen)
  215. {
  216. int ret;
  217. RSA_PKEY_CTX *rctx = ctx->data;
  218. if (rctx->md) {
  219. if (rctx->pad_mode == RSA_X931_PADDING) {
  220. if (!setup_tbuf(rctx, ctx))
  221. return -1;
  222. ret = RSA_public_decrypt(siglen, sig,
  223. rctx->tbuf, ctx->pkey->pkey.rsa,
  224. RSA_X931_PADDING);
  225. if (ret < 1)
  226. return 0;
  227. ret--;
  228. if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
  229. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  230. RSA_R_ALGORITHM_MISMATCH);
  231. return 0;
  232. }
  233. if (ret != EVP_MD_size(rctx->md)) {
  234. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  235. RSA_R_INVALID_DIGEST_LENGTH);
  236. return 0;
  237. }
  238. if (rout)
  239. memcpy(rout, rctx->tbuf, ret);
  240. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  241. size_t sltmp;
  242. ret = int_rsa_verify(EVP_MD_type(rctx->md),
  243. NULL, 0, rout, &sltmp,
  244. sig, siglen, ctx->pkey->pkey.rsa);
  245. if (ret <= 0)
  246. return 0;
  247. ret = sltmp;
  248. } else
  249. return -1;
  250. } else
  251. ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
  252. rctx->pad_mode);
  253. if (ret < 0)
  254. return ret;
  255. *routlen = ret;
  256. return 1;
  257. }
  258. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
  259. const unsigned char *sig, size_t siglen,
  260. const unsigned char *tbs, size_t tbslen)
  261. {
  262. RSA_PKEY_CTX *rctx = ctx->data;
  263. RSA *rsa = ctx->pkey->pkey.rsa;
  264. size_t rslen;
  265. if (rctx->md) {
  266. if (rctx->pad_mode == RSA_PKCS1_PADDING)
  267. return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
  268. sig, siglen, rsa);
  269. if (rctx->pad_mode == RSA_X931_PADDING) {
  270. if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
  271. return 0;
  272. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  273. int ret;
  274. if (!setup_tbuf(rctx, ctx))
  275. return -1;
  276. ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  277. rsa, RSA_NO_PADDING);
  278. if (ret <= 0)
  279. return 0;
  280. ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
  281. rctx->md, rctx->mgf1md,
  282. rctx->tbuf, rctx->saltlen);
  283. if (ret <= 0)
  284. return 0;
  285. return 1;
  286. } else
  287. return -1;
  288. } else {
  289. if (!setup_tbuf(rctx, ctx))
  290. return -1;
  291. rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  292. rsa, rctx->pad_mode);
  293. if (rslen == 0)
  294. return 0;
  295. }
  296. if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
  297. return 0;
  298. return 1;
  299. }
  300. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
  301. unsigned char *out, size_t *outlen,
  302. const unsigned char *in, size_t inlen)
  303. {
  304. int ret;
  305. RSA_PKEY_CTX *rctx = ctx->data;
  306. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  307. int klen = RSA_size(ctx->pkey->pkey.rsa);
  308. if (!setup_tbuf(rctx, ctx))
  309. return -1;
  310. if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
  311. in, inlen,
  312. rctx->oaep_label,
  313. rctx->oaep_labellen,
  314. rctx->md, rctx->mgf1md))
  315. return -1;
  316. ret = RSA_public_encrypt(klen, rctx->tbuf, out,
  317. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  318. } else
  319. ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  320. rctx->pad_mode);
  321. if (ret < 0)
  322. return ret;
  323. *outlen = ret;
  324. return 1;
  325. }
  326. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
  327. unsigned char *out, size_t *outlen,
  328. const unsigned char *in, size_t inlen)
  329. {
  330. int ret;
  331. RSA_PKEY_CTX *rctx = ctx->data;
  332. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  333. int i;
  334. if (!setup_tbuf(rctx, ctx))
  335. return -1;
  336. ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
  337. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  338. if (ret <= 0)
  339. return ret;
  340. for (i = 0; i < ret; i++) {
  341. if (rctx->tbuf[i])
  342. break;
  343. }
  344. ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
  345. ret - i, ret,
  346. rctx->oaep_label,
  347. rctx->oaep_labellen,
  348. rctx->md, rctx->mgf1md);
  349. } else
  350. ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  351. rctx->pad_mode);
  352. if (ret < 0)
  353. return ret;
  354. *outlen = ret;
  355. return 1;
  356. }
  357. static int check_padding_md(const EVP_MD *md, int padding)
  358. {
  359. int mdnid;
  360. if (!md)
  361. return 1;
  362. mdnid = EVP_MD_type(md);
  363. if (padding == RSA_NO_PADDING) {
  364. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
  365. return 0;
  366. }
  367. if (padding == RSA_X931_PADDING) {
  368. if (RSA_X931_hash_id(mdnid) == -1) {
  369. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
  370. return 0;
  371. }
  372. } else {
  373. switch(mdnid) {
  374. /* List of all supported RSA digests */
  375. case NID_sha1:
  376. case NID_sha224:
  377. case NID_sha256:
  378. case NID_sha384:
  379. case NID_sha512:
  380. case NID_md5:
  381. case NID_md5_sha1:
  382. case NID_md2:
  383. case NID_md4:
  384. case NID_mdc2:
  385. case NID_ripemd160:
  386. return 1;
  387. default:
  388. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
  389. return 0;
  390. }
  391. }
  392. return 1;
  393. }
  394. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  395. {
  396. RSA_PKEY_CTX *rctx = ctx->data;
  397. switch (type) {
  398. case EVP_PKEY_CTRL_RSA_PADDING:
  399. if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
  400. if (!check_padding_md(rctx->md, p1))
  401. return 0;
  402. if (p1 == RSA_PKCS1_PSS_PADDING) {
  403. if (!(ctx->operation &
  404. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
  405. goto bad_pad;
  406. if (!rctx->md)
  407. rctx->md = EVP_sha1();
  408. }
  409. if (p1 == RSA_PKCS1_OAEP_PADDING) {
  410. if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
  411. goto bad_pad;
  412. if (!rctx->md)
  413. rctx->md = EVP_sha1();
  414. }
  415. rctx->pad_mode = p1;
  416. return 1;
  417. }
  418. bad_pad:
  419. RSAerr(RSA_F_PKEY_RSA_CTRL,
  420. RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  421. return -2;
  422. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  423. *(int *)p2 = rctx->pad_mode;
  424. return 1;
  425. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  426. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  427. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  428. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
  429. return -2;
  430. }
  431. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
  432. *(int *)p2 = rctx->saltlen;
  433. else {
  434. if (p1 < -2)
  435. return -2;
  436. rctx->saltlen = p1;
  437. }
  438. return 1;
  439. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  440. if (p1 < 512) {
  441. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
  442. return -2;
  443. }
  444. rctx->nbits = p1;
  445. return 1;
  446. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  447. if (!p2)
  448. return -2;
  449. BN_free(rctx->pub_exp);
  450. rctx->pub_exp = p2;
  451. return 1;
  452. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  453. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  454. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  455. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  456. return -2;
  457. }
  458. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
  459. *(const EVP_MD **)p2 = rctx->md;
  460. else
  461. rctx->md = p2;
  462. return 1;
  463. case EVP_PKEY_CTRL_MD:
  464. if (!check_padding_md(p2, rctx->pad_mode))
  465. return 0;
  466. rctx->md = p2;
  467. return 1;
  468. case EVP_PKEY_CTRL_GET_MD:
  469. *(const EVP_MD **)p2 = rctx->md;
  470. return 1;
  471. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  472. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  473. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
  474. && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  475. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
  476. return -2;
  477. }
  478. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  479. if (rctx->mgf1md)
  480. *(const EVP_MD **)p2 = rctx->mgf1md;
  481. else
  482. *(const EVP_MD **)p2 = rctx->md;
  483. } else
  484. rctx->mgf1md = p2;
  485. return 1;
  486. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  487. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  488. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  489. return -2;
  490. }
  491. OPENSSL_free(rctx->oaep_label);
  492. if (p2 && p1 > 0) {
  493. rctx->oaep_label = p2;
  494. rctx->oaep_labellen = p1;
  495. } else {
  496. rctx->oaep_label = NULL;
  497. rctx->oaep_labellen = 0;
  498. }
  499. return 1;
  500. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  501. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  502. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  503. return -2;
  504. }
  505. *(unsigned char **)p2 = rctx->oaep_label;
  506. return rctx->oaep_labellen;
  507. case EVP_PKEY_CTRL_DIGESTINIT:
  508. case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
  509. case EVP_PKEY_CTRL_PKCS7_DECRYPT:
  510. case EVP_PKEY_CTRL_PKCS7_SIGN:
  511. return 1;
  512. #ifndef OPENSSL_NO_CMS
  513. case EVP_PKEY_CTRL_CMS_DECRYPT:
  514. case EVP_PKEY_CTRL_CMS_ENCRYPT:
  515. case EVP_PKEY_CTRL_CMS_SIGN:
  516. return 1;
  517. #endif
  518. case EVP_PKEY_CTRL_PEER_KEY:
  519. RSAerr(RSA_F_PKEY_RSA_CTRL,
  520. RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  521. return -2;
  522. default:
  523. return -2;
  524. }
  525. }
  526. static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
  527. const char *type, const char *value)
  528. {
  529. if (!value) {
  530. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
  531. return 0;
  532. }
  533. if (strcmp(type, "rsa_padding_mode") == 0) {
  534. int pm;
  535. if (strcmp(value, "pkcs1") == 0)
  536. pm = RSA_PKCS1_PADDING;
  537. else if (strcmp(value, "sslv23") == 0)
  538. pm = RSA_SSLV23_PADDING;
  539. else if (strcmp(value, "none") == 0)
  540. pm = RSA_NO_PADDING;
  541. else if (strcmp(value, "oeap") == 0)
  542. pm = RSA_PKCS1_OAEP_PADDING;
  543. else if (strcmp(value, "oaep") == 0)
  544. pm = RSA_PKCS1_OAEP_PADDING;
  545. else if (strcmp(value, "x931") == 0)
  546. pm = RSA_X931_PADDING;
  547. else if (strcmp(value, "pss") == 0)
  548. pm = RSA_PKCS1_PSS_PADDING;
  549. else {
  550. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
  551. return -2;
  552. }
  553. return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
  554. }
  555. if (strcmp(type, "rsa_pss_saltlen") == 0) {
  556. int saltlen;
  557. saltlen = atoi(value);
  558. return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
  559. }
  560. if (strcmp(type, "rsa_keygen_bits") == 0) {
  561. int nbits;
  562. nbits = atoi(value);
  563. return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
  564. }
  565. if (strcmp(type, "rsa_keygen_pubexp") == 0) {
  566. int ret;
  567. BIGNUM *pubexp = NULL;
  568. if (!BN_asc2bn(&pubexp, value))
  569. return 0;
  570. ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
  571. if (ret <= 0)
  572. BN_free(pubexp);
  573. return ret;
  574. }
  575. if (strcmp(type, "rsa_mgf1_md") == 0) {
  576. const EVP_MD *md;
  577. if ((md = EVP_get_digestbyname(value)) == NULL) {
  578. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
  579. return 0;
  580. }
  581. return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
  582. }
  583. if (strcmp(type, "rsa_oaep_md") == 0) {
  584. const EVP_MD *md;
  585. if ((md = EVP_get_digestbyname(value)) == NULL) {
  586. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
  587. return 0;
  588. }
  589. return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
  590. }
  591. if (strcmp(type, "rsa_oaep_label") == 0) {
  592. unsigned char *lab;
  593. long lablen;
  594. int ret;
  595. lab = string_to_hex(value, &lablen);
  596. if (!lab)
  597. return 0;
  598. ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
  599. if (ret <= 0)
  600. OPENSSL_free(lab);
  601. return ret;
  602. }
  603. return -2;
  604. }
  605. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  606. {
  607. RSA *rsa = NULL;
  608. RSA_PKEY_CTX *rctx = ctx->data;
  609. BN_GENCB *pcb;
  610. int ret;
  611. if (rctx->pub_exp == NULL) {
  612. rctx->pub_exp = BN_new();
  613. if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
  614. return 0;
  615. }
  616. rsa = RSA_new();
  617. if (rsa == NULL)
  618. return 0;
  619. if (ctx->pkey_gencb) {
  620. pcb = BN_GENCB_new();
  621. if (pcb == NULL) {
  622. RSA_free(rsa);
  623. return 0;
  624. }
  625. evp_pkey_set_cb_translate(pcb, ctx);
  626. } else
  627. pcb = NULL;
  628. ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
  629. BN_GENCB_free(pcb);
  630. if (ret > 0)
  631. EVP_PKEY_assign_RSA(pkey, rsa);
  632. else
  633. RSA_free(rsa);
  634. return ret;
  635. }
  636. const EVP_PKEY_METHOD rsa_pkey_meth = {
  637. EVP_PKEY_RSA,
  638. EVP_PKEY_FLAG_AUTOARGLEN,
  639. pkey_rsa_init,
  640. pkey_rsa_copy,
  641. pkey_rsa_cleanup,
  642. 0, 0,
  643. 0,
  644. pkey_rsa_keygen,
  645. 0,
  646. pkey_rsa_sign,
  647. 0,
  648. pkey_rsa_verify,
  649. 0,
  650. pkey_rsa_verifyrecover,
  651. 0, 0, 0, 0,
  652. 0,
  653. pkey_rsa_encrypt,
  654. 0,
  655. pkey_rsa_decrypt,
  656. 0, 0,
  657. pkey_rsa_ctrl,
  658. pkey_rsa_ctrl_str
  659. };