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