core_algorithm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2019-2020 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 "internal/core.h"
  12. #include "internal/property.h"
  13. #include "internal/provider.h"
  14. struct algorithm_data_st {
  15. OSSL_LIB_CTX *libctx;
  16. int operation_id; /* May be zero for finding them all */
  17. int (*pre)(OSSL_PROVIDER *, int operation_id, void *data, int *result);
  18. void (*fn)(OSSL_PROVIDER *, const OSSL_ALGORITHM *, int no_store,
  19. void *data);
  20. int (*post)(OSSL_PROVIDER *, int operation_id, int no_store, void *data,
  21. int *result);
  22. void *data;
  23. };
  24. static int algorithm_do_this(OSSL_PROVIDER *provider, void *cbdata)
  25. {
  26. struct algorithm_data_st *data = cbdata;
  27. int no_store = 0; /* Assume caching is ok */
  28. int first_operation = 1;
  29. int last_operation = OSSL_OP__HIGHEST;
  30. int cur_operation;
  31. int ok = 1;
  32. if (data->operation_id != 0)
  33. first_operation = last_operation = data->operation_id;
  34. for (cur_operation = first_operation;
  35. cur_operation <= last_operation;
  36. cur_operation++) {
  37. const OSSL_ALGORITHM *map = NULL;
  38. int ret;
  39. /* Do we fulfill pre-conditions? */
  40. if (data->pre == NULL) {
  41. /* If there is no pre-condition function, assume "yes" */
  42. ret = 1;
  43. } else {
  44. if (!data->pre(provider, cur_operation, data->data, &ret))
  45. /* Error, bail out! */
  46. return 0;
  47. }
  48. /* If pre-condition not fulfilled, go to the next operation */
  49. if (!ret)
  50. continue;
  51. map = ossl_provider_query_operation(provider, cur_operation,
  52. &no_store);
  53. if (map != NULL) {
  54. while (map->algorithm_names != NULL) {
  55. const OSSL_ALGORITHM *thismap = map++;
  56. data->fn(provider, thismap, no_store, data->data);
  57. }
  58. }
  59. /* Do we fulfill post-conditions? */
  60. if (data->post == NULL) {
  61. /* If there is no post-condition function, assume "yes" */
  62. ret = 1;
  63. } else {
  64. if (!data->post(provider, cur_operation, no_store, data->data,
  65. &ret))
  66. /* Error, bail out! */
  67. return 0;
  68. }
  69. /* If post-condition not fulfilled, set general failure */
  70. if (!ret)
  71. ok = 0;
  72. }
  73. return ok;
  74. }
  75. void ossl_algorithm_do_all(OSSL_LIB_CTX *libctx, int operation_id,
  76. OSSL_PROVIDER *provider,
  77. int (*pre)(OSSL_PROVIDER *, int operation_id,
  78. void *data, int *result),
  79. void (*fn)(OSSL_PROVIDER *provider,
  80. const OSSL_ALGORITHM *algo,
  81. int no_store, void *data),
  82. int (*post)(OSSL_PROVIDER *, int operation_id,
  83. int no_store, void *data, int *result),
  84. void *data)
  85. {
  86. struct algorithm_data_st cbdata = { 0, };
  87. cbdata.libctx = libctx;
  88. cbdata.operation_id = operation_id;
  89. cbdata.pre = pre;
  90. cbdata.fn = fn;
  91. cbdata.post = post;
  92. cbdata.data = data;
  93. if (provider == NULL)
  94. ossl_provider_forall_loaded(libctx, algorithm_do_this, &cbdata);
  95. else
  96. algorithm_do_this(provider, &cbdata);
  97. }