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