cmsverify.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/cms.h>
  15. #include <gmssl/x509.h>
  16. #include <gmssl/rand.h>
  17. static const char *options = "-in file [-out file]";
  18. int cmsverify_main(int argc, char **argv)
  19. {
  20. int ret = 1;
  21. char *prog = argv[0];
  22. char *infile = NULL;
  23. char *outfile = NULL;
  24. FILE *infp = NULL;
  25. FILE *outfp = NULL;
  26. size_t inlen;
  27. uint8_t *cms = NULL;
  28. size_t cmslen, cms_maxlen;
  29. int content_type;
  30. const uint8_t *content;
  31. size_t content_len;
  32. const uint8_t *certs;
  33. size_t certslen;
  34. const uint8_t *crls;
  35. size_t crlslen;
  36. const uint8_t *signer_infos;
  37. size_t signer_infos_len;
  38. int rv;
  39. argc--;
  40. argv++;
  41. if (argc < 1) {
  42. fprintf(stderr, "usage: %s %s\n", prog, options);
  43. return 1;
  44. }
  45. while (argc > 1) {
  46. if (!strcmp(*argv, "-help")) {
  47. printf("usage: %s %s\n", prog, options);
  48. ret = 0;
  49. goto end;
  50. } else if (!strcmp(*argv, "-in")) {
  51. if (--argc < 1) goto bad;
  52. infile = *(++argv);
  53. if (!(infp = fopen(infile, "rb"))) {
  54. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  55. goto end;
  56. }
  57. } else if (!strcmp(*argv, "-out")) {
  58. if (--argc < 1) goto bad;
  59. outfile = *(++argv);
  60. if (!(outfp = fopen(outfile, "wb"))) {
  61. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  62. goto end;
  63. }
  64. } else {
  65. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  66. goto end;
  67. bad:
  68. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  69. goto end;
  70. }
  71. argc--;
  72. argv++;
  73. }
  74. if (!infile) {
  75. fprintf(stderr, "%s: '-in' option required\n", prog);
  76. goto end;
  77. }
  78. if (file_size(infp, &inlen) != 1) {
  79. fprintf(stderr, "%s: get input length failed\n", prog);
  80. goto end;
  81. }
  82. cms_maxlen = (inlen * 3)/4 + 1;
  83. if (!(cms = malloc(cms_maxlen))) {
  84. fprintf(stderr, "%s: malloc failure\n", prog);
  85. goto end;
  86. }
  87. if (cms_from_pem(cms, &cmslen, cms_maxlen, infp) != 1) {
  88. fprintf(stderr, "%s: read CMS failure\n", prog);
  89. goto end;
  90. }
  91. if ((rv = cms_verify(cms, cmslen, NULL, 0, NULL, 0,
  92. &content_type, &content, &content_len,
  93. &certs, &certslen, &crls, &crlslen,
  94. &signer_infos, &signer_infos_len)) < 0) {
  95. fprintf(stderr, "%s: verify error\n", prog);
  96. goto end;
  97. }
  98. printf("verify %s\n", rv ? "success" : "failure");
  99. ret = rv ? 0 : 1;
  100. if (outfile) {
  101. const uint8_t *p;
  102. size_t len;
  103. if (content_type == OID_cms_data) {
  104. if (asn1_octet_string_from_der(&p, &len, &content, &content_len) != 1
  105. || asn1_length_is_zero(content_len) != 1) {
  106. fprintf(stderr, "%s: invalid CMS\n", prog);
  107. goto end;
  108. }
  109. if (len != fwrite(p, 1, len, outfp)) {
  110. fprintf(stderr, "%s: output error : %s\n", prog, strerror(errno));
  111. goto end;
  112. }
  113. } else {
  114. fprintf(stderr, "%s: error\n", prog);
  115. goto end;
  116. }
  117. }
  118. end:
  119. if (infile && infp) fclose(infp);
  120. if (outfile && outfp) fclose(outfp);
  121. if (cms) free(cms);
  122. return ret;
  123. }