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/oid.h>
16 #include <gmssl/sm9.h>
17 #include <gmssl/error.h>
18
19 static const char *options = "-alg (sm9sign|sm9encrypt) -in master_key.pem -inpass str -id str [-out pem] -outpass str";
20
sm9keygen_main(int argc,char ** argv)21 int sm9keygen_main(int argc, char **argv)
22 {
23 int ret = -1;
24 char *prog = argv[0];
25 char *alg = NULL;
26 char *infile = NULL;
27 char *inpass = NULL;
28 char *id = NULL;
29 char *outfile = NULL;
30 char *outpass = NULL;
31 int oid = 0;
32 FILE *infp = stdin;
33 FILE *outfp = stdout;
34 SM9_SIGN_MASTER_KEY sign_msk;
35 SM9_ENC_MASTER_KEY enc_msk;
36 SM9_SIGN_KEY sign_key;
37 SM9_ENC_KEY enc_key;
38
39 argc--;
40 argv++;
41
42 if (argc < 1) {
43 fprintf(stderr, "usage: %s %s\n", prog, options);
44 return 1;
45 }
46
47 while (argc > 0) {
48 if (!strcmp(*argv, "-help")) {
49 fprintf(stdout, "usage: %s %s\n", prog, options);
50 return 0;
51 } else if (!strcmp(*argv, "-alg")) {
52 if (--argc < 1) goto bad;
53 alg = *(++argv);
54 if ((oid = sm9_oid_from_name(alg)) < 1) {
55 fprintf(stdout, "%s: invalid alg '%s', should be sm9sign or sm9encrypt\n", prog, alg);
56 goto end;
57 }
58 } else if (!strcmp(*argv, "-in")) {
59 if (--argc < 1) goto bad;
60 infile = *(++argv);
61 if (!(infp = fopen(infile, "r"))) {
62 error_print();
63 goto end;
64 }
65 } else if (!strcmp(*argv, "-inpass")) {
66 if (--argc < 1) goto bad;
67 inpass = *(++argv);
68 } else if (!strcmp(*argv, "-id")) {
69 if (--argc < 1) goto bad;
70 id = *(++argv);
71 } else if (!strcmp(*argv, "-out")) {
72 if (--argc < 1) goto bad;
73 outfile = *(++argv);
74 if (!(outfp = fopen(outfile, "w"))) {
75 error_print();
76 goto end;
77 }
78 } else if (!strcmp(*argv, "-outpass")) {
79 if (--argc < 1) goto bad;
80 outpass = *(++argv);
81 } else {
82 bad:
83 fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
84 return 1;
85 }
86
87
88 argc--;
89 argv++;
90 }
91
92 if (!id) {
93 fprintf(stderr, "%s: option '-id' is required\n", prog);
94 goto end;
95 }
96 if (!inpass || !outpass) {
97 error_print();
98 goto end;
99 }
100
101 switch (oid) {
102 case OID_sm9sign:
103 if (sm9_sign_master_key_info_decrypt_from_pem(&sign_msk, inpass, infp) != 1
104 || sm9_sign_master_key_extract_key(&sign_msk, id, strlen(id), &sign_key) != 1
105 || sm9_sign_key_info_encrypt_to_pem(&sign_key, outpass, outfp) != 1) {
106 error_print();
107 goto end;
108 }
109 break;
110 case OID_sm9encrypt:
111 if (sm9_enc_master_key_info_decrypt_from_pem(&enc_msk, inpass, infp) != 1
112 || sm9_enc_master_key_extract_key(&enc_msk, id, strlen(id), &enc_key) != 1
113 || sm9_enc_key_info_encrypt_to_pem(&enc_key, outpass, outfp) != 1) {
114 error_print();
115 goto end;
116 }
117 break;
118 default:
119 error_print();
120 goto end;
121 }
122 ret = 0;
123 end:
124 gmssl_secure_clear(&sign_msk, sizeof(sign_msk));
125 gmssl_secure_clear(&enc_msk, sizeof(enc_msk));
126 gmssl_secure_clear(&sign_key, sizeof(sign_key));
127 gmssl_secure_clear(&enc_key, sizeof(enc_key));
128 if (infile && infp) fclose(infp);
129 if (outfile && outfp) fclose(outfp);
130 return 1;
131 }
132