1 /*
2  *  CRL 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_RSA_C) ||  \
25     !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
26     defined(MBEDTLS_X509_REMOVE_INFO)
main(void)27 int main( void )
28 {
29     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
30            "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
31            "MBEDTLS_X509_REMOVE_INFO defined.\n");
32     mbedtls_exit( 0 );
33 }
34 #else
35 
36 #include "mbedtls/x509_crl.h"
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #define DFL_FILENAME            "crl.pem"
43 #define DFL_DEBUG_LEVEL         0
44 
45 #define USAGE \
46     "\n usage: crl_app param=<>...\n"                   \
47     "\n acceptable parameters:\n"                       \
48     "    filename=%%s         default: crl.pem\n"      \
49     "\n"
50 
51 
52 /*
53  * global options
54  */
55 struct options
56 {
57     const char *filename;       /* filename of the certificate file     */
58 } opt;
59 
main(int argc,char * argv[])60 int main( int argc, char *argv[] )
61 {
62     int ret = 1;
63     int exit_code = MBEDTLS_EXIT_FAILURE;
64     unsigned char buf[100000];
65     mbedtls_x509_crl crl;
66     int i;
67     char *p, *q;
68 
69     /*
70      * Set to sane values
71      */
72     mbedtls_x509_crl_init( &crl );
73 
74     if( argc == 0 )
75     {
76     usage:
77         mbedtls_printf( USAGE );
78         goto exit;
79     }
80 
81     opt.filename            = DFL_FILENAME;
82 
83     for( i = 1; i < argc; i++ )
84     {
85         p = argv[i];
86         if( ( q = strchr( p, '=' ) ) == NULL )
87             goto usage;
88         *q++ = '\0';
89 
90         if( strcmp( p, "filename" ) == 0 )
91             opt.filename = q;
92         else
93             goto usage;
94     }
95 
96     /*
97      * 1.1. Load the CRL
98      */
99     mbedtls_printf( "\n  . Loading the CRL ..." );
100     fflush( stdout );
101 
102     ret = mbedtls_x509_crl_parse_file( &crl, opt.filename );
103 
104     if( ret != 0 )
105     {
106         mbedtls_printf( " failed\n  !  mbedtls_x509_crl_parse_file returned %d\n\n", ret );
107         mbedtls_x509_crl_free( &crl );
108         goto exit;
109     }
110 
111     mbedtls_printf( " ok\n" );
112 
113     /*
114      * 1.2 Print the CRL
115      */
116     mbedtls_printf( "  . CRL information    ...\n" );
117     ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, "      ", &crl );
118     if( ret == -1 )
119     {
120         mbedtls_printf( " failed\n  !  mbedtls_x509_crl_info returned %d\n\n", ret );
121         mbedtls_x509_crl_free( &crl );
122         goto exit;
123     }
124 
125     mbedtls_printf( "%s\n", buf );
126 
127     exit_code = MBEDTLS_EXIT_SUCCESS;
128 
129 exit:
130     mbedtls_x509_crl_free( &crl );
131 
132     mbedtls_exit( exit_code );
133 }
134 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
135           MBEDTLS_FS_IO */
136