defltprov.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/opensslconf.h>
  12. #include <openssl/core.h>
  13. #include <openssl/core_numbers.h>
  14. #include <openssl/core_names.h>
  15. #include <openssl/params.h>
  16. #include "internal/provider_algs.h"
  17. /* Functions provided by the core */
  18. static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
  19. static OSSL_core_get_params_fn *c_get_params = NULL;
  20. /* Parameters we provide to the core */
  21. static const OSSL_PARAM deflt_param_types[] = {
  22. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
  23. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
  24. OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
  25. OSSL_PARAM_END
  26. };
  27. static const OSSL_PARAM *deflt_gettable_params(const OSSL_PROVIDER *prov)
  28. {
  29. return deflt_param_types;
  30. }
  31. static int deflt_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
  32. {
  33. OSSL_PARAM *p;
  34. p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
  35. if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Default 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 deflt_digests[] = {
  46. { "SHA1", "default=yes", sha1_functions },
  47. { "SHA224", "default=yes", sha224_functions },
  48. { "SHA256", "default=yes", sha256_functions },
  49. { "SHA384", "default=yes", sha384_functions },
  50. { "SHA512", "default=yes", sha512_functions },
  51. { "SHA512-224", "default=yes", sha512_224_functions },
  52. { "SHA512-256", "default=yes", sha512_256_functions },
  53. { "SHA3-224", "default=yes", sha3_224_functions },
  54. { "SHA3-256", "default=yes", sha3_256_functions },
  55. { "SHA3-384", "default=yes", sha3_384_functions },
  56. { "SHA3-512", "default=yes", sha3_512_functions },
  57. /*
  58. * KECCAK_KMAC128 and KECCAK_KMAC256 as hashes are mostly useful for
  59. * the KMAC128 and KMAC256.
  60. */
  61. { "KECCAK_KMAC128", "default=yes", keccak_kmac_128_functions },
  62. { "KECCAK_KMAC256", "default=yes", keccak_kmac_256_functions },
  63. { "SHAKE128", "default=yes", shake_128_functions },
  64. { "SHAKE256", "default=yes", shake_256_functions },
  65. #ifndef OPENSSL_NO_BLAKE2
  66. { "BLAKE2s256", "default=yes", blake2s256_functions },
  67. { "BLAKE2b512", "default=yes", blake2b512_functions },
  68. #endif /* OPENSSL_NO_BLAKE2 */
  69. #ifndef OPENSSL_NO_SM3
  70. { "SM3", "default=yes", sm3_functions },
  71. #endif /* OPENSSL_NO_SM3 */
  72. #ifndef OPENSSL_NO_MD5
  73. { "MD5", "default=yes", md5_functions },
  74. { "MD5-SHA1", "default=yes", md5_sha1_functions },
  75. #endif /* OPENSSL_NO_MD5 */
  76. { NULL, NULL, NULL }
  77. };
  78. static const OSSL_ALGORITHM deflt_ciphers[] = {
  79. { "AES-256-ECB", "default=yes", aes256ecb_functions },
  80. { "AES-192-ECB", "default=yes", aes192ecb_functions },
  81. { "AES-128-ECB", "default=yes", aes128ecb_functions },
  82. { "AES-256-CBC", "default=yes", aes256cbc_functions },
  83. { "AES-192-CBC", "default=yes", aes192cbc_functions },
  84. { "AES-128-CBC", "default=yes", aes128cbc_functions },
  85. { "AES-256-OFB", "default=yes", aes256ofb_functions },
  86. { "AES-192-OFB", "default=yes", aes192ofb_functions },
  87. { "AES-128-OFB", "default=yes", aes128ofb_functions },
  88. { "AES-256-CFB", "default=yes", aes256cfb_functions },
  89. { "AES-192-CFB", "default=yes", aes192cfb_functions },
  90. { "AES-128-CFB", "default=yes", aes128cfb_functions },
  91. { "AES-256-CFB1", "default=yes", aes256cfb1_functions },
  92. { "AES-192-CFB1", "default=yes", aes192cfb1_functions },
  93. { "AES-128-CFB1", "default=yes", aes128cfb1_functions },
  94. { "AES-256-CFB8", "default=yes", aes256cfb8_functions },
  95. { "AES-192-CFB8", "default=yes", aes192cfb8_functions },
  96. { "AES-128-CFB8", "default=yes", aes128cfb8_functions },
  97. { "AES-256-CTR", "default=yes", aes256ctr_functions },
  98. { "AES-192-CTR", "default=yes", aes192ctr_functions },
  99. { "AES-128-CTR", "default=yes", aes128ctr_functions },
  100. { "AES-256-XTS", "default=yes", aes256xts_functions },
  101. { "AES-128-XTS", "default=yes", aes128xts_functions },
  102. #ifndef OPENSSL_NO_OCB
  103. { "AES-256-OCB", "default=yes", aes256ocb_functions },
  104. { "AES-192-OCB", "default=yes", aes192ocb_functions },
  105. { "AES-128-OCB", "default=yes", aes128ocb_functions },
  106. #endif /* OPENSSL_NO_OCB */
  107. /* TODO(3.0) Add aliases when they are supported */
  108. { "id-aes256-GCM", "default=yes", aes256gcm_functions },
  109. { "id-aes192-GCM", "default=yes", aes192gcm_functions },
  110. { "id-aes128-GCM", "default=yes", aes128gcm_functions },
  111. { "id-aes256-CCM", "default=yes", aes256ccm_functions },
  112. { "id-aes192-CCM", "default=yes", aes192ccm_functions },
  113. { "id-aes128-CCM", "default=yes", aes128ccm_functions },
  114. { "id-aes256-wrap", "default=yes", aes256wrap_functions },
  115. { "id-aes192-wrap", "default=yes", aes192wrap_functions },
  116. { "id-aes128-wrap", "default=yes", aes128wrap_functions },
  117. { "id-aes256-wrap-pad", "default=yes", aes256wrappad_functions },
  118. { "id-aes192-wrap-pad", "default=yes", aes192wrappad_functions },
  119. { "id-aes128-wrap-pad", "default=yes", aes128wrappad_functions },
  120. #ifndef OPENSSL_NO_ARIA
  121. { "ARIA-256-GCM", "default=yes", aria256gcm_functions },
  122. { "ARIA-192-GCM", "default=yes", aria192gcm_functions },
  123. { "ARIA-128-GCM", "default=yes", aria128gcm_functions },
  124. { "ARIA-256-CCM", "default=yes", aria256ccm_functions },
  125. { "ARIA-192-CCM", "default=yes", aria192ccm_functions },
  126. { "ARIA-128-CCM", "default=yes", aria128ccm_functions },
  127. { "ARIA-256-ECB", "default=yes", aria256ecb_functions },
  128. { "ARIA-192-ECB", "default=yes", aria192ecb_functions },
  129. { "ARIA-128-ECB", "default=yes", aria128ecb_functions },
  130. { "ARIA-256-CBC", "default=yes", aria256cbc_functions },
  131. { "ARIA-192-CBC", "default=yes", aria192cbc_functions },
  132. { "ARIA-128-CBC", "default=yes", aria128cbc_functions },
  133. { "ARIA-256-OFB", "default=yes", aria256ofb_functions },
  134. { "ARIA-192-OFB", "default=yes", aria192ofb_functions },
  135. { "ARIA-128-OFB", "default=yes", aria128ofb_functions },
  136. { "ARIA-256-CFB", "default=yes", aria256cfb_functions },
  137. { "ARIA-192-CFB", "default=yes", aria192cfb_functions },
  138. { "ARIA-128-CFB", "default=yes", aria128cfb_functions },
  139. { "ARIA-256-CFB1", "default=yes", aria256cfb1_functions },
  140. { "ARIA-192-CFB1", "default=yes", aria192cfb1_functions },
  141. { "ARIA-128-CFB1", "default=yes", aria128cfb1_functions },
  142. { "ARIA-256-CFB8", "default=yes", aria256cfb8_functions },
  143. { "ARIA-192-CFB8", "default=yes", aria192cfb8_functions },
  144. { "ARIA-128-CFB8", "default=yes", aria128cfb8_functions },
  145. { "ARIA-256-CTR", "default=yes", aria256ctr_functions },
  146. { "ARIA-192-CTR", "default=yes", aria192ctr_functions },
  147. { "ARIA-128-CTR", "default=yes", aria128ctr_functions },
  148. #endif /* OPENSSL_NO_ARIA */
  149. #ifndef OPENSSL_NO_CAMELLIA
  150. { "CAMELLIA-256-ECB", "default=yes", camellia256ecb_functions },
  151. { "CAMELLIA-192-ECB", "default=yes", camellia192ecb_functions },
  152. { "CAMELLIA-128-ECB", "default=yes", camellia128ecb_functions },
  153. { "CAMELLIA-256-CBC", "default=yes", camellia256cbc_functions },
  154. { "CAMELLIA-192-CBC", "default=yes", camellia192cbc_functions },
  155. { "CAMELLIA-128-CBC", "default=yes", camellia128cbc_functions },
  156. { "CAMELLIA-256-OFB", "default=yes", camellia256ofb_functions },
  157. { "CAMELLIA-192-OFB", "default=yes", camellia192ofb_functions },
  158. { "CAMELLIA-128-OFB", "default=yes", camellia128ofb_functions },
  159. { "CAMELLIA-256-CFB", "default=yes", camellia256cfb_functions },
  160. { "CAMELLIA-192-CFB", "default=yes", camellia192cfb_functions },
  161. { "CAMELLIA-128-CFB", "default=yes", camellia128cfb_functions },
  162. { "CAMELLIA-256-CFB1", "default=yes", camellia256cfb1_functions },
  163. { "CAMELLIA-192-CFB1", "default=yes", camellia192cfb1_functions },
  164. { "CAMELLIA-128-CFB1", "default=yes", camellia128cfb1_functions },
  165. { "CAMELLIA-256-CFB8", "default=yes", camellia256cfb8_functions },
  166. { "CAMELLIA-192-CFB8", "default=yes", camellia192cfb8_functions },
  167. { "CAMELLIA-128-CFB8", "default=yes", camellia128cfb8_functions },
  168. { "CAMELLIA-256-CTR", "default=yes", camellia256ctr_functions },
  169. { "CAMELLIA-192-CTR", "default=yes", camellia192ctr_functions },
  170. { "CAMELLIA-128-CTR", "default=yes", camellia128ctr_functions },
  171. #endif /* OPENSSL_NO_CAMELLIA */
  172. #ifndef OPENSSL_NO_DES
  173. { "DES-EDE3", "default=yes", tdes_ede3_ecb_functions },
  174. { "DES-EDE3-CBC", "default=yes", tdes_ede3_cbc_functions },
  175. { "DES-EDE3-OFB", "default=yes", tdes_ede3_ofb_functions },
  176. { "DES-EDE3-CFB", "default=yes", tdes_ede3_cfb_functions },
  177. { "DES-EDE3-CFB8", "default=yes", tdes_ede3_cfb8_functions },
  178. { "DES-EDE3-CFB1", "default=yes", tdes_ede3_cfb1_functions },
  179. { "DES-EDE", "default=yes", tdes_ede2_ecb_functions },
  180. { "DES-EDE-CBC", "default=yes", tdes_ede2_cbc_functions },
  181. { "DES-EDE-OFB", "default=yes", tdes_ede2_ofb_functions },
  182. { "DES-EDE-CFB", "default=yes", tdes_ede2_cfb_functions },
  183. { "DESX-CBC", "default=yes", tdes_desx_cbc_functions },
  184. { "id-smime-alg-CMS3DESwrap", "default=yes", tdes_wrap_cbc_functions },
  185. { "DES-ECB", "default=yes", des_ecb_functions },
  186. { "DES-CBC", "default=yes", des_cbc_functions },
  187. { "DES-OFB", "default=yes", des_ofb64_functions },
  188. { "DES-CFB", "default=yes", des_cfb64_functions },
  189. { "DES-CFB1", "default=yes", des_cfb1_functions },
  190. { "DES-CFB8", "default=yes", des_cfb8_functions },
  191. #endif /* OPENSSL_NO_DES */
  192. #ifndef OPENSSL_NO_BF
  193. { "BF-ECB", "default=yes", blowfish128ecb_functions },
  194. { "BF-CBC", "default=yes", blowfish128cbc_functions },
  195. { "BF-OFB", "default=yes", blowfish64ofb64_functions },
  196. { "BF-CFB", "default=yes", blowfish64cfb64_functions },
  197. #endif /* OPENSSL_NO_BF */
  198. #ifndef OPENSSL_NO_IDEA
  199. { "IDEA-ECB", "default=yes", idea128ecb_functions },
  200. { "IDEA-CBC", "default=yes", idea128cbc_functions },
  201. { "IDEA-OFB", "default=yes", idea128ofb64_functions },
  202. { "IDEA-CFB", "default=yes", idea128cfb64_functions },
  203. #endif /* OPENSSL_NO_IDEA */
  204. #ifndef OPENSSL_NO_CAST
  205. { "CAST5-ECB", "default=yes", cast5128ecb_functions },
  206. { "CAST5-CBC", "default=yes", cast5128cbc_functions },
  207. { "CAST5-OFB", "default=yes", cast564ofb64_functions },
  208. { "CAST5-CFB", "default=yes", cast564cfb64_functions },
  209. #endif /* OPENSSL_NO_CAST */
  210. #ifndef OPENSSL_NO_SEED
  211. { "SEED-ECB", "default=yes", seed128ecb_functions },
  212. { "SEED-CBC", "default=yes", seed128cbc_functions },
  213. { "SEED-OFB", "default=yes", seed128ofb128_functions },
  214. { "SEED-CFB", "default=yes", seed128cfb128_functions },
  215. #endif /* OPENSSL_NO_SEED */
  216. #ifndef OPENSSL_NO_SM4
  217. { "SM4-ECB", "default=yes", sm4128ecb_functions },
  218. { "SM4-CBC", "default=yes", sm4128cbc_functions },
  219. { "SM4-CTR", "default=yes", sm4128ctr_functions },
  220. { "SM4-OFB", "default=yes", sm4128ofb128_functions },
  221. { "SM4-CFB", "default=yes", sm4128cfb128_functions },
  222. #endif /* OPENSSL_NO_SM4 */
  223. #ifndef OPENSSL_NO_RC4
  224. { "RC4", "default=yes", rc4128_functions },
  225. { "RC4-40", "default=yes", rc440_functions },
  226. #endif /* OPENSSL_NO_RC4 */
  227. #ifndef OPENSSL_NO_RC5
  228. { "RC5-ECB", "default=yes", rc5128ecb_functions },
  229. { "RC5-CBC", "default=yes", rc5128cbc_functions },
  230. { "RC5-OFB", "default=yes", rc5128ofb64_functions },
  231. { "RC5-CFB", "default=yes", rc5128cfb64_functions },
  232. #endif /* OPENSSL_NO_RC5 */
  233. { NULL, NULL, NULL }
  234. };
  235. static const OSSL_ALGORITHM deflt_macs[] = {
  236. #ifndef OPENSSL_NO_BLAKE2
  237. { "BLAKE2BMAC", "default=yes", blake2bmac_functions },
  238. { "BLAKE2SMAC", "default=yes", blake2smac_functions },
  239. #endif
  240. #ifndef OPENSSL_NO_CMAC
  241. { "CMAC", "default=yes", cmac_functions },
  242. #endif
  243. { "GMAC", "default=yes", gmac_functions },
  244. { "HMAC", "default=yes", hmac_functions },
  245. { "KMAC128", "default=yes", kmac128_functions },
  246. { "KMAC256", "default=yes", kmac256_functions },
  247. #ifndef OPENSSL_NO_SIPHASH
  248. { "SipHash", "default=yes", siphash_functions },
  249. #endif
  250. #ifndef OPENSSL_NO_POLY1305
  251. { "Poly1305", "default=yes", poly1305_functions },
  252. #endif
  253. { NULL, NULL, NULL }
  254. };
  255. static const OSSL_ALGORITHM deflt_kdfs[] = {
  256. { OSSL_KDF_NAME_HKDF, "default=yes", kdf_hkdf_functions },
  257. { OSSL_KDF_NAME_SSKDF, "default=yes", kdf_sskdf_functions },
  258. { OSSL_KDF_NAME_PBKDF2, "default=yes", kdf_pbkdf2_functions },
  259. { OSSL_KDF_NAME_SSHKDF, "default=yes", kdf_sshkdf_functions },
  260. { OSSL_KDF_NAME_X963KDF, "default=yes", kdf_x963_kdf_functions },
  261. { OSSL_KDF_NAME_TLS1_PRF, "default=yes", kdf_tls1_prf_functions },
  262. { OSSL_KDF_NAME_KBKDF, "default=yes", kdf_kbkdf_functions },
  263. #ifndef OPENSSL_NO_CMS
  264. { OSSL_KDF_NAME_X942KDF, "default=yes", kdf_x942_kdf_functions },
  265. #endif
  266. #ifndef OPENSSL_NO_SCRYPT
  267. { OSSL_KDF_NAME_SCRYPT, "default=yes", kdf_scrypt_functions },
  268. #endif
  269. { NULL, NULL, NULL }
  270. };
  271. static const OSSL_ALGORITHM deflt_keyexch[] = {
  272. #ifndef OPENSSL_NO_DH
  273. { "dhKeyAgreement", "default=yes", dh_keyexch_functions },
  274. #endif
  275. { NULL, NULL, NULL }
  276. };
  277. static const OSSL_ALGORITHM deflt_signature[] = {
  278. #ifndef OPENSSL_NO_DSA
  279. { "DSA", "default=yes", dsa_signature_functions },
  280. #endif
  281. { NULL, NULL, NULL }
  282. };
  283. static const OSSL_ALGORITHM deflt_keymgmt[] = {
  284. #ifndef OPENSSL_NO_DH
  285. { "dhKeyAgreement", "default=yes", dh_keymgmt_functions },
  286. #endif
  287. #ifndef OPENSSL_NO_DSA
  288. { "DSA", "default=yes", dsa_keymgmt_functions },
  289. #endif
  290. { NULL, NULL, NULL }
  291. };
  292. static const OSSL_ALGORITHM *deflt_query(OSSL_PROVIDER *prov,
  293. int operation_id,
  294. int *no_cache)
  295. {
  296. *no_cache = 0;
  297. switch (operation_id) {
  298. case OSSL_OP_DIGEST:
  299. return deflt_digests;
  300. case OSSL_OP_CIPHER:
  301. return deflt_ciphers;
  302. case OSSL_OP_MAC:
  303. return deflt_macs;
  304. case OSSL_OP_KDF:
  305. return deflt_kdfs;
  306. case OSSL_OP_KEYMGMT:
  307. return deflt_keymgmt;
  308. case OSSL_OP_KEYEXCH:
  309. return deflt_keyexch;
  310. case OSSL_OP_SIGNATURE:
  311. return deflt_signature;
  312. }
  313. return NULL;
  314. }
  315. /* Functions we provide to the core */
  316. static const OSSL_DISPATCH deflt_dispatch_table[] = {
  317. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
  318. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
  319. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
  320. { 0, NULL }
  321. };
  322. OSSL_provider_init_fn ossl_default_provider_init;
  323. int ossl_default_provider_init(const OSSL_PROVIDER *provider,
  324. const OSSL_DISPATCH *in,
  325. const OSSL_DISPATCH **out,
  326. void **provctx)
  327. {
  328. OSSL_core_get_library_context_fn *c_get_libctx = NULL;
  329. for (; in->function_id != 0; in++) {
  330. switch (in->function_id) {
  331. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  332. c_gettable_params = OSSL_get_core_gettable_params(in);
  333. break;
  334. case OSSL_FUNC_CORE_GET_PARAMS:
  335. c_get_params = OSSL_get_core_get_params(in);
  336. break;
  337. case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
  338. c_get_libctx = OSSL_get_core_get_library_context(in);
  339. break;
  340. default:
  341. /* Just ignore anything we don't understand */
  342. break;
  343. }
  344. }
  345. if (c_get_libctx == NULL)
  346. return 0;
  347. *out = deflt_dispatch_table;
  348. /*
  349. * We want to make sure that all calls from this provider that requires
  350. * a library context use the same context as the one used to call our
  351. * functions. We do that by passing it along as the provider context.
  352. */
  353. *provctx = c_get_libctx(provider);
  354. return 1;
  355. }