• 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/mem.h>
16 #include <gmssl/rand.h>
17 #include <gmssl/pkcs8.h>
18 #include <gmssl/x509.h>
19 #include <gmssl/x509_req.h>
20 
21 
22 static const char *options =
23 	"[-C str] [-ST str] [-L str] [-O str] [-OU str] -CN str -days num"
24 	" -key file [-pass pass] [-out file]";
25 
reqgen_main(int argc,char ** argv)26 int reqgen_main(int argc, char **argv)
27 {
28 	int ret = 1;
29 	char *prog = argv[0];
30 	char *country = NULL;
31 	char *state = NULL;
32 	char *locality = NULL;
33 	char *org = NULL;
34 	char *org_unit = NULL;
35 	char *common_name = NULL;
36 	int days = 0;
37 	char *keyfile = NULL;
38 	char *pass = NULL;
39 	char *outfile = NULL;
40 	uint8_t name[256];
41 	size_t namelen = 0;
42 	FILE *keyfp = NULL;
43 	FILE *outfp = stdout;
44 	uint8_t req[1024];
45 	size_t reqlen = 0;
46 	SM2_KEY sm2_key;
47 
48 	argc--;
49 	argv++;
50 
51 	if (argc < 1) {
52 		fprintf(stderr, "usage: %s %s\n", prog, options);
53 		return 1;
54 	}
55 
56 	while (argc > 0) {
57 		if (!strcmp(*argv, "-help")) {
58 			printf("usage: %s %s\n", prog, options);
59 			ret = 0;
60 			goto end;
61 		} else if (!strcmp(*argv, "-C")) {
62 			if (--argc < 1) goto bad;
63 			country = *(++argv);
64 		} else if (!strcmp(*argv, "-ST")) {
65 			if (--argc < 1) goto bad;
66 			state = *(++argv);
67 		} else if (!strcmp(*argv, "-L")) {
68 			if (--argc < 1) goto bad;
69 			locality = *(++argv);
70 		} else if (!strcmp(*argv, "-O")) {
71 			if (--argc < 1) goto bad;
72 			org = *(++argv);
73 		} else if (!strcmp(*argv, "-OU")) {
74 			if (--argc < 1) goto bad;
75 			org_unit = *(++argv);
76 		} else if (!strcmp(*argv, "-CN")) {
77 			if (--argc < 1) goto bad;
78 			common_name = *(++argv);
79 		} else if (!strcmp(*argv, "-days")) {
80 			if (--argc < 1) goto bad;
81 			days = atoi(*(++argv));
82 			if (days <= 0) {
83 				fprintf(stderr, "%s: invalid '-days' value\n", prog);
84 				goto end;
85 			}
86 		} else if (!strcmp(*argv, "-key")) {
87 			if (--argc < 1) goto bad;
88 			keyfile = *(++argv);
89 			if (!(keyfp = fopen(keyfile, "r"))) {
90 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, keyfile, strerror(errno));
91 				goto end;
92 			}
93 		} else if (!strcmp(*argv, "-pass")) {
94 			if (--argc < 1) goto bad;
95 			pass = *(++argv);
96 		} else if (!strcmp(*argv, "-out")) {
97 			if (--argc < 1) goto bad;
98 			outfile = *(++argv);
99 			if (!(outfp = fopen(outfile, "w"))) {
100 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
101 				goto end;
102 			}
103 		} else {
104 			fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
105 			goto end;
106 bad:
107 			fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
108 			goto end;
109 		}
110 
111 		argc--;
112 		argv++;
113 	}
114 
115 	if (!common_name) {
116 		fprintf(stderr, "%s: '-CN' option required\n", prog);
117 		goto end;
118 	}
119 	if (!days) {
120 		fprintf(stderr, "%s: '-days' option required\n", prog);
121 		goto end;
122 	}
123 	if (!keyfile) {
124 		fprintf(stderr, "%s: '-key' option required\n", prog);
125 		goto end;
126 	}
127 	if (!pass) {
128 		fprintf(stderr, "%s: '-pass' option required\n", prog);
129 		goto end;
130 	}
131 
132 	if (sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, keyfp) != 1) {
133 		fprintf(stderr, "%s: load private key failed\n", prog);
134 		goto end;
135 	}
136 
137 	if (x509_name_set(name, &namelen, sizeof(name),
138 			country, state, locality, org, org_unit, common_name) != 1
139 		|| x509_req_sign(req, &reqlen, sizeof(req),
140 			X509_version_v1,
141 			name, namelen,
142 			&sm2_key,
143 			NULL, 0,
144 			OID_sm2sign_with_sm3,
145 			&sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
146 		fprintf(stderr, "%s: inner error\n", prog);
147 		goto end;
148 	}
149 	if (x509_req_to_pem(req, reqlen, outfp) != 1) {
150 		fprintf(stderr, "%s: output CSR failed\n", prog);
151 		goto end;
152 	}
153 	ret = 0;
154 end:
155 	gmssl_secure_clear(&sm2_key, sizeof(SM2_KEY));
156 	if (keyfp) fclose(keyfp);
157 	if (outfile && outfp) fclose(outfp);
158 	return ret;
159 }
160