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