rsa_ameth.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /* crypto/rsa/rsa_ameth.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2006.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/asn1t.h>
  62. #include <openssl/x509.h>
  63. #include <openssl/rsa.h>
  64. #include <openssl/bn.h>
  65. #ifndef OPENSSL_NO_CMS
  66. # include <openssl/cms.h>
  67. #endif
  68. #include "asn1_locl.h"
  69. static int rsa_cms_sign(CMS_SignerInfo *si);
  70. static int rsa_cms_verify(CMS_SignerInfo *si);
  71. static int rsa_cms_decrypt(CMS_RecipientInfo *ri);
  72. static int rsa_cms_encrypt(CMS_RecipientInfo *ri);
  73. static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  74. {
  75. unsigned char *penc = NULL;
  76. int penclen;
  77. penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
  78. if (penclen <= 0)
  79. return 0;
  80. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_RSA),
  81. V_ASN1_NULL, NULL, penc, penclen))
  82. return 1;
  83. OPENSSL_free(penc);
  84. return 0;
  85. }
  86. static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  87. {
  88. const unsigned char *p;
  89. int pklen;
  90. RSA *rsa = NULL;
  91. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, NULL, pubkey))
  92. return 0;
  93. if (!(rsa = d2i_RSAPublicKey(NULL, &p, pklen))) {
  94. RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
  95. return 0;
  96. }
  97. EVP_PKEY_assign_RSA(pkey, rsa);
  98. return 1;
  99. }
  100. static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  101. {
  102. if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
  103. || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
  104. return 0;
  105. return 1;
  106. }
  107. static int old_rsa_priv_decode(EVP_PKEY *pkey,
  108. const unsigned char **pder, int derlen)
  109. {
  110. RSA *rsa;
  111. if (!(rsa = d2i_RSAPrivateKey(NULL, pder, derlen))) {
  112. RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  113. return 0;
  114. }
  115. EVP_PKEY_assign_RSA(pkey, rsa);
  116. return 1;
  117. }
  118. static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  119. {
  120. return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
  121. }
  122. static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  123. {
  124. unsigned char *rk = NULL;
  125. int rklen;
  126. rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
  127. if (rklen <= 0) {
  128. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  129. return 0;
  130. }
  131. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_rsaEncryption), 0,
  132. V_ASN1_NULL, NULL, rk, rklen)) {
  133. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. static int rsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
  139. {
  140. const unsigned char *p;
  141. int pklen;
  142. if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
  143. return 0;
  144. return old_rsa_priv_decode(pkey, &p, pklen);
  145. }
  146. static int int_rsa_size(const EVP_PKEY *pkey)
  147. {
  148. return RSA_size(pkey->pkey.rsa);
  149. }
  150. static int rsa_bits(const EVP_PKEY *pkey)
  151. {
  152. return BN_num_bits(pkey->pkey.rsa->n);
  153. }
  154. static void int_rsa_free(EVP_PKEY *pkey)
  155. {
  156. RSA_free(pkey->pkey.rsa);
  157. }
  158. static void update_buflen(const BIGNUM *b, size_t *pbuflen)
  159. {
  160. size_t i;
  161. if (!b)
  162. return;
  163. if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
  164. *pbuflen = i;
  165. }
  166. static int do_rsa_print(BIO *bp, const RSA *x, int off, int priv)
  167. {
  168. char *str;
  169. const char *s;
  170. unsigned char *m = NULL;
  171. int ret = 0, mod_len = 0;
  172. size_t buf_len = 0;
  173. update_buflen(x->n, &buf_len);
  174. update_buflen(x->e, &buf_len);
  175. if (priv) {
  176. update_buflen(x->d, &buf_len);
  177. update_buflen(x->p, &buf_len);
  178. update_buflen(x->q, &buf_len);
  179. update_buflen(x->dmp1, &buf_len);
  180. update_buflen(x->dmq1, &buf_len);
  181. update_buflen(x->iqmp, &buf_len);
  182. }
  183. m = (unsigned char *)OPENSSL_malloc(buf_len + 10);
  184. if (m == NULL) {
  185. RSAerr(RSA_F_DO_RSA_PRINT, ERR_R_MALLOC_FAILURE);
  186. goto err;
  187. }
  188. if (x->n != NULL)
  189. mod_len = BN_num_bits(x->n);
  190. if (!BIO_indent(bp, off, 128))
  191. goto err;
  192. if (priv && x->d) {
  193. if (BIO_printf(bp, "Private-Key: (%d bit)\n", mod_len)
  194. <= 0)
  195. goto err;
  196. str = "modulus:";
  197. s = "publicExponent:";
  198. } else {
  199. if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len)
  200. <= 0)
  201. goto err;
  202. str = "Modulus:";
  203. s = "Exponent:";
  204. }
  205. if (!ASN1_bn_print(bp, str, x->n, m, off))
  206. goto err;
  207. if (!ASN1_bn_print(bp, s, x->e, m, off))
  208. goto err;
  209. if (priv) {
  210. if (!ASN1_bn_print(bp, "privateExponent:", x->d, m, off))
  211. goto err;
  212. if (!ASN1_bn_print(bp, "prime1:", x->p, m, off))
  213. goto err;
  214. if (!ASN1_bn_print(bp, "prime2:", x->q, m, off))
  215. goto err;
  216. if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, m, off))
  217. goto err;
  218. if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, m, off))
  219. goto err;
  220. if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, m, off))
  221. goto err;
  222. }
  223. ret = 1;
  224. err:
  225. if (m != NULL)
  226. OPENSSL_free(m);
  227. return (ret);
  228. }
  229. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  230. ASN1_PCTX *ctx)
  231. {
  232. return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
  233. }
  234. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  235. ASN1_PCTX *ctx)
  236. {
  237. return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
  238. }
  239. /* Given an MGF1 Algorithm ID decode to an Algorithm Identifier */
  240. static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
  241. {
  242. const unsigned char *p;
  243. int plen;
  244. if (alg == NULL || alg->parameter == NULL)
  245. return NULL;
  246. if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
  247. return NULL;
  248. if (alg->parameter->type != V_ASN1_SEQUENCE)
  249. return NULL;
  250. p = alg->parameter->value.sequence->data;
  251. plen = alg->parameter->value.sequence->length;
  252. return d2i_X509_ALGOR(NULL, &p, plen);
  253. }
  254. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg,
  255. X509_ALGOR **pmaskHash)
  256. {
  257. const unsigned char *p;
  258. int plen;
  259. RSA_PSS_PARAMS *pss;
  260. *pmaskHash = NULL;
  261. if (!alg->parameter || alg->parameter->type != V_ASN1_SEQUENCE)
  262. return NULL;
  263. p = alg->parameter->value.sequence->data;
  264. plen = alg->parameter->value.sequence->length;
  265. pss = d2i_RSA_PSS_PARAMS(NULL, &p, plen);
  266. if (!pss)
  267. return NULL;
  268. *pmaskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
  269. return pss;
  270. }
  271. static int rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss,
  272. X509_ALGOR *maskHash, int indent)
  273. {
  274. int rv = 0;
  275. if (!pss) {
  276. if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0)
  277. return 0;
  278. return 1;
  279. }
  280. if (BIO_puts(bp, "\n") <= 0)
  281. goto err;
  282. if (!BIO_indent(bp, indent, 128))
  283. goto err;
  284. if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
  285. goto err;
  286. if (pss->hashAlgorithm) {
  287. if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
  288. goto err;
  289. } else if (BIO_puts(bp, "sha1 (default)") <= 0)
  290. goto err;
  291. if (BIO_puts(bp, "\n") <= 0)
  292. goto err;
  293. if (!BIO_indent(bp, indent, 128))
  294. goto err;
  295. if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
  296. goto err;
  297. if (pss->maskGenAlgorithm) {
  298. if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
  299. goto err;
  300. if (BIO_puts(bp, " with ") <= 0)
  301. goto err;
  302. if (maskHash) {
  303. if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
  304. goto err;
  305. } else if (BIO_puts(bp, "INVALID") <= 0)
  306. goto err;
  307. } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0)
  308. goto err;
  309. BIO_puts(bp, "\n");
  310. if (!BIO_indent(bp, indent, 128))
  311. goto err;
  312. if (BIO_puts(bp, "Salt Length: 0x") <= 0)
  313. goto err;
  314. if (pss->saltLength) {
  315. if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
  316. goto err;
  317. } else if (BIO_puts(bp, "14 (default)") <= 0)
  318. goto err;
  319. BIO_puts(bp, "\n");
  320. if (!BIO_indent(bp, indent, 128))
  321. goto err;
  322. if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
  323. goto err;
  324. if (pss->trailerField) {
  325. if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
  326. goto err;
  327. } else if (BIO_puts(bp, "BC (default)") <= 0)
  328. goto err;
  329. BIO_puts(bp, "\n");
  330. rv = 1;
  331. err:
  332. return rv;
  333. }
  334. static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  335. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  336. {
  337. if (OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss) {
  338. int rv;
  339. RSA_PSS_PARAMS *pss;
  340. X509_ALGOR *maskHash;
  341. pss = rsa_pss_decode(sigalg, &maskHash);
  342. rv = rsa_pss_param_print(bp, pss, maskHash, indent);
  343. if (pss)
  344. RSA_PSS_PARAMS_free(pss);
  345. if (maskHash)
  346. X509_ALGOR_free(maskHash);
  347. if (!rv)
  348. return 0;
  349. } else if (!sig && BIO_puts(bp, "\n") <= 0)
  350. return 0;
  351. if (sig)
  352. return X509_signature_dump(bp, sig, indent);
  353. return 1;
  354. }
  355. static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  356. {
  357. X509_ALGOR *alg = NULL;
  358. switch (op) {
  359. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  360. if (arg1 == 0)
  361. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
  362. break;
  363. case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
  364. if (arg1 == 0)
  365. PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
  366. break;
  367. #ifndef OPENSSL_NO_CMS
  368. case ASN1_PKEY_CTRL_CMS_SIGN:
  369. if (arg1 == 0)
  370. return rsa_cms_sign(arg2);
  371. else if (arg1 == 1)
  372. return rsa_cms_verify(arg2);
  373. break;
  374. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  375. if (arg1 == 0)
  376. return rsa_cms_encrypt(arg2);
  377. else if (arg1 == 1)
  378. return rsa_cms_decrypt(arg2);
  379. break;
  380. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  381. *(int *)arg2 = CMS_RECIPINFO_TRANS;
  382. return 1;
  383. #endif
  384. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  385. *(int *)arg2 = NID_sha256;
  386. return 1;
  387. default:
  388. return -2;
  389. }
  390. if (alg)
  391. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  392. return 1;
  393. }
  394. /* allocate and set algorithm ID from EVP_MD, default SHA1 */
  395. static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
  396. {
  397. if (EVP_MD_type(md) == NID_sha1)
  398. return 1;
  399. *palg = X509_ALGOR_new();
  400. if (!*palg)
  401. return 0;
  402. X509_ALGOR_set_md(*palg, md);
  403. return 1;
  404. }
  405. /* Allocate and set MGF1 algorithm ID from EVP_MD */
  406. static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
  407. {
  408. X509_ALGOR *algtmp = NULL;
  409. ASN1_STRING *stmp = NULL;
  410. *palg = NULL;
  411. if (EVP_MD_type(mgf1md) == NID_sha1)
  412. return 1;
  413. /* need to embed algorithm ID inside another */
  414. if (!rsa_md_to_algor(&algtmp, mgf1md))
  415. goto err;
  416. if (!ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp))
  417. goto err;
  418. *palg = X509_ALGOR_new();
  419. if (!*palg)
  420. goto err;
  421. X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
  422. stmp = NULL;
  423. err:
  424. if (stmp)
  425. ASN1_STRING_free(stmp);
  426. if (algtmp)
  427. X509_ALGOR_free(algtmp);
  428. if (*palg)
  429. return 1;
  430. return 0;
  431. }
  432. /* convert algorithm ID to EVP_MD, default SHA1 */
  433. static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
  434. {
  435. const EVP_MD *md;
  436. if (!alg)
  437. return EVP_sha1();
  438. md = EVP_get_digestbyobj(alg->algorithm);
  439. if (md == NULL)
  440. RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
  441. return md;
  442. }
  443. /* convert MGF1 algorithm ID to EVP_MD, default SHA1 */
  444. static const EVP_MD *rsa_mgf1_to_md(X509_ALGOR *alg, X509_ALGOR *maskHash)
  445. {
  446. const EVP_MD *md;
  447. if (!alg)
  448. return EVP_sha1();
  449. /* Check mask and lookup mask hash algorithm */
  450. if (OBJ_obj2nid(alg->algorithm) != NID_mgf1) {
  451. RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNSUPPORTED_MASK_ALGORITHM);
  452. return NULL;
  453. }
  454. if (!maskHash) {
  455. RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNSUPPORTED_MASK_PARAMETER);
  456. return NULL;
  457. }
  458. md = EVP_get_digestbyobj(maskHash->algorithm);
  459. if (md == NULL) {
  460. RSAerr(RSA_F_RSA_MGF1_TO_MD, RSA_R_UNKNOWN_MASK_DIGEST);
  461. return NULL;
  462. }
  463. return md;
  464. }
  465. /*
  466. * Convert EVP_PKEY_CTX is PSS mode into corresponding algorithm parameter,
  467. * suitable for setting an AlgorithmIdentifier.
  468. */
  469. static ASN1_STRING *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
  470. {
  471. const EVP_MD *sigmd, *mgf1md;
  472. RSA_PSS_PARAMS *pss = NULL;
  473. ASN1_STRING *os = NULL;
  474. EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
  475. int saltlen, rv = 0;
  476. if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
  477. goto err;
  478. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  479. goto err;
  480. if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
  481. goto err;
  482. if (saltlen == -1)
  483. saltlen = EVP_MD_size(sigmd);
  484. else if (saltlen == -2) {
  485. saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
  486. if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0)
  487. saltlen--;
  488. }
  489. pss = RSA_PSS_PARAMS_new();
  490. if (!pss)
  491. goto err;
  492. if (saltlen != 20) {
  493. pss->saltLength = ASN1_INTEGER_new();
  494. if (!pss->saltLength)
  495. goto err;
  496. if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
  497. goto err;
  498. }
  499. if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
  500. goto err;
  501. if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
  502. goto err;
  503. /* Finally create string with pss parameter encoding. */
  504. if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os))
  505. goto err;
  506. rv = 1;
  507. err:
  508. if (pss)
  509. RSA_PSS_PARAMS_free(pss);
  510. if (rv)
  511. return os;
  512. if (os)
  513. ASN1_STRING_free(os);
  514. return NULL;
  515. }
  516. /*
  517. * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
  518. * then the EVP_MD_CTX is setup and initalised. If it is NULL parameters are
  519. * passed to pkctx instead.
  520. */
  521. static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
  522. X509_ALGOR *sigalg, EVP_PKEY *pkey)
  523. {
  524. int rv = -1;
  525. int saltlen;
  526. const EVP_MD *mgf1md = NULL, *md = NULL;
  527. RSA_PSS_PARAMS *pss;
  528. X509_ALGOR *maskHash;
  529. /* Sanity check: make sure it is PSS */
  530. if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) {
  531. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  532. return -1;
  533. }
  534. /* Decode PSS parameters */
  535. pss = rsa_pss_decode(sigalg, &maskHash);
  536. if (pss == NULL) {
  537. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
  538. goto err;
  539. }
  540. mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm, maskHash);
  541. if (!mgf1md)
  542. goto err;
  543. md = rsa_algor_to_md(pss->hashAlgorithm);
  544. if (!md)
  545. goto err;
  546. if (pss->saltLength) {
  547. saltlen = ASN1_INTEGER_get(pss->saltLength);
  548. /*
  549. * Could perform more salt length sanity checks but the main RSA
  550. * routines will trap other invalid values anyway.
  551. */
  552. if (saltlen < 0) {
  553. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_SALT_LENGTH);
  554. goto err;
  555. }
  556. } else
  557. saltlen = 20;
  558. /*
  559. * low-level routines support only trailer field 0xbc (value 1) and
  560. * PKCS#1 says we should reject any other value anyway.
  561. */
  562. if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
  563. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_TRAILER);
  564. goto err;
  565. }
  566. /* We have all parameters now set up context */
  567. if (pkey) {
  568. if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
  569. goto err;
  570. } else {
  571. const EVP_MD *checkmd;
  572. if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
  573. goto err;
  574. if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
  575. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
  576. goto err;
  577. }
  578. }
  579. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
  580. goto err;
  581. if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
  582. goto err;
  583. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  584. goto err;
  585. /* Carry on */
  586. rv = 1;
  587. err:
  588. RSA_PSS_PARAMS_free(pss);
  589. if (maskHash)
  590. X509_ALGOR_free(maskHash);
  591. return rv;
  592. }
  593. static int rsa_cms_verify(CMS_SignerInfo *si)
  594. {
  595. int nid, nid2;
  596. X509_ALGOR *alg;
  597. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  598. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  599. nid = OBJ_obj2nid(alg->algorithm);
  600. if (nid == NID_rsaEncryption)
  601. return 1;
  602. if (nid == NID_rsassaPss)
  603. return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
  604. /* Workaround for some implementation that use a signature OID */
  605. if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
  606. if (nid2 == NID_rsaEncryption)
  607. return 1;
  608. }
  609. return 0;
  610. }
  611. /*
  612. * Customised RSA item verification routine. This is called when a signature
  613. * is encountered requiring special handling. We currently only handle PSS.
  614. */
  615. static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  616. X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
  617. EVP_PKEY *pkey)
  618. {
  619. /* Sanity check: make sure it is PSS */
  620. if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) {
  621. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  622. return -1;
  623. }
  624. if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
  625. /* Carry on */
  626. return 2;
  627. }
  628. return -1;
  629. }
  630. static int rsa_cms_sign(CMS_SignerInfo *si)
  631. {
  632. int pad_mode = RSA_PKCS1_PADDING;
  633. X509_ALGOR *alg;
  634. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  635. ASN1_STRING *os = NULL;
  636. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  637. if (pkctx) {
  638. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  639. return 0;
  640. }
  641. if (pad_mode == RSA_PKCS1_PADDING) {
  642. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  643. return 1;
  644. }
  645. /* We don't support it */
  646. if (pad_mode != RSA_PKCS1_PSS_PADDING)
  647. return 0;
  648. os = rsa_ctx_to_pss(pkctx);
  649. if (!os)
  650. return 0;
  651. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os);
  652. return 1;
  653. }
  654. static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  655. X509_ALGOR *alg1, X509_ALGOR *alg2,
  656. ASN1_BIT_STRING *sig)
  657. {
  658. int pad_mode;
  659. EVP_PKEY_CTX *pkctx = ctx->pctx;
  660. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  661. return 0;
  662. if (pad_mode == RSA_PKCS1_PADDING)
  663. return 2;
  664. if (pad_mode == RSA_PKCS1_PSS_PADDING) {
  665. ASN1_STRING *os1 = NULL;
  666. os1 = rsa_ctx_to_pss(pkctx);
  667. if (!os1)
  668. return 0;
  669. /* Duplicate parameters if we have to */
  670. if (alg2) {
  671. ASN1_STRING *os2 = ASN1_STRING_dup(os1);
  672. if (!os2) {
  673. ASN1_STRING_free(os1);
  674. return 0;
  675. }
  676. X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_rsassaPss),
  677. V_ASN1_SEQUENCE, os2);
  678. }
  679. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_rsassaPss),
  680. V_ASN1_SEQUENCE, os1);
  681. return 3;
  682. }
  683. return 2;
  684. }
  685. static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg,
  686. X509_ALGOR **pmaskHash)
  687. {
  688. const unsigned char *p;
  689. int plen;
  690. RSA_OAEP_PARAMS *pss;
  691. *pmaskHash = NULL;
  692. if (!alg->parameter || alg->parameter->type != V_ASN1_SEQUENCE)
  693. return NULL;
  694. p = alg->parameter->value.sequence->data;
  695. plen = alg->parameter->value.sequence->length;
  696. pss = d2i_RSA_OAEP_PARAMS(NULL, &p, plen);
  697. if (!pss)
  698. return NULL;
  699. *pmaskHash = rsa_mgf1_decode(pss->maskGenFunc);
  700. return pss;
  701. }
  702. static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
  703. {
  704. EVP_PKEY_CTX *pkctx;
  705. X509_ALGOR *cmsalg;
  706. int nid;
  707. int rv = -1;
  708. unsigned char *label = NULL;
  709. int labellen = 0;
  710. const EVP_MD *mgf1md = NULL, *md = NULL;
  711. RSA_OAEP_PARAMS *oaep;
  712. X509_ALGOR *maskHash;
  713. pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  714. if (!pkctx)
  715. return 0;
  716. if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
  717. return -1;
  718. nid = OBJ_obj2nid(cmsalg->algorithm);
  719. if (nid == NID_rsaEncryption)
  720. return 1;
  721. if (nid != NID_rsaesOaep) {
  722. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
  723. return -1;
  724. }
  725. /* Decode OAEP parameters */
  726. oaep = rsa_oaep_decode(cmsalg, &maskHash);
  727. if (oaep == NULL) {
  728. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
  729. goto err;
  730. }
  731. mgf1md = rsa_mgf1_to_md(oaep->maskGenFunc, maskHash);
  732. if (!mgf1md)
  733. goto err;
  734. md = rsa_algor_to_md(oaep->hashFunc);
  735. if (!md)
  736. goto err;
  737. if (oaep->pSourceFunc) {
  738. X509_ALGOR *plab = oaep->pSourceFunc;
  739. if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
  740. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
  741. goto err;
  742. }
  743. if (plab->parameter->type != V_ASN1_OCTET_STRING) {
  744. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
  745. goto err;
  746. }
  747. label = plab->parameter->value.octet_string->data;
  748. /* Stop label being freed when OAEP parameters are freed */
  749. plab->parameter->value.octet_string->data = NULL;
  750. labellen = plab->parameter->value.octet_string->length;
  751. }
  752. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
  753. goto err;
  754. if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
  755. goto err;
  756. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  757. goto err;
  758. if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
  759. goto err;
  760. /* Carry on */
  761. rv = 1;
  762. err:
  763. RSA_OAEP_PARAMS_free(oaep);
  764. if (maskHash)
  765. X509_ALGOR_free(maskHash);
  766. return rv;
  767. }
  768. static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
  769. {
  770. const EVP_MD *md, *mgf1md;
  771. RSA_OAEP_PARAMS *oaep = NULL;
  772. ASN1_STRING *os = NULL;
  773. X509_ALGOR *alg;
  774. EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  775. int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
  776. unsigned char *label;
  777. CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg);
  778. if (pkctx) {
  779. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  780. return 0;
  781. }
  782. if (pad_mode == RSA_PKCS1_PADDING) {
  783. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  784. return 1;
  785. }
  786. /* Not supported */
  787. if (pad_mode != RSA_PKCS1_OAEP_PADDING)
  788. return 0;
  789. if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
  790. goto err;
  791. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  792. goto err;
  793. labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
  794. if (labellen < 0)
  795. goto err;
  796. oaep = RSA_OAEP_PARAMS_new();
  797. if (!oaep)
  798. goto err;
  799. if (!rsa_md_to_algor(&oaep->hashFunc, md))
  800. goto err;
  801. if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
  802. goto err;
  803. if (labellen > 0) {
  804. ASN1_OCTET_STRING *los = ASN1_OCTET_STRING_new();
  805. oaep->pSourceFunc = X509_ALGOR_new();
  806. if (!oaep->pSourceFunc)
  807. goto err;
  808. if (!los)
  809. goto err;
  810. if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
  811. ASN1_OCTET_STRING_free(los);
  812. goto err;
  813. }
  814. X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
  815. V_ASN1_OCTET_STRING, los);
  816. }
  817. /* create string with pss parameter encoding. */
  818. if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
  819. goto err;
  820. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
  821. os = NULL;
  822. rv = 1;
  823. err:
  824. if (oaep)
  825. RSA_OAEP_PARAMS_free(oaep);
  826. if (os)
  827. ASN1_STRING_free(os);
  828. return rv;
  829. }
  830. const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[] = {
  831. {
  832. EVP_PKEY_RSA,
  833. EVP_PKEY_RSA,
  834. ASN1_PKEY_SIGPARAM_NULL,
  835. "RSA",
  836. "OpenSSL RSA method",
  837. rsa_pub_decode,
  838. rsa_pub_encode,
  839. rsa_pub_cmp,
  840. rsa_pub_print,
  841. rsa_priv_decode,
  842. rsa_priv_encode,
  843. rsa_priv_print,
  844. int_rsa_size,
  845. rsa_bits,
  846. 0, 0, 0, 0, 0, 0,
  847. rsa_sig_print,
  848. int_rsa_free,
  849. rsa_pkey_ctrl,
  850. old_rsa_priv_decode,
  851. old_rsa_priv_encode,
  852. rsa_item_verify,
  853. rsa_item_sign},
  854. {
  855. EVP_PKEY_RSA2,
  856. EVP_PKEY_RSA,
  857. ASN1_PKEY_ALIAS}
  858. };