• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string.h>
13 #include <stdlib.h>
14 #include <gmssl/sm9.h>
15 #include <gmssl/error.h>
16 
17 
18 static const char *options = "[-in file] -pubmaster file -id str -sig file";
19 
sm9verify_main(int argc,char ** argv)20 int sm9verify_main(int argc, char **argv)
21 {
22 	int ret = 1;
23 	char *prog = argv[0];
24 	char *infile = NULL;
25 	char *mpkfile = NULL;
26 	char *id = NULL;
27 	char *sigfile = NULL;
28 	FILE *infp = stdin;
29 	FILE *mpkfp = NULL;
30 	FILE *sigfp = NULL;
31 	SM9_SIGN_MASTER_KEY mpk;
32 	SM9_SIGN_CTX ctx;
33 	uint8_t buf[4096];
34 	ssize_t len;
35 	uint8_t sig[SM9_SIGNATURE_SIZE];
36 	ssize_t siglen;
37 
38 	argc--;
39 	argv++;
40 
41 	if (argc < 1) {
42 		fprintf(stderr, "usage: %s %s\n", prog, options);
43 		return 1;
44 	}
45 
46 	while (argc > 0) {
47 		if (!strcmp(*argv, "-help")) {
48 			fprintf(stdout, "usage: %s %s\n", prog, options);
49 			return 0;
50 		} else if (!strcmp(*argv, "-in")) {
51 			if (--argc < 1) goto bad;
52 			infile = *(++argv);
53 			if (!(infp = fopen(infile, "r"))) {
54 				error_print();
55 				goto end;
56 			}
57 		} else if (!strcmp(*argv, "-pubmaster")) {
58 			if (--argc < 1) goto bad;
59 			mpkfile = *(++argv);
60 			if (!(mpkfp = fopen(mpkfile, "r"))) {
61 				error_print();
62 				goto end;
63 			}
64 		} else if (!strcmp(*argv, "-id")) {
65 			if (--argc < 1) goto bad;
66 			id = *(++argv);
67 		} else if (!strcmp(*argv, "-sig")) {
68 			if (--argc < 1) goto bad;
69 			sigfile = *(++argv);
70 			if (!(sigfp = fopen(sigfile, "r"))) {
71 				error_print();
72 				goto end;
73 			}
74 		} else {
75 bad:
76 			fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
77 			return 1;
78 		}
79 
80 		argc--;
81 		argv++;
82 	}
83 
84 	if (!mpkfile || !id || !sigfile) {
85 		error_print();
86 		goto end;
87 	}
88 
89 	if (sm9_sign_master_public_key_from_pem(&mpk, mpkfp) != 1) {
90 		error_print();
91 		goto end;
92 	}
93 
94 	if ((siglen = fread(sig, 1, sizeof(sig), sigfp)) <= 0) {
95 		error_print();
96 		goto end;
97 	}
98 
99 	if (sm9_verify_init(&ctx) != 1) {
100 		error_print();
101 		goto end;
102 	}
103 	while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
104 		if (sm9_verify_update(&ctx, buf, len) != 1) {
105 			error_print();
106 			goto end;
107 		}
108 	}
109 	if ((ret = sm9_verify_finish(&ctx, sig, siglen, &mpk, id, strlen(id))) != 1) {
110 		error_print();
111 		goto end;
112 	}
113 	printf("%s %s\n", prog, ret ? "success" : "failure");
114 
115 end:
116 	if (infile && infp) fclose(infp);
117 	if (mpkfile && mpkfp) fclose(mpkfp);
118 	if (sigfile && sigfp) fclose(sigfp);
119 	return ret;
120 }
121 
122 
123 
124 
125 
126 
127 
128 
129