dh_client.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
  22. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
  23. defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \
  24. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
  25. defined(MBEDTLS_SHA1_C)
  26. #include "mbedtls/net_sockets.h"
  27. #include "mbedtls/aes.h"
  28. #include "mbedtls/dhm.h"
  29. #include "mbedtls/rsa.h"
  30. #include "mbedtls/sha1.h"
  31. #include "mbedtls/entropy.h"
  32. #include "mbedtls/ctr_drbg.h"
  33. #include <stdio.h>
  34. #include <string.h>
  35. #endif
  36. #define SERVER_NAME "localhost"
  37. #define SERVER_PORT "11999"
  38. #if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
  39. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
  40. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
  41. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
  42. !defined(MBEDTLS_SHA1_C)
  43. int main(void)
  44. {
  45. mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
  46. "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  47. "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
  48. "MBEDTLS_CTR_DRBG_C not defined.\n");
  49. mbedtls_exit(0);
  50. }
  51. #else
  52. int main(void)
  53. {
  54. FILE *f;
  55. int ret = 1;
  56. int exit_code = MBEDTLS_EXIT_FAILURE;
  57. size_t n, buflen;
  58. mbedtls_net_context server_fd;
  59. unsigned char *p, *end;
  60. unsigned char buf[2048];
  61. unsigned char hash[32];
  62. const char *pers = "dh_client";
  63. mbedtls_entropy_context entropy;
  64. mbedtls_ctr_drbg_context ctr_drbg;
  65. mbedtls_rsa_context rsa;
  66. mbedtls_dhm_context dhm;
  67. mbedtls_aes_context aes;
  68. mbedtls_net_init(&server_fd);
  69. mbedtls_dhm_init(&dhm);
  70. mbedtls_aes_init(&aes);
  71. mbedtls_ctr_drbg_init(&ctr_drbg);
  72. /*
  73. * 1. Setup the RNG
  74. */
  75. mbedtls_printf("\n . Seeding the random number generator");
  76. fflush(stdout);
  77. mbedtls_entropy_init(&entropy);
  78. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  79. (const unsigned char *) pers,
  80. strlen(pers))) != 0) {
  81. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
  82. goto exit;
  83. }
  84. /*
  85. * 2. Read the server's public RSA key
  86. */
  87. mbedtls_printf("\n . Reading public key from rsa_pub.txt");
  88. fflush(stdout);
  89. if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
  90. mbedtls_printf(" failed\n ! Could not open rsa_pub.txt\n" \
  91. " ! Please run rsa_genkey first\n\n");
  92. goto exit;
  93. }
  94. mbedtls_rsa_init(&rsa);
  95. if ((ret = mbedtls_mpi_read_file(&rsa.MBEDTLS_PRIVATE(N), 16, f)) != 0 ||
  96. (ret = mbedtls_mpi_read_file(&rsa.MBEDTLS_PRIVATE(E), 16, f)) != 0) {
  97. mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
  98. fclose(f);
  99. goto exit;
  100. }
  101. rsa.MBEDTLS_PRIVATE(len) = (mbedtls_mpi_bitlen(&rsa.MBEDTLS_PRIVATE(N)) + 7) >> 3;
  102. fclose(f);
  103. /*
  104. * 3. Initiate the connection
  105. */
  106. mbedtls_printf("\n . Connecting to tcp/%s/%s", SERVER_NAME,
  107. SERVER_PORT);
  108. fflush(stdout);
  109. if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME,
  110. SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
  111. mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
  112. goto exit;
  113. }
  114. /*
  115. * 4a. First get the buffer length
  116. */
  117. mbedtls_printf("\n . Receiving the server's DH parameters");
  118. fflush(stdout);
  119. memset(buf, 0, sizeof(buf));
  120. if ((ret = mbedtls_net_recv(&server_fd, buf, 2)) != 2) {
  121. mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
  122. goto exit;
  123. }
  124. n = buflen = (buf[0] << 8) | buf[1];
  125. if (buflen < 1 || buflen > sizeof(buf)) {
  126. mbedtls_printf(" failed\n ! Got an invalid buffer length\n\n");
  127. goto exit;
  128. }
  129. /*
  130. * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
  131. */
  132. memset(buf, 0, sizeof(buf));
  133. if ((ret = mbedtls_net_recv(&server_fd, buf, n)) != (int) n) {
  134. mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
  135. goto exit;
  136. }
  137. p = buf, end = buf + buflen;
  138. if ((ret = mbedtls_dhm_read_params(&dhm, &p, end)) != 0) {
  139. mbedtls_printf(" failed\n ! mbedtls_dhm_read_params returned %d\n\n", ret);
  140. goto exit;
  141. }
  142. n = mbedtls_dhm_get_len(&dhm);
  143. if (n < 64 || n > 512) {
  144. mbedtls_printf(" failed\n ! Invalid DHM modulus size\n\n");
  145. goto exit;
  146. }
  147. /*
  148. * 5. Check that the server's RSA signature matches
  149. * the SHA-256 hash of (P,G,Ys)
  150. */
  151. mbedtls_printf("\n . Verifying the server's RSA signature");
  152. fflush(stdout);
  153. p += 2;
  154. if ((n = (size_t) (end - p)) != rsa.MBEDTLS_PRIVATE(len)) {
  155. mbedtls_printf(" failed\n ! Invalid RSA signature size\n\n");
  156. goto exit;
  157. }
  158. if ((ret = mbedtls_sha1(buf, (int) (p - 2 - buf), hash)) != 0) {
  159. mbedtls_printf(" failed\n ! mbedtls_sha1 returned %d\n\n", ret);
  160. goto exit;
  161. }
  162. if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA256,
  163. 32, hash, p)) != 0) {
  164. mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_verify returned %d\n\n", ret);
  165. goto exit;
  166. }
  167. /*
  168. * 6. Send our public value: Yc = G ^ Xc mod P
  169. */
  170. mbedtls_printf("\n . Sending own public value to server");
  171. fflush(stdout);
  172. n = mbedtls_dhm_get_len(&dhm);
  173. if ((ret = mbedtls_dhm_make_public(&dhm, (int) n, buf, n,
  174. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  175. mbedtls_printf(" failed\n ! mbedtls_dhm_make_public returned %d\n\n", ret);
  176. goto exit;
  177. }
  178. if ((ret = mbedtls_net_send(&server_fd, buf, n)) != (int) n) {
  179. mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
  180. goto exit;
  181. }
  182. /*
  183. * 7. Derive the shared secret: K = Ys ^ Xc mod P
  184. */
  185. mbedtls_printf("\n . Shared secret: ");
  186. fflush(stdout);
  187. if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n,
  188. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  189. mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret);
  190. goto exit;
  191. }
  192. for (n = 0; n < 16; n++) {
  193. mbedtls_printf("%02x", buf[n]);
  194. }
  195. /*
  196. * 8. Setup the AES-256 decryption key
  197. *
  198. * This is an overly simplified example; best practice is
  199. * to hash the shared secret with a random value to derive
  200. * the keying material for the encryption/decryption keys,
  201. * IVs and MACs.
  202. */
  203. mbedtls_printf("...\n . Receiving and decrypting the ciphertext");
  204. fflush(stdout);
  205. ret = mbedtls_aes_setkey_dec(&aes, buf, 256);
  206. if (ret != 0) {
  207. goto exit;
  208. }
  209. memset(buf, 0, sizeof(buf));
  210. if ((ret = mbedtls_net_recv(&server_fd, buf, 16)) != 16) {
  211. mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
  212. goto exit;
  213. }
  214. ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, buf, buf);
  215. if (ret != 0) {
  216. goto exit;
  217. }
  218. buf[16] = '\0';
  219. mbedtls_printf("\n . Plaintext is \"%s\"\n\n", (char *) buf);
  220. exit_code = MBEDTLS_EXIT_SUCCESS;
  221. exit:
  222. mbedtls_net_free(&server_fd);
  223. mbedtls_aes_free(&aes);
  224. mbedtls_rsa_free(&rsa);
  225. mbedtls_dhm_free(&dhm);
  226. mbedtls_ctr_drbg_free(&ctr_drbg);
  227. mbedtls_entropy_free(&entropy);
  228. mbedtls_exit(exit_code);
  229. }
  230. #endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
  231. MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  232. MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */