crl_app.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * CRL reading application
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_printf printf
  57. #define mbedtls_exit exit
  58. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  59. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  60. #endif /* MBEDTLS_PLATFORM_C */
  61. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  62. !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO)
  63. int main( void )
  64. {
  65. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  66. "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
  67. mbedtls_exit( 0 );
  68. }
  69. #else
  70. #include "mbedtls/x509_crl.h"
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #define DFL_FILENAME "crl.pem"
  75. #define DFL_DEBUG_LEVEL 0
  76. #define USAGE \
  77. "\n usage: crl_app param=<>...\n" \
  78. "\n acceptable parameters:\n" \
  79. " filename=%%s default: crl.pem\n" \
  80. "\n"
  81. /*
  82. * global options
  83. */
  84. struct options
  85. {
  86. const char *filename; /* filename of the certificate file */
  87. } opt;
  88. int main( int argc, char *argv[] )
  89. {
  90. int ret = 1;
  91. int exit_code = MBEDTLS_EXIT_FAILURE;
  92. unsigned char buf[100000];
  93. mbedtls_x509_crl crl;
  94. int i;
  95. char *p, *q;
  96. /*
  97. * Set to sane values
  98. */
  99. mbedtls_x509_crl_init( &crl );
  100. if( argc == 0 )
  101. {
  102. usage:
  103. mbedtls_printf( USAGE );
  104. goto exit;
  105. }
  106. opt.filename = DFL_FILENAME;
  107. for( i = 1; i < argc; i++ )
  108. {
  109. p = argv[i];
  110. if( ( q = strchr( p, '=' ) ) == NULL )
  111. goto usage;
  112. *q++ = '\0';
  113. if( strcmp( p, "filename" ) == 0 )
  114. opt.filename = q;
  115. else
  116. goto usage;
  117. }
  118. /*
  119. * 1.1. Load the CRL
  120. */
  121. mbedtls_printf( "\n . Loading the CRL ..." );
  122. fflush( stdout );
  123. ret = mbedtls_x509_crl_parse_file( &crl, opt.filename );
  124. if( ret != 0 )
  125. {
  126. mbedtls_printf( " failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret );
  127. mbedtls_x509_crl_free( &crl );
  128. goto exit;
  129. }
  130. mbedtls_printf( " ok\n" );
  131. /*
  132. * 1.2 Print the CRL
  133. */
  134. mbedtls_printf( " . CRL information ...\n" );
  135. ret = mbedtls_x509_crl_info( (char *) buf, sizeof( buf ) - 1, " ", &crl );
  136. if( ret == -1 )
  137. {
  138. mbedtls_printf( " failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret );
  139. mbedtls_x509_crl_free( &crl );
  140. goto exit;
  141. }
  142. mbedtls_printf( "%s\n", buf );
  143. exit_code = MBEDTLS_EXIT_SUCCESS;
  144. exit:
  145. mbedtls_x509_crl_free( &crl );
  146. #if defined(_WIN32)
  147. mbedtls_printf( " + Press Enter to exit this program.\n" );
  148. fflush( stdout ); getchar();
  149. #endif
  150. mbedtls_exit( exit_code );
  151. }
  152. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
  153. MBEDTLS_FS_IO */