dh_server.c 9.2 KB

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