blob: b99ffad186f19a257493a836ed7471af328d8650 [file] [log] [blame]
Paul Bakkera9507c02011-02-12 15:27:28 +00001/*
2 * CRL reading application
3 *
4 * Copyright (C) 2006-2010, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
34#include "polarssl/x509.h"
35
36#define DFL_FILENAME "crl.pem"
37#define DFL_DEBUG_LEVEL 0
38
39/*
40 * global options
41 */
42struct options
43{
44 char *filename; /* filename of the certificate file */
45 int debug_level; /* level of debugging */
46} opt;
47
48void my_debug( void *ctx, int level, const char *str )
49{
50 if( level < opt.debug_level )
51 {
52 fprintf( (FILE *) ctx, "%s", str );
53 fflush( (FILE *) ctx );
54 }
55}
56
57#define USAGE \
58 "\n usage: crl_app param=<>...\n" \
59 "\n acceptable parameters:\n" \
60 " filename=%%s default: cert.crt\n" \
61 " debug_level=%%d default: 0 (disabled)\n" \
62 "\n"
63
64int main( int argc, char *argv[] )
65{
66 int ret = 0;
67 unsigned char buf[1024];
68 x509_crl crl;
69 int i, j, n;
70 char *p, *q;
71
72 /*
73 * Set to sane values
74 */
75 memset( &crl, 0, sizeof( x509_crl ) );
76
77 if( argc == 0 )
78 {
79 usage:
80 printf( USAGE );
81 goto exit;
82 }
83
84 opt.filename = DFL_FILENAME;
85 opt.debug_level = DFL_DEBUG_LEVEL;
86
87 for( i = 1; i < argc; i++ )
88 {
89 n = strlen( argv[i] );
90
91 for( j = 0; j < n; j++ )
92 {
93 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
94 argv[i][j] |= 0x20;
95 }
96
97 p = argv[i];
98 if( ( q = strchr( p, '=' ) ) == NULL )
99 goto usage;
100 *q++ = '\0';
101
102 if( strcmp( p, "filename" ) == 0 )
103 opt.filename = q;
104 else if( strcmp( p, "debug_level" ) == 0 )
105 {
106 opt.debug_level = atoi( q );
107 if( opt.debug_level < 0 || opt.debug_level > 65535 )
108 goto usage;
109 }
110 else
111 goto usage;
112 }
113
114 /*
115 * 1.1. Load the CRL
116 */
117 printf( "\n . Loading the CRL ..." );
118 fflush( stdout );
119
120 ret = x509parse_crlfile( &crl, opt.filename );
121
122 if( ret != 0 )
123 {
124 printf( " failed\n ! x509parse_crl returned %d\n\n", ret );
125 x509_crl_free( &crl );
126 goto exit;
127 }
128
129 printf( " ok\n" );
130
131 /*
132 * 1.2 Print the CRL
133 */
134 printf( " . CRL information ...\n" );
135 ret = x509parse_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl );
136 if( ret == -1 )
137 {
138 printf( " failed\n ! x509parse_crl_info returned %d\n\n", ret );
139 x509_crl_free( &crl );
140 goto exit;
141 }
142
143 printf( "%s\n", buf );
144
145exit:
146 x509_crl_free( &crl );
147
148#ifdef WIN32
149 printf( " + Press Enter to exit this program.\n" );
150 fflush( stdout ); getchar();
151#endif
152
153 return( ret );
154}