blob: 8b9ced4c7b8d2ad95ae524adca3434052be920d6 [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 Bakker40e46942009-01-03 21:51:57 +000033#include "polarssl/net.h"
34#include "polarssl/aes.h"
35#include "polarssl/dhm.h"
36#include "polarssl/rsa.h"
37#include "polarssl/sha1.h"
38#include "polarssl/havege.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
40#define SERVER_NAME "localhost"
41#define SERVER_PORT 11999
42
43int main( void )
44{
45 FILE *f;
46
47 int ret, n, buflen;
48 int server_fd = -1;
49
50 unsigned char *p, *end;
51 unsigned char buf[1024];
52 unsigned char hash[20];
53
54 havege_state hs;
55 rsa_context rsa;
56 dhm_context dhm;
57 aes_context aes;
58
59 memset( &rsa, 0, sizeof( rsa ) );
60 memset( &dhm, 0, sizeof( dhm ) );
61
62 /*
63 * 1. Setup the RNG
64 */
65 printf( "\n . Seeding the random number generator" );
66 fflush( stdout );
67
68 havege_init( &hs );
69
70 /*
71 * 2. Read the server's public RSA key
72 */
73 printf( "\n . Reading public key from rsa_pub.txt" );
74 fflush( stdout );
75
76 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
77 {
78 ret = 1;
79 printf( " failed\n ! Could not open rsa_pub.txt\n" \
80 " ! Please run rsa_genkey first\n\n" );
81 goto exit;
82 }
83
Paul Bakkera802e1a2010-08-16 11:56:45 +000084 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000085
86 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
87 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
88 {
89 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
90 goto exit;
91 }
92
93 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
94
95 fclose( f );
96
97 /*
98 * 3. Initiate the connection
99 */
100 printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME,
101 SERVER_PORT );
102 fflush( stdout );
103
104 if( ( ret = net_connect( &server_fd, SERVER_NAME,
105 SERVER_PORT ) ) != 0 )
106 {
107 printf( " failed\n ! net_connect returned %d\n\n", ret );
108 goto exit;
109 }
110
111 /*
112 * 4a. First get the buffer length
113 */
114 printf( "\n . Receiving the server's DH parameters" );
115 fflush( stdout );
116
117 memset( buf, 0, sizeof( buf ) );
118
119 if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 )
120 {
121 printf( " failed\n ! net_recv returned %d\n\n", ret );
122 goto exit;
123 }
124
125 n = buflen = ( buf[0] << 8 ) | buf[1];
126 if( buflen < 1 || buflen > (int) sizeof( buf ) )
127 {
128 printf( " failed\n ! Got an invalid buffer length\n\n" );
129 goto exit;
130 }
131
132 /*
133 * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
134 */
135 memset( buf, 0, sizeof( buf ) );
136
137 if( ( ret = net_recv( &server_fd, buf, n ) ) != n )
138 {
139 printf( " failed\n ! net_recv returned %d\n\n", ret );
140 goto exit;
141 }
142
143 p = buf, end = buf + buflen;
144
145 if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 )
146 {
147 printf( " failed\n ! dhm_read_params returned %d\n\n", ret );
148 goto exit;
149 }
150
151 if( dhm.len < 64 || dhm.len > 256 )
152 {
153 ret = 1;
154 printf( " failed\n ! Invalid DHM modulus size\n\n" );
155 goto exit;
156 }
157
158 /*
159 * 5. Check that the server's RSA signature matches
160 * the SHA-1 hash of (P,G,Ys)
161 */
162 printf( "\n . Verifying the server's RSA signature" );
163 fflush( stdout );
164
165 if( ( n = (int)( end - p ) ) != rsa.len )
166 {
167 ret = 1;
168 printf( " failed\n ! Invalid RSA signature size\n\n" );
169 goto exit;
170 }
171
172 sha1( buf, (int)( p - 2 - buf ), hash );
173
Paul Bakker4593aea2009-02-09 22:32:35 +0000174 if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 0, hash, p ) ) != 0 )
176 {
177 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
178 goto exit;
179 }
180
181 /*
182 * 6. Send our public value: Yc = G ^ Xc mod P
183 */
184 printf( "\n . Sending own public value to server" );
185 fflush( stdout );
186
187 n = dhm.len;
188 if( ( ret = dhm_make_public( &dhm, 256, buf, n,
189 havege_rand, &hs ) ) != 0 )
190 {
191 printf( " failed\n ! dhm_make_public returned %d\n\n", ret );
192 goto exit;
193 }
194
195 if( ( ret = net_send( &server_fd, buf, n ) ) != n )
196 {
197 printf( " failed\n ! net_send returned %d\n\n", ret );
198 goto exit;
199 }
200
201 /*
202 * 7. Derive the shared secret: K = Ys ^ Xc mod P
203 */
204 printf( "\n . Shared secret: " );
205 fflush( stdout );
206
207 n = dhm.len;
208 if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
209 {
210 printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
211 goto exit;
212 }
213
214 for( n = 0; n < 16; n++ )
215 printf( "%02x", buf[n] );
216
217 /*
218 * 8. Setup the AES-256 decryption key
219 *
220 * This is an overly simplified example; best practice is
221 * to hash the shared secret with a random value to derive
222 * the keying material for the encryption/decryption keys,
223 * IVs and MACs.
224 */
225 printf( "...\n . Receiving and decrypting the ciphertext" );
226 fflush( stdout );
227
228 aes_setkey_dec( &aes, buf, 256 );
229
230 memset( buf, 0, sizeof( buf ) );
231
232 if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 )
233 {
234 printf( " failed\n ! net_recv returned %d\n\n", ret );
235 goto exit;
236 }
237
238 aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf );
239 buf[16] = '\0';
240 printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
241
242exit:
243
244 net_close( server_fd );
245 rsa_free( &rsa );
246 dhm_free( &dhm );
247
248#ifdef WIN32
249 printf( " + Press Enter to exit this program.\n" );
250 fflush( stdout ); getchar();
251#endif
252
253 return( ret );
254}