crlparse.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <gmssl/file.h>
  14. #include <gmssl/x509.h>
  15. #include <gmssl/x509_crl.h>
  16. static const char *options = "-in file [-out file]";
  17. int crlparse_main(int argc, char **argv)
  18. {
  19. int ret = 1;
  20. char *prog = argv[0];
  21. char *infile = NULL;
  22. char *outfile = NULL;
  23. FILE *infp = stdin;
  24. FILE *outfp = stdout;
  25. uint8_t *in = NULL;
  26. size_t inlen;
  27. const uint8_t *pin;
  28. const uint8_t *crl = NULL;
  29. size_t crllen;
  30. argc--;
  31. argv++;
  32. if (argc < 1) {
  33. fprintf(stderr, "usage: %s %s\n", prog, options);
  34. return 1;
  35. }
  36. while (argc > 0) {
  37. if (!strcmp(*argv, "-help")) {
  38. printf("usage: %s %s\n", prog, options);
  39. goto end;
  40. } else if (!strcmp(*argv, "-in")) {
  41. if (--argc < 1) goto bad;
  42. infile = *(++argv);
  43. if (!(infp = fopen(infile, "rb"))) {
  44. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  45. goto end;
  46. }
  47. } else if (!strcmp(*argv, "-out")) {
  48. if (--argc < 1) goto bad;
  49. outfile = *(++argv);
  50. if (!(outfp = fopen(outfile, "wb"))) {
  51. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  52. goto end;
  53. }
  54. } else {
  55. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  56. goto end;
  57. bad:
  58. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  59. goto end;
  60. }
  61. argc--;
  62. argv++;
  63. }
  64. if (!infile) {
  65. fprintf(stderr, "%s: '-in' option required\n", prog);
  66. goto end;
  67. }
  68. if (file_size(infp, &inlen) != 1) {
  69. fprintf(stderr, "%s: get input length failed\n", prog);
  70. goto end;
  71. }
  72. if (!(in = malloc(inlen))) {
  73. fprintf(stderr, "%s: malloc failure\n", prog);
  74. goto end;
  75. }
  76. if (fread(in, 1, inlen, infp) != inlen) {
  77. fprintf(stderr, "%s: read file error : %s\n", prog, strerror(errno));
  78. goto end;
  79. }
  80. pin = in;
  81. if (x509_crl_from_der(&crl, &crllen, &pin, &inlen) != 1
  82. || asn1_length_is_zero(inlen) != 1) {
  83. fprintf(stderr, "%s: read CRL failure\n", prog);
  84. goto end;
  85. }
  86. x509_crl_print(outfp, 0, 0, "CRL", crl, crllen);
  87. ret = 0;
  88. end:
  89. if (infile && infp) fclose(infp);
  90. if (outfile && outfp) fclose(outfp);
  91. if (in) free(in);
  92. return ret;
  93. }