1 /*
2 * Diffie-Hellman-Merkle key exchange (server side)
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #include "mbedtls/build_info.h"
9
10 #include "mbedtls/platform.h"
11 /* md.h is included this early since MD_CAN_XXX macros are defined there. */
12 #include "mbedtls/md.h"
13
14 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
15 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
16 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256) && \
17 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
18 defined(MBEDTLS_MD_CAN_SHA1)
19 #include "mbedtls/net_sockets.h"
20 #include "mbedtls/aes.h"
21 #include "mbedtls/dhm.h"
22 #include "mbedtls/rsa.h"
23 #include "mbedtls/sha1.h"
24 #include "mbedtls/entropy.h"
25 #include "mbedtls/ctr_drbg.h"
26
27 #include <stdio.h>
28 #include <string.h>
29 #endif
30
31 #define SERVER_PORT "11999"
32 #define PLAINTEXT "==Hello there!=="
33
34 #if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
35 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
36 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
37 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
38 !defined(MBEDTLS_SHA1_C)
main(void)39 int main(void)
40 {
41 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
42 "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
43 "MBEDTLS_MD_CAN_SHA256 and/or MBEDTLS_FS_IO and/or "
44 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_SHA1_C not defined.\n");
45 mbedtls_exit(0);
46 }
47 #else
48
49
main(void)50 int main(void)
51 {
52 FILE *f;
53
54 int ret = 1;
55 int exit_code = MBEDTLS_EXIT_FAILURE;
56 size_t n, buflen;
57 mbedtls_net_context listen_fd, client_fd;
58
59 unsigned char buf[2048];
60 unsigned char hash[32];
61 unsigned char buf2[2];
62 const char *pers = "dh_server";
63
64 mbedtls_entropy_context entropy;
65 mbedtls_ctr_drbg_context ctr_drbg;
66 mbedtls_rsa_context rsa;
67 mbedtls_dhm_context dhm;
68 mbedtls_aes_context aes;
69
70 mbedtls_mpi N, P, Q, D, E;
71
72 mbedtls_net_init(&listen_fd);
73 mbedtls_net_init(&client_fd);
74 mbedtls_dhm_init(&dhm);
75 mbedtls_aes_init(&aes);
76 mbedtls_ctr_drbg_init(&ctr_drbg);
77
78 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
79 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
80
81 /*
82 * 1. Setup the RNG
83 */
84 mbedtls_printf("\n . Seeding the random number generator");
85 fflush(stdout);
86
87 mbedtls_entropy_init(&entropy);
88 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
89 (const unsigned char *) pers,
90 strlen(pers))) != 0) {
91 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
92 goto exit;
93 }
94
95 /*
96 * 2a. Read the server's private RSA key
97 */
98 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
99 fflush(stdout);
100
101 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
102 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
103 " ! Please run rsa_genkey first\n\n");
104 goto exit;
105 }
106
107 mbedtls_rsa_init(&rsa);
108
109 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
110 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
111 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
112 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
113 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0) {
114 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
115 ret);
116 fclose(f);
117 goto exit;
118 }
119 fclose(f);
120
121 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
122 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
123 ret);
124 goto exit;
125 }
126
127 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
128 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
129 ret);
130 goto exit;
131 }
132
133 /*
134 * 2b. Get the DHM modulus and generator
135 */
136 mbedtls_printf("\n . Reading DH parameters from dh_prime.txt");
137 fflush(stdout);
138
139 if ((f = fopen("dh_prime.txt", "rb")) == NULL) {
140 mbedtls_printf(" failed\n ! Could not open dh_prime.txt\n" \
141 " ! Please run dh_genprime first\n\n");
142 goto exit;
143 }
144
145 if (mbedtls_mpi_read_file(&dhm.MBEDTLS_PRIVATE(P), 16, f) != 0 ||
146 mbedtls_mpi_read_file(&dhm.MBEDTLS_PRIVATE(G), 16, f) != 0) {
147 mbedtls_printf(" failed\n ! Invalid DH parameter file\n\n");
148 fclose(f);
149 goto exit;
150 }
151
152 fclose(f);
153
154 /*
155 * 3. Wait for a client to connect
156 */
157 mbedtls_printf("\n . Waiting for a remote connection");
158 fflush(stdout);
159
160 if ((ret = mbedtls_net_bind(&listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
161 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
162 goto exit;
163 }
164
165 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
166 NULL, 0, NULL)) != 0) {
167 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
168 goto exit;
169 }
170
171 /*
172 * 4. Setup the DH parameters (P,G,Ys)
173 */
174 mbedtls_printf("\n . Sending the server's DH parameters");
175 fflush(stdout);
176
177 memset(buf, 0, sizeof(buf));
178
179 if ((ret =
180 mbedtls_dhm_make_params(&dhm, (int) mbedtls_mpi_size(&dhm.MBEDTLS_PRIVATE(P)), buf, &n,
181 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
182 mbedtls_printf(" failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret);
183 goto exit;
184 }
185
186 /*
187 * 5. Sign the parameters and send them
188 */
189 if ((ret = mbedtls_sha1(buf, n, hash)) != 0) {
190 mbedtls_printf(" failed\n ! mbedtls_sha1 returned %d\n\n", ret);
191 goto exit;
192 }
193
194 buf[n] = (unsigned char) (rsa.MBEDTLS_PRIVATE(len) >> 8);
195 buf[n + 1] = (unsigned char) (rsa.MBEDTLS_PRIVATE(len));
196
197 if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_MD_SHA256,
198 32, hash, buf + n + 2)) != 0) {
199 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_sign returned %d\n\n", ret);
200 goto exit;
201 }
202
203 buflen = n + 2 + rsa.MBEDTLS_PRIVATE(len);
204 buf2[0] = (unsigned char) (buflen >> 8);
205 buf2[1] = (unsigned char) (buflen);
206
207 if ((ret = mbedtls_net_send(&client_fd, buf2, 2)) != 2 ||
208 (ret = mbedtls_net_send(&client_fd, buf, buflen)) != (int) buflen) {
209 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
210 goto exit;
211 }
212
213 /*
214 * 6. Get the client's public value: Yc = G ^ Xc mod P
215 */
216 mbedtls_printf("\n . Receiving the client's public value");
217 fflush(stdout);
218
219 memset(buf, 0, sizeof(buf));
220
221 n = mbedtls_dhm_get_len(&dhm);
222 if ((ret = mbedtls_net_recv(&client_fd, buf, n)) != (int) n) {
223 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
224 goto exit;
225 }
226
227 if ((ret = mbedtls_dhm_read_public(&dhm, buf, n)) != 0) {
228 mbedtls_printf(" failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret);
229 goto exit;
230 }
231
232 /*
233 * 7. Derive the shared secret: K = Ys ^ Xc mod P
234 */
235 mbedtls_printf("\n . Shared secret: ");
236 fflush(stdout);
237
238 if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n,
239 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
240 mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret);
241 goto exit;
242 }
243
244 for (n = 0; n < 16; n++) {
245 mbedtls_printf("%02x", buf[n]);
246 }
247
248 /*
249 * 8. Setup the AES-256 encryption key
250 *
251 * This is an overly simplified example; best practice is
252 * to hash the shared secret with a random value to derive
253 * the keying material for the encryption/decryption keys
254 * and MACs.
255 */
256 mbedtls_printf("...\n . Encrypting and sending the ciphertext");
257 fflush(stdout);
258
259 ret = mbedtls_aes_setkey_enc(&aes, buf, 256);
260 if (ret != 0) {
261 goto exit;
262 }
263 memcpy(buf, PLAINTEXT, 16);
264 ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, buf, buf);
265 if (ret != 0) {
266 goto exit;
267 }
268
269 if ((ret = mbedtls_net_send(&client_fd, buf, 16)) != 16) {
270 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
271 goto exit;
272 }
273
274 mbedtls_printf("\n\n");
275
276 exit_code = MBEDTLS_EXIT_SUCCESS;
277
278 exit:
279
280 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
281 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
282
283 mbedtls_net_free(&client_fd);
284 mbedtls_net_free(&listen_fd);
285
286 mbedtls_aes_free(&aes);
287 mbedtls_rsa_free(&rsa);
288 mbedtls_dhm_free(&dhm);
289 mbedtls_ctr_drbg_free(&ctr_drbg);
290 mbedtls_entropy_free(&entropy);
291
292 mbedtls_exit(exit_code);
293 }
294 #endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
295 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
296 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
297