ssl_client1.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * SSL client demonstration program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_time time
  57. #define mbedtls_time_t time_t
  58. #define mbedtls_fprintf fprintf
  59. #define mbedtls_printf printf
  60. #define mbedtls_exit exit
  61. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  62. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  63. #endif /* MBEDTLS_PLATFORM_C */
  64. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  65. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
  66. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
  67. !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
  68. !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
  69. int main( void )
  70. {
  71. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  72. "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
  73. "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  74. "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
  75. "not defined.\n");
  76. mbedtls_exit( 0 );
  77. }
  78. #else
  79. #include "mbedtls/net_sockets.h"
  80. #include "mbedtls/debug.h"
  81. #include "mbedtls/ssl.h"
  82. #include "mbedtls/entropy.h"
  83. #include "mbedtls/ctr_drbg.h"
  84. #include "mbedtls/error.h"
  85. #include "mbedtls/certs.h"
  86. #include <string.h>
  87. #define SERVER_PORT "4433"
  88. #define SERVER_NAME "localhost"
  89. #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
  90. #define DEBUG_LEVEL 1
  91. static void my_debug( void *ctx, int level,
  92. const char *file, int line,
  93. const char *str )
  94. {
  95. ((void) level);
  96. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
  97. fflush( (FILE *) ctx );
  98. }
  99. int main( void )
  100. {
  101. int ret = 1, len;
  102. int exit_code = MBEDTLS_EXIT_FAILURE;
  103. mbedtls_net_context server_fd;
  104. uint32_t flags;
  105. unsigned char buf[1024];
  106. const char *pers = "ssl_client1";
  107. mbedtls_entropy_context entropy;
  108. mbedtls_ctr_drbg_context ctr_drbg;
  109. mbedtls_ssl_context ssl;
  110. mbedtls_ssl_config conf;
  111. mbedtls_x509_crt cacert;
  112. #if defined(MBEDTLS_DEBUG_C)
  113. mbedtls_debug_set_threshold( DEBUG_LEVEL );
  114. #endif
  115. /*
  116. * 0. Initialize the RNG and the session data
  117. */
  118. mbedtls_net_init( &server_fd );
  119. mbedtls_ssl_init( &ssl );
  120. mbedtls_ssl_config_init( &conf );
  121. mbedtls_x509_crt_init( &cacert );
  122. mbedtls_ctr_drbg_init( &ctr_drbg );
  123. mbedtls_printf( "\n . Seeding the random number generator..." );
  124. fflush( stdout );
  125. mbedtls_entropy_init( &entropy );
  126. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  127. (const unsigned char *) pers,
  128. strlen( pers ) ) ) != 0 )
  129. {
  130. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  131. goto exit;
  132. }
  133. mbedtls_printf( " ok\n" );
  134. /*
  135. * 0. Initialize certificates
  136. */
  137. mbedtls_printf( " . Loading the CA root certificate ..." );
  138. fflush( stdout );
  139. ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
  140. mbedtls_test_cas_pem_len );
  141. if( ret < 0 )
  142. {
  143. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
  144. goto exit;
  145. }
  146. mbedtls_printf( " ok (%d skipped)\n", ret );
  147. /*
  148. * 1. Start the connection
  149. */
  150. mbedtls_printf( " . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT );
  151. fflush( stdout );
  152. if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
  153. SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
  154. {
  155. mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
  156. goto exit;
  157. }
  158. mbedtls_printf( " ok\n" );
  159. /*
  160. * 2. Setup stuff
  161. */
  162. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
  163. fflush( stdout );
  164. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  165. MBEDTLS_SSL_IS_CLIENT,
  166. MBEDTLS_SSL_TRANSPORT_STREAM,
  167. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  168. {
  169. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
  170. goto exit;
  171. }
  172. mbedtls_printf( " ok\n" );
  173. /* OPTIONAL is not optimal for security,
  174. * but makes interop easier in this simplified example */
  175. mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
  176. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  177. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  178. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  179. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  180. {
  181. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
  182. goto exit;
  183. }
  184. if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
  185. {
  186. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
  187. goto exit;
  188. }
  189. mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
  190. /*
  191. * 4. Handshake
  192. */
  193. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
  194. fflush( stdout );
  195. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  196. {
  197. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  198. {
  199. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
  200. goto exit;
  201. }
  202. }
  203. mbedtls_printf( " ok\n" );
  204. /*
  205. * 5. Verify the server certificate
  206. */
  207. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  208. /* In real life, we probably want to bail out when ret != 0 */
  209. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  210. {
  211. char vrfy_buf[512];
  212. mbedtls_printf( " failed\n" );
  213. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  214. mbedtls_printf( "%s\n", vrfy_buf );
  215. }
  216. else
  217. mbedtls_printf( " ok\n" );
  218. /*
  219. * 3. Write the GET request
  220. */
  221. mbedtls_printf( " > Write to server:" );
  222. fflush( stdout );
  223. len = sprintf( (char *) buf, GET_REQUEST );
  224. while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
  225. {
  226. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  227. {
  228. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  229. goto exit;
  230. }
  231. }
  232. len = ret;
  233. mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
  234. /*
  235. * 7. Read the HTTP response
  236. */
  237. mbedtls_printf( " < Read from server:" );
  238. fflush( stdout );
  239. do
  240. {
  241. len = sizeof( buf ) - 1;
  242. memset( buf, 0, sizeof( buf ) );
  243. ret = mbedtls_ssl_read( &ssl, buf, len );
  244. if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  245. continue;
  246. if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
  247. break;
  248. if( ret < 0 )
  249. {
  250. mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
  251. break;
  252. }
  253. if( ret == 0 )
  254. {
  255. mbedtls_printf( "\n\nEOF\n\n" );
  256. break;
  257. }
  258. len = ret;
  259. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  260. }
  261. while( 1 );
  262. mbedtls_ssl_close_notify( &ssl );
  263. exit_code = MBEDTLS_EXIT_SUCCESS;
  264. exit:
  265. #ifdef MBEDTLS_ERROR_C
  266. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  267. {
  268. char error_buf[100];
  269. mbedtls_strerror( ret, error_buf, 100 );
  270. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
  271. }
  272. #endif
  273. mbedtls_net_free( &server_fd );
  274. mbedtls_x509_crt_free( &cacert );
  275. mbedtls_ssl_free( &ssl );
  276. mbedtls_ssl_config_free( &conf );
  277. mbedtls_ctr_drbg_free( &ctr_drbg );
  278. mbedtls_entropy_free( &entropy );
  279. #if defined(_WIN32)
  280. mbedtls_printf( " + Press Enter to exit this program.\n" );
  281. fflush( stdout ); getchar();
  282. #endif
  283. mbedtls_exit( exit_code );
  284. }
  285. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  286. MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
  287. MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C &&
  288. MBEDTLS_X509_CRT_PARSE_C */