crlverify.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/hex.h>
  14. #include <gmssl/file.h>
  15. #include <gmssl/x509.h>
  16. #include <gmssl/x509_crl.h>
  17. static const char *usage = " -in der -cacert pem [-req_sm2_id str | -req_sm2_id_hex hex]\n";
  18. static const char *options =
  19. "Options\n"
  20. "\n"
  21. " -in pem Input CSR file in PEM format\n"
  22. " -cacert pem Issuer CA certificate\n"
  23. " -sm2_id str Authority's ID in SM2 signature algorithm\n"
  24. " -sm2_id_hex hex Authority's ID in hex format\n"
  25. " When `-sm2_id` or `-sm2_id_hex` is specified,\n"
  26. " must use the same ID in other commands explicitly.\n"
  27. " If neither `-sm2_id` nor `-sm2_id_hex` is specified,\n"
  28. " the default string '1234567812345678' is used\n"
  29. "\n"
  30. "Examples\n"
  31. "\n"
  32. " gmssl certverify -in crl.der -cacert cacert.pem\n"
  33. "\n";
  34. int crlverify_main(int argc, char **argv)
  35. {
  36. int ret = 1;
  37. char *prog = argv[0];
  38. char *str;
  39. uint8_t *crl = NULL;
  40. size_t crl_len;
  41. uint8_t *cacert = NULL;
  42. size_t cacertlen;
  43. char signer_id[SM2_MAX_ID_LENGTH + 1] = SM2_DEFAULT_ID;
  44. size_t signer_id_len = strlen(SM2_DEFAULT_ID);
  45. int rv;
  46. argc--;
  47. argv++;
  48. if (argc < 1) {
  49. fprintf(stderr, "usage: %s %s\n", prog, options);
  50. return 1;
  51. }
  52. while (argc > 0) {
  53. if (!strcmp(*argv, "-help")) {
  54. printf("usage: %s %s\n", prog, options);
  55. ret = 0;
  56. goto end;
  57. } else if (!strcmp(*argv, "-in")) {
  58. if (--argc < 1) goto bad;
  59. str = *(++argv);
  60. if (file_read_all(str, &crl, &crl_len) != 1) {
  61. fprintf(stderr, "%s: read '%s' failure : %s\n", prog, str, strerror(errno));
  62. goto end;
  63. }
  64. } else if (!strcmp(*argv, "-cacert")) {
  65. if (--argc < 1) goto bad;
  66. str = *(++argv);
  67. if (x509_cert_new_from_file(&cacert, &cacertlen, str) != 1) {
  68. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, str, strerror(errno));
  69. goto end;
  70. }
  71. } else if (!strcmp(*argv, "-sm2_id")) {
  72. if (--argc < 1) goto bad;
  73. str = *(++argv);
  74. if (strlen(str) > sizeof(signer_id) - 1) {
  75. fprintf(stderr, "%s: invalid `-sm2_id` length\n", prog);
  76. goto end;
  77. }
  78. strncpy(signer_id, str, sizeof(signer_id));
  79. signer_id_len = strlen(str);
  80. } else if (!strcmp(*argv, "-sm2_id_hex")) {
  81. if (--argc < 1) goto bad;
  82. str = *(++argv);
  83. if (strlen(str) > (sizeof(signer_id) - 1) * 2) {
  84. fprintf(stderr, "%s: invalid `-sm2_id_hex` length\n", prog);
  85. goto end;
  86. }
  87. if (hex_to_bytes(str, strlen(str), (uint8_t *)signer_id, &signer_id_len) != 1) {
  88. fprintf(stderr, "%s: invalid `-sm2_id_hex` value\n", prog);
  89. goto end;
  90. }
  91. } else {
  92. fprintf(stderr, "%s: illegal option `%s`\n", prog, *argv);
  93. goto end;
  94. bad:
  95. fprintf(stderr, "%s: `%s` option value missing\n", prog, *argv);
  96. goto end;
  97. }
  98. argc--;
  99. argv++;
  100. }
  101. if (!crl) {
  102. fprintf(stderr, "%s: `-in` option required\n", prog);
  103. goto end;
  104. }
  105. if (!cacert) {
  106. fprintf(stderr, "%s: `-cacert` option required\n", prog);
  107. goto end;
  108. }
  109. if (x509_crl_check(crl, crl_len, time(NULL)) != 1) {
  110. fprintf(stderr, "%s: invalid CRL data or format\n", prog);
  111. goto end;
  112. }
  113. if ((rv = x509_crl_verify_by_ca_cert(crl, crl_len, cacert, cacertlen, signer_id, signer_id_len)) < 0) {
  114. fprintf(stderr, "%s: verification inner error\n", prog);
  115. goto end;
  116. }
  117. printf("Verification %s\n", rv ? "success" : "failure");
  118. if (rv == 1) ret = 0;
  119. end:
  120. if (crl) free(crl);
  121. if (cacert) free(cacert);
  122. return ret;
  123. }