rsa_pmeth.c 25 KB

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