2
0

legacyprov.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2019-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/core.h>
  12. #include <openssl/core_dispatch.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/params.h>
  15. #include "prov/provider_ctx.h"
  16. #include "prov/implementations.h"
  17. #include "prov/providercommon.h"
  18. /*
  19. * Forward declarations to ensure that interface functions are correctly
  20. * defined.
  21. */
  22. static OSSL_FUNC_provider_gettable_params_fn legacy_gettable_params;
  23. static OSSL_FUNC_provider_get_params_fn legacy_get_params;
  24. static OSSL_FUNC_provider_query_operation_fn legacy_query;
  25. #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC }
  26. #ifdef STATIC_LEGACY
  27. OSSL_provider_init_fn ossl_legacy_provider_init;
  28. # define OSSL_provider_init ossl_legacy_provider_init
  29. #endif
  30. /* Functions provided by the core */
  31. static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
  32. static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
  33. /* Parameters we provide to the core */
  34. static const OSSL_PARAM legacy_param_types[] = {
  35. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
  36. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  37. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
  38. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
  39. OSSL_PARAM_END
  40. };
  41. static const OSSL_PARAM *legacy_gettable_params(void *provctx)
  42. {
  43. return legacy_param_types;
  44. }
  45. static int legacy_get_params(void *provctx, OSSL_PARAM params[])
  46. {
  47. OSSL_PARAM *p;
  48. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  49. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy 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 legacy_digests[] = {
  63. #ifndef OPENSSL_NO_MD2
  64. ALG("MD2", ossl_md2_functions),
  65. #endif
  66. #ifndef OPENSSL_NO_MD4
  67. ALG("MD4", ossl_md4_functions),
  68. #endif
  69. #ifndef OPENSSL_NO_MDC2
  70. ALG("MDC2", ossl_mdc2_functions),
  71. #endif /* OPENSSL_NO_MDC2 */
  72. #ifndef OPENSSL_NO_WHIRLPOOL
  73. ALG("WHIRLPOOL", ossl_wp_functions),
  74. #endif /* OPENSSL_NO_WHIRLPOOL */
  75. #ifndef OPENSSL_NO_RMD160
  76. ALG("RIPEMD-160:RIPEMD160:RIPEMD:RMD160", ossl_ripemd160_functions),
  77. #endif /* OPENSSL_NO_RMD160 */
  78. { NULL, NULL, NULL }
  79. };
  80. static const OSSL_ALGORITHM legacy_ciphers[] = {
  81. #ifndef OPENSSL_NO_CAST
  82. ALG("CAST5-ECB", ossl_cast5128ecb_functions),
  83. ALG("CAST5-CBC:CAST-CBC:CAST", ossl_cast5128cbc_functions),
  84. ALG("CAST5-OFB", ossl_cast5128ofb64_functions),
  85. ALG("CAST5-CFB", ossl_cast5128cfb64_functions),
  86. #endif /* OPENSSL_NO_CAST */
  87. #ifndef OPENSSL_NO_BF
  88. ALG("BF-ECB", ossl_blowfish128ecb_functions),
  89. ALG("BF-CBC:BF:BLOWFISH", ossl_blowfish128cbc_functions),
  90. ALG("BF-OFB", ossl_blowfish64ofb64_functions),
  91. ALG("BF-CFB", ossl_blowfish64cfb64_functions),
  92. #endif /* OPENSSL_NO_BF */
  93. #ifndef OPENSSL_NO_IDEA
  94. ALG("IDEA-ECB", ossl_idea128ecb_functions),
  95. ALG("IDEA-CBC:IDEA", ossl_idea128cbc_functions),
  96. ALG("IDEA-OFB:IDEA-OFB64", ossl_idea128ofb64_functions),
  97. ALG("IDEA-CFB:IDEA-CFB64", ossl_idea128cfb64_functions),
  98. #endif /* OPENSSL_NO_IDEA */
  99. #ifndef OPENSSL_NO_SEED
  100. ALG("SEED-ECB", ossl_seed128ecb_functions),
  101. ALG("SEED-CBC:SEED", ossl_seed128cbc_functions),
  102. ALG("SEED-OFB:SEED-OFB128", ossl_seed128ofb128_functions),
  103. ALG("SEED-CFB:SEED-CFB128", ossl_seed128cfb128_functions),
  104. #endif /* OPENSSL_NO_SEED */
  105. #ifndef OPENSSL_NO_RC2
  106. ALG("RC2-ECB", ossl_rc2128ecb_functions),
  107. ALG("RC2-CBC:RC2:RC2-128", ossl_rc2128cbc_functions),
  108. ALG("RC2-40-CBC:RC2-40", ossl_rc240cbc_functions),
  109. ALG("RC2-64-CBC:RC2-64", ossl_rc264cbc_functions),
  110. ALG("RC2-CFB", ossl_rc2128cfb128_functions),
  111. ALG("RC2-OFB", ossl_rc2128ofb128_functions),
  112. #endif /* OPENSSL_NO_RC2 */
  113. #ifndef OPENSSL_NO_RC4
  114. ALG("RC4", ossl_rc4128_functions),
  115. ALG("RC4-40", ossl_rc440_functions),
  116. # ifndef OPENSSL_NO_MD5
  117. ALG("RC4-HMAC-MD5", ossl_rc4_hmac_ossl_md5_functions),
  118. # endif /* OPENSSL_NO_MD5 */
  119. #endif /* OPENSSL_NO_RC4 */
  120. #ifndef OPENSSL_NO_RC5
  121. ALG("RC5-ECB", ossl_rc5128ecb_functions),
  122. ALG("RC5-CBC:RC5", ossl_rc5128cbc_functions),
  123. ALG("RC5-OFB", ossl_rc5128ofb64_functions),
  124. ALG("RC5-CFB", ossl_rc5128cfb64_functions),
  125. #endif /* OPENSSL_NO_RC5 */
  126. #ifndef OPENSSL_NO_DES
  127. ALG("DESX-CBC:DESX", ossl_tdes_desx_cbc_functions),
  128. ALG("DES-ECB", ossl_des_ecb_functions),
  129. ALG("DES-CBC:DES", ossl_des_cbc_functions),
  130. ALG("DES-OFB", ossl_des_ofb64_functions),
  131. ALG("DES-CFB", ossl_des_cfb64_functions),
  132. ALG("DES-CFB1", ossl_des_cfb1_functions),
  133. ALG("DES-CFB8", ossl_des_cfb8_functions),
  134. #endif /* OPENSSL_NO_DES */
  135. { NULL, NULL, NULL }
  136. };
  137. static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id,
  138. int *no_cache)
  139. {
  140. *no_cache = 0;
  141. switch (operation_id) {
  142. case OSSL_OP_DIGEST:
  143. return legacy_digests;
  144. case OSSL_OP_CIPHER:
  145. return legacy_ciphers;
  146. }
  147. return NULL;
  148. }
  149. static void legacy_teardown(void *provctx)
  150. {
  151. OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
  152. ossl_prov_ctx_free(provctx);
  153. }
  154. /* Functions we provide to the core */
  155. static const OSSL_DISPATCH legacy_dispatch_table[] = {
  156. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))legacy_teardown },
  157. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },
  158. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
  159. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
  160. { 0, NULL }
  161. };
  162. int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
  163. const OSSL_DISPATCH *in,
  164. const OSSL_DISPATCH **out,
  165. void **provctx)
  166. {
  167. OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
  168. OSSL_LIB_CTX *libctx = NULL;
  169. for (; in->function_id != 0; in++) {
  170. switch (in->function_id) {
  171. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  172. c_gettable_params = OSSL_FUNC_core_gettable_params(in);
  173. break;
  174. case OSSL_FUNC_CORE_GET_PARAMS:
  175. c_get_params = OSSL_FUNC_core_get_params(in);
  176. break;
  177. case OSSL_FUNC_CORE_GET_LIBCTX:
  178. c_get_libctx = OSSL_FUNC_core_get_libctx(in);
  179. break;
  180. /* Just ignore anything we don't understand */
  181. default:
  182. break;
  183. }
  184. }
  185. if (c_get_libctx == NULL)
  186. return 0;
  187. if ((*provctx = ossl_prov_ctx_new()) == NULL
  188. || (libctx = OSSL_LIB_CTX_new()) == NULL) {
  189. OSSL_LIB_CTX_free(libctx);
  190. legacy_teardown(*provctx);
  191. *provctx = NULL;
  192. return 0;
  193. }
  194. ossl_prov_ctx_set0_libctx(*provctx, libctx);
  195. ossl_prov_ctx_set0_handle(*provctx, handle);
  196. *out = legacy_dispatch_table;
  197. return 1;
  198. }