rsa_pmeth.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "internal/constant_time.h"
  15. #include <stdio.h>
  16. #include "internal/cryptlib.h"
  17. #include <openssl/asn1t.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/rsa.h>
  20. #include <openssl/bn.h>
  21. #include <openssl/evp.h>
  22. #include <openssl/x509v3.h>
  23. #include <openssl/cms.h>
  24. #include "crypto/evp.h"
  25. #include "crypto/rsa.h"
  26. #include "rsa_local.h"
  27. /* RSA pkey context structure */
  28. typedef struct {
  29. /* Key gen parameters */
  30. int nbits;
  31. BIGNUM *pub_exp;
  32. int primes;
  33. /* Keygen callback info */
  34. int gentmp[2];
  35. /* RSA padding mode */
  36. int pad_mode;
  37. /* message digest */
  38. const EVP_MD *md;
  39. /* message digest for MGF1 */
  40. const EVP_MD *mgf1md;
  41. /* PSS salt length */
  42. int saltlen;
  43. /* Minimum salt length or -1 if no PSS parameter restriction */
  44. int min_saltlen;
  45. /* Temp buffer */
  46. unsigned char *tbuf;
  47. /* OAEP label */
  48. unsigned char *oaep_label;
  49. size_t oaep_labellen;
  50. } RSA_PKEY_CTX;
  51. /* True if PSS parameters are restricted */
  52. #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
  53. static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
  54. {
  55. RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));
  56. if (rctx == NULL)
  57. return 0;
  58. rctx->nbits = 2048;
  59. rctx->primes = RSA_DEFAULT_PRIME_NUM;
  60. if (pkey_ctx_is_pss(ctx))
  61. rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
  62. else
  63. rctx->pad_mode = RSA_PKCS1_PADDING;
  64. /* Maximum for sign, auto for verify */
  65. rctx->saltlen = RSA_PSS_SALTLEN_AUTO;
  66. rctx->min_saltlen = -1;
  67. ctx->data = rctx;
  68. ctx->keygen_info = rctx->gentmp;
  69. ctx->keygen_info_count = 2;
  70. return 1;
  71. }
  72. static int pkey_rsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
  73. {
  74. RSA_PKEY_CTX *dctx, *sctx;
  75. if (!pkey_rsa_init(dst))
  76. return 0;
  77. sctx = src->data;
  78. dctx = dst->data;
  79. dctx->nbits = sctx->nbits;
  80. if (sctx->pub_exp) {
  81. dctx->pub_exp = BN_dup(sctx->pub_exp);
  82. if (!dctx->pub_exp)
  83. return 0;
  84. }
  85. dctx->pad_mode = sctx->pad_mode;
  86. dctx->md = sctx->md;
  87. dctx->mgf1md = sctx->mgf1md;
  88. dctx->saltlen = sctx->saltlen;
  89. if (sctx->oaep_label) {
  90. OPENSSL_free(dctx->oaep_label);
  91. dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
  92. if (!dctx->oaep_label)
  93. return 0;
  94. dctx->oaep_labellen = sctx->oaep_labellen;
  95. }
  96. return 1;
  97. }
  98. static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
  99. {
  100. if (ctx->tbuf != NULL)
  101. return 1;
  102. if ((ctx->tbuf = OPENSSL_malloc(RSA_size(pk->pkey->pkey.rsa))) == NULL) {
  103. RSAerr(RSA_F_SETUP_TBUF, ERR_R_MALLOC_FAILURE);
  104. return 0;
  105. }
  106. return 1;
  107. }
  108. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
  109. {
  110. RSA_PKEY_CTX *rctx = ctx->data;
  111. if (rctx) {
  112. BN_free(rctx->pub_exp);
  113. OPENSSL_free(rctx->tbuf);
  114. OPENSSL_free(rctx->oaep_label);
  115. OPENSSL_free(rctx);
  116. }
  117. }
  118. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
  119. size_t *siglen, const unsigned char *tbs,
  120. size_t tbslen)
  121. {
  122. int ret;
  123. RSA_PKEY_CTX *rctx = ctx->data;
  124. RSA *rsa = ctx->pkey->pkey.rsa;
  125. if (rctx->md) {
  126. if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
  127. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
  128. return -1;
  129. }
  130. if (EVP_MD_type(rctx->md) == NID_mdc2) {
  131. unsigned int sltmp;
  132. if (rctx->pad_mode != RSA_PKCS1_PADDING)
  133. return -1;
  134. ret = RSA_sign_ASN1_OCTET_STRING(0,
  135. tbs, tbslen, sig, &sltmp, rsa);
  136. if (ret <= 0)
  137. return ret;
  138. ret = sltmp;
  139. } else if (rctx->pad_mode == RSA_X931_PADDING) {
  140. if ((size_t)RSA_size(rsa) < tbslen + 1) {
  141. RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
  142. return -1;
  143. }
  144. if (!setup_tbuf(rctx, ctx)) {
  145. RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
  146. return -1;
  147. }
  148. memcpy(rctx->tbuf, tbs, tbslen);
  149. rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
  150. ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
  151. sig, rsa, RSA_X931_PADDING);
  152. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  153. unsigned int sltmp;
  154. ret = RSA_sign(EVP_MD_type(rctx->md),
  155. tbs, tbslen, sig, &sltmp, rsa);
  156. if (ret <= 0)
  157. return ret;
  158. ret = sltmp;
  159. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  160. if (!setup_tbuf(rctx, ctx))
  161. return -1;
  162. if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
  163. rctx->tbuf, tbs,
  164. rctx->md, rctx->mgf1md,
  165. rctx->saltlen))
  166. return -1;
  167. ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
  168. sig, rsa, RSA_NO_PADDING);
  169. } else {
  170. return -1;
  171. }
  172. } else {
  173. ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
  174. rctx->pad_mode);
  175. }
  176. if (ret < 0)
  177. return ret;
  178. *siglen = ret;
  179. return 1;
  180. }
  181. static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
  182. unsigned char *rout, size_t *routlen,
  183. const unsigned char *sig, size_t siglen)
  184. {
  185. int ret;
  186. RSA_PKEY_CTX *rctx = ctx->data;
  187. if (rctx->md) {
  188. if (rctx->pad_mode == RSA_X931_PADDING) {
  189. if (!setup_tbuf(rctx, ctx))
  190. return -1;
  191. ret = RSA_public_decrypt(siglen, sig,
  192. rctx->tbuf, ctx->pkey->pkey.rsa,
  193. RSA_X931_PADDING);
  194. if (ret < 1)
  195. return 0;
  196. ret--;
  197. if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
  198. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  199. RSA_R_ALGORITHM_MISMATCH);
  200. return 0;
  201. }
  202. if (ret != EVP_MD_size(rctx->md)) {
  203. RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
  204. RSA_R_INVALID_DIGEST_LENGTH);
  205. return 0;
  206. }
  207. if (rout)
  208. memcpy(rout, rctx->tbuf, ret);
  209. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  210. size_t sltmp;
  211. ret = int_rsa_verify(EVP_MD_type(rctx->md),
  212. NULL, 0, rout, &sltmp,
  213. sig, siglen, ctx->pkey->pkey.rsa);
  214. if (ret <= 0)
  215. return 0;
  216. ret = sltmp;
  217. } else {
  218. return -1;
  219. }
  220. } else {
  221. ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
  222. rctx->pad_mode);
  223. }
  224. if (ret < 0)
  225. return ret;
  226. *routlen = ret;
  227. return 1;
  228. }
  229. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
  230. const unsigned char *sig, size_t siglen,
  231. const unsigned char *tbs, size_t tbslen)
  232. {
  233. RSA_PKEY_CTX *rctx = ctx->data;
  234. RSA *rsa = ctx->pkey->pkey.rsa;
  235. size_t rslen;
  236. if (rctx->md) {
  237. if (rctx->pad_mode == RSA_PKCS1_PADDING)
  238. return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
  239. sig, siglen, rsa);
  240. if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
  241. RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
  242. return -1;
  243. }
  244. if (rctx->pad_mode == RSA_X931_PADDING) {
  245. if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
  246. return 0;
  247. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  248. int ret;
  249. if (!setup_tbuf(rctx, ctx))
  250. return -1;
  251. ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  252. rsa, RSA_NO_PADDING);
  253. if (ret <= 0)
  254. return 0;
  255. ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
  256. rctx->md, rctx->mgf1md,
  257. rctx->tbuf, rctx->saltlen);
  258. if (ret <= 0)
  259. return 0;
  260. return 1;
  261. } else {
  262. return -1;
  263. }
  264. } else {
  265. if (!setup_tbuf(rctx, ctx))
  266. return -1;
  267. rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  268. rsa, rctx->pad_mode);
  269. if (rslen == 0)
  270. return 0;
  271. }
  272. if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
  273. return 0;
  274. return 1;
  275. }
  276. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
  277. unsigned char *out, size_t *outlen,
  278. const unsigned char *in, size_t inlen)
  279. {
  280. int ret;
  281. RSA_PKEY_CTX *rctx = ctx->data;
  282. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  283. int klen = RSA_size(ctx->pkey->pkey.rsa);
  284. if (!setup_tbuf(rctx, ctx))
  285. return -1;
  286. if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
  287. in, inlen,
  288. rctx->oaep_label,
  289. rctx->oaep_labellen,
  290. rctx->md, rctx->mgf1md))
  291. return -1;
  292. ret = RSA_public_encrypt(klen, rctx->tbuf, out,
  293. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  294. } else {
  295. ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  296. rctx->pad_mode);
  297. }
  298. if (ret < 0)
  299. return ret;
  300. *outlen = ret;
  301. return 1;
  302. }
  303. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
  304. unsigned char *out, size_t *outlen,
  305. const unsigned char *in, size_t inlen)
  306. {
  307. int ret;
  308. RSA_PKEY_CTX *rctx = ctx->data;
  309. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  310. if (!setup_tbuf(rctx, ctx))
  311. return -1;
  312. ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
  313. ctx->pkey->pkey.rsa, RSA_NO_PADDING);
  314. if (ret <= 0)
  315. return ret;
  316. ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
  317. ret, ret,
  318. rctx->oaep_label,
  319. rctx->oaep_labellen,
  320. rctx->md, rctx->mgf1md);
  321. } else {
  322. ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
  323. rctx->pad_mode);
  324. }
  325. *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
  326. ret = constant_time_select_int(constant_time_msb(ret), ret, 1);
  327. return ret;
  328. }
  329. static int check_padding_md(const EVP_MD *md, int padding)
  330. {
  331. int mdnid;
  332. if (!md)
  333. return 1;
  334. mdnid = EVP_MD_type(md);
  335. if (padding == RSA_NO_PADDING) {
  336. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
  337. return 0;
  338. }
  339. if (padding == RSA_X931_PADDING) {
  340. if (RSA_X931_hash_id(mdnid) == -1) {
  341. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
  342. return 0;
  343. }
  344. } else {
  345. switch(mdnid) {
  346. /* List of all supported RSA digests */
  347. case NID_sha1:
  348. case NID_sha224:
  349. case NID_sha256:
  350. case NID_sha384:
  351. case NID_sha512:
  352. case NID_sha512_224:
  353. case NID_sha512_256:
  354. case NID_md5:
  355. case NID_md5_sha1:
  356. case NID_md2:
  357. case NID_md4:
  358. case NID_mdc2:
  359. case NID_ripemd160:
  360. case NID_sha3_224:
  361. case NID_sha3_256:
  362. case NID_sha3_384:
  363. case NID_sha3_512:
  364. return 1;
  365. default:
  366. RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
  367. return 0;
  368. }
  369. }
  370. return 1;
  371. }
  372. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  373. {
  374. RSA_PKEY_CTX *rctx = ctx->data;
  375. switch (type) {
  376. case EVP_PKEY_CTRL_RSA_PADDING:
  377. if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
  378. if (!check_padding_md(rctx->md, p1))
  379. return 0;
  380. if (p1 == RSA_PKCS1_PSS_PADDING) {
  381. if (!(ctx->operation &
  382. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
  383. goto bad_pad;
  384. if (!rctx->md)
  385. rctx->md = EVP_sha1();
  386. } else if (pkey_ctx_is_pss(ctx)) {
  387. goto bad_pad;
  388. }
  389. if (p1 == RSA_PKCS1_OAEP_PADDING) {
  390. if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
  391. goto bad_pad;
  392. if (!rctx->md)
  393. rctx->md = EVP_sha1();
  394. }
  395. rctx->pad_mode = p1;
  396. return 1;
  397. }
  398. bad_pad:
  399. RSAerr(RSA_F_PKEY_RSA_CTRL,
  400. RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  401. return -2;
  402. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  403. *(int *)p2 = rctx->pad_mode;
  404. return 1;
  405. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  406. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  407. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  408. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
  409. return -2;
  410. }
  411. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
  412. *(int *)p2 = rctx->saltlen;
  413. } else {
  414. if (p1 < RSA_PSS_SALTLEN_MAX)
  415. return -2;
  416. if (rsa_pss_restricted(rctx)) {
  417. if (p1 == RSA_PSS_SALTLEN_AUTO
  418. && ctx->operation == EVP_PKEY_OP_VERIFY) {
  419. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
  420. return -2;
  421. }
  422. if ((p1 == RSA_PSS_SALTLEN_DIGEST
  423. && rctx->min_saltlen > EVP_MD_size(rctx->md))
  424. || (p1 >= 0 && p1 < rctx->min_saltlen)) {
  425. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_PSS_SALTLEN_TOO_SMALL);
  426. return 0;
  427. }
  428. }
  429. rctx->saltlen = p1;
  430. }
  431. return 1;
  432. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  433. if (p1 < RSA_MIN_MODULUS_BITS) {
  434. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
  435. return -2;
  436. }
  437. rctx->nbits = p1;
  438. return 1;
  439. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  440. if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
  441. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
  442. return -2;
  443. }
  444. BN_free(rctx->pub_exp);
  445. rctx->pub_exp = p2;
  446. return 1;
  447. case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
  448. if (p1 < RSA_DEFAULT_PRIME_NUM || p1 > RSA_MAX_PRIME_NUM) {
  449. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_PRIME_NUM_INVALID);
  450. return -2;
  451. }
  452. rctx->primes = p1;
  453. return 1;
  454. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  455. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  456. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  457. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  458. return -2;
  459. }
  460. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
  461. *(const EVP_MD **)p2 = rctx->md;
  462. else
  463. rctx->md = p2;
  464. return 1;
  465. case EVP_PKEY_CTRL_MD:
  466. if (!check_padding_md(p2, rctx->pad_mode))
  467. return 0;
  468. if (rsa_pss_restricted(rctx)) {
  469. if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
  470. return 1;
  471. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_DIGEST_NOT_ALLOWED);
  472. return 0;
  473. }
  474. rctx->md = p2;
  475. return 1;
  476. case EVP_PKEY_CTRL_GET_MD:
  477. *(const EVP_MD **)p2 = rctx->md;
  478. return 1;
  479. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  480. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  481. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
  482. && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  483. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
  484. return -2;
  485. }
  486. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  487. if (rctx->mgf1md)
  488. *(const EVP_MD **)p2 = rctx->mgf1md;
  489. else
  490. *(const EVP_MD **)p2 = rctx->md;
  491. } else {
  492. if (rsa_pss_restricted(rctx)) {
  493. if (EVP_MD_type(rctx->mgf1md) == EVP_MD_type(p2))
  494. return 1;
  495. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
  496. return 0;
  497. }
  498. rctx->mgf1md = p2;
  499. }
  500. return 1;
  501. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  502. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  503. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  504. return -2;
  505. }
  506. OPENSSL_free(rctx->oaep_label);
  507. if (p2 && p1 > 0) {
  508. rctx->oaep_label = p2;
  509. rctx->oaep_labellen = p1;
  510. } else {
  511. rctx->oaep_label = NULL;
  512. rctx->oaep_labellen = 0;
  513. }
  514. return 1;
  515. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  516. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  517. RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
  518. return -2;
  519. }
  520. *(unsigned char **)p2 = rctx->oaep_label;
  521. return rctx->oaep_labellen;
  522. case EVP_PKEY_CTRL_DIGESTINIT:
  523. case EVP_PKEY_CTRL_PKCS7_SIGN:
  524. #ifndef OPENSSL_NO_CMS
  525. case EVP_PKEY_CTRL_CMS_SIGN:
  526. #endif
  527. return 1;
  528. case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
  529. case EVP_PKEY_CTRL_PKCS7_DECRYPT:
  530. #ifndef OPENSSL_NO_CMS
  531. case EVP_PKEY_CTRL_CMS_DECRYPT:
  532. case EVP_PKEY_CTRL_CMS_ENCRYPT:
  533. #endif
  534. if (!pkey_ctx_is_pss(ctx))
  535. return 1;
  536. /* fall through */
  537. case EVP_PKEY_CTRL_PEER_KEY:
  538. RSAerr(RSA_F_PKEY_RSA_CTRL,
  539. RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  540. return -2;
  541. default:
  542. return -2;
  543. }
  544. }
  545. static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
  546. const char *type, const char *value)
  547. {
  548. if (value == NULL) {
  549. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
  550. return 0;
  551. }
  552. if (strcmp(type, "rsa_padding_mode") == 0) {
  553. int pm;
  554. if (strcmp(value, "pkcs1") == 0) {
  555. pm = RSA_PKCS1_PADDING;
  556. } else if (strcmp(value, "sslv23") == 0) {
  557. pm = RSA_SSLV23_PADDING;
  558. } else if (strcmp(value, "none") == 0) {
  559. pm = RSA_NO_PADDING;
  560. } else if (strcmp(value, "oeap") == 0) {
  561. pm = RSA_PKCS1_OAEP_PADDING;
  562. } else if (strcmp(value, "oaep") == 0) {
  563. pm = RSA_PKCS1_OAEP_PADDING;
  564. } else if (strcmp(value, "x931") == 0) {
  565. pm = RSA_X931_PADDING;
  566. } else if (strcmp(value, "pss") == 0) {
  567. pm = RSA_PKCS1_PSS_PADDING;
  568. } else {
  569. RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
  570. return -2;
  571. }
  572. return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
  573. }
  574. if (strcmp(type, "rsa_pss_saltlen") == 0) {
  575. int saltlen;
  576. if (!strcmp(value, "digest"))
  577. saltlen = RSA_PSS_SALTLEN_DIGEST;
  578. else if (!strcmp(value, "max"))
  579. saltlen = RSA_PSS_SALTLEN_MAX;
  580. else if (!strcmp(value, "auto"))
  581. saltlen = RSA_PSS_SALTLEN_AUTO;
  582. else
  583. saltlen = atoi(value);
  584. return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
  585. }
  586. if (strcmp(type, "rsa_keygen_bits") == 0) {
  587. int nbits = atoi(value);
  588. return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
  589. }
  590. if (strcmp(type, "rsa_keygen_pubexp") == 0) {
  591. int ret;
  592. BIGNUM *pubexp = NULL;
  593. if (!BN_asc2bn(&pubexp, value))
  594. return 0;
  595. ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
  596. if (ret <= 0)
  597. BN_free(pubexp);
  598. return ret;
  599. }
  600. if (strcmp(type, "rsa_keygen_primes") == 0) {
  601. int nprimes = atoi(value);
  602. return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, nprimes);
  603. }
  604. if (strcmp(type, "rsa_mgf1_md") == 0)
  605. return EVP_PKEY_CTX_md(ctx,
  606. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  607. EVP_PKEY_CTRL_RSA_MGF1_MD, value);
  608. if (pkey_ctx_is_pss(ctx)) {
  609. if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
  610. return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
  611. EVP_PKEY_CTRL_RSA_MGF1_MD, value);
  612. if (strcmp(type, "rsa_pss_keygen_md") == 0)
  613. return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
  614. EVP_PKEY_CTRL_MD, value);
  615. if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
  616. int saltlen = atoi(value);
  617. return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
  618. }
  619. }
  620. if (strcmp(type, "rsa_oaep_md") == 0)
  621. return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
  622. EVP_PKEY_CTRL_RSA_OAEP_MD, value);
  623. if (strcmp(type, "rsa_oaep_label") == 0) {
  624. unsigned char *lab;
  625. long lablen;
  626. int ret;
  627. lab = OPENSSL_hexstr2buf(value, &lablen);
  628. if (!lab)
  629. return 0;
  630. ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
  631. if (ret <= 0)
  632. OPENSSL_free(lab);
  633. return ret;
  634. }
  635. return -2;
  636. }
  637. /* Set PSS parameters when generating a key, if necessary */
  638. static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
  639. {
  640. RSA_PKEY_CTX *rctx = ctx->data;
  641. if (!pkey_ctx_is_pss(ctx))
  642. return 1;
  643. /* If all parameters are default values don't set pss */
  644. if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
  645. return 1;
  646. rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
  647. rctx->saltlen == -2 ? 0 : rctx->saltlen);
  648. if (rsa->pss == NULL)
  649. return 0;
  650. return 1;
  651. }
  652. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  653. {
  654. RSA *rsa = NULL;
  655. RSA_PKEY_CTX *rctx = ctx->data;
  656. BN_GENCB *pcb;
  657. int ret;
  658. if (rctx->pub_exp == NULL) {
  659. rctx->pub_exp = BN_new();
  660. if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
  661. return 0;
  662. }
  663. rsa = RSA_new();
  664. if (rsa == NULL)
  665. return 0;
  666. if (ctx->pkey_gencb) {
  667. pcb = BN_GENCB_new();
  668. if (pcb == NULL) {
  669. RSA_free(rsa);
  670. return 0;
  671. }
  672. evp_pkey_set_cb_translate(pcb, ctx);
  673. } else {
  674. pcb = NULL;
  675. }
  676. ret = RSA_generate_multi_prime_key(rsa, rctx->nbits, rctx->primes,
  677. rctx->pub_exp, pcb);
  678. BN_GENCB_free(pcb);
  679. if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
  680. RSA_free(rsa);
  681. return 0;
  682. }
  683. if (ret > 0)
  684. EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
  685. else
  686. RSA_free(rsa);
  687. return ret;
  688. }
  689. static const EVP_PKEY_METHOD rsa_pkey_meth = {
  690. EVP_PKEY_RSA,
  691. EVP_PKEY_FLAG_AUTOARGLEN,
  692. pkey_rsa_init,
  693. pkey_rsa_copy,
  694. pkey_rsa_cleanup,
  695. 0, 0,
  696. 0,
  697. pkey_rsa_keygen,
  698. 0,
  699. pkey_rsa_sign,
  700. 0,
  701. pkey_rsa_verify,
  702. 0,
  703. pkey_rsa_verifyrecover,
  704. 0, 0, 0, 0,
  705. 0,
  706. pkey_rsa_encrypt,
  707. 0,
  708. pkey_rsa_decrypt,
  709. 0, 0,
  710. pkey_rsa_ctrl,
  711. pkey_rsa_ctrl_str
  712. };
  713. const EVP_PKEY_METHOD *rsa_pkey_method(void)
  714. {
  715. return &rsa_pkey_meth;
  716. }
  717. /*
  718. * Called for PSS sign or verify initialisation: checks PSS parameter
  719. * sanity and sets any restrictions on key usage.
  720. */
  721. static int pkey_pss_init(EVP_PKEY_CTX *ctx)
  722. {
  723. RSA *rsa;
  724. RSA_PKEY_CTX *rctx = ctx->data;
  725. const EVP_MD *md;
  726. const EVP_MD *mgf1md;
  727. int min_saltlen, max_saltlen;
  728. /* Should never happen */
  729. if (!pkey_ctx_is_pss(ctx))
  730. return 0;
  731. rsa = ctx->pkey->pkey.rsa;
  732. /* If no restrictions just return */
  733. if (rsa->pss == NULL)
  734. return 1;
  735. /* Get and check parameters */
  736. if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
  737. return 0;
  738. /* See if minimum salt length exceeds maximum possible */
  739. max_saltlen = RSA_size(rsa) - EVP_MD_size(md);
  740. if ((RSA_bits(rsa) & 0x7) == 1)
  741. max_saltlen--;
  742. if (min_saltlen > max_saltlen) {
  743. RSAerr(RSA_F_PKEY_PSS_INIT, RSA_R_INVALID_SALT_LENGTH);
  744. return 0;
  745. }
  746. rctx->min_saltlen = min_saltlen;
  747. /*
  748. * Set PSS restrictions as defaults: we can then block any attempt to
  749. * use invalid values in pkey_rsa_ctrl
  750. */
  751. rctx->md = md;
  752. rctx->mgf1md = mgf1md;
  753. rctx->saltlen = min_saltlen;
  754. return 1;
  755. }
  756. static const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
  757. EVP_PKEY_RSA_PSS,
  758. EVP_PKEY_FLAG_AUTOARGLEN,
  759. pkey_rsa_init,
  760. pkey_rsa_copy,
  761. pkey_rsa_cleanup,
  762. 0, 0,
  763. 0,
  764. pkey_rsa_keygen,
  765. pkey_pss_init,
  766. pkey_rsa_sign,
  767. pkey_pss_init,
  768. pkey_rsa_verify,
  769. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  770. pkey_rsa_ctrl,
  771. pkey_rsa_ctrl_str
  772. };
  773. const EVP_PKEY_METHOD *rsa_pss_pkey_method(void)
  774. {
  775. return &rsa_pss_pkey_meth;
  776. }