blob: de2ce970d8996ebd655ab0090a4cbeaa71a3d70e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (server 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_PORT 11999
43#define PLAINTEXT "==Hello there!=="
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 listen_fd = -1;
67 int client_fd = -1;
68
69 unsigned char buf[1024];
70 unsigned char hash[20];
71 unsigned char buf2[2];
72
73 havege_state hs;
74 rsa_context rsa;
75 dhm_context dhm;
76 aes_context aes;
77
Paul Bakkercce9d772011-11-18 14:26:47 +000078 ((void) argc);
79 ((void) argv);
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081 memset( &rsa, 0, sizeof( rsa ) );
82 memset( &dhm, 0, sizeof( dhm ) );
83
84 /*
85 * 1. Setup the RNG
86 */
87 printf( "\n . Seeding the random number generator" );
88 fflush( stdout );
89
90 havege_init( &hs );
91
92 /*
93 * 2a. Read the server's private RSA key
94 */
95 printf( "\n . Reading private key from rsa_priv.txt" );
96 fflush( stdout );
97
98 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
99 {
100 ret = 1;
101 printf( " failed\n ! Could not open rsa_priv.txt\n" \
102 " ! Please run rsa_genkey first\n\n" );
103 goto exit;
104 }
105
Paul Bakkera802e1a2010-08-16 11:56:45 +0000106 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
109 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
110 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
111 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
112 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
113 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
114 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
115 ( ret = mpi_read_file( &rsa.QP, 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 * 2b. Get the DHM modulus and generator
127 */
128 printf( "\n . Reading DH parameters from dh_prime.txt" );
129 fflush( stdout );
130
131 if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
132 {
133 ret = 1;
134 printf( " failed\n ! Could not open dh_prime.txt\n" \
135 " ! Please run dh_genprime first\n\n" );
136 goto exit;
137 }
138
139 if( mpi_read_file( &dhm.P, 16, f ) != 0 ||
140 mpi_read_file( &dhm.G, 16, f ) != 0 )
141 {
142 printf( " failed\n ! Invalid DH parameter file\n\n" );
143 goto exit;
144 }
145
146 fclose( f );
147
148 /*
149 * 3. Wait for a client to connect
150 */
151 printf( "\n . Waiting for a remote connection" );
152 fflush( stdout );
153
154 if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 )
155 {
156 printf( " failed\n ! net_bind returned %d\n\n", ret );
157 goto exit;
158 }
159
160 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
161 {
162 printf( " failed\n ! net_accept returned %d\n\n", ret );
163 goto exit;
164 }
165
166 /*
167 * 4. Setup the DH parameters (P,G,Ys)
168 */
169 printf( "\n . Sending the server's DH parameters" );
170 fflush( stdout );
171
172 memset( buf, 0, sizeof( buf ) );
173
174 if( ( ret = dhm_make_params( &dhm, 256, buf, &n,
175 havege_rand, &hs ) ) != 0 )
176 {
177 printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
178 goto exit;
179 }
180
181 /*
182 * 5. Sign the parameters and send them
183 */
184 sha1( buf, n, hash );
185
186 buf[n ] = (unsigned char)( rsa.len >> 8 );
187 buf[n + 1] = (unsigned char)( rsa.len );
188
Paul Bakker9dcc3222011-03-08 14:16:06 +0000189 if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, SIG_RSA_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 0, hash, buf + n + 2 ) ) != 0 )
191 {
192 printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
193 goto exit;
194 }
195
196 buflen = n + 2 + rsa.len;
197 buf2[0] = (unsigned char)( buflen >> 8 );
198 buf2[1] = (unsigned char)( buflen );
199
200 if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 ||
Paul Bakker23986e52011-04-24 08:57:21 +0000201 ( ret = net_send( &client_fd, buf, buflen ) ) != (int) buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 {
203 printf( " failed\n ! net_send returned %d\n\n", ret );
204 goto exit;
205 }
206
207 /*
208 * 6. Get the client's public value: Yc = G ^ Xc mod P
209 */
210 printf( "\n . Receiving the client's public value" );
211 fflush( stdout );
212
213 memset( buf, 0, sizeof( buf ) );
214 n = dhm.len;
215
Paul Bakker23986e52011-04-24 08:57:21 +0000216 if( ( ret = net_recv( &client_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 {
218 printf( " failed\n ! net_recv returned %d\n\n", ret );
219 goto exit;
220 }
221
222 if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
223 {
224 printf( " failed\n ! dhm_read_public returned %d\n\n", ret );
225 goto exit;
226 }
227
228 /*
229 * 7. Derive the shared secret: K = Ys ^ Xc mod P
230 */
231 printf( "\n . Shared secret: " );
232 fflush( stdout );
233
234 if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
235 {
236 printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
237 goto exit;
238 }
239
240 for( n = 0; n < 16; n++ )
241 printf( "%02x", buf[n] );
242
243 /*
244 * 8. Setup the AES-256 encryption key
245 *
246 * This is an overly simplified example; best practice is
247 * to hash the shared secret with a random value to derive
248 * the keying material for the encryption/decryption keys
249 * and MACs.
250 */
251 printf( "...\n . Encrypting and sending the ciphertext" );
252 fflush( stdout );
253
254 aes_setkey_enc( &aes, buf, 256 );
255 memcpy( buf, PLAINTEXT, 16 );
256 aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf );
257
258 if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 )
259 {
260 printf( " failed\n ! net_send returned %d\n\n", ret );
261 goto exit;
262 }
263
264 printf( "\n\n" );
265
266exit:
267
268 net_close( client_fd );
269 rsa_free( &rsa );
270 dhm_free( &dhm );
271
Paul Bakkercce9d772011-11-18 14:26:47 +0000272#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 printf( " + Press Enter to exit this program.\n" );
274 fflush( stdout ); getchar();
275#endif
276
277 return( ret );
278}
Paul Bakker5690efc2011-05-26 13:16:06 +0000279#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_HAVEGE_C &&
280 POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
281 POLARSSL_FS_IO */