dh_client.c 7.4 KB

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