blob: f85f028ce619fd7f3485382d299f531d523d4fc1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (client side)
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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)
49int main( void )
50{
51 printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_HAVEGE_C "
52 "and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
53 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n");
54 return( 0 );
55}
56#else
Paul Bakker5121ce52009-01-03 21:22:43 +000057int main( void )
58{
59 FILE *f;
60
Paul Bakker23986e52011-04-24 08:57:21 +000061 int ret;
62 size_t n, buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +000063 int server_fd = -1;
64
65 unsigned char *p, *end;
66 unsigned char buf[1024];
67 unsigned char hash[20];
68
69 havege_state hs;
70 rsa_context rsa;
71 dhm_context dhm;
72 aes_context aes;
73
74 memset( &rsa, 0, sizeof( rsa ) );
75 memset( &dhm, 0, sizeof( dhm ) );
76
77 /*
78 * 1. Setup the RNG
79 */
80 printf( "\n . Seeding the random number generator" );
81 fflush( stdout );
82
83 havege_init( &hs );
84
85 /*
86 * 2. Read the server's public RSA key
87 */
88 printf( "\n . Reading public key from rsa_pub.txt" );
89 fflush( stdout );
90
91 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
92 {
93 ret = 1;
94 printf( " failed\n ! Could not open rsa_pub.txt\n" \
95 " ! Please run rsa_genkey first\n\n" );
96 goto exit;
97 }
98
Paul Bakkera802e1a2010-08-16 11:56:45 +000099 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
102 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
103 {
104 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
105 goto exit;
106 }
107
108 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
109
110 fclose( f );
111
112 /*
113 * 3. Initiate the connection
114 */
115 printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME,
116 SERVER_PORT );
117 fflush( stdout );
118
119 if( ( ret = net_connect( &server_fd, SERVER_NAME,
120 SERVER_PORT ) ) != 0 )
121 {
122 printf( " failed\n ! net_connect returned %d\n\n", ret );
123 goto exit;
124 }
125
126 /*
127 * 4a. First get the buffer length
128 */
129 printf( "\n . Receiving the server's DH parameters" );
130 fflush( stdout );
131
132 memset( buf, 0, sizeof( buf ) );
133
134 if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 )
135 {
136 printf( " failed\n ! net_recv returned %d\n\n", ret );
137 goto exit;
138 }
139
140 n = buflen = ( buf[0] << 8 ) | buf[1];
Paul Bakker23986e52011-04-24 08:57:21 +0000141 if( buflen < 1 || buflen > sizeof( buf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 {
143 printf( " failed\n ! Got an invalid buffer length\n\n" );
144 goto exit;
145 }
146
147 /*
148 * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
149 */
150 memset( buf, 0, sizeof( buf ) );
151
Paul Bakker23986e52011-04-24 08:57:21 +0000152 if( ( ret = net_recv( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 {
154 printf( " failed\n ! net_recv returned %d\n\n", ret );
155 goto exit;
156 }
157
158 p = buf, end = buf + buflen;
159
160 if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 )
161 {
162 printf( " failed\n ! dhm_read_params returned %d\n\n", ret );
163 goto exit;
164 }
165
166 if( dhm.len < 64 || dhm.len > 256 )
167 {
168 ret = 1;
169 printf( " failed\n ! Invalid DHM modulus size\n\n" );
170 goto exit;
171 }
172
173 /*
174 * 5. Check that the server's RSA signature matches
175 * the SHA-1 hash of (P,G,Ys)
176 */
177 printf( "\n . Verifying the server's RSA signature" );
178 fflush( stdout );
179
Paul Bakker23986e52011-04-24 08:57:21 +0000180 if( ( n = (size_t) ( end - p ) ) != rsa.len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 {
182 ret = 1;
183 printf( " failed\n ! Invalid RSA signature size\n\n" );
184 goto exit;
185 }
186
187 sha1( buf, (int)( p - 2 - buf ), hash );
188
Paul Bakker4593aea2009-02-09 22:32:35 +0000189 if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 0, hash, p ) ) != 0 )
191 {
192 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
193 goto exit;
194 }
195
196 /*
197 * 6. Send our public value: Yc = G ^ Xc mod P
198 */
199 printf( "\n . Sending own public value to server" );
200 fflush( stdout );
201
202 n = dhm.len;
203 if( ( ret = dhm_make_public( &dhm, 256, buf, n,
204 havege_rand, &hs ) ) != 0 )
205 {
206 printf( " failed\n ! dhm_make_public returned %d\n\n", ret );
207 goto exit;
208 }
209
Paul Bakker23986e52011-04-24 08:57:21 +0000210 if( ( ret = net_send( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 {
212 printf( " failed\n ! net_send returned %d\n\n", ret );
213 goto exit;
214 }
215
216 /*
217 * 7. Derive the shared secret: K = Ys ^ Xc mod P
218 */
219 printf( "\n . Shared secret: " );
220 fflush( stdout );
221
222 n = dhm.len;
223 if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
224 {
225 printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
226 goto exit;
227 }
228
229 for( n = 0; n < 16; n++ )
230 printf( "%02x", buf[n] );
231
232 /*
233 * 8. Setup the AES-256 decryption key
234 *
235 * This is an overly simplified example; best practice is
236 * to hash the shared secret with a random value to derive
237 * the keying material for the encryption/decryption keys,
238 * IVs and MACs.
239 */
240 printf( "...\n . Receiving and decrypting the ciphertext" );
241 fflush( stdout );
242
243 aes_setkey_dec( &aes, buf, 256 );
244
245 memset( buf, 0, sizeof( buf ) );
246
247 if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 )
248 {
249 printf( " failed\n ! net_recv returned %d\n\n", ret );
250 goto exit;
251 }
252
253 aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf );
254 buf[16] = '\0';
255 printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
256
257exit:
258
259 net_close( server_fd );
260 rsa_free( &rsa );
261 dhm_free( &dhm );
262
263#ifdef WIN32
264 printf( " + Press Enter to exit this program.\n" );
265 fflush( stdout ); getchar();
266#endif
267
268 return( ret );
269}
Paul Bakker5690efc2011-05-26 13:16:06 +0000270#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_HAVEGE_C &&
271 POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
272 POLARSSL_FS_IO */