rsa.c 48 KB

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