encoder_meth.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. #include <openssl/core.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/encoder.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/encoder.h"
  18. #include "encoder_local.h"
  19. /*
  20. * Encoder 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_ENCODER *ossl_encoder_new(void)
  25. {
  26. OSSL_ENCODER *encoder = NULL;
  27. if ((encoder = OPENSSL_zalloc(sizeof(*encoder))) == NULL
  28. || (encoder->base.lock = CRYPTO_THREAD_lock_new()) == NULL) {
  29. OSSL_ENCODER_free(encoder);
  30. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
  31. return NULL;
  32. }
  33. encoder->base.refcnt = 1;
  34. return encoder;
  35. }
  36. int OSSL_ENCODER_up_ref(OSSL_ENCODER *encoder)
  37. {
  38. int ref = 0;
  39. CRYPTO_UP_REF(&encoder->base.refcnt, &ref, encoder->base.lock);
  40. return 1;
  41. }
  42. void OSSL_ENCODER_free(OSSL_ENCODER *encoder)
  43. {
  44. int ref = 0;
  45. if (encoder == NULL)
  46. return;
  47. CRYPTO_DOWN_REF(&encoder->base.refcnt, &ref, encoder->base.lock);
  48. if (ref > 0)
  49. return;
  50. OPENSSL_free(encoder->base.name);
  51. ossl_provider_free(encoder->base.prov);
  52. CRYPTO_THREAD_lock_free(encoder->base.lock);
  53. OPENSSL_free(encoder);
  54. }
  55. /* Permanent encoder method store, constructor and destructor */
  56. static void encoder_store_free(void *vstore)
  57. {
  58. ossl_method_store_free(vstore);
  59. }
  60. static void *encoder_store_new(OSSL_LIB_CTX *ctx)
  61. {
  62. return ossl_method_store_new(ctx);
  63. }
  64. static const OSSL_LIB_CTX_METHOD encoder_store_method = {
  65. OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
  66. encoder_store_new,
  67. encoder_store_free,
  68. };
  69. /* Data to be passed through ossl_method_construct() */
  70. struct encoder_data_st {
  71. OSSL_LIB_CTX *libctx;
  72. OSSL_METHOD_CONSTRUCT_METHOD *mcm;
  73. int id; /* For get_encoder_from_store() */
  74. const char *names; /* For get_encoder_from_store() */
  75. const char *propquery; /* For get_encoder_from_store() */
  76. unsigned int flag_construct_error_occurred : 1;
  77. };
  78. /*
  79. * Generic routines to fetch / create ENCODER methods with
  80. * ossl_method_construct()
  81. */
  82. /* Temporary encoder method store, constructor and destructor */
  83. static void *alloc_tmp_encoder_store(OSSL_LIB_CTX *ctx)
  84. {
  85. return ossl_method_store_new(ctx);
  86. }
  87. static void dealloc_tmp_encoder_store(void *store)
  88. {
  89. if (store != NULL)
  90. ossl_method_store_free(store);
  91. }
  92. /* Get the permanent encoder store */
  93. static OSSL_METHOD_STORE *get_encoder_store(OSSL_LIB_CTX *libctx)
  94. {
  95. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_ENCODER_STORE_INDEX,
  96. &encoder_store_method);
  97. }
  98. /* Get encoder methods from a store, or put one in */
  99. static void *get_encoder_from_store(OSSL_LIB_CTX *libctx, void *store,
  100. void *data)
  101. {
  102. struct encoder_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_encoder_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_encoder_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_encoder_store(libctx)) == NULL)
  127. return 0;
  128. return ossl_method_store_add(store, prov, id, propdef, method,
  129. (int (*)(void *))OSSL_ENCODER_up_ref,
  130. (void (*)(void *))OSSL_ENCODER_free);
  131. }
  132. /* Create and populate a encoder method */
  133. static void *encoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
  134. OSSL_PROVIDER *prov)
  135. {
  136. OSSL_ENCODER *encoder = NULL;
  137. const OSSL_DISPATCH *fns = algodef->implementation;
  138. if ((encoder = ossl_encoder_new()) == NULL)
  139. return NULL;
  140. encoder->base.id = id;
  141. if ((encoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  142. OSSL_ENCODER_free(encoder);
  143. return NULL;
  144. }
  145. encoder->base.propdef = algodef->property_definition;
  146. encoder->base.description = algodef->algorithm_description;
  147. for (; fns->function_id != 0; fns++) {
  148. switch (fns->function_id) {
  149. case OSSL_FUNC_ENCODER_NEWCTX:
  150. if (encoder->newctx == NULL)
  151. encoder->newctx =
  152. OSSL_FUNC_encoder_newctx(fns);
  153. break;
  154. case OSSL_FUNC_ENCODER_FREECTX:
  155. if (encoder->freectx == NULL)
  156. encoder->freectx =
  157. OSSL_FUNC_encoder_freectx(fns);
  158. break;
  159. case OSSL_FUNC_ENCODER_GET_PARAMS:
  160. if (encoder->get_params == NULL)
  161. encoder->get_params =
  162. OSSL_FUNC_encoder_get_params(fns);
  163. break;
  164. case OSSL_FUNC_ENCODER_GETTABLE_PARAMS:
  165. if (encoder->gettable_params == NULL)
  166. encoder->gettable_params =
  167. OSSL_FUNC_encoder_gettable_params(fns);
  168. break;
  169. case OSSL_FUNC_ENCODER_SET_CTX_PARAMS:
  170. if (encoder->set_ctx_params == NULL)
  171. encoder->set_ctx_params =
  172. OSSL_FUNC_encoder_set_ctx_params(fns);
  173. break;
  174. case OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS:
  175. if (encoder->settable_ctx_params == NULL)
  176. encoder->settable_ctx_params =
  177. OSSL_FUNC_encoder_settable_ctx_params(fns);
  178. break;
  179. case OSSL_FUNC_ENCODER_DOES_SELECTION:
  180. if (encoder->does_selection == NULL)
  181. encoder->does_selection =
  182. OSSL_FUNC_encoder_does_selection(fns);
  183. break;
  184. case OSSL_FUNC_ENCODER_ENCODE:
  185. if (encoder->encode == NULL)
  186. encoder->encode = OSSL_FUNC_encoder_encode(fns);
  187. break;
  188. case OSSL_FUNC_ENCODER_IMPORT_OBJECT:
  189. if (encoder->import_object == NULL)
  190. encoder->import_object =
  191. OSSL_FUNC_encoder_import_object(fns);
  192. break;
  193. case OSSL_FUNC_ENCODER_FREE_OBJECT:
  194. if (encoder->free_object == NULL)
  195. encoder->free_object =
  196. OSSL_FUNC_encoder_free_object(fns);
  197. break;
  198. }
  199. }
  200. /*
  201. * Try to check that the method is sensible.
  202. * If you have a constructor, you must have a destructor and vice versa.
  203. * You must have the encoding driver functions.
  204. */
  205. if (!((encoder->newctx == NULL && encoder->freectx == NULL)
  206. || (encoder->newctx != NULL && encoder->freectx != NULL)
  207. || (encoder->import_object != NULL && encoder->free_object != NULL)
  208. || (encoder->import_object == NULL && encoder->free_object == NULL))
  209. || encoder->encode == NULL
  210. || encoder->gettable_params == NULL
  211. || encoder->get_params == NULL) {
  212. OSSL_ENCODER_free(encoder);
  213. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
  214. return NULL;
  215. }
  216. if (prov != NULL && !ossl_provider_up_ref(prov)) {
  217. OSSL_ENCODER_free(encoder);
  218. return NULL;
  219. }
  220. encoder->base.prov = prov;
  221. return encoder;
  222. }
  223. /*
  224. * The core fetching functionality passes the names of the implementation.
  225. * This function is responsible to getting an identity number for them,
  226. * then call encoder_from_algorithm() with that identity number.
  227. */
  228. static void *construct_encoder(const OSSL_ALGORITHM *algodef,
  229. OSSL_PROVIDER *prov, void *data)
  230. {
  231. /*
  232. * This function is only called if get_encoder_from_store() returned
  233. * NULL, so it's safe to say that of all the spots to create a new
  234. * namemap entry, this is it. Should the name already exist there, we
  235. * know that ossl_namemap_add() will return its corresponding number.
  236. */
  237. struct encoder_data_st *methdata = data;
  238. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  239. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  240. const char *names = algodef->algorithm_names;
  241. int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
  242. void *method = NULL;
  243. if (id != 0)
  244. method = encoder_from_algorithm(id, algodef, prov);
  245. /*
  246. * Flag to indicate that there was actual construction errors. This
  247. * helps inner_evp_generic_fetch() determine what error it should
  248. * record on inaccessible algorithms.
  249. */
  250. if (method == NULL)
  251. methdata->flag_construct_error_occurred = 1;
  252. return method;
  253. }
  254. /* Intermediary function to avoid ugly casts, used below */
  255. static void destruct_encoder(void *method, void *data)
  256. {
  257. OSSL_ENCODER_free(method);
  258. }
  259. static int up_ref_encoder(void *method)
  260. {
  261. return OSSL_ENCODER_up_ref(method);
  262. }
  263. static void free_encoder(void *method)
  264. {
  265. OSSL_ENCODER_free(method);
  266. }
  267. /* Fetching support. Can fetch by numeric identity or by name */
  268. static OSSL_ENCODER *inner_ossl_encoder_fetch(OSSL_LIB_CTX *libctx,
  269. int id, const char *name,
  270. const char *properties)
  271. {
  272. OSSL_METHOD_STORE *store = get_encoder_store(libctx);
  273. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  274. void *method = NULL;
  275. int unsupported = 0;
  276. if (store == NULL || namemap == NULL) {
  277. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
  278. return NULL;
  279. }
  280. /*
  281. * If we have been passed neither a name_id or a name, we have an
  282. * internal programming error.
  283. */
  284. if (!ossl_assert(id != 0 || name != NULL)) {
  285. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
  286. return NULL;
  287. }
  288. if (id == 0)
  289. id = ossl_namemap_name2num(namemap, name);
  290. /*
  291. * If we haven't found the name yet, chances are that the algorithm to
  292. * be fetched is unsupported.
  293. */
  294. if (id == 0)
  295. unsupported = 1;
  296. if (id == 0
  297. || !ossl_method_store_cache_get(store, id, properties, &method)) {
  298. OSSL_METHOD_CONSTRUCT_METHOD mcm = {
  299. alloc_tmp_encoder_store,
  300. dealloc_tmp_encoder_store,
  301. get_encoder_from_store,
  302. put_encoder_in_store,
  303. construct_encoder,
  304. destruct_encoder
  305. };
  306. struct encoder_data_st mcmdata;
  307. mcmdata.libctx = libctx;
  308. mcmdata.mcm = &mcm;
  309. mcmdata.id = id;
  310. mcmdata.names = name;
  311. mcmdata.propquery = properties;
  312. mcmdata.flag_construct_error_occurred = 0;
  313. if ((method = ossl_method_construct(libctx, OSSL_OP_ENCODER,
  314. 0 /* !force_cache */,
  315. &mcm, &mcmdata)) != NULL) {
  316. /*
  317. * If construction did create a method for us, we know that
  318. * there is a correct name_id and meth_id, since those have
  319. * already been calculated in get_encoder_from_store() and
  320. * put_encoder_in_store() above.
  321. */
  322. if (id == 0)
  323. id = ossl_namemap_name2num(namemap, name);
  324. ossl_method_store_cache_set(store, id, properties, method,
  325. up_ref_encoder, free_encoder);
  326. }
  327. /*
  328. * If we never were in the constructor, the algorithm to be fetched
  329. * is unsupported.
  330. */
  331. unsupported = !mcmdata.flag_construct_error_occurred;
  332. }
  333. if (method == NULL) {
  334. int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
  335. if (name == NULL)
  336. name = ossl_namemap_num2name(namemap, id, 0);
  337. ERR_raise_data(ERR_LIB_OSSL_ENCODER, code,
  338. "%s, Name (%s : %d), Properties (%s)",
  339. ossl_lib_ctx_get_descriptor(libctx),
  340. name = NULL ? "<null>" : name, id,
  341. properties == NULL ? "<null>" : properties);
  342. }
  343. return method;
  344. }
  345. OSSL_ENCODER *OSSL_ENCODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
  346. const char *properties)
  347. {
  348. return inner_ossl_encoder_fetch(libctx, 0, name, properties);
  349. }
  350. OSSL_ENCODER *ossl_encoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
  351. const char *properties)
  352. {
  353. return inner_ossl_encoder_fetch(libctx, id, NULL, properties);
  354. }
  355. /*
  356. * Library of basic method functions
  357. */
  358. const OSSL_PROVIDER *OSSL_ENCODER_get0_provider(const OSSL_ENCODER *encoder)
  359. {
  360. if (!ossl_assert(encoder != NULL)) {
  361. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  362. return 0;
  363. }
  364. return encoder->base.prov;
  365. }
  366. const char *OSSL_ENCODER_get0_properties(const OSSL_ENCODER *encoder)
  367. {
  368. if (!ossl_assert(encoder != NULL)) {
  369. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  370. return 0;
  371. }
  372. return encoder->base.propdef;
  373. }
  374. int ossl_encoder_get_number(const OSSL_ENCODER *encoder)
  375. {
  376. if (!ossl_assert(encoder != NULL)) {
  377. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  378. return 0;
  379. }
  380. return encoder->base.id;
  381. }
  382. const char *OSSL_ENCODER_get0_name(const OSSL_ENCODER *encoder)
  383. {
  384. return encoder->base.name;
  385. }
  386. const char *OSSL_ENCODER_get0_description(const OSSL_ENCODER *encoder)
  387. {
  388. return encoder->base.description;
  389. }
  390. int OSSL_ENCODER_is_a(const OSSL_ENCODER *encoder, const char *name)
  391. {
  392. if (encoder->base.prov != NULL) {
  393. OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov);
  394. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  395. return ossl_namemap_name2num(namemap, name) == encoder->base.id;
  396. }
  397. return 0;
  398. }
  399. struct encoder_do_all_data_st {
  400. void (*user_fn)(void *method, void *arg);
  401. void *user_arg;
  402. };
  403. static void encoder_do_one(OSSL_PROVIDER *provider,
  404. const OSSL_ALGORITHM *algodef,
  405. int no_store, void *vdata)
  406. {
  407. struct encoder_do_all_data_st *data = vdata;
  408. OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
  409. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  410. const char *names = algodef->algorithm_names;
  411. int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
  412. void *method = NULL;
  413. if (id != 0)
  414. method =
  415. encoder_from_algorithm(id, algodef, provider);
  416. if (method != NULL) {
  417. data->user_fn(method, data->user_arg);
  418. OSSL_ENCODER_free(method);
  419. }
  420. }
  421. void OSSL_ENCODER_do_all_provided(OSSL_LIB_CTX *libctx,
  422. void (*fn)(OSSL_ENCODER *encoder, void *arg),
  423. void *arg)
  424. {
  425. struct encoder_do_all_data_st data;
  426. data.user_fn = (void (*)(void *, void *))fn;
  427. data.user_arg = arg;
  428. /*
  429. * No pre- or post-condition for this call, as this only creates methods
  430. * temporarly and then promptly destroys them.
  431. */
  432. ossl_algorithm_do_all(libctx, OSSL_OP_ENCODER, NULL, NULL,
  433. encoder_do_one, NULL, &data);
  434. }
  435. int OSSL_ENCODER_names_do_all(const OSSL_ENCODER *encoder,
  436. void (*fn)(const char *name, void *data),
  437. void *data)
  438. {
  439. if (encoder == NULL)
  440. return 0;
  441. if (encoder->base.prov != NULL) {
  442. OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov);
  443. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  444. return ossl_namemap_doall_names(namemap, encoder->base.id, fn, data);
  445. }
  446. return 1;
  447. }
  448. const OSSL_PARAM *
  449. OSSL_ENCODER_gettable_params(OSSL_ENCODER *encoder)
  450. {
  451. if (encoder != NULL && encoder->gettable_params != NULL) {
  452. void *provctx = ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder));
  453. return encoder->gettable_params(provctx);
  454. }
  455. return NULL;
  456. }
  457. int OSSL_ENCODER_get_params(OSSL_ENCODER *encoder, OSSL_PARAM params[])
  458. {
  459. if (encoder != NULL && encoder->get_params != NULL)
  460. return encoder->get_params(params);
  461. return 0;
  462. }
  463. const OSSL_PARAM *OSSL_ENCODER_settable_ctx_params(OSSL_ENCODER *encoder)
  464. {
  465. if (encoder != NULL && encoder->settable_ctx_params != NULL) {
  466. void *provctx = ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder));
  467. return encoder->settable_ctx_params(provctx);
  468. }
  469. return NULL;
  470. }
  471. /*
  472. * Encoder context support
  473. */
  474. OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new(void)
  475. {
  476. OSSL_ENCODER_CTX *ctx;
  477. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  478. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
  479. return ctx;
  480. }
  481. int OSSL_ENCODER_CTX_set_params(OSSL_ENCODER_CTX *ctx,
  482. const OSSL_PARAM params[])
  483. {
  484. int ok = 1;
  485. size_t i;
  486. size_t l;
  487. if (!ossl_assert(ctx != NULL)) {
  488. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  489. return 0;
  490. }
  491. if (ctx->encoder_insts == NULL)
  492. return 1;
  493. l = OSSL_ENCODER_CTX_get_num_encoders(ctx);
  494. for (i = 0; i < l; i++) {
  495. OSSL_ENCODER_INSTANCE *encoder_inst =
  496. sk_OSSL_ENCODER_INSTANCE_value(ctx->encoder_insts, i);
  497. OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst);
  498. void *encoderctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst);
  499. if (encoderctx == NULL || encoder->set_ctx_params == NULL)
  500. continue;
  501. if (!encoder->set_ctx_params(encoderctx, params))
  502. ok = 0;
  503. }
  504. return ok;
  505. }
  506. void OSSL_ENCODER_CTX_free(OSSL_ENCODER_CTX *ctx)
  507. {
  508. if (ctx != NULL) {
  509. sk_OSSL_ENCODER_INSTANCE_pop_free(ctx->encoder_insts,
  510. ossl_encoder_instance_free);
  511. OPENSSL_free(ctx->construct_data);
  512. ossl_pw_clear_passphrase_data(&ctx->pwdata);
  513. OPENSSL_free(ctx);
  514. }
  515. }