1
0

decoder_pkey.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/core_names.h>
  10. #include <openssl/core_object.h>
  11. #include <openssl/provider.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/ui.h>
  14. #include <openssl/decoder.h>
  15. #include <openssl/safestack.h>
  16. #include <openssl/trace.h>
  17. #include "crypto/evp.h"
  18. #include "crypto/decoder.h"
  19. #include "crypto/evp/evp_local.h"
  20. #include "encoder_local.h"
  21. #include "internal/namemap.h"
  22. int OSSL_DECODER_CTX_set_passphrase(OSSL_DECODER_CTX *ctx,
  23. const unsigned char *kstr,
  24. size_t klen)
  25. {
  26. return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen);
  27. }
  28. int OSSL_DECODER_CTX_set_passphrase_ui(OSSL_DECODER_CTX *ctx,
  29. const UI_METHOD *ui_method,
  30. void *ui_data)
  31. {
  32. return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data);
  33. }
  34. int OSSL_DECODER_CTX_set_pem_password_cb(OSSL_DECODER_CTX *ctx,
  35. pem_password_cb *cb, void *cbarg)
  36. {
  37. return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg);
  38. }
  39. int OSSL_DECODER_CTX_set_passphrase_cb(OSSL_DECODER_CTX *ctx,
  40. OSSL_PASSPHRASE_CALLBACK *cb,
  41. void *cbarg)
  42. {
  43. return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg);
  44. }
  45. /*
  46. * Support for OSSL_DECODER_CTX_new_for_pkey:
  47. * The construct data, and collecting keymgmt information for it
  48. */
  49. DEFINE_STACK_OF(EVP_KEYMGMT)
  50. struct decoder_pkey_data_st {
  51. OSSL_LIB_CTX *libctx;
  52. char *propq;
  53. int selection;
  54. STACK_OF(EVP_KEYMGMT) *keymgmts;
  55. char *object_type; /* recorded object data type, may be NULL */
  56. void **object; /* Where the result should end up */
  57. };
  58. static int decoder_construct_pkey(OSSL_DECODER_INSTANCE *decoder_inst,
  59. const OSSL_PARAM *params,
  60. void *construct_data)
  61. {
  62. struct decoder_pkey_data_st *data = construct_data;
  63. OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
  64. void *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
  65. const OSSL_PROVIDER *decoder_prov = OSSL_DECODER_get0_provider(decoder);
  66. EVP_KEYMGMT *keymgmt = NULL;
  67. const OSSL_PROVIDER *keymgmt_prov = NULL;
  68. int i, end;
  69. /*
  70. * |object_ref| points to a provider reference to an object, its exact
  71. * contents entirely opaque to us, but may be passed to any provider
  72. * function that expects this (such as OSSL_FUNC_keymgmt_load().
  73. *
  74. * This pointer is considered volatile, i.e. whatever it points at
  75. * is assumed to be freed as soon as this function returns.
  76. */
  77. void *object_ref = NULL;
  78. size_t object_ref_sz = 0;
  79. const OSSL_PARAM *p;
  80. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE);
  81. if (p != NULL) {
  82. char *object_type = NULL;
  83. if (!OSSL_PARAM_get_utf8_string(p, &object_type, 0))
  84. return 0;
  85. OPENSSL_free(data->object_type);
  86. data->object_type = object_type;
  87. }
  88. /*
  89. * For stuff that should end up in an EVP_PKEY, we only accept an object
  90. * reference for the moment. This enforces that the key data itself
  91. * remains with the provider.
  92. */
  93. p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE);
  94. if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
  95. return 0;
  96. object_ref = p->data;
  97. object_ref_sz = p->data_size;
  98. /*
  99. * First, we try to find a keymgmt that comes from the same provider as
  100. * the decoder that passed the params.
  101. */
  102. end = sk_EVP_KEYMGMT_num(data->keymgmts);
  103. for (i = 0; i < end; i++) {
  104. keymgmt = sk_EVP_KEYMGMT_value(data->keymgmts, i);
  105. keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
  106. if (keymgmt_prov == decoder_prov
  107. && evp_keymgmt_has_load(keymgmt)
  108. && EVP_KEYMGMT_is_a(keymgmt, data->object_type))
  109. break;
  110. }
  111. if (i < end) {
  112. /* To allow it to be freed further down */
  113. if (!EVP_KEYMGMT_up_ref(keymgmt))
  114. return 0;
  115. } else if ((keymgmt = EVP_KEYMGMT_fetch(data->libctx,
  116. data->object_type,
  117. data->propq)) != NULL) {
  118. keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt);
  119. }
  120. if (keymgmt != NULL) {
  121. EVP_PKEY *pkey = NULL;
  122. void *keydata = NULL;
  123. /*
  124. * If the EVP_KEYMGMT and the OSSL_DECODER are from the
  125. * same provider, we assume that the KEYMGMT has a key loading
  126. * function that can handle the provider reference we hold.
  127. *
  128. * Otherwise, we export from the decoder and import the
  129. * result in the keymgmt.
  130. */
  131. if (keymgmt_prov == decoder_prov) {
  132. keydata = evp_keymgmt_load(keymgmt, object_ref, object_ref_sz);
  133. } else {
  134. struct evp_keymgmt_util_try_import_data_st import_data;
  135. import_data.keymgmt = keymgmt;
  136. import_data.keydata = NULL;
  137. if (data->selection == 0)
  138. /* import/export functions do not tolerate 0 selection */
  139. import_data.selection = OSSL_KEYMGMT_SELECT_ALL;
  140. else
  141. import_data.selection = data->selection;
  142. /*
  143. * No need to check for errors here, the value of
  144. * |import_data.keydata| is as much an indicator.
  145. */
  146. (void)decoder->export_object(decoderctx,
  147. object_ref, object_ref_sz,
  148. &evp_keymgmt_util_try_import,
  149. &import_data);
  150. keydata = import_data.keydata;
  151. import_data.keydata = NULL;
  152. }
  153. if (keydata != NULL
  154. && (pkey = evp_keymgmt_util_make_pkey(keymgmt, keydata)) == NULL)
  155. evp_keymgmt_freedata(keymgmt, keydata);
  156. *data->object = pkey;
  157. /*
  158. * evp_keymgmt_util_make_pkey() increments the reference count when
  159. * assigning the EVP_PKEY, so we can free the keymgmt here.
  160. */
  161. EVP_KEYMGMT_free(keymgmt);
  162. }
  163. /*
  164. * We successfully looked through, |*ctx->object| determines if we
  165. * actually found something.
  166. */
  167. return (*data->object != NULL);
  168. }
  169. static void decoder_clean_pkey_construct_arg(void *construct_data)
  170. {
  171. struct decoder_pkey_data_st *data = construct_data;
  172. if (data != NULL) {
  173. sk_EVP_KEYMGMT_pop_free(data->keymgmts, EVP_KEYMGMT_free);
  174. OPENSSL_free(data->propq);
  175. OPENSSL_free(data->object_type);
  176. OPENSSL_free(data);
  177. }
  178. }
  179. struct collect_data_st {
  180. OSSL_LIB_CTX *libctx;
  181. OSSL_DECODER_CTX *ctx;
  182. const char *keytype; /* the keytype requested, if any */
  183. int keytype_id; /* if keytype_resolved is set, keymgmt name_id; else 0 */
  184. int sm2_id; /* if keytype_resolved is set and EC, SM2 name_id; else 0 */
  185. int total; /* number of matching results */
  186. char error_occurred;
  187. char keytype_resolved;
  188. STACK_OF(EVP_KEYMGMT) *keymgmts;
  189. };
  190. static void collect_decoder_keymgmt(EVP_KEYMGMT *keymgmt, OSSL_DECODER *decoder,
  191. void *provctx, struct collect_data_st *data)
  192. {
  193. void *decoderctx = NULL;
  194. OSSL_DECODER_INSTANCE *di = NULL;
  195. /*
  196. * We already checked the EVP_KEYMGMT is applicable in check_keymgmt so we
  197. * don't check it again here.
  198. */
  199. if (keymgmt->name_id != decoder->base.id)
  200. /* Mismatch is not an error, continue. */
  201. return;
  202. if ((decoderctx = decoder->newctx(provctx)) == NULL) {
  203. data->error_occurred = 1;
  204. return;
  205. }
  206. if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) {
  207. decoder->freectx(decoderctx);
  208. data->error_occurred = 1;
  209. return;
  210. }
  211. OSSL_TRACE_BEGIN(DECODER) {
  212. BIO_printf(trc_out,
  213. "(ctx %p) Checking out decoder %p:\n"
  214. " %s with %s\n",
  215. (void *)data->ctx, (void *)decoder,
  216. OSSL_DECODER_get0_name(decoder),
  217. OSSL_DECODER_get0_properties(decoder));
  218. } OSSL_TRACE_END(DECODER);
  219. if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) {
  220. ossl_decoder_instance_free(di);
  221. data->error_occurred = 1;
  222. return;
  223. }
  224. ++data->total;
  225. }
  226. static void collect_decoder(OSSL_DECODER *decoder, void *arg)
  227. {
  228. struct collect_data_st *data = arg;
  229. STACK_OF(EVP_KEYMGMT) *keymgmts = data->keymgmts;
  230. int i, end_i;
  231. EVP_KEYMGMT *keymgmt;
  232. const OSSL_PROVIDER *prov;
  233. void *provctx;
  234. if (data->error_occurred)
  235. return;
  236. prov = OSSL_DECODER_get0_provider(decoder);
  237. provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
  238. /*
  239. * Either the caller didn't give us a selection, or if they did, the decoder
  240. * must tell us if it supports that selection to be accepted. If the decoder
  241. * doesn't have |does_selection|, it's seen as taking anything.
  242. */
  243. if (decoder->does_selection != NULL
  244. && !decoder->does_selection(provctx, data->ctx->selection))
  245. return;
  246. OSSL_TRACE_BEGIN(DECODER) {
  247. BIO_printf(trc_out,
  248. "(ctx %p) Checking out decoder %p:\n"
  249. " %s with %s\n",
  250. (void *)data->ctx, (void *)decoder,
  251. OSSL_DECODER_get0_name(decoder),
  252. OSSL_DECODER_get0_properties(decoder));
  253. } OSSL_TRACE_END(DECODER);
  254. end_i = sk_EVP_KEYMGMT_num(keymgmts);
  255. for (i = 0; i < end_i; ++i) {
  256. keymgmt = sk_EVP_KEYMGMT_value(keymgmts, i);
  257. collect_decoder_keymgmt(keymgmt, decoder, provctx, data);
  258. if (data->error_occurred)
  259. return;
  260. }
  261. }
  262. /*
  263. * Is this EVP_KEYMGMT applicable given the key type given in the call to
  264. * ossl_decoder_ctx_setup_for_pkey (if any)?
  265. */
  266. static int check_keymgmt(EVP_KEYMGMT *keymgmt, struct collect_data_st *data)
  267. {
  268. /* If no keytype was specified, everything matches. */
  269. if (data->keytype == NULL)
  270. return 1;
  271. if (!data->keytype_resolved) {
  272. /* We haven't cached the IDs from the keytype string yet. */
  273. OSSL_NAMEMAP *namemap = ossl_namemap_stored(data->libctx);
  274. data->keytype_id = ossl_namemap_name2num(namemap, data->keytype);
  275. /*
  276. * If keytype is a value ambiguously used for both EC and SM2,
  277. * collect the ID for SM2 as well.
  278. */
  279. if (data->keytype_id != 0
  280. && (strcmp(data->keytype, "id-ecPublicKey") == 0
  281. || strcmp(data->keytype, "1.2.840.10045.2.1") == 0))
  282. data->sm2_id = ossl_namemap_name2num(namemap, "SM2");
  283. /*
  284. * If keytype_id is zero the name was not found, but we still
  285. * set keytype_resolved to avoid trying all this again.
  286. */
  287. data->keytype_resolved = 1;
  288. }
  289. /* Specified keytype could not be resolved, so nothing matches. */
  290. if (data->keytype_id == 0)
  291. return 0;
  292. /* Does not match the keytype specified, so skip. */
  293. if (keymgmt->name_id != data->keytype_id
  294. && keymgmt->name_id != data->sm2_id)
  295. return 0;
  296. return 1;
  297. }
  298. static void collect_keymgmt(EVP_KEYMGMT *keymgmt, void *arg)
  299. {
  300. struct collect_data_st *data = arg;
  301. if (!check_keymgmt(keymgmt, data))
  302. return;
  303. /*
  304. * We have to ref EVP_KEYMGMT here because in the success case,
  305. * data->keymgmts is referenced by the constructor we register in the
  306. * OSSL_DECODER_CTX. The registered cleanup function
  307. * (decoder_clean_pkey_construct_arg) unrefs every element of the stack and
  308. * frees it.
  309. */
  310. if (!EVP_KEYMGMT_up_ref(keymgmt))
  311. return;
  312. if (sk_EVP_KEYMGMT_push(data->keymgmts, keymgmt) <= 0) {
  313. EVP_KEYMGMT_free(keymgmt);
  314. data->error_occurred = 1;
  315. }
  316. }
  317. /*
  318. * This function does the actual binding of decoders to the OSSL_DECODER_CTX. It
  319. * searches for decoders matching 'keytype', which is a string like "RSA", "DH",
  320. * etc. If 'keytype' is NULL, decoders for all keytypes are bound.
  321. */
  322. int ossl_decoder_ctx_setup_for_pkey(OSSL_DECODER_CTX *ctx,
  323. EVP_PKEY **pkey, const char *keytype,
  324. OSSL_LIB_CTX *libctx,
  325. const char *propquery)
  326. {
  327. int ok = 0;
  328. struct decoder_pkey_data_st *process_data = NULL;
  329. struct collect_data_st collect_data = { NULL };
  330. STACK_OF(EVP_KEYMGMT) *keymgmts = NULL;
  331. OSSL_TRACE_BEGIN(DECODER) {
  332. const char *input_type = ctx->start_input_type;
  333. const char *input_structure = ctx->input_structure;
  334. BIO_printf(trc_out,
  335. "(ctx %p) Looking for decoders producing %s%s%s%s%s%s\n",
  336. (void *)ctx,
  337. keytype != NULL ? keytype : "",
  338. keytype != NULL ? " keys" : "keys of any type",
  339. input_type != NULL ? " from " : "",
  340. input_type != NULL ? input_type : "",
  341. input_structure != NULL ? " with " : "",
  342. input_structure != NULL ? input_structure : "");
  343. } OSSL_TRACE_END(DECODER);
  344. /* Allocate data. */
  345. if ((process_data = OPENSSL_zalloc(sizeof(*process_data))) == NULL
  346. || (propquery != NULL
  347. && (process_data->propq = OPENSSL_strdup(propquery)) == NULL)) {
  348. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  349. goto err;
  350. }
  351. /* Allocate our list of EVP_KEYMGMTs. */
  352. keymgmts = sk_EVP_KEYMGMT_new_null();
  353. if (keymgmts == NULL) {
  354. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  355. goto err;
  356. }
  357. process_data->object = (void **)pkey;
  358. process_data->libctx = libctx;
  359. process_data->selection = ctx->selection;
  360. process_data->keymgmts = keymgmts;
  361. /*
  362. * Enumerate all keymgmts into a stack.
  363. *
  364. * We could nest EVP_KEYMGMT_do_all_provided inside
  365. * OSSL_DECODER_do_all_provided or vice versa but these functions become
  366. * bottlenecks if called repeatedly, which is why we collect the
  367. * EVP_KEYMGMTs into a stack here and call both functions only once.
  368. *
  369. * We resolve the keytype string to a name ID so we don't have to resolve it
  370. * multiple times, avoiding repeated calls to EVP_KEYMGMT_is_a, which is a
  371. * performance bottleneck. However, we do this lazily on the first call to
  372. * collect_keymgmt made by EVP_KEYMGMT_do_all_provided, rather than do it
  373. * upfront, as this ensures that the names for all loaded providers have
  374. * been registered by the time we try to resolve the keytype string.
  375. */
  376. collect_data.ctx = ctx;
  377. collect_data.libctx = libctx;
  378. collect_data.keymgmts = keymgmts;
  379. collect_data.keytype = keytype;
  380. EVP_KEYMGMT_do_all_provided(libctx, collect_keymgmt, &collect_data);
  381. if (collect_data.error_occurred)
  382. goto err;
  383. /* Enumerate all matching decoders. */
  384. OSSL_DECODER_do_all_provided(libctx, collect_decoder, &collect_data);
  385. if (collect_data.error_occurred)
  386. goto err;
  387. OSSL_TRACE_BEGIN(DECODER) {
  388. BIO_printf(trc_out,
  389. "(ctx %p) Got %d decoders producing keys\n",
  390. (void *)ctx, collect_data.total);
  391. } OSSL_TRACE_END(DECODER);
  392. /*
  393. * Finish initializing the decoder context. If one or more decoders matched
  394. * above then the number of decoders attached to the OSSL_DECODER_CTX will
  395. * be nonzero. Else nothing was found and we do nothing.
  396. */
  397. if (OSSL_DECODER_CTX_get_num_decoders(ctx) != 0) {
  398. if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_pkey)
  399. || !OSSL_DECODER_CTX_set_construct_data(ctx, process_data)
  400. || !OSSL_DECODER_CTX_set_cleanup(ctx,
  401. decoder_clean_pkey_construct_arg))
  402. goto err;
  403. process_data = NULL; /* Avoid it being freed */
  404. }
  405. ok = 1;
  406. err:
  407. decoder_clean_pkey_construct_arg(process_data);
  408. return ok;
  409. }
  410. OSSL_DECODER_CTX *
  411. OSSL_DECODER_CTX_new_for_pkey(EVP_PKEY **pkey,
  412. const char *input_type,
  413. const char *input_structure,
  414. const char *keytype, int selection,
  415. OSSL_LIB_CTX *libctx, const char *propquery)
  416. {
  417. OSSL_DECODER_CTX *ctx = NULL;
  418. if ((ctx = OSSL_DECODER_CTX_new()) == NULL) {
  419. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  420. return NULL;
  421. }
  422. OSSL_TRACE_BEGIN(DECODER) {
  423. BIO_printf(trc_out,
  424. "(ctx %p) Looking for %s decoders with selection %d\n",
  425. (void *)ctx, keytype, selection);
  426. BIO_printf(trc_out, " input type: %s, input structure: %s\n",
  427. input_type, input_structure);
  428. } OSSL_TRACE_END(DECODER);
  429. if (OSSL_DECODER_CTX_set_input_type(ctx, input_type)
  430. && OSSL_DECODER_CTX_set_input_structure(ctx, input_structure)
  431. && OSSL_DECODER_CTX_set_selection(ctx, selection)
  432. && ossl_decoder_ctx_setup_for_pkey(ctx, pkey, keytype,
  433. libctx, propquery)
  434. && OSSL_DECODER_CTX_add_extra(ctx, libctx, propquery)) {
  435. OSSL_TRACE_BEGIN(DECODER) {
  436. BIO_printf(trc_out, "(ctx %p) Got %d decoders\n",
  437. (void *)ctx, OSSL_DECODER_CTX_get_num_decoders(ctx));
  438. } OSSL_TRACE_END(DECODER);
  439. return ctx;
  440. }
  441. OSSL_DECODER_CTX_free(ctx);
  442. return NULL;
  443. }