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