dtls_client.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Simple DTLS client demonstration program
  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_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
  22. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
  23. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  24. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
  25. !defined(MBEDTLS_PEM_PARSE_C)
  26. int main(void)
  27. {
  28. mbedtls_printf("MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
  29. "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or "
  30. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
  31. "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
  32. "MBEDTLS_PEM_PARSE_C not defined.\n");
  33. mbedtls_exit(0);
  34. }
  35. #else
  36. #include <string.h>
  37. #include "mbedtls/net_sockets.h"
  38. #include "mbedtls/debug.h"
  39. #include "mbedtls/ssl.h"
  40. #include "mbedtls/entropy.h"
  41. #include "mbedtls/ctr_drbg.h"
  42. #include "mbedtls/error.h"
  43. #include "mbedtls/timing.h"
  44. #include "test/certs.h"
  45. /* Uncomment out the following line to default to IPv4 and disable IPv6 */
  46. //#define FORCE_IPV4
  47. #define SERVER_PORT "4433"
  48. #define SERVER_NAME "localhost"
  49. #ifdef FORCE_IPV4
  50. #define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
  51. #else
  52. #define SERVER_ADDR "::1"
  53. #endif
  54. #define MESSAGE "Echo this"
  55. #define READ_TIMEOUT_MS 1000
  56. #define MAX_RETRY 5
  57. #define DEBUG_LEVEL 0
  58. static void my_debug(void *ctx, int level,
  59. const char *file, int line,
  60. const char *str)
  61. {
  62. ((void) level);
  63. mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
  64. fflush((FILE *) ctx);
  65. }
  66. int main(int argc, char *argv[])
  67. {
  68. int ret, len;
  69. mbedtls_net_context server_fd;
  70. uint32_t flags;
  71. unsigned char buf[1024];
  72. const char *pers = "dtls_client";
  73. int retry_left = MAX_RETRY;
  74. mbedtls_entropy_context entropy;
  75. mbedtls_ctr_drbg_context ctr_drbg;
  76. mbedtls_ssl_context ssl;
  77. mbedtls_ssl_config conf;
  78. mbedtls_x509_crt cacert;
  79. mbedtls_timing_delay_context timer;
  80. ((void) argc);
  81. ((void) argv);
  82. #if defined(MBEDTLS_DEBUG_C)
  83. mbedtls_debug_set_threshold(DEBUG_LEVEL);
  84. #endif
  85. /*
  86. * 0. Initialize the RNG and the session data
  87. */
  88. mbedtls_net_init(&server_fd);
  89. mbedtls_ssl_init(&ssl);
  90. mbedtls_ssl_config_init(&conf);
  91. mbedtls_x509_crt_init(&cacert);
  92. mbedtls_ctr_drbg_init(&ctr_drbg);
  93. mbedtls_printf("\n . Seeding the random number generator...");
  94. fflush(stdout);
  95. mbedtls_entropy_init(&entropy);
  96. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  97. (const unsigned char *) pers,
  98. strlen(pers))) != 0) {
  99. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
  100. goto exit;
  101. }
  102. mbedtls_printf(" ok\n");
  103. /*
  104. * 0. Load certificates
  105. */
  106. mbedtls_printf(" . Loading the CA root certificate ...");
  107. fflush(stdout);
  108. ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
  109. mbedtls_test_cas_pem_len);
  110. if (ret < 0) {
  111. mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
  112. (unsigned int) -ret);
  113. goto exit;
  114. }
  115. mbedtls_printf(" ok (%d skipped)\n", ret);
  116. /*
  117. * 1. Start the connection
  118. */
  119. mbedtls_printf(" . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT);
  120. fflush(stdout);
  121. if ((ret = mbedtls_net_connect(&server_fd, SERVER_ADDR,
  122. SERVER_PORT, MBEDTLS_NET_PROTO_UDP)) != 0) {
  123. mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
  124. goto exit;
  125. }
  126. mbedtls_printf(" ok\n");
  127. /*
  128. * 2. Setup stuff
  129. */
  130. mbedtls_printf(" . Setting up the DTLS structure...");
  131. fflush(stdout);
  132. if ((ret = mbedtls_ssl_config_defaults(&conf,
  133. MBEDTLS_SSL_IS_CLIENT,
  134. MBEDTLS_SSL_TRANSPORT_DATAGRAM,
  135. MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
  136. mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
  137. goto exit;
  138. }
  139. /* OPTIONAL is usually a bad choice for security, but makes interop easier
  140. * in this simplified example, in which the ca chain is hardcoded.
  141. * Production code should set a proper ca chain and use REQUIRED. */
  142. mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
  143. mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
  144. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  145. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  146. mbedtls_ssl_conf_read_timeout(&conf, READ_TIMEOUT_MS);
  147. if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
  148. mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
  149. goto exit;
  150. }
  151. if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
  152. mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
  153. goto exit;
  154. }
  155. mbedtls_ssl_set_bio(&ssl, &server_fd,
  156. mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout);
  157. mbedtls_ssl_set_timer_cb(&ssl, &timer, mbedtls_timing_set_delay,
  158. mbedtls_timing_get_delay);
  159. mbedtls_printf(" ok\n");
  160. /*
  161. * 4. Handshake
  162. */
  163. mbedtls_printf(" . Performing the DTLS handshake...");
  164. fflush(stdout);
  165. do {
  166. ret = mbedtls_ssl_handshake(&ssl);
  167. } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
  168. ret == MBEDTLS_ERR_SSL_WANT_WRITE);
  169. if (ret != 0) {
  170. mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
  171. (unsigned int) -ret);
  172. goto exit;
  173. }
  174. mbedtls_printf(" ok\n");
  175. /*
  176. * 5. Verify the server certificate
  177. */
  178. mbedtls_printf(" . Verifying peer X.509 certificate...");
  179. /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
  180. * handshake would not succeed if the peer's cert is bad. Even if we used
  181. * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
  182. if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
  183. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  184. char vrfy_buf[512];
  185. #endif
  186. mbedtls_printf(" failed\n");
  187. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  188. mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
  189. mbedtls_printf("%s\n", vrfy_buf);
  190. #endif
  191. } else {
  192. mbedtls_printf(" ok\n");
  193. }
  194. /*
  195. * 6. Write the echo request
  196. */
  197. send_request:
  198. mbedtls_printf(" > Write to server:");
  199. fflush(stdout);
  200. len = sizeof(MESSAGE) - 1;
  201. do {
  202. ret = mbedtls_ssl_write(&ssl, (unsigned char *) MESSAGE, len);
  203. } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
  204. ret == MBEDTLS_ERR_SSL_WANT_WRITE);
  205. if (ret < 0) {
  206. mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
  207. goto exit;
  208. }
  209. len = ret;
  210. mbedtls_printf(" %d bytes written\n\n%s\n\n", len, MESSAGE);
  211. /*
  212. * 7. Read the echo response
  213. */
  214. mbedtls_printf(" < Read from server:");
  215. fflush(stdout);
  216. len = sizeof(buf) - 1;
  217. memset(buf, 0, sizeof(buf));
  218. do {
  219. ret = mbedtls_ssl_read(&ssl, buf, len);
  220. } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
  221. ret == MBEDTLS_ERR_SSL_WANT_WRITE);
  222. if (ret <= 0) {
  223. switch (ret) {
  224. case MBEDTLS_ERR_SSL_TIMEOUT:
  225. mbedtls_printf(" timeout\n\n");
  226. if (retry_left-- > 0) {
  227. goto send_request;
  228. }
  229. goto exit;
  230. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  231. mbedtls_printf(" connection was closed gracefully\n");
  232. ret = 0;
  233. goto close_notify;
  234. default:
  235. mbedtls_printf(" mbedtls_ssl_read returned -0x%x\n\n", (unsigned int) -ret);
  236. goto exit;
  237. }
  238. }
  239. len = ret;
  240. mbedtls_printf(" %d bytes read\n\n%s\n\n", len, buf);
  241. /*
  242. * 8. Done, cleanly close the connection
  243. */
  244. close_notify:
  245. mbedtls_printf(" . Closing the connection...");
  246. /* No error checking, the connection might be closed already */
  247. do {
  248. ret = mbedtls_ssl_close_notify(&ssl);
  249. } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
  250. ret = 0;
  251. mbedtls_printf(" done\n");
  252. /*
  253. * 9. Final clean-ups and exit
  254. */
  255. exit:
  256. #ifdef MBEDTLS_ERROR_C
  257. if (ret != 0) {
  258. char error_buf[100];
  259. mbedtls_strerror(ret, error_buf, 100);
  260. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
  261. }
  262. #endif
  263. mbedtls_net_free(&server_fd);
  264. mbedtls_x509_crt_free(&cacert);
  265. mbedtls_ssl_free(&ssl);
  266. mbedtls_ssl_config_free(&conf);
  267. mbedtls_ctr_drbg_free(&ctr_drbg);
  268. mbedtls_entropy_free(&entropy);
  269. /* Shell can not handle large exit numbers -> 1 for errors */
  270. if (ret < 0) {
  271. ret = 1;
  272. }
  273. mbedtls_exit(ret);
  274. }
  275. #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
  276. MBEDTLS_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
  277. MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_PEM_PARSE_C */