dh_client.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Diffie-Hellman-Merkle key exchange (client 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_NAME "localhost"
  51. #define SERVER_PORT "11999"
  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 server_fd;
  73. unsigned char *p, *end;
  74. unsigned char buf[2048];
  75. unsigned char hash[32];
  76. const char *pers = "dh_client";
  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_net_init( &server_fd );
  83. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256 );
  84. mbedtls_dhm_init( &dhm );
  85. mbedtls_aes_init( &aes );
  86. mbedtls_ctr_drbg_init( &ctr_drbg );
  87. /*
  88. * 1. Setup the RNG
  89. */
  90. mbedtls_printf( "\n . Seeding the random number generator" );
  91. fflush( stdout );
  92. mbedtls_entropy_init( &entropy );
  93. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  94. (const unsigned char *) pers,
  95. strlen( pers ) ) ) != 0 )
  96. {
  97. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  98. goto exit;
  99. }
  100. /*
  101. * 2. Read the server's public RSA key
  102. */
  103. mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
  104. fflush( stdout );
  105. if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
  106. {
  107. mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
  108. " ! Please run rsa_genkey first\n\n" );
  109. goto exit;
  110. }
  111. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  112. if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
  113. ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
  114. {
  115. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
  116. fclose( f );
  117. goto exit;
  118. }
  119. rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
  120. fclose( f );
  121. /*
  122. * 3. Initiate the connection
  123. */
  124. mbedtls_printf( "\n . Connecting to tcp/%s/%s", SERVER_NAME,
  125. SERVER_PORT );
  126. fflush( stdout );
  127. if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
  128. SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
  129. {
  130. mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
  131. goto exit;
  132. }
  133. /*
  134. * 4a. First get the buffer length
  135. */
  136. mbedtls_printf( "\n . Receiving the server's DH parameters" );
  137. fflush( stdout );
  138. memset( buf, 0, sizeof( buf ) );
  139. if( ( ret = mbedtls_net_recv( &server_fd, buf, 2 ) ) != 2 )
  140. {
  141. mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
  142. goto exit;
  143. }
  144. n = buflen = ( buf[0] << 8 ) | buf[1];
  145. if( buflen < 1 || buflen > sizeof( buf ) )
  146. {
  147. mbedtls_printf( " failed\n ! Got an invalid buffer length\n\n" );
  148. goto exit;
  149. }
  150. /*
  151. * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
  152. */
  153. memset( buf, 0, sizeof( buf ) );
  154. if( ( ret = mbedtls_net_recv( &server_fd, buf, n ) ) != (int) n )
  155. {
  156. mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
  157. goto exit;
  158. }
  159. p = buf, end = buf + buflen;
  160. if( ( ret = mbedtls_dhm_read_params( &dhm, &p, end ) ) != 0 )
  161. {
  162. mbedtls_printf( " failed\n ! mbedtls_dhm_read_params returned %d\n\n", ret );
  163. goto exit;
  164. }
  165. if( dhm.len < 64 || dhm.len > 512 )
  166. {
  167. mbedtls_printf( " failed\n ! Invalid DHM modulus size\n\n" );
  168. goto exit;
  169. }
  170. /*
  171. * 5. Check that the server's RSA signature matches
  172. * the SHA-256 hash of (P,G,Ys)
  173. */
  174. mbedtls_printf( "\n . Verifying the server's RSA signature" );
  175. fflush( stdout );
  176. p += 2;
  177. if( ( n = (size_t) ( end - p ) ) != rsa.len )
  178. {
  179. mbedtls_printf( " failed\n ! Invalid RSA signature size\n\n" );
  180. goto exit;
  181. }
  182. if( ( ret = mbedtls_sha1_ret( buf, (int)( p - 2 - buf ), hash ) ) != 0 )
  183. {
  184. mbedtls_printf( " failed\n ! mbedtls_sha1_ret returned %d\n\n", ret );
  185. goto exit;
  186. }
  187. if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
  188. MBEDTLS_MD_SHA256, 0, hash, p ) ) != 0 )
  189. {
  190. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned %d\n\n", ret );
  191. goto exit;
  192. }
  193. /*
  194. * 6. Send our public value: Yc = G ^ Xc mod P
  195. */
  196. mbedtls_printf( "\n . Sending own public value to server" );
  197. fflush( stdout );
  198. n = dhm.len;
  199. if( ( ret = mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, n,
  200. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  201. {
  202. mbedtls_printf( " failed\n ! mbedtls_dhm_make_public returned %d\n\n", ret );
  203. goto exit;
  204. }
  205. if( ( ret = mbedtls_net_send( &server_fd, buf, n ) ) != (int) n )
  206. {
  207. mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
  208. goto exit;
  209. }
  210. /*
  211. * 7. Derive the shared secret: K = Ys ^ Xc mod P
  212. */
  213. mbedtls_printf( "\n . Shared secret: " );
  214. fflush( stdout );
  215. if( ( ret = mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &n,
  216. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  217. {
  218. mbedtls_printf( " failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret );
  219. goto exit;
  220. }
  221. for( n = 0; n < 16; n++ )
  222. mbedtls_printf( "%02x", buf[n] );
  223. /*
  224. * 8. Setup the AES-256 decryption key
  225. *
  226. * This is an overly simplified example; best practice is
  227. * to hash the shared secret with a random value to derive
  228. * the keying material for the encryption/decryption keys,
  229. * IVs and MACs.
  230. */
  231. mbedtls_printf( "...\n . Receiving and decrypting the ciphertext" );
  232. fflush( stdout );
  233. ret = mbedtls_aes_setkey_dec( &aes, buf, 256 );
  234. if( ret != 0 )
  235. goto exit;
  236. memset( buf, 0, sizeof( buf ) );
  237. if( ( ret = mbedtls_net_recv( &server_fd, buf, 16 ) ) != 16 )
  238. {
  239. mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
  240. goto exit;
  241. }
  242. ret = mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_DECRYPT, buf, buf );
  243. if( ret != 0 )
  244. goto exit;
  245. buf[16] = '\0';
  246. mbedtls_printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
  247. exit_code = MBEDTLS_EXIT_SUCCESS;
  248. exit:
  249. mbedtls_net_free( &server_fd );
  250. mbedtls_aes_free( &aes );
  251. mbedtls_rsa_free( &rsa );
  252. mbedtls_dhm_free( &dhm );
  253. mbedtls_ctr_drbg_free( &ctr_drbg );
  254. mbedtls_entropy_free( &entropy );
  255. #if defined(_WIN32)
  256. mbedtls_printf( " + Press Enter to exit this program.\n" );
  257. fflush( stdout ); getchar();
  258. #endif
  259. mbedtls_exit( exit_code );
  260. }
  261. #endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
  262. MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  263. MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */