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