A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
6.3 KiB

  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 "prov/implementations.h"
  16. #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC }
  17. #ifdef STATIC_LEGACY
  18. OSSL_provider_init_fn ossl_legacy_provider_init;
  19. # define OSSL_provider_init ossl_legacy_provider_init
  20. #endif
  21. /* Functions provided by the core */
  22. static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
  23. static OSSL_core_get_params_fn *c_get_params = NULL;
  24. /* Parameters we provide to the core */
  25. static const OSSL_ITEM legacy_param_types[] = {
  26. { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
  27. { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
  28. { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
  29. { 0, NULL }
  30. };
  31. static const OSSL_ITEM *legacy_gettable_params(const OSSL_PROVIDER *prov)
  32. {
  33. return legacy_param_types;
  34. }
  35. static int legacy_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  36. {
  37. OSSL_PARAM *p;
  38. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  39. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))
  40. return 0;
  41. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  42. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  43. return 0;
  44. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  45. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  46. return 0;
  47. return 1;
  48. }
  49. static const OSSL_ALGORITHM legacy_digests[] = {
  50. #ifndef OPENSSL_NO_MD2
  51. ALG("MD2", md2_functions),
  52. #endif
  53. #ifndef OPENSSL_NO_MD4
  54. ALG("MD4", md4_functions),
  55. #endif
  56. #ifndef OPENSSL_NO_MDC2
  57. ALG("MDC2", mdc2_functions),
  58. #endif /* OPENSSL_NO_MDC2 */
  59. #ifndef OPENSSL_NO_WHIRLPOOL
  60. ALG("WHIRLPOOL", wp_functions),
  61. #endif /* OPENSSL_NO_WHIRLPOOL */
  62. #ifndef OPENSSL_NO_RMD160
  63. ALG("RIPEMD-160:RIPEMD160:RIPEMD:RMD160", ripemd160_functions),
  64. #endif /* OPENSSL_NO_RMD160 */
  65. { NULL, NULL, NULL }
  66. };
  67. static const OSSL_ALGORITHM legacy_ciphers[] = {
  68. #ifndef OPENSSL_NO_CAST
  69. ALG("CAST5-ECB", cast5128ecb_functions),
  70. ALG("CAST5-CBC:CAST-CBC:CAST", cast5128cbc_functions),
  71. ALG("CAST5-OFB", cast564ofb64_functions),
  72. ALG("CAST5-CFB", cast564cfb64_functions),
  73. #endif /* OPENSSL_NO_CAST */
  74. #ifndef OPENSSL_NO_BF
  75. ALG("BF-ECB", blowfish128ecb_functions),
  76. ALG("BF-CBC:BF:BLOWFISH", blowfish128cbc_functions),
  77. ALG("BF-OFB", blowfish64ofb64_functions),
  78. ALG("BF-CFB", blowfish64cfb64_functions),
  79. #endif /* OPENSSL_NO_BF */
  80. #ifndef OPENSSL_NO_IDEA
  81. ALG("IDEA-ECB", idea128ecb_functions),
  82. ALG("IDEA-CBC:IDEA", idea128cbc_functions),
  83. ALG("IDEA-OFB:IDEA-OFB64", idea128ofb64_functions),
  84. ALG("IDEA-CFB:IDEA-CFB64", idea128cfb64_functions),
  85. #endif /* OPENSSL_NO_IDEA */
  86. #ifndef OPENSSL_NO_SEED
  87. ALG("SEED-ECB", seed128ecb_functions),
  88. ALG("SEED-CBC:SEED", seed128cbc_functions),
  89. ALG("SEED-OFB:SEED-OFB128", seed128ofb128_functions),
  90. ALG("SEED-CFB:SEED-CFB128", seed128cfb128_functions),
  91. #endif /* OPENSSL_NO_SEED */
  92. #ifndef OPENSSL_NO_RC2
  93. ALG("RC2-ECB", rc2128ecb_functions),
  94. ALG("RC2-CBC", rc2128cbc_functions),
  95. ALG("RC2-40-CBC", rc240cbc_functions),
  96. ALG("RC2-64-CBC", rc264cbc_functions),
  97. ALG("RC2-CFB", rc2128cfb128_functions),
  98. ALG("RC2-OFB", rc2128ofb128_functions),
  99. #endif /* OPENSSL_NO_RC2 */
  100. #ifndef OPENSSL_NO_RC4
  101. ALG("RC4", rc4128_functions),
  102. ALG("RC4-40", rc440_functions),
  103. # ifndef OPENSSL_NO_MD5
  104. ALG("RC4-HMAC-MD5", rc4_hmac_md5_functions),
  105. # endif /* OPENSSL_NO_MD5 */
  106. #endif /* OPENSSL_NO_RC4 */
  107. #ifndef OPENSSL_NO_RC5
  108. ALG("RC5-ECB", rc5128ecb_functions),
  109. ALG("RC5-CBC", rc5128cbc_functions),
  110. ALG("RC5-OFB", rc5128ofb64_functions),
  111. ALG("RC5-CFB", rc5128cfb64_functions),
  112. #endif /* OPENSSL_NO_RC5 */
  113. #ifndef OPENSSL_NO_DES
  114. ALG("DESX-CBC:DESX", tdes_desx_cbc_functions),
  115. ALG("DES-ECB", des_ecb_functions),
  116. ALG("DES-CBC:DES", des_cbc_functions),
  117. ALG("DES-OFB", des_ofb64_functions),
  118. ALG("DES-CFB", des_cfb64_functions),
  119. ALG("DES-CFB1", des_cfb1_functions),
  120. ALG("DES-CFB8", des_cfb8_functions),
  121. #endif /* OPENSSL_NO_DES */
  122. { NULL, NULL, NULL }
  123. };
  124. static const OSSL_ALGORITHM *legacy_query(OSSL_PROVIDER *prov,
  125. int operation_id,
  126. int *no_cache)
  127. {
  128. *no_cache = 0;
  129. switch (operation_id) {
  130. case OSSL_OP_DIGEST:
  131. return legacy_digests;
  132. case OSSL_OP_CIPHER:
  133. return legacy_ciphers;
  134. }
  135. return NULL;
  136. }
  137. /* Functions we provide to the core */
  138. static const OSSL_DISPATCH legacy_dispatch_table[] = {
  139. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },
  140. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
  141. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
  142. { 0, NULL }
  143. };
  144. int OSSL_provider_init(const OSSL_PROVIDER *provider,
  145. const OSSL_DISPATCH *in,
  146. const OSSL_DISPATCH **out,
  147. void **provctx)
  148. {
  149. OSSL_core_get_library_context_fn *c_get_libctx = NULL;
  150. for (; in->function_id != 0; in++) {
  151. switch (in->function_id) {
  152. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  153. c_gettable_params = OSSL_get_core_gettable_params(in);
  154. break;
  155. case OSSL_FUNC_CORE_GET_PARAMS:
  156. c_get_params = OSSL_get_core_get_params(in);
  157. break;
  158. case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
  159. c_get_libctx = OSSL_get_core_get_library_context(in);
  160. break;
  161. /* Just ignore anything we don't understand */
  162. default:
  163. break;
  164. }
  165. }
  166. if (c_get_libctx == NULL)
  167. return 0;
  168. *out = legacy_dispatch_table;
  169. /*
  170. * We want to make sure that all calls from this provider that requires
  171. * a library context use the same context as the one used to call our
  172. * functions. We do that by passing it along as the provider context.
  173. */
  174. *provctx = c_get_libctx(provider);
  175. return 1;
  176. }