decoder_meth.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * Copyright 2020-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. #include <openssl/core.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/decoder.h>
  12. #include <openssl/ui.h>
  13. #include "internal/core.h"
  14. #include "internal/namemap.h"
  15. #include "internal/property.h"
  16. #include "internal/provider.h"
  17. #include "crypto/decoder.h"
  18. #include "encoder_local.h"
  19. /*
  20. * Decoder can have multiple names, separated with colons in a name string
  21. */
  22. #define NAME_SEPARATOR ':'
  23. /* Simple method structure constructor and destructor */
  24. static OSSL_DECODER *ossl_decoder_new(void)
  25. {
  26. OSSL_DECODER *decoder = NULL;
  27. if ((decoder = OPENSSL_zalloc(sizeof(*decoder))) == NULL
  28. || (decoder->base.lock = CRYPTO_THREAD_lock_new()) == NULL) {
  29. OSSL_DECODER_free(decoder);
  30. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  31. return NULL;
  32. }
  33. decoder->base.refcnt = 1;
  34. return decoder;
  35. }
  36. int OSSL_DECODER_up_ref(OSSL_DECODER *decoder)
  37. {
  38. int ref = 0;
  39. CRYPTO_UP_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
  40. return 1;
  41. }
  42. void OSSL_DECODER_free(OSSL_DECODER *decoder)
  43. {
  44. int ref = 0;
  45. if (decoder == NULL)
  46. return;
  47. CRYPTO_DOWN_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
  48. if (ref > 0)
  49. return;
  50. OPENSSL_free(decoder->base.name);
  51. ossl_provider_free(decoder->base.prov);
  52. CRYPTO_THREAD_lock_free(decoder->base.lock);
  53. OPENSSL_free(decoder);
  54. }
  55. /* Permanent decoder method store, constructor and destructor */
  56. static void decoder_store_free(void *vstore)
  57. {
  58. ossl_method_store_free(vstore);
  59. }
  60. static void *decoder_store_new(OSSL_LIB_CTX *ctx)
  61. {
  62. return ossl_method_store_new(ctx);
  63. }
  64. static const OSSL_LIB_CTX_METHOD decoder_store_method = {
  65. OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
  66. decoder_store_new,
  67. decoder_store_free,
  68. };
  69. /* Data to be passed through ossl_method_construct() */
  70. struct decoder_data_st {
  71. OSSL_LIB_CTX *libctx;
  72. OSSL_METHOD_CONSTRUCT_METHOD *mcm;
  73. int id; /* For get_decoder_from_store() */
  74. const char *names; /* For get_decoder_from_store() */
  75. const char *propquery; /* For get_decoder_from_store() */
  76. unsigned int flag_construct_error_occurred : 1;
  77. };
  78. /*
  79. * Generic routines to fetch / create DECODER methods with
  80. * ossl_method_construct()
  81. */
  82. /* Temporary decoder method store, constructor and destructor */
  83. static void *alloc_tmp_decoder_store(OSSL_LIB_CTX *ctx)
  84. {
  85. return ossl_method_store_new(ctx);
  86. }
  87. static void dealloc_tmp_decoder_store(void *store)
  88. {
  89. if (store != NULL)
  90. ossl_method_store_free(store);
  91. }
  92. /* Get the permanent decoder store */
  93. static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
  94. {
  95. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX,
  96. &decoder_store_method);
  97. }
  98. /* Get decoder methods from a store, or put one in */
  99. static void *get_decoder_from_store(OSSL_LIB_CTX *libctx, void *store,
  100. void *data)
  101. {
  102. struct decoder_data_st *methdata = data;
  103. void *method = NULL;
  104. int id;
  105. if ((id = methdata->id) == 0) {
  106. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  107. id = ossl_namemap_name2num(namemap, methdata->names);
  108. }
  109. if (store == NULL
  110. && (store = get_decoder_store(libctx)) == NULL)
  111. return NULL;
  112. if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
  113. return NULL;
  114. return method;
  115. }
  116. static int put_decoder_in_store(OSSL_LIB_CTX *libctx, void *store,
  117. void *method, const OSSL_PROVIDER *prov,
  118. int operation_id, const char *names,
  119. const char *propdef, void *unused)
  120. {
  121. OSSL_NAMEMAP *namemap;
  122. int id;
  123. if ((namemap = ossl_namemap_stored(libctx)) == NULL
  124. || (id = ossl_namemap_name2num(namemap, names)) == 0)
  125. return 0;
  126. if (store == NULL && (store = get_decoder_store(libctx)) == NULL)
  127. return 0;
  128. return ossl_method_store_add(store, prov, id, propdef, method,
  129. (int (*)(void *))OSSL_DECODER_up_ref,
  130. (void (*)(void *))OSSL_DECODER_free);
  131. }
  132. /* Create and populate a decoder method */
  133. void *ossl_decoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
  134. OSSL_PROVIDER *prov)
  135. {
  136. OSSL_DECODER *decoder = NULL;
  137. const OSSL_DISPATCH *fns = algodef->implementation;
  138. if ((decoder = ossl_decoder_new()) == NULL)
  139. return NULL;
  140. decoder->base.id = id;
  141. if ((decoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  142. OSSL_DECODER_free(decoder);
  143. return NULL;
  144. }
  145. decoder->base.propdef = algodef->property_definition;
  146. decoder->base.description = algodef->algorithm_description;
  147. for (; fns->function_id != 0; fns++) {
  148. switch (fns->function_id) {
  149. case OSSL_FUNC_DECODER_NEWCTX:
  150. if (decoder->newctx == NULL)
  151. decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
  152. break;
  153. case OSSL_FUNC_DECODER_FREECTX:
  154. if (decoder->freectx == NULL)
  155. decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
  156. break;
  157. case OSSL_FUNC_DECODER_GET_PARAMS:
  158. if (decoder->get_params == NULL)
  159. decoder->get_params =
  160. OSSL_FUNC_decoder_get_params(fns);
  161. break;
  162. case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
  163. if (decoder->gettable_params == NULL)
  164. decoder->gettable_params =
  165. OSSL_FUNC_decoder_gettable_params(fns);
  166. break;
  167. case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
  168. if (decoder->set_ctx_params == NULL)
  169. decoder->set_ctx_params =
  170. OSSL_FUNC_decoder_set_ctx_params(fns);
  171. break;
  172. case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
  173. if (decoder->settable_ctx_params == NULL)
  174. decoder->settable_ctx_params =
  175. OSSL_FUNC_decoder_settable_ctx_params(fns);
  176. break;
  177. case OSSL_FUNC_DECODER_DOES_SELECTION:
  178. if (decoder->does_selection == NULL)
  179. decoder->does_selection =
  180. OSSL_FUNC_decoder_does_selection(fns);
  181. break;
  182. case OSSL_FUNC_DECODER_DECODE:
  183. if (decoder->decode == NULL)
  184. decoder->decode = OSSL_FUNC_decoder_decode(fns);
  185. break;
  186. case OSSL_FUNC_DECODER_EXPORT_OBJECT:
  187. if (decoder->export_object == NULL)
  188. decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
  189. break;
  190. }
  191. }
  192. /*
  193. * Try to check that the method is sensible.
  194. * If you have a constructor, you must have a destructor and vice versa.
  195. * You must have at least one of the encoding driver functions.
  196. */
  197. if (!((decoder->newctx == NULL && decoder->freectx == NULL)
  198. || (decoder->newctx != NULL && decoder->freectx != NULL))
  199. || decoder->decode == NULL) {
  200. OSSL_DECODER_free(decoder);
  201. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
  202. return NULL;
  203. }
  204. if (prov != NULL && !ossl_provider_up_ref(prov)) {
  205. OSSL_DECODER_free(decoder);
  206. return NULL;
  207. }
  208. decoder->base.prov = prov;
  209. return decoder;
  210. }
  211. /*
  212. * The core fetching functionality passes the names of the implementation.
  213. * This function is responsible to getting an identity number for them,
  214. * then call ossl_decoder_from_algorithm() with that identity number.
  215. */
  216. static void *construct_decoder(const OSSL_ALGORITHM *algodef,
  217. OSSL_PROVIDER *prov, void *data)
  218. {
  219. /*
  220. * This function is only called if get_decoder_from_store() returned
  221. * NULL, so it's safe to say that of all the spots to create a new
  222. * namemap entry, this is it. Should the name already exist there, we
  223. * know that ossl_namemap_add() will return its corresponding number.
  224. */
  225. struct decoder_data_st *methdata = data;
  226. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  227. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  228. const char *names = algodef->algorithm_names;
  229. int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
  230. void *method = NULL;
  231. if (id != 0)
  232. method = ossl_decoder_from_algorithm(id, algodef, prov);
  233. /*
  234. * Flag to indicate that there was actual construction errors. This
  235. * helps inner_evp_generic_fetch() determine what error it should
  236. * record on inaccessible algorithms.
  237. */
  238. if (method == NULL)
  239. methdata->flag_construct_error_occurred = 1;
  240. return method;
  241. }
  242. /* Intermediary function to avoid ugly casts, used below */
  243. static void destruct_decoder(void *method, void *data)
  244. {
  245. OSSL_DECODER_free(method);
  246. }
  247. static int up_ref_decoder(void *method)
  248. {
  249. return OSSL_DECODER_up_ref(method);
  250. }
  251. static void free_decoder(void *method)
  252. {
  253. OSSL_DECODER_free(method);
  254. }
  255. /* Fetching support. Can fetch by numeric identity or by name */
  256. static OSSL_DECODER *inner_ossl_decoder_fetch(OSSL_LIB_CTX *libctx, int id,
  257. const char *name,
  258. const char *properties)
  259. {
  260. OSSL_METHOD_STORE *store = get_decoder_store(libctx);
  261. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  262. void *method = NULL;
  263. int unsupported = 0;
  264. if (store == NULL || namemap == NULL) {
  265. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_INVALID_ARGUMENT);
  266. return NULL;
  267. }
  268. /*
  269. * If we have been passed neither a name_id or a name, we have an
  270. * internal programming error.
  271. */
  272. if (!ossl_assert(id != 0 || name != NULL)) {
  273. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
  274. return NULL;
  275. }
  276. if (id == 0)
  277. id = ossl_namemap_name2num(namemap, name);
  278. /*
  279. * If we haven't found the name yet, chances are that the algorithm to
  280. * be fetched is unsupported.
  281. */
  282. if (id == 0)
  283. unsupported = 1;
  284. if (id == 0
  285. || !ossl_method_store_cache_get(store, id, properties, &method)) {
  286. OSSL_METHOD_CONSTRUCT_METHOD mcm = {
  287. alloc_tmp_decoder_store,
  288. dealloc_tmp_decoder_store,
  289. get_decoder_from_store,
  290. put_decoder_in_store,
  291. construct_decoder,
  292. destruct_decoder
  293. };
  294. struct decoder_data_st mcmdata;
  295. mcmdata.libctx = libctx;
  296. mcmdata.mcm = &mcm;
  297. mcmdata.id = id;
  298. mcmdata.names = name;
  299. mcmdata.propquery = properties;
  300. mcmdata.flag_construct_error_occurred = 0;
  301. if ((method = ossl_method_construct(libctx, OSSL_OP_DECODER,
  302. 0 /* !force_cache */,
  303. &mcm, &mcmdata)) != NULL) {
  304. /*
  305. * If construction did create a method for us, we know that
  306. * there is a correct name_id and meth_id, since those have
  307. * already been calculated in get_decoder_from_store() and
  308. * put_decoder_in_store() above.
  309. */
  310. if (id == 0)
  311. id = ossl_namemap_name2num(namemap, name);
  312. ossl_method_store_cache_set(store, id, properties, method,
  313. up_ref_decoder, free_decoder);
  314. }
  315. /*
  316. * If we never were in the constructor, the algorithm to be fetched
  317. * is unsupported.
  318. */
  319. unsupported = !mcmdata.flag_construct_error_occurred;
  320. }
  321. if (method == NULL) {
  322. int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
  323. if (name == NULL)
  324. name = ossl_namemap_num2name(namemap, id, 0);
  325. ERR_raise_data(ERR_LIB_OSSL_DECODER, code,
  326. "%s, Name (%s : %d), Properties (%s)",
  327. ossl_lib_ctx_get_descriptor(libctx),
  328. name = NULL ? "<null>" : name, id,
  329. properties == NULL ? "<null>" : properties);
  330. }
  331. return method;
  332. }
  333. OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
  334. const char *properties)
  335. {
  336. return inner_ossl_decoder_fetch(libctx, 0, name, properties);
  337. }
  338. OSSL_DECODER *ossl_decoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
  339. const char *properties)
  340. {
  341. return inner_ossl_decoder_fetch(libctx, id, NULL, properties);
  342. }
  343. /*
  344. * Library of basic method functions
  345. */
  346. const OSSL_PROVIDER *OSSL_DECODER_get0_provider(const OSSL_DECODER *decoder)
  347. {
  348. if (!ossl_assert(decoder != NULL)) {
  349. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  350. return 0;
  351. }
  352. return decoder->base.prov;
  353. }
  354. const char *OSSL_DECODER_get0_properties(const OSSL_DECODER *decoder)
  355. {
  356. if (!ossl_assert(decoder != NULL)) {
  357. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  358. return 0;
  359. }
  360. return decoder->base.propdef;
  361. }
  362. int ossl_decoder_get_number(const OSSL_DECODER *decoder)
  363. {
  364. if (!ossl_assert(decoder != NULL)) {
  365. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  366. return 0;
  367. }
  368. return decoder->base.id;
  369. }
  370. const char *OSSL_DECODER_get0_name(const OSSL_DECODER *decoder)
  371. {
  372. return decoder->base.name;
  373. }
  374. const char *OSSL_DECODER_get0_description(const OSSL_DECODER *decoder)
  375. {
  376. return decoder->base.description;
  377. }
  378. int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
  379. {
  380. if (decoder->base.prov != NULL) {
  381. OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
  382. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  383. return ossl_namemap_name2num(namemap, name) == decoder->base.id;
  384. }
  385. return 0;
  386. }
  387. struct decoder_do_all_data_st {
  388. void (*user_fn)(void *method, void *arg);
  389. void *user_arg;
  390. };
  391. static void decoder_do_one(OSSL_PROVIDER *provider,
  392. const OSSL_ALGORITHM *algodef,
  393. int no_store, void *vdata)
  394. {
  395. struct decoder_do_all_data_st *data = vdata;
  396. OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
  397. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  398. const char *names = algodef->algorithm_names;
  399. int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
  400. void *method = NULL;
  401. if (id != 0)
  402. method = ossl_decoder_from_algorithm(id, algodef, provider);
  403. if (method != NULL) {
  404. data->user_fn(method, data->user_arg);
  405. OSSL_DECODER_free(method);
  406. }
  407. }
  408. void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
  409. void (*fn)(OSSL_DECODER *decoder, void *arg),
  410. void *arg)
  411. {
  412. struct decoder_do_all_data_st data;
  413. data.user_fn = (void (*)(void *, void *))fn;
  414. data.user_arg = arg;
  415. ossl_algorithm_do_all(libctx, OSSL_OP_DECODER, NULL,
  416. NULL, decoder_do_one, NULL,
  417. &data);
  418. }
  419. int OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
  420. void (*fn)(const char *name, void *data),
  421. void *data)
  422. {
  423. if (decoder == NULL)
  424. return 0;
  425. if (decoder->base.prov != NULL) {
  426. OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
  427. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  428. return ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
  429. }
  430. return 1;
  431. }
  432. const OSSL_PARAM *
  433. OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
  434. {
  435. if (decoder != NULL && decoder->gettable_params != NULL) {
  436. void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
  437. return decoder->gettable_params(provctx);
  438. }
  439. return NULL;
  440. }
  441. int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
  442. {
  443. if (decoder != NULL && decoder->get_params != NULL)
  444. return decoder->get_params(params);
  445. return 0;
  446. }
  447. const OSSL_PARAM *
  448. OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
  449. {
  450. if (decoder != NULL && decoder->settable_ctx_params != NULL) {
  451. void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
  452. return decoder->settable_ctx_params(provctx);
  453. }
  454. return NULL;
  455. }
  456. /*
  457. * Decoder context support
  458. */
  459. /*
  460. * |encoder| value NULL is valid, and signifies that there is no decoder.
  461. * This is useful to provide fallback mechanisms.
  462. * Functions that want to verify if there is a decoder can do so with
  463. * OSSL_DECODER_CTX_get_decoder()
  464. */
  465. OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
  466. {
  467. OSSL_DECODER_CTX *ctx;
  468. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  469. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  470. return ctx;
  471. }
  472. int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
  473. const OSSL_PARAM params[])
  474. {
  475. int ok = 1;
  476. size_t i;
  477. size_t l;
  478. if (!ossl_assert(ctx != NULL)) {
  479. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  480. return 0;
  481. }
  482. if (ctx->decoder_insts == NULL)
  483. return 1;
  484. l = OSSL_DECODER_CTX_get_num_decoders(ctx);
  485. for (i = 0; i < l; i++) {
  486. OSSL_DECODER_INSTANCE *decoder_inst =
  487. sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
  488. OSSL_DECODER *decoder =
  489. OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
  490. OSSL_DECODER *decoderctx =
  491. OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
  492. if (decoderctx == NULL || decoder->set_ctx_params == NULL)
  493. continue;
  494. if (!decoder->set_ctx_params(decoderctx, params))
  495. ok = 0;
  496. }
  497. return ok;
  498. }
  499. void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
  500. {
  501. if (ctx != NULL) {
  502. if (ctx->cleanup != NULL)
  503. ctx->cleanup(ctx->construct_data);
  504. sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
  505. ossl_decoder_instance_free);
  506. ossl_pw_clear_passphrase_data(&ctx->pwdata);
  507. OPENSSL_free(ctx);
  508. }
  509. }