dh_server.c 7.7 KB

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