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