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