baseprov.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 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 <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 "internal/nelem.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_query(void *provctx, int operation_id,
  82. int *no_cache)
  83. {
  84. *no_cache = 0;
  85. switch (operation_id) {
  86. case OSSL_OP_ENCODER:
  87. return base_encoder;
  88. case OSSL_OP_DECODER:
  89. return base_decoder;
  90. case OSSL_OP_STORE:
  91. return base_store;
  92. }
  93. return NULL;
  94. }
  95. static void base_teardown(void *provctx)
  96. {
  97. BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));
  98. ossl_prov_ctx_free(provctx);
  99. }
  100. /* Functions we provide to the core */
  101. static const OSSL_DISPATCH base_dispatch_table[] = {
  102. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))base_teardown },
  103. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS,
  104. (void (*)(void))base_gettable_params },
  105. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))base_get_params },
  106. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))base_query },
  107. { 0, NULL }
  108. };
  109. OSSL_provider_init_fn ossl_base_provider_init;
  110. int ossl_base_provider_init(const OSSL_CORE_HANDLE *handle,
  111. const OSSL_DISPATCH *in, const OSSL_DISPATCH **out,
  112. void **provctx)
  113. {
  114. OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
  115. BIO_METHOD *corebiometh;
  116. if (!ossl_prov_bio_from_dispatch(in))
  117. return 0;
  118. for (; in->function_id != 0; in++) {
  119. switch (in->function_id) {
  120. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  121. c_gettable_params = OSSL_FUNC_core_gettable_params(in);
  122. break;
  123. case OSSL_FUNC_CORE_GET_PARAMS:
  124. c_get_params = OSSL_FUNC_core_get_params(in);
  125. break;
  126. case OSSL_FUNC_CORE_GET_LIBCTX:
  127. c_get_libctx = OSSL_FUNC_core_get_libctx(in);
  128. break;
  129. default:
  130. /* Just ignore anything we don't understand */
  131. break;
  132. }
  133. }
  134. if (c_get_libctx == NULL)
  135. return 0;
  136. /*
  137. * We want to make sure that all calls from this provider that requires
  138. * a library context use the same context as the one used to call our
  139. * functions. We do that by passing it along in the provider context.
  140. *
  141. * This only works for built-in providers. Most providers should
  142. * create their own library context.
  143. */
  144. if ((*provctx = ossl_prov_ctx_new()) == NULL
  145. || (corebiometh = ossl_bio_prov_init_bio_method()) == NULL) {
  146. ossl_prov_ctx_free(*provctx);
  147. *provctx = NULL;
  148. return 0;
  149. }
  150. ossl_prov_ctx_set0_libctx(*provctx,
  151. (OSSL_LIB_CTX *)c_get_libctx(handle));
  152. ossl_prov_ctx_set0_handle(*provctx, handle);
  153. ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);
  154. *out = base_dispatch_table;
  155. return 1;
  156. }