• 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 <errno.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <gmssl/sm2.h>
16 #include <gmssl/x509.h>
17 
18 
19 static const char *options = "(-pubkey pem | -cert pem) [-id str] [-in file] -sig file";
20 
sm2verify_main(int argc,char ** argv)21 int sm2verify_main(int argc, char **argv)
22 {
23 	int ret = 1;
24 	char *prog = argv[0];
25 	char *id = SM2_DEFAULT_ID;
26 	char *pubkeyfile = NULL;
27 	char *certfile = NULL;
28 	char *infile = NULL;
29 	char *sigfile = NULL;
30 	FILE *pubkeyfp = NULL;
31 	FILE *certfp = NULL;
32 	FILE *infp = stdin;
33 	FILE *sigfp = NULL;
34 	SM2_KEY key;
35 	SM2_SIGN_CTX verify_ctx;
36 	uint8_t cert[1024];
37 	size_t certlen;
38 	uint8_t buf[4096];
39 	ssize_t len;
40 	uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
41 	size_t siglen;
42 	int vr;
43 
44 	argc--;
45 	argv++;
46 
47 	if (argc < 1) {
48 		fprintf(stderr, "usage: %s %s\n", prog, options);
49 		return 1;
50 	}
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, "-pubkey")) {
58 			if (certfile) {
59 				fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
60 				goto end;
61 			}
62 			if (--argc < 1) goto bad;
63 			pubkeyfile = *(++argv);
64 			if (!(pubkeyfp = fopen(pubkeyfile, "r"))) {
65 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, pubkeyfile, strerror(errno));
66 				goto end;
67 			}
68 		} else if (!strcmp(*argv, "-cert")) {
69 			if (pubkeyfile) {
70 				fprintf(stderr, "%s: options '-pubkey' '-cert' conflict\n", prog);
71 				goto end;
72 			}
73 			if (--argc < 1) goto bad;
74 			certfile = *(++argv);
75 			if (!(certfp = fopen(certfile, "r"))) {
76 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, certfile, strerror(errno));
77 				goto end;
78 			}
79 		} else if (!strcmp(*argv, "-id")) {
80 			if (--argc < 1) goto bad;
81 			id = *(++argv);
82 		} else if (!strcmp(*argv, "-in")) {
83 			if (--argc < 1) goto bad;
84 			infile = *(++argv);
85 			if (!(infp = fopen(infile, "r"))) {
86 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
87 				goto end;
88 			}
89 		} else if (!strcmp(*argv, "-sig")) {
90 			if (--argc < 1) goto bad;
91 			sigfile = *(++argv);
92 			if (!(sigfp = fopen(sigfile, "r"))) {
93 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, sigfile, strerror(errno));
94 				goto end;
95 			}
96 
97 		} else {
98 			fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
99 			goto end;
100 bad:
101 			fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
102 			goto end;
103 		}
104 
105 		argc--;
106 		argv++;
107 	}
108 
109 	if (!sigfile) {
110 		fprintf(stderr, "%s: '-sig' option required\n", prog);
111 		goto end;
112 	}
113 	if ((siglen = fread(sig, 1, sizeof(sig), sigfp)) <= 0) {
114 		fprintf(stderr, "%s: read signature error : %s\n", prog, strerror(errno));
115 		goto end;
116 	}
117 
118 	if (pubkeyfile) {
119 		if (sm2_public_key_info_from_pem(&key, pubkeyfp) != 1) {
120 			fprintf(stderr, "%s: parse public key failed\n", prog);
121 			goto end;
122 		}
123 	} else if (certfile) {
124 		if (x509_cert_from_pem(cert, &certlen, sizeof(cert), certfp) != 1
125 			|| x509_cert_get_subject_public_key(cert, certlen, &key) != 1) {
126 			fprintf(stderr, "%s: parse certificate failed\n", prog);
127 			goto end;
128 		}
129 	} else {
130 		fprintf(stderr, "%s: '-pubkey' or '-cert' option required\n", prog);
131 		goto end;
132 	}
133 
134 
135 	if (sm2_verify_init(&verify_ctx, &key, id, strlen(id)) != 1) {
136 		fprintf(stderr, "%s: inner error\n", prog);
137 		goto end;
138 	}
139 	while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
140 		if (sm2_verify_update(&verify_ctx, buf, len) != 1) {
141 			fprintf(stderr, "%s: inner error\n", prog);
142 			goto end;
143 		}
144 	}
145 	if ((vr = sm2_verify_finish(&verify_ctx, sig, siglen)) < 0) {
146 		fprintf(stderr, "%s: inner error\n", prog);
147 		goto end;
148 	}
149 
150 	fprintf(stdout, "verify : %s\n", vr == 1 ? "success" : "failure");
151 	if (vr == 1) {
152 		ret = 0;
153 	}
154 
155 end:
156 	if (infile && infp) fclose(infp);
157 	if (pubkeyfp) fclose(pubkeyfp);
158 	if (certfp) fclose(certfp);
159 	if (sigfp) fclose(sigfp);
160 	return ret;
161 }
162