legacyprov.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2019-2021 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/names.h"
  18. #include "prov/providercommon.h"
  19. /*
  20. * Forward declarations to ensure that interface functions are correctly
  21. * defined.
  22. */
  23. static OSSL_FUNC_provider_gettable_params_fn legacy_gettable_params;
  24. static OSSL_FUNC_provider_get_params_fn legacy_get_params;
  25. static OSSL_FUNC_provider_query_operation_fn legacy_query;
  26. #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC }
  27. #ifdef STATIC_LEGACY
  28. OSSL_provider_init_fn ossl_legacy_provider_init;
  29. # define OSSL_provider_init ossl_legacy_provider_init
  30. #endif
  31. /* Parameters we provide to the core */
  32. static const OSSL_PARAM legacy_param_types[] = {
  33. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
  34. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  35. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
  36. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
  37. OSSL_PARAM_END
  38. };
  39. static const OSSL_PARAM *legacy_gettable_params(void *provctx)
  40. {
  41. return legacy_param_types;
  42. }
  43. static int legacy_get_params(void *provctx, OSSL_PARAM params[])
  44. {
  45. OSSL_PARAM *p;
  46. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  47. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))
  48. return 0;
  49. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
  50. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
  51. return 0;
  52. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
  53. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
  54. return 0;
  55. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
  56. if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
  57. return 0;
  58. return 1;
  59. }
  60. static const OSSL_ALGORITHM legacy_digests[] = {
  61. #ifndef OPENSSL_NO_MD2
  62. ALG(PROV_NAMES_MD2, ossl_md2_functions),
  63. #endif
  64. #ifndef OPENSSL_NO_MD4
  65. ALG(PROV_NAMES_MD4, ossl_md4_functions),
  66. #endif
  67. #ifndef OPENSSL_NO_MDC2
  68. ALG(PROV_NAMES_MDC2, ossl_mdc2_functions),
  69. #endif /* OPENSSL_NO_MDC2 */
  70. #ifndef OPENSSL_NO_WHIRLPOOL
  71. ALG(PROV_NAMES_WHIRLPOOL, ossl_wp_functions),
  72. #endif /* OPENSSL_NO_WHIRLPOOL */
  73. #ifndef OPENSSL_NO_RMD160
  74. ALG(PROV_NAMES_RIPEMD_160, ossl_ripemd160_functions),
  75. #endif /* OPENSSL_NO_RMD160 */
  76. { NULL, NULL, NULL }
  77. };
  78. static const OSSL_ALGORITHM legacy_ciphers[] = {
  79. #ifndef OPENSSL_NO_CAST
  80. ALG(PROV_NAMES_CAST5_ECB, ossl_cast5128ecb_functions),
  81. ALG(PROV_NAMES_CAST5_CBC, ossl_cast5128cbc_functions),
  82. ALG(PROV_NAMES_CAST5_OFB, ossl_cast5128ofb64_functions),
  83. ALG(PROV_NAMES_CAST5_CFB, ossl_cast5128cfb64_functions),
  84. #endif /* OPENSSL_NO_CAST */
  85. #ifndef OPENSSL_NO_BF
  86. ALG(PROV_NAMES_BF_ECB, ossl_blowfish128ecb_functions),
  87. ALG(PROV_NAMES_BF_CBC, ossl_blowfish128cbc_functions),
  88. ALG(PROV_NAMES_BF_OFB, ossl_blowfish64ofb64_functions),
  89. ALG(PROV_NAMES_BF_CFB, ossl_blowfish64cfb64_functions),
  90. #endif /* OPENSSL_NO_BF */
  91. #ifndef OPENSSL_NO_IDEA
  92. ALG(PROV_NAMES_IDEA_ECB, ossl_idea128ecb_functions),
  93. ALG(PROV_NAMES_IDEA_CBC, ossl_idea128cbc_functions),
  94. ALG(PROV_NAMES_IDEA_OFB, ossl_idea128ofb64_functions),
  95. ALG(PROV_NAMES_IDEA_CFB, ossl_idea128cfb64_functions),
  96. #endif /* OPENSSL_NO_IDEA */
  97. #ifndef OPENSSL_NO_SEED
  98. ALG(PROV_NAMES_SEED_ECB, ossl_seed128ecb_functions),
  99. ALG(PROV_NAMES_SEED_CBC, ossl_seed128cbc_functions),
  100. ALG(PROV_NAMES_SEED_OFB, ossl_seed128ofb128_functions),
  101. ALG(PROV_NAMES_SEED_CFB, ossl_seed128cfb128_functions),
  102. #endif /* OPENSSL_NO_SEED */
  103. #ifndef OPENSSL_NO_RC2
  104. ALG(PROV_NAMES_RC2_ECB, ossl_rc2128ecb_functions),
  105. ALG(PROV_NAMES_RC2_CBC, ossl_rc2128cbc_functions),
  106. ALG(PROV_NAMES_RC2_40_CBC, ossl_rc240cbc_functions),
  107. ALG(PROV_NAMES_RC2_64_CBC, ossl_rc264cbc_functions),
  108. ALG(PROV_NAMES_RC2_CFB, ossl_rc2128cfb128_functions),
  109. ALG(PROV_NAMES_RC2_OFB, ossl_rc2128ofb128_functions),
  110. #endif /* OPENSSL_NO_RC2 */
  111. #ifndef OPENSSL_NO_RC4
  112. ALG(PROV_NAMES_RC4, ossl_rc4128_functions),
  113. ALG(PROV_NAMES_RC4_40, ossl_rc440_functions),
  114. # ifndef OPENSSL_NO_MD5
  115. ALG(PROV_NAMES_RC4_HMAC_MD5, ossl_rc4_hmac_ossl_md5_functions),
  116. # endif /* OPENSSL_NO_MD5 */
  117. #endif /* OPENSSL_NO_RC4 */
  118. #ifndef OPENSSL_NO_RC5
  119. ALG(PROV_NAMES_RC5_ECB, ossl_rc5128ecb_functions),
  120. ALG(PROV_NAMES_RC5_CBC, ossl_rc5128cbc_functions),
  121. ALG(PROV_NAMES_RC5_OFB, ossl_rc5128ofb64_functions),
  122. ALG(PROV_NAMES_RC5_CFB, ossl_rc5128cfb64_functions),
  123. #endif /* OPENSSL_NO_RC5 */
  124. #ifndef OPENSSL_NO_DES
  125. ALG(PROV_NAMES_DESX_CBC, ossl_tdes_desx_cbc_functions),
  126. ALG(PROV_NAMES_DES_ECB, ossl_des_ecb_functions),
  127. ALG(PROV_NAMES_DES_CBC, ossl_des_cbc_functions),
  128. ALG(PROV_NAMES_DES_OFB, ossl_des_ofb64_functions),
  129. ALG(PROV_NAMES_DES_CFB, ossl_des_cfb64_functions),
  130. ALG(PROV_NAMES_DES_CFB1, ossl_des_cfb1_functions),
  131. ALG(PROV_NAMES_DES_CFB8, ossl_des_cfb8_functions),
  132. #endif /* OPENSSL_NO_DES */
  133. { NULL, NULL, NULL }
  134. };
  135. static const OSSL_ALGORITHM legacy_kdfs[] = {
  136. ALG("PBKDF1", ossl_kdf_pbkdf1_functions),
  137. { NULL, NULL, NULL }
  138. };
  139. static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id,
  140. int *no_cache)
  141. {
  142. *no_cache = 0;
  143. switch (operation_id) {
  144. case OSSL_OP_DIGEST:
  145. return legacy_digests;
  146. case OSSL_OP_CIPHER:
  147. return legacy_ciphers;
  148. case OSSL_OP_KDF:
  149. return legacy_kdfs;
  150. }
  151. return NULL;
  152. }
  153. static void legacy_teardown(void *provctx)
  154. {
  155. OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
  156. ossl_prov_ctx_free(provctx);
  157. }
  158. /* Functions we provide to the core */
  159. static const OSSL_DISPATCH legacy_dispatch_table[] = {
  160. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))legacy_teardown },
  161. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },
  162. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
  163. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
  164. { 0, NULL }
  165. };
  166. int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
  167. const OSSL_DISPATCH *in,
  168. const OSSL_DISPATCH **out,
  169. void **provctx)
  170. {
  171. OSSL_LIB_CTX *libctx = NULL;
  172. if ((*provctx = ossl_prov_ctx_new()) == NULL
  173. || (libctx = OSSL_LIB_CTX_new_child(handle, in)) == NULL) {
  174. OSSL_LIB_CTX_free(libctx);
  175. legacy_teardown(*provctx);
  176. *provctx = NULL;
  177. return 0;
  178. }
  179. ossl_prov_ctx_set0_libctx(*provctx, libctx);
  180. ossl_prov_ctx_set0_handle(*provctx, handle);
  181. *out = legacy_dispatch_table;
  182. return 1;
  183. }