crlget.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2014-2023 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/pem.h>
  14. #include <gmssl/x509.h>
  15. #include <gmssl/x509_ext.h>
  16. #include <gmssl/x509_crl.h>
  17. #include <gmssl/http.h>
  18. #include <gmssl/error.h>
  19. static const char *usage = "-cert pem [-out file]\n";
  20. static const char *options =
  21. "Options\n"
  22. "\n"
  23. " -cert pem Input certificates in PEM format.\n"
  24. " -out der | stdout Output CRL file in DER-encoding\n"
  25. "\n"
  26. "Examples\n"
  27. "\n"
  28. " gmssl crlget -cert cert.pem -out crl.der\n"
  29. "\n";
  30. int crlget_main(int argc, char **argv)
  31. {
  32. int ret = 1;
  33. char *prog = argv[0];
  34. char *str;
  35. uint8_t *cert = NULL;
  36. size_t certlen = 0;
  37. char *outfile = NULL;
  38. FILE *outfp = stdout;
  39. uint8_t *crl = NULL;
  40. size_t crl_len = 0;
  41. argc--;
  42. argv++;
  43. while (argc > 0) {
  44. if (!strcmp(*argv, "-help")) {
  45. printf("usage: gmssl %s %s\n\n", prog, usage);
  46. printf("%s\n", options);
  47. goto end;
  48. } else if (!strcmp(*argv, "-cert")) {
  49. if (--argc < 1) goto bad;
  50. str = *(++argv);
  51. if (x509_cert_new_from_file(&cert, &certlen, str) != 1) {
  52. fprintf(stderr, "%s: load ca certificate '%s' failure\n", prog, str);
  53. goto end;
  54. }
  55. } else if (!strcmp(*argv, "-out")) {
  56. if (--argc < 1) goto bad;
  57. outfile = *(++argv);
  58. if (!(outfp = fopen(outfile, "wb"))) {
  59. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  60. goto end;
  61. }
  62. } else {
  63. fprintf(stderr, "%s: illegal option `%s`\n", prog, *argv);
  64. goto end;
  65. bad:
  66. fprintf(stderr, "%s: `%s` option value missing\n", prog, *argv);
  67. goto end;
  68. }
  69. argc--;
  70. argv++;
  71. }
  72. if (!cert) {
  73. fprintf(stderr, "%s: `-cert` option required\n", prog);
  74. printf("usage: gmssl %s %s\n\n", prog, usage);
  75. goto end;
  76. }
  77. /*
  78. const uint8_t *exts;
  79. size_t extslen;
  80. if (x509_cert_get_exts(cert, certlen, &exts, &extslen) != 1) {
  81. error_print();
  82. goto end;
  83. }
  84. if (!exts) {
  85. goto end;
  86. }
  87. int critical;
  88. const uint8_t *val;
  89. size_t vlen;
  90. if ((ret = x509_exts_get_ext_by_oid(exts, extslen, OID_ce_crl_distribution_points, &critical, &val, &vlen)) < 0) {
  91. error_print();
  92. goto end;
  93. }
  94. char *uristr;
  95. const char *uri;
  96. size_t urilen;
  97. int reason;
  98. const uint8_t *crl_issuer;
  99. size_t crl_issuer_len;
  100. if (x509_uri_as_distribution_points_from_der(&uri, &urilen, &reason, &crl_issuer, &crl_issuer_len, &val, &vlen) != 1) {
  101. error_print();
  102. goto end;
  103. }
  104. if (!(uristr = strndup(uri, urilen))) {
  105. error_print();
  106. goto end;
  107. }
  108. if (http_get(uristr, NULL, &crl_len, 0) < 0) {
  109. error_print();
  110. goto end;
  111. }
  112. if (!(crl = malloc(crl_len))) {
  113. error_print();
  114. goto end;
  115. }
  116. if (http_get(uristr, crl, &crl_len, crl_len) != 1) {
  117. error_print();
  118. goto end;
  119. }
  120. */
  121. if (x509_crl_new_from_cert(&crl, &crl_len, cert, certlen) != 1) {
  122. error_print();
  123. goto end;
  124. }
  125. fwrite(crl, crl_len, 1, outfp);
  126. ret = 0;
  127. end:
  128. if (cert) free(cert);
  129. if (outfile && outfp) fclose(outfp);
  130. return ret;
  131. }