Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame^] | 34 | #include "polarssl/config.h" |
| 35 | |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 36 | #include "polarssl/x509.h" |
| 37 | |
| 38 | #define DFL_FILENAME "crl.pem" |
| 39 | #define DFL_DEBUG_LEVEL 0 |
| 40 | |
| 41 | /* |
| 42 | * global options |
| 43 | */ |
| 44 | struct options |
| 45 | { |
| 46 | char *filename; /* filename of the certificate file */ |
| 47 | int debug_level; /* level of debugging */ |
| 48 | } opt; |
| 49 | |
| 50 | void my_debug( void *ctx, int level, const char *str ) |
| 51 | { |
| 52 | if( level < opt.debug_level ) |
| 53 | { |
| 54 | fprintf( (FILE *) ctx, "%s", str ); |
| 55 | fflush( (FILE *) ctx ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #define USAGE \ |
| 60 | "\n usage: crl_app param=<>...\n" \ |
| 61 | "\n acceptable parameters:\n" \ |
| 62 | " filename=%%s default: cert.crt\n" \ |
| 63 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 64 | "\n" |
| 65 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame^] | 66 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
| 67 | !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) |
| 68 | int main( void ) |
| 69 | { |
| 70 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
| 71 | "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
| 72 | return( 0 ); |
| 73 | } |
| 74 | #else |
Paul Bakker | a9507c0 | 2011-02-12 15:27:28 +0000 | [diff] [blame] | 75 | int main( int argc, char *argv[] ) |
| 76 | { |
| 77 | int ret = 0; |
| 78 | unsigned char buf[1024]; |
| 79 | x509_crl crl; |
| 80 | int i, j, n; |
| 81 | char *p, *q; |
| 82 | |
| 83 | /* |
| 84 | * Set to sane values |
| 85 | */ |
| 86 | memset( &crl, 0, sizeof( x509_crl ) ); |
| 87 | |
| 88 | if( argc == 0 ) |
| 89 | { |
| 90 | usage: |
| 91 | printf( USAGE ); |
| 92 | goto exit; |
| 93 | } |
| 94 | |
| 95 | opt.filename = DFL_FILENAME; |
| 96 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 97 | |
| 98 | for( i = 1; i < argc; i++ ) |
| 99 | { |
| 100 | n = strlen( argv[i] ); |
| 101 | |
| 102 | for( j = 0; j < n; j++ ) |
| 103 | { |
| 104 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
| 105 | argv[i][j] |= 0x20; |
| 106 | } |
| 107 | |
| 108 | p = argv[i]; |
| 109 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 110 | goto usage; |
| 111 | *q++ = '\0'; |
| 112 | |
| 113 | if( strcmp( p, "filename" ) == 0 ) |
| 114 | opt.filename = q; |
| 115 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 116 | { |
| 117 | opt.debug_level = atoi( q ); |
| 118 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 119 | goto usage; |
| 120 | } |
| 121 | else |
| 122 | goto usage; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * 1.1. Load the CRL |
| 127 | */ |
| 128 | printf( "\n . Loading the CRL ..." ); |
| 129 | fflush( stdout ); |
| 130 | |
| 131 | ret = x509parse_crlfile( &crl, opt.filename ); |
| 132 | |
| 133 | if( ret != 0 ) |
| 134 | { |
| 135 | printf( " failed\n ! x509parse_crl returned %d\n\n", ret ); |
| 136 | x509_crl_free( &crl ); |
| 137 | goto exit; |
| 138 | } |
| 139 | |
| 140 | printf( " ok\n" ); |
| 141 | |
| 142 | /* |
| 143 | * 1.2 Print the CRL |
| 144 | */ |
| 145 | printf( " . CRL information ...\n" ); |
| 146 | ret = x509parse_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl ); |
| 147 | if( ret == -1 ) |
| 148 | { |
| 149 | printf( " failed\n ! x509parse_crl_info returned %d\n\n", ret ); |
| 150 | x509_crl_free( &crl ); |
| 151 | goto exit; |
| 152 | } |
| 153 | |
| 154 | printf( "%s\n", buf ); |
| 155 | |
| 156 | exit: |
| 157 | x509_crl_free( &crl ); |
| 158 | |
| 159 | #ifdef WIN32 |
| 160 | printf( " + Press Enter to exit this program.\n" ); |
| 161 | fflush( stdout ); getchar(); |
| 162 | #endif |
| 163 | |
| 164 | return( ret ); |
| 165 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame^] | 166 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_PARSE_C && |
| 167 | POLARSSL_FS_IO */ |