Modify providers that keep track of underlying algorithms
[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 }