rsa_ameth.c 26 KB

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