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