EVP: Make the KEYEXCH implementation leaner
[openssl.git] / crypto / evp / exchange.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/evp.h>
12 #include <openssl/err.h>
13 #include "internal/refcount.h"
14 #include "crypto/evp.h"
15 #include "internal/provider.h"
16 #include "internal/numbers.h"   /* includes SIZE_MAX */
17 #include "evp_local.h"
18
19 static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
20 {
21     EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
22
23     exchange->lock = CRYPTO_THREAD_lock_new();
24     if (exchange->lock == NULL) {
25         OPENSSL_free(exchange);
26         return NULL;
27     }
28     exchange->prov = prov;
29     ossl_provider_up_ref(prov);
30     exchange->refcnt = 1;
31
32     return exchange;
33 }
34
35 static void *evp_keyexch_from_dispatch(int name_id,
36                                        const OSSL_DISPATCH *fns,
37                                        OSSL_PROVIDER *prov,
38                                        void *unused)
39 {
40     EVP_KEYEXCH *exchange = NULL;
41     int fncnt = 0, paramfncnt = 0;
42
43     if ((exchange = evp_keyexch_new(prov)) == NULL) {
44         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
45         goto err;
46     }
47
48     exchange->name_id = name_id;
49
50     for (; fns->function_id != 0; fns++) {
51         switch (fns->function_id) {
52         case OSSL_FUNC_KEYEXCH_NEWCTX:
53             if (exchange->newctx != NULL)
54                 break;
55             exchange->newctx = OSSL_get_OP_keyexch_newctx(fns);
56             fncnt++;
57             break;
58         case OSSL_FUNC_KEYEXCH_INIT:
59             if (exchange->init != NULL)
60                 break;
61             exchange->init = OSSL_get_OP_keyexch_init(fns);
62             fncnt++;
63             break;
64         case OSSL_FUNC_KEYEXCH_SET_PEER:
65             if (exchange->set_peer != NULL)
66                 break;
67             exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
68             break;
69         case OSSL_FUNC_KEYEXCH_DERIVE:
70             if (exchange->derive != NULL)
71                 break;
72             exchange->derive = OSSL_get_OP_keyexch_derive(fns);
73             fncnt++;
74             break;
75         case OSSL_FUNC_KEYEXCH_FREECTX:
76             if (exchange->freectx != NULL)
77                 break;
78             exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
79             fncnt++;
80             break;
81         case OSSL_FUNC_KEYEXCH_DUPCTX:
82             if (exchange->dupctx != NULL)
83                 break;
84             exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
85             break;
86         case OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS:
87             if (exchange->set_ctx_params != NULL)
88                 break;
89             exchange->set_ctx_params = OSSL_get_OP_keyexch_set_ctx_params(fns);
90             paramfncnt++;
91             break;
92         case OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS:
93             if (exchange->settable_ctx_params != NULL)
94                 break;
95             exchange->settable_ctx_params
96                 = OSSL_get_OP_keyexch_settable_ctx_params(fns);
97             paramfncnt++;
98             break;
99         }
100     }
101     if (fncnt != 4 || (paramfncnt != 0 && paramfncnt != 2)) {
102         /*
103          * In order to be a consistent set of functions we must have at least
104          * a complete set of "exchange" functions: init, derive, newctx,
105          * and freectx. The set_ctx_params and settable_ctx_params functions are
106          * optional, but if one of them is present then the other one must also
107          * be present. The dupctx and set_peer functions are optional.
108          */
109         EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
110                EVP_R_INVALID_PROVIDER_FUNCTIONS);
111         goto err;
112     }
113
114     return exchange;
115
116  err:
117     EVP_KEYEXCH_free(exchange);
118     return NULL;
119 }
120
121 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
122 {
123     if (exchange != NULL) {
124         int i;
125
126         CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
127         if (i > 0)
128             return;
129         ossl_provider_free(exchange->prov);
130         CRYPTO_THREAD_lock_free(exchange->lock);
131         OPENSSL_free(exchange);
132     }
133 }
134
135 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
136 {
137     int ref = 0;
138
139     CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
140     return 1;
141 }
142
143 OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
144 {
145     return exchange->prov;
146 }
147
148 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
149                                const char *properties)
150 {
151     EVP_KEYEXCH *keyexch = NULL;
152
153     keyexch = evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
154                                 evp_keyexch_from_dispatch, NULL,
155                                 (int (*)(void *))EVP_KEYEXCH_up_ref,
156                                 (void (*)(void *))EVP_KEYEXCH_free);
157
158     return keyexch;
159 }
160
161 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
162 {
163     int ret;
164     void *provkey = NULL;
165     EVP_KEYEXCH *exchange = NULL;
166
167     if (ctx == NULL) {
168         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
169         return -2;
170     }
171
172     evp_pkey_ctx_free_old_ops(ctx);
173     ctx->operation = EVP_PKEY_OP_DERIVE;
174
175     if (ctx->engine != NULL || ctx->algorithm == NULL)
176         goto legacy;
177
178     /*
179      * Because we cleared out old ops, we shouldn't need to worry about
180      * checking if exchange is already there.  Keymgmt is a different
181      * matter, as it isn't tied to a specific EVP_PKEY op.
182      */
183     exchange = EVP_KEYEXCH_fetch(ctx->libctx, ctx->algorithm, ctx->propquery);
184     if (exchange != NULL && ctx->keymgmt == NULL) {
185         int name_id = EVP_KEYEXCH_number(exchange);
186
187         ctx->keymgmt =
188             evp_keymgmt_fetch_by_number(ctx->libctx, name_id, ctx->propquery);
189     }
190
191     if (ctx->keymgmt == NULL
192         || exchange == NULL
193         || (EVP_KEYMGMT_provider(ctx->keymgmt)
194             != EVP_KEYEXCH_provider(exchange))) {
195         /*
196          * We don't have the full support we need with provided methods,
197          * let's go see if legacy does.  Also, we don't need to free
198          * ctx->keymgmt here, as it's not necessarily tied to this
199          * operation.  It will be freed by EVP_PKEY_CTX_free().
200          */
201         EVP_KEYEXCH_free(exchange);
202         goto legacy;
203     }
204
205
206     ctx->op.kex.exchange = exchange;
207
208     if (ctx->pkey != NULL) {
209         provkey = evp_keymgmt_export_to_provider(ctx->pkey, ctx->keymgmt, 0);
210         if (provkey == NULL) {
211             EVPerr(0, EVP_R_INITIALIZATION_ERROR);
212             goto err;
213         }
214     }
215     ctx->op.kex.exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
216     if (ctx->op.kex.exchprovctx == NULL) {
217         /* The provider key can stay in the cache */
218         EVPerr(0, EVP_R_INITIALIZATION_ERROR);
219         goto err;
220     }
221     ret = exchange->init(ctx->op.kex.exchprovctx, provkey);
222
223     return ret ? 1 : 0;
224  err:
225     ctx->operation = EVP_PKEY_OP_UNDEFINED;
226     return 0;
227
228  legacy:
229     if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
230         EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
231         return -2;
232     }
233
234     if (ctx->pmeth->derive_init == NULL)
235         return 1;
236     ret = ctx->pmeth->derive_init(ctx);
237     if (ret <= 0)
238         ctx->operation = EVP_PKEY_OP_UNDEFINED;
239     return ret;
240 }
241
242 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
243 {
244     int ret;
245     void *provkey = NULL;
246
247     if (ctx == NULL) {
248         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
249                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
250         return -2;
251     }
252
253     if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx) || ctx->op.kex.exchprovctx == NULL)
254         goto legacy;
255
256     if (ctx->op.kex.exchange->set_peer == NULL) {
257         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
258                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
259         return -2;
260     }
261
262     provkey = evp_keymgmt_export_to_provider(peer, ctx->keymgmt, 0);
263     if (provkey == NULL) {
264         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
265         return 0;
266     }
267     return ctx->op.kex.exchange->set_peer(ctx->op.kex.exchprovctx, provkey);
268
269  legacy:
270     if (ctx->pmeth == NULL
271         || !(ctx->pmeth->derive != NULL
272              || ctx->pmeth->encrypt != NULL
273              || ctx->pmeth->decrypt != NULL)
274         || ctx->pmeth->ctrl == NULL) {
275         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
276                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
277         return -2;
278     }
279     if (ctx->operation != EVP_PKEY_OP_DERIVE
280         && ctx->operation != EVP_PKEY_OP_ENCRYPT
281         && ctx->operation != EVP_PKEY_OP_DECRYPT) {
282         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
283                EVP_R_OPERATON_NOT_INITIALIZED);
284         return -1;
285     }
286
287     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
288
289     if (ret <= 0)
290         return ret;
291
292     if (ret == 2)
293         return 1;
294
295     if (ctx->pkey == NULL) {
296         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
297         return -1;
298     }
299
300     if (ctx->pkey->type != peer->type) {
301         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
302         return -1;
303     }
304
305     /*
306      * For clarity.  The error is if parameters in peer are
307      * present (!missing) but don't match.  EVP_PKEY_cmp_parameters may return
308      * 1 (match), 0 (don't match) and -2 (comparison is not defined).  -1
309      * (different key types) is impossible here because it is checked earlier.
310      * -2 is OK for us here, as well as 1, so we can check for 0 only.
311      */
312     if (!EVP_PKEY_missing_parameters(peer) &&
313         !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
314         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
315         return -1;
316     }
317
318     EVP_PKEY_free(ctx->peerkey);
319     ctx->peerkey = peer;
320
321     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
322
323     if (ret <= 0) {
324         ctx->peerkey = NULL;
325         return ret;
326     }
327
328     EVP_PKEY_up_ref(peer);
329     return 1;
330 }
331
332 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
333 {
334     int ret;
335
336     if (ctx == NULL) {
337         EVPerr(EVP_F_EVP_PKEY_DERIVE,
338                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
339         return -2;
340     }
341
342     if (!EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
343         EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
344         return -1;
345     }
346
347     if (ctx->op.kex.exchprovctx == NULL)
348         goto legacy;
349
350     ret = ctx->op.kex.exchange->derive(ctx->op.kex.exchprovctx, key, pkeylen,
351                                        SIZE_MAX);
352
353     return ret;
354  legacy:
355     if (ctx ==  NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
356         EVPerr(EVP_F_EVP_PKEY_DERIVE,
357                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
358         return -2;
359     }
360
361     M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
362         return ctx->pmeth->derive(ctx, key, pkeylen);
363 }
364
365 int EVP_KEYEXCH_number(const EVP_KEYEXCH *keyexch)
366 {
367     return keyexch->name_id;
368 }
369
370 int EVP_KEYEXCH_is_a(const EVP_KEYEXCH *keyexch, const char *name)
371 {
372     return evp_is_a(keyexch->prov, keyexch->name_id, name);
373 }
374
375 void EVP_KEYEXCH_do_all_provided(OPENSSL_CTX *libctx,
376                                  void (*fn)(EVP_KEYEXCH *keyexch, void *arg),
377                                  void *arg)
378 {
379     evp_generic_do_all(libctx, OSSL_OP_KEYEXCH,
380                        (void (*)(void *, void *))fn, arg,
381                        evp_keyexch_from_dispatch, NULL,
382                        (void (*)(void *))EVP_KEYEXCH_free);
383 }
384
385 void EVP_KEYEXCH_names_do_all(const EVP_KEYEXCH *keyexch,
386                               void (*fn)(const char *name, void *data),
387                               void *data)
388 {
389     if (keyexch->prov != NULL)
390         evp_names_do_all(keyexch->prov, keyexch->name_id, fn, data);
391 }