dh_client.c 8.7 KB

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