1
0

rsa_ameth.c 30 KB

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