dh_server.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Diffie-Hellman-Merkle key exchange (server 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_PORT 11999
  38. #define PLAINTEXT "==Hello there!=="
  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 listen_fd = -1;
  57. int client_fd = -1;
  58. unsigned char buf[1024];
  59. unsigned char hash[20];
  60. unsigned char buf2[2];
  61. havege_state hs;
  62. rsa_context rsa;
  63. dhm_context dhm;
  64. aes_context aes;
  65. memset( &rsa, 0, sizeof( rsa ) );
  66. memset( &dhm, 0, sizeof( dhm ) );
  67. /*
  68. * 1. Setup the RNG
  69. */
  70. printf( "\n . Seeding the random number generator" );
  71. fflush( stdout );
  72. havege_init( &hs );
  73. /*
  74. * 2a. Read the server's private RSA key
  75. */
  76. printf( "\n . Reading private key from rsa_priv.txt" );
  77. fflush( stdout );
  78. if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
  79. {
  80. ret = 1;
  81. printf( " failed\n ! Could not open rsa_priv.txt\n" \
  82. " ! Please run rsa_genkey first\n\n" );
  83. goto exit;
  84. }
  85. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  86. if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
  87. ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
  88. ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
  89. ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
  90. ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
  91. ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
  92. ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
  93. ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
  94. {
  95. printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
  96. goto exit;
  97. }
  98. rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
  99. fclose( f );
  100. /*
  101. * 2b. Get the DHM modulus and generator
  102. */
  103. printf( "\n . Reading DH parameters from dh_prime.txt" );
  104. fflush( stdout );
  105. if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
  106. {
  107. ret = 1;
  108. printf( " failed\n ! Could not open dh_prime.txt\n" \
  109. " ! Please run dh_genprime first\n\n" );
  110. goto exit;
  111. }
  112. if( mpi_read_file( &dhm.P, 16, f ) != 0 ||
  113. mpi_read_file( &dhm.G, 16, f ) != 0 )
  114. {
  115. printf( " failed\n ! Invalid DH parameter file\n\n" );
  116. goto exit;
  117. }
  118. fclose( f );
  119. /*
  120. * 3. Wait for a client to connect
  121. */
  122. printf( "\n . Waiting for a remote connection" );
  123. fflush( stdout );
  124. if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 )
  125. {
  126. printf( " failed\n ! net_bind returned %d\n\n", ret );
  127. goto exit;
  128. }
  129. if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
  130. {
  131. printf( " failed\n ! net_accept returned %d\n\n", ret );
  132. goto exit;
  133. }
  134. /*
  135. * 4. Setup the DH parameters (P,G,Ys)
  136. */
  137. printf( "\n . Sending the server's DH parameters" );
  138. fflush( stdout );
  139. memset( buf, 0, sizeof( buf ) );
  140. if( ( ret = dhm_make_params( &dhm, 256, buf, &n,
  141. havege_rand, &hs ) ) != 0 )
  142. {
  143. printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
  144. goto exit;
  145. }
  146. /*
  147. * 5. Sign the parameters and send them
  148. */
  149. sha1( buf, n, hash );
  150. buf[n ] = (unsigned char)( rsa.len >> 8 );
  151. buf[n + 1] = (unsigned char)( rsa.len );
  152. if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, SIG_RSA_SHA1,
  153. 0, hash, buf + n + 2 ) ) != 0 )
  154. {
  155. printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
  156. goto exit;
  157. }
  158. buflen = n + 2 + rsa.len;
  159. buf2[0] = (unsigned char)( buflen >> 8 );
  160. buf2[1] = (unsigned char)( buflen );
  161. if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 ||
  162. ( ret = net_send( &client_fd, buf, buflen ) ) != (int) buflen )
  163. {
  164. printf( " failed\n ! net_send returned %d\n\n", ret );
  165. goto exit;
  166. }
  167. /*
  168. * 6. Get the client's public value: Yc = G ^ Xc mod P
  169. */
  170. printf( "\n . Receiving the client's public value" );
  171. fflush( stdout );
  172. memset( buf, 0, sizeof( buf ) );
  173. n = dhm.len;
  174. if( ( ret = net_recv( &client_fd, buf, n ) ) != (int) n )
  175. {
  176. printf( " failed\n ! net_recv returned %d\n\n", ret );
  177. goto exit;
  178. }
  179. if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
  180. {
  181. printf( " failed\n ! dhm_read_public 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. if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
  190. {
  191. printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
  192. goto exit;
  193. }
  194. for( n = 0; n < 16; n++ )
  195. printf( "%02x", buf[n] );
  196. /*
  197. * 8. Setup the AES-256 encryption key
  198. *
  199. * This is an overly simplified example; best practice is
  200. * to hash the shared secret with a random value to derive
  201. * the keying material for the encryption/decryption keys
  202. * and MACs.
  203. */
  204. printf( "...\n . Encrypting and sending the ciphertext" );
  205. fflush( stdout );
  206. aes_setkey_enc( &aes, buf, 256 );
  207. memcpy( buf, PLAINTEXT, 16 );
  208. aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf );
  209. if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 )
  210. {
  211. printf( " failed\n ! net_send returned %d\n\n", ret );
  212. goto exit;
  213. }
  214. printf( "\n\n" );
  215. exit:
  216. net_close( client_fd );
  217. rsa_free( &rsa );
  218. dhm_free( &dhm );
  219. #ifdef WIN32
  220. printf( " + Press Enter to exit this program.\n" );
  221. fflush( stdout ); getchar();
  222. #endif
  223. return( ret );
  224. }
  225. #endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_HAVEGE_C &&
  226. POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
  227. POLARSSL_FS_IO */