dh_client.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Diffie-Hellman-Merkle key exchange (client side)
  3. *
  4. * Copyright (C) 2006-2010, 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. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include "polarssl/config.h"
  31. #include "polarssl/net.h"
  32. #include "polarssl/aes.h"
  33. #include "polarssl/dhm.h"
  34. #include "polarssl/rsa.h"
  35. #include "polarssl/sha1.h"
  36. #include "polarssl/havege.h"
  37. #define SERVER_NAME "localhost"
  38. #define SERVER_PORT 11999
  39. #if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \
  40. !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_NET_C) || \
  41. !defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \
  42. !defined(POLARSSL_FS_IO)
  43. int main( void )
  44. {
  45. printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_HAVEGE_C "
  46. "and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
  47. "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n");
  48. return( 0 );
  49. }
  50. #else
  51. int main( void )
  52. {
  53. FILE *f;
  54. int ret;
  55. size_t n, buflen;
  56. int server_fd = -1;
  57. unsigned char *p, *end;
  58. unsigned char buf[1024];
  59. unsigned char hash[20];
  60. havege_state hs;
  61. rsa_context rsa;
  62. dhm_context dhm;
  63. aes_context aes;
  64. memset( &rsa, 0, sizeof( rsa ) );
  65. memset( &dhm, 0, sizeof( dhm ) );
  66. /*
  67. * 1. Setup the RNG
  68. */
  69. printf( "\n . Seeding the random number generator" );
  70. fflush( stdout );
  71. havege_init( &hs );
  72. /*
  73. * 2. Read the server's public RSA key
  74. */
  75. printf( "\n . Reading public key from rsa_pub.txt" );
  76. fflush( stdout );
  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. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  85. if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
  86. ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
  87. {
  88. printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
  89. goto exit;
  90. }
  91. rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
  92. fclose( f );
  93. /*
  94. * 3. Initiate the connection
  95. */
  96. printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME,
  97. SERVER_PORT );
  98. fflush( stdout );
  99. if( ( ret = net_connect( &server_fd, SERVER_NAME,
  100. SERVER_PORT ) ) != 0 )
  101. {
  102. printf( " failed\n ! net_connect returned %d\n\n", ret );
  103. goto exit;
  104. }
  105. /*
  106. * 4a. First get the buffer length
  107. */
  108. printf( "\n . Receiving the server's DH parameters" );
  109. fflush( stdout );
  110. memset( buf, 0, sizeof( buf ) );
  111. if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 )
  112. {
  113. printf( " failed\n ! net_recv returned %d\n\n", ret );
  114. goto exit;
  115. }
  116. n = buflen = ( buf[0] << 8 ) | buf[1];
  117. if( buflen < 1 || buflen > sizeof( buf ) )
  118. {
  119. printf( " failed\n ! Got an invalid buffer length\n\n" );
  120. goto exit;
  121. }
  122. /*
  123. * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
  124. */
  125. memset( buf, 0, sizeof( buf ) );
  126. if( ( ret = net_recv( &server_fd, buf, n ) ) != (int) n )
  127. {
  128. printf( " failed\n ! net_recv returned %d\n\n", ret );
  129. goto exit;
  130. }
  131. p = buf, end = buf + buflen;
  132. if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 )
  133. {
  134. printf( " failed\n ! dhm_read_params returned %d\n\n", ret );
  135. goto exit;
  136. }
  137. if( dhm.len < 64 || dhm.len > 256 )
  138. {
  139. ret = 1;
  140. printf( " failed\n ! Invalid DHM modulus size\n\n" );
  141. goto exit;
  142. }
  143. /*
  144. * 5. Check that the server's RSA signature matches
  145. * the SHA-1 hash of (P,G,Ys)
  146. */
  147. printf( "\n . Verifying the server's RSA signature" );
  148. fflush( stdout );
  149. if( ( n = (size_t) ( end - p ) ) != rsa.len )
  150. {
  151. ret = 1;
  152. printf( " failed\n ! Invalid RSA signature size\n\n" );
  153. goto exit;
  154. }
  155. sha1( buf, (int)( p - 2 - buf ), hash );
  156. if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
  157. 0, hash, p ) ) != 0 )
  158. {
  159. printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
  160. goto exit;
  161. }
  162. /*
  163. * 6. Send our public value: Yc = G ^ Xc mod P
  164. */
  165. printf( "\n . Sending own public value to server" );
  166. fflush( stdout );
  167. n = dhm.len;
  168. if( ( ret = dhm_make_public( &dhm, 256, buf, n,
  169. havege_rand, &hs ) ) != 0 )
  170. {
  171. printf( " failed\n ! dhm_make_public returned %d\n\n", ret );
  172. goto exit;
  173. }
  174. if( ( ret = net_send( &server_fd, buf, n ) ) != (int) n )
  175. {
  176. printf( " failed\n ! net_send returned %d\n\n", ret );
  177. goto exit;
  178. }
  179. /*
  180. * 7. Derive the shared secret: K = Ys ^ Xc mod P
  181. */
  182. printf( "\n . Shared secret: " );
  183. fflush( stdout );
  184. n = dhm.len;
  185. if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
  186. {
  187. printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
  188. goto exit;
  189. }
  190. for( n = 0; n < 16; n++ )
  191. printf( "%02x", buf[n] );
  192. /*
  193. * 8. Setup the AES-256 decryption key
  194. *
  195. * This is an overly simplified example; best practice is
  196. * to hash the shared secret with a random value to derive
  197. * the keying material for the encryption/decryption keys,
  198. * IVs and MACs.
  199. */
  200. printf( "...\n . Receiving and decrypting the ciphertext" );
  201. fflush( stdout );
  202. aes_setkey_dec( &aes, buf, 256 );
  203. memset( buf, 0, sizeof( buf ) );
  204. if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 )
  205. {
  206. printf( " failed\n ! net_recv returned %d\n\n", ret );
  207. goto exit;
  208. }
  209. aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf );
  210. buf[16] = '\0';
  211. printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
  212. exit:
  213. net_close( server_fd );
  214. rsa_free( &rsa );
  215. dhm_free( &dhm );
  216. #ifdef WIN32
  217. printf( " + Press Enter to exit this program.\n" );
  218. fflush( stdout ); getchar();
  219. #endif
  220. return( ret );
  221. }
  222. #endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_HAVEGE_C &&
  223. POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
  224. POLARSSL_FS_IO */