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