dh_server.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Diffie-Hellman-Merkle key exchange (server side)
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #if defined(MBEDTLS_PLATFORM_C)
  25. #include "mbedtls/platform.h"
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define mbedtls_printf printf
  30. #define mbedtls_time_t time_t
  31. #define mbedtls_exit exit
  32. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  33. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  34. #endif /* MBEDTLS_PLATFORM_C */
  35. #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
  36. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
  37. defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \
  38. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
  39. defined(MBEDTLS_SHA1_C)
  40. #include "mbedtls/net_sockets.h"
  41. #include "mbedtls/aes.h"
  42. #include "mbedtls/dhm.h"
  43. #include "mbedtls/rsa.h"
  44. #include "mbedtls/sha1.h"
  45. #include "mbedtls/entropy.h"
  46. #include "mbedtls/ctr_drbg.h"
  47. #include <stdio.h>
  48. #include <string.h>
  49. #endif
  50. #define SERVER_PORT "11999"
  51. #define PLAINTEXT "==Hello there!=="
  52. #if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
  53. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
  54. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
  55. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
  56. !defined(MBEDTLS_SHA1_C)
  57. int main( void )
  58. {
  59. mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
  60. "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  61. "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
  62. "MBEDTLS_CTR_DRBG_C not defined.\n");
  63. mbedtls_exit( 0 );
  64. }
  65. #else
  66. int main( void )
  67. {
  68. FILE *f;
  69. int ret = 1;
  70. int exit_code = MBEDTLS_EXIT_FAILURE;
  71. size_t n, buflen;
  72. mbedtls_net_context listen_fd, client_fd;
  73. unsigned char buf[2048];
  74. unsigned char hash[32];
  75. unsigned char buf2[2];
  76. const char *pers = "dh_server";
  77. mbedtls_entropy_context entropy;
  78. mbedtls_ctr_drbg_context ctr_drbg;
  79. mbedtls_rsa_context rsa;
  80. mbedtls_dhm_context dhm;
  81. mbedtls_aes_context aes;
  82. mbedtls_mpi N, P, Q, D, E;
  83. mbedtls_net_init( &listen_fd );
  84. mbedtls_net_init( &client_fd );
  85. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256 );
  86. mbedtls_dhm_init( &dhm );
  87. mbedtls_aes_init( &aes );
  88. mbedtls_ctr_drbg_init( &ctr_drbg );
  89. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  90. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
  91. /*
  92. * 1. Setup the RNG
  93. */
  94. mbedtls_printf( "\n . Seeding the random number generator" );
  95. fflush( stdout );
  96. mbedtls_entropy_init( &entropy );
  97. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  98. (const unsigned char *) pers,
  99. strlen( pers ) ) ) != 0 )
  100. {
  101. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  102. goto exit;
  103. }
  104. /*
  105. * 2a. Read the server's private RSA key
  106. */
  107. mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
  108. fflush( stdout );
  109. if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
  110. {
  111. mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
  112. " ! Please run rsa_genkey first\n\n" );
  113. goto exit;
  114. }
  115. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  116. if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
  117. ( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
  118. ( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
  119. ( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
  120. ( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 )
  121. {
  122. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  123. ret );
  124. fclose( f );
  125. goto exit;
  126. }
  127. fclose( f );
  128. if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
  129. {
  130. mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
  131. ret );
  132. goto exit;
  133. }
  134. if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
  135. {
  136. mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n",
  137. ret );
  138. goto exit;
  139. }
  140. /*
  141. * 2b. Get the DHM modulus and generator
  142. */
  143. mbedtls_printf( "\n . Reading DH parameters from dh_prime.txt" );
  144. fflush( stdout );
  145. if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
  146. {
  147. mbedtls_printf( " failed\n ! Could not open dh_prime.txt\n" \
  148. " ! Please run dh_genprime first\n\n" );
  149. goto exit;
  150. }
  151. if( mbedtls_mpi_read_file( &dhm.P, 16, f ) != 0 ||
  152. mbedtls_mpi_read_file( &dhm.G, 16, f ) != 0 )
  153. {
  154. mbedtls_printf( " failed\n ! Invalid DH parameter file\n\n" );
  155. fclose( f );
  156. goto exit;
  157. }
  158. fclose( f );
  159. /*
  160. * 3. Wait for a client to connect
  161. */
  162. mbedtls_printf( "\n . Waiting for a remote connection" );
  163. fflush( stdout );
  164. if( ( ret = mbedtls_net_bind( &listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
  165. {
  166. mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
  167. goto exit;
  168. }
  169. if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
  170. NULL, 0, NULL ) ) != 0 )
  171. {
  172. mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
  173. goto exit;
  174. }
  175. /*
  176. * 4. Setup the DH parameters (P,G,Ys)
  177. */
  178. mbedtls_printf( "\n . Sending the server's DH parameters" );
  179. fflush( stdout );
  180. memset( buf, 0, sizeof( buf ) );
  181. if( ( ret = mbedtls_dhm_make_params( &dhm, (int) mbedtls_mpi_size( &dhm.P ), buf, &n,
  182. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  183. {
  184. mbedtls_printf( " failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret );
  185. goto exit;
  186. }
  187. /*
  188. * 5. Sign the parameters and send them
  189. */
  190. if( ( ret = mbedtls_sha1_ret( buf, n, hash ) ) != 0 )
  191. {
  192. mbedtls_printf( " failed\n ! mbedtls_sha1_ret returned %d\n\n", ret );
  193. goto exit;
  194. }
  195. buf[n ] = (unsigned char)( rsa.len >> 8 );
  196. buf[n + 1] = (unsigned char)( rsa.len );
  197. if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
  198. 0, hash, buf + n + 2 ) ) != 0 )
  199. {
  200. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_sign returned %d\n\n", ret );
  201. goto exit;
  202. }
  203. buflen = n + 2 + rsa.len;
  204. buf2[0] = (unsigned char)( buflen >> 8 );
  205. buf2[1] = (unsigned char)( buflen );
  206. if( ( ret = mbedtls_net_send( &client_fd, buf2, 2 ) ) != 2 ||
  207. ( ret = mbedtls_net_send( &client_fd, buf, buflen ) ) != (int) buflen )
  208. {
  209. mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
  210. goto exit;
  211. }
  212. /*
  213. * 6. Get the client's public value: Yc = G ^ Xc mod P
  214. */
  215. mbedtls_printf( "\n . Receiving the client's public value" );
  216. fflush( stdout );
  217. memset( buf, 0, sizeof( buf ) );
  218. n = dhm.len;
  219. if( ( ret = mbedtls_net_recv( &client_fd, buf, n ) ) != (int) n )
  220. {
  221. mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
  222. goto exit;
  223. }
  224. if( ( ret = mbedtls_dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
  225. {
  226. mbedtls_printf( " failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret );
  227. goto exit;
  228. }
  229. /*
  230. * 7. Derive the shared secret: K = Ys ^ Xc mod P
  231. */
  232. mbedtls_printf( "\n . Shared secret: " );
  233. fflush( stdout );
  234. if( ( ret = mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &n,
  235. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  236. {
  237. mbedtls_printf( " failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret );
  238. goto exit;
  239. }
  240. for( n = 0; n < 16; n++ )
  241. mbedtls_printf( "%02x", buf[n] );
  242. /*
  243. * 8. Setup the AES-256 encryption key
  244. *
  245. * This is an overly simplified example; best practice is
  246. * to hash the shared secret with a random value to derive
  247. * the keying material for the encryption/decryption keys
  248. * and MACs.
  249. */
  250. mbedtls_printf( "...\n . Encrypting and sending the ciphertext" );
  251. fflush( stdout );
  252. ret = mbedtls_aes_setkey_enc( &aes, buf, 256 );
  253. if( ret != 0 )
  254. goto exit;
  255. memcpy( buf, PLAINTEXT, 16 );
  256. ret = mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_ENCRYPT, buf, buf );
  257. if( ret != 0 )
  258. goto exit;
  259. if( ( ret = mbedtls_net_send( &client_fd, buf, 16 ) ) != 16 )
  260. {
  261. mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
  262. goto exit;
  263. }
  264. mbedtls_printf( "\n\n" );
  265. exit_code = MBEDTLS_EXIT_SUCCESS;
  266. exit:
  267. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  268. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
  269. mbedtls_net_free( &client_fd );
  270. mbedtls_net_free( &listen_fd );
  271. mbedtls_aes_free( &aes );
  272. mbedtls_rsa_free( &rsa );
  273. mbedtls_dhm_free( &dhm );
  274. mbedtls_ctr_drbg_free( &ctr_drbg );
  275. mbedtls_entropy_free( &entropy );
  276. #if defined(_WIN32)
  277. mbedtls_printf( " + Press Enter to exit this program.\n" );
  278. fflush( stdout ); getchar();
  279. #endif
  280. mbedtls_exit( exit_code );
  281. }
  282. #endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
  283. MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  284. MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */