2
0

baseprov.c 5.5 KB

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