Adapt DH to use with KEYMGMT
[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 "internal/evp_int.h"
15 #include "internal/provider.h"
16 #include "evp_locl.h"
17
18 static EVP_KEYEXCH *evp_keyexch_new(OSSL_PROVIDER *prov)
19 {
20     EVP_KEYEXCH *exchange = OPENSSL_zalloc(sizeof(EVP_KEYEXCH));
21
22     exchange->lock = CRYPTO_THREAD_lock_new();
23     if (exchange->lock == NULL) {
24         OPENSSL_free(exchange);
25         return NULL;
26     }
27     exchange->prov = prov;
28     ossl_provider_up_ref(prov);
29     exchange->refcnt = 1;
30
31     return exchange;
32 }
33
34 static void *evp_keyexch_from_dispatch(const char *name,
35                                        const OSSL_DISPATCH *fns,
36                                        OSSL_PROVIDER *prov)
37 {
38     EVP_KEYEXCH *exchange = NULL;
39     int fncnt = 0;
40
41     if ((exchange = evp_keyexch_new(prov)) == NULL
42         || (exchange->name = OPENSSL_strdup(name)) == NULL) {
43         EVP_KEYEXCH_free(exchange);
44         EVPerr(0, ERR_R_MALLOC_FAILURE);
45         return NULL;
46     }
47
48     for (; fns->function_id != 0; fns++) {
49         switch (fns->function_id) {
50         case OSSL_FUNC_KEYEXCH_NEWCTX:
51             if (exchange->newctx != NULL)
52                 break;
53             exchange->newctx = OSSL_get_OP_keyexch_newctx(fns);
54             fncnt++;
55             break;
56         case OSSL_FUNC_KEYEXCH_INIT:
57             if (exchange->init != NULL)
58                 break;
59             exchange->init = OSSL_get_OP_keyexch_init(fns);
60             fncnt++;
61             break;
62         case OSSL_FUNC_KEYEXCH_SET_PEER:
63             if (exchange->set_peer != NULL)
64                 break;
65             exchange->set_peer = OSSL_get_OP_keyexch_set_peer(fns);
66             break;
67         case OSSL_FUNC_KEYEXCH_DERIVE:
68             if (exchange->derive != NULL)
69                 break;
70             exchange->derive = OSSL_get_OP_keyexch_derive(fns);
71             fncnt++;
72             break;
73         case OSSL_FUNC_KEYEXCH_FREECTX:
74             if (exchange->freectx != NULL)
75                 break;
76             exchange->freectx = OSSL_get_OP_keyexch_freectx(fns);
77             fncnt++;
78             break;
79         case OSSL_FUNC_KEYEXCH_DUPCTX:
80             if (exchange->dupctx != NULL)
81                 break;
82             exchange->dupctx = OSSL_get_OP_keyexch_dupctx(fns);
83             break;
84         case OSSL_FUNC_KEYEXCH_SET_PARAMS:
85             if (exchange->set_params != NULL)
86                 break;
87             exchange->set_params = OSSL_get_OP_keyexch_set_params(fns);
88             break;
89         }
90     }
91     if (fncnt != 4) {
92         /*
93          * In order to be a consistent set of functions we must have at least
94          * a complete set of "exchange" functions: init, derive, newctx,
95          * and freectx. The dupctx, set_peer and set_params functions are
96          * optional.
97          */
98         EVP_KEYEXCH_free(exchange);
99         EVPerr(EVP_F_EVP_KEYEXCH_FROM_DISPATCH,
100                EVP_R_INVALID_PROVIDER_FUNCTIONS);
101         return NULL;
102     }
103
104     return exchange;
105 }
106
107 void EVP_KEYEXCH_free(EVP_KEYEXCH *exchange)
108 {
109     if (exchange != NULL) {
110         int i;
111
112         CRYPTO_DOWN_REF(&exchange->refcnt, &i, exchange->lock);
113         if (i > 0)
114             return;
115         EVP_KEYMGMT_free(exchange->keymgmt);
116         ossl_provider_free(exchange->prov);
117         OPENSSL_free(exchange->name);
118         CRYPTO_THREAD_lock_free(exchange->lock);
119         OPENSSL_free(exchange);
120     }
121 }
122
123 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
124 {
125     int ref = 0;
126
127     CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
128     return 1;
129 }
130
131 OSSL_PROVIDER *EVP_KEYEXCH_provider(const EVP_KEYEXCH *exchange)
132 {
133     return exchange->prov;
134 }
135
136 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
137                                const char *properties)
138 {
139     /*
140      * Key exchange cannot work without a key, and we key management
141      * from the same provider to manage its keys.
142      */
143     EVP_KEYEXCH *keyexch =
144         evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
145                           evp_keyexch_from_dispatch,
146                           (int (*)(void *))EVP_KEYEXCH_up_ref,
147                           (void (*)(void *))EVP_KEYEXCH_free);
148
149     /* If the method is newly created, there's no keymgmt attached */
150     if (keyexch->keymgmt == NULL) {
151         EVP_KEYMGMT *keymgmt = EVP_KEYMGMT_fetch(ctx, algorithm, properties);
152
153         if (keymgmt == NULL
154             || (EVP_KEYEXCH_provider(keyexch)
155                 != EVP_KEYMGMT_provider(keymgmt))) {
156             EVP_KEYEXCH_free(keyexch);
157             EVP_KEYMGMT_free(keymgmt);
158             EVPerr(EVP_F_EVP_KEYEXCH_FETCH, EVP_R_NO_KEYMGMT_PRESENT);
159             return NULL;
160         }
161
162         keyexch->keymgmt = keymgmt;
163     }
164     return keyexch;
165 }
166
167 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange)
168 {
169     int ret;
170     void *provkey = NULL;
171
172     ctx->operation = EVP_PKEY_OP_DERIVE;
173
174     if (ctx->engine != NULL)
175         goto legacy;
176
177     if (exchange != NULL) {
178         if (!EVP_KEYEXCH_up_ref(exchange))
179             goto err;
180     } else {
181         int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
182
183         /*
184          * TODO(3.0): Check for legacy handling. Remove this once all all
185          * algorithms are moved to providers.
186          */
187         if (ctx->pkey != NULL) {
188             switch (ctx->pkey->type) {
189             case EVP_PKEY_DH:
190                 break;
191             default:
192                 goto legacy;
193             }
194             exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL);
195         } else {
196             goto legacy;
197         }
198
199         if (exchange == NULL) {
200             EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
201             goto err;
202         }
203     }
204
205     if (ctx->exchprovctx != NULL && ctx->exchange != NULL)
206         ctx->exchange->freectx(ctx->exchprovctx);
207     EVP_KEYEXCH_free(ctx->exchange);
208     ctx->exchange = exchange;
209     if (ctx->pkey != NULL) {
210         provkey = evp_keymgmt_export_to_provider(ctx->pkey, exchange->keymgmt);
211         if (provkey == NULL) {
212             EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
213             goto err;
214         }
215     }
216     ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
217     if (ctx->exchprovctx == NULL) {
218         /* The provider key can stay in the cache */
219         EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
220         goto err;
221     }
222     ret = exchange->init(ctx->exchprovctx, provkey);
223
224     return ret ? 1 : 0;
225  err:
226     ctx->operation = EVP_PKEY_OP_UNDEFINED;
227     return 0;
228
229  legacy:
230     if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
231         EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX,
232                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
233         return -2;
234     }
235
236     if (ctx->pmeth->derive_init == NULL)
237         return 1;
238     ret = ctx->pmeth->derive_init(ctx);
239     if (ret <= 0)
240         ctx->operation = EVP_PKEY_OP_UNDEFINED;
241     return ret;
242 }
243
244 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
245 {
246     return EVP_PKEY_derive_init_ex(ctx, NULL);
247 }
248
249 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
250 {
251     int ret;
252     void *provkey = NULL;
253
254     if (ctx == NULL) {
255         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
256                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
257         return -2;
258     }
259
260     if (ctx->exchprovctx == NULL)
261         goto legacy;
262
263     if (ctx->operation != EVP_PKEY_OP_DERIVE) {
264         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
265                EVP_R_OPERATON_NOT_INITIALIZED);
266         return -1;
267     }
268
269     if (ctx->exchange->set_peer == NULL) {
270         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
271                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
272         return -2;
273     }
274
275     provkey = evp_keymgmt_export_to_provider(peer, ctx->exchange->keymgmt);
276     if (provkey == NULL) {
277         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
278         return 0;
279     }
280     return ctx->exchange->set_peer(ctx->exchprovctx, provkey);
281
282  legacy:
283     if (ctx->pmeth == NULL
284         || !(ctx->pmeth->derive != NULL
285              || ctx->pmeth->encrypt != NULL
286              || ctx->pmeth->decrypt != NULL)
287         || ctx->pmeth->ctrl == NULL) {
288         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
289                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
290         return -2;
291     }
292     if (ctx->operation != EVP_PKEY_OP_DERIVE
293         && ctx->operation != EVP_PKEY_OP_ENCRYPT
294         && ctx->operation != EVP_PKEY_OP_DECRYPT) {
295         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
296                EVP_R_OPERATON_NOT_INITIALIZED);
297         return -1;
298     }
299
300     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
301
302     if (ret <= 0)
303         return ret;
304
305     if (ret == 2)
306         return 1;
307
308     if (ctx->pkey == NULL) {
309         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
310         return -1;
311     }
312
313     if (ctx->pkey->type != peer->type) {
314         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
315         return -1;
316     }
317
318     /*
319      * For clarity.  The error is if parameters in peer are
320      * present (!missing) but don't match.  EVP_PKEY_cmp_parameters may return
321      * 1 (match), 0 (don't match) and -2 (comparison is not defined).  -1
322      * (different key types) is impossible here because it is checked earlier.
323      * -2 is OK for us here, as well as 1, so we can check for 0 only.
324      */
325     if (!EVP_PKEY_missing_parameters(peer) &&
326         !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
327         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
328         return -1;
329     }
330
331     EVP_PKEY_free(ctx->peerkey);
332     ctx->peerkey = peer;
333
334     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
335
336     if (ret <= 0) {
337         ctx->peerkey = NULL;
338         return ret;
339     }
340
341     EVP_PKEY_up_ref(peer);
342     return 1;
343 }
344
345 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
346 {
347     int ret;
348
349     if (ctx == NULL) {
350         EVPerr(EVP_F_EVP_PKEY_DERIVE,
351                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
352         return -2;
353     }
354
355     if (ctx->operation != EVP_PKEY_OP_DERIVE) {
356         EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
357         return -1;
358     }
359
360     if (ctx->exchprovctx == NULL)
361         goto legacy;
362
363     ret = ctx->exchange->derive(ctx->exchprovctx, key, pkeylen, SIZE_MAX);
364
365     return ret;
366  legacy:
367     if (ctx ==  NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
368         EVPerr(EVP_F_EVP_PKEY_DERIVE,
369                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
370         return -2;
371     }
372
373     M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
374         return ctx->pmeth->derive(ctx, key, pkeylen);
375 }