rsa_pmeth.c 25 KB

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