1 /*
2  *  Certificate reading application
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 #include "mbedtls/platform.h"
23 
24 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||  \
25     !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
26     !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) ||         \
27     !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) ||  \
28     !defined(MBEDTLS_CTR_DRBG_C) || defined(MBEDTLS_X509_REMOVE_INFO)
main(void)29 int main( void )
30 {
31     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
32            "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
33            "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
34            "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
35            "MBEDTLS_CTR_DRBG_C not defined and/or MBEDTLS_X509_REMOVE_INFO defined.\n");
36     mbedtls_exit( 0 );
37 }
38 #else
39 
40 #include "mbedtls/entropy.h"
41 #include "mbedtls/ctr_drbg.h"
42 #include "mbedtls/net_sockets.h"
43 #include "mbedtls/ssl.h"
44 #include "mbedtls/x509.h"
45 #include "mbedtls/debug.h"
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #define MODE_NONE               0
52 #define MODE_FILE               1
53 #define MODE_SSL                2
54 
55 #define DFL_MODE                MODE_NONE
56 #define DFL_FILENAME            "cert.crt"
57 #define DFL_CA_FILE             ""
58 #define DFL_CRL_FILE            ""
59 #define DFL_CA_PATH             ""
60 #define DFL_SERVER_NAME         "localhost"
61 #define DFL_SERVER_PORT         "4433"
62 #define DFL_DEBUG_LEVEL         0
63 #define DFL_PERMISSIVE          0
64 
65 #define USAGE_IO \
66     "    ca_file=%%s          The single file containing the top-level CA(s) you fully trust\n" \
67     "                        default: \"\" (none)\n" \
68     "    crl_file=%%s         The single CRL file you want to use\n" \
69     "                        default: \"\" (none)\n" \
70     "    ca_path=%%s          The path containing the top-level CA(s) you fully trust\n" \
71     "                        default: \"\" (none) (overrides ca_file)\n"
72 
73 #define USAGE \
74     "\n usage: cert_app param=<>...\n"                  \
75     "\n acceptable parameters:\n"                       \
76     "    mode=file|ssl       default: none\n"           \
77     "    filename=%%s         default: cert.crt\n"      \
78     USAGE_IO                                            \
79     "    server_name=%%s      default: localhost\n"     \
80     "    server_port=%%d      default: 4433\n"          \
81     "    debug_level=%%d      default: 0 (disabled)\n"  \
82     "    permissive=%%d       default: 0 (disabled)\n"  \
83     "\n"
84 
85 
86 /*
87  * global options
88  */
89 struct options
90 {
91     int mode;                   /* the mode to run the application in   */
92     const char *filename;       /* filename of the certificate file     */
93     const char *ca_file;        /* the file with the CA certificate(s)  */
94     const char *crl_file;       /* the file with the CRL to use         */
95     const char *ca_path;        /* the path with the CA certificate(s) reside */
96     const char *server_name;    /* hostname of the server (client only) */
97     const char *server_port;    /* port on which the ssl service runs   */
98     int debug_level;            /* level of debugging                   */
99     int permissive;             /* permissive parsing                   */
100 } opt;
101 
my_debug(void * ctx,int level,const char * file,int line,const char * str)102 static void my_debug( void *ctx, int level,
103                       const char *file, int line,
104                       const char *str )
105 {
106     ((void) level);
107 
108     mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
109     fflush(  (FILE *) ctx  );
110 }
111 
my_verify(void * data,mbedtls_x509_crt * crt,int depth,uint32_t * flags)112 static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags )
113 {
114     char buf[1024];
115     ((void) data);
116 
117     mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
118     mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
119     mbedtls_printf( "%s", buf );
120 
121     if ( ( *flags ) == 0 )
122         mbedtls_printf( "  This certificate has no flags\n" );
123     else
124     {
125         mbedtls_x509_crt_verify_info( buf, sizeof( buf ), "  ! ", *flags );
126         mbedtls_printf( "%s\n", buf );
127     }
128 
129     return( 0 );
130 }
131 
main(int argc,char * argv[])132 int main( int argc, char *argv[] )
133 {
134     int ret = 1;
135     int exit_code = MBEDTLS_EXIT_FAILURE;
136     mbedtls_net_context server_fd;
137     unsigned char buf[1024];
138     mbedtls_entropy_context entropy;
139     mbedtls_ctr_drbg_context ctr_drbg;
140     mbedtls_ssl_context ssl;
141     mbedtls_ssl_config conf;
142     mbedtls_x509_crt cacert;
143     mbedtls_x509_crl cacrl;
144     int i, j;
145     uint32_t flags;
146     int verify = 0;
147     char *p, *q;
148     const char *pers = "cert_app";
149 
150     /*
151      * Set to sane values
152      */
153     mbedtls_net_init( &server_fd );
154     mbedtls_ctr_drbg_init( &ctr_drbg );
155     mbedtls_ssl_init( &ssl );
156     mbedtls_ssl_config_init( &conf );
157     mbedtls_x509_crt_init( &cacert );
158 #if defined(MBEDTLS_X509_CRL_PARSE_C)
159     mbedtls_x509_crl_init( &cacrl );
160 #else
161     /* Zeroize structure as CRL parsing is not supported and we have to pass
162        it to the verify function */
163     memset( &cacrl, 0, sizeof(mbedtls_x509_crl) );
164 #endif
165 
166     if( argc == 0 )
167     {
168     usage:
169         mbedtls_printf( USAGE );
170         goto exit;
171     }
172 
173     opt.mode                = DFL_MODE;
174     opt.filename            = DFL_FILENAME;
175     opt.ca_file             = DFL_CA_FILE;
176     opt.crl_file            = DFL_CRL_FILE;
177     opt.ca_path             = DFL_CA_PATH;
178     opt.server_name         = DFL_SERVER_NAME;
179     opt.server_port         = DFL_SERVER_PORT;
180     opt.debug_level         = DFL_DEBUG_LEVEL;
181     opt.permissive          = DFL_PERMISSIVE;
182 
183     for( i = 1; i < argc; i++ )
184     {
185         p = argv[i];
186         if( ( q = strchr( p, '=' ) ) == NULL )
187             goto usage;
188         *q++ = '\0';
189 
190         for( j = 0; p + j < q; j++ )
191         {
192             if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
193                 argv[i][j] |= 0x20;
194         }
195 
196         if( strcmp( p, "mode" ) == 0 )
197         {
198             if( strcmp( q, "file" ) == 0 )
199                 opt.mode = MODE_FILE;
200             else if( strcmp( q, "ssl" ) == 0 )
201                 opt.mode = MODE_SSL;
202             else
203                 goto usage;
204         }
205         else if( strcmp( p, "filename" ) == 0 )
206             opt.filename = q;
207         else if( strcmp( p, "ca_file" ) == 0 )
208             opt.ca_file = q;
209         else if( strcmp( p, "crl_file" ) == 0 )
210             opt.crl_file = q;
211         else if( strcmp( p, "ca_path" ) == 0 )
212             opt.ca_path = q;
213         else if( strcmp( p, "server_name" ) == 0 )
214             opt.server_name = q;
215         else if( strcmp( p, "server_port" ) == 0 )
216             opt.server_port = q;
217         else if( strcmp( p, "debug_level" ) == 0 )
218         {
219             opt.debug_level = atoi( q );
220             if( opt.debug_level < 0 || opt.debug_level > 65535 )
221                 goto usage;
222         }
223         else if( strcmp( p, "permissive" ) == 0 )
224         {
225             opt.permissive = atoi( q );
226             if( opt.permissive < 0 || opt.permissive > 1 )
227                 goto usage;
228         }
229         else
230             goto usage;
231     }
232 
233     /*
234      * 1.1. Load the trusted CA
235      */
236     mbedtls_printf( "  . Loading the CA root certificate ..." );
237     fflush( stdout );
238 
239     if( strlen( opt.ca_path ) )
240     {
241         if( ( ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path ) ) < 0 )
242         {
243             mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse_path returned -0x%x\n\n", (unsigned int) -ret );
244             goto exit;
245         }
246 
247         verify = 1;
248     }
249     else if( strlen( opt.ca_file ) )
250     {
251         if( ( ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file ) ) < 0 )
252         {
253             mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse_file returned -0x%x\n\n", (unsigned int) -ret );
254             goto exit;
255         }
256 
257         verify = 1;
258     }
259 
260     mbedtls_printf( " ok (%d skipped)\n", ret );
261 
262 #if defined(MBEDTLS_X509_CRL_PARSE_C)
263     if( strlen( opt.crl_file ) )
264     {
265         if( ( ret = mbedtls_x509_crl_parse_file( &cacrl, opt.crl_file ) ) != 0 )
266         {
267             mbedtls_printf( " failed\n  !  mbedtls_x509_crl_parse returned -0x%x\n\n", (unsigned int) -ret );
268             goto exit;
269         }
270 
271         verify = 1;
272     }
273 #endif
274 
275     if( opt.mode == MODE_FILE )
276     {
277         mbedtls_x509_crt crt;
278         mbedtls_x509_crt *cur = &crt;
279         mbedtls_x509_crt_init( &crt );
280 
281         /*
282          * 1.1. Load the certificate(s)
283          */
284         mbedtls_printf( "\n  . Loading the certificate(s) ..." );
285         fflush( stdout );
286 
287         ret = mbedtls_x509_crt_parse_file( &crt, opt.filename );
288 
289         if( ret < 0 )
290         {
291             mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse_file returned %d\n\n", ret );
292             mbedtls_x509_crt_free( &crt );
293             goto exit;
294         }
295 
296         if( opt.permissive == 0 && ret > 0 )
297         {
298             mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse failed to parse %d certificates\n\n", ret );
299             mbedtls_x509_crt_free( &crt );
300             goto exit;
301         }
302 
303         mbedtls_printf( " ok\n" );
304 
305         /*
306          * 1.2 Print the certificate(s)
307          */
308         while( cur != NULL )
309         {
310             mbedtls_printf( "  . Peer certificate information    ...\n" );
311             ret = mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, "      ",
312                                  cur );
313             if( ret == -1 )
314             {
315                 mbedtls_printf( " failed\n  !  mbedtls_x509_crt_info returned %d\n\n", ret );
316                 mbedtls_x509_crt_free( &crt );
317                 goto exit;
318             }
319 
320             mbedtls_printf( "%s\n", buf );
321 
322             cur = cur->next;
323         }
324 
325         /*
326          * 1.3 Verify the certificate
327          */
328         if( verify )
329         {
330             mbedtls_printf( "  . Verifying X.509 certificate..." );
331 
332             if( ( ret = mbedtls_x509_crt_verify( &crt, &cacert, &cacrl, NULL, &flags,
333                                          my_verify, NULL ) ) != 0 )
334             {
335                 char vrfy_buf[512];
336 
337                 mbedtls_printf( " failed\n" );
338 
339                 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), "  ! ", flags );
340 
341                 mbedtls_printf( "%s\n", vrfy_buf );
342             }
343             else
344                 mbedtls_printf( " ok\n" );
345         }
346 
347         mbedtls_x509_crt_free( &crt );
348     }
349     else if( opt.mode == MODE_SSL )
350     {
351         /*
352          * 1. Initialize the RNG and the session data
353          */
354         mbedtls_printf( "\n  . Seeding the random number generator..." );
355         fflush( stdout );
356 
357         mbedtls_entropy_init( &entropy );
358         if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
359                                    (const unsigned char *) pers,
360                                    strlen( pers ) ) ) != 0 )
361         {
362             mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
363             goto ssl_exit;
364         }
365 
366         mbedtls_printf( " ok\n" );
367 
368 #if defined(MBEDTLS_DEBUG_C)
369         mbedtls_debug_set_threshold( opt.debug_level );
370 #endif
371 
372         /*
373          * 2. Start the connection
374          */
375         mbedtls_printf( "  . SSL connection to tcp/%s/%s...", opt.server_name,
376                                                               opt.server_port );
377         fflush( stdout );
378 
379         if( ( ret = mbedtls_net_connect( &server_fd, opt.server_name,
380                                  opt.server_port, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
381         {
382             mbedtls_printf( " failed\n  ! mbedtls_net_connect returned %d\n\n", ret );
383             goto ssl_exit;
384         }
385 
386         /*
387          * 3. Setup stuff
388          */
389         if( ( ret = mbedtls_ssl_config_defaults( &conf,
390                         MBEDTLS_SSL_IS_CLIENT,
391                         MBEDTLS_SSL_TRANSPORT_STREAM,
392                         MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
393         {
394             mbedtls_printf( " failed\n  ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
395             goto exit;
396         }
397 
398         if( verify )
399         {
400             mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_REQUIRED );
401             mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
402             mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
403         }
404         else
405             mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_NONE );
406 
407         mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
408         mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
409 
410         if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
411         {
412             mbedtls_printf( " failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret );
413             goto ssl_exit;
414         }
415 
416         if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
417         {
418             mbedtls_printf( " failed\n  ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
419             goto ssl_exit;
420         }
421 
422         mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
423 
424         /*
425          * 4. Handshake
426          */
427         while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
428         {
429             if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
430             {
431                 mbedtls_printf( " failed\n  ! mbedtls_ssl_handshake returned %d\n\n", ret );
432                 goto ssl_exit;
433             }
434         }
435 
436         mbedtls_printf( " ok\n" );
437 
438         /*
439          * 5. Print the certificate
440          */
441 #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
442         mbedtls_printf( "  . Peer certificate information    ... skipped\n" );
443 #else
444         mbedtls_printf( "  . Peer certificate information    ...\n" );
445         ret = mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, "      ",
446                                      mbedtls_ssl_get_peer_cert( &ssl ) );
447         if( ret == -1 )
448         {
449             mbedtls_printf( " failed\n  !  mbedtls_x509_crt_info returned %d\n\n", ret );
450             goto ssl_exit;
451         }
452 
453         mbedtls_printf( "%s\n", buf );
454 #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
455 
456         mbedtls_ssl_close_notify( &ssl );
457 
458 ssl_exit:
459         mbedtls_ssl_free( &ssl );
460         mbedtls_ssl_config_free( &conf );
461     }
462     else
463         goto usage;
464 
465     exit_code = MBEDTLS_EXIT_SUCCESS;
466 
467 exit:
468 
469     mbedtls_net_free( &server_fd );
470     mbedtls_x509_crt_free( &cacert );
471 #if defined(MBEDTLS_X509_CRL_PARSE_C)
472     mbedtls_x509_crl_free( &cacrl );
473 #endif
474     mbedtls_ctr_drbg_free( &ctr_drbg );
475     mbedtls_entropy_free( &entropy );
476 
477     mbedtls_exit( exit_code );
478 }
479 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
480           MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
481           MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
482