certrevoke.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_crl.h>
  16. static const char *options =
  17. " -in pem"
  18. " [-reason str]"
  19. " [-invalid_date time]"
  20. " -out der"; // on windows, send 0x0a through pipe will be connverted to 0x0d0a
  21. // so stdout and pipe is not supported
  22. static char *usage =
  23. "Options\n"
  24. "\n"
  25. " -in pem Certificate in PEM format to be revoked\n"
  26. " -reason str Revocation reason code, avaiable codes:\n"
  27. " * unspecified\n"
  28. " * keyCompromise\n"
  29. " * cACompromise\n"
  30. " * affiliationChanged\n"
  31. " * superseded\n"
  32. " * cessationOfOperation\n"
  33. " * certificateHold\n"
  34. " * notAssigned\n"
  35. " * removeFromCRL\n"
  36. " * privilegeWithdrawn\n"
  37. " * aACompromise\n"
  38. " -invalid_date time The date on which it is known or suspected the certificate became invalid\n"
  39. " Time in `YYYYMMDDHHMMSSZ` format such as 20221231000000Z\n"
  40. " The last 'Z' means it is Zulu (GMT) time\n"
  41. " -out der Output X.509 RevokedCertificate in DER-encoding\n"
  42. " This file stores multiple RevokedCertificates, used as input by `crlsign`\n"
  43. "\n"
  44. "Examples\n"
  45. "\n"
  46. " gmssl certrevoke -in cert1.pem -reason keyCompromise -invalid_date 20221230000000Z -out revoked_certs.der\n"
  47. " gmssl certrevoke -in cert1.pem -reason keyCompromise -invalid_date 20221230000000Z >> revoked_certs.der\n"
  48. "\n";
  49. int certrevoke_main(int argc, char **argv)
  50. {
  51. int ret = 1;
  52. char *prog = argv[0];
  53. char *str;
  54. uint8_t *cert = NULL;
  55. size_t certlen;
  56. int reason = -1;
  57. time_t invalid_date = -1;
  58. char *outfile = NULL;
  59. FILE *outfp = NULL;
  60. uint8_t *outbuf = NULL;
  61. uint8_t *out;
  62. size_t outlen = 0;
  63. argc--;
  64. argv++;
  65. while (argc > 0) {
  66. if (!strcmp(*argv, "-help")) {
  67. printf("usage: gmssl %s %s\n\n", prog, options);
  68. printf("%s", usage);
  69. goto end;
  70. } else if (!strcmp(*argv, "-in")) {
  71. if (--argc < 1) goto bad;
  72. str = *(++argv);
  73. if (x509_cert_new_from_file(&cert, &certlen, str) != 1) {
  74. fprintf(stderr, "%s: open cert file '%s' failure\n", prog, str);
  75. goto end;
  76. }
  77. } else if (!strcmp(*argv, "-out")) {
  78. if (--argc < 1) goto bad;
  79. outfile = *(++argv);
  80. if (!(outfp = fopen(outfile, "ab"))) {
  81. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  82. goto end;
  83. }
  84. } else if (!strcmp(*argv, "-reason")) {
  85. if (--argc < 1) goto bad;
  86. str = *(++argv);
  87. if (x509_crl_reason_from_name(&reason, str) != 1) {
  88. fprintf(stderr, "%s: invalid reason '%s'\n", prog, str);
  89. goto end;
  90. }
  91. } else if (!strcmp(*argv, "-invalid_date")) {
  92. if (--argc < 1) goto bad;
  93. str =*(++argv);
  94. if (asn1_time_from_str(0, &invalid_date, str) != 1) {
  95. fprintf(stderr, "%s: invalid time '%s', should in 'YYYYMMDDHHMMSSZ' format\n", prog, str);
  96. goto end;
  97. }
  98. } else {
  99. fprintf(stderr, "%s: illegal option `%s`\n", prog, *argv);
  100. goto end;
  101. bad:
  102. fprintf(stderr, "%s: `%s` option value missing\n", prog, *argv);
  103. goto end;
  104. }
  105. argc--;
  106. argv++;
  107. }
  108. if (!cert) {
  109. fprintf(stderr, "%s: option `-in` missing\n", prog);
  110. printf("usage: gmssl %s %s\n\n", prog, options);
  111. goto end;
  112. }
  113. if (!outfile) {
  114. fprintf(stderr, "%s: option `-out` missing\n", prog);
  115. goto end;
  116. }
  117. if (x509_cert_revoke_to_der(cert, certlen, time(NULL), reason, invalid_date, NULL, 0, NULL, &outlen) != 1) {
  118. fprintf(stderr, "%s: inner error\n", prog);
  119. goto end;
  120. }
  121. if (!(outbuf = malloc(outlen))) {
  122. fprintf(stderr, "%s: malloc failure\n", prog);
  123. goto end;
  124. }
  125. out = outbuf;
  126. outlen = 0;
  127. if (x509_cert_revoke_to_der(cert, certlen, time(NULL), reason, invalid_date, NULL, 0, &out, &outlen) != 1) {
  128. fprintf(stderr, "%s: inner error\n", prog);
  129. goto end;
  130. }
  131. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  132. fprintf(stderr, "%s: output failure\n", prog);
  133. goto end;
  134. }
  135. ret = 0;
  136. end:
  137. if (cert) free(cert);
  138. if (outfile && outfp) fclose(outfp);
  139. if (outbuf) free(outbuf);
  140. return ret;
  141. }