provider_util.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <openssl/evp.h>
  12. #include <openssl/core_names.h>
  13. #include <openssl/err.h>
  14. #include <openssl/proverr.h>
  15. #ifndef FIPS_MODULE
  16. # include <openssl/engine.h>
  17. # include "crypto/evp.h"
  18. #endif
  19. #include "prov/provider_util.h"
  20. void ossl_prov_cipher_reset(PROV_CIPHER *pc)
  21. {
  22. EVP_CIPHER_free(pc->alloc_cipher);
  23. pc->alloc_cipher = NULL;
  24. pc->cipher = NULL;
  25. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  26. ENGINE_finish(pc->engine);
  27. #endif
  28. pc->engine = NULL;
  29. }
  30. int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
  31. {
  32. if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
  33. return 0;
  34. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  35. if (src->engine != NULL && !ENGINE_init(src->engine)) {
  36. EVP_CIPHER_free(src->alloc_cipher);
  37. return 0;
  38. }
  39. #endif
  40. dst->engine = src->engine;
  41. dst->cipher = src->cipher;
  42. dst->alloc_cipher = src->alloc_cipher;
  43. return 1;
  44. }
  45. static int load_common(const OSSL_PARAM params[], const char **propquery,
  46. ENGINE **engine)
  47. {
  48. const OSSL_PARAM *p;
  49. *propquery = NULL;
  50. p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
  51. if (p != NULL) {
  52. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  53. return 0;
  54. *propquery = p->data;
  55. }
  56. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  57. ENGINE_finish(*engine);
  58. #endif
  59. *engine = NULL;
  60. /* Inside the FIPS module, we don't support legacy ciphers */
  61. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  62. p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
  63. if (p != NULL) {
  64. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  65. return 0;
  66. /* Get a structural reference */
  67. *engine = ENGINE_by_id(p->data);
  68. if (*engine == NULL)
  69. return 0;
  70. /* Get a functional reference */
  71. if (!ENGINE_init(*engine)) {
  72. ENGINE_free(*engine);
  73. *engine = NULL;
  74. return 0;
  75. }
  76. /* Free the structural reference */
  77. ENGINE_free(*engine);
  78. }
  79. #endif
  80. return 1;
  81. }
  82. int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
  83. const OSSL_PARAM params[],
  84. OSSL_LIB_CTX *ctx)
  85. {
  86. const OSSL_PARAM *p;
  87. const char *propquery;
  88. if (params == NULL)
  89. return 1;
  90. if (!load_common(params, &propquery, &pc->engine))
  91. return 0;
  92. p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
  93. if (p == NULL)
  94. return 1;
  95. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  96. return 0;
  97. EVP_CIPHER_free(pc->alloc_cipher);
  98. ERR_set_mark();
  99. pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
  100. #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
  101. if (pc->cipher == NULL) {
  102. const EVP_CIPHER *cipher;
  103. cipher = EVP_get_cipherbyname(p->data);
  104. /* Do not use global EVP_CIPHERs */
  105. if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
  106. pc->cipher = cipher;
  107. }
  108. #endif
  109. if (pc->cipher != NULL)
  110. ERR_pop_to_mark();
  111. else
  112. ERR_clear_last_mark();
  113. return pc->cipher != NULL;
  114. }
  115. const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
  116. {
  117. return pc->cipher;
  118. }
  119. ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
  120. {
  121. return pc->engine;
  122. }
  123. void ossl_prov_digest_reset(PROV_DIGEST *pd)
  124. {
  125. EVP_MD_free(pd->alloc_md);
  126. pd->alloc_md = NULL;
  127. pd->md = NULL;
  128. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  129. ENGINE_finish(pd->engine);
  130. #endif
  131. pd->engine = NULL;
  132. }
  133. int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
  134. {
  135. if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
  136. return 0;
  137. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  138. if (src->engine != NULL && !ENGINE_init(src->engine)) {
  139. EVP_MD_free(src->alloc_md);
  140. return 0;
  141. }
  142. #endif
  143. dst->engine = src->engine;
  144. dst->md = src->md;
  145. dst->alloc_md = src->alloc_md;
  146. return 1;
  147. }
  148. const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
  149. const char *mdname, const char *propquery)
  150. {
  151. EVP_MD_free(pd->alloc_md);
  152. pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
  153. return pd->md;
  154. }
  155. int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
  156. const OSSL_PARAM params[],
  157. OSSL_LIB_CTX *ctx)
  158. {
  159. const OSSL_PARAM *p;
  160. const char *propquery;
  161. if (params == NULL)
  162. return 1;
  163. if (!load_common(params, &propquery, &pd->engine))
  164. return 0;
  165. p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
  166. if (p == NULL)
  167. return 1;
  168. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  169. return 0;
  170. ERR_set_mark();
  171. ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
  172. #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
  173. if (pd->md == NULL) {
  174. const EVP_MD *md;
  175. md = EVP_get_digestbyname(p->data);
  176. /* Do not use global EVP_MDs */
  177. if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
  178. pd->md = md;
  179. }
  180. #endif
  181. if (pd->md != NULL)
  182. ERR_pop_to_mark();
  183. else
  184. ERR_clear_last_mark();
  185. return pd->md != NULL;
  186. }
  187. const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
  188. {
  189. return pd->md;
  190. }
  191. ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
  192. {
  193. return pd->engine;
  194. }
  195. int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
  196. const OSSL_PARAM params[],
  197. const char *ciphername,
  198. const char *mdname,
  199. const char *engine,
  200. const char *properties,
  201. const unsigned char *key,
  202. size_t keylen)
  203. {
  204. const OSSL_PARAM *p;
  205. OSSL_PARAM mac_params[6], *mp = mac_params;
  206. if (params != NULL) {
  207. if (mdname == NULL) {
  208. if ((p = OSSL_PARAM_locate_const(params,
  209. OSSL_ALG_PARAM_DIGEST)) != NULL) {
  210. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  211. return 0;
  212. mdname = p->data;
  213. }
  214. }
  215. if (ciphername == NULL) {
  216. if ((p = OSSL_PARAM_locate_const(params,
  217. OSSL_ALG_PARAM_CIPHER)) != NULL) {
  218. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  219. return 0;
  220. ciphername = p->data;
  221. }
  222. }
  223. if (engine == NULL) {
  224. if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
  225. != NULL) {
  226. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  227. return 0;
  228. engine = p->data;
  229. }
  230. }
  231. }
  232. if (mdname != NULL)
  233. *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  234. (char *)mdname, 0);
  235. if (ciphername != NULL)
  236. *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
  237. (char *)ciphername, 0);
  238. if (properties != NULL)
  239. *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
  240. (char *)properties, 0);
  241. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  242. if (engine != NULL)
  243. *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
  244. (char *) engine, 0);
  245. #endif
  246. if (key != NULL)
  247. *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  248. (unsigned char *)key,
  249. keylen);
  250. *mp = OSSL_PARAM_construct_end();
  251. return EVP_MAC_CTX_set_params(macctx, mac_params);
  252. }
  253. int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
  254. const OSSL_PARAM params[],
  255. const char *macname,
  256. const char *ciphername,
  257. const char *mdname,
  258. OSSL_LIB_CTX *libctx)
  259. {
  260. const OSSL_PARAM *p;
  261. const char *properties = NULL;
  262. if (macname == NULL
  263. && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
  264. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  265. return 0;
  266. macname = p->data;
  267. }
  268. if ((p = OSSL_PARAM_locate_const(params,
  269. OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
  270. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  271. return 0;
  272. properties = p->data;
  273. }
  274. /* If we got a new mac name, we make a new EVP_MAC_CTX */
  275. if (macname != NULL) {
  276. EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
  277. EVP_MAC_CTX_free(*macctx);
  278. *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
  279. /* The context holds on to the MAC */
  280. EVP_MAC_free(mac);
  281. if (*macctx == NULL)
  282. return 0;
  283. }
  284. /*
  285. * If there is no MAC yet (and therefore, no MAC context), we ignore
  286. * all other parameters.
  287. */
  288. if (*macctx == NULL)
  289. return 1;
  290. if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
  291. properties, NULL, 0))
  292. return 1;
  293. EVP_MAC_CTX_free(*macctx);
  294. *macctx = NULL;
  295. return 0;
  296. }
  297. void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
  298. OSSL_ALGORITHM *out)
  299. {
  300. int i, j;
  301. if (out[0].algorithm_names == NULL) {
  302. for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
  303. if (in[i].capable == NULL || in[i].capable())
  304. out[j++] = in[i].alg;
  305. }
  306. out[j++] = in[i].alg;
  307. }
  308. }
  309. /* Duplicate a lump of memory safely */
  310. int ossl_prov_memdup(const void *src, size_t src_len,
  311. unsigned char **dest, size_t *dest_len)
  312. {
  313. if (src != NULL) {
  314. if ((*dest = OPENSSL_memdup(src, src_len)) == NULL)
  315. return 0;
  316. *dest_len = src_len;
  317. } else {
  318. *dest = NULL;
  319. *dest_len = 0;
  320. }
  321. return 1;
  322. }