crl_app.c 4.3 KB

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