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