dh_server.c 10 KB

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