1 /*
2  *  CRL reading application
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: GPL-2.0
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License along
18  *  with this program; if not, write to the Free Software Foundation, Inc.,
19  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
29 
30 #if defined(MBEDTLS_PLATFORM_C)
31 #include "mbedtls/platform.h"
32 #else
33 #include <stdio.h>
34 #include <stdlib.h>
35 #define mbedtls_printf          printf
36 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
39 
40 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
41     !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO)
main(void)42 int main( void )
43 {
44     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
45            "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
46     return( 0 );
47 }
48 #else
49 
50 #include "mbedtls/x509_crl.h"
51 
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 
56 #define DFL_FILENAME            "crl.pem"
57 #define DFL_DEBUG_LEVEL         0
58 
59 #define USAGE \
60     "\n usage: crl_app param=<>...\n"                   \
61     "\n acceptable parameters:\n"                       \
62     "    filename=%%s         default: crl.pem\n"      \
63     "\n"
64 
65 /*
66  * global options
67  */
68 struct options
69 {
70     const char *filename;       /* filename of the certificate file     */
71 } opt;
72 
main(int argc,char * argv[])73 int main( int argc, char *argv[] )
74 {
75     int ret = 1;
76     int exit_code = MBEDTLS_EXIT_FAILURE;
77     unsigned char buf[100000];
78     mbedtls_x509_crl crl;
79     int i;
80     char *p, *q;
81 
82     /*
83      * Set to sane values
84      */
85     mbedtls_x509_crl_init( &crl );
86 
87     if( argc == 0 )
88     {
89     usage:
90         mbedtls_printf( USAGE );
91         goto exit;
92     }
93 
94     opt.filename            = DFL_FILENAME;
95 
96     for( i = 1; i < argc; i++ )
97     {
98         p = argv[i];
99         if( ( q = strchr( p, '=' ) ) == NULL )
100             goto usage;
101         *q++ = '\0';
102 
103         if( strcmp( p, "filename" ) == 0 )
104             opt.filename = q;
105         else
106             goto usage;
107     }
108 
109     /*
110      * 1.1. Load the CRL
111      */
112     mbedtls_printf( "\n  . Loading the CRL ..." );
113     fflush( stdout );
114 
115     ret = mbedtls_x509_crl_parse_file( &crl, opt.filename );
116 
117     if( ret != 0 )
118     {
119         mbedtls_printf( " failed\n  !  mbedtls_x509_crl_parse_file returned %d\n\n", ret );
120         mbedtls_x509_crl_free( &crl );
121         goto exit;
122     }
123 
124     mbedtls_printf( " ok\n" );
125 
126     /*
127      * 1.2 Print the CRL
128      */
129     mbedtls_printf( "  . CRL information    ...\n" );
130     ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, "      ", &crl );
131     if( ret == -1 )
132     {
133         mbedtls_printf( " failed\n  !  mbedtls_x509_crl_info returned %d\n\n", ret );
134         mbedtls_x509_crl_free( &crl );
135         goto exit;
136     }
137 
138     mbedtls_printf( "%s\n", buf );
139 
140     exit_code = MBEDTLS_EXIT_SUCCESS;
141 
142 exit:
143     mbedtls_x509_crl_free( &crl );
144 
145 #if defined(_WIN32)
146     mbedtls_printf( "  + Press Enter to exit this program.\n" );
147     fflush( stdout ); getchar();
148 #endif
149 
150     return( exit_code );
151 }
152 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
153           MBEDTLS_FS_IO */
154