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