d8afcbd6331e55a56e33c7f77ec19314378cd25a
[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         ossl_provider_free(exchange->prov);
116         OPENSSL_free(exchange->name);
117         CRYPTO_THREAD_lock_free(exchange->lock);
118         OPENSSL_free(exchange);
119     }
120 }
121
122 int EVP_KEYEXCH_up_ref(EVP_KEYEXCH *exchange)
123 {
124     int ref = 0;
125
126     CRYPTO_UP_REF(&exchange->refcnt, &ref, exchange->lock);
127     return 1;
128 }
129
130 EVP_KEYEXCH *EVP_KEYEXCH_fetch(OPENSSL_CTX *ctx, const char *algorithm,
131                                const char *properties)
132 {
133     return evp_generic_fetch(ctx, OSSL_OP_KEYEXCH, algorithm, properties,
134                              evp_keyexch_from_dispatch,
135                              (int (*)(void *))EVP_KEYEXCH_up_ref,
136                              (void (*)(void *))EVP_KEYEXCH_free);
137 }
138
139 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange)
140 {
141     int ret;
142     OSSL_PARAM *param = NULL;
143     size_t paramsz = 0;
144
145     ctx->operation = EVP_PKEY_OP_DERIVE;
146
147     if (ctx->engine != NULL)
148         goto legacy;
149
150     if (exchange != NULL) {
151         if (!EVP_KEYEXCH_up_ref(exchange))
152             goto err;
153     } else {
154         int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
155
156         /*
157          * TODO(3.0): Check for legacy handling. Remove this once all all
158          * algorithms are moved to providers.
159          */
160         if (ctx->pkey != NULL) {
161             switch (ctx->pkey->type) {
162             case EVP_PKEY_DH:
163                 break;
164             default:
165                 goto legacy;
166             }
167             exchange = EVP_KEYEXCH_fetch(NULL, OBJ_nid2sn(nid), NULL);
168         } else {
169             goto legacy;
170         }
171
172         if (exchange == NULL) {
173             EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
174             goto err;
175         }
176     }
177
178     if (ctx->exchprovctx != NULL && ctx->exchange != NULL)
179         ctx->exchange->freectx(ctx->exchprovctx);
180     EVP_KEYEXCH_free(ctx->exchange);
181     ctx->exchange = exchange;
182     if (ctx->pkey != NULL) {
183         param = evp_pkey_to_param(ctx->pkey, &paramsz);
184         if (param == NULL) {
185             EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
186             goto err;
187         }
188     }
189     ctx->exchprovctx = exchange->newctx(ossl_provider_ctx(exchange->prov));
190     if (ctx->exchprovctx == NULL) {
191         OPENSSL_secure_clear_free(param, paramsz);
192         EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX, EVP_R_INITIALIZATION_ERROR);
193         goto err;
194     }
195     ret = exchange->init(ctx->exchprovctx, param);
196     /*
197      * TODO(3.0): Really we should detect whether to call OPENSSL_free or
198      * OPENSSL_secure_clear_free based on the presence of a private key or not.
199      * Since we always expect a private key to be present we just call
200      * OPENSSL_secure_clear_free for now.
201      */
202     OPENSSL_secure_clear_free(param, paramsz);
203
204     return ret ? 1 : 0;
205  err:
206     ctx->operation = EVP_PKEY_OP_UNDEFINED;
207     return 0;
208
209  legacy:
210     if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
211         EVPerr(EVP_F_EVP_PKEY_DERIVE_INIT_EX,
212                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
213         return -2;
214     }
215
216     if (ctx->pmeth->derive_init == NULL)
217         return 1;
218     ret = ctx->pmeth->derive_init(ctx);
219     if (ret <= 0)
220         ctx->operation = EVP_PKEY_OP_UNDEFINED;
221     return ret;
222 }
223
224 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx)
225 {
226     return EVP_PKEY_derive_init_ex(ctx, NULL);
227 }
228
229 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
230 {
231     int ret;
232     OSSL_PARAM *param = NULL;
233
234     if (ctx == NULL) {
235         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
236                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
237         return -2;
238     }
239
240     if (ctx->exchprovctx == NULL)
241         goto legacy;
242
243     if (ctx->operation != EVP_PKEY_OP_DERIVE) {
244         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
245                EVP_R_OPERATON_NOT_INITIALIZED);
246         return -1;
247     }
248
249     if (ctx->exchange->set_peer == NULL) {
250         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
251                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
252         return -2;
253     }
254
255     param = evp_pkey_to_param(peer, NULL);
256     if (param == NULL) {
257         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, ERR_R_INTERNAL_ERROR);
258         return 0;
259     }
260     ret = ctx->exchange->set_peer(ctx->exchprovctx, param);
261     /*
262      * TODO(3.0): Really we should detect whether to call OPENSSL_free or
263      * OPENSSL_secure_clear_free based on the presence of a private key or not.
264      * Since we always expect a public key to be present we just call
265      * OPENSSL_free for now.
266      */
267     OPENSSL_free(param);
268
269     return ret;
270
271  legacy:
272     if (ctx->pmeth == NULL
273         || !(ctx->pmeth->derive != NULL
274              || ctx->pmeth->encrypt != NULL
275              || ctx->pmeth->decrypt != NULL)
276         || ctx->pmeth->ctrl == NULL) {
277         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
278                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
279         return -2;
280     }
281     if (ctx->operation != EVP_PKEY_OP_DERIVE
282         && ctx->operation != EVP_PKEY_OP_ENCRYPT
283         && ctx->operation != EVP_PKEY_OP_DECRYPT) {
284         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER,
285                EVP_R_OPERATON_NOT_INITIALIZED);
286         return -1;
287     }
288
289     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
290
291     if (ret <= 0)
292         return ret;
293
294     if (ret == 2)
295         return 1;
296
297     if (ctx->pkey == NULL) {
298         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_NO_KEY_SET);
299         return -1;
300     }
301
302     if (ctx->pkey->type != peer->type) {
303         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_KEY_TYPES);
304         return -1;
305     }
306
307     /*
308      * For clarity.  The error is if parameters in peer are
309      * present (!missing) but don't match.  EVP_PKEY_cmp_parameters may return
310      * 1 (match), 0 (don't match) and -2 (comparison is not defined).  -1
311      * (different key types) is impossible here because it is checked earlier.
312      * -2 is OK for us here, as well as 1, so we can check for 0 only.
313      */
314     if (!EVP_PKEY_missing_parameters(peer) &&
315         !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
316         EVPerr(EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS);
317         return -1;
318     }
319
320     EVP_PKEY_free(ctx->peerkey);
321     ctx->peerkey = peer;
322
323     ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
324
325     if (ret <= 0) {
326         ctx->peerkey = NULL;
327         return ret;
328     }
329
330     EVP_PKEY_up_ref(peer);
331     return 1;
332 }
333
334 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *pkeylen)
335 {
336     int ret;
337
338     if (ctx == NULL) {
339         EVPerr(EVP_F_EVP_PKEY_DERIVE,
340                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
341         return -2;
342     }
343
344     if (ctx->operation != EVP_PKEY_OP_DERIVE) {
345         EVPerr(EVP_F_EVP_PKEY_DERIVE, EVP_R_OPERATON_NOT_INITIALIZED);
346         return -1;
347     }
348
349     if (ctx->exchprovctx == NULL)
350         goto legacy;
351
352     ret = ctx->exchange->derive(ctx->exchprovctx, key, pkeylen, SIZE_MAX);
353
354     return ret;
355  legacy:
356     if (ctx ==  NULL || ctx->pmeth == NULL || ctx->pmeth->derive == NULL) {
357         EVPerr(EVP_F_EVP_PKEY_DERIVE,
358                EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
359         return -2;
360     }
361
362     M_check_autoarg(ctx, key, pkeylen, EVP_F_EVP_PKEY_DERIVE)
363         return ctx->pmeth->derive(ctx, key, pkeylen);
364 }