legacyprov.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2019 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/core.h>
  12. #include <openssl/core_numbers.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/params.h>
  15. #include "internal/provider_algs.h"
  16. /* Functions provided by the core */
  17. static OSSL_core_get_param_types_fn *c_get_param_types = NULL;
  18. static OSSL_core_get_params_fn *c_get_params = NULL;
  19. /* Parameters we provide to the core */
  20. static const OSSL_ITEM legacy_param_types[] = {
  21. { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
  22. { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
  23. { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
  24. { 0, NULL }
  25. };
  26. static const OSSL_ITEM *legacy_get_param_types(const OSSL_PROVIDER *prov)
  27. {
  28. return legacy_param_types;
  29. }
  30. static int legacy_get_params(const OSSL_PROVIDER *prov,
  31. const OSSL_PARAM params[])
  32. {
  33. const OSSL_PARAM *p;
  34. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  35. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))
  36. return 0;
  37. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  38. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  39. return 0;
  40. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  41. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  42. return 0;
  43. return 1;
  44. }
  45. static const OSSL_ALGORITHM legacy_digests[] = {
  46. #ifndef OPENSSL_NO_MD2
  47. { "MD2", "legacy=yes", md2_functions },
  48. #endif
  49. #ifndef OPENSSL_NO_MD4
  50. { "MD4", "legacy=yes", md4_functions },
  51. #endif
  52. #ifndef OPENSSL_NO_MDC2
  53. { "MDC2", "legacy=yes", mdc2_functions },
  54. #endif /* OPENSSL_NO_MDC2 */
  55. #ifndef OPENSSL_NO_WHIRLPOOL
  56. { "whirlpool", "legacy=yes", wp_functions },
  57. #endif /* OPENSSL_NO_WHIRLPOOL */
  58. #ifndef OPENSSL_NO_RMD160
  59. { "RIPEMD160", "legacy=yes", ripemd160_functions },
  60. #endif /* OPENSSL_NO_RMD160 */
  61. { NULL, NULL, NULL }
  62. };
  63. static const OSSL_ALGORITHM *legacy_query(OSSL_PROVIDER *prov,
  64. int operation_id,
  65. int *no_cache)
  66. {
  67. *no_cache = 0;
  68. switch (operation_id) {
  69. case OSSL_OP_DIGEST:
  70. return legacy_digests;
  71. }
  72. return NULL;
  73. }
  74. /* Functions we provide to the core */
  75. static const OSSL_DISPATCH legacy_dispatch_table[] = {
  76. { OSSL_FUNC_PROVIDER_GET_PARAM_TYPES, (void (*)(void))legacy_get_param_types },
  77. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
  78. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
  79. { 0, NULL }
  80. };
  81. int OSSL_provider_init(const OSSL_PROVIDER *provider,
  82. const OSSL_DISPATCH *in,
  83. const OSSL_DISPATCH **out,
  84. void **provctx)
  85. {
  86. OSSL_core_get_library_context_fn *c_get_libctx = NULL;
  87. for (; in->function_id != 0; in++) {
  88. switch (in->function_id) {
  89. case OSSL_FUNC_CORE_GET_PARAM_TYPES:
  90. c_get_param_types = OSSL_get_core_get_param_types(in);
  91. break;
  92. case OSSL_FUNC_CORE_GET_PARAMS:
  93. c_get_params = OSSL_get_core_get_params(in);
  94. break;
  95. case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
  96. c_get_libctx = OSSL_get_core_get_library_context(in);
  97. break;
  98. /* Just ignore anything we don't understand */
  99. default:
  100. break;
  101. }
  102. }
  103. if (c_get_libctx == NULL)
  104. return 0;
  105. *out = legacy_dispatch_table;
  106. /*
  107. * We want to make sure that all calls from this provider that requires
  108. * a library context use the same context as the one used to call our
  109. * functions. We do that by passing it along as the provider context.
  110. */
  111. *provctx = c_get_libctx(provider);
  112. return 1;
  113. }