blob: aa89fcf5b8b495562f65449352878ebc3f276aac [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 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_PORT 11999
41#define PLAINTEXT "==Hello there!=="
42
43int main( void )
44{
45 FILE *f;
46
47 int ret, n, buflen;
48 int listen_fd = -1;
49 int client_fd = -1;
50
51 unsigned char buf[1024];
52 unsigned char hash[20];
53 unsigned char buf2[2];
54
55 havege_state hs;
56 rsa_context rsa;
57 dhm_context dhm;
58 aes_context aes;
59
60 memset( &rsa, 0, sizeof( rsa ) );
61 memset( &dhm, 0, sizeof( dhm ) );
62
63 /*
64 * 1. Setup the RNG
65 */
66 printf( "\n . Seeding the random number generator" );
67 fflush( stdout );
68
69 havege_init( &hs );
70
71 /*
72 * 2a. Read the server's private RSA key
73 */
74 printf( "\n . Reading private key from rsa_priv.txt" );
75 fflush( stdout );
76
77 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
78 {
79 ret = 1;
80 printf( " failed\n ! Could not open rsa_priv.txt\n" \
81 " ! Please run rsa_genkey first\n\n" );
82 goto exit;
83 }
84
Paul Bakkera802e1a2010-08-16 11:56:45 +000085 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
88 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
89 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
90 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
91 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
92 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
93 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
94 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
95 {
96 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
97 goto exit;
98 }
99
100 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
101
102 fclose( f );
103
104 /*
105 * 2b. Get the DHM modulus and generator
106 */
107 printf( "\n . Reading DH parameters from dh_prime.txt" );
108 fflush( stdout );
109
110 if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
111 {
112 ret = 1;
113 printf( " failed\n ! Could not open dh_prime.txt\n" \
114 " ! Please run dh_genprime first\n\n" );
115 goto exit;
116 }
117
118 if( mpi_read_file( &dhm.P, 16, f ) != 0 ||
119 mpi_read_file( &dhm.G, 16, f ) != 0 )
120 {
121 printf( " failed\n ! Invalid DH parameter file\n\n" );
122 goto exit;
123 }
124
125 fclose( f );
126
127 /*
128 * 3. Wait for a client to connect
129 */
130 printf( "\n . Waiting for a remote connection" );
131 fflush( stdout );
132
133 if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 )
134 {
135 printf( " failed\n ! net_bind returned %d\n\n", ret );
136 goto exit;
137 }
138
139 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
140 {
141 printf( " failed\n ! net_accept returned %d\n\n", ret );
142 goto exit;
143 }
144
145 /*
146 * 4. Setup the DH parameters (P,G,Ys)
147 */
148 printf( "\n . Sending the server's DH parameters" );
149 fflush( stdout );
150
151 memset( buf, 0, sizeof( buf ) );
152
153 if( ( ret = dhm_make_params( &dhm, 256, buf, &n,
154 havege_rand, &hs ) ) != 0 )
155 {
156 printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
157 goto exit;
158 }
159
160 /*
161 * 5. Sign the parameters and send them
162 */
163 sha1( buf, n, hash );
164
165 buf[n ] = (unsigned char)( rsa.len >> 8 );
166 buf[n + 1] = (unsigned char)( rsa.len );
167
Paul Bakker4593aea2009-02-09 22:32:35 +0000168 if( ( ret = rsa_pkcs1_sign( &rsa, RSA_PRIVATE, SIG_RSA_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 0, hash, buf + n + 2 ) ) != 0 )
170 {
171 printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
172 goto exit;
173 }
174
175 buflen = n + 2 + rsa.len;
176 buf2[0] = (unsigned char)( buflen >> 8 );
177 buf2[1] = (unsigned char)( buflen );
178
179 if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 ||
180 ( ret = net_send( &client_fd, buf, buflen ) ) != buflen )
181 {
182 printf( " failed\n ! net_send returned %d\n\n", ret );
183 goto exit;
184 }
185
186 /*
187 * 6. Get the client's public value: Yc = G ^ Xc mod P
188 */
189 printf( "\n . Receiving the client's public value" );
190 fflush( stdout );
191
192 memset( buf, 0, sizeof( buf ) );
193 n = dhm.len;
194
195 if( ( ret = net_recv( &client_fd, buf, n ) ) != n )
196 {
197 printf( " failed\n ! net_recv returned %d\n\n", ret );
198 goto exit;
199 }
200
201 if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
202 {
203 printf( " failed\n ! dhm_read_public returned %d\n\n", ret );
204 goto exit;
205 }
206
207 /*
208 * 7. Derive the shared secret: K = Ys ^ Xc mod P
209 */
210 printf( "\n . Shared secret: " );
211 fflush( stdout );
212
213 if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
214 {
215 printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
216 goto exit;
217 }
218
219 for( n = 0; n < 16; n++ )
220 printf( "%02x", buf[n] );
221
222 /*
223 * 8. Setup the AES-256 encryption key
224 *
225 * This is an overly simplified example; best practice is
226 * to hash the shared secret with a random value to derive
227 * the keying material for the encryption/decryption keys
228 * and MACs.
229 */
230 printf( "...\n . Encrypting and sending the ciphertext" );
231 fflush( stdout );
232
233 aes_setkey_enc( &aes, buf, 256 );
234 memcpy( buf, PLAINTEXT, 16 );
235 aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf );
236
237 if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 )
238 {
239 printf( " failed\n ! net_send returned %d\n\n", ret );
240 goto exit;
241 }
242
243 printf( "\n\n" );
244
245exit:
246
247 net_close( client_fd );
248 rsa_free( &rsa );
249 dhm_free( &dhm );
250
251#ifdef WIN32
252 printf( " + Press Enter to exit this program.\n" );
253 fflush( stdout ); getchar();
254#endif
255
256 return( ret );
257}