dh_client.c 9.1 KB

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