blob: eb417daa16e207659f065ea891366d2ef31225cf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (server side)
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#include <string.h>
29#include <stdio.h>
30
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/net.h"
32#include "polarssl/aes.h"
33#include "polarssl/dhm.h"
34#include "polarssl/rsa.h"
35#include "polarssl/sha1.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000036#include "polarssl/entropy.h"
37#include "polarssl/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
39#define SERVER_PORT 11999
40#define PLAINTEXT "==Hello there!=="
41
Paul Bakker5690efc2011-05-26 13:16:06 +000042#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000043 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_NET_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000044 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000045 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000046int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000047{
Paul Bakkercce9d772011-11-18 14:26:47 +000048 ((void) argc);
49 ((void) argv);
50
Paul Bakker508ad5a2011-12-04 17:09:26 +000051 printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_ENTROPY_C "
Paul Bakker5690efc2011-05-26 13:16:06 +000052 "and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000053 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO and/or "
54 "POLARSSL_CTR_DBRG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000055 return( 0 );
56}
57#else
Paul Bakkercce9d772011-11-18 14:26:47 +000058int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000059{
60 FILE *f;
61
Paul Bakker23986e52011-04-24 08:57:21 +000062 int ret;
63 size_t n, buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +000064 int listen_fd = -1;
65 int client_fd = -1;
66
Paul Bakker520ea912012-10-24 14:17:01 +000067 unsigned char buf[2048];
Paul Bakker5121ce52009-01-03 21:22:43 +000068 unsigned char hash[20];
69 unsigned char buf2[2];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020070 const char *pers = "dh_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Paul Bakker508ad5a2011-12-04 17:09:26 +000072 entropy_context entropy;
73 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000074 rsa_context rsa;
75 dhm_context dhm;
76 aes_context aes;
77
Paul Bakkercce9d772011-11-18 14:26:47 +000078 ((void) argc);
79 ((void) argv);
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081 memset( &rsa, 0, sizeof( rsa ) );
82 memset( &dhm, 0, sizeof( dhm ) );
83
84 /*
85 * 1. Setup the RNG
86 */
87 printf( "\n . Seeding the random number generator" );
88 fflush( stdout );
89
Paul Bakker508ad5a2011-12-04 17:09:26 +000090 entropy_init( &entropy );
91 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020092 (const unsigned char *) pers,
93 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000094 {
95 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
96 goto exit;
97 }
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99 /*
100 * 2a. Read the server's private RSA key
101 */
102 printf( "\n . Reading private key from rsa_priv.txt" );
103 fflush( stdout );
104
105 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
106 {
107 ret = 1;
108 printf( " failed\n ! Could not open rsa_priv.txt\n" \
109 " ! Please run rsa_genkey first\n\n" );
110 goto exit;
111 }
112
Paul Bakkera802e1a2010-08-16 11:56:45 +0000113 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
116 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
117 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
118 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
119 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
120 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
121 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
122 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
123 {
124 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
125 goto exit;
126 }
127
128 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
129
130 fclose( f );
131
132 /*
133 * 2b. Get the DHM modulus and generator
134 */
135 printf( "\n . Reading DH parameters from dh_prime.txt" );
136 fflush( stdout );
137
138 if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
139 {
140 ret = 1;
141 printf( " failed\n ! Could not open dh_prime.txt\n" \
142 " ! Please run dh_genprime first\n\n" );
143 goto exit;
144 }
145
146 if( mpi_read_file( &dhm.P, 16, f ) != 0 ||
147 mpi_read_file( &dhm.G, 16, f ) != 0 )
148 {
149 printf( " failed\n ! Invalid DH parameter file\n\n" );
150 goto exit;
151 }
152
153 fclose( f );
154
155 /*
156 * 3. Wait for a client to connect
157 */
158 printf( "\n . Waiting for a remote connection" );
159 fflush( stdout );
160
161 if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 )
162 {
163 printf( " failed\n ! net_bind returned %d\n\n", ret );
164 goto exit;
165 }
166
167 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
168 {
169 printf( " failed\n ! net_accept returned %d\n\n", ret );
170 goto exit;
171 }
172
173 /*
174 * 4. Setup the DH parameters (P,G,Ys)
175 */
176 printf( "\n . Sending the server's DH parameters" );
177 fflush( stdout );
178
179 memset( buf, 0, sizeof( buf ) );
180
Paul Bakker840ab202013-11-30 15:14:38 +0100181 if( ( ret = dhm_make_params( &dhm, (int) mpi_size( &dhm.P ), buf, &n,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000182 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 {
184 printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
185 goto exit;
186 }
187
188 /*
189 * 5. Sign the parameters and send them
190 */
191 sha1( buf, n, hash );
192
193 buf[n ] = (unsigned char)( rsa.len >> 8 );
194 buf[n + 1] = (unsigned char)( rsa.len );
195
Paul Bakkerc70b9822013-04-07 22:00:46 +0200196 if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 0, hash, buf + n + 2 ) ) != 0 )
198 {
199 printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
200 goto exit;
201 }
202
203 buflen = n + 2 + rsa.len;
204 buf2[0] = (unsigned char)( buflen >> 8 );
205 buf2[1] = (unsigned char)( buflen );
206
207 if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 ||
Paul Bakker23986e52011-04-24 08:57:21 +0000208 ( ret = net_send( &client_fd, buf, buflen ) ) != (int) buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 {
210 printf( " failed\n ! net_send returned %d\n\n", ret );
211 goto exit;
212 }
213
214 /*
215 * 6. Get the client's public value: Yc = G ^ Xc mod P
216 */
217 printf( "\n . Receiving the client's public value" );
218 fflush( stdout );
219
220 memset( buf, 0, sizeof( buf ) );
221 n = dhm.len;
222
Paul Bakker23986e52011-04-24 08:57:21 +0000223 if( ( ret = net_recv( &client_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 {
225 printf( " failed\n ! net_recv returned %d\n\n", ret );
226 goto exit;
227 }
228
229 if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
230 {
231 printf( " failed\n ! dhm_read_public returned %d\n\n", ret );
232 goto exit;
233 }
234
235 /*
236 * 7. Derive the shared secret: K = Ys ^ Xc mod P
237 */
238 printf( "\n . Shared secret: " );
239 fflush( stdout );
240
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200241 if( ( ret = dhm_calc_secret( &dhm, buf, &n,
242 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 {
244 printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
245 goto exit;
246 }
247
248 for( n = 0; n < 16; n++ )
249 printf( "%02x", buf[n] );
250
251 /*
252 * 8. Setup the AES-256 encryption key
253 *
254 * This is an overly simplified example; best practice is
255 * to hash the shared secret with a random value to derive
256 * the keying material for the encryption/decryption keys
257 * and MACs.
258 */
259 printf( "...\n . Encrypting and sending the ciphertext" );
260 fflush( stdout );
261
262 aes_setkey_enc( &aes, buf, 256 );
263 memcpy( buf, PLAINTEXT, 16 );
264 aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf );
265
266 if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 )
267 {
268 printf( " failed\n ! net_send returned %d\n\n", ret );
269 goto exit;
270 }
271
272 printf( "\n\n" );
273
274exit:
275
Paul Bakker0c226102014-04-17 16:02:36 +0200276 if( client_fd != -1 )
277 net_close( client_fd );
278
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 rsa_free( &rsa );
280 dhm_free( &dhm );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200281 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
Paul Bakkercce9d772011-11-18 14:26:47 +0000283#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 printf( " + Press Enter to exit this program.\n" );
285 fflush( stdout ); getchar();
286#endif
287
288 return( ret );
289}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000290#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000291 POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000292 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */