rsa_ameth.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*
  2. * Copyright 2006-2020 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/bn.h>
  14. #include <openssl/cms.h>
  15. #include "crypto/asn1.h"
  16. #include "crypto/evp.h"
  17. #include "rsa_local.h"
  18. #ifndef OPENSSL_NO_CMS
  19. static int rsa_cms_sign(CMS_SignerInfo *si);
  20. static int rsa_cms_verify(CMS_SignerInfo *si);
  21. static int rsa_cms_decrypt(CMS_RecipientInfo *ri);
  22. static int rsa_cms_encrypt(CMS_RecipientInfo *ri);
  23. #endif
  24. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg);
  25. /* Set any parameters associated with pkey */
  26. static int rsa_param_encode(const EVP_PKEY *pkey,
  27. ASN1_STRING **pstr, int *pstrtype)
  28. {
  29. const RSA *rsa = pkey->pkey.rsa;
  30. *pstr = NULL;
  31. /* If RSA it's just NULL type */
  32. if (pkey->ameth->pkey_id != EVP_PKEY_RSA_PSS) {
  33. *pstrtype = V_ASN1_NULL;
  34. return 1;
  35. }
  36. /* If no PSS parameters we omit parameters entirely */
  37. if (rsa->pss == NULL) {
  38. *pstrtype = V_ASN1_UNDEF;
  39. return 1;
  40. }
  41. /* Encode PSS parameters */
  42. if (ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr) == NULL)
  43. return 0;
  44. *pstrtype = V_ASN1_SEQUENCE;
  45. return 1;
  46. }
  47. /* Decode any parameters and set them in RSA structure */
  48. static int rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
  49. {
  50. const ASN1_OBJECT *algoid;
  51. const void *algp;
  52. int algptype;
  53. X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
  54. if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
  55. return 1;
  56. if (algptype == V_ASN1_UNDEF)
  57. return 1;
  58. if (algptype != V_ASN1_SEQUENCE) {
  59. RSAerr(RSA_F_RSA_PARAM_DECODE, RSA_R_INVALID_PSS_PARAMETERS);
  60. return 0;
  61. }
  62. rsa->pss = rsa_pss_decode(alg);
  63. if (rsa->pss == NULL)
  64. return 0;
  65. return 1;
  66. }
  67. static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  68. {
  69. unsigned char *penc = NULL;
  70. int penclen;
  71. ASN1_STRING *str;
  72. int strtype;
  73. if (!rsa_param_encode(pkey, &str, &strtype))
  74. return 0;
  75. penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
  76. if (penclen <= 0)
  77. return 0;
  78. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
  79. strtype, str, penc, penclen))
  80. return 1;
  81. OPENSSL_free(penc);
  82. return 0;
  83. }
  84. static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  85. {
  86. const unsigned char *p;
  87. int pklen;
  88. X509_ALGOR *alg;
  89. RSA *rsa = NULL;
  90. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
  91. return 0;
  92. if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) {
  93. RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
  94. return 0;
  95. }
  96. if (!rsa_param_decode(rsa, alg)) {
  97. RSA_free(rsa);
  98. return 0;
  99. }
  100. if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) {
  101. RSA_free(rsa);
  102. return 0;
  103. }
  104. return 1;
  105. }
  106. static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  107. {
  108. /*
  109. * Don't check the public/private key, this is mostly for smart
  110. * cards.
  111. */
  112. if (((RSA_flags(a->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
  113. || (RSA_flags(b->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) {
  114. return 1;
  115. }
  116. if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
  117. || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
  118. return 0;
  119. return 1;
  120. }
  121. static int old_rsa_priv_decode(EVP_PKEY *pkey,
  122. const unsigned char **pder, int derlen)
  123. {
  124. RSA *rsa;
  125. if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) {
  126. RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  127. return 0;
  128. }
  129. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
  130. return 1;
  131. }
  132. static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  133. {
  134. return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
  135. }
  136. static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  137. {
  138. unsigned char *rk = NULL;
  139. int rklen;
  140. ASN1_STRING *str;
  141. int strtype;
  142. if (!rsa_param_encode(pkey, &str, &strtype))
  143. return 0;
  144. rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
  145. if (rklen <= 0) {
  146. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  147. ASN1_STRING_free(str);
  148. return 0;
  149. }
  150. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
  151. strtype, str, rk, rklen)) {
  152. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  153. ASN1_STRING_free(str);
  154. OPENSSL_clear_free(rk, rklen);
  155. return 0;
  156. }
  157. return 1;
  158. }
  159. static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
  160. {
  161. const unsigned char *p;
  162. RSA *rsa;
  163. int pklen;
  164. const X509_ALGOR *alg;
  165. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8))
  166. return 0;
  167. rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
  168. if (rsa == NULL) {
  169. RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  170. return 0;
  171. }
  172. if (!rsa_param_decode(rsa, alg)) {
  173. RSA_free(rsa);
  174. return 0;
  175. }
  176. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
  177. return 1;
  178. }
  179. static int int_rsa_size(const EVP_PKEY *pkey)
  180. {
  181. return RSA_size(pkey->pkey.rsa);
  182. }
  183. static int rsa_bits(const EVP_PKEY *pkey)
  184. {
  185. return BN_num_bits(pkey->pkey.rsa->n);
  186. }
  187. static int rsa_security_bits(const EVP_PKEY *pkey)
  188. {
  189. return RSA_security_bits(pkey->pkey.rsa);
  190. }
  191. static void int_rsa_free(EVP_PKEY *pkey)
  192. {
  193. RSA_free(pkey->pkey.rsa);
  194. }
  195. static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
  196. {
  197. if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
  198. return NULL;
  199. return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
  200. alg->parameter);
  201. }
  202. static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
  203. int indent)
  204. {
  205. int rv = 0;
  206. X509_ALGOR *maskHash = NULL;
  207. if (!BIO_indent(bp, indent, 128))
  208. goto err;
  209. if (pss_key) {
  210. if (pss == NULL) {
  211. if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
  212. return 0;
  213. return 1;
  214. } else {
  215. if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
  216. return 0;
  217. }
  218. } else if (pss == NULL) {
  219. if (BIO_puts(bp,"(INVALID PSS PARAMETERS)\n") <= 0)
  220. return 0;
  221. return 1;
  222. }
  223. if (BIO_puts(bp, "\n") <= 0)
  224. goto err;
  225. if (pss_key)
  226. indent += 2;
  227. if (!BIO_indent(bp, indent, 128))
  228. goto err;
  229. if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
  230. goto err;
  231. if (pss->hashAlgorithm) {
  232. if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
  233. goto err;
  234. } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
  235. goto err;
  236. }
  237. if (BIO_puts(bp, "\n") <= 0)
  238. goto err;
  239. if (!BIO_indent(bp, indent, 128))
  240. goto err;
  241. if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
  242. goto err;
  243. if (pss->maskGenAlgorithm) {
  244. if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
  245. goto err;
  246. if (BIO_puts(bp, " with ") <= 0)
  247. goto err;
  248. maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
  249. if (maskHash != NULL) {
  250. if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
  251. goto err;
  252. } else if (BIO_puts(bp, "INVALID") <= 0) {
  253. goto err;
  254. }
  255. } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
  256. goto err;
  257. }
  258. BIO_puts(bp, "\n");
  259. if (!BIO_indent(bp, indent, 128))
  260. goto err;
  261. if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
  262. goto err;
  263. if (pss->saltLength) {
  264. if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
  265. goto err;
  266. } else if (BIO_puts(bp, "14 (default)") <= 0) {
  267. goto err;
  268. }
  269. BIO_puts(bp, "\n");
  270. if (!BIO_indent(bp, indent, 128))
  271. goto err;
  272. if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
  273. goto err;
  274. if (pss->trailerField) {
  275. if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
  276. goto err;
  277. } else if (BIO_puts(bp, "BC (default)") <= 0) {
  278. goto err;
  279. }
  280. BIO_puts(bp, "\n");
  281. rv = 1;
  282. err:
  283. X509_ALGOR_free(maskHash);
  284. return rv;
  285. }
  286. static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
  287. {
  288. const RSA *x = pkey->pkey.rsa;
  289. char *str;
  290. const char *s;
  291. int ret = 0, mod_len = 0, ex_primes;
  292. if (x->n != NULL)
  293. mod_len = BN_num_bits(x->n);
  294. ex_primes = sk_RSA_PRIME_INFO_num(x->prime_infos);
  295. if (!BIO_indent(bp, off, 128))
  296. goto err;
  297. if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ? "RSA-PSS" : "RSA") <= 0)
  298. goto err;
  299. if (priv && x->d) {
  300. if (BIO_printf(bp, "Private-Key: (%d bit, %d primes)\n",
  301. mod_len, ex_primes <= 0 ? 2 : ex_primes + 2) <= 0)
  302. goto err;
  303. str = "modulus:";
  304. s = "publicExponent:";
  305. } else {
  306. if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
  307. goto err;
  308. str = "Modulus:";
  309. s = "Exponent:";
  310. }
  311. if (!ASN1_bn_print(bp, str, x->n, NULL, off))
  312. goto err;
  313. if (!ASN1_bn_print(bp, s, x->e, NULL, off))
  314. goto err;
  315. if (priv) {
  316. int i;
  317. if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
  318. goto err;
  319. if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
  320. goto err;
  321. if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
  322. goto err;
  323. if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
  324. goto err;
  325. if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
  326. goto err;
  327. if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
  328. goto err;
  329. for (i = 0; i < sk_RSA_PRIME_INFO_num(x->prime_infos); i++) {
  330. /* print multi-prime info */
  331. BIGNUM *bn = NULL;
  332. RSA_PRIME_INFO *pinfo;
  333. int j;
  334. pinfo = sk_RSA_PRIME_INFO_value(x->prime_infos, i);
  335. for (j = 0; j < 3; j++) {
  336. if (!BIO_indent(bp, off, 128))
  337. goto err;
  338. switch (j) {
  339. case 0:
  340. if (BIO_printf(bp, "prime%d:", i + 3) <= 0)
  341. goto err;
  342. bn = pinfo->r;
  343. break;
  344. case 1:
  345. if (BIO_printf(bp, "exponent%d:", i + 3) <= 0)
  346. goto err;
  347. bn = pinfo->d;
  348. break;
  349. case 2:
  350. if (BIO_printf(bp, "coefficient%d:", i + 3) <= 0)
  351. goto err;
  352. bn = pinfo->t;
  353. break;
  354. default:
  355. break;
  356. }
  357. if (!ASN1_bn_print(bp, "", bn, NULL, off))
  358. goto err;
  359. }
  360. }
  361. }
  362. if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
  363. goto err;
  364. ret = 1;
  365. err:
  366. return ret;
  367. }
  368. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  369. ASN1_PCTX *ctx)
  370. {
  371. return pkey_rsa_print(bp, pkey, indent, 0);
  372. }
  373. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  374. ASN1_PCTX *ctx)
  375. {
  376. return pkey_rsa_print(bp, pkey, indent, 1);
  377. }
  378. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg)
  379. {
  380. RSA_PSS_PARAMS *pss;
  381. pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
  382. alg->parameter);
  383. if (pss == NULL)
  384. return NULL;
  385. if (pss->maskGenAlgorithm != NULL) {
  386. pss->maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
  387. if (pss->maskHash == NULL) {
  388. RSA_PSS_PARAMS_free(pss);
  389. return NULL;
  390. }
  391. }
  392. return pss;
  393. }
  394. static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  395. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  396. {
  397. if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
  398. int rv;
  399. RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
  400. rv = rsa_pss_param_print(bp, 0, pss, indent);
  401. RSA_PSS_PARAMS_free(pss);
  402. if (!rv)
  403. return 0;
  404. } else if (!sig && BIO_puts(bp, "\n") <= 0) {
  405. return 0;
  406. }
  407. if (sig)
  408. return X509_signature_dump(bp, sig, indent);
  409. return 1;
  410. }
  411. static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  412. {
  413. X509_ALGOR *alg = NULL;
  414. const EVP_MD *md;
  415. const EVP_MD *mgf1md;
  416. int min_saltlen;
  417. switch (op) {
  418. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  419. if (arg1 == 0)
  420. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
  421. break;
  422. case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
  423. if (pkey_is_pss(pkey))
  424. return -2;
  425. if (arg1 == 0)
  426. PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
  427. break;
  428. #ifndef OPENSSL_NO_CMS
  429. case ASN1_PKEY_CTRL_CMS_SIGN:
  430. if (arg1 == 0)
  431. return rsa_cms_sign(arg2);
  432. else if (arg1 == 1)
  433. return rsa_cms_verify(arg2);
  434. break;
  435. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  436. if (pkey_is_pss(pkey))
  437. return -2;
  438. if (arg1 == 0)
  439. return rsa_cms_encrypt(arg2);
  440. else if (arg1 == 1)
  441. return rsa_cms_decrypt(arg2);
  442. break;
  443. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  444. if (pkey_is_pss(pkey))
  445. return -2;
  446. *(int *)arg2 = CMS_RECIPINFO_TRANS;
  447. return 1;
  448. #endif
  449. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  450. if (pkey->pkey.rsa->pss != NULL) {
  451. if (!rsa_pss_get_param(pkey->pkey.rsa->pss, &md, &mgf1md,
  452. &min_saltlen)) {
  453. RSAerr(0, ERR_R_INTERNAL_ERROR);
  454. return 0;
  455. }
  456. *(int *)arg2 = EVP_MD_type(md);
  457. /* Return of 2 indicates this MD is mandatory */
  458. return 2;
  459. }
  460. *(int *)arg2 = NID_sha256;
  461. return 1;
  462. default:
  463. return -2;
  464. }
  465. if (alg)
  466. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  467. return 1;
  468. }
  469. /* allocate and set algorithm ID from EVP_MD, default SHA1 */
  470. static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
  471. {
  472. if (md == NULL || EVP_MD_type(md) == NID_sha1)
  473. return 1;
  474. *palg = X509_ALGOR_new();
  475. if (*palg == NULL)
  476. return 0;
  477. X509_ALGOR_set_md(*palg, md);
  478. return 1;
  479. }
  480. /* Allocate and set MGF1 algorithm ID from EVP_MD */
  481. static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
  482. {
  483. X509_ALGOR *algtmp = NULL;
  484. ASN1_STRING *stmp = NULL;
  485. *palg = NULL;
  486. if (mgf1md == NULL || EVP_MD_type(mgf1md) == NID_sha1)
  487. return 1;
  488. /* need to embed algorithm ID inside another */
  489. if (!rsa_md_to_algor(&algtmp, mgf1md))
  490. goto err;
  491. if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
  492. goto err;
  493. *palg = X509_ALGOR_new();
  494. if (*palg == NULL)
  495. goto err;
  496. X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
  497. stmp = NULL;
  498. err:
  499. ASN1_STRING_free(stmp);
  500. X509_ALGOR_free(algtmp);
  501. if (*palg)
  502. return 1;
  503. return 0;
  504. }
  505. /* convert algorithm ID to EVP_MD, default SHA1 */
  506. static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
  507. {
  508. const EVP_MD *md;
  509. if (!alg)
  510. return EVP_sha1();
  511. md = EVP_get_digestbyobj(alg->algorithm);
  512. if (md == NULL)
  513. RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
  514. return md;
  515. }
  516. /*
  517. * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
  518. * suitable for setting an AlgorithmIdentifier.
  519. */
  520. static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
  521. {
  522. const EVP_MD *sigmd, *mgf1md;
  523. EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
  524. int saltlen;
  525. if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
  526. return NULL;
  527. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  528. return NULL;
  529. if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
  530. return NULL;
  531. if (saltlen == -1) {
  532. saltlen = EVP_MD_size(sigmd);
  533. } else if (saltlen == -2 || saltlen == -3) {
  534. saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
  535. if ((EVP_PKEY_bits(pk) & 0x7) == 1)
  536. saltlen--;
  537. if (saltlen < 0)
  538. return NULL;
  539. }
  540. return rsa_pss_params_create(sigmd, mgf1md, saltlen);
  541. }
  542. RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
  543. const EVP_MD *mgf1md, int saltlen)
  544. {
  545. RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
  546. if (pss == NULL)
  547. goto err;
  548. if (saltlen != 20) {
  549. pss->saltLength = ASN1_INTEGER_new();
  550. if (pss->saltLength == NULL)
  551. goto err;
  552. if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
  553. goto err;
  554. }
  555. if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
  556. goto err;
  557. if (mgf1md == NULL)
  558. mgf1md = sigmd;
  559. if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
  560. goto err;
  561. if (!rsa_md_to_algor(&pss->maskHash, mgf1md))
  562. goto err;
  563. return pss;
  564. err:
  565. RSA_PSS_PARAMS_free(pss);
  566. return NULL;
  567. }
  568. static ASN1_STRING *rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
  569. {
  570. RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
  571. ASN1_STRING *os;
  572. if (pss == NULL)
  573. return NULL;
  574. os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
  575. RSA_PSS_PARAMS_free(pss);
  576. return os;
  577. }
  578. /*
  579. * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
  580. * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
  581. * passed to pkctx instead.
  582. */
  583. static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
  584. X509_ALGOR *sigalg, EVP_PKEY *pkey)
  585. {
  586. int rv = -1;
  587. int saltlen;
  588. const EVP_MD *mgf1md = NULL, *md = NULL;
  589. RSA_PSS_PARAMS *pss;
  590. /* Sanity check: make sure it is PSS */
  591. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  592. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  593. return -1;
  594. }
  595. /* Decode PSS parameters */
  596. pss = rsa_pss_decode(sigalg);
  597. if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
  598. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
  599. goto err;
  600. }
  601. /* We have all parameters now set up context */
  602. if (pkey) {
  603. if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
  604. goto err;
  605. } else {
  606. const EVP_MD *checkmd;
  607. if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
  608. goto err;
  609. if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
  610. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
  611. goto err;
  612. }
  613. }
  614. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
  615. goto err;
  616. if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
  617. goto err;
  618. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  619. goto err;
  620. /* Carry on */
  621. rv = 1;
  622. err:
  623. RSA_PSS_PARAMS_free(pss);
  624. return rv;
  625. }
  626. int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
  627. const EVP_MD **pmgf1md, int *psaltlen)
  628. {
  629. if (pss == NULL)
  630. return 0;
  631. *pmd = rsa_algor_to_md(pss->hashAlgorithm);
  632. if (*pmd == NULL)
  633. return 0;
  634. *pmgf1md = rsa_algor_to_md(pss->maskHash);
  635. if (*pmgf1md == NULL)
  636. return 0;
  637. if (pss->saltLength) {
  638. *psaltlen = ASN1_INTEGER_get(pss->saltLength);
  639. if (*psaltlen < 0) {
  640. RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_SALT_LENGTH);
  641. return 0;
  642. }
  643. } else {
  644. *psaltlen = 20;
  645. }
  646. /*
  647. * low-level routines support only trailer field 0xbc (value 1) and
  648. * PKCS#1 says we should reject any other value anyway.
  649. */
  650. if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
  651. RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_TRAILER);
  652. return 0;
  653. }
  654. return 1;
  655. }
  656. #ifndef OPENSSL_NO_CMS
  657. static int rsa_cms_verify(CMS_SignerInfo *si)
  658. {
  659. int nid, nid2;
  660. X509_ALGOR *alg;
  661. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  662. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  663. nid = OBJ_obj2nid(alg->algorithm);
  664. if (nid == EVP_PKEY_RSA_PSS)
  665. return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
  666. /* Only PSS allowed for PSS keys */
  667. if (pkey_ctx_is_pss(pkctx)) {
  668. RSAerr(RSA_F_RSA_CMS_VERIFY, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  669. return 0;
  670. }
  671. if (nid == NID_rsaEncryption)
  672. return 1;
  673. /* Workaround for some implementation that use a signature OID */
  674. if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
  675. if (nid2 == NID_rsaEncryption)
  676. return 1;
  677. }
  678. return 0;
  679. }
  680. #endif
  681. /*
  682. * Customised RSA item verification routine. This is called when a signature
  683. * is encountered requiring special handling. We currently only handle PSS.
  684. */
  685. static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  686. X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
  687. EVP_PKEY *pkey)
  688. {
  689. /* Sanity check: make sure it is PSS */
  690. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  691. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  692. return -1;
  693. }
  694. if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
  695. /* Carry on */
  696. return 2;
  697. }
  698. return -1;
  699. }
  700. #ifndef OPENSSL_NO_CMS
  701. static int rsa_cms_sign(CMS_SignerInfo *si)
  702. {
  703. int pad_mode = RSA_PKCS1_PADDING;
  704. X509_ALGOR *alg;
  705. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  706. ASN1_STRING *os = NULL;
  707. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  708. if (pkctx) {
  709. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  710. return 0;
  711. }
  712. if (pad_mode == RSA_PKCS1_PADDING) {
  713. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  714. return 1;
  715. }
  716. /* We don't support it */
  717. if (pad_mode != RSA_PKCS1_PSS_PADDING)
  718. return 0;
  719. os = rsa_ctx_to_pss_string(pkctx);
  720. if (!os)
  721. return 0;
  722. X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
  723. return 1;
  724. }
  725. #endif
  726. static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  727. X509_ALGOR *alg1, X509_ALGOR *alg2,
  728. ASN1_BIT_STRING *sig)
  729. {
  730. int pad_mode;
  731. EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
  732. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  733. return 0;
  734. if (pad_mode == RSA_PKCS1_PADDING)
  735. return 2;
  736. if (pad_mode == RSA_PKCS1_PSS_PADDING) {
  737. ASN1_STRING *os1 = NULL;
  738. os1 = rsa_ctx_to_pss_string(pkctx);
  739. if (!os1)
  740. return 0;
  741. /* Duplicate parameters if we have to */
  742. if (alg2) {
  743. ASN1_STRING *os2 = ASN1_STRING_dup(os1);
  744. if (!os2) {
  745. ASN1_STRING_free(os1);
  746. return 0;
  747. }
  748. X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  749. V_ASN1_SEQUENCE, os2);
  750. }
  751. X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  752. V_ASN1_SEQUENCE, os1);
  753. return 3;
  754. }
  755. return 2;
  756. }
  757. static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
  758. const ASN1_STRING *sig)
  759. {
  760. int rv = 0;
  761. int mdnid, saltlen;
  762. uint32_t flags;
  763. const EVP_MD *mgf1md = NULL, *md = NULL;
  764. RSA_PSS_PARAMS *pss;
  765. /* Sanity check: make sure it is PSS */
  766. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
  767. return 0;
  768. /* Decode PSS parameters */
  769. pss = rsa_pss_decode(sigalg);
  770. if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
  771. goto err;
  772. mdnid = EVP_MD_type(md);
  773. /*
  774. * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
  775. * match and salt length must equal digest size
  776. */
  777. if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512)
  778. && mdnid == EVP_MD_type(mgf1md) && saltlen == EVP_MD_size(md))
  779. flags = X509_SIG_INFO_TLS;
  780. else
  781. flags = 0;
  782. /* Note: security bits half number of digest bits */
  783. X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, EVP_MD_size(md) * 4,
  784. flags);
  785. rv = 1;
  786. err:
  787. RSA_PSS_PARAMS_free(pss);
  788. return rv;
  789. }
  790. #ifndef OPENSSL_NO_CMS
  791. static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
  792. {
  793. RSA_OAEP_PARAMS *oaep;
  794. oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
  795. alg->parameter);
  796. if (oaep == NULL)
  797. return NULL;
  798. if (oaep->maskGenFunc != NULL) {
  799. oaep->maskHash = rsa_mgf1_decode(oaep->maskGenFunc);
  800. if (oaep->maskHash == NULL) {
  801. RSA_OAEP_PARAMS_free(oaep);
  802. return NULL;
  803. }
  804. }
  805. return oaep;
  806. }
  807. static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
  808. {
  809. EVP_PKEY_CTX *pkctx;
  810. X509_ALGOR *cmsalg;
  811. int nid;
  812. int rv = -1;
  813. unsigned char *label = NULL;
  814. int labellen = 0;
  815. const EVP_MD *mgf1md = NULL, *md = NULL;
  816. RSA_OAEP_PARAMS *oaep;
  817. pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  818. if (pkctx == NULL)
  819. return 0;
  820. if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
  821. return -1;
  822. nid = OBJ_obj2nid(cmsalg->algorithm);
  823. if (nid == NID_rsaEncryption)
  824. return 1;
  825. if (nid != NID_rsaesOaep) {
  826. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
  827. return -1;
  828. }
  829. /* Decode OAEP parameters */
  830. oaep = rsa_oaep_decode(cmsalg);
  831. if (oaep == NULL) {
  832. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
  833. goto err;
  834. }
  835. mgf1md = rsa_algor_to_md(oaep->maskHash);
  836. if (mgf1md == NULL)
  837. goto err;
  838. md = rsa_algor_to_md(oaep->hashFunc);
  839. if (md == NULL)
  840. goto err;
  841. if (oaep->pSourceFunc != NULL) {
  842. X509_ALGOR *plab = oaep->pSourceFunc;
  843. if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
  844. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
  845. goto err;
  846. }
  847. if (plab->parameter->type != V_ASN1_OCTET_STRING) {
  848. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
  849. goto err;
  850. }
  851. label = plab->parameter->value.octet_string->data;
  852. /* Stop label being freed when OAEP parameters are freed */
  853. plab->parameter->value.octet_string->data = NULL;
  854. labellen = plab->parameter->value.octet_string->length;
  855. }
  856. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
  857. goto err;
  858. if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
  859. goto err;
  860. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  861. goto err;
  862. if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
  863. goto err;
  864. /* Carry on */
  865. rv = 1;
  866. err:
  867. RSA_OAEP_PARAMS_free(oaep);
  868. return rv;
  869. }
  870. static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
  871. {
  872. const EVP_MD *md, *mgf1md;
  873. RSA_OAEP_PARAMS *oaep = NULL;
  874. ASN1_STRING *os = NULL;
  875. X509_ALGOR *alg;
  876. EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  877. int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
  878. unsigned char *label;
  879. if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
  880. return 0;
  881. if (pkctx) {
  882. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  883. return 0;
  884. }
  885. if (pad_mode == RSA_PKCS1_PADDING) {
  886. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  887. return 1;
  888. }
  889. /* Not supported */
  890. if (pad_mode != RSA_PKCS1_OAEP_PADDING)
  891. return 0;
  892. if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
  893. goto err;
  894. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  895. goto err;
  896. labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
  897. if (labellen < 0)
  898. goto err;
  899. oaep = RSA_OAEP_PARAMS_new();
  900. if (oaep == NULL)
  901. goto err;
  902. if (!rsa_md_to_algor(&oaep->hashFunc, md))
  903. goto err;
  904. if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
  905. goto err;
  906. if (labellen > 0) {
  907. ASN1_OCTET_STRING *los;
  908. oaep->pSourceFunc = X509_ALGOR_new();
  909. if (oaep->pSourceFunc == NULL)
  910. goto err;
  911. los = ASN1_OCTET_STRING_new();
  912. if (los == NULL)
  913. goto err;
  914. if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
  915. ASN1_OCTET_STRING_free(los);
  916. goto err;
  917. }
  918. X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
  919. V_ASN1_OCTET_STRING, los);
  920. }
  921. /* create string with pss parameter encoding. */
  922. if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
  923. goto err;
  924. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
  925. os = NULL;
  926. rv = 1;
  927. err:
  928. RSA_OAEP_PARAMS_free(oaep);
  929. ASN1_STRING_free(os);
  930. return rv;
  931. }
  932. #endif
  933. static int rsa_pkey_check(const EVP_PKEY *pkey)
  934. {
  935. return RSA_check_key_ex(pkey->pkey.rsa, NULL);
  936. }
  937. const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
  938. {
  939. EVP_PKEY_RSA,
  940. EVP_PKEY_RSA,
  941. ASN1_PKEY_SIGPARAM_NULL,
  942. "RSA",
  943. "OpenSSL RSA method",
  944. rsa_pub_decode,
  945. rsa_pub_encode,
  946. rsa_pub_cmp,
  947. rsa_pub_print,
  948. rsa_priv_decode,
  949. rsa_priv_encode,
  950. rsa_priv_print,
  951. int_rsa_size,
  952. rsa_bits,
  953. rsa_security_bits,
  954. 0, 0, 0, 0, 0, 0,
  955. rsa_sig_print,
  956. int_rsa_free,
  957. rsa_pkey_ctrl,
  958. old_rsa_priv_decode,
  959. old_rsa_priv_encode,
  960. rsa_item_verify,
  961. rsa_item_sign,
  962. rsa_sig_info_set,
  963. rsa_pkey_check
  964. },
  965. {
  966. EVP_PKEY_RSA2,
  967. EVP_PKEY_RSA,
  968. ASN1_PKEY_ALIAS}
  969. };
  970. const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
  971. EVP_PKEY_RSA_PSS,
  972. EVP_PKEY_RSA_PSS,
  973. ASN1_PKEY_SIGPARAM_NULL,
  974. "RSA-PSS",
  975. "OpenSSL RSA-PSS method",
  976. rsa_pub_decode,
  977. rsa_pub_encode,
  978. rsa_pub_cmp,
  979. rsa_pub_print,
  980. rsa_priv_decode,
  981. rsa_priv_encode,
  982. rsa_priv_print,
  983. int_rsa_size,
  984. rsa_bits,
  985. rsa_security_bits,
  986. 0, 0, 0, 0, 0, 0,
  987. rsa_sig_print,
  988. int_rsa_free,
  989. rsa_pkey_ctrl,
  990. 0, 0,
  991. rsa_item_verify,
  992. rsa_item_sign,
  993. 0,
  994. rsa_pkey_check
  995. };