xref: /netbsd-src/crypto/dist/ipsec-tools/src/racoon/plainrsa-gen.c (revision f0fde9902fd4d72ded2807793acc7bfaa1ebf243)
1 /*	$NetBSD: plainrsa-gen.c,v 1.7 2022/01/23 14:35:45 christos Exp $	*/
2 
3 /* Id: plainrsa-gen.c,v 1.6 2005/04/21 09:08:40 monas Exp */
4 /*
5  * Copyright (C) 2004 SuSE Linux AG, Nuernberg, Germany.
6  * Contributed by: Michal Ludvig <mludvig@suse.cz>, SUSE Labs
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /* This file contains a generator for FreeS/WAN-style ipsec.secrets RSA keys. */
35 
36 #include "config.h"
37 
38 #include <stdio.h>
39 #include <string.h>
40 #include <errno.h>
41 
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/socket.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 
48 #include <openssl/bio.h>
49 #include <openssl/bn.h>
50 #include <openssl/err.h>
51 #include <openssl/objects.h>
52 #include <openssl/pem.h>
53 #include <openssl/rsa.h>
54 #include <openssl/evp.h>
55 #ifdef HAVE_OPENSSL_ENGINE_H
56 #include <openssl/engine.h>
57 #endif
58 
59 #include "misc.h"
60 #include "vmbuf.h"
61 #include "plog.h"
62 #include "crypto_openssl.h"
63 
64 #include "package_version.h"
65 
66 #define DEFAULT_PUBEXP RSA_F4
67 
68 void
usage(char * argv0)69 usage (char *argv0)
70 {
71 	fprintf(stderr, "Plain RSA key generator, part of %s\n", TOP_PACKAGE_STRING);
72 	fprintf(stderr, "By Michal Ludvig (http://www.logix.cz/michal)\n");
73 	fprintf(stderr, "\n");
74 	fprintf(stderr, "Usage: %s [options]\n", argv0);
75 	fprintf(stderr, "\n");
76 	fprintf(stderr, "  -b bits       Generate <bits> long RSA key (default=1024)\n");
77 	fprintf(stderr, "  -e pubexp     Public exponent to use (default=%#x)\n", DEFAULT_PUBEXP);
78 	fprintf(stderr, "  -f filename   Filename to store the key to (default=stdout)\n");
79 	fprintf(stderr, "  -i filename   Input source for format conversion\n");
80 	fprintf(stderr, "  -h            Help\n");
81 	fprintf(stderr, "\n");
82 	fprintf(stderr, "Report bugs to <ipsec-tools-devel@lists.sourceforge.net>\n");
83 	exit(1);
84 }
85 
86 /*
87  * See RFC 2065, section 3.5 for details about the output format.
88  */
89 vchar_t *
mix_b64_pubkey(const RSA * key)90 mix_b64_pubkey(const RSA *key)
91 {
92 	char *binbuf;
93 	long binlen, ret;
94 	vchar_t *res;
95 
96 	binlen = 1 + BN_num_bytes(RSA_get0_e(key)) + BN_num_bytes(RSA_get0_n(key));
97 	binbuf = malloc(binlen);
98 	memset(binbuf, 0, binlen);
99 	binbuf[0] = BN_bn2bin(RSA_get0_e(key), (unsigned char *) &binbuf[1]);
100 	ret = BN_bn2bin(RSA_get0_n(key), (unsigned char *) (&binbuf[binbuf[0] + 1]));
101 	if (1 + binbuf[0] + ret != binlen) {
102 		plog(LLV_ERROR, LOCATION, NULL,
103 		     "Pubkey generation failed. This is really strange...\n");
104 		return NULL;
105 	}
106 
107 	return base64_encode(binbuf, binlen);
108 }
109 
110 char *
lowercase(char * input)111 lowercase(char *input)
112 {
113 	char *ptr = input;
114 	while (*ptr) {
115 		if (*ptr >= 'A' && *ptr <= 'F')
116 			*ptr -= 'A' - 'a';
117 		*ptr++;
118 	}
119 
120 	return input;
121 }
122 
123 int
print_rsa_key(FILE * fp,const RSA * key)124 print_rsa_key(FILE *fp, const RSA *key)
125 {
126 	vchar_t *pubkey64 = NULL;
127 
128 	pubkey64 = mix_b64_pubkey(key);
129 	if (!pubkey64) {
130 		fprintf(stderr, "mix_b64_pubkey(): %s\n", eay_strerror());
131 		return -1;
132 	}
133 
134 	fprintf(fp, "# : PUB 0s%s\n", pubkey64->v);
135 	fprintf(fp, ": RSA\t{\n");
136 	fprintf(fp, "\t# RSA %d bits\n", BN_num_bits(RSA_get0_n(key)));
137 	fprintf(fp, "\t# pubkey=0s%s\n", pubkey64->v);
138 	fprintf(fp, "\tModulus: 0x%s\n", lowercase(BN_bn2hex(RSA_get0_n(key))));
139 	fprintf(fp, "\tPublicExponent: 0x%s\n", lowercase(BN_bn2hex(RSA_get0_e(key))));
140 	fprintf(fp, "\tPrivateExponent: 0x%s\n", lowercase(BN_bn2hex(RSA_get0_d(key))));
141 	fprintf(fp, "\tPrime1: 0x%s\n", lowercase(BN_bn2hex(RSA_get0_p(key))));
142 	fprintf(fp, "\tPrime2: 0x%s\n", lowercase(BN_bn2hex(RSA_get0_q(key))));
143 	fprintf(fp, "\tExponent1: 0x%s\n", lowercase(BN_bn2hex(RSA_get0_dmp1(key))));
144 	fprintf(fp, "\tExponent2: 0x%s\n", lowercase(BN_bn2hex(RSA_get0_dmq1(key))));
145 	fprintf(fp, "\tCoefficient: 0x%s\n", lowercase(BN_bn2hex(RSA_get0_iqmp(key))));
146 	fprintf(fp, "  }\n");
147 
148 	vfree(pubkey64);
149 	return 0;
150 }
151 
152 int
print_public_rsa_key(FILE * fp,const RSA * key)153 print_public_rsa_key(FILE *fp, const RSA *key)
154 {
155 	vchar_t *pubkey64 = NULL;
156 
157 	pubkey64 = mix_b64_pubkey(key);
158 	if (!pubkey64) {
159 		fprintf(stderr, "mix_b64_pubkey(): %s\n", eay_strerror());
160 		return -1;
161 	}
162 
163 	fprintf(fp, ": PUB 0s%s\n", pubkey64->v);
164 
165 	vfree(pubkey64);
166 	return 0;
167 }
168 
169 int
convert_rsa_key(FILE * fpout,FILE * fpin)170 convert_rsa_key(FILE *fpout, FILE *fpin)
171 {
172 	int ret;
173 	RSA *key = NULL;
174 
175 	key = PEM_read_RSAPrivateKey(fpin, NULL, NULL, NULL);
176 	if (key) {
177 		ret = print_rsa_key(fpout, key);
178 		RSA_free(key);
179 
180 		return ret;
181 	}
182 
183 	rewind(fpin);
184 
185 	key = PEM_read_RSA_PUBKEY(fpin, NULL, NULL, NULL);
186 	if (key) {
187 		ret = print_public_rsa_key(fpout, key);
188 		RSA_free(key);
189 
190 		return ret;
191 	}
192 
193 	/* Implement parsing of input stream containing
194 	 * private or public "plainrsa" formatted text.
195 	 * Convert the result to PEM formatted output.
196 	 *
197 	 * This seemingly needs manual use of prsaparse().
198 	 * An expert ought to do this. */
199 
200 	fprintf(stderr, "convert_rsa_key: %s\n", "Only conversion from PEM at this time");
201 	return -1;
202 }
203 
204 int
gen_rsa_key(FILE * fp,size_t bits,unsigned long exp)205 gen_rsa_key(FILE *fp, size_t bits, unsigned long exp)
206 {
207 	int ret;
208 	RSA *key;
209 	BIGNUM *e;
210 
211 	key = RSA_new();
212 	e = BN_new();
213 	BN_set_word(e, exp);
214 
215 	if (1 != RSA_generate_key_ex(key, bits, e, NULL)) {
216 		fprintf(stderr, "RSA_generate_key(): %s\n", eay_strerror());
217 		return -1;
218 	}
219 
220 	ret = print_rsa_key(fp, key);
221 	RSA_free(key);
222 
223 	return ret;
224 }
225 
226 int
main(int argc,char * argv[])227 main (int argc, char *argv[])
228 {
229 	FILE *fp = stdout, *fpin = NULL;
230 	size_t bits = 1024;
231 	unsigned int pubexp = DEFAULT_PUBEXP;
232 	struct stat st;
233 	extern char *optarg;
234 	extern int optind;
235 	int c, fd = -1, fdin = -1;
236 	char *fname = NULL, *finput = NULL;
237 
238 	while ((c = getopt(argc, argv, "e:b:f:i:h")) != -1)
239 		switch (c) {
240 			case 'e':
241 				pubexp = (unsigned int)strtoul(optarg, NULL, 0);
242 				break;
243 			case 'b':
244 				bits = atoi(optarg);
245 				break;
246 			case 'f':
247 				fname = optarg;
248 				break;
249 			case 'i':
250 				finput = optarg;
251 				break;
252 			case 'h':
253 			default:
254 				usage(argv[0]);
255 		}
256 
257 	if (fname) {
258 		umask(0077);
259 		/* Restrictive access due to private key material. */
260 		fd = open(fname, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
261 		if (fd < 0) {
262 			if (errno == EEXIST)
263 				fprintf(stderr, "%s: file exists! Please use a different name.\n", fname);
264 			else
265 				fprintf(stderr, "%s: %s\n", fname, strerror(errno));
266 			exit(1);
267 		}
268 		fp = fdopen(fd, "w");
269 		if (fp == NULL) {
270 			fprintf(stderr, "%s: %s\n", fname, strerror(errno));
271 			close(fd);
272 			exit(1);
273 		}
274 	}
275 
276 	if (finput) {
277 		/* Restrictive access once more. Do not be fooled by a link. */
278 		fdin = open(finput, O_RDONLY | O_NOFOLLOW);
279 		if (fdin < 0) {
280 			if (errno == ELOOP)
281 				fprintf(stderr, "%s: file is a link. Discarded for security.\n", fname);
282 			if (fp)
283 				fclose(fp);
284 			exit(1);
285 		}
286 		fpin = fdopen(fdin, "r");
287 		if (fpin == NULL) {
288 			fprintf(stderr, "%s: %s\n", fname, strerror(errno));
289 			close(fdin);
290 			if (fp)
291 				fclose(fp);
292 			exit(1);
293 		}
294 
295 	}
296 
297 	ploginit();
298 	eay_init();
299 
300 	if (fpin)
301 		convert_rsa_key(fp, fpin);
302 	else
303 		gen_rsa_key(fp, bits, pubexp);
304 
305 	fclose(fp);
306 	if (fpin)
307 		fclose(fpin);
308 
309 	return 0;
310 }
311