69980d8e9736a7c1499171ce2cd37467766558eb
[openssl.git] / providers / common / exchange / dh_exch.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/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/dh.h>
14 #include <openssl/params.h>
15 #include "internal/provider_algs.h"
16
17 static OSSL_OP_keyexch_newctx_fn dh_newctx;
18 static OSSL_OP_keyexch_init_fn dh_init;
19 static OSSL_OP_keyexch_set_peer_fn dh_set_peer;
20 static OSSL_OP_keyexch_derive_fn dh_derive;
21 static OSSL_OP_keyexch_freectx_fn dh_freectx;
22 static OSSL_OP_keyexch_dupctx_fn dh_dupctx;
23
24 /*
25  * What's passed as an actual key is defined by the KEYMGMT interface.
26  * We happen to know that our KEYMGMT simply passes DH structures, so
27  * we use that here too.
28  */
29
30 typedef struct {
31     DH *dh;
32     DH *dhpeer;
33     int pad;
34 } PROV_DH_CTX;
35
36 static void *dh_newctx(void *provctx)
37 {
38     return OPENSSL_zalloc(sizeof(PROV_DH_CTX));
39 }
40
41 static int dh_init(void *vpdhctx, void *vdh)
42 {
43     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
44
45     if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
46         return 0;
47     DH_free(pdhctx->dh);
48     pdhctx->dh = vdh;
49     return 1;
50 }
51
52 static int dh_set_peer(void *vpdhctx, void *vdh)
53 {
54     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
55
56     if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
57         return 0;
58     DH_free(pdhctx->dhpeer);
59     pdhctx->dhpeer = vdh;
60     return 1;
61 }
62
63 static int dh_derive(void *vpdhctx, unsigned char *secret, size_t *secretlen,
64                      size_t outlen)
65 {
66     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
67     int ret;
68     size_t dhsize;
69     const BIGNUM *pub_key = NULL;
70
71     /* TODO(3.0): Add errors to stack */
72     if (pdhctx->dh == NULL || pdhctx->dhpeer == NULL)
73         return 0;
74
75     dhsize = (size_t)DH_size(pdhctx->dh);
76     if (secret == NULL) {
77         *secretlen = dhsize;
78         return 1;
79     }
80     if (outlen < dhsize)
81         return 0;
82
83     DH_get0_key(pdhctx->dhpeer, &pub_key, NULL);
84     ret = (pdhctx->pad) ? DH_compute_key_padded(secret, pub_key, pdhctx->dh)
85                         : DH_compute_key(secret, pub_key, pdhctx->dh);
86     if (ret <= 0)
87         return 0;
88
89     *secretlen = ret;
90     return 1;
91 }
92
93 static void dh_freectx(void *vpdhctx)
94 {
95     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
96
97     DH_free(pdhctx->dh);
98     DH_free(pdhctx->dhpeer);
99
100     OPENSSL_free(pdhctx);
101 }
102
103 static void *dh_dupctx(void *vpdhctx)
104 {
105     PROV_DH_CTX *srcctx = (PROV_DH_CTX *)vpdhctx;
106     PROV_DH_CTX *dstctx;
107
108     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
109     if (dstctx == NULL)
110         return NULL;
111
112     *dstctx = *srcctx;
113     if (dstctx->dh != NULL && !DH_up_ref(dstctx->dh)) {
114         OPENSSL_free(dstctx);
115         return NULL;
116     }
117
118     if (dstctx->dhpeer != NULL && !DH_up_ref(dstctx->dhpeer)) {
119         DH_free(dstctx->dh);
120         OPENSSL_free(dstctx);
121         return NULL;
122     }
123
124     return dstctx;
125 }
126
127 static int dh_set_params(void *vpdhctx, const OSSL_PARAM params[])
128 {
129     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
130     const OSSL_PARAM *p;
131     int pad;
132
133     if (pdhctx == NULL || params == NULL)
134         return 0;
135
136     p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_PAD);
137     if (p == NULL || !OSSL_PARAM_get_int(p, &pad))
138         return 0;
139
140     pdhctx->pad = pad;
141
142     return 1;
143 }
144
145 const OSSL_DISPATCH dh_keyexch_functions[] = {
146     { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))dh_newctx },
147     { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))dh_init },
148     { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))dh_derive },
149     { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))dh_set_peer },
150     { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))dh_freectx },
151     { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))dh_dupctx },
152     { OSSL_FUNC_KEYEXCH_SET_PARAMS, (void (*)(void))dh_set_params },
153     { 0, NULL }
154 };