blob: 34956b817a926bab4d1018a75c11ed86fc40ec49 [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"
Paul Bakker508ad5a2011-12-04 17:09:26 +000040#include "polarssl/entropy.h"
41#include "polarssl/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
43#define SERVER_NAME "localhost"
44#define SERVER_PORT 11999
45
Paul Bakker5690efc2011-05-26 13:16:06 +000046#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000047 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_NET_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000048 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000049 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000050int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000051{
Paul Bakkercce9d772011-11-18 14:26:47 +000052 ((void) argc);
53 ((void) argv);
54
Paul Bakker508ad5a2011-12-04 17:09:26 +000055 printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_ENTROPY_C "
Paul Bakker5690efc2011-05-26 13:16:06 +000056 "and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000057 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO and/or "
58 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000059 return( 0 );
60}
61#else
Paul Bakkercce9d772011-11-18 14:26:47 +000062int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000063{
64 FILE *f;
65
Paul Bakker23986e52011-04-24 08:57:21 +000066 int ret;
67 size_t n, buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +000068 int server_fd = -1;
69
70 unsigned char *p, *end;
Paul Bakker520ea912012-10-24 14:17:01 +000071 unsigned char buf[2048];
Paul Bakker5121ce52009-01-03 21:22:43 +000072 unsigned char hash[20];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020073 const char *pers = "dh_client";
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Paul Bakker508ad5a2011-12-04 17:09:26 +000075 entropy_context entropy;
76 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000077 rsa_context rsa;
78 dhm_context dhm;
79 aes_context aes;
80
Paul Bakkercce9d772011-11-18 14:26:47 +000081 ((void) argc);
82 ((void) argv);
83
Paul Bakker5121ce52009-01-03 21:22:43 +000084 memset( &rsa, 0, sizeof( rsa ) );
85 memset( &dhm, 0, sizeof( dhm ) );
86
87 /*
88 * 1. Setup the RNG
89 */
90 printf( "\n . Seeding the random number generator" );
91 fflush( stdout );
92
Paul Bakker508ad5a2011-12-04 17:09:26 +000093 entropy_init( &entropy );
94 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020095 (const unsigned char *) pers,
96 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000097 {
98 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
99 goto exit;
100 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102 /*
103 * 2. Read the server's public RSA key
104 */
105 printf( "\n . Reading public key from rsa_pub.txt" );
106 fflush( stdout );
107
108 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
109 {
110 ret = 1;
111 printf( " failed\n ! Could not open rsa_pub.txt\n" \
112 " ! Please run rsa_genkey first\n\n" );
113 goto exit;
114 }
115
Paul Bakkera802e1a2010-08-16 11:56:45 +0000116 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
119 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
120 {
121 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
122 goto exit;
123 }
124
125 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
126
127 fclose( f );
128
129 /*
130 * 3. Initiate the connection
131 */
132 printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME,
133 SERVER_PORT );
134 fflush( stdout );
135
136 if( ( ret = net_connect( &server_fd, SERVER_NAME,
137 SERVER_PORT ) ) != 0 )
138 {
139 printf( " failed\n ! net_connect returned %d\n\n", ret );
140 goto exit;
141 }
142
143 /*
144 * 4a. First get the buffer length
145 */
146 printf( "\n . Receiving the server's DH parameters" );
147 fflush( stdout );
148
149 memset( buf, 0, sizeof( buf ) );
150
151 if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 )
152 {
153 printf( " failed\n ! net_recv returned %d\n\n", ret );
154 goto exit;
155 }
156
157 n = buflen = ( buf[0] << 8 ) | buf[1];
Paul Bakker23986e52011-04-24 08:57:21 +0000158 if( buflen < 1 || buflen > sizeof( buf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 {
160 printf( " failed\n ! Got an invalid buffer length\n\n" );
161 goto exit;
162 }
163
164 /*
165 * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
166 */
167 memset( buf, 0, sizeof( buf ) );
168
Paul Bakker23986e52011-04-24 08:57:21 +0000169 if( ( ret = net_recv( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 {
171 printf( " failed\n ! net_recv returned %d\n\n", ret );
172 goto exit;
173 }
174
175 p = buf, end = buf + buflen;
176
177 if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 )
178 {
179 printf( " failed\n ! dhm_read_params returned %d\n\n", ret );
180 goto exit;
181 }
182
Paul Bakker520ea912012-10-24 14:17:01 +0000183 if( dhm.len < 64 || dhm.len > 512 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 {
185 ret = 1;
186 printf( " failed\n ! Invalid DHM modulus size\n\n" );
187 goto exit;
188 }
189
190 /*
191 * 5. Check that the server's RSA signature matches
192 * the SHA-1 hash of (P,G,Ys)
193 */
194 printf( "\n . Verifying the server's RSA signature" );
195 fflush( stdout );
196
Paul Bakker88f17b82012-04-26 18:52:13 +0000197 p += 2;
198
Paul Bakker23986e52011-04-24 08:57:21 +0000199 if( ( n = (size_t) ( end - p ) ) != rsa.len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 {
201 ret = 1;
202 printf( " failed\n ! Invalid RSA signature size\n\n" );
203 goto exit;
204 }
205
206 sha1( buf, (int)( p - 2 - buf ), hash );
207
Paul Bakkerc70b9822013-04-07 22:00:46 +0200208 if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, POLARSSL_MD_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 0, hash, p ) ) != 0 )
210 {
211 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
212 goto exit;
213 }
214
215 /*
216 * 6. Send our public value: Yc = G ^ Xc mod P
217 */
218 printf( "\n . Sending own public value to server" );
219 fflush( stdout );
220
221 n = dhm.len;
Paul Bakker520ea912012-10-24 14:17:01 +0000222 if( ( ret = dhm_make_public( &dhm, dhm.len, buf, n,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000223 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 {
225 printf( " failed\n ! dhm_make_public returned %d\n\n", ret );
226 goto exit;
227 }
228
Paul Bakker23986e52011-04-24 08:57:21 +0000229 if( ( ret = net_send( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 {
231 printf( " failed\n ! net_send 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
241 n = dhm.len;
242 if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
243 {
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 decryption 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 * IVs and MACs.
258 */
259 printf( "...\n . Receiving and decrypting the ciphertext" );
260 fflush( stdout );
261
262 aes_setkey_dec( &aes, buf, 256 );
263
264 memset( buf, 0, sizeof( buf ) );
265
266 if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 )
267 {
268 printf( " failed\n ! net_recv returned %d\n\n", ret );
269 goto exit;
270 }
271
272 aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf );
273 buf[16] = '\0';
274 printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
275
276exit:
277
278 net_close( server_fd );
279 rsa_free( &rsa );
280 dhm_free( &dhm );
281
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 */