Refactor TLS-PRF's kdf_tls1_prf_mkmacctx() to a provider utility
[openssl.git] / providers / common / provider_util.c
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
14 void 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;
20     pc->name[0] = '\0';
21 }
22
23 int 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;
30     OPENSSL_strlcpy(dst->name, src->name, sizeof(dst->name));
31     return 1;
32 }
33
34 static 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 */
49     /* Inside the FIPS module, we don't support legacy ciphers */
50 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
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
64 int 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);
82     OPENSSL_strlcpy(pc->name, p->data, sizeof(pc->name));
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
91 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
92 {
93     return pc->cipher;
94 }
95
96 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
97 {
98     return pc->engine;
99 }
100
101 const char *ossl_prov_cipher_name(const PROV_CIPHER *pc)
102 {
103     return pc->name;
104 }
105
106 void 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;
112     pd->name[0] = '\0';
113 }
114
115 int 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;
122     OPENSSL_strlcpy(dst->name, src->name, sizeof(dst->name));
123     return 1;
124 }
125
126 int 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);
145     OPENSSL_strlcpy(pd->name, p->data, sizeof(pd->name));
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
154 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
155 {
156     return pd->md;
157 }
158
159 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
160 {
161     return pd->engine;
162 }
163
164 const char *ossl_prov_digest_name(const PROV_DIGEST *pd)
165 {
166     return pd->name;
167 }
168
169 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
170                                       const OSSL_PARAM params[],
171                                       const char *macname,
172                                       const char *ciphername,
173                                       const char *mdname,
174                                       OPENSSL_CTX *libctx)
175 {
176     const OSSL_PARAM *p;
177     OSSL_PARAM mac_params[5], *mp = mac_params;
178     const char *properties = NULL;
179
180     if (macname == NULL
181         && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
182         if (p->data_type != OSSL_PARAM_UTF8_STRING)
183             return 0;
184         macname = p->data;
185     }
186     if ((p = OSSL_PARAM_locate_const(params,
187                                      OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
188         if (p->data_type != OSSL_PARAM_UTF8_STRING)
189             return 0;
190         properties = p->data;
191     }
192
193     /* If we got a new mac name, we make a new EVP_MAC_CTX */
194     if (macname != NULL) {
195         EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
196
197         EVP_MAC_CTX_free(*macctx);
198         *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
199         /* The context holds on to the MAC */
200         EVP_MAC_free(mac);
201         if (*macctx == NULL)
202             return 0;
203     }
204
205     /*
206      * If there is no MAC yet (and therefore, no MAC context), we ignore
207      * all other parameters.
208      */
209     if (*macctx == NULL)
210         return 1;
211
212     if (mdname == NULL) {
213         if ((p = OSSL_PARAM_locate_const(params,
214                                          OSSL_ALG_PARAM_DIGEST)) != NULL) {
215             if (p->data_type != OSSL_PARAM_UTF8_STRING)
216                 return 0;
217             mdname = p->data;
218         }
219     }
220     if (ciphername == NULL) {
221         if ((p = OSSL_PARAM_locate_const(params,
222                                          OSSL_ALG_PARAM_CIPHER)) != NULL) {
223             if (p->data_type != OSSL_PARAM_UTF8_STRING)
224                 return 0;
225             ciphername = p->data;
226         }
227     }
228
229     if (mdname != NULL)
230         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
231                                                  (char *)mdname, 0);
232     if (ciphername != NULL)
233         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
234                                                  (char *)ciphername, 0);
235     if (properties != NULL)
236         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
237                                                  (char *)properties, 0);
238
239 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
240     if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE)) != NULL) {
241         if (p->data_type != OSSL_PARAM_UTF8_STRING)
242             return 0;
243         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ENGINE,
244                                                  p->data, p->data_size);
245     }
246 #endif
247     *mp = OSSL_PARAM_construct_end();
248
249     if (EVP_MAC_CTX_set_params(*macctx, mac_params))
250         return 1;
251
252     EVP_MAC_CTX_free(*macctx);
253     *macctx = NULL;
254     return 0;
255 }