c8ed76e8b168b73420c4def6251d80d7ff5133fe
[openssl.git] / providers / implementations / exchange / dh_exch.c
1 /*
2  * Copyright 2019-2020 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 /*
11  * DH low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/crypto.h>
17 #include <openssl/core_dispatch.h>
18 #include <openssl/core_names.h>
19 #include <openssl/dh.h>
20 #include <openssl/params.h>
21 #include "prov/implementations.h"
22 #include "prov/provider_ctx.h"
23 #include "crypto/dh.h"
24
25 static OSSL_OP_keyexch_newctx_fn dh_newctx;
26 static OSSL_OP_keyexch_init_fn dh_init;
27 static OSSL_OP_keyexch_set_peer_fn dh_set_peer;
28 static OSSL_OP_keyexch_derive_fn dh_derive;
29 static OSSL_OP_keyexch_freectx_fn dh_freectx;
30 static OSSL_OP_keyexch_dupctx_fn dh_dupctx;
31 static OSSL_OP_keyexch_set_ctx_params_fn dh_set_ctx_params;
32 static OSSL_OP_keyexch_settable_ctx_params_fn dh_settable_ctx_params;
33
34 /*
35  * What's passed as an actual key is defined by the KEYMGMT interface.
36  * We happen to know that our KEYMGMT simply passes DH structures, so
37  * we use that here too.
38  */
39
40 typedef struct {
41     OPENSSL_CTX *libctx;
42     DH *dh;
43     DH *dhpeer;
44     unsigned int pad : 1;
45 } PROV_DH_CTX;
46
47 static void *dh_newctx(void *provctx)
48 {
49     PROV_DH_CTX *pdhctx = OPENSSL_zalloc(sizeof(PROV_DH_CTX));
50
51     if (pdhctx == NULL)
52         return NULL;
53     pdhctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
54     return pdhctx;
55 }
56
57 static int dh_init(void *vpdhctx, void *vdh)
58 {
59     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
60
61     if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
62         return 0;
63     DH_free(pdhctx->dh);
64     pdhctx->dh = vdh;
65     return 1;
66 }
67
68 static int dh_set_peer(void *vpdhctx, void *vdh)
69 {
70     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
71
72     if (pdhctx == NULL || vdh == NULL || !DH_up_ref(vdh))
73         return 0;
74     DH_free(pdhctx->dhpeer);
75     pdhctx->dhpeer = vdh;
76     return 1;
77 }
78
79 static int dh_derive(void *vpdhctx, unsigned char *secret, size_t *secretlen,
80                      size_t outlen)
81 {
82     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
83     int ret;
84     size_t dhsize;
85     const BIGNUM *pub_key = NULL;
86
87     /* TODO(3.0): Add errors to stack */
88     if (pdhctx->dh == NULL || pdhctx->dhpeer == NULL)
89         return 0;
90
91     dhsize = (size_t)DH_size(pdhctx->dh);
92     if (secret == NULL) {
93         *secretlen = dhsize;
94         return 1;
95     }
96     if (outlen < dhsize)
97         return 0;
98
99     DH_get0_key(pdhctx->dhpeer, &pub_key, NULL);
100     if (pdhctx->pad)
101         ret = DH_compute_key_padded(secret, pub_key, pdhctx->dh);
102     else
103         ret = DH_compute_key(secret, pub_key, pdhctx->dh);
104     if (ret <= 0)
105         return 0;
106
107     *secretlen = ret;
108     return 1;
109 }
110
111 static void dh_freectx(void *vpdhctx)
112 {
113     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
114
115     DH_free(pdhctx->dh);
116     DH_free(pdhctx->dhpeer);
117
118     OPENSSL_free(pdhctx);
119 }
120
121 static void *dh_dupctx(void *vpdhctx)
122 {
123     PROV_DH_CTX *srcctx = (PROV_DH_CTX *)vpdhctx;
124     PROV_DH_CTX *dstctx;
125
126     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
127     if (dstctx == NULL)
128         return NULL;
129
130     *dstctx = *srcctx;
131     if (dstctx->dh != NULL && !DH_up_ref(dstctx->dh)) {
132         OPENSSL_free(dstctx);
133         return NULL;
134     }
135
136     if (dstctx->dhpeer != NULL && !DH_up_ref(dstctx->dhpeer)) {
137         DH_free(dstctx->dh);
138         OPENSSL_free(dstctx);
139         return NULL;
140     }
141
142     return dstctx;
143 }
144
145 static int dh_set_ctx_params(void *vpdhctx, const OSSL_PARAM params[])
146 {
147     PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
148     const OSSL_PARAM *p;
149     unsigned int pad;
150
151     if (pdhctx == NULL || params == NULL)
152         return 0;
153
154     p = OSSL_PARAM_locate_const(params, OSSL_EXCHANGE_PARAM_PAD);
155     if (p == NULL || !OSSL_PARAM_get_uint(p, &pad))
156         return 0;
157     pdhctx->pad = pad ? 1 : 0;
158     return 1;
159 }
160
161 static const OSSL_PARAM known_settable_ctx_params[] = {
162     OSSL_PARAM_int(OSSL_EXCHANGE_PARAM_PAD, NULL),
163     OSSL_PARAM_END
164 };
165
166 static const OSSL_PARAM *dh_settable_ctx_params(void)
167 {
168     return known_settable_ctx_params;
169 }
170
171 const OSSL_DISPATCH dh_keyexch_functions[] = {
172     { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))dh_newctx },
173     { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))dh_init },
174     { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))dh_derive },
175     { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))dh_set_peer },
176     { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))dh_freectx },
177     { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))dh_dupctx },
178     { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))dh_set_ctx_params },
179     { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS,
180       (void (*)(void))dh_settable_ctx_params },
181     { 0, NULL }
182 };