Unify the digest getting code inside providers.
[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 }
21
22 int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
23 {
24     if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
25         return 0;
26     dst->engine = src->engine;
27     dst->cipher = src->cipher;
28     dst->alloc_cipher = src->alloc_cipher;
29     return 1;
30 }
31
32 static int load_common(const OSSL_PARAM params[], const char **propquery,
33                        ENGINE **engine)
34 {
35     const OSSL_PARAM *p;
36
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
45     *engine = NULL;
46     /* TODO legacy stuff, to be removed */
47 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
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
61 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
62                                       const OSSL_PARAM params[],
63                                       OPENSSL_CTX *ctx)
64 {
65     const OSSL_PARAM *p;
66     const char *propquery;
67
68     if (!load_common(params, &propquery, &pc->engine))
69         return 0;
70
71     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
72     if (p == NULL)
73         return 1;
74     if (p->data_type != OSSL_PARAM_UTF8_STRING)
75         return 0;
76
77     EVP_CIPHER_free(pc->alloc_cipher);
78     pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
79     /* TODO legacy stuff, to be removed */
80 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
81     if (pc->cipher == NULL)
82         pc->cipher = EVP_get_cipherbyname(p->data);
83 #endif
84     return pc->cipher != NULL;
85 }
86
87 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
88 {
89     return pc->cipher;
90 }
91
92 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
93 {
94     return pc->engine;
95 }
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
105 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
106 {
107     if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
108         return 0;
109     dst->engine = src->engine;
110     dst->md = src->md;
111     dst->alloc_md = src->alloc_md;
112     return 1;
113 }
114
115 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
116                                       const OSSL_PARAM params[],
117                                       OPENSSL_CTX *ctx)
118 {
119     const OSSL_PARAM *p;
120     const char *propquery;
121
122     if (!load_common(params, &propquery, &pd->engine))
123         return 0;
124
125
126     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
127     if (p == NULL)
128         return 1;
129     if (p->data_type != OSSL_PARAM_UTF8_STRING)
130         return 0;
131
132     EVP_MD_free(pd->alloc_md);
133     pd->md = pd->alloc_md = EVP_MD_fetch(ctx, p->data, propquery);
134     /* TODO legacy stuff, to be removed */
135 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
136     if (pd->md == NULL)
137         pd->md = EVP_get_digestbyname(p->data);
138 #endif
139     return pd->md != NULL;
140 }
141
142 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
143 {
144     return pd->md;
145 }
146
147 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
148 {
149     return pd->engine;
150 }
151