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