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