]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/common/provider_util.c
Modify providers that keep track of underlying algorithms
[thirdparty/openssl.git] / providers / common / provider_util.c
CommitLineData
2f17cc49
P
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
10#include <openssl/evp.h>
11#include <openssl/core_names.h>
12#include "internal/provider_util.h"
13
14void ossl_prov_cipher_reset(PROV_CIPHER *pc)
15{
16 EVP_CIPHER_free(pc->alloc_cipher);
17 pc->alloc_cipher = NULL;
18 pc->cipher = NULL;
19 pc->engine = NULL;
7cfa1717 20 pc->name[0] = '\0';
2f17cc49
P
21}
22
23int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
24{
25 if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
26 return 0;
27 dst->engine = src->engine;
28 dst->cipher = src->cipher;
29 dst->alloc_cipher = src->alloc_cipher;
7cfa1717 30 OPENSSL_strlcpy(dst->name, src->name, sizeof(dst->name));
2f17cc49
P
31 return 1;
32}
33
34static int load_common(const OSSL_PARAM params[], const char **propquery,
35 ENGINE **engine)
36{
37 const OSSL_PARAM *p;
38
39 *propquery = NULL;
40 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
41 if (p != NULL) {
42 if (p->data_type != OSSL_PARAM_UTF8_STRING)
43 return 0;
44 *propquery = p->data;
45 }
46
47 *engine = NULL;
48 /* TODO legacy stuff, to be removed */
252a3665
MC
49 /* Inside the FIPS module, we don't support legacy ciphers */
50#if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
2f17cc49
P
51 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
52 if (p != NULL) {
53 if (p->data_type != OSSL_PARAM_UTF8_STRING)
54 return 0;
55 ENGINE_finish(*engine);
56 *engine = ENGINE_by_id(p->data);
57 if (*engine == NULL)
58 return 0;
59 }
60#endif
61 return 1;
62}
63
64int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
65 const OSSL_PARAM params[],
66 OPENSSL_CTX *ctx)
67{
68 const OSSL_PARAM *p;
69 const char *propquery;
70
71 if (!load_common(params, &propquery, &pc->engine))
72 return 0;
73
74 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
75 if (p == NULL)
76 return 1;
77 if (p->data_type != OSSL_PARAM_UTF8_STRING)
78 return 0;
79
80 EVP_CIPHER_free(pc->alloc_cipher);
81 pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
7cfa1717 82 OPENSSL_strlcpy(pc->name, p->data, sizeof(pc->name));
2f17cc49
P
83 /* TODO legacy stuff, to be removed */
84#ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
85 if (pc->cipher == NULL)
86 pc->cipher = EVP_get_cipherbyname(p->data);
87#endif
88 return pc->cipher != NULL;
89}
90
91const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
92{
93 return pc->cipher;
94}
95
96ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
97{
98 return pc->engine;
99}
100
7cfa1717
RL
101const char *ossl_prov_cipher_name(const PROV_CIPHER *pc)
102{
103 return pc->name;
104}
105
2f17cc49
P
106void ossl_prov_digest_reset(PROV_DIGEST *pd)
107{
108 EVP_MD_free(pd->alloc_md);
109 pd->alloc_md = NULL;
110 pd->md = NULL;
111 pd->engine = NULL;
7cfa1717 112 pd->name[0] = '\0';
2f17cc49
P
113}
114
115int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
116{
117 if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
118 return 0;
119 dst->engine = src->engine;
120 dst->md = src->md;
121 dst->alloc_md = src->alloc_md;
7cfa1717 122 OPENSSL_strlcpy(dst->name, src->name, sizeof(dst->name));
2f17cc49
P
123 return 1;
124}
125
126int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
127 const OSSL_PARAM params[],
128 OPENSSL_CTX *ctx)
129{
130 const OSSL_PARAM *p;
131 const char *propquery;
132
133 if (!load_common(params, &propquery, &pd->engine))
134 return 0;
135
136
137 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
138 if (p == NULL)
139 return 1;
140 if (p->data_type != OSSL_PARAM_UTF8_STRING)
141 return 0;
142
143 EVP_MD_free(pd->alloc_md);
144 pd->md = pd->alloc_md = EVP_MD_fetch(ctx, p->data, propquery);
7cfa1717 145 OPENSSL_strlcpy(pd->name, p->data, sizeof(pd->name));
2f17cc49
P
146 /* TODO legacy stuff, to be removed */
147#ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
148 if (pd->md == NULL)
149 pd->md = EVP_get_digestbyname(p->data);
150#endif
151 return pd->md != NULL;
152}
153
154const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
155{
156 return pd->md;
157}
158
159ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
160{
161 return pd->engine;
162}
163
7cfa1717
RL
164const char *ossl_prov_digest_name(const PROV_DIGEST *pd)
165{
166 return pd->name;
167}