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/mem.h>
16 #include <gmssl/sm2.h>
17
18
19 static const char *options = "-key pem -pass str [-in file] [-out file]";
20
sm2decrypt_main(int argc,char ** argv)21 int sm2decrypt_main(int argc, char **argv)
22 {
23 int ret = 1;
24 char *prog = argv[0];
25 char *keyfile = NULL;
26 char *pass = NULL;
27 char *infile = NULL;
28 char *outfile = NULL;
29 FILE *keyfp = NULL;
30 FILE *infp = stdin;
31 FILE *outfp = stdout;
32 SM2_KEY key;
33 uint8_t inbuf[SM2_MAX_CIPHERTEXT_SIZE];
34 uint8_t outbuf[SM2_MAX_CIPHERTEXT_SIZE];
35 size_t inlen, outlen;
36
37 argc--;
38 argv++;
39
40 if (argc < 1) {
41 fprintf(stderr, "usage: %s %s\n", prog, options);
42 return 1;
43 }
44
45 while (argc > 0) {
46 if (!strcmp(*argv, "-help")) {
47 printf("usage: %s %s\n", prog, options);
48 ret = 0;
49 goto end;
50 } else if (!strcmp(*argv, "-key")) {
51 if (--argc < 1) goto bad;
52 keyfile = *(++argv);
53 if (!(keyfp = fopen(keyfile, "r"))) {
54 fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
55 goto end;
56 }
57 } else if (!strcmp(*argv, "-pass")) {
58 if (--argc < 1) goto bad;
59 pass = *(++argv);
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 (!keyfile) {
87 fprintf(stderr, "%s: '-key' option required\n", prog);
88 goto end;
89 }
90 if (!pass) {
91 fprintf(stderr, "%s: '-pass' option required\n", prog);
92 goto end;
93 }
94
95 if (sm2_private_key_info_decrypt_from_pem(&key, pass, keyfp) != 1) {
96 fprintf(stderr, "%s: private key decryption failure\n", prog);
97 goto end;
98 }
99
100 if ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) <= 0) {
101 fprintf(stderr, "%s: read input failed : %s\n", prog, strerror(errno));
102 goto end;
103 }
104 if (sm2_decrypt(&key, inbuf, inlen, outbuf, &outlen) != 1) {
105 fprintf(stderr, "%s: decryption failure\n", prog);
106 goto end;
107 }
108 if (outlen != fwrite(outbuf, 1, outlen, outfp)) {
109 fprintf(stderr, "%s: output plaintext failed : %s\n", prog, strerror(errno));
110 goto end;
111 }
112 ret = 0;
113 end:
114 gmssl_secure_clear(&key, sizeof(key));
115 if (keyfp) fclose(keyfp);
116 if (infile && infp) fclose(infp);
117 if (outfile && outfp) fclose(outfp);
118 return ret;
119 }
120