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