legacyprov.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright 2019-2022 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/err.h>
  15. #include <openssl/params.h>
  16. #include "prov/provider_ctx.h"
  17. #include "prov/implementations.h"
  18. #include "prov/names.h"
  19. #include "prov/providercommon.h"
  20. /*
  21. * Forward declarations to ensure that interface functions are correctly
  22. * defined.
  23. */
  24. static OSSL_FUNC_provider_gettable_params_fn legacy_gettable_params;
  25. static OSSL_FUNC_provider_get_params_fn legacy_get_params;
  26. static OSSL_FUNC_provider_query_operation_fn legacy_query;
  27. #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC }
  28. #ifdef STATIC_LEGACY
  29. OSSL_provider_init_fn ossl_legacy_provider_init;
  30. # define OSSL_provider_init ossl_legacy_provider_init
  31. #endif
  32. #ifndef STATIC_LEGACY
  33. /*
  34. * Should these function pointers be stored in the provider side provctx?
  35. * Could they ever be different from one init to the next? We assume not for
  36. * now.
  37. */
  38. /* Functions provided by the core */
  39. static OSSL_FUNC_core_new_error_fn *c_new_error;
  40. static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
  41. static OSSL_FUNC_core_vset_error_fn *c_vset_error;
  42. static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
  43. static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
  44. static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
  45. #endif
  46. /* Parameters we provide to the core */
  47. static const OSSL_PARAM legacy_param_types[] = {
  48. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
  49. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  50. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
  51. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
  52. OSSL_PARAM_END
  53. };
  54. static const OSSL_PARAM *legacy_gettable_params(void *provctx)
  55. {
  56. return legacy_param_types;
  57. }
  58. static int legacy_get_params(void *provctx, OSSL_PARAM params[])
  59. {
  60. OSSL_PARAM *p;
  61. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  62. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))
  63. return 0;
  64. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  65. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  66. return 0;
  67. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  68. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  69. return 0;
  70. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
  71. if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
  72. return 0;
  73. return 1;
  74. }
  75. static const OSSL_ALGORITHM legacy_digests[] = {
  76. #ifndef OPENSSL_NO_MD2
  77. ALG(PROV_NAMES_MD2, ossl_md2_functions),
  78. #endif
  79. #ifndef OPENSSL_NO_MD4
  80. ALG(PROV_NAMES_MD4, ossl_md4_functions),
  81. #endif
  82. #ifndef OPENSSL_NO_MDC2
  83. ALG(PROV_NAMES_MDC2, ossl_mdc2_functions),
  84. #endif /* OPENSSL_NO_MDC2 */
  85. #ifndef OPENSSL_NO_WHIRLPOOL
  86. ALG(PROV_NAMES_WHIRLPOOL, ossl_wp_functions),
  87. #endif /* OPENSSL_NO_WHIRLPOOL */
  88. #ifndef OPENSSL_NO_RMD160
  89. ALG(PROV_NAMES_RIPEMD_160, ossl_ripemd160_functions),
  90. #endif /* OPENSSL_NO_RMD160 */
  91. { NULL, NULL, NULL }
  92. };
  93. static const OSSL_ALGORITHM legacy_ciphers[] = {
  94. #ifndef OPENSSL_NO_CAST
  95. ALG(PROV_NAMES_CAST5_ECB, ossl_cast5128ecb_functions),
  96. ALG(PROV_NAMES_CAST5_CBC, ossl_cast5128cbc_functions),
  97. ALG(PROV_NAMES_CAST5_OFB, ossl_cast5128ofb64_functions),
  98. ALG(PROV_NAMES_CAST5_CFB, ossl_cast5128cfb64_functions),
  99. #endif /* OPENSSL_NO_CAST */
  100. #ifndef OPENSSL_NO_BF
  101. ALG(PROV_NAMES_BF_ECB, ossl_blowfish128ecb_functions),
  102. ALG(PROV_NAMES_BF_CBC, ossl_blowfish128cbc_functions),
  103. ALG(PROV_NAMES_BF_OFB, ossl_blowfish128ofb64_functions),
  104. ALG(PROV_NAMES_BF_CFB, ossl_blowfish128cfb64_functions),
  105. #endif /* OPENSSL_NO_BF */
  106. #ifndef OPENSSL_NO_IDEA
  107. ALG(PROV_NAMES_IDEA_ECB, ossl_idea128ecb_functions),
  108. ALG(PROV_NAMES_IDEA_CBC, ossl_idea128cbc_functions),
  109. ALG(PROV_NAMES_IDEA_OFB, ossl_idea128ofb64_functions),
  110. ALG(PROV_NAMES_IDEA_CFB, ossl_idea128cfb64_functions),
  111. #endif /* OPENSSL_NO_IDEA */
  112. #ifndef OPENSSL_NO_SEED
  113. ALG(PROV_NAMES_SEED_ECB, ossl_seed128ecb_functions),
  114. ALG(PROV_NAMES_SEED_CBC, ossl_seed128cbc_functions),
  115. ALG(PROV_NAMES_SEED_OFB, ossl_seed128ofb128_functions),
  116. ALG(PROV_NAMES_SEED_CFB, ossl_seed128cfb128_functions),
  117. #endif /* OPENSSL_NO_SEED */
  118. #ifndef OPENSSL_NO_RC2
  119. ALG(PROV_NAMES_RC2_ECB, ossl_rc2128ecb_functions),
  120. ALG(PROV_NAMES_RC2_CBC, ossl_rc2128cbc_functions),
  121. ALG(PROV_NAMES_RC2_40_CBC, ossl_rc240cbc_functions),
  122. ALG(PROV_NAMES_RC2_64_CBC, ossl_rc264cbc_functions),
  123. ALG(PROV_NAMES_RC2_CFB, ossl_rc2128cfb128_functions),
  124. ALG(PROV_NAMES_RC2_OFB, ossl_rc2128ofb128_functions),
  125. #endif /* OPENSSL_NO_RC2 */
  126. #ifndef OPENSSL_NO_RC4
  127. ALG(PROV_NAMES_RC4, ossl_rc4128_functions),
  128. ALG(PROV_NAMES_RC4_40, ossl_rc440_functions),
  129. # ifndef OPENSSL_NO_MD5
  130. ALG(PROV_NAMES_RC4_HMAC_MD5, ossl_rc4_hmac_ossl_md5_functions),
  131. # endif /* OPENSSL_NO_MD5 */
  132. #endif /* OPENSSL_NO_RC4 */
  133. #ifndef OPENSSL_NO_RC5
  134. ALG(PROV_NAMES_RC5_ECB, ossl_rc5128ecb_functions),
  135. ALG(PROV_NAMES_RC5_CBC, ossl_rc5128cbc_functions),
  136. ALG(PROV_NAMES_RC5_OFB, ossl_rc5128ofb64_functions),
  137. ALG(PROV_NAMES_RC5_CFB, ossl_rc5128cfb64_functions),
  138. #endif /* OPENSSL_NO_RC5 */
  139. #ifndef OPENSSL_NO_DES
  140. ALG(PROV_NAMES_DESX_CBC, ossl_tdes_desx_cbc_functions),
  141. ALG(PROV_NAMES_DES_ECB, ossl_des_ecb_functions),
  142. ALG(PROV_NAMES_DES_CBC, ossl_des_cbc_functions),
  143. ALG(PROV_NAMES_DES_OFB, ossl_des_ofb64_functions),
  144. ALG(PROV_NAMES_DES_CFB, ossl_des_cfb64_functions),
  145. ALG(PROV_NAMES_DES_CFB1, ossl_des_cfb1_functions),
  146. ALG(PROV_NAMES_DES_CFB8, ossl_des_cfb8_functions),
  147. #endif /* OPENSSL_NO_DES */
  148. { NULL, NULL, NULL }
  149. };
  150. static const OSSL_ALGORITHM legacy_kdfs[] = {
  151. ALG(PROV_NAMES_PBKDF1, ossl_kdf_pbkdf1_functions),
  152. ALG(PROV_NAMES_PVKKDF, ossl_kdf_pvk_functions),
  153. { NULL, NULL, NULL }
  154. };
  155. static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id,
  156. int *no_cache)
  157. {
  158. *no_cache = 0;
  159. switch (operation_id) {
  160. case OSSL_OP_DIGEST:
  161. return legacy_digests;
  162. case OSSL_OP_CIPHER:
  163. return legacy_ciphers;
  164. case OSSL_OP_KDF:
  165. return legacy_kdfs;
  166. }
  167. return NULL;
  168. }
  169. static void legacy_teardown(void *provctx)
  170. {
  171. OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
  172. ossl_prov_ctx_free(provctx);
  173. }
  174. /* Functions we provide to the core */
  175. static const OSSL_DISPATCH legacy_dispatch_table[] = {
  176. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))legacy_teardown },
  177. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },
  178. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
  179. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
  180. OSSL_DISPATCH_END
  181. };
  182. int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
  183. const OSSL_DISPATCH *in,
  184. const OSSL_DISPATCH **out,
  185. void **provctx)
  186. {
  187. OSSL_LIB_CTX *libctx = NULL;
  188. #ifndef STATIC_LEGACY
  189. const OSSL_DISPATCH *tmp;
  190. #endif
  191. #ifndef STATIC_LEGACY
  192. for (tmp = in; tmp->function_id != 0; tmp++) {
  193. /*
  194. * We do not support the scenario of an application linked against
  195. * multiple versions of libcrypto (e.g. one static and one dynamic),
  196. * but sharing a single legacy.so. We do a simple sanity check here.
  197. */
  198. #define set_func(c, f) if (c == NULL) c = f; else if (c != f) return 0;
  199. switch (tmp->function_id) {
  200. case OSSL_FUNC_CORE_NEW_ERROR:
  201. set_func(c_new_error, OSSL_FUNC_core_new_error(tmp));
  202. break;
  203. case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
  204. set_func(c_set_error_debug, OSSL_FUNC_core_set_error_debug(tmp));
  205. break;
  206. case OSSL_FUNC_CORE_VSET_ERROR:
  207. set_func(c_vset_error, OSSL_FUNC_core_vset_error(tmp));
  208. break;
  209. case OSSL_FUNC_CORE_SET_ERROR_MARK:
  210. set_func(c_set_error_mark, OSSL_FUNC_core_set_error_mark(tmp));
  211. break;
  212. case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
  213. set_func(c_clear_last_error_mark,
  214. OSSL_FUNC_core_clear_last_error_mark(tmp));
  215. break;
  216. case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
  217. set_func(c_pop_error_to_mark, OSSL_FUNC_core_pop_error_to_mark(tmp));
  218. break;
  219. }
  220. }
  221. #endif
  222. if ((*provctx = ossl_prov_ctx_new()) == NULL
  223. || (libctx = OSSL_LIB_CTX_new_child(handle, in)) == NULL) {
  224. OSSL_LIB_CTX_free(libctx);
  225. legacy_teardown(*provctx);
  226. *provctx = NULL;
  227. return 0;
  228. }
  229. ossl_prov_ctx_set0_libctx(*provctx, libctx);
  230. ossl_prov_ctx_set0_handle(*provctx, handle);
  231. *out = legacy_dispatch_table;
  232. return 1;
  233. }
  234. #ifndef STATIC_LEGACY
  235. /*
  236. * Provider specific implementation of libcrypto functions in terms of
  237. * upcalls.
  238. */
  239. /*
  240. * For ERR functions, we pass a NULL context. This is valid to do as long
  241. * as only error codes that the calling libcrypto supports are used.
  242. */
  243. void ERR_new(void)
  244. {
  245. c_new_error(NULL);
  246. }
  247. void ERR_set_debug(const char *file, int line, const char *func)
  248. {
  249. c_set_error_debug(NULL, file, line, func);
  250. }
  251. void ERR_set_error(int lib, int reason, const char *fmt, ...)
  252. {
  253. va_list args;
  254. va_start(args, fmt);
  255. c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
  256. va_end(args);
  257. }
  258. void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
  259. {
  260. c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
  261. }
  262. int ERR_set_mark(void)
  263. {
  264. return c_set_error_mark(NULL);
  265. }
  266. int ERR_clear_last_mark(void)
  267. {
  268. return c_clear_last_error_mark(NULL);
  269. }
  270. int ERR_pop_to_mark(void)
  271. {
  272. return c_pop_error_to_mark(NULL);
  273. }
  274. #endif