| 1 | /* |
|---|
| 2 | * CRL reading application |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2006-2011, 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/config.h" |
|---|
| 35 | |
|---|
| 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: crl.pem\n" \ |
|---|
| 63 | " debug_level=%%d default: 0 (disabled)\n" \ |
|---|
| 64 | "\n" |
|---|
| 65 | |
|---|
| 66 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
|---|
| 67 | !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) |
|---|
| 68 | int main( int argc, char *argv[] ) |
|---|
| 69 | { |
|---|
| 70 | ((void) argc); |
|---|
| 71 | ((void) argv); |
|---|
| 72 | |
|---|
| 73 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
|---|
| 74 | "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
|---|
| 75 | return( 0 ); |
|---|
| 76 | } |
|---|
| 77 | #else |
|---|
| 78 | int main( int argc, char *argv[] ) |
|---|
| 79 | { |
|---|
| 80 | int ret = 0; |
|---|
| 81 | unsigned char buf[100000]; |
|---|
| 82 | x509_crl crl; |
|---|
| 83 | int i, j, n; |
|---|
| 84 | char *p, *q; |
|---|
| 85 | |
|---|
| 86 | /* |
|---|
| 87 | * Set to sane values |
|---|
| 88 | */ |
|---|
| 89 | memset( &crl, 0, sizeof( x509_crl ) ); |
|---|
| 90 | |
|---|
| 91 | if( argc == 0 ) |
|---|
| 92 | { |
|---|
| 93 | usage: |
|---|
| 94 | printf( USAGE ); |
|---|
| 95 | goto exit; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | opt.filename = DFL_FILENAME; |
|---|
| 99 | opt.debug_level = DFL_DEBUG_LEVEL; |
|---|
| 100 | |
|---|
| 101 | for( i = 1; i < argc; i++ ) |
|---|
| 102 | { |
|---|
| 103 | n = strlen( argv[i] ); |
|---|
| 104 | |
|---|
| 105 | for( j = 0; j < n; j++ ) |
|---|
| 106 | { |
|---|
| 107 | if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' ) |
|---|
| 108 | argv[i][j] |= 0x20; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | p = argv[i]; |
|---|
| 112 | if( ( q = strchr( p, '=' ) ) == NULL ) |
|---|
| 113 | goto usage; |
|---|
| 114 | *q++ = '\0'; |
|---|
| 115 | |
|---|
| 116 | if( strcmp( p, "filename" ) == 0 ) |
|---|
| 117 | opt.filename = q; |
|---|
| 118 | else if( strcmp( p, "debug_level" ) == 0 ) |
|---|
| 119 | { |
|---|
| 120 | opt.debug_level = atoi( q ); |
|---|
| 121 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
|---|
| 122 | goto usage; |
|---|
| 123 | } |
|---|
| 124 | else |
|---|
| 125 | goto usage; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /* |
|---|
| 129 | * 1.1. Load the CRL |
|---|
| 130 | */ |
|---|
| 131 | printf( "\n . Loading the CRL ..." ); |
|---|
| 132 | fflush( stdout ); |
|---|
| 133 | |
|---|
| 134 | ret = x509parse_crlfile( &crl, opt.filename ); |
|---|
| 135 | |
|---|
| 136 | if( ret != 0 ) |
|---|
| 137 | { |
|---|
| 138 | printf( " failed\n ! x509parse_crl returned %d\n\n", ret ); |
|---|
| 139 | x509_crl_free( &crl ); |
|---|
| 140 | goto exit; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | printf( " ok\n" ); |
|---|
| 144 | |
|---|
| 145 | /* |
|---|
| 146 | * 1.2 Print the CRL |
|---|
| 147 | */ |
|---|
| 148 | printf( " . CRL information ...\n" ); |
|---|
| 149 | ret = x509parse_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl ); |
|---|
| 150 | if( ret == -1 ) |
|---|
| 151 | { |
|---|
| 152 | printf( " failed\n ! x509parse_crl_info returned %d\n\n", ret ); |
|---|
| 153 | x509_crl_free( &crl ); |
|---|
| 154 | goto exit; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | printf( "%s\n", buf ); |
|---|
| 158 | |
|---|
| 159 | exit: |
|---|
| 160 | x509_crl_free( &crl ); |
|---|
| 161 | |
|---|
| 162 | #if defined(_WIN32) |
|---|
| 163 | printf( " + Press Enter to exit this program.\n" ); |
|---|
| 164 | fflush( stdout ); getchar(); |
|---|
| 165 | #endif |
|---|
| 166 | |
|---|
| 167 | return( ret ); |
|---|
| 168 | } |
|---|
| 169 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_PARSE_C && |
|---|
| 170 | POLARSSL_FS_IO */ |
|---|