blob: ab7b9a6cc13191d74ac75d1ea1ff7c2c20401851 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdio.h>
32
Paul Bakker5690efc2011-05-26 13:16:06 +000033#include "polarssl/config.h"
34
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/net.h"
36#include "polarssl/aes.h"
37#include "polarssl/dhm.h"
38#include "polarssl/rsa.h"
39#include "polarssl/sha1.h"
40#include "polarssl/havege.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
42#define SERVER_NAME "localhost"
43#define SERVER_PORT 11999
44
Paul Bakker5690efc2011-05-26 13:16:06 +000045#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \
46 !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_NET_C) || \
47 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \
48 !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000049int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000050{
Paul Bakkercce9d772011-11-18 14:26:47 +000051 ((void) argc);
52 ((void) argv);
53
Paul Bakker5690efc2011-05-26 13:16:06 +000054 printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_HAVEGE_C "
55 "and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
56 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n");
57 return( 0 );
58}
59#else
Paul Bakkercce9d772011-11-18 14:26:47 +000060int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000061{
62 FILE *f;
63
Paul Bakker23986e52011-04-24 08:57:21 +000064 int ret;
65 size_t n, buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +000066 int server_fd = -1;
67
68 unsigned char *p, *end;
69 unsigned char buf[1024];
70 unsigned char hash[20];
71
72 havege_state hs;
73 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
89 havege_init( &hs );
90
91 /*
92 * 2. Read the server's public RSA key
93 */
94 printf( "\n . Reading public key from rsa_pub.txt" );
95 fflush( stdout );
96
97 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
98 {
99 ret = 1;
100 printf( " failed\n ! Could not open rsa_pub.txt\n" \
101 " ! Please run rsa_genkey first\n\n" );
102 goto exit;
103 }
104
Paul Bakkera802e1a2010-08-16 11:56:45 +0000105 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
108 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
109 {
110 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
111 goto exit;
112 }
113
114 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
115
116 fclose( f );
117
118 /*
119 * 3. Initiate the connection
120 */
121 printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME,
122 SERVER_PORT );
123 fflush( stdout );
124
125 if( ( ret = net_connect( &server_fd, SERVER_NAME,
126 SERVER_PORT ) ) != 0 )
127 {
128 printf( " failed\n ! net_connect returned %d\n\n", ret );
129 goto exit;
130 }
131
132 /*
133 * 4a. First get the buffer length
134 */
135 printf( "\n . Receiving the server's DH parameters" );
136 fflush( stdout );
137
138 memset( buf, 0, sizeof( buf ) );
139
140 if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 )
141 {
142 printf( " failed\n ! net_recv returned %d\n\n", ret );
143 goto exit;
144 }
145
146 n = buflen = ( buf[0] << 8 ) | buf[1];
Paul Bakker23986e52011-04-24 08:57:21 +0000147 if( buflen < 1 || buflen > sizeof( buf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 {
149 printf( " failed\n ! Got an invalid buffer length\n\n" );
150 goto exit;
151 }
152
153 /*
154 * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
155 */
156 memset( buf, 0, sizeof( buf ) );
157
Paul Bakker23986e52011-04-24 08:57:21 +0000158 if( ( ret = net_recv( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 {
160 printf( " failed\n ! net_recv returned %d\n\n", ret );
161 goto exit;
162 }
163
164 p = buf, end = buf + buflen;
165
166 if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 )
167 {
168 printf( " failed\n ! dhm_read_params returned %d\n\n", ret );
169 goto exit;
170 }
171
172 if( dhm.len < 64 || dhm.len > 256 )
173 {
174 ret = 1;
175 printf( " failed\n ! Invalid DHM modulus size\n\n" );
176 goto exit;
177 }
178
179 /*
180 * 5. Check that the server's RSA signature matches
181 * the SHA-1 hash of (P,G,Ys)
182 */
183 printf( "\n . Verifying the server's RSA signature" );
184 fflush( stdout );
185
Paul Bakker23986e52011-04-24 08:57:21 +0000186 if( ( n = (size_t) ( end - p ) ) != rsa.len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 {
188 ret = 1;
189 printf( " failed\n ! Invalid RSA signature size\n\n" );
190 goto exit;
191 }
192
193 sha1( buf, (int)( p - 2 - buf ), hash );
194
Paul Bakker4593aea2009-02-09 22:32:35 +0000195 if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 0, hash, p ) ) != 0 )
197 {
198 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
199 goto exit;
200 }
201
202 /*
203 * 6. Send our public value: Yc = G ^ Xc mod P
204 */
205 printf( "\n . Sending own public value to server" );
206 fflush( stdout );
207
208 n = dhm.len;
209 if( ( ret = dhm_make_public( &dhm, 256, buf, n,
210 havege_rand, &hs ) ) != 0 )
211 {
212 printf( " failed\n ! dhm_make_public returned %d\n\n", ret );
213 goto exit;
214 }
215
Paul Bakker23986e52011-04-24 08:57:21 +0000216 if( ( ret = net_send( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 {
218 printf( " failed\n ! net_send returned %d\n\n", ret );
219 goto exit;
220 }
221
222 /*
223 * 7. Derive the shared secret: K = Ys ^ Xc mod P
224 */
225 printf( "\n . Shared secret: " );
226 fflush( stdout );
227
228 n = dhm.len;
229 if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
230 {
231 printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
232 goto exit;
233 }
234
235 for( n = 0; n < 16; n++ )
236 printf( "%02x", buf[n] );
237
238 /*
239 * 8. Setup the AES-256 decryption key
240 *
241 * This is an overly simplified example; best practice is
242 * to hash the shared secret with a random value to derive
243 * the keying material for the encryption/decryption keys,
244 * IVs and MACs.
245 */
246 printf( "...\n . Receiving and decrypting the ciphertext" );
247 fflush( stdout );
248
249 aes_setkey_dec( &aes, buf, 256 );
250
251 memset( buf, 0, sizeof( buf ) );
252
253 if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 )
254 {
255 printf( " failed\n ! net_recv returned %d\n\n", ret );
256 goto exit;
257 }
258
259 aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf );
260 buf[16] = '\0';
261 printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
262
263exit:
264
265 net_close( server_fd );
266 rsa_free( &rsa );
267 dhm_free( &dhm );
268
Paul Bakkercce9d772011-11-18 14:26:47 +0000269#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 printf( " + Press Enter to exit this program.\n" );
271 fflush( stdout ); getchar();
272#endif
273
274 return( ret );
275}
Paul Bakker5690efc2011-05-26 13:16:06 +0000276#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_HAVEGE_C &&
277 POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
278 POLARSSL_FS_IO */