baseprov.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <string.h>
  10. #include <stdio.h>
  11. #include <openssl/opensslconf.h>
  12. #include <openssl/core.h>
  13. #include <openssl/core_dispatch.h>
  14. #include <openssl/core_names.h>
  15. #include <openssl/params.h>
  16. #include "prov/bio.h"
  17. #include "prov/provider_ctx.h"
  18. #include "prov/providercommon.h"
  19. #include "prov/implementations.h"
  20. #include "prov/provider_util.h"
  21. #include "prov/names.h"
  22. /*
  23. * Forward declarations to ensure that interface functions are correctly
  24. * defined.
  25. */
  26. static OSSL_FUNC_provider_gettable_params_fn base_gettable_params;
  27. static OSSL_FUNC_provider_get_params_fn base_get_params;
  28. static OSSL_FUNC_provider_query_operation_fn base_query;
  29. /* Functions provided by the core */
  30. static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
  31. static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
  32. /* Parameters we provide to the core */
  33. static const OSSL_PARAM base_param_types[] = {
  34. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
  35. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  36. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
  37. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
  38. OSSL_PARAM_END
  39. };
  40. static const OSSL_PARAM *base_gettable_params(void *provctx)
  41. {
  42. return base_param_types;
  43. }
  44. static int base_get_params(void *provctx, OSSL_PARAM params[])
  45. {
  46. OSSL_PARAM *p;
  47. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  48. if (p != NULL
  49. && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Base Provider"))
  50. return 0;
  51. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  52. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  53. return 0;
  54. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  55. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  56. return 0;
  57. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
  58. if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
  59. return 0;
  60. return 1;
  61. }
  62. static const OSSL_ALGORITHM base_encoder[] = {
  63. #define ENCODER_PROVIDER "base"
  64. #include "encoders.inc"
  65. { NULL, NULL, NULL }
  66. #undef ENCODER_PROVIDER
  67. };
  68. static const OSSL_ALGORITHM base_decoder[] = {
  69. #define DECODER_PROVIDER "base"
  70. #include "decoders.inc"
  71. { NULL, NULL, NULL }
  72. #undef DECODER_PROVIDER
  73. };
  74. static const OSSL_ALGORITHM base_store[] = {
  75. #define STORE(name, _fips, func_table) \
  76. { name, "provider=base,fips=" _fips, (func_table) },
  77. #include "stores.inc"
  78. { NULL, NULL, NULL }
  79. #undef STORE
  80. };
  81. static const OSSL_ALGORITHM base_rands[] = {
  82. { PROV_NAMES_SEED_SRC, "provider=base", ossl_seed_src_functions },
  83. { NULL, NULL, NULL }
  84. };
  85. static const OSSL_ALGORITHM *base_query(void *provctx, int operation_id,
  86. int *no_cache)
  87. {
  88. *no_cache = 0;
  89. switch (operation_id) {
  90. case OSSL_OP_ENCODER:
  91. return base_encoder;
  92. case OSSL_OP_DECODER:
  93. return base_decoder;
  94. case OSSL_OP_STORE:
  95. return base_store;
  96. case OSSL_OP_RAND:
  97. return base_rands;
  98. }
  99. return NULL;
  100. }
  101. static void base_teardown(void *provctx)
  102. {
  103. BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));
  104. ossl_prov_ctx_free(provctx);
  105. }
  106. /* Functions we provide to the core */
  107. static const OSSL_DISPATCH base_dispatch_table[] = {
  108. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))base_teardown },
  109. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS,
  110. (void (*)(void))base_gettable_params },
  111. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))base_get_params },
  112. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))base_query },
  113. OSSL_DISPATCH_END
  114. };
  115. OSSL_provider_init_fn ossl_base_provider_init;
  116. int ossl_base_provider_init(const OSSL_CORE_HANDLE *handle,
  117. const OSSL_DISPATCH *in, const OSSL_DISPATCH **out,
  118. void **provctx)
  119. {
  120. OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
  121. BIO_METHOD *corebiometh;
  122. if (!ossl_prov_bio_from_dispatch(in))
  123. return 0;
  124. for (; in->function_id != 0; in++) {
  125. switch (in->function_id) {
  126. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  127. c_gettable_params = OSSL_FUNC_core_gettable_params(in);
  128. break;
  129. case OSSL_FUNC_CORE_GET_PARAMS:
  130. c_get_params = OSSL_FUNC_core_get_params(in);
  131. break;
  132. case OSSL_FUNC_CORE_GET_LIBCTX:
  133. c_get_libctx = OSSL_FUNC_core_get_libctx(in);
  134. break;
  135. default:
  136. /* Just ignore anything we don't understand */
  137. break;
  138. }
  139. }
  140. if (c_get_libctx == NULL)
  141. return 0;
  142. /*
  143. * We want to make sure that all calls from this provider that requires
  144. * a library context use the same context as the one used to call our
  145. * functions. We do that by passing it along in the provider context.
  146. *
  147. * This only works for built-in providers. Most providers should
  148. * create their own library context.
  149. */
  150. if ((*provctx = ossl_prov_ctx_new()) == NULL
  151. || (corebiometh = ossl_bio_prov_init_bio_method()) == NULL) {
  152. ossl_prov_ctx_free(*provctx);
  153. *provctx = NULL;
  154. return 0;
  155. }
  156. ossl_prov_ctx_set0_libctx(*provctx,
  157. (OSSL_LIB_CTX *)c_get_libctx(handle));
  158. ossl_prov_ctx_set0_handle(*provctx, handle);
  159. ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);
  160. *out = base_dispatch_table;
  161. return 1;
  162. }