2
0

provider_util.c 9.1 KB

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