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