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