blob: 6ad015a48a36a7d18fe8ec3810cc6a73fdd0f8df [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (server side)
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
17 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
18 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \
Janos Follath9fe6f922016-10-07 14:17:56 +010019 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
20 defined(MBEDTLS_SHA1_C)
Andres AG788aa4a2016-09-14 14:32:09 +010021#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/aes.h"
23#include "mbedtls/dhm.h"
24#include "mbedtls/rsa.h"
25#include "mbedtls/sha1.h"
26#include "mbedtls/entropy.h"
27#include "mbedtls/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evans18b78c72015-02-11 14:06:19 +000029#include <stdio.h>
30#include <string.h>
31#endif
32
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020033#define SERVER_PORT "11999"
Paul Bakker5121ce52009-01-03 21:22:43 +000034#define PLAINTEXT "==Hello there!=="
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
37 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
38 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
Janos Follath9fe6f922016-10-07 14:17:56 +010039 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
40 !defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044 "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
45 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
46 "MBEDTLS_CTR_DRBG_C not defined.\n");
47 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000048}
49#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000050
Simon Butcher63cb97e2018-12-06 17:43:31 +000051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000053{
54 FILE *f;
55
Andres Amaya Garcia03a992c2018-04-29 19:40:45 +010056 int ret = 1;
57 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000058 size_t n, buflen;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020059 mbedtls_net_context listen_fd, client_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakker520ea912012-10-24 14:17:01 +000061 unsigned char buf[2048];
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020062 unsigned char hash[32];
Paul Bakker5121ce52009-01-03 21:22:43 +000063 unsigned char buf2[2];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020064 const char *pers = "dh_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_entropy_context entropy;
67 mbedtls_ctr_drbg_context ctr_drbg;
68 mbedtls_rsa_context rsa;
69 mbedtls_dhm_context dhm;
70 mbedtls_aes_context aes;
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Hanno Beckerc95fad32017-08-23 06:44:30 +010072 mbedtls_mpi N, P, Q, D, E;
73
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 mbedtls_net_init(&listen_fd);
75 mbedtls_net_init(&client_fd);
76 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256);
77 mbedtls_dhm_init(&dhm);
78 mbedtls_aes_init(&aes);
79 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
82 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerc95fad32017-08-23 06:44:30 +010083
Paul Bakker5121ce52009-01-03 21:22:43 +000084 /*
85 * 1. Setup the RNG
86 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 mbedtls_printf("\n . Seeding the random number generator");
88 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 mbedtls_entropy_init(&entropy);
91 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
92 (const unsigned char *) pers,
93 strlen(pers))) != 0) {
94 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +000095 goto exit;
96 }
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 /*
99 * 2a. Read the server's private RSA key
100 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
102 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
105 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
106 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 goto exit;
108 }
109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
113 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
114 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
115 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
116 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0) {
117 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
118 ret);
119 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 goto exit;
121 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
125 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
126 ret);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100127 goto exit;
128 }
129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
131 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
132 ret);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100133 goto exit;
134 }
135
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 /*
137 * 2b. Get the DHM modulus and generator
138 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 mbedtls_printf("\n . Reading DH parameters from dh_prime.txt");
140 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 if ((f = fopen("dh_prime.txt", "rb")) == NULL) {
143 mbedtls_printf(" failed\n ! Could not open dh_prime.txt\n" \
144 " ! Please run dh_genprime first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 goto exit;
146 }
147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 if (mbedtls_mpi_read_file(&dhm.P, 16, f) != 0 ||
149 mbedtls_mpi_read_file(&dhm.G, 16, f) != 0) {
150 mbedtls_printf(" failed\n ! Invalid DH parameter file\n\n");
151 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 goto exit;
153 }
154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 /*
158 * 3. Wait for a client to connect
159 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_printf("\n . Waiting for a remote connection");
161 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 if ((ret = mbedtls_net_bind(&listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
164 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 goto exit;
166 }
167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
169 NULL, 0, NULL)) != 0) {
170 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 goto exit;
172 }
173
174 /*
175 * 4. Setup the DH parameters (P,G,Ys)
176 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 mbedtls_printf("\n . Sending the server's DH parameters");
178 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 if ((ret = mbedtls_dhm_make_params(&dhm, (int) mbedtls_mpi_size(&dhm.P), buf, &n,
183 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
184 mbedtls_printf(" failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 goto exit;
186 }
187
188 /*
189 * 5. Sign the parameters and send them
190 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 if ((ret = mbedtls_sha1_ret(buf, n, hash)) != 0) {
192 mbedtls_printf(" failed\n ! mbedtls_sha1_ret returned %d\n\n", ret);
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100193 goto exit;
194 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 buf[n] = (unsigned char) (rsa.len >> 8);
197 buf[n + 1] = (unsigned char) (rsa.len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
200 0, hash, buf + n + 2)) != 0) {
201 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_sign returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 goto exit;
203 }
204
205 buflen = n + 2 + rsa.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 buf2[0] = (unsigned char) (buflen >> 8);
207 buf2[1] = (unsigned char) (buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 if ((ret = mbedtls_net_send(&client_fd, buf2, 2)) != 2 ||
210 (ret = mbedtls_net_send(&client_fd, buf, buflen)) != (int) buflen) {
211 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 goto exit;
213 }
214
215 /*
216 * 6. Get the client's public value: Yc = G ^ Xc mod P
217 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 mbedtls_printf("\n . Receiving the client's public value");
219 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Martijn de Millianob194a282017-07-06 23:55:59 +0200223 n = dhm.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 if ((ret = mbedtls_net_recv(&client_fd, buf, n)) != (int) n) {
225 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 goto exit;
227 }
228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 if ((ret = mbedtls_dhm_read_public(&dhm, buf, dhm.len)) != 0) {
230 mbedtls_printf(" failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 goto exit;
232 }
233
234 /*
235 * 7. Derive the shared secret: K = Ys ^ Xc mod P
236 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 mbedtls_printf("\n . Shared secret: ");
238 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n,
241 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
242 mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 goto exit;
244 }
245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 for (n = 0; n < 16; n++) {
247 mbedtls_printf("%02x", buf[n]);
248 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250 /*
251 * 8. Setup the AES-256 encryption key
252 *
253 * This is an overly simplified example; best practice is
254 * to hash the shared secret with a random value to derive
255 * the keying material for the encryption/decryption keys
256 * and MACs.
257 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 mbedtls_printf("...\n . Encrypting and sending the ciphertext");
259 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 ret = mbedtls_aes_setkey_enc(&aes, buf, 256);
262 if (ret != 0) {
Gilles Peskine377a3102021-07-07 21:08:28 +0200263 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 }
265 memcpy(buf, PLAINTEXT, 16);
266 ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, buf, buf);
267 if (ret != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 goto exit;
269 }
270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 if ((ret = mbedtls_net_send(&client_fd, buf, 16)) != 16) {
272 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
273 goto exit;
274 }
275
276 mbedtls_printf("\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
Andres Amaya Garcia03a992c2018-04-29 19:40:45 +0100278 exit_code = MBEDTLS_EXIT_SUCCESS;
279
Paul Bakker5121ce52009-01-03 21:22:43 +0000280exit:
281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
283 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 mbedtls_net_free(&client_fd);
286 mbedtls_net_free(&listen_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 mbedtls_aes_free(&aes);
289 mbedtls_rsa_free(&rsa);
290 mbedtls_dhm_free(&dhm);
291 mbedtls_ctr_drbg_free(&ctr_drbg);
292 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
Paul Bakkercce9d772011-11-18 14:26:47 +0000294#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 mbedtls_printf(" + Press Enter to exit this program.\n");
296 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000297#endif
298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
302 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
303 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */