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