ssl_client1.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * SSL 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_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  22. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
  23. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
  24. !defined(MBEDTLS_PEM_PARSE_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
  25. !defined(MBEDTLS_X509_CRT_PARSE_C)
  26. int main(void)
  27. {
  28. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  29. "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
  30. "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  31. "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
  32. "not defined.\n");
  33. mbedtls_exit(0);
  34. }
  35. #else
  36. #include "mbedtls/net_sockets.h"
  37. #include "mbedtls/debug.h"
  38. #include "mbedtls/ssl.h"
  39. #include "mbedtls/entropy.h"
  40. #include "mbedtls/ctr_drbg.h"
  41. #include "mbedtls/error.h"
  42. #include "test/certs.h"
  43. #include <string.h>
  44. #define SERVER_PORT "4433"
  45. #define SERVER_NAME "localhost"
  46. #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
  47. #define DEBUG_LEVEL 1
  48. static void my_debug(void *ctx, int level,
  49. const char *file, int line,
  50. const char *str)
  51. {
  52. ((void) level);
  53. mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
  54. fflush((FILE *) ctx);
  55. }
  56. int main(void)
  57. {
  58. int ret = 1, len;
  59. int exit_code = MBEDTLS_EXIT_FAILURE;
  60. mbedtls_net_context server_fd;
  61. uint32_t flags;
  62. unsigned char buf[1024];
  63. const char *pers = "ssl_client1";
  64. mbedtls_entropy_context entropy;
  65. mbedtls_ctr_drbg_context ctr_drbg;
  66. mbedtls_ssl_context ssl;
  67. mbedtls_ssl_config conf;
  68. mbedtls_x509_crt cacert;
  69. #if defined(MBEDTLS_DEBUG_C)
  70. mbedtls_debug_set_threshold(DEBUG_LEVEL);
  71. #endif
  72. /*
  73. * 0. Initialize the RNG and the session data
  74. */
  75. mbedtls_net_init(&server_fd);
  76. mbedtls_ssl_init(&ssl);
  77. mbedtls_ssl_config_init(&conf);
  78. mbedtls_x509_crt_init(&cacert);
  79. mbedtls_ctr_drbg_init(&ctr_drbg);
  80. mbedtls_printf("\n . Seeding the random number generator...");
  81. fflush(stdout);
  82. mbedtls_entropy_init(&entropy);
  83. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  84. (const unsigned char *) pers,
  85. strlen(pers))) != 0) {
  86. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
  87. goto exit;
  88. }
  89. mbedtls_printf(" ok\n");
  90. /*
  91. * 0. Initialize certificates
  92. */
  93. mbedtls_printf(" . Loading the CA root certificate ...");
  94. fflush(stdout);
  95. ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
  96. mbedtls_test_cas_pem_len);
  97. if (ret < 0) {
  98. mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
  99. (unsigned int) -ret);
  100. goto exit;
  101. }
  102. mbedtls_printf(" ok (%d skipped)\n", ret);
  103. /*
  104. * 1. Start the connection
  105. */
  106. mbedtls_printf(" . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT);
  107. fflush(stdout);
  108. if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME,
  109. SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
  110. mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
  111. goto exit;
  112. }
  113. mbedtls_printf(" ok\n");
  114. /*
  115. * 2. Setup stuff
  116. */
  117. mbedtls_printf(" . Setting up the SSL/TLS structure...");
  118. fflush(stdout);
  119. if ((ret = mbedtls_ssl_config_defaults(&conf,
  120. MBEDTLS_SSL_IS_CLIENT,
  121. MBEDTLS_SSL_TRANSPORT_STREAM,
  122. MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
  123. mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
  124. goto exit;
  125. }
  126. mbedtls_printf(" ok\n");
  127. /* OPTIONAL is not optimal for security,
  128. * but makes interop easier in this simplified example */
  129. mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
  130. mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
  131. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  132. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  133. if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
  134. mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
  135. goto exit;
  136. }
  137. if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
  138. mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
  139. goto exit;
  140. }
  141. mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
  142. /*
  143. * 4. Handshake
  144. */
  145. mbedtls_printf(" . Performing the SSL/TLS handshake...");
  146. fflush(stdout);
  147. while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
  148. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  149. mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
  150. (unsigned int) -ret);
  151. goto exit;
  152. }
  153. }
  154. mbedtls_printf(" ok\n");
  155. /*
  156. * 5. Verify the server certificate
  157. */
  158. mbedtls_printf(" . Verifying peer X.509 certificate...");
  159. /* In real life, we probably want to bail out when ret != 0 */
  160. if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
  161. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  162. char vrfy_buf[512];
  163. #endif
  164. mbedtls_printf(" failed\n");
  165. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  166. mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
  167. mbedtls_printf("%s\n", vrfy_buf);
  168. #endif
  169. } else {
  170. mbedtls_printf(" ok\n");
  171. }
  172. /*
  173. * 3. Write the GET request
  174. */
  175. mbedtls_printf(" > Write to server:");
  176. fflush(stdout);
  177. len = sprintf((char *) buf, GET_REQUEST);
  178. while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
  179. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  180. mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
  181. goto exit;
  182. }
  183. }
  184. len = ret;
  185. mbedtls_printf(" %d bytes written\n\n%s", len, (char *) buf);
  186. /*
  187. * 7. Read the HTTP response
  188. */
  189. mbedtls_printf(" < Read from server:");
  190. fflush(stdout);
  191. do {
  192. len = sizeof(buf) - 1;
  193. memset(buf, 0, sizeof(buf));
  194. ret = mbedtls_ssl_read(&ssl, buf, len);
  195. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  196. continue;
  197. }
  198. if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  199. break;
  200. }
  201. if (ret < 0) {
  202. mbedtls_printf("failed\n ! mbedtls_ssl_read returned %d\n\n", ret);
  203. break;
  204. }
  205. if (ret == 0) {
  206. mbedtls_printf("\n\nEOF\n\n");
  207. break;
  208. }
  209. len = ret;
  210. mbedtls_printf(" %d bytes read\n\n%s", len, (char *) buf);
  211. } while (1);
  212. mbedtls_ssl_close_notify(&ssl);
  213. exit_code = MBEDTLS_EXIT_SUCCESS;
  214. exit:
  215. #ifdef MBEDTLS_ERROR_C
  216. if (exit_code != MBEDTLS_EXIT_SUCCESS) {
  217. char error_buf[100];
  218. mbedtls_strerror(ret, error_buf, 100);
  219. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
  220. }
  221. #endif
  222. mbedtls_net_free(&server_fd);
  223. mbedtls_x509_crt_free(&cacert);
  224. mbedtls_ssl_free(&ssl);
  225. mbedtls_ssl_config_free(&conf);
  226. mbedtls_ctr_drbg_free(&ctr_drbg);
  227. mbedtls_entropy_free(&entropy);
  228. mbedtls_exit(exit_code);
  229. }
  230. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  231. MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
  232. MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C */