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