Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (client side) |
| 3 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, 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 | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 33 | #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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 | |
| 40 | #define SERVER_NAME "localhost" |
| 41 | #define SERVER_PORT 11999 |
| 42 | |
| 43 | int main( void ) |
| 44 | { |
| 45 | FILE *f; |
| 46 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 47 | int ret; |
| 48 | size_t n, buflen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 | int server_fd = -1; |
| 50 | |
| 51 | unsigned char *p, *end; |
| 52 | unsigned char buf[1024]; |
| 53 | unsigned char hash[20]; |
| 54 | |
| 55 | havege_state hs; |
| 56 | rsa_context rsa; |
| 57 | dhm_context dhm; |
| 58 | aes_context aes; |
| 59 | |
| 60 | memset( &rsa, 0, sizeof( rsa ) ); |
| 61 | memset( &dhm, 0, sizeof( dhm ) ); |
| 62 | |
| 63 | /* |
| 64 | * 1. Setup the RNG |
| 65 | */ |
| 66 | printf( "\n . Seeding the random number generator" ); |
| 67 | fflush( stdout ); |
| 68 | |
| 69 | havege_init( &hs ); |
| 70 | |
| 71 | /* |
| 72 | * 2. Read the server's public RSA key |
| 73 | */ |
| 74 | printf( "\n . Reading public key from rsa_pub.txt" ); |
| 75 | fflush( stdout ); |
| 76 | |
| 77 | if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) |
| 78 | { |
| 79 | ret = 1; |
| 80 | printf( " failed\n ! Could not open rsa_pub.txt\n" \ |
| 81 | " ! Please run rsa_genkey first\n\n" ); |
| 82 | goto exit; |
| 83 | } |
| 84 | |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 85 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | |
| 87 | if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || |
| 88 | ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) |
| 89 | { |
| 90 | printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
| 91 | goto exit; |
| 92 | } |
| 93 | |
| 94 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 95 | |
| 96 | fclose( f ); |
| 97 | |
| 98 | /* |
| 99 | * 3. Initiate the connection |
| 100 | */ |
| 101 | printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME, |
| 102 | SERVER_PORT ); |
| 103 | fflush( stdout ); |
| 104 | |
| 105 | if( ( ret = net_connect( &server_fd, SERVER_NAME, |
| 106 | SERVER_PORT ) ) != 0 ) |
| 107 | { |
| 108 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 109 | goto exit; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * 4a. First get the buffer length |
| 114 | */ |
| 115 | printf( "\n . Receiving the server's DH parameters" ); |
| 116 | fflush( stdout ); |
| 117 | |
| 118 | memset( buf, 0, sizeof( buf ) ); |
| 119 | |
| 120 | if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 ) |
| 121 | { |
| 122 | printf( " failed\n ! net_recv returned %d\n\n", ret ); |
| 123 | goto exit; |
| 124 | } |
| 125 | |
| 126 | n = buflen = ( buf[0] << 8 ) | buf[1]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 127 | if( buflen < 1 || buflen > sizeof( buf ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | { |
| 129 | printf( " failed\n ! Got an invalid buffer length\n\n" ); |
| 130 | goto exit; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P |
| 135 | */ |
| 136 | memset( buf, 0, sizeof( buf ) ); |
| 137 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 138 | if( ( ret = net_recv( &server_fd, buf, n ) ) != (int) n ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | { |
| 140 | printf( " failed\n ! net_recv returned %d\n\n", ret ); |
| 141 | goto exit; |
| 142 | } |
| 143 | |
| 144 | p = buf, end = buf + buflen; |
| 145 | |
| 146 | if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 ) |
| 147 | { |
| 148 | printf( " failed\n ! dhm_read_params returned %d\n\n", ret ); |
| 149 | goto exit; |
| 150 | } |
| 151 | |
| 152 | if( dhm.len < 64 || dhm.len > 256 ) |
| 153 | { |
| 154 | ret = 1; |
| 155 | printf( " failed\n ! Invalid DHM modulus size\n\n" ); |
| 156 | goto exit; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * 5. Check that the server's RSA signature matches |
| 161 | * the SHA-1 hash of (P,G,Ys) |
| 162 | */ |
| 163 | printf( "\n . Verifying the server's RSA signature" ); |
| 164 | fflush( stdout ); |
| 165 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 166 | if( ( n = (size_t) ( end - p ) ) != rsa.len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 167 | { |
| 168 | ret = 1; |
| 169 | printf( " failed\n ! Invalid RSA signature size\n\n" ); |
| 170 | goto exit; |
| 171 | } |
| 172 | |
| 173 | sha1( buf, (int)( p - 2 - buf ), hash ); |
| 174 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 175 | if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | 0, hash, p ) ) != 0 ) |
| 177 | { |
| 178 | printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret ); |
| 179 | goto exit; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * 6. Send our public value: Yc = G ^ Xc mod P |
| 184 | */ |
| 185 | printf( "\n . Sending own public value to server" ); |
| 186 | fflush( stdout ); |
| 187 | |
| 188 | n = dhm.len; |
| 189 | if( ( ret = dhm_make_public( &dhm, 256, buf, n, |
| 190 | havege_rand, &hs ) ) != 0 ) |
| 191 | { |
| 192 | printf( " failed\n ! dhm_make_public returned %d\n\n", ret ); |
| 193 | goto exit; |
| 194 | } |
| 195 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 196 | if( ( ret = net_send( &server_fd, buf, n ) ) != (int) n ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 197 | { |
| 198 | printf( " failed\n ! net_send returned %d\n\n", ret ); |
| 199 | goto exit; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * 7. Derive the shared secret: K = Ys ^ Xc mod P |
| 204 | */ |
| 205 | printf( "\n . Shared secret: " ); |
| 206 | fflush( stdout ); |
| 207 | |
| 208 | n = dhm.len; |
| 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 decryption 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 | * IVs and MACs. |
| 225 | */ |
| 226 | printf( "...\n . Receiving and decrypting the ciphertext" ); |
| 227 | fflush( stdout ); |
| 228 | |
| 229 | aes_setkey_dec( &aes, buf, 256 ); |
| 230 | |
| 231 | memset( buf, 0, sizeof( buf ) ); |
| 232 | |
| 233 | if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 ) |
| 234 | { |
| 235 | printf( " failed\n ! net_recv returned %d\n\n", ret ); |
| 236 | goto exit; |
| 237 | } |
| 238 | |
| 239 | aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf ); |
| 240 | buf[16] = '\0'; |
| 241 | printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf ); |
| 242 | |
| 243 | exit: |
| 244 | |
| 245 | net_close( server_fd ); |
| 246 | rsa_free( &rsa ); |
| 247 | dhm_free( &dhm ); |
| 248 | |
| 249 | #ifdef WIN32 |
| 250 | printf( " + Press Enter to exit this program.\n" ); |
| 251 | fflush( stdout ); getchar(); |
| 252 | #endif |
| 253 | |
| 254 | return( ret ); |
| 255 | } |