blob: 66450b6d181003b6edbb8e79660651682d1c7161 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (client 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_NAME "localhost"
40#define SERVER_PORT 11999
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_DRBG_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 server_fd = -1;
65
66 unsigned char *p, *end;
Paul Bakker520ea912012-10-24 14:17:01 +000067 unsigned char buf[2048];
Paul Bakker5121ce52009-01-03 21:22:43 +000068 unsigned char hash[20];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020069 const char *pers = "dh_client";
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakker508ad5a2011-12-04 17:09:26 +000071 entropy_context entropy;
72 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000073 rsa_context rsa;
74 dhm_context dhm;
75 aes_context aes;
76
Paul Bakkercce9d772011-11-18 14:26:47 +000077 ((void) argc);
78 ((void) argv);
79
Paul Bakker5121ce52009-01-03 21:22:43 +000080 memset( &rsa, 0, sizeof( rsa ) );
81 memset( &dhm, 0, sizeof( dhm ) );
82
83 /*
84 * 1. Setup the RNG
85 */
86 printf( "\n . Seeding the random number generator" );
87 fflush( stdout );
88
Paul Bakker508ad5a2011-12-04 17:09:26 +000089 entropy_init( &entropy );
90 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020091 (const unsigned char *) pers,
92 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000093 {
94 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
95 goto exit;
96 }
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 /*
99 * 2. Read the server's public RSA key
100 */
101 printf( "\n . Reading public key from rsa_pub.txt" );
102 fflush( stdout );
103
104 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
105 {
106 ret = 1;
107 printf( " failed\n ! Could not open rsa_pub.txt\n" \
108 " ! Please run rsa_genkey first\n\n" );
109 goto exit;
110 }
111
Paul Bakkera802e1a2010-08-16 11:56:45 +0000112 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
115 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
116 {
117 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
118 goto exit;
119 }
120
121 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
122
123 fclose( f );
124
125 /*
126 * 3. Initiate the connection
127 */
128 printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME,
129 SERVER_PORT );
130 fflush( stdout );
131
132 if( ( ret = net_connect( &server_fd, SERVER_NAME,
133 SERVER_PORT ) ) != 0 )
134 {
135 printf( " failed\n ! net_connect returned %d\n\n", ret );
136 goto exit;
137 }
138
139 /*
140 * 4a. First get the buffer length
141 */
142 printf( "\n . Receiving the server's DH parameters" );
143 fflush( stdout );
144
145 memset( buf, 0, sizeof( buf ) );
146
147 if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 )
148 {
149 printf( " failed\n ! net_recv returned %d\n\n", ret );
150 goto exit;
151 }
152
153 n = buflen = ( buf[0] << 8 ) | buf[1];
Paul Bakker23986e52011-04-24 08:57:21 +0000154 if( buflen < 1 || buflen > sizeof( buf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 {
156 printf( " failed\n ! Got an invalid buffer length\n\n" );
157 goto exit;
158 }
159
160 /*
161 * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
162 */
163 memset( buf, 0, sizeof( buf ) );
164
Paul Bakker23986e52011-04-24 08:57:21 +0000165 if( ( ret = net_recv( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 {
167 printf( " failed\n ! net_recv returned %d\n\n", ret );
168 goto exit;
169 }
170
171 p = buf, end = buf + buflen;
172
173 if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 )
174 {
175 printf( " failed\n ! dhm_read_params returned %d\n\n", ret );
176 goto exit;
177 }
178
Paul Bakker520ea912012-10-24 14:17:01 +0000179 if( dhm.len < 64 || dhm.len > 512 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 {
181 ret = 1;
182 printf( " failed\n ! Invalid DHM modulus size\n\n" );
183 goto exit;
184 }
185
186 /*
187 * 5. Check that the server's RSA signature matches
188 * the SHA-1 hash of (P,G,Ys)
189 */
190 printf( "\n . Verifying the server's RSA signature" );
191 fflush( stdout );
192
Paul Bakker88f17b82012-04-26 18:52:13 +0000193 p += 2;
194
Paul Bakker23986e52011-04-24 08:57:21 +0000195 if( ( n = (size_t) ( end - p ) ) != rsa.len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 {
197 ret = 1;
198 printf( " failed\n ! Invalid RSA signature size\n\n" );
199 goto exit;
200 }
201
202 sha1( buf, (int)( p - 2 - buf ), hash );
203
Paul Bakker548957d2013-08-30 10:30:02 +0200204 if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC,
205 POLARSSL_MD_SHA1, 0, hash, p ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 {
207 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
208 goto exit;
209 }
210
211 /*
212 * 6. Send our public value: Yc = G ^ Xc mod P
213 */
214 printf( "\n . Sending own public value to server" );
215 fflush( stdout );
216
217 n = dhm.len;
Paul Bakker840ab202013-11-30 15:14:38 +0100218 if( ( ret = dhm_make_public( &dhm, (int) dhm.len, buf, n,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000219 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 {
221 printf( " failed\n ! dhm_make_public returned %d\n\n", ret );
222 goto exit;
223 }
224
Paul Bakker23986e52011-04-24 08:57:21 +0000225 if( ( ret = net_send( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 {
227 printf( " failed\n ! net_send returned %d\n\n", ret );
228 goto exit;
229 }
230
231 /*
232 * 7. Derive the shared secret: K = Ys ^ Xc mod P
233 */
234 printf( "\n . Shared secret: " );
235 fflush( stdout );
236
237 n = dhm.len;
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200238 if( ( ret = dhm_calc_secret( &dhm, buf, &n,
239 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 {
241 printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
242 goto exit;
243 }
244
245 for( n = 0; n < 16; n++ )
246 printf( "%02x", buf[n] );
247
248 /*
249 * 8. Setup the AES-256 decryption key
250 *
251 * This is an overly simplified example; best practice is
252 * to hash the shared secret with a random value to derive
253 * the keying material for the encryption/decryption keys,
254 * IVs and MACs.
255 */
256 printf( "...\n . Receiving and decrypting the ciphertext" );
257 fflush( stdout );
258
259 aes_setkey_dec( &aes, buf, 256 );
260
261 memset( buf, 0, sizeof( buf ) );
262
263 if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 )
264 {
265 printf( " failed\n ! net_recv returned %d\n\n", ret );
266 goto exit;
267 }
268
269 aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf );
270 buf[16] = '\0';
271 printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
272
273exit:
274
Paul Bakker0c226102014-04-17 16:02:36 +0200275 if( server_fd != -1 )
276 net_close( server_fd );
277
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 rsa_free( &rsa );
279 dhm_free( &dhm );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200280 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
Paul Bakkercce9d772011-11-18 14:26:47 +0000282#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 printf( " + Press Enter to exit this program.\n" );
284 fflush( stdout ); getchar();
285#endif
286
287 return( ret );
288}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000289#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000290 POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000291 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */