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
20 #if !defined(MBEDTLS_CONFIG_FILE)
21 #include "mbedtls/config.h"
22 #else
23 #include MBEDTLS_CONFIG_FILE
24 #endif
25
26 #if defined(MBEDTLS_PLATFORM_C)
27 #include "mbedtls/platform.h"
28 #else
29 #include <stdio.h>
30 #include <stdlib.h>
31 #define mbedtls_time time
32 #define mbedtls_time_t time_t
33 #define mbedtls_fprintf fprintf
34 #define mbedtls_printf printf
35 #define mbedtls_exit exit
36 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
39
40 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
41 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
42 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
43 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
44 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
main(void)45 int main( void )
46 {
47 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
48 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
49 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
50 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
51 "not defined.\n");
52 mbedtls_exit( 0 );
53 }
54 #else
55
56 #include "mbedtls/net_sockets.h"
57 #include "mbedtls/debug.h"
58 #include "mbedtls/ssl.h"
59 #include "mbedtls/entropy.h"
60 #include "mbedtls/ctr_drbg.h"
61 #include "mbedtls/error.h"
62 #include "mbedtls/certs.h"
63
64 #include <string.h>
65
66 #define SERVER_PORT "4433"
67 #define SERVER_NAME "localhost"
68 #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
69
70 #define DEBUG_LEVEL 1
71
72
my_debug(void * ctx,int level,const char * file,int line,const char * str)73 static void my_debug( void *ctx, int level,
74 const char *file, int line,
75 const char *str )
76 {
77 ((void) level);
78
79 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
80 fflush( (FILE *) ctx );
81 }
82
main(void)83 int main( void )
84 {
85 int ret = 1, len;
86 int exit_code = MBEDTLS_EXIT_FAILURE;
87 mbedtls_net_context server_fd;
88 uint32_t flags;
89 unsigned char buf[1024];
90 const char *pers = "ssl_client1";
91
92 mbedtls_entropy_context entropy;
93 mbedtls_ctr_drbg_context ctr_drbg;
94 mbedtls_ssl_context ssl;
95 mbedtls_ssl_config conf;
96 mbedtls_x509_crt cacert;
97
98 #if defined(MBEDTLS_DEBUG_C)
99 mbedtls_debug_set_threshold( DEBUG_LEVEL );
100 #endif
101
102 /*
103 * 0. Initialize the RNG and the session data
104 */
105 mbedtls_net_init( &server_fd );
106 mbedtls_ssl_init( &ssl );
107 mbedtls_ssl_config_init( &conf );
108 mbedtls_x509_crt_init( &cacert );
109 mbedtls_ctr_drbg_init( &ctr_drbg );
110
111 mbedtls_printf( "\n . Seeding the random number generator..." );
112 fflush( stdout );
113
114 mbedtls_entropy_init( &entropy );
115 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
116 (const unsigned char *) pers,
117 strlen( pers ) ) ) != 0 )
118 {
119 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
120 goto exit;
121 }
122
123 mbedtls_printf( " ok\n" );
124
125 /*
126 * 0. Initialize certificates
127 */
128 mbedtls_printf( " . Loading the CA root certificate ..." );
129 fflush( stdout );
130
131 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
132 mbedtls_test_cas_pem_len );
133 if( ret < 0 )
134 {
135 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", (unsigned int) -ret );
136 goto exit;
137 }
138
139 mbedtls_printf( " ok (%d skipped)\n", ret );
140
141 /*
142 * 1. Start the connection
143 */
144 mbedtls_printf( " . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT );
145 fflush( stdout );
146
147 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
148 SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
149 {
150 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
151 goto exit;
152 }
153
154 mbedtls_printf( " ok\n" );
155
156 /*
157 * 2. Setup stuff
158 */
159 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
160 fflush( stdout );
161
162 if( ( ret = mbedtls_ssl_config_defaults( &conf,
163 MBEDTLS_SSL_IS_CLIENT,
164 MBEDTLS_SSL_TRANSPORT_STREAM,
165 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
166 {
167 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
168 goto exit;
169 }
170
171 mbedtls_printf( " ok\n" );
172
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
180 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
181 {
182 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
183 goto exit;
184 }
185
186 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
187 {
188 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
189 goto exit;
190 }
191
192 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
193
194 /*
195 * 4. Handshake
196 */
197 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
198 fflush( stdout );
199
200 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
201 {
202 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
203 {
204 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", (unsigned int) -ret );
205 goto exit;
206 }
207 }
208
209 mbedtls_printf( " ok\n" );
210
211 /*
212 * 5. Verify the server certificate
213 */
214 mbedtls_printf( " . Verifying peer X.509 certificate..." );
215
216 /* In real life, we probably want to bail out when ret != 0 */
217 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
218 {
219 char vrfy_buf[512];
220
221 mbedtls_printf( " failed\n" );
222
223 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
224
225 mbedtls_printf( "%s\n", vrfy_buf );
226 }
227 else
228 mbedtls_printf( " ok\n" );
229
230 /*
231 * 3. Write the GET request
232 */
233 mbedtls_printf( " > Write to server:" );
234 fflush( stdout );
235
236 len = sprintf( (char *) buf, GET_REQUEST );
237
238 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
239 {
240 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
241 {
242 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
243 goto exit;
244 }
245 }
246
247 len = ret;
248 mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
249
250 /*
251 * 7. Read the HTTP response
252 */
253 mbedtls_printf( " < Read from server:" );
254 fflush( stdout );
255
256 do
257 {
258 len = sizeof( buf ) - 1;
259 memset( buf, 0, sizeof( buf ) );
260 ret = mbedtls_ssl_read( &ssl, buf, len );
261
262 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
263 continue;
264
265 if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
266 break;
267
268 if( ret < 0 )
269 {
270 mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
271 break;
272 }
273
274 if( ret == 0 )
275 {
276 mbedtls_printf( "\n\nEOF\n\n" );
277 break;
278 }
279
280 len = ret;
281 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
282 }
283 while( 1 );
284
285 mbedtls_ssl_close_notify( &ssl );
286
287 exit_code = MBEDTLS_EXIT_SUCCESS;
288
289 exit:
290
291 #ifdef MBEDTLS_ERROR_C
292 if( exit_code != MBEDTLS_EXIT_SUCCESS )
293 {
294 char error_buf[100];
295 mbedtls_strerror( ret, error_buf, 100 );
296 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
297 }
298 #endif
299
300 mbedtls_net_free( &server_fd );
301
302 mbedtls_x509_crt_free( &cacert );
303 mbedtls_ssl_free( &ssl );
304 mbedtls_ssl_config_free( &conf );
305 mbedtls_ctr_drbg_free( &ctr_drbg );
306 mbedtls_entropy_free( &entropy );
307
308 #if defined(_WIN32)
309 mbedtls_printf( " + Press Enter to exit this program.\n" );
310 fflush( stdout ); getchar();
311 #endif
312
313 mbedtls_exit( exit_code );
314 }
315 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
316 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
317 MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C &&
318 MBEDTLS_X509_CRT_PARSE_C */
319