crl_app.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * CRL reading application
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  22. !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  23. defined(MBEDTLS_X509_REMOVE_INFO)
  24. int main(void)
  25. {
  26. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  27. "MBEDTLS_X509_CRL_PARSE_C and/or MBEDTLS_FS_IO not defined and/or "
  28. "MBEDTLS_X509_REMOVE_INFO defined.\n");
  29. mbedtls_exit(0);
  30. }
  31. #else
  32. #include "mbedtls/x509_crl.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #define DFL_FILENAME "crl.pem"
  37. #define DFL_DEBUG_LEVEL 0
  38. #define USAGE \
  39. "\n usage: crl_app param=<>...\n" \
  40. "\n acceptable parameters:\n" \
  41. " filename=%%s default: crl.pem\n" \
  42. "\n"
  43. /*
  44. * global options
  45. */
  46. struct options {
  47. const char *filename; /* filename of the certificate file */
  48. } opt;
  49. int main(int argc, char *argv[])
  50. {
  51. int ret = 1;
  52. int exit_code = MBEDTLS_EXIT_FAILURE;
  53. unsigned char buf[100000];
  54. mbedtls_x509_crl crl;
  55. int i;
  56. char *p, *q;
  57. /*
  58. * Set to sane values
  59. */
  60. mbedtls_x509_crl_init(&crl);
  61. if (argc < 2) {
  62. usage:
  63. mbedtls_printf(USAGE);
  64. goto exit;
  65. }
  66. opt.filename = DFL_FILENAME;
  67. for (i = 1; i < argc; i++) {
  68. p = argv[i];
  69. if ((q = strchr(p, '=')) == NULL) {
  70. goto usage;
  71. }
  72. *q++ = '\0';
  73. if (strcmp(p, "filename") == 0) {
  74. opt.filename = q;
  75. } else {
  76. goto usage;
  77. }
  78. }
  79. /*
  80. * 1.1. Load the CRL
  81. */
  82. mbedtls_printf("\n . Loading the CRL ...");
  83. fflush(stdout);
  84. ret = mbedtls_x509_crl_parse_file(&crl, opt.filename);
  85. if (ret != 0) {
  86. mbedtls_printf(" failed\n ! mbedtls_x509_crl_parse_file returned %d\n\n", ret);
  87. mbedtls_x509_crl_free(&crl);
  88. goto exit;
  89. }
  90. mbedtls_printf(" ok\n");
  91. /*
  92. * 1.2 Print the CRL
  93. */
  94. mbedtls_printf(" . CRL information ...\n");
  95. ret = mbedtls_x509_crl_info((char *) buf, sizeof(buf) - 1, " ", &crl);
  96. if (ret == -1) {
  97. mbedtls_printf(" failed\n ! mbedtls_x509_crl_info returned %d\n\n", ret);
  98. mbedtls_x509_crl_free(&crl);
  99. goto exit;
  100. }
  101. mbedtls_printf("%s\n", buf);
  102. exit_code = MBEDTLS_EXIT_SUCCESS;
  103. exit:
  104. mbedtls_x509_crl_free(&crl);
  105. mbedtls_exit(exit_code);
  106. }
  107. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C &&
  108. MBEDTLS_FS_IO */