1 /*
2 * Diffie-Hellman-Merkle key exchange (client side)
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #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) && \
19 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
20 defined(MBEDTLS_SHA1_C)
21 #include "mbedtls/net_sockets.h"
22 #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"
28
29 #include <stdio.h>
30 #include <string.h>
31 #endif
32
33 #define SERVER_NAME "localhost"
34 #define SERVER_PORT "11999"
35
36 #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) || \
39 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
40 !defined(MBEDTLS_SHA1_C)
main(void)41 int main(void)
42 {
43 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
44 "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);
48 }
49 #else
50
51
main(void)52 int main(void)
53 {
54 FILE *f;
55
56 int ret = 1;
57 int exit_code = MBEDTLS_EXIT_FAILURE;
58 size_t n, buflen;
59 mbedtls_net_context server_fd;
60
61 unsigned char *p, *end;
62 unsigned char buf[2048];
63 unsigned char hash[32];
64 const char *pers = "dh_client";
65
66 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;
71
72 mbedtls_net_init(&server_fd);
73 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256);
74 mbedtls_dhm_init(&dhm);
75 mbedtls_aes_init(&aes);
76 mbedtls_ctr_drbg_init(&ctr_drbg);
77
78 /*
79 * 1. Setup the RNG
80 */
81 mbedtls_printf("\n . Seeding the random number generator");
82 fflush(stdout);
83
84 mbedtls_entropy_init(&entropy);
85 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
86 (const unsigned char *) pers,
87 strlen(pers))) != 0) {
88 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
89 goto exit;
90 }
91
92 /*
93 * 2. Read the server's public RSA key
94 */
95 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
96 fflush(stdout);
97
98 if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
99 mbedtls_printf(" failed\n ! Could not open rsa_pub.txt\n" \
100 " ! Please run rsa_genkey first\n\n");
101 goto exit;
102 }
103
104 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
105
106 if ((ret = mbedtls_mpi_read_file(&rsa.N, 16, f)) != 0 ||
107 (ret = mbedtls_mpi_read_file(&rsa.E, 16, f)) != 0) {
108 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
109 fclose(f);
110 goto exit;
111 }
112
113 rsa.len = (mbedtls_mpi_bitlen(&rsa.N) + 7) >> 3;
114
115 fclose(f);
116
117 /*
118 * 3. Initiate the connection
119 */
120 mbedtls_printf("\n . Connecting to tcp/%s/%s", SERVER_NAME,
121 SERVER_PORT);
122 fflush(stdout);
123
124 if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME,
125 SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
126 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
127 goto exit;
128 }
129
130 /*
131 * 4a. First get the buffer length
132 */
133 mbedtls_printf("\n . Receiving the server's DH parameters");
134 fflush(stdout);
135
136 memset(buf, 0, sizeof(buf));
137
138 if ((ret = mbedtls_net_recv(&server_fd, buf, 2)) != 2) {
139 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
140 goto exit;
141 }
142
143 n = buflen = (buf[0] << 8) | buf[1];
144 if (buflen < 1 || buflen > sizeof(buf)) {
145 mbedtls_printf(" failed\n ! Got an invalid buffer length\n\n");
146 goto exit;
147 }
148
149 /*
150 * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
151 */
152 memset(buf, 0, sizeof(buf));
153
154 if ((ret = mbedtls_net_recv(&server_fd, buf, n)) != (int) n) {
155 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
156 goto exit;
157 }
158
159 p = buf, end = buf + buflen;
160
161 if ((ret = mbedtls_dhm_read_params(&dhm, &p, end)) != 0) {
162 mbedtls_printf(" failed\n ! mbedtls_dhm_read_params returned %d\n\n", ret);
163 goto exit;
164 }
165
166 if (dhm.len < 64 || dhm.len > 512) {
167 mbedtls_printf(" failed\n ! Invalid DHM modulus size\n\n");
168 goto exit;
169 }
170
171 /*
172 * 5. Check that the server's RSA signature matches
173 * the SHA-256 hash of (P,G,Ys)
174 */
175 mbedtls_printf("\n . Verifying the server's RSA signature");
176 fflush(stdout);
177
178 p += 2;
179
180 if ((n = (size_t) (end - p)) != rsa.len) {
181 mbedtls_printf(" failed\n ! Invalid RSA signature size\n\n");
182 goto exit;
183 }
184
185 if ((ret = mbedtls_sha1_ret(buf, (int) (p - 2 - buf), hash)) != 0) {
186 mbedtls_printf(" failed\n ! mbedtls_sha1_ret returned %d\n\n", ret);
187 goto exit;
188 }
189
190 if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
191 MBEDTLS_MD_SHA256, 0, hash, p)) != 0) {
192 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_verify returned %d\n\n", ret);
193 goto exit;
194 }
195
196 /*
197 * 6. Send our public value: Yc = G ^ Xc mod P
198 */
199 mbedtls_printf("\n . Sending own public value to server");
200 fflush(stdout);
201
202 n = dhm.len;
203 if ((ret = mbedtls_dhm_make_public(&dhm, (int) dhm.len, buf, n,
204 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
205 mbedtls_printf(" failed\n ! mbedtls_dhm_make_public returned %d\n\n", ret);
206 goto exit;
207 }
208
209 if ((ret = mbedtls_net_send(&server_fd, buf, n)) != (int) n) {
210 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
211 goto exit;
212 }
213
214 /*
215 * 7. Derive the shared secret: K = Ys ^ Xc mod P
216 */
217 mbedtls_printf("\n . Shared secret: ");
218 fflush(stdout);
219
220 if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n,
221 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
222 mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret);
223 goto exit;
224 }
225
226 for (n = 0; n < 16; n++) {
227 mbedtls_printf("%02x", buf[n]);
228 }
229
230 /*
231 * 8. Setup the AES-256 decryption key
232 *
233 * This is an overly simplified example; best practice is
234 * to hash the shared secret with a random value to derive
235 * the keying material for the encryption/decryption keys,
236 * IVs and MACs.
237 */
238 mbedtls_printf("...\n . Receiving and decrypting the ciphertext");
239 fflush(stdout);
240
241 ret = mbedtls_aes_setkey_dec(&aes, buf, 256);
242 if (ret != 0) {
243 goto exit;
244 }
245
246 memset(buf, 0, sizeof(buf));
247
248 if ((ret = mbedtls_net_recv(&server_fd, buf, 16)) != 16) {
249 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
250 goto exit;
251 }
252
253 ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, buf, buf);
254 if (ret != 0) {
255 goto exit;
256 }
257 buf[16] = '\0';
258 mbedtls_printf("\n . Plaintext is \"%s\"\n\n", (char *) buf);
259
260 exit_code = MBEDTLS_EXIT_SUCCESS;
261
262 exit:
263
264 mbedtls_net_free(&server_fd);
265
266 mbedtls_aes_free(&aes);
267 mbedtls_rsa_free(&rsa);
268 mbedtls_dhm_free(&dhm);
269 mbedtls_ctr_drbg_free(&ctr_drbg);
270 mbedtls_entropy_free(&entropy);
271
272 #if defined(_WIN32)
273 mbedtls_printf(" + Press Enter to exit this program.\n");
274 fflush(stdout); getchar();
275 #endif
276
277 mbedtls_exit(exit_code);
278 }
279 #endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
280 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
281 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
282