provider_ctx.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2020-2025 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 <stdlib.h>
  10. #include <string.h>
  11. #include "prov/provider_ctx.h"
  12. #include "prov/bio.h"
  13. PROV_CTX *ossl_prov_ctx_new(void)
  14. {
  15. return OPENSSL_zalloc(sizeof(PROV_CTX));
  16. }
  17. void ossl_prov_ctx_free(PROV_CTX *ctx)
  18. {
  19. OPENSSL_free(ctx);
  20. }
  21. void ossl_prov_ctx_set0_libctx(PROV_CTX *ctx, OSSL_LIB_CTX *libctx)
  22. {
  23. if (ctx != NULL)
  24. ctx->libctx = libctx;
  25. }
  26. void ossl_prov_ctx_set0_handle(PROV_CTX *ctx, const OSSL_CORE_HANDLE *handle)
  27. {
  28. if (ctx != NULL)
  29. ctx->handle = handle;
  30. }
  31. void ossl_prov_ctx_set0_core_bio_method(PROV_CTX *ctx, BIO_METHOD *corebiometh)
  32. {
  33. if (ctx != NULL)
  34. ctx->corebiometh = corebiometh;
  35. }
  36. void
  37. ossl_prov_ctx_set0_core_get_params(PROV_CTX *ctx,
  38. OSSL_FUNC_core_get_params_fn *c_get_params)
  39. {
  40. if (ctx != NULL)
  41. ctx->core_get_params = c_get_params;
  42. }
  43. OSSL_LIB_CTX *ossl_prov_ctx_get0_libctx(PROV_CTX *ctx)
  44. {
  45. if (ctx == NULL)
  46. return NULL;
  47. return ctx->libctx;
  48. }
  49. const OSSL_CORE_HANDLE *ossl_prov_ctx_get0_handle(PROV_CTX *ctx)
  50. {
  51. if (ctx == NULL)
  52. return NULL;
  53. return ctx->handle;
  54. }
  55. BIO_METHOD *ossl_prov_ctx_get0_core_bio_method(PROV_CTX *ctx)
  56. {
  57. if (ctx == NULL)
  58. return NULL;
  59. return ctx->corebiometh;
  60. }
  61. OSSL_FUNC_core_get_params_fn *ossl_prov_ctx_get0_core_get_params(PROV_CTX *ctx)
  62. {
  63. if (ctx == NULL)
  64. return NULL;
  65. return ctx->core_get_params;
  66. }
  67. const char *
  68. ossl_prov_ctx_get_param(PROV_CTX *ctx, const char *name, const char *defval)
  69. {
  70. char *val = NULL;
  71. OSSL_PARAM param[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  72. if (ctx == NULL
  73. || ctx->handle == NULL
  74. || ctx->core_get_params == NULL)
  75. return defval;
  76. param[0].key = (char *) name;
  77. param[0].data_type = OSSL_PARAM_UTF8_PTR;
  78. param[0].data = (void *) &val;
  79. param[0].data_size = sizeof(val);
  80. param[0].return_size = OSSL_PARAM_UNMODIFIED;
  81. /* Errors are ignored, returning the default value */
  82. if (ctx->core_get_params(ctx->handle, param)
  83. && OSSL_PARAM_modified(param)
  84. && val != NULL)
  85. return val;
  86. return defval;
  87. }
  88. int ossl_prov_ctx_get_bool_param(PROV_CTX *ctx, const char *name, int defval)
  89. {
  90. const char *val = ossl_prov_ctx_get_param(ctx, name, NULL);
  91. if (val != NULL) {
  92. if ((strcmp(val, "1") == 0)
  93. || (OPENSSL_strcasecmp(val, "yes") == 0)
  94. || (OPENSSL_strcasecmp(val, "true") == 0)
  95. || (OPENSSL_strcasecmp(val, "on") == 0))
  96. return 1;
  97. else if ((strcmp(val, "0") == 0)
  98. || (OPENSSL_strcasecmp(val, "no") == 0)
  99. || (OPENSSL_strcasecmp(val, "false") == 0)
  100. || (OPENSSL_strcasecmp(val, "off") == 0))
  101. return 0;
  102. }
  103. return defval;
  104. }