core_algorithm.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2019 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_numbers.h>
  11. #include "internal/core.h"
  12. #include "internal/property.h"
  13. #include "internal/provider.h"
  14. struct algorithm_data_st {
  15. OPENSSL_CTX *libctx;
  16. int operation_id; /* May be zero for finding them all */
  17. void (*fn)(OSSL_PROVIDER *, const OSSL_ALGORITHM *, int no_store,
  18. void *data);
  19. void *data;
  20. };
  21. static int algorithm_do_this(OSSL_PROVIDER *provider, void *cbdata)
  22. {
  23. struct algorithm_data_st *data = cbdata;
  24. int no_store = 0; /* Assume caching is ok */
  25. int first_operation = 1;
  26. int last_operation = OSSL_OP__HIGHEST;
  27. int cur_operation;
  28. int ok = 0;
  29. if (data->operation_id != 0)
  30. first_operation = last_operation = data->operation_id;
  31. for (cur_operation = first_operation;
  32. cur_operation <= last_operation;
  33. cur_operation++) {
  34. const OSSL_ALGORITHM *map =
  35. ossl_provider_query_operation(provider, data->operation_id,
  36. &no_store);
  37. if (map == NULL)
  38. break;
  39. ok = 1; /* As long as we've found *something* */
  40. while (map->algorithm_names != NULL) {
  41. const OSSL_ALGORITHM *thismap = map++;
  42. data->fn(provider, thismap, no_store, data->data);
  43. }
  44. }
  45. return ok;
  46. }
  47. void ossl_algorithm_do_all(OPENSSL_CTX *libctx, int operation_id,
  48. OSSL_PROVIDER *provider,
  49. void (*fn)(OSSL_PROVIDER *provider,
  50. const OSSL_ALGORITHM *algo,
  51. int no_store, void *data),
  52. void *data)
  53. {
  54. struct algorithm_data_st cbdata;
  55. cbdata.libctx = libctx;
  56. cbdata.operation_id = operation_id;
  57. cbdata.fn = fn;
  58. cbdata.data = data;
  59. if (provider == NULL)
  60. ossl_provider_forall_loaded(libctx, algorithm_do_this, &cbdata);
  61. else
  62. algorithm_do_this(provider, &cbdata);
  63. }