2
0

rsa_ameth.c 34 KB

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