rsa_sig.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /*
  2. * Copyright 2019-2022 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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/crypto.h>
  16. #include <openssl/core_dispatch.h>
  17. #include <openssl/core_names.h>
  18. #include <openssl/err.h>
  19. #include <openssl/rsa.h>
  20. #include <openssl/params.h>
  21. #include <openssl/evp.h>
  22. #include <openssl/proverr.h>
  23. #include "internal/cryptlib.h"
  24. #include "internal/nelem.h"
  25. #include "internal/sizes.h"
  26. #include "crypto/rsa.h"
  27. #include "prov/providercommon.h"
  28. #include "prov/implementations.h"
  29. #include "prov/provider_ctx.h"
  30. #include "prov/der_rsa.h"
  31. #include "prov/securitycheck.h"
  32. #define RSA_DEFAULT_DIGEST_NAME OSSL_DIGEST_NAME_SHA1
  33. static OSSL_FUNC_signature_newctx_fn rsa_newctx;
  34. static OSSL_FUNC_signature_sign_init_fn rsa_sign_init;
  35. static OSSL_FUNC_signature_verify_init_fn rsa_verify_init;
  36. static OSSL_FUNC_signature_verify_recover_init_fn rsa_verify_recover_init;
  37. static OSSL_FUNC_signature_sign_fn rsa_sign;
  38. static OSSL_FUNC_signature_verify_fn rsa_verify;
  39. static OSSL_FUNC_signature_verify_recover_fn rsa_verify_recover;
  40. static OSSL_FUNC_signature_digest_sign_init_fn rsa_digest_sign_init;
  41. static OSSL_FUNC_signature_digest_sign_update_fn rsa_digest_signverify_update;
  42. static OSSL_FUNC_signature_digest_sign_final_fn rsa_digest_sign_final;
  43. static OSSL_FUNC_signature_digest_verify_init_fn rsa_digest_verify_init;
  44. static OSSL_FUNC_signature_digest_verify_update_fn rsa_digest_signverify_update;
  45. static OSSL_FUNC_signature_digest_verify_final_fn rsa_digest_verify_final;
  46. static OSSL_FUNC_signature_freectx_fn rsa_freectx;
  47. static OSSL_FUNC_signature_dupctx_fn rsa_dupctx;
  48. static OSSL_FUNC_signature_get_ctx_params_fn rsa_get_ctx_params;
  49. static OSSL_FUNC_signature_gettable_ctx_params_fn rsa_gettable_ctx_params;
  50. static OSSL_FUNC_signature_set_ctx_params_fn rsa_set_ctx_params;
  51. static OSSL_FUNC_signature_settable_ctx_params_fn rsa_settable_ctx_params;
  52. static OSSL_FUNC_signature_get_ctx_md_params_fn rsa_get_ctx_md_params;
  53. static OSSL_FUNC_signature_gettable_ctx_md_params_fn rsa_gettable_ctx_md_params;
  54. static OSSL_FUNC_signature_set_ctx_md_params_fn rsa_set_ctx_md_params;
  55. static OSSL_FUNC_signature_settable_ctx_md_params_fn rsa_settable_ctx_md_params;
  56. static OSSL_ITEM padding_item[] = {
  57. { RSA_PKCS1_PADDING, OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
  58. { RSA_NO_PADDING, OSSL_PKEY_RSA_PAD_MODE_NONE },
  59. { RSA_X931_PADDING, OSSL_PKEY_RSA_PAD_MODE_X931 },
  60. { RSA_PKCS1_PSS_PADDING, OSSL_PKEY_RSA_PAD_MODE_PSS },
  61. { 0, NULL }
  62. };
  63. /*
  64. * What's passed as an actual key is defined by the KEYMGMT interface.
  65. * We happen to know that our KEYMGMT simply passes RSA structures, so
  66. * we use that here too.
  67. */
  68. typedef struct {
  69. OSSL_LIB_CTX *libctx;
  70. char *propq;
  71. RSA *rsa;
  72. int operation;
  73. /*
  74. * Flag to determine if the hash function can be changed (1) or not (0)
  75. * Because it's dangerous to change during a DigestSign or DigestVerify
  76. * operation, this flag is cleared by their Init function, and set again
  77. * by their Final function.
  78. */
  79. unsigned int flag_allow_md : 1;
  80. unsigned int mgf1_md_set : 1;
  81. /* main digest */
  82. EVP_MD *md;
  83. EVP_MD_CTX *mdctx;
  84. int mdnid;
  85. char mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
  86. /* RSA padding mode */
  87. int pad_mode;
  88. /* message digest for MGF1 */
  89. EVP_MD *mgf1_md;
  90. int mgf1_mdnid;
  91. char mgf1_mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
  92. /* PSS salt length */
  93. int saltlen;
  94. /* Minimum salt length or -1 if no PSS parameter restriction */
  95. int min_saltlen;
  96. /* Temp buffer */
  97. unsigned char *tbuf;
  98. } PROV_RSA_CTX;
  99. /* True if PSS parameters are restricted */
  100. #define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
  101. static size_t rsa_get_md_size(const PROV_RSA_CTX *prsactx)
  102. {
  103. if (prsactx->md != NULL)
  104. return EVP_MD_get_size(prsactx->md);
  105. return 0;
  106. }
  107. static int rsa_check_padding(const PROV_RSA_CTX *prsactx,
  108. const char *mdname, const char *mgf1_mdname,
  109. int mdnid)
  110. {
  111. switch (prsactx->pad_mode) {
  112. case RSA_NO_PADDING:
  113. if (mdname != NULL || mdnid != NID_undef) {
  114. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
  115. return 0;
  116. }
  117. break;
  118. case RSA_X931_PADDING:
  119. if (RSA_X931_hash_id(mdnid) == -1) {
  120. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_X931_DIGEST);
  121. return 0;
  122. }
  123. break;
  124. case RSA_PKCS1_PSS_PADDING:
  125. if (rsa_pss_restricted(prsactx))
  126. if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname))
  127. || (mgf1_mdname != NULL
  128. && !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) {
  129. ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
  130. return 0;
  131. }
  132. break;
  133. default:
  134. break;
  135. }
  136. return 1;
  137. }
  138. static int rsa_check_parameters(PROV_RSA_CTX *prsactx, int min_saltlen)
  139. {
  140. if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  141. int max_saltlen;
  142. /* See if minimum salt length exceeds maximum possible */
  143. max_saltlen = RSA_size(prsactx->rsa) - EVP_MD_get_size(prsactx->md);
  144. if ((RSA_bits(prsactx->rsa) & 0x7) == 1)
  145. max_saltlen--;
  146. if (min_saltlen < 0 || min_saltlen > max_saltlen) {
  147. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
  148. return 0;
  149. }
  150. prsactx->min_saltlen = min_saltlen;
  151. }
  152. return 1;
  153. }
  154. static void *rsa_newctx(void *provctx, const char *propq)
  155. {
  156. PROV_RSA_CTX *prsactx = NULL;
  157. char *propq_copy = NULL;
  158. if (!ossl_prov_is_running())
  159. return NULL;
  160. if ((prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX))) == NULL
  161. || (propq != NULL
  162. && (propq_copy = OPENSSL_strdup(propq)) == NULL)) {
  163. OPENSSL_free(prsactx);
  164. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  165. return NULL;
  166. }
  167. prsactx->libctx = PROV_LIBCTX_OF(provctx);
  168. prsactx->flag_allow_md = 1;
  169. prsactx->propq = propq_copy;
  170. /* Maximum for sign, auto for verify */
  171. prsactx->saltlen = RSA_PSS_SALTLEN_AUTO;
  172. prsactx->min_saltlen = -1;
  173. return prsactx;
  174. }
  175. static int rsa_pss_compute_saltlen(PROV_RSA_CTX *ctx)
  176. {
  177. int saltlen = ctx->saltlen;
  178. if (saltlen == RSA_PSS_SALTLEN_DIGEST) {
  179. saltlen = EVP_MD_get_size(ctx->md);
  180. } else if (saltlen == RSA_PSS_SALTLEN_AUTO || saltlen == RSA_PSS_SALTLEN_MAX) {
  181. saltlen = RSA_size(ctx->rsa) - EVP_MD_get_size(ctx->md) - 2;
  182. if ((RSA_bits(ctx->rsa) & 0x7) == 1)
  183. saltlen--;
  184. }
  185. if (saltlen < 0) {
  186. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  187. return -1;
  188. } else if (saltlen < ctx->min_saltlen) {
  189. ERR_raise_data(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL,
  190. "minimum salt length: %d, actual salt length: %d",
  191. ctx->min_saltlen, saltlen);
  192. return -1;
  193. }
  194. return saltlen;
  195. }
  196. static unsigned char *rsa_generate_signature_aid(PROV_RSA_CTX *ctx,
  197. unsigned char *aid_buf,
  198. size_t buf_len,
  199. size_t *aid_len)
  200. {
  201. WPACKET pkt;
  202. unsigned char *aid = NULL;
  203. int saltlen;
  204. RSA_PSS_PARAMS_30 pss_params;
  205. int ret;
  206. if (!WPACKET_init_der(&pkt, aid_buf, buf_len)) {
  207. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  208. return NULL;
  209. }
  210. switch (ctx->pad_mode) {
  211. case RSA_PKCS1_PADDING:
  212. ret = ossl_DER_w_algorithmIdentifier_MDWithRSAEncryption(&pkt, -1,
  213. ctx->mdnid);
  214. if (ret > 0) {
  215. break;
  216. } else if (ret == 0) {
  217. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  218. goto cleanup;
  219. }
  220. ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
  221. "Algorithm ID generation - md NID: %d",
  222. ctx->mdnid);
  223. goto cleanup;
  224. case RSA_PKCS1_PSS_PADDING:
  225. saltlen = rsa_pss_compute_saltlen(ctx);
  226. if (saltlen < 0)
  227. goto cleanup;
  228. if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
  229. || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, ctx->mdnid)
  230. || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
  231. ctx->mgf1_mdnid)
  232. || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
  233. || !ossl_DER_w_algorithmIdentifier_RSA_PSS(&pkt, -1,
  234. RSA_FLAG_TYPE_RSASSAPSS,
  235. &pss_params)) {
  236. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  237. goto cleanup;
  238. }
  239. break;
  240. default:
  241. ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
  242. "Algorithm ID generation - pad mode: %d",
  243. ctx->pad_mode);
  244. goto cleanup;
  245. }
  246. if (WPACKET_finish(&pkt)) {
  247. WPACKET_get_total_written(&pkt, aid_len);
  248. aid = WPACKET_get_curr(&pkt);
  249. }
  250. cleanup:
  251. WPACKET_cleanup(&pkt);
  252. return aid;
  253. }
  254. static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname,
  255. const char *mdprops)
  256. {
  257. if (mdprops == NULL)
  258. mdprops = ctx->propq;
  259. if (mdname != NULL) {
  260. EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
  261. int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
  262. int md_nid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md,
  263. sha1_allowed);
  264. size_t mdname_len = strlen(mdname);
  265. if (md == NULL
  266. || md_nid <= 0
  267. || !rsa_check_padding(ctx, mdname, NULL, md_nid)
  268. || mdname_len >= sizeof(ctx->mdname)) {
  269. if (md == NULL)
  270. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  271. "%s could not be fetched", mdname);
  272. if (md_nid <= 0)
  273. ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
  274. "digest=%s", mdname);
  275. if (mdname_len >= sizeof(ctx->mdname))
  276. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  277. "%s exceeds name buffer length", mdname);
  278. EVP_MD_free(md);
  279. return 0;
  280. }
  281. if (!ctx->flag_allow_md) {
  282. if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) {
  283. ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
  284. "digest %s != %s", mdname, ctx->mdname);
  285. EVP_MD_free(md);
  286. return 0;
  287. }
  288. EVP_MD_free(md);
  289. return 1;
  290. }
  291. if (!ctx->mgf1_md_set) {
  292. if (!EVP_MD_up_ref(md)) {
  293. EVP_MD_free(md);
  294. return 0;
  295. }
  296. EVP_MD_free(ctx->mgf1_md);
  297. ctx->mgf1_md = md;
  298. ctx->mgf1_mdnid = md_nid;
  299. OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
  300. }
  301. EVP_MD_CTX_free(ctx->mdctx);
  302. EVP_MD_free(ctx->md);
  303. ctx->mdctx = NULL;
  304. ctx->md = md;
  305. ctx->mdnid = md_nid;
  306. OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
  307. }
  308. return 1;
  309. }
  310. static int rsa_setup_mgf1_md(PROV_RSA_CTX *ctx, const char *mdname,
  311. const char *mdprops)
  312. {
  313. size_t len;
  314. EVP_MD *md = NULL;
  315. int mdnid;
  316. if (mdprops == NULL)
  317. mdprops = ctx->propq;
  318. if ((md = EVP_MD_fetch(ctx->libctx, mdname, mdprops)) == NULL) {
  319. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  320. "%s could not be fetched", mdname);
  321. return 0;
  322. }
  323. /* The default for mgf1 is SHA1 - so allow SHA1 */
  324. if ((mdnid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md, 1)) <= 0
  325. || !rsa_check_padding(ctx, NULL, mdname, mdnid)) {
  326. if (mdnid <= 0)
  327. ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
  328. "digest=%s", mdname);
  329. EVP_MD_free(md);
  330. return 0;
  331. }
  332. len = OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
  333. if (len >= sizeof(ctx->mgf1_mdname)) {
  334. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  335. "%s exceeds name buffer length", mdname);
  336. EVP_MD_free(md);
  337. return 0;
  338. }
  339. EVP_MD_free(ctx->mgf1_md);
  340. ctx->mgf1_md = md;
  341. ctx->mgf1_mdnid = mdnid;
  342. ctx->mgf1_md_set = 1;
  343. return 1;
  344. }
  345. static int rsa_signverify_init(void *vprsactx, void *vrsa,
  346. const OSSL_PARAM params[], int operation)
  347. {
  348. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  349. if (!ossl_prov_is_running() || prsactx == NULL)
  350. return 0;
  351. if (vrsa == NULL && prsactx->rsa == NULL) {
  352. ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
  353. return 0;
  354. }
  355. if (vrsa != NULL) {
  356. if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation))
  357. return 0;
  358. if (!RSA_up_ref(vrsa))
  359. return 0;
  360. RSA_free(prsactx->rsa);
  361. prsactx->rsa = vrsa;
  362. }
  363. prsactx->operation = operation;
  364. /* Maximum for sign, auto for verify */
  365. prsactx->saltlen = RSA_PSS_SALTLEN_AUTO;
  366. prsactx->min_saltlen = -1;
  367. switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
  368. case RSA_FLAG_TYPE_RSA:
  369. prsactx->pad_mode = RSA_PKCS1_PADDING;
  370. break;
  371. case RSA_FLAG_TYPE_RSASSAPSS:
  372. prsactx->pad_mode = RSA_PKCS1_PSS_PADDING;
  373. {
  374. const RSA_PSS_PARAMS_30 *pss =
  375. ossl_rsa_get0_pss_params_30(prsactx->rsa);
  376. if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
  377. int md_nid = ossl_rsa_pss_params_30_hashalg(pss);
  378. int mgf1md_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
  379. int min_saltlen = ossl_rsa_pss_params_30_saltlen(pss);
  380. const char *mdname, *mgf1mdname;
  381. size_t len;
  382. mdname = ossl_rsa_oaeppss_nid2name(md_nid);
  383. mgf1mdname = ossl_rsa_oaeppss_nid2name(mgf1md_nid);
  384. if (mdname == NULL) {
  385. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  386. "PSS restrictions lack hash algorithm");
  387. return 0;
  388. }
  389. if (mgf1mdname == NULL) {
  390. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  391. "PSS restrictions lack MGF1 hash algorithm");
  392. return 0;
  393. }
  394. len = OPENSSL_strlcpy(prsactx->mdname, mdname,
  395. sizeof(prsactx->mdname));
  396. if (len >= sizeof(prsactx->mdname)) {
  397. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  398. "hash algorithm name too long");
  399. return 0;
  400. }
  401. len = OPENSSL_strlcpy(prsactx->mgf1_mdname, mgf1mdname,
  402. sizeof(prsactx->mgf1_mdname));
  403. if (len >= sizeof(prsactx->mgf1_mdname)) {
  404. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  405. "MGF1 hash algorithm name too long");
  406. return 0;
  407. }
  408. prsactx->saltlen = min_saltlen;
  409. /* call rsa_setup_mgf1_md before rsa_setup_md to avoid duplication */
  410. if (!rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq)
  411. || !rsa_setup_md(prsactx, mdname, prsactx->propq)
  412. || !rsa_check_parameters(prsactx, min_saltlen))
  413. return 0;
  414. }
  415. }
  416. break;
  417. default:
  418. ERR_raise(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  419. return 0;
  420. }
  421. if (!rsa_set_ctx_params(prsactx, params))
  422. return 0;
  423. return 1;
  424. }
  425. static int setup_tbuf(PROV_RSA_CTX *ctx)
  426. {
  427. if (ctx->tbuf != NULL)
  428. return 1;
  429. if ((ctx->tbuf = OPENSSL_malloc(RSA_size(ctx->rsa))) == NULL) {
  430. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  431. return 0;
  432. }
  433. return 1;
  434. }
  435. static void clean_tbuf(PROV_RSA_CTX *ctx)
  436. {
  437. if (ctx->tbuf != NULL)
  438. OPENSSL_cleanse(ctx->tbuf, RSA_size(ctx->rsa));
  439. }
  440. static void free_tbuf(PROV_RSA_CTX *ctx)
  441. {
  442. clean_tbuf(ctx);
  443. OPENSSL_free(ctx->tbuf);
  444. ctx->tbuf = NULL;
  445. }
  446. static int rsa_sign_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[])
  447. {
  448. if (!ossl_prov_is_running())
  449. return 0;
  450. return rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_SIGN);
  451. }
  452. static int rsa_sign(void *vprsactx, unsigned char *sig, size_t *siglen,
  453. size_t sigsize, const unsigned char *tbs, size_t tbslen)
  454. {
  455. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  456. int ret;
  457. size_t rsasize = RSA_size(prsactx->rsa);
  458. size_t mdsize = rsa_get_md_size(prsactx);
  459. if (!ossl_prov_is_running())
  460. return 0;
  461. if (sig == NULL) {
  462. *siglen = rsasize;
  463. return 1;
  464. }
  465. if (sigsize < rsasize) {
  466. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE,
  467. "is %zu, should be at least %zu", sigsize, rsasize);
  468. return 0;
  469. }
  470. if (mdsize != 0) {
  471. if (tbslen != mdsize) {
  472. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
  473. return 0;
  474. }
  475. #ifndef FIPS_MODULE
  476. if (EVP_MD_is_a(prsactx->md, OSSL_DIGEST_NAME_MDC2)) {
  477. unsigned int sltmp;
  478. if (prsactx->pad_mode != RSA_PKCS1_PADDING) {
  479. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
  480. "only PKCS#1 padding supported with MDC2");
  481. return 0;
  482. }
  483. ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, tbslen, sig, &sltmp,
  484. prsactx->rsa);
  485. if (ret <= 0) {
  486. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  487. return 0;
  488. }
  489. ret = sltmp;
  490. goto end;
  491. }
  492. #endif
  493. switch (prsactx->pad_mode) {
  494. case RSA_X931_PADDING:
  495. if ((size_t)RSA_size(prsactx->rsa) < tbslen + 1) {
  496. ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL,
  497. "RSA key size = %d, expected minimum = %d",
  498. RSA_size(prsactx->rsa), tbslen + 1);
  499. return 0;
  500. }
  501. if (!setup_tbuf(prsactx)) {
  502. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  503. return 0;
  504. }
  505. memcpy(prsactx->tbuf, tbs, tbslen);
  506. prsactx->tbuf[tbslen] = RSA_X931_hash_id(prsactx->mdnid);
  507. ret = RSA_private_encrypt(tbslen + 1, prsactx->tbuf,
  508. sig, prsactx->rsa, RSA_X931_PADDING);
  509. clean_tbuf(prsactx);
  510. break;
  511. case RSA_PKCS1_PADDING:
  512. {
  513. unsigned int sltmp;
  514. ret = RSA_sign(prsactx->mdnid, tbs, tbslen, sig, &sltmp,
  515. prsactx->rsa);
  516. if (ret <= 0) {
  517. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  518. return 0;
  519. }
  520. ret = sltmp;
  521. }
  522. break;
  523. case RSA_PKCS1_PSS_PADDING:
  524. /* Check PSS restrictions */
  525. if (rsa_pss_restricted(prsactx)) {
  526. switch (prsactx->saltlen) {
  527. case RSA_PSS_SALTLEN_DIGEST:
  528. if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
  529. ERR_raise_data(ERR_LIB_PROV,
  530. PROV_R_PSS_SALTLEN_TOO_SMALL,
  531. "minimum salt length set to %d, "
  532. "but the digest only gives %d",
  533. prsactx->min_saltlen,
  534. EVP_MD_get_size(prsactx->md));
  535. return 0;
  536. }
  537. /* FALLTHRU */
  538. default:
  539. if (prsactx->saltlen >= 0
  540. && prsactx->saltlen < prsactx->min_saltlen) {
  541. ERR_raise_data(ERR_LIB_PROV,
  542. PROV_R_PSS_SALTLEN_TOO_SMALL,
  543. "minimum salt length set to %d, but the"
  544. "actual salt length is only set to %d",
  545. prsactx->min_saltlen,
  546. prsactx->saltlen);
  547. return 0;
  548. }
  549. break;
  550. }
  551. }
  552. if (!setup_tbuf(prsactx))
  553. return 0;
  554. if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx->rsa,
  555. prsactx->tbuf, tbs,
  556. prsactx->md, prsactx->mgf1_md,
  557. prsactx->saltlen)) {
  558. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  559. return 0;
  560. }
  561. ret = RSA_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf,
  562. sig, prsactx->rsa, RSA_NO_PADDING);
  563. clean_tbuf(prsactx);
  564. break;
  565. default:
  566. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
  567. "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
  568. return 0;
  569. }
  570. } else {
  571. ret = RSA_private_encrypt(tbslen, tbs, sig, prsactx->rsa,
  572. prsactx->pad_mode);
  573. }
  574. #ifndef FIPS_MODULE
  575. end:
  576. #endif
  577. if (ret <= 0) {
  578. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  579. return 0;
  580. }
  581. *siglen = ret;
  582. return 1;
  583. }
  584. static int rsa_verify_recover_init(void *vprsactx, void *vrsa,
  585. const OSSL_PARAM params[])
  586. {
  587. if (!ossl_prov_is_running())
  588. return 0;
  589. return rsa_signverify_init(vprsactx, vrsa, params,
  590. EVP_PKEY_OP_VERIFYRECOVER);
  591. }
  592. static int rsa_verify_recover(void *vprsactx,
  593. unsigned char *rout,
  594. size_t *routlen,
  595. size_t routsize,
  596. const unsigned char *sig,
  597. size_t siglen)
  598. {
  599. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  600. int ret;
  601. if (!ossl_prov_is_running())
  602. return 0;
  603. if (rout == NULL) {
  604. *routlen = RSA_size(prsactx->rsa);
  605. return 1;
  606. }
  607. if (prsactx->md != NULL) {
  608. switch (prsactx->pad_mode) {
  609. case RSA_X931_PADDING:
  610. if (!setup_tbuf(prsactx))
  611. return 0;
  612. ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
  613. RSA_X931_PADDING);
  614. if (ret < 1) {
  615. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  616. return 0;
  617. }
  618. ret--;
  619. if (prsactx->tbuf[ret] != RSA_X931_hash_id(prsactx->mdnid)) {
  620. ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
  621. return 0;
  622. }
  623. if (ret != EVP_MD_get_size(prsactx->md)) {
  624. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
  625. "Should be %d, but got %d",
  626. EVP_MD_get_size(prsactx->md), ret);
  627. return 0;
  628. }
  629. *routlen = ret;
  630. if (rout != prsactx->tbuf) {
  631. if (routsize < (size_t)ret) {
  632. ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
  633. "buffer size is %d, should be %d",
  634. routsize, ret);
  635. return 0;
  636. }
  637. memcpy(rout, prsactx->tbuf, ret);
  638. }
  639. break;
  640. case RSA_PKCS1_PADDING:
  641. {
  642. size_t sltmp;
  643. ret = ossl_rsa_verify(prsactx->mdnid, NULL, 0, rout, &sltmp,
  644. sig, siglen, prsactx->rsa);
  645. if (ret <= 0) {
  646. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  647. return 0;
  648. }
  649. ret = sltmp;
  650. }
  651. break;
  652. default:
  653. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
  654. "Only X.931 or PKCS#1 v1.5 padding allowed");
  655. return 0;
  656. }
  657. } else {
  658. ret = RSA_public_decrypt(siglen, sig, rout, prsactx->rsa,
  659. prsactx->pad_mode);
  660. if (ret < 0) {
  661. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  662. return 0;
  663. }
  664. }
  665. *routlen = ret;
  666. return 1;
  667. }
  668. static int rsa_verify_init(void *vprsactx, void *vrsa,
  669. const OSSL_PARAM params[])
  670. {
  671. if (!ossl_prov_is_running())
  672. return 0;
  673. return rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_VERIFY);
  674. }
  675. static int rsa_verify(void *vprsactx, const unsigned char *sig, size_t siglen,
  676. const unsigned char *tbs, size_t tbslen)
  677. {
  678. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  679. size_t rslen;
  680. if (!ossl_prov_is_running())
  681. return 0;
  682. if (prsactx->md != NULL) {
  683. switch (prsactx->pad_mode) {
  684. case RSA_PKCS1_PADDING:
  685. if (!RSA_verify(prsactx->mdnid, tbs, tbslen, sig, siglen,
  686. prsactx->rsa)) {
  687. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  688. return 0;
  689. }
  690. return 1;
  691. case RSA_X931_PADDING:
  692. if (!setup_tbuf(prsactx))
  693. return 0;
  694. if (rsa_verify_recover(prsactx, prsactx->tbuf, &rslen, 0,
  695. sig, siglen) <= 0)
  696. return 0;
  697. break;
  698. case RSA_PKCS1_PSS_PADDING:
  699. {
  700. int ret;
  701. size_t mdsize;
  702. /*
  703. * We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
  704. * call
  705. */
  706. mdsize = rsa_get_md_size(prsactx);
  707. if (tbslen != mdsize) {
  708. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
  709. "Should be %d, but got %d",
  710. mdsize, tbslen);
  711. return 0;
  712. }
  713. if (!setup_tbuf(prsactx))
  714. return 0;
  715. ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf,
  716. prsactx->rsa, RSA_NO_PADDING);
  717. if (ret <= 0) {
  718. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  719. return 0;
  720. }
  721. ret = RSA_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs,
  722. prsactx->md, prsactx->mgf1_md,
  723. prsactx->tbuf,
  724. prsactx->saltlen);
  725. if (ret <= 0) {
  726. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  727. return 0;
  728. }
  729. return 1;
  730. }
  731. default:
  732. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
  733. "Only X.931, PKCS#1 v1.5 or PSS padding allowed");
  734. return 0;
  735. }
  736. } else {
  737. if (!setup_tbuf(prsactx))
  738. return 0;
  739. rslen = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
  740. prsactx->pad_mode);
  741. if (rslen == 0) {
  742. ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
  743. return 0;
  744. }
  745. }
  746. if ((rslen != tbslen) || memcmp(tbs, prsactx->tbuf, rslen))
  747. return 0;
  748. return 1;
  749. }
  750. static int rsa_digest_signverify_init(void *vprsactx, const char *mdname,
  751. void *vrsa, const OSSL_PARAM params[],
  752. int operation)
  753. {
  754. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  755. if (!ossl_prov_is_running())
  756. return 0;
  757. if (!rsa_signverify_init(vprsactx, vrsa, params, operation))
  758. return 0;
  759. if (mdname != NULL
  760. /* was rsa_setup_md already called in rsa_signverify_init()? */
  761. && (mdname[0] == '\0' || OPENSSL_strcasecmp(prsactx->mdname, mdname) != 0)
  762. && !rsa_setup_md(prsactx, mdname, prsactx->propq))
  763. return 0;
  764. prsactx->flag_allow_md = 0;
  765. if (prsactx->mdctx == NULL) {
  766. prsactx->mdctx = EVP_MD_CTX_new();
  767. if (prsactx->mdctx == NULL)
  768. goto error;
  769. }
  770. if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
  771. goto error;
  772. return 1;
  773. error:
  774. EVP_MD_CTX_free(prsactx->mdctx);
  775. prsactx->mdctx = NULL;
  776. return 0;
  777. }
  778. static int rsa_digest_signverify_update(void *vprsactx,
  779. const unsigned char *data,
  780. size_t datalen)
  781. {
  782. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  783. if (prsactx == NULL || prsactx->mdctx == NULL)
  784. return 0;
  785. return EVP_DigestUpdate(prsactx->mdctx, data, datalen);
  786. }
  787. static int rsa_digest_sign_init(void *vprsactx, const char *mdname,
  788. void *vrsa, const OSSL_PARAM params[])
  789. {
  790. if (!ossl_prov_is_running())
  791. return 0;
  792. return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
  793. params, EVP_PKEY_OP_SIGN);
  794. }
  795. static int rsa_digest_sign_final(void *vprsactx, unsigned char *sig,
  796. size_t *siglen, size_t sigsize)
  797. {
  798. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  799. unsigned char digest[EVP_MAX_MD_SIZE];
  800. unsigned int dlen = 0;
  801. if (!ossl_prov_is_running() || prsactx == NULL)
  802. return 0;
  803. prsactx->flag_allow_md = 1;
  804. if (prsactx->mdctx == NULL)
  805. return 0;
  806. /*
  807. * If sig is NULL then we're just finding out the sig size. Other fields
  808. * are ignored. Defer to rsa_sign.
  809. */
  810. if (sig != NULL) {
  811. /*
  812. * The digests used here are all known (see rsa_get_md_nid()), so they
  813. * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
  814. */
  815. if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
  816. return 0;
  817. }
  818. return rsa_sign(vprsactx, sig, siglen, sigsize, digest, (size_t)dlen);
  819. }
  820. static int rsa_digest_verify_init(void *vprsactx, const char *mdname,
  821. void *vrsa, const OSSL_PARAM params[])
  822. {
  823. if (!ossl_prov_is_running())
  824. return 0;
  825. return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
  826. params, EVP_PKEY_OP_VERIFY);
  827. }
  828. int rsa_digest_verify_final(void *vprsactx, const unsigned char *sig,
  829. size_t siglen)
  830. {
  831. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  832. unsigned char digest[EVP_MAX_MD_SIZE];
  833. unsigned int dlen = 0;
  834. if (!ossl_prov_is_running())
  835. return 0;
  836. if (prsactx == NULL)
  837. return 0;
  838. prsactx->flag_allow_md = 1;
  839. if (prsactx->mdctx == NULL)
  840. return 0;
  841. /*
  842. * The digests used here are all known (see rsa_get_md_nid()), so they
  843. * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
  844. */
  845. if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
  846. return 0;
  847. return rsa_verify(vprsactx, sig, siglen, digest, (size_t)dlen);
  848. }
  849. static void rsa_freectx(void *vprsactx)
  850. {
  851. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  852. if (prsactx == NULL)
  853. return;
  854. EVP_MD_CTX_free(prsactx->mdctx);
  855. EVP_MD_free(prsactx->md);
  856. EVP_MD_free(prsactx->mgf1_md);
  857. OPENSSL_free(prsactx->propq);
  858. free_tbuf(prsactx);
  859. RSA_free(prsactx->rsa);
  860. OPENSSL_clear_free(prsactx, sizeof(*prsactx));
  861. }
  862. static void *rsa_dupctx(void *vprsactx)
  863. {
  864. PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
  865. PROV_RSA_CTX *dstctx;
  866. if (!ossl_prov_is_running())
  867. return NULL;
  868. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  869. if (dstctx == NULL) {
  870. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  871. return NULL;
  872. }
  873. *dstctx = *srcctx;
  874. dstctx->rsa = NULL;
  875. dstctx->md = NULL;
  876. dstctx->mdctx = NULL;
  877. dstctx->tbuf = NULL;
  878. dstctx->propq = NULL;
  879. if (srcctx->rsa != NULL && !RSA_up_ref(srcctx->rsa))
  880. goto err;
  881. dstctx->rsa = srcctx->rsa;
  882. if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
  883. goto err;
  884. dstctx->md = srcctx->md;
  885. if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md))
  886. goto err;
  887. dstctx->mgf1_md = srcctx->mgf1_md;
  888. if (srcctx->mdctx != NULL) {
  889. dstctx->mdctx = EVP_MD_CTX_new();
  890. if (dstctx->mdctx == NULL
  891. || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
  892. goto err;
  893. }
  894. if (srcctx->propq != NULL) {
  895. dstctx->propq = OPENSSL_strdup(srcctx->propq);
  896. if (dstctx->propq == NULL)
  897. goto err;
  898. }
  899. return dstctx;
  900. err:
  901. rsa_freectx(dstctx);
  902. return NULL;
  903. }
  904. static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
  905. {
  906. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  907. OSSL_PARAM *p;
  908. if (prsactx == NULL)
  909. return 0;
  910. p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
  911. if (p != NULL) {
  912. /* The Algorithm Identifier of the combined signature algorithm */
  913. unsigned char aid_buf[128];
  914. unsigned char *aid;
  915. size_t aid_len;
  916. aid = rsa_generate_signature_aid(prsactx, aid_buf,
  917. sizeof(aid_buf), &aid_len);
  918. if (aid == NULL || !OSSL_PARAM_set_octet_string(p, aid, aid_len))
  919. return 0;
  920. }
  921. p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
  922. if (p != NULL)
  923. switch (p->data_type) {
  924. case OSSL_PARAM_INTEGER:
  925. if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
  926. return 0;
  927. break;
  928. case OSSL_PARAM_UTF8_STRING:
  929. {
  930. int i;
  931. const char *word = NULL;
  932. for (i = 0; padding_item[i].id != 0; i++) {
  933. if (prsactx->pad_mode == (int)padding_item[i].id) {
  934. word = padding_item[i].ptr;
  935. break;
  936. }
  937. }
  938. if (word != NULL) {
  939. if (!OSSL_PARAM_set_utf8_string(p, word))
  940. return 0;
  941. } else {
  942. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  943. }
  944. }
  945. break;
  946. default:
  947. return 0;
  948. }
  949. p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
  950. if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mdname))
  951. return 0;
  952. p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
  953. if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mgf1_mdname))
  954. return 0;
  955. p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
  956. if (p != NULL) {
  957. if (p->data_type == OSSL_PARAM_INTEGER) {
  958. if (!OSSL_PARAM_set_int(p, prsactx->saltlen))
  959. return 0;
  960. } else if (p->data_type == OSSL_PARAM_UTF8_STRING) {
  961. const char *value = NULL;
  962. switch (prsactx->saltlen) {
  963. case RSA_PSS_SALTLEN_DIGEST:
  964. value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST;
  965. break;
  966. case RSA_PSS_SALTLEN_MAX:
  967. value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX;
  968. break;
  969. case RSA_PSS_SALTLEN_AUTO:
  970. value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO;
  971. break;
  972. default:
  973. {
  974. int len = BIO_snprintf(p->data, p->data_size, "%d",
  975. prsactx->saltlen);
  976. if (len <= 0)
  977. return 0;
  978. p->return_size = len;
  979. break;
  980. }
  981. }
  982. if (value != NULL
  983. && !OSSL_PARAM_set_utf8_string(p, value))
  984. return 0;
  985. }
  986. }
  987. return 1;
  988. }
  989. static const OSSL_PARAM known_gettable_ctx_params[] = {
  990. OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
  991. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
  992. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
  993. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
  994. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
  995. OSSL_PARAM_END
  996. };
  997. static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
  998. ossl_unused void *provctx)
  999. {
  1000. return known_gettable_ctx_params;
  1001. }
  1002. static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
  1003. {
  1004. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  1005. const OSSL_PARAM *p;
  1006. int pad_mode;
  1007. int saltlen;
  1008. char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = NULL;
  1009. char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = NULL;
  1010. char mgf1mdname[OSSL_MAX_NAME_SIZE] = "", *pmgf1mdname = NULL;
  1011. char mgf1mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmgf1mdprops = NULL;
  1012. if (prsactx == NULL)
  1013. return 0;
  1014. if (params == NULL)
  1015. return 1;
  1016. pad_mode = prsactx->pad_mode;
  1017. saltlen = prsactx->saltlen;
  1018. p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
  1019. if (p != NULL) {
  1020. const OSSL_PARAM *propsp =
  1021. OSSL_PARAM_locate_const(params,
  1022. OSSL_SIGNATURE_PARAM_PROPERTIES);
  1023. pmdname = mdname;
  1024. if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
  1025. return 0;
  1026. if (propsp != NULL) {
  1027. pmdprops = mdprops;
  1028. if (!OSSL_PARAM_get_utf8_string(propsp,
  1029. &pmdprops, sizeof(mdprops)))
  1030. return 0;
  1031. }
  1032. }
  1033. p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
  1034. if (p != NULL) {
  1035. const char *err_extra_text = NULL;
  1036. switch (p->data_type) {
  1037. case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
  1038. if (!OSSL_PARAM_get_int(p, &pad_mode))
  1039. return 0;
  1040. break;
  1041. case OSSL_PARAM_UTF8_STRING:
  1042. {
  1043. int i;
  1044. if (p->data == NULL)
  1045. return 0;
  1046. for (i = 0; padding_item[i].id != 0; i++) {
  1047. if (strcmp(p->data, padding_item[i].ptr) == 0) {
  1048. pad_mode = padding_item[i].id;
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. break;
  1054. default:
  1055. return 0;
  1056. }
  1057. switch (pad_mode) {
  1058. case RSA_PKCS1_OAEP_PADDING:
  1059. /*
  1060. * OAEP padding is for asymmetric cipher only so is not compatible
  1061. * with signature use.
  1062. */
  1063. err_extra_text = "OAEP padding not allowed for signing / verifying";
  1064. goto bad_pad;
  1065. case RSA_PKCS1_PSS_PADDING:
  1066. if ((prsactx->operation
  1067. & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)) == 0) {
  1068. err_extra_text =
  1069. "PSS padding only allowed for sign and verify operations";
  1070. goto bad_pad;
  1071. }
  1072. break;
  1073. case RSA_PKCS1_PADDING:
  1074. err_extra_text = "PKCS#1 padding not allowed with RSA-PSS";
  1075. goto cont;
  1076. case RSA_NO_PADDING:
  1077. err_extra_text = "No padding not allowed with RSA-PSS";
  1078. goto cont;
  1079. case RSA_X931_PADDING:
  1080. err_extra_text = "X.931 padding not allowed with RSA-PSS";
  1081. cont:
  1082. if (RSA_test_flags(prsactx->rsa,
  1083. RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSA)
  1084. break;
  1085. /* FALLTHRU */
  1086. default:
  1087. bad_pad:
  1088. if (err_extra_text == NULL)
  1089. ERR_raise(ERR_LIB_PROV,
  1090. PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  1091. else
  1092. ERR_raise_data(ERR_LIB_PROV,
  1093. PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE,
  1094. err_extra_text);
  1095. return 0;
  1096. }
  1097. }
  1098. p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
  1099. if (p != NULL) {
  1100. if (pad_mode != RSA_PKCS1_PSS_PADDING) {
  1101. ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED,
  1102. "PSS saltlen can only be specified if "
  1103. "PSS padding has been specified first");
  1104. return 0;
  1105. }
  1106. switch (p->data_type) {
  1107. case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
  1108. if (!OSSL_PARAM_get_int(p, &saltlen))
  1109. return 0;
  1110. break;
  1111. case OSSL_PARAM_UTF8_STRING:
  1112. if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0)
  1113. saltlen = RSA_PSS_SALTLEN_DIGEST;
  1114. else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0)
  1115. saltlen = RSA_PSS_SALTLEN_MAX;
  1116. else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0)
  1117. saltlen = RSA_PSS_SALTLEN_AUTO;
  1118. else
  1119. saltlen = atoi(p->data);
  1120. break;
  1121. default:
  1122. return 0;
  1123. }
  1124. /*
  1125. * RSA_PSS_SALTLEN_MAX seems curiously named in this check.
  1126. * Contrary to what it's name suggests, it's the currently
  1127. * lowest saltlen number possible.
  1128. */
  1129. if (saltlen < RSA_PSS_SALTLEN_MAX) {
  1130. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
  1131. return 0;
  1132. }
  1133. if (rsa_pss_restricted(prsactx)) {
  1134. switch (saltlen) {
  1135. case RSA_PSS_SALTLEN_AUTO:
  1136. if (prsactx->operation == EVP_PKEY_OP_VERIFY) {
  1137. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH,
  1138. "Cannot use autodetected salt length");
  1139. return 0;
  1140. }
  1141. break;
  1142. case RSA_PSS_SALTLEN_DIGEST:
  1143. if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
  1144. ERR_raise_data(ERR_LIB_PROV,
  1145. PROV_R_PSS_SALTLEN_TOO_SMALL,
  1146. "Should be more than %d, but would be "
  1147. "set to match digest size (%d)",
  1148. prsactx->min_saltlen,
  1149. EVP_MD_get_size(prsactx->md));
  1150. return 0;
  1151. }
  1152. break;
  1153. default:
  1154. if (saltlen >= 0 && saltlen < prsactx->min_saltlen) {
  1155. ERR_raise_data(ERR_LIB_PROV,
  1156. PROV_R_PSS_SALTLEN_TOO_SMALL,
  1157. "Should be more than %d, "
  1158. "but would be set to %d",
  1159. prsactx->min_saltlen, saltlen);
  1160. return 0;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
  1166. if (p != NULL) {
  1167. const OSSL_PARAM *propsp =
  1168. OSSL_PARAM_locate_const(params,
  1169. OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES);
  1170. pmgf1mdname = mgf1mdname;
  1171. if (!OSSL_PARAM_get_utf8_string(p, &pmgf1mdname, sizeof(mgf1mdname)))
  1172. return 0;
  1173. if (propsp != NULL) {
  1174. pmgf1mdprops = mgf1mdprops;
  1175. if (!OSSL_PARAM_get_utf8_string(propsp,
  1176. &pmgf1mdprops, sizeof(mgf1mdprops)))
  1177. return 0;
  1178. }
  1179. if (pad_mode != RSA_PKCS1_PSS_PADDING) {
  1180. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD);
  1181. return 0;
  1182. }
  1183. }
  1184. prsactx->saltlen = saltlen;
  1185. prsactx->pad_mode = pad_mode;
  1186. if (prsactx->md == NULL && pmdname == NULL
  1187. && pad_mode == RSA_PKCS1_PSS_PADDING)
  1188. pmdname = RSA_DEFAULT_DIGEST_NAME;
  1189. if (pmgf1mdname != NULL
  1190. && !rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops))
  1191. return 0;
  1192. if (pmdname != NULL) {
  1193. if (!rsa_setup_md(prsactx, pmdname, pmdprops))
  1194. return 0;
  1195. } else {
  1196. if (!rsa_check_padding(prsactx, NULL, NULL, prsactx->mdnid))
  1197. return 0;
  1198. }
  1199. return 1;
  1200. }
  1201. static const OSSL_PARAM settable_ctx_params[] = {
  1202. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
  1203. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
  1204. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
  1205. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
  1206. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
  1207. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
  1208. OSSL_PARAM_END
  1209. };
  1210. static const OSSL_PARAM settable_ctx_params_no_digest[] = {
  1211. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
  1212. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
  1213. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
  1214. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
  1215. OSSL_PARAM_END
  1216. };
  1217. static const OSSL_PARAM *rsa_settable_ctx_params(void *vprsactx,
  1218. ossl_unused void *provctx)
  1219. {
  1220. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  1221. if (prsactx != NULL && !prsactx->flag_allow_md)
  1222. return settable_ctx_params_no_digest;
  1223. return settable_ctx_params;
  1224. }
  1225. static int rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params)
  1226. {
  1227. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  1228. if (prsactx->mdctx == NULL)
  1229. return 0;
  1230. return EVP_MD_CTX_get_params(prsactx->mdctx, params);
  1231. }
  1232. static const OSSL_PARAM *rsa_gettable_ctx_md_params(void *vprsactx)
  1233. {
  1234. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  1235. if (prsactx->md == NULL)
  1236. return 0;
  1237. return EVP_MD_gettable_ctx_params(prsactx->md);
  1238. }
  1239. static int rsa_set_ctx_md_params(void *vprsactx, const OSSL_PARAM params[])
  1240. {
  1241. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  1242. if (prsactx->mdctx == NULL)
  1243. return 0;
  1244. return EVP_MD_CTX_set_params(prsactx->mdctx, params);
  1245. }
  1246. static const OSSL_PARAM *rsa_settable_ctx_md_params(void *vprsactx)
  1247. {
  1248. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  1249. if (prsactx->md == NULL)
  1250. return 0;
  1251. return EVP_MD_settable_ctx_params(prsactx->md);
  1252. }
  1253. const OSSL_DISPATCH ossl_rsa_signature_functions[] = {
  1254. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },
  1255. { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))rsa_sign_init },
  1256. { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },
  1257. { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))rsa_verify_init },
  1258. { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))rsa_verify },
  1259. { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,
  1260. (void (*)(void))rsa_verify_recover_init },
  1261. { OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,
  1262. (void (*)(void))rsa_verify_recover },
  1263. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
  1264. (void (*)(void))rsa_digest_sign_init },
  1265. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
  1266. (void (*)(void))rsa_digest_signverify_update },
  1267. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
  1268. (void (*)(void))rsa_digest_sign_final },
  1269. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
  1270. (void (*)(void))rsa_digest_verify_init },
  1271. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
  1272. (void (*)(void))rsa_digest_signverify_update },
  1273. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
  1274. (void (*)(void))rsa_digest_verify_final },
  1275. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx },
  1276. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },
  1277. { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))rsa_get_ctx_params },
  1278. { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
  1279. (void (*)(void))rsa_gettable_ctx_params },
  1280. { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))rsa_set_ctx_params },
  1281. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
  1282. (void (*)(void))rsa_settable_ctx_params },
  1283. { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
  1284. (void (*)(void))rsa_get_ctx_md_params },
  1285. { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
  1286. (void (*)(void))rsa_gettable_ctx_md_params },
  1287. { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
  1288. (void (*)(void))rsa_set_ctx_md_params },
  1289. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
  1290. (void (*)(void))rsa_settable_ctx_md_params },
  1291. { 0, NULL }
  1292. };