blob: abea37375281ea05cc27fcddcca3c3332b0a3cd2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (server side)
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakker5121ce52009-01-03 21:22:43 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef _CRT_SECURE_NO_DEPRECATE
25#define _CRT_SECURE_NO_DEPRECATE 1
26#endif
27
28#include <string.h>
29#include <stdio.h>
30
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/net.h"
32#include "polarssl/aes.h"
33#include "polarssl/dhm.h"
34#include "polarssl/rsa.h"
35#include "polarssl/sha1.h"
36#include "polarssl/havege.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
38#define SERVER_PORT 11999
39#define PLAINTEXT "==Hello there!=="
40
41int main( void )
42{
43 FILE *f;
44
45 int ret, n, buflen;
46 int listen_fd = -1;
47 int client_fd = -1;
48
49 unsigned char buf[1024];
50 unsigned char hash[20];
51 unsigned char buf2[2];
52
53 havege_state hs;
54 rsa_context rsa;
55 dhm_context dhm;
56 aes_context aes;
57
58 memset( &rsa, 0, sizeof( rsa ) );
59 memset( &dhm, 0, sizeof( dhm ) );
60
61 /*
62 * 1. Setup the RNG
63 */
64 printf( "\n . Seeding the random number generator" );
65 fflush( stdout );
66
67 havege_init( &hs );
68
69 /*
70 * 2a. Read the server's private RSA key
71 */
72 printf( "\n . Reading private key from rsa_priv.txt" );
73 fflush( stdout );
74
75 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
76 {
77 ret = 1;
78 printf( " failed\n ! Could not open rsa_priv.txt\n" \
79 " ! Please run rsa_genkey first\n\n" );
80 goto exit;
81 }
82
83 rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL );
84
85 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
86 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
87 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
88 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
89 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
90 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
91 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
92 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
93 {
94 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
95 goto exit;
96 }
97
98 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
99
100 fclose( f );
101
102 /*
103 * 2b. Get the DHM modulus and generator
104 */
105 printf( "\n . Reading DH parameters from dh_prime.txt" );
106 fflush( stdout );
107
108 if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
109 {
110 ret = 1;
111 printf( " failed\n ! Could not open dh_prime.txt\n" \
112 " ! Please run dh_genprime first\n\n" );
113 goto exit;
114 }
115
116 if( mpi_read_file( &dhm.P, 16, f ) != 0 ||
117 mpi_read_file( &dhm.G, 16, f ) != 0 )
118 {
119 printf( " failed\n ! Invalid DH parameter file\n\n" );
120 goto exit;
121 }
122
123 fclose( f );
124
125 /*
126 * 3. Wait for a client to connect
127 */
128 printf( "\n . Waiting for a remote connection" );
129 fflush( stdout );
130
131 if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 )
132 {
133 printf( " failed\n ! net_bind returned %d\n\n", ret );
134 goto exit;
135 }
136
137 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
138 {
139 printf( " failed\n ! net_accept returned %d\n\n", ret );
140 goto exit;
141 }
142
143 /*
144 * 4. Setup the DH parameters (P,G,Ys)
145 */
146 printf( "\n . Sending the server's DH parameters" );
147 fflush( stdout );
148
149 memset( buf, 0, sizeof( buf ) );
150
151 if( ( ret = dhm_make_params( &dhm, 256, buf, &n,
152 havege_rand, &hs ) ) != 0 )
153 {
154 printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
155 goto exit;
156 }
157
158 /*
159 * 5. Sign the parameters and send them
160 */
161 sha1( buf, n, hash );
162
163 buf[n ] = (unsigned char)( rsa.len >> 8 );
164 buf[n + 1] = (unsigned char)( rsa.len );
165
Paul Bakker4593aea2009-02-09 22:32:35 +0000166 if( ( ret = rsa_pkcs1_sign( &rsa, RSA_PRIVATE, SIG_RSA_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 0, hash, buf + n + 2 ) ) != 0 )
168 {
169 printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
170 goto exit;
171 }
172
173 buflen = n + 2 + rsa.len;
174 buf2[0] = (unsigned char)( buflen >> 8 );
175 buf2[1] = (unsigned char)( buflen );
176
177 if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 ||
178 ( ret = net_send( &client_fd, buf, buflen ) ) != buflen )
179 {
180 printf( " failed\n ! net_send returned %d\n\n", ret );
181 goto exit;
182 }
183
184 /*
185 * 6. Get the client's public value: Yc = G ^ Xc mod P
186 */
187 printf( "\n . Receiving the client's public value" );
188 fflush( stdout );
189
190 memset( buf, 0, sizeof( buf ) );
191 n = dhm.len;
192
193 if( ( ret = net_recv( &client_fd, buf, n ) ) != n )
194 {
195 printf( " failed\n ! net_recv returned %d\n\n", ret );
196 goto exit;
197 }
198
199 if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
200 {
201 printf( " failed\n ! dhm_read_public returned %d\n\n", ret );
202 goto exit;
203 }
204
205 /*
206 * 7. Derive the shared secret: K = Ys ^ Xc mod P
207 */
208 printf( "\n . Shared secret: " );
209 fflush( stdout );
210
211 if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
212 {
213 printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
214 goto exit;
215 }
216
217 for( n = 0; n < 16; n++ )
218 printf( "%02x", buf[n] );
219
220 /*
221 * 8. Setup the AES-256 encryption key
222 *
223 * This is an overly simplified example; best practice is
224 * to hash the shared secret with a random value to derive
225 * the keying material for the encryption/decryption keys
226 * and MACs.
227 */
228 printf( "...\n . Encrypting and sending the ciphertext" );
229 fflush( stdout );
230
231 aes_setkey_enc( &aes, buf, 256 );
232 memcpy( buf, PLAINTEXT, 16 );
233 aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf );
234
235 if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 )
236 {
237 printf( " failed\n ! net_send returned %d\n\n", ret );
238 goto exit;
239 }
240
241 printf( "\n\n" );
242
243exit:
244
245 net_close( client_fd );
246 rsa_free( &rsa );
247 dhm_free( &dhm );
248
249#ifdef WIN32
250 printf( " + Press Enter to exit this program.\n" );
251 fflush( stdout ); getchar();
252#endif
253
254 return( ret );
255}