Configuration: make Solaris builds with gcc recognise GNU ld
[openssl.git] / crypto / evp / keymgmt_meth.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/crypto.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/evp.h>
13 #include <openssl/err.h>
14 #include "internal/provider.h"
15 #include "internal/refcount.h"
16 #include "crypto/evp.h"
17 #include "evp_local.h"
18
19
20 static void *keymgmt_new(void)
21 {
22     EVP_KEYMGMT *keymgmt = NULL;
23
24     if ((keymgmt = OPENSSL_zalloc(sizeof(*keymgmt))) == NULL
25         || (keymgmt->lock = CRYPTO_THREAD_lock_new()) == NULL) {
26         EVP_KEYMGMT_free(keymgmt);
27         EVPerr(0, ERR_R_MALLOC_FAILURE);
28         return NULL;
29     }
30
31     keymgmt->refcnt = 1;
32
33     return keymgmt;
34 }
35
36 static void *keymgmt_from_dispatch(int name_id,
37                                    const OSSL_DISPATCH *fns,
38                                    OSSL_PROVIDER *prov,
39                                    void *unused)
40 {
41     EVP_KEYMGMT *keymgmt = NULL;
42
43     if ((keymgmt = keymgmt_new()) == NULL) {
44         EVP_KEYMGMT_free(keymgmt);
45         return NULL;
46     }
47     keymgmt->name_id = name_id;
48
49     for (; fns->function_id != 0; fns++) {
50         switch (fns->function_id) {
51         case OSSL_FUNC_KEYMGMT_IMPORTDOMPARAMS:
52             if (keymgmt->importdomparams != NULL)
53                 break;
54             keymgmt->importdomparams =
55                 OSSL_get_OP_keymgmt_importdomparams(fns);
56             break;
57         case OSSL_FUNC_KEYMGMT_GENDOMPARAMS:
58             if (keymgmt->gendomparams != NULL)
59                 break;
60             keymgmt->gendomparams = OSSL_get_OP_keymgmt_gendomparams(fns);
61             break;
62         case OSSL_FUNC_KEYMGMT_FREEDOMPARAMS:
63             if (keymgmt->freedomparams != NULL)
64                 break;
65             keymgmt->freedomparams = OSSL_get_OP_keymgmt_freedomparams(fns);
66             break;
67         case OSSL_FUNC_KEYMGMT_EXPORTDOMPARAMS:
68             if (keymgmt->exportdomparams != NULL)
69                 break;
70             keymgmt->exportdomparams =
71                 OSSL_get_OP_keymgmt_exportdomparams(fns);
72             break;
73         case OSSL_FUNC_KEYMGMT_IMPORTDOMPARAM_TYPES:
74             if (keymgmt->importdomparam_types != NULL)
75                 break;
76             keymgmt->importdomparam_types =
77                 OSSL_get_OP_keymgmt_importdomparam_types(fns);
78             break;
79         case OSSL_FUNC_KEYMGMT_EXPORTDOMPARAM_TYPES:
80             if (keymgmt->exportdomparam_types != NULL)
81                 break;
82             keymgmt->exportdomparam_types =
83                 OSSL_get_OP_keymgmt_exportdomparam_types(fns);
84             break;
85         case OSSL_FUNC_KEYMGMT_IMPORTKEY:
86             if (keymgmt->importkey != NULL)
87                 break;
88             keymgmt->importkey = OSSL_get_OP_keymgmt_importkey(fns);
89             break;
90         case OSSL_FUNC_KEYMGMT_GENKEY:
91             if (keymgmt->genkey != NULL)
92                 break;
93             keymgmt->genkey = OSSL_get_OP_keymgmt_genkey(fns);
94             break;
95         case OSSL_FUNC_KEYMGMT_LOADKEY:
96             if (keymgmt->loadkey != NULL)
97                 break;
98             keymgmt->loadkey = OSSL_get_OP_keymgmt_loadkey(fns);
99             break;
100         case OSSL_FUNC_KEYMGMT_FREEKEY:
101             if (keymgmt->freekey != NULL)
102                 break;
103             keymgmt->freekey = OSSL_get_OP_keymgmt_freekey(fns);
104             break;
105         case OSSL_FUNC_KEYMGMT_EXPORTKEY:
106             if (keymgmt->exportkey != NULL)
107                 break;
108             keymgmt->exportkey = OSSL_get_OP_keymgmt_exportkey(fns);
109             break;
110         case OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES:
111             if (keymgmt->importkey_types != NULL)
112                 break;
113             keymgmt->importkey_types =
114                 OSSL_get_OP_keymgmt_importkey_types(fns);
115             break;
116         case OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES:
117             if (keymgmt->exportkey_types != NULL)
118                 break;
119             keymgmt->exportkey_types =
120                 OSSL_get_OP_keymgmt_exportkey_types(fns);
121             break;
122         }
123     }
124     /*
125      * Try to check that the method is sensible.
126      * It makes no sense being able to free stuff if you can't create it.
127      * It makes no sense providing OSSL_PARAM descriptors for import and
128      * export if you can't import or export.
129      */
130     if ((keymgmt->freedomparams != NULL
131          && (keymgmt->importdomparams == NULL
132              && keymgmt->gendomparams == NULL))
133         || (keymgmt->freekey != NULL
134             && (keymgmt->importkey == NULL
135                 && keymgmt->genkey == NULL
136                 && keymgmt->loadkey == NULL))
137         || (keymgmt->importdomparam_types != NULL
138             && keymgmt->importdomparams == NULL)
139         || (keymgmt->exportdomparam_types != NULL
140             && keymgmt->exportdomparams == NULL)
141         || (keymgmt->importkey_types != NULL
142             && keymgmt->importkey == NULL)
143         || (keymgmt->exportkey_types != NULL
144             && keymgmt->exportkey == NULL)) {
145         EVP_KEYMGMT_free(keymgmt);
146         EVPerr(0, EVP_R_INVALID_PROVIDER_FUNCTIONS);
147         return NULL;
148     }
149     keymgmt->prov = prov;
150     if (prov != NULL)
151         ossl_provider_up_ref(prov);
152
153     return keymgmt;
154 }
155
156 EVP_KEYMGMT *evp_keymgmt_fetch_by_number(OPENSSL_CTX *ctx, int name_id,
157                                          const char *properties)
158 {
159     return evp_generic_fetch_by_number(ctx,
160                                        OSSL_OP_KEYMGMT, name_id, properties,
161                                        keymgmt_from_dispatch, NULL,
162                                        (int (*)(void *))EVP_KEYMGMT_up_ref,
163                                        (void (*)(void *))EVP_KEYMGMT_free);
164 }
165
166 EVP_KEYMGMT *EVP_KEYMGMT_fetch(OPENSSL_CTX *ctx, const char *algorithm,
167                                const char *properties)
168 {
169     return evp_generic_fetch(ctx, OSSL_OP_KEYMGMT, algorithm, properties,
170                              keymgmt_from_dispatch, NULL,
171                              (int (*)(void *))EVP_KEYMGMT_up_ref,
172                              (void (*)(void *))EVP_KEYMGMT_free);
173 }
174
175 int EVP_KEYMGMT_up_ref(EVP_KEYMGMT *keymgmt)
176 {
177     int ref = 0;
178
179     CRYPTO_UP_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
180     return 1;
181 }
182
183 void EVP_KEYMGMT_free(EVP_KEYMGMT *keymgmt)
184 {
185     int ref = 0;
186
187     if (keymgmt == NULL)
188         return;
189
190     CRYPTO_DOWN_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
191     if (ref > 0)
192         return;
193     ossl_provider_free(keymgmt->prov);
194     CRYPTO_THREAD_lock_free(keymgmt->lock);
195     OPENSSL_free(keymgmt);
196 }
197
198 const OSSL_PROVIDER *EVP_KEYMGMT_provider(const EVP_KEYMGMT *keymgmt)
199 {
200     return keymgmt->prov;
201 }
202
203 int EVP_KEYMGMT_number(const EVP_KEYMGMT *keymgmt)
204 {
205     return keymgmt->name_id;
206 }
207
208 int EVP_KEYMGMT_is_a(const EVP_KEYMGMT *keymgmt, const char *name)
209 {
210     return evp_is_a(keymgmt->prov, keymgmt->name_id, name);
211 }
212
213 void EVP_KEYMGMT_do_all_provided(OPENSSL_CTX *libctx,
214                                  void (*fn)(EVP_KEYMGMT *keymgmt, void *arg),
215                                  void *arg)
216 {
217     evp_generic_do_all(libctx, OSSL_OP_KEYMGMT,
218                        (void (*)(void *, void *))fn, arg,
219                        keymgmt_from_dispatch, NULL,
220                        (void (*)(void *))EVP_KEYMGMT_free);
221 }
222
223 void EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT *keymgmt,
224                               void (*fn)(const char *name, void *data),
225                               void *data)
226 {
227     if (keymgmt->prov != NULL)
228         evp_names_do_all(keymgmt->prov, keymgmt->name_id, fn, data);
229 }