crl_app.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #ifdef PRINTF_STDLIB
  31. #include <stdio.h>
  32. #endif
  33. #ifdef PRINTF_CUSTOM
  34. #include "tinystdio.h"
  35. #endif
  36. #include "polarssl/config.h"
  37. #include "polarssl/x509.h"
  38. #define DFL_FILENAME "crl.pem"
  39. #define DFL_DEBUG_LEVEL 0
  40. /*
  41. * global options
  42. */
  43. struct options
  44. {
  45. char *filename; /* filename of the certificate file */
  46. int debug_level; /* level of debugging */
  47. } opt;
  48. void 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. #define USAGE \
  57. "\n usage: crl_app param=<>...\n" \
  58. "\n acceptable parameters:\n" \
  59. " filename=%%s default: cert.crt\n" \
  60. " debug_level=%%d default: 0 (disabled)\n" \
  61. "\n"
  62. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
  63. !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO)
  64. int main( void )
  65. {
  66. printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
  67. "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
  68. return( 0 );
  69. }
  70. #else
  71. int main( int argc, char *argv[] )
  72. {
  73. int ret = 0;
  74. unsigned char buf[1024];
  75. x509_crl crl;
  76. int i, j, n;
  77. char *p, *q;
  78. /*
  79. * Set to sane values
  80. */
  81. memset( &crl, 0, sizeof( x509_crl ) );
  82. if( argc == 0 )
  83. {
  84. usage:
  85. printf( USAGE );
  86. goto exit;
  87. }
  88. opt.filename = DFL_FILENAME;
  89. opt.debug_level = DFL_DEBUG_LEVEL;
  90. for( i = 1; i < argc; i++ )
  91. {
  92. n = strlen( argv[i] );
  93. for( j = 0; j < n; j++ )
  94. {
  95. if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
  96. argv[i][j] |= 0x20;
  97. }
  98. p = argv[i];
  99. if( ( q = strchr( p, '=' ) ) == NULL )
  100. goto usage;
  101. *q++ = '\0';
  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. * 1.1. Load the CRL
  115. */
  116. printf( "\n . Loading the CRL ..." );
  117. fflush( stdout );
  118. ret = x509parse_crlfile( &crl, opt.filename );
  119. if( ret != 0 )
  120. {
  121. printf( " failed\n ! x509parse_crl returned %d\n\n", ret );
  122. x509_crl_free( &crl );
  123. goto exit;
  124. }
  125. printf( " ok\n" );
  126. /*
  127. * 1.2 Print the CRL
  128. */
  129. printf( " . CRL information ...\n" );
  130. ret = x509parse_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl );
  131. if( ret == -1 )
  132. {
  133. printf( " failed\n ! x509parse_crl_info returned %d\n\n", ret );
  134. x509_crl_free( &crl );
  135. goto exit;
  136. }
  137. printf( "%s\n", buf );
  138. exit:
  139. x509_crl_free( &crl );
  140. #ifdef WIN32
  141. printf( " + Press Enter to exit this program.\n" );
  142. fflush( stdout ); getchar();
  143. #endif
  144. return( ret );
  145. }
  146. #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_X509_PARSE_C &&
  147. POLARSSL_FS_IO */