Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (server side) |
| 3 | * |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | * 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 Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 33 | #include "polarssl/config.h" |
| 34 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 35 | #include "polarssl/net.h" |
| 36 | #include "polarssl/aes.h" |
| 37 | #include "polarssl/dhm.h" |
| 38 | #include "polarssl/rsa.h" |
| 39 | #include "polarssl/sha1.h" |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 40 | #include "polarssl/entropy.h" |
| 41 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 42 | |
| 43 | #define SERVER_PORT 11999 |
| 44 | #define PLAINTEXT "==Hello there!==" |
| 45 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 46 | #if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 47 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_NET_C) || \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 48 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 49 | !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 50 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 51 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 52 | ((void) argc); |
| 53 | ((void) argv); |
| 54 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 55 | printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_ENTROPY_C " |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 56 | "and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 57 | "POLARSSL_SHA1_C and/or POLARSSL_FS_IO and/or " |
| 58 | "POLARSSL_CTR_DBRG_C not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 59 | return( 0 ); |
| 60 | } |
| 61 | #else |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 62 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | { |
| 64 | FILE *f; |
| 65 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 66 | int ret; |
| 67 | size_t n, buflen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | int listen_fd = -1; |
| 69 | int client_fd = -1; |
| 70 | |
Paul Bakker | 520ea91 | 2012-10-24 14:17:01 +0000 | [diff] [blame] | 71 | unsigned char buf[2048]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | unsigned char hash[20]; |
| 73 | unsigned char buf2[2]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame^] | 74 | const char *pers = "dh_server"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 75 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 76 | entropy_context entropy; |
| 77 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | rsa_context rsa; |
| 79 | dhm_context dhm; |
| 80 | aes_context aes; |
| 81 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 82 | ((void) argc); |
| 83 | ((void) argv); |
| 84 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | memset( &rsa, 0, sizeof( rsa ) ); |
| 86 | memset( &dhm, 0, sizeof( dhm ) ); |
| 87 | |
| 88 | /* |
| 89 | * 1. Setup the RNG |
| 90 | */ |
| 91 | printf( "\n . Seeding the random number generator" ); |
| 92 | fflush( stdout ); |
| 93 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 94 | entropy_init( &entropy ); |
| 95 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame^] | 96 | (const unsigned char *) pers, |
| 97 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 98 | { |
| 99 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 100 | goto exit; |
| 101 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * 2a. Read the server's private RSA key |
| 105 | */ |
| 106 | printf( "\n . Reading private key from rsa_priv.txt" ); |
| 107 | fflush( stdout ); |
| 108 | |
| 109 | if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) |
| 110 | { |
| 111 | ret = 1; |
| 112 | printf( " failed\n ! Could not open rsa_priv.txt\n" \ |
| 113 | " ! Please run rsa_genkey first\n\n" ); |
| 114 | goto exit; |
| 115 | } |
| 116 | |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 117 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | |
| 119 | if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || |
| 120 | ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || |
| 121 | ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || |
| 122 | ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 || |
| 123 | ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 || |
| 124 | ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 || |
| 125 | ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || |
| 126 | ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) |
| 127 | { |
| 128 | printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
| 129 | goto exit; |
| 130 | } |
| 131 | |
| 132 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 133 | |
| 134 | fclose( f ); |
| 135 | |
| 136 | /* |
| 137 | * 2b. Get the DHM modulus and generator |
| 138 | */ |
| 139 | printf( "\n . Reading DH parameters from dh_prime.txt" ); |
| 140 | fflush( stdout ); |
| 141 | |
| 142 | if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL ) |
| 143 | { |
| 144 | ret = 1; |
| 145 | printf( " failed\n ! Could not open dh_prime.txt\n" \ |
| 146 | " ! Please run dh_genprime first\n\n" ); |
| 147 | goto exit; |
| 148 | } |
| 149 | |
| 150 | if( mpi_read_file( &dhm.P, 16, f ) != 0 || |
| 151 | mpi_read_file( &dhm.G, 16, f ) != 0 ) |
| 152 | { |
| 153 | printf( " failed\n ! Invalid DH parameter file\n\n" ); |
| 154 | goto exit; |
| 155 | } |
| 156 | |
| 157 | fclose( f ); |
| 158 | |
| 159 | /* |
| 160 | * 3. Wait for a client to connect |
| 161 | */ |
| 162 | printf( "\n . Waiting for a remote connection" ); |
| 163 | fflush( stdout ); |
| 164 | |
| 165 | if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 ) |
| 166 | { |
| 167 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 168 | goto exit; |
| 169 | } |
| 170 | |
| 171 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 172 | { |
| 173 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 174 | goto exit; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * 4. Setup the DH parameters (P,G,Ys) |
| 179 | */ |
| 180 | printf( "\n . Sending the server's DH parameters" ); |
| 181 | fflush( stdout ); |
| 182 | |
| 183 | memset( buf, 0, sizeof( buf ) ); |
| 184 | |
Paul Bakker | 520ea91 | 2012-10-24 14:17:01 +0000 | [diff] [blame] | 185 | if( ( ret = dhm_make_params( &dhm, mpi_size( &dhm.P ), buf, &n, |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 186 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | { |
| 188 | printf( " failed\n ! dhm_make_params returned %d\n\n", ret ); |
| 189 | goto exit; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * 5. Sign the parameters and send them |
| 194 | */ |
| 195 | sha1( buf, n, hash ); |
| 196 | |
| 197 | buf[n ] = (unsigned char)( rsa.len >> 8 ); |
| 198 | buf[n + 1] = (unsigned char)( rsa.len ); |
| 199 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 200 | if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | 0, hash, buf + n + 2 ) ) != 0 ) |
| 202 | { |
| 203 | printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret ); |
| 204 | goto exit; |
| 205 | } |
| 206 | |
| 207 | buflen = n + 2 + rsa.len; |
| 208 | buf2[0] = (unsigned char)( buflen >> 8 ); |
| 209 | buf2[1] = (unsigned char)( buflen ); |
| 210 | |
| 211 | if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 || |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 212 | ( ret = net_send( &client_fd, buf, buflen ) ) != (int) buflen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 | { |
| 214 | printf( " failed\n ! net_send returned %d\n\n", ret ); |
| 215 | goto exit; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * 6. Get the client's public value: Yc = G ^ Xc mod P |
| 220 | */ |
| 221 | printf( "\n . Receiving the client's public value" ); |
| 222 | fflush( stdout ); |
| 223 | |
| 224 | memset( buf, 0, sizeof( buf ) ); |
| 225 | n = dhm.len; |
| 226 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 227 | if( ( ret = net_recv( &client_fd, buf, n ) ) != (int) n ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 228 | { |
| 229 | printf( " failed\n ! net_recv returned %d\n\n", ret ); |
| 230 | goto exit; |
| 231 | } |
| 232 | |
| 233 | if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 ) |
| 234 | { |
| 235 | printf( " failed\n ! dhm_read_public returned %d\n\n", ret ); |
| 236 | goto exit; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * 7. Derive the shared secret: K = Ys ^ Xc mod P |
| 241 | */ |
| 242 | printf( "\n . Shared secret: " ); |
| 243 | fflush( stdout ); |
| 244 | |
| 245 | if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 ) |
| 246 | { |
| 247 | printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret ); |
| 248 | goto exit; |
| 249 | } |
| 250 | |
| 251 | for( n = 0; n < 16; n++ ) |
| 252 | printf( "%02x", buf[n] ); |
| 253 | |
| 254 | /* |
| 255 | * 8. Setup the AES-256 encryption key |
| 256 | * |
| 257 | * This is an overly simplified example; best practice is |
| 258 | * to hash the shared secret with a random value to derive |
| 259 | * the keying material for the encryption/decryption keys |
| 260 | * and MACs. |
| 261 | */ |
| 262 | printf( "...\n . Encrypting and sending the ciphertext" ); |
| 263 | fflush( stdout ); |
| 264 | |
| 265 | aes_setkey_enc( &aes, buf, 256 ); |
| 266 | memcpy( buf, PLAINTEXT, 16 ); |
| 267 | aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf ); |
| 268 | |
| 269 | if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 ) |
| 270 | { |
| 271 | printf( " failed\n ! net_send returned %d\n\n", ret ); |
| 272 | goto exit; |
| 273 | } |
| 274 | |
| 275 | printf( "\n\n" ); |
| 276 | |
| 277 | exit: |
| 278 | |
| 279 | net_close( client_fd ); |
| 280 | rsa_free( &rsa ); |
| 281 | dhm_free( &dhm ); |
| 282 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 283 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 284 | printf( " + Press Enter to exit this program.\n" ); |
| 285 | fflush( stdout ); getchar(); |
| 286 | #endif |
| 287 | |
| 288 | return( ret ); |
| 289 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 290 | #endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_ENTROPY_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 291 | POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C && |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 292 | POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */ |