xref: /freebsd/contrib/bearssl/tools/skey.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty  *
4*0957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty  * the following conditions:
11*0957b409SSimon J. Gerraty  *
12*0957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty  *
15*0957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty  * SOFTWARE.
23*0957b409SSimon J. Gerraty  */
24*0957b409SSimon J. Gerraty 
25*0957b409SSimon J. Gerraty #include <stdio.h>
26*0957b409SSimon J. Gerraty #include <stdlib.h>
27*0957b409SSimon J. Gerraty #include <string.h>
28*0957b409SSimon J. Gerraty #include <stdint.h>
29*0957b409SSimon J. Gerraty #include <errno.h>
30*0957b409SSimon J. Gerraty 
31*0957b409SSimon J. Gerraty #include "brssl.h"
32*0957b409SSimon J. Gerraty #include "bearssl.h"
33*0957b409SSimon J. Gerraty 
34*0957b409SSimon J. Gerraty typedef struct {
35*0957b409SSimon J. Gerraty 	int print_text;
36*0957b409SSimon J. Gerraty 	int print_C;
37*0957b409SSimon J. Gerraty 	const char *rawder;
38*0957b409SSimon J. Gerraty 	const char *rawpem;
39*0957b409SSimon J. Gerraty 	const char *pk8der;
40*0957b409SSimon J. Gerraty 	const char *pk8pem;
41*0957b409SSimon J. Gerraty } outspec;
42*0957b409SSimon J. Gerraty 
43*0957b409SSimon J. Gerraty static void
print_int_text(const char * name,const unsigned char * buf,size_t len)44*0957b409SSimon J. Gerraty print_int_text(const char *name, const unsigned char *buf, size_t len)
45*0957b409SSimon J. Gerraty {
46*0957b409SSimon J. Gerraty 	size_t u;
47*0957b409SSimon J. Gerraty 
48*0957b409SSimon J. Gerraty 	printf("%s = ", name);
49*0957b409SSimon J. Gerraty 	for (u = 0; u < len; u ++) {
50*0957b409SSimon J. Gerraty 		printf("%02X", buf[u]);
51*0957b409SSimon J. Gerraty 	}
52*0957b409SSimon J. Gerraty 	printf("\n");
53*0957b409SSimon J. Gerraty }
54*0957b409SSimon J. Gerraty 
55*0957b409SSimon J. Gerraty static void
print_int_C(const char * name,const unsigned char * buf,size_t len)56*0957b409SSimon J. Gerraty print_int_C(const char *name, const unsigned char *buf, size_t len)
57*0957b409SSimon J. Gerraty {
58*0957b409SSimon J. Gerraty 	size_t u;
59*0957b409SSimon J. Gerraty 
60*0957b409SSimon J. Gerraty 	printf("\nstatic const unsigned char %s[] = {", name);
61*0957b409SSimon J. Gerraty 	for (u = 0; u < len; u ++) {
62*0957b409SSimon J. Gerraty 		if (u != 0) {
63*0957b409SSimon J. Gerraty 			printf(",");
64*0957b409SSimon J. Gerraty 		}
65*0957b409SSimon J. Gerraty 		if (u % 12 == 0) {
66*0957b409SSimon J. Gerraty 			printf("\n\t");
67*0957b409SSimon J. Gerraty 		} else {
68*0957b409SSimon J. Gerraty 			printf(" ");
69*0957b409SSimon J. Gerraty 		}
70*0957b409SSimon J. Gerraty 		printf("0x%02X", buf[u]);
71*0957b409SSimon J. Gerraty 	}
72*0957b409SSimon J. Gerraty 	printf("\n};\n");
73*0957b409SSimon J. Gerraty }
74*0957b409SSimon J. Gerraty 
75*0957b409SSimon J. Gerraty static int
write_to_file(const char * name,const void * data,size_t len)76*0957b409SSimon J. Gerraty write_to_file(const char *name, const void *data, size_t len)
77*0957b409SSimon J. Gerraty {
78*0957b409SSimon J. Gerraty 	FILE *f;
79*0957b409SSimon J. Gerraty 
80*0957b409SSimon J. Gerraty 	f = fopen(name, "wb");
81*0957b409SSimon J. Gerraty 	if (f == NULL) {
82*0957b409SSimon J. Gerraty 		fprintf(stderr,
83*0957b409SSimon J. Gerraty 			"ERROR: cannot open file '%s' for writing\n",
84*0957b409SSimon J. Gerraty 			name);
85*0957b409SSimon J. Gerraty 		return 0;
86*0957b409SSimon J. Gerraty 	}
87*0957b409SSimon J. Gerraty 	if (fwrite(data, 1, len, f) != len) {
88*0957b409SSimon J. Gerraty 		fclose(f);
89*0957b409SSimon J. Gerraty 		fprintf(stderr,
90*0957b409SSimon J. Gerraty 			"ERROR: cannot write to file '%s'\n",
91*0957b409SSimon J. Gerraty 			name);
92*0957b409SSimon J. Gerraty 		return 0;
93*0957b409SSimon J. Gerraty 	}
94*0957b409SSimon J. Gerraty 	fclose(f);
95*0957b409SSimon J. Gerraty 	return 1;
96*0957b409SSimon J. Gerraty }
97*0957b409SSimon J. Gerraty 
98*0957b409SSimon J. Gerraty static int
write_to_pem_file(const char * name,const void * data,size_t len,const char * banner)99*0957b409SSimon J. Gerraty write_to_pem_file(const char *name,
100*0957b409SSimon J. Gerraty 	const void *data, size_t len, const char *banner)
101*0957b409SSimon J. Gerraty {
102*0957b409SSimon J. Gerraty 	void *pem;
103*0957b409SSimon J. Gerraty 	size_t pemlen;
104*0957b409SSimon J. Gerraty 	int r;
105*0957b409SSimon J. Gerraty 
106*0957b409SSimon J. Gerraty 	pemlen = br_pem_encode(NULL, NULL, len, banner, 0);
107*0957b409SSimon J. Gerraty 	pem = xmalloc(pemlen + 1);
108*0957b409SSimon J. Gerraty 	br_pem_encode(pem, data, len, banner, 0);
109*0957b409SSimon J. Gerraty 	r = write_to_file(name, pem, pemlen);
110*0957b409SSimon J. Gerraty 	xfree(pem);
111*0957b409SSimon J. Gerraty 	return r;
112*0957b409SSimon J. Gerraty }
113*0957b409SSimon J. Gerraty 
114*0957b409SSimon J. Gerraty static int
print_rsa(const br_rsa_private_key * sk,outspec * os)115*0957b409SSimon J. Gerraty print_rsa(const br_rsa_private_key *sk, outspec *os)
116*0957b409SSimon J. Gerraty {
117*0957b409SSimon J. Gerraty 	int ret;
118*0957b409SSimon J. Gerraty 	unsigned char *n, *d, *buf;
119*0957b409SSimon J. Gerraty 	uint32_t e;
120*0957b409SSimon J. Gerraty 	size_t nlen, dlen, len;
121*0957b409SSimon J. Gerraty 	br_rsa_compute_modulus cm;
122*0957b409SSimon J. Gerraty 	br_rsa_compute_pubexp ce;
123*0957b409SSimon J. Gerraty 	br_rsa_compute_privexp cd;
124*0957b409SSimon J. Gerraty 	br_rsa_public_key pk;
125*0957b409SSimon J. Gerraty 	unsigned char ebuf[4];
126*0957b409SSimon J. Gerraty 
127*0957b409SSimon J. Gerraty 	n = NULL;
128*0957b409SSimon J. Gerraty 	d = NULL;
129*0957b409SSimon J. Gerraty 	buf = NULL;
130*0957b409SSimon J. Gerraty 	ret = 1;
131*0957b409SSimon J. Gerraty 	if (os->print_text) {
132*0957b409SSimon J. Gerraty 		print_int_text("p ", sk->p, sk->plen);
133*0957b409SSimon J. Gerraty 		print_int_text("q ", sk->q, sk->qlen);
134*0957b409SSimon J. Gerraty 		print_int_text("dp", sk->dp, sk->dplen);
135*0957b409SSimon J. Gerraty 		print_int_text("dq", sk->dq, sk->dqlen);
136*0957b409SSimon J. Gerraty 		print_int_text("iq", sk->iq, sk->iqlen);
137*0957b409SSimon J. Gerraty 	}
138*0957b409SSimon J. Gerraty 	if (os->print_C) {
139*0957b409SSimon J. Gerraty 		print_int_C("RSA_P", sk->p, sk->plen);
140*0957b409SSimon J. Gerraty 		print_int_C("RSA_Q", sk->q, sk->qlen);
141*0957b409SSimon J. Gerraty 		print_int_C("RSA_DP", sk->dp, sk->dplen);
142*0957b409SSimon J. Gerraty 		print_int_C("RSA_DQ", sk->dq, sk->dqlen);
143*0957b409SSimon J. Gerraty 		print_int_C("RSA_IQ", sk->iq, sk->iqlen);
144*0957b409SSimon J. Gerraty 		printf("\nstatic const br_rsa_private_key RSA = {\n");
145*0957b409SSimon J. Gerraty 		printf("\t%lu,\n", (unsigned long)sk->n_bitlen);
146*0957b409SSimon J. Gerraty 		printf("\t(unsigned char *)RSA_P, sizeof RSA_P,\n");
147*0957b409SSimon J. Gerraty 		printf("\t(unsigned char *)RSA_Q, sizeof RSA_Q,\n");
148*0957b409SSimon J. Gerraty 		printf("\t(unsigned char *)RSA_DP, sizeof RSA_DP,\n");
149*0957b409SSimon J. Gerraty 		printf("\t(unsigned char *)RSA_DQ, sizeof RSA_DQ,\n");
150*0957b409SSimon J. Gerraty 		printf("\t(unsigned char *)RSA_IQ, sizeof RSA_IQ\n");
151*0957b409SSimon J. Gerraty 		printf("};\n");
152*0957b409SSimon J. Gerraty 	}
153*0957b409SSimon J. Gerraty 
154*0957b409SSimon J. Gerraty 	if (os->rawder == NULL && os->rawpem == NULL
155*0957b409SSimon J. Gerraty 		&& os->pk8der == NULL && os->pk8pem == NULL)
156*0957b409SSimon J. Gerraty 	{
157*0957b409SSimon J. Gerraty 		return ret;
158*0957b409SSimon J. Gerraty 	}
159*0957b409SSimon J. Gerraty 
160*0957b409SSimon J. Gerraty 	cm = br_rsa_compute_modulus_get_default();
161*0957b409SSimon J. Gerraty 	ce = br_rsa_compute_pubexp_get_default();
162*0957b409SSimon J. Gerraty 	cd = br_rsa_compute_privexp_get_default();
163*0957b409SSimon J. Gerraty 	nlen = cm(NULL, sk);
164*0957b409SSimon J. Gerraty 	if (nlen == 0) {
165*0957b409SSimon J. Gerraty 		goto print_RSA_error;
166*0957b409SSimon J. Gerraty 	}
167*0957b409SSimon J. Gerraty 	n = xmalloc(nlen);
168*0957b409SSimon J. Gerraty 	if (cm(n, sk) != nlen) {
169*0957b409SSimon J. Gerraty 		goto print_RSA_error;
170*0957b409SSimon J. Gerraty 	}
171*0957b409SSimon J. Gerraty 	e = ce(sk);
172*0957b409SSimon J. Gerraty 	if (e == 0) {
173*0957b409SSimon J. Gerraty 		goto print_RSA_error;
174*0957b409SSimon J. Gerraty 	}
175*0957b409SSimon J. Gerraty 	dlen = cd(NULL, sk, e);
176*0957b409SSimon J. Gerraty 	if (dlen == 0) {
177*0957b409SSimon J. Gerraty 		goto print_RSA_error;
178*0957b409SSimon J. Gerraty 	}
179*0957b409SSimon J. Gerraty 	d = xmalloc(dlen);
180*0957b409SSimon J. Gerraty 	if (cd(d, sk, e) != dlen) {
181*0957b409SSimon J. Gerraty 		goto print_RSA_error;
182*0957b409SSimon J. Gerraty 	}
183*0957b409SSimon J. Gerraty 	ebuf[0] = e >> 24;
184*0957b409SSimon J. Gerraty 	ebuf[1] = e >> 16;
185*0957b409SSimon J. Gerraty 	ebuf[2] = e >> 8;
186*0957b409SSimon J. Gerraty 	ebuf[3] = e;
187*0957b409SSimon J. Gerraty 	pk.n = n;
188*0957b409SSimon J. Gerraty 	pk.nlen = nlen;
189*0957b409SSimon J. Gerraty 	pk.e = ebuf;
190*0957b409SSimon J. Gerraty 	pk.elen = sizeof ebuf;
191*0957b409SSimon J. Gerraty 
192*0957b409SSimon J. Gerraty 	if (os->rawder != NULL || os->rawpem != NULL) {
193*0957b409SSimon J. Gerraty 		len = br_encode_rsa_raw_der(NULL, sk, &pk, d, dlen);
194*0957b409SSimon J. Gerraty 		if (len == 0) {
195*0957b409SSimon J. Gerraty 			goto print_RSA_error;
196*0957b409SSimon J. Gerraty 		}
197*0957b409SSimon J. Gerraty 		buf = xmalloc(len);
198*0957b409SSimon J. Gerraty 		if (br_encode_rsa_raw_der(buf, sk, &pk, d, dlen) != len) {
199*0957b409SSimon J. Gerraty 			goto print_RSA_error;
200*0957b409SSimon J. Gerraty 		}
201*0957b409SSimon J. Gerraty 		if (os->rawder != NULL) {
202*0957b409SSimon J. Gerraty 			ret &= write_to_file(os->rawder, buf, len);
203*0957b409SSimon J. Gerraty 		}
204*0957b409SSimon J. Gerraty 		if (os->rawpem != NULL) {
205*0957b409SSimon J. Gerraty 			ret &= write_to_pem_file(os->rawpem,
206*0957b409SSimon J. Gerraty 				buf, len, "RSA PRIVATE KEY");
207*0957b409SSimon J. Gerraty 		}
208*0957b409SSimon J. Gerraty 		xfree(buf);
209*0957b409SSimon J. Gerraty 		buf = NULL;
210*0957b409SSimon J. Gerraty 	}
211*0957b409SSimon J. Gerraty 
212*0957b409SSimon J. Gerraty 	if (os->pk8der != NULL || os->pk8pem != NULL) {
213*0957b409SSimon J. Gerraty 		len = br_encode_rsa_pkcs8_der(NULL, sk, &pk, d, dlen);
214*0957b409SSimon J. Gerraty 		if (len == 0) {
215*0957b409SSimon J. Gerraty 			goto print_RSA_error;
216*0957b409SSimon J. Gerraty 		}
217*0957b409SSimon J. Gerraty 		buf = xmalloc(len);
218*0957b409SSimon J. Gerraty 		if (br_encode_rsa_pkcs8_der(buf, sk, &pk, d, dlen) != len) {
219*0957b409SSimon J. Gerraty 			goto print_RSA_error;
220*0957b409SSimon J. Gerraty 		}
221*0957b409SSimon J. Gerraty 		if (os->pk8der != NULL) {
222*0957b409SSimon J. Gerraty 			ret &= write_to_file(os->pk8der, buf, len);
223*0957b409SSimon J. Gerraty 		}
224*0957b409SSimon J. Gerraty 		if (os->pk8pem != NULL) {
225*0957b409SSimon J. Gerraty 			ret &= write_to_pem_file(os->pk8pem,
226*0957b409SSimon J. Gerraty 				buf, len, "PRIVATE KEY");
227*0957b409SSimon J. Gerraty 		}
228*0957b409SSimon J. Gerraty 		xfree(buf);
229*0957b409SSimon J. Gerraty 		buf = NULL;
230*0957b409SSimon J. Gerraty 	}
231*0957b409SSimon J. Gerraty 
232*0957b409SSimon J. Gerraty print_RSA_exit:
233*0957b409SSimon J. Gerraty 	xfree(n);
234*0957b409SSimon J. Gerraty 	xfree(d);
235*0957b409SSimon J. Gerraty 	xfree(buf);
236*0957b409SSimon J. Gerraty 	return ret;
237*0957b409SSimon J. Gerraty 
238*0957b409SSimon J. Gerraty print_RSA_error:
239*0957b409SSimon J. Gerraty 	fprintf(stderr, "ERROR: cannot encode RSA key\n");
240*0957b409SSimon J. Gerraty 	ret = 0;
241*0957b409SSimon J. Gerraty 	goto print_RSA_exit;
242*0957b409SSimon J. Gerraty }
243*0957b409SSimon J. Gerraty 
244*0957b409SSimon J. Gerraty static int
print_ec(const br_ec_private_key * sk,outspec * os)245*0957b409SSimon J. Gerraty print_ec(const br_ec_private_key *sk, outspec *os)
246*0957b409SSimon J. Gerraty {
247*0957b409SSimon J. Gerraty 	br_ec_public_key pk;
248*0957b409SSimon J. Gerraty 	unsigned kbuf[BR_EC_KBUF_PUB_MAX_SIZE];
249*0957b409SSimon J. Gerraty 	unsigned char *buf;
250*0957b409SSimon J. Gerraty 	size_t len;
251*0957b409SSimon J. Gerraty 	int r;
252*0957b409SSimon J. Gerraty 
253*0957b409SSimon J. Gerraty 	if (os->print_text) {
254*0957b409SSimon J. Gerraty 		print_int_text("x", sk->x, sk->xlen);
255*0957b409SSimon J. Gerraty 	}
256*0957b409SSimon J. Gerraty 	if (os->print_C) {
257*0957b409SSimon J. Gerraty 		print_int_C("EC_X", sk->x, sk->xlen);
258*0957b409SSimon J. Gerraty 		printf("\nstatic const br_ec_private_key EC = {\n");
259*0957b409SSimon J. Gerraty 		printf("\t%d,\n", sk->curve);
260*0957b409SSimon J. Gerraty 		printf("\t(unsigned char *)EC_X, sizeof EC_X\n");
261*0957b409SSimon J. Gerraty 		printf("};\n");
262*0957b409SSimon J. Gerraty 	}
263*0957b409SSimon J. Gerraty 
264*0957b409SSimon J. Gerraty 	if (os->rawder == NULL && os->rawpem == NULL
265*0957b409SSimon J. Gerraty 		&& os->pk8der == NULL && os->pk8pem == NULL)
266*0957b409SSimon J. Gerraty 	{
267*0957b409SSimon J. Gerraty 		return 1;
268*0957b409SSimon J. Gerraty 	}
269*0957b409SSimon J. Gerraty 	if (br_ec_compute_pub(br_ec_get_default(), &pk, kbuf, sk) == 0) {
270*0957b409SSimon J. Gerraty 		fprintf(stderr,
271*0957b409SSimon J. Gerraty 			"ERROR: cannot re-encode (unsupported curve)\n");
272*0957b409SSimon J. Gerraty 		return 0;
273*0957b409SSimon J. Gerraty 	}
274*0957b409SSimon J. Gerraty 
275*0957b409SSimon J. Gerraty 	r = 1;
276*0957b409SSimon J. Gerraty 	if (os->rawder != NULL || os->rawpem != NULL) {
277*0957b409SSimon J. Gerraty 		len = br_encode_ec_raw_der(NULL, sk, &pk);
278*0957b409SSimon J. Gerraty 		if (len == 0) {
279*0957b409SSimon J. Gerraty 			fprintf(stderr, "ERROR: cannot re-encode"
280*0957b409SSimon J. Gerraty 				" (unsupported curve)\n");
281*0957b409SSimon J. Gerraty 			return 0;
282*0957b409SSimon J. Gerraty 		}
283*0957b409SSimon J. Gerraty 		buf = xmalloc(len);
284*0957b409SSimon J. Gerraty 		if (br_encode_ec_raw_der(buf, sk, &pk) != len) {
285*0957b409SSimon J. Gerraty 			fprintf(stderr, "ERROR: re-encode failure\n");
286*0957b409SSimon J. Gerraty 			xfree(buf);
287*0957b409SSimon J. Gerraty 			return 0;
288*0957b409SSimon J. Gerraty 		}
289*0957b409SSimon J. Gerraty 		if (os->rawder != NULL) {
290*0957b409SSimon J. Gerraty 			r &= write_to_file(os->rawder, buf, len);
291*0957b409SSimon J. Gerraty 		}
292*0957b409SSimon J. Gerraty 		if (os->rawpem != NULL) {
293*0957b409SSimon J. Gerraty 			r &= write_to_pem_file(os->rawpem,
294*0957b409SSimon J. Gerraty 				buf, len, "EC PRIVATE KEY");
295*0957b409SSimon J. Gerraty 		}
296*0957b409SSimon J. Gerraty 		xfree(buf);
297*0957b409SSimon J. Gerraty 	}
298*0957b409SSimon J. Gerraty 	if (os->pk8der != NULL || os->pk8pem != NULL) {
299*0957b409SSimon J. Gerraty 		len = br_encode_ec_pkcs8_der(NULL, sk, &pk);
300*0957b409SSimon J. Gerraty 		if (len == 0) {
301*0957b409SSimon J. Gerraty 			fprintf(stderr, "ERROR: cannot re-encode"
302*0957b409SSimon J. Gerraty 				" (unsupported curve)\n");
303*0957b409SSimon J. Gerraty 			return 0;
304*0957b409SSimon J. Gerraty 		}
305*0957b409SSimon J. Gerraty 		buf = xmalloc(len);
306*0957b409SSimon J. Gerraty 		if (br_encode_ec_pkcs8_der(buf, sk, &pk) != len) {
307*0957b409SSimon J. Gerraty 			fprintf(stderr, "ERROR: re-encode failure\n");
308*0957b409SSimon J. Gerraty 			xfree(buf);
309*0957b409SSimon J. Gerraty 			return 0;
310*0957b409SSimon J. Gerraty 		}
311*0957b409SSimon J. Gerraty 		if (os->pk8der != NULL) {
312*0957b409SSimon J. Gerraty 			r &= write_to_file(os->pk8der, buf, len);
313*0957b409SSimon J. Gerraty 		}
314*0957b409SSimon J. Gerraty 		if (os->pk8pem != NULL) {
315*0957b409SSimon J. Gerraty 			r &= write_to_pem_file(os->pk8pem,
316*0957b409SSimon J. Gerraty 				buf, len, "PRIVATE KEY");
317*0957b409SSimon J. Gerraty 		}
318*0957b409SSimon J. Gerraty 		xfree(buf);
319*0957b409SSimon J. Gerraty 	}
320*0957b409SSimon J. Gerraty 	return r;
321*0957b409SSimon J. Gerraty }
322*0957b409SSimon J. Gerraty 
323*0957b409SSimon J. Gerraty static int
parse_rsa_spec(const char * kgen_spec,unsigned * size,uint32_t * pubexp)324*0957b409SSimon J. Gerraty parse_rsa_spec(const char *kgen_spec, unsigned *size, uint32_t *pubexp)
325*0957b409SSimon J. Gerraty {
326*0957b409SSimon J. Gerraty 	const char *p;
327*0957b409SSimon J. Gerraty 	char *end;
328*0957b409SSimon J. Gerraty 	unsigned long ul;
329*0957b409SSimon J. Gerraty 
330*0957b409SSimon J. Gerraty 	p = kgen_spec;
331*0957b409SSimon J. Gerraty 	if (*p != 'r' && *p != 'R') {
332*0957b409SSimon J. Gerraty 		return 0;
333*0957b409SSimon J. Gerraty 	}
334*0957b409SSimon J. Gerraty 	p ++;
335*0957b409SSimon J. Gerraty 	if (*p != 's' && *p != 'S') {
336*0957b409SSimon J. Gerraty 		return 0;
337*0957b409SSimon J. Gerraty 	}
338*0957b409SSimon J. Gerraty 	p ++;
339*0957b409SSimon J. Gerraty 	if (*p != 'a' && *p != 'A') {
340*0957b409SSimon J. Gerraty 		return 0;
341*0957b409SSimon J. Gerraty 	}
342*0957b409SSimon J. Gerraty 	p ++;
343*0957b409SSimon J. Gerraty 	if (*p == 0) {
344*0957b409SSimon J. Gerraty 		*size = 2048;
345*0957b409SSimon J. Gerraty 		*pubexp = 3;
346*0957b409SSimon J. Gerraty 		return 1;
347*0957b409SSimon J. Gerraty 	} else if (*p != ':') {
348*0957b409SSimon J. Gerraty 		return 0;
349*0957b409SSimon J. Gerraty 	}
350*0957b409SSimon J. Gerraty 	p ++;
351*0957b409SSimon J. Gerraty 	ul = strtoul(p, &end, 10);
352*0957b409SSimon J. Gerraty 	if (ul < 512 || ul > 32768) {
353*0957b409SSimon J. Gerraty 		return 0;
354*0957b409SSimon J. Gerraty 	}
355*0957b409SSimon J. Gerraty 	*size = ul;
356*0957b409SSimon J. Gerraty 	p = end;
357*0957b409SSimon J. Gerraty 	if (*p == 0) {
358*0957b409SSimon J. Gerraty 		*pubexp = 3;
359*0957b409SSimon J. Gerraty 		return 1;
360*0957b409SSimon J. Gerraty 	} else if (*p != ':') {
361*0957b409SSimon J. Gerraty 		return 0;
362*0957b409SSimon J. Gerraty 	}
363*0957b409SSimon J. Gerraty 	p ++;
364*0957b409SSimon J. Gerraty 	ul = strtoul(p, &end, 10);
365*0957b409SSimon J. Gerraty 	if ((ul & 1) == 0 || ul == 1 || ((ul >> 30) >> 2) != 0) {
366*0957b409SSimon J. Gerraty 		return 0;
367*0957b409SSimon J. Gerraty 	}
368*0957b409SSimon J. Gerraty 	*pubexp = ul;
369*0957b409SSimon J. Gerraty 	if (*end != 0) {
370*0957b409SSimon J. Gerraty 		return 0;
371*0957b409SSimon J. Gerraty 	}
372*0957b409SSimon J. Gerraty 	return 1;
373*0957b409SSimon J. Gerraty }
374*0957b409SSimon J. Gerraty 
375*0957b409SSimon J. Gerraty static int
keygen_rsa(unsigned size,uint32_t pubexp,outspec * os)376*0957b409SSimon J. Gerraty keygen_rsa(unsigned size, uint32_t pubexp, outspec *os)
377*0957b409SSimon J. Gerraty {
378*0957b409SSimon J. Gerraty 	br_hmac_drbg_context rng;
379*0957b409SSimon J. Gerraty 	br_prng_seeder seeder;
380*0957b409SSimon J. Gerraty 	br_rsa_keygen kg;
381*0957b409SSimon J. Gerraty 	br_rsa_private_key sk;
382*0957b409SSimon J. Gerraty 	unsigned char *kbuf_priv;
383*0957b409SSimon J. Gerraty 	uint32_t r;
384*0957b409SSimon J. Gerraty 
385*0957b409SSimon J. Gerraty 	seeder = br_prng_seeder_system(NULL);
386*0957b409SSimon J. Gerraty 	if (seeder == 0) {
387*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR: no system source of randomness\n");
388*0957b409SSimon J. Gerraty 		return 0;
389*0957b409SSimon J. Gerraty 	}
390*0957b409SSimon J. Gerraty 	br_hmac_drbg_init(&rng, &br_sha256_vtable, NULL, 0);
391*0957b409SSimon J. Gerraty 	if (!seeder(&rng.vtable)) {
392*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR: system source of randomness failed\n");
393*0957b409SSimon J. Gerraty 		return 0;
394*0957b409SSimon J. Gerraty 	}
395*0957b409SSimon J. Gerraty 	kbuf_priv = xmalloc(BR_RSA_KBUF_PRIV_SIZE(size));
396*0957b409SSimon J. Gerraty 	kg = br_rsa_keygen_get_default();
397*0957b409SSimon J. Gerraty 	r = kg(&rng.vtable, &sk, kbuf_priv, NULL, NULL, size, pubexp);
398*0957b409SSimon J. Gerraty 	if (!r) {
399*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR: RSA key pair generation failed\n");
400*0957b409SSimon J. Gerraty 	} else {
401*0957b409SSimon J. Gerraty 		r = print_rsa(&sk, os);
402*0957b409SSimon J. Gerraty 	}
403*0957b409SSimon J. Gerraty 	xfree(kbuf_priv);
404*0957b409SSimon J. Gerraty 	return r;
405*0957b409SSimon J. Gerraty }
406*0957b409SSimon J. Gerraty 
407*0957b409SSimon J. Gerraty static int
parse_ec_spec(const char * kgen_spec,int * curve)408*0957b409SSimon J. Gerraty parse_ec_spec(const char *kgen_spec, int *curve)
409*0957b409SSimon J. Gerraty {
410*0957b409SSimon J. Gerraty 	const char *p;
411*0957b409SSimon J. Gerraty 
412*0957b409SSimon J. Gerraty 	*curve = 0;
413*0957b409SSimon J. Gerraty 	p = kgen_spec;
414*0957b409SSimon J. Gerraty 	if (*p != 'e' && *p != 'E') {
415*0957b409SSimon J. Gerraty 		return 0;
416*0957b409SSimon J. Gerraty 	}
417*0957b409SSimon J. Gerraty 	p ++;
418*0957b409SSimon J. Gerraty 	if (*p != 'c' && *p != 'C') {
419*0957b409SSimon J. Gerraty 		return 0;
420*0957b409SSimon J. Gerraty 	}
421*0957b409SSimon J. Gerraty 	p ++;
422*0957b409SSimon J. Gerraty 	if (*p == 0) {
423*0957b409SSimon J. Gerraty 		*curve = BR_EC_secp256r1;
424*0957b409SSimon J. Gerraty 		return 1;
425*0957b409SSimon J. Gerraty 	}
426*0957b409SSimon J. Gerraty 	if (*p != ':') {
427*0957b409SSimon J. Gerraty 		return 0;
428*0957b409SSimon J. Gerraty 	}
429*0957b409SSimon J. Gerraty 	*curve = get_curve_by_name(p);
430*0957b409SSimon J. Gerraty 	return *curve > 0;
431*0957b409SSimon J. Gerraty }
432*0957b409SSimon J. Gerraty 
433*0957b409SSimon J. Gerraty static int
keygen_ec(int curve,outspec * os)434*0957b409SSimon J. Gerraty keygen_ec(int curve, outspec *os)
435*0957b409SSimon J. Gerraty {
436*0957b409SSimon J. Gerraty 	br_hmac_drbg_context rng;
437*0957b409SSimon J. Gerraty 	br_prng_seeder seeder;
438*0957b409SSimon J. Gerraty 	const br_ec_impl *impl;
439*0957b409SSimon J. Gerraty 	br_ec_private_key sk;
440*0957b409SSimon J. Gerraty 	unsigned char kbuf_priv[BR_EC_KBUF_PRIV_MAX_SIZE];
441*0957b409SSimon J. Gerraty 	size_t len;
442*0957b409SSimon J. Gerraty 
443*0957b409SSimon J. Gerraty 	seeder = br_prng_seeder_system(NULL);
444*0957b409SSimon J. Gerraty 	if (seeder == 0) {
445*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR: no system source of randomness\n");
446*0957b409SSimon J. Gerraty 		return 0;
447*0957b409SSimon J. Gerraty 	}
448*0957b409SSimon J. Gerraty 	br_hmac_drbg_init(&rng, &br_sha256_vtable, NULL, 0);
449*0957b409SSimon J. Gerraty 	if (!seeder(&rng.vtable)) {
450*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR: system source of randomness failed\n");
451*0957b409SSimon J. Gerraty 		return 0;
452*0957b409SSimon J. Gerraty 	}
453*0957b409SSimon J. Gerraty 	impl = br_ec_get_default();
454*0957b409SSimon J. Gerraty 	len = br_ec_keygen(&rng.vtable, impl, &sk, kbuf_priv, curve);
455*0957b409SSimon J. Gerraty 	if (len == 0) {
456*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR: curve is not supported\n");
457*0957b409SSimon J. Gerraty 		return 0;
458*0957b409SSimon J. Gerraty 	}
459*0957b409SSimon J. Gerraty 	return print_ec(&sk, os);
460*0957b409SSimon J. Gerraty }
461*0957b409SSimon J. Gerraty 
462*0957b409SSimon J. Gerraty static int
decode_key(const unsigned char * buf,size_t len,outspec * os)463*0957b409SSimon J. Gerraty decode_key(const unsigned char *buf, size_t len, outspec *os)
464*0957b409SSimon J. Gerraty {
465*0957b409SSimon J. Gerraty 	br_skey_decoder_context dc;
466*0957b409SSimon J. Gerraty 	int err, ret;
467*0957b409SSimon J. Gerraty 
468*0957b409SSimon J. Gerraty 	br_skey_decoder_init(&dc);
469*0957b409SSimon J. Gerraty 	br_skey_decoder_push(&dc, buf, len);
470*0957b409SSimon J. Gerraty 	err = br_skey_decoder_last_error(&dc);
471*0957b409SSimon J. Gerraty 	if (err != 0) {
472*0957b409SSimon J. Gerraty 		const char *errname, *errmsg;
473*0957b409SSimon J. Gerraty 
474*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR (decoding): err=%d\n", err);
475*0957b409SSimon J. Gerraty 		errname = find_error_name(err, &errmsg);
476*0957b409SSimon J. Gerraty 		if (errname != NULL) {
477*0957b409SSimon J. Gerraty 			fprintf(stderr, "  %s: %s\n", errname, errmsg);
478*0957b409SSimon J. Gerraty 		} else {
479*0957b409SSimon J. Gerraty 			fprintf(stderr, "  (unknown)\n");
480*0957b409SSimon J. Gerraty 		}
481*0957b409SSimon J. Gerraty 		return 0;
482*0957b409SSimon J. Gerraty 	}
483*0957b409SSimon J. Gerraty 	ret = 1;
484*0957b409SSimon J. Gerraty 	switch (br_skey_decoder_key_type(&dc)) {
485*0957b409SSimon J. Gerraty 		const br_rsa_private_key *rk;
486*0957b409SSimon J. Gerraty 		const br_ec_private_key *ek;
487*0957b409SSimon J. Gerraty 
488*0957b409SSimon J. Gerraty 	case BR_KEYTYPE_RSA:
489*0957b409SSimon J. Gerraty 		rk = br_skey_decoder_get_rsa(&dc);
490*0957b409SSimon J. Gerraty 		printf("RSA key (%lu bits)\n", (unsigned long)rk->n_bitlen);
491*0957b409SSimon J. Gerraty 		ret = print_rsa(rk, os);
492*0957b409SSimon J. Gerraty 		break;
493*0957b409SSimon J. Gerraty 
494*0957b409SSimon J. Gerraty 	case BR_KEYTYPE_EC:
495*0957b409SSimon J. Gerraty 		ek = br_skey_decoder_get_ec(&dc);
496*0957b409SSimon J. Gerraty 		printf("EC key (curve = %d: %s)\n",
497*0957b409SSimon J. Gerraty 			ek->curve, ec_curve_name(ek->curve));
498*0957b409SSimon J. Gerraty 		ret = print_ec(ek, os);
499*0957b409SSimon J. Gerraty 		break;
500*0957b409SSimon J. Gerraty 
501*0957b409SSimon J. Gerraty 	default:
502*0957b409SSimon J. Gerraty 		fprintf(stderr, "Unknown key type: %d\n",
503*0957b409SSimon J. Gerraty 			br_skey_decoder_key_type(&dc));
504*0957b409SSimon J. Gerraty 		ret = 0;
505*0957b409SSimon J. Gerraty 		break;
506*0957b409SSimon J. Gerraty 	}
507*0957b409SSimon J. Gerraty 
508*0957b409SSimon J. Gerraty 	return ret;
509*0957b409SSimon J. Gerraty }
510*0957b409SSimon J. Gerraty 
511*0957b409SSimon J. Gerraty static void
usage_skey(void)512*0957b409SSimon J. Gerraty usage_skey(void)
513*0957b409SSimon J. Gerraty {
514*0957b409SSimon J. Gerraty 	fprintf(stderr,
515*0957b409SSimon J. Gerraty "usage: brssl skey [ options ] file...\n");
516*0957b409SSimon J. Gerraty 	fprintf(stderr,
517*0957b409SSimon J. Gerraty "options:\n");
518*0957b409SSimon J. Gerraty 	fprintf(stderr,
519*0957b409SSimon J. Gerraty "   -q             suppress verbose messages\n");
520*0957b409SSimon J. Gerraty 	fprintf(stderr,
521*0957b409SSimon J. Gerraty "   -text          print private key details (human-readable)\n");
522*0957b409SSimon J. Gerraty 	fprintf(stderr,
523*0957b409SSimon J. Gerraty "   -C             print private key details (C code)\n");
524*0957b409SSimon J. Gerraty 	fprintf(stderr,
525*0957b409SSimon J. Gerraty "   -rawder file   save private key in 'file' (raw format, DER)\n");
526*0957b409SSimon J. Gerraty 	fprintf(stderr,
527*0957b409SSimon J. Gerraty "   -rawpem file   save private key in 'file' (raw format, PEM)\n");
528*0957b409SSimon J. Gerraty 	fprintf(stderr,
529*0957b409SSimon J. Gerraty "   -pk8der file   save private key in 'file' (PKCS#8 format, DER)\n");
530*0957b409SSimon J. Gerraty 	fprintf(stderr,
531*0957b409SSimon J. Gerraty "   -pk8pem file   save private key in 'file' (PKCS#8 format, PEM)\n");
532*0957b409SSimon J. Gerraty 	fprintf(stderr,
533*0957b409SSimon J. Gerraty "   -gen spec      generate a new key using the provided key specification\n");
534*0957b409SSimon J. Gerraty 	fprintf(stderr,
535*0957b409SSimon J. Gerraty "   -list          list known elliptic curve names\n");
536*0957b409SSimon J. Gerraty 	fprintf(stderr,
537*0957b409SSimon J. Gerraty "Key specification begins with a key type, followed by optional parameters\n");
538*0957b409SSimon J. Gerraty 	fprintf(stderr,
539*0957b409SSimon J. Gerraty "that depend on the key type, separated by colon characters:\n");
540*0957b409SSimon J. Gerraty 	fprintf(stderr,
541*0957b409SSimon J. Gerraty "   rsa[:size[:pubexep]]   RSA key (defaults: size = 2048, pubexp = 3)\n");
542*0957b409SSimon J. Gerraty 	fprintf(stderr,
543*0957b409SSimon J. Gerraty "   ec[:curvename]         EC key (default curve: secp256r1)\n");
544*0957b409SSimon J. Gerraty }
545*0957b409SSimon J. Gerraty 
546*0957b409SSimon J. Gerraty /* see brssl.h */
547*0957b409SSimon J. Gerraty int
do_skey(int argc,char * argv[])548*0957b409SSimon J. Gerraty do_skey(int argc, char *argv[])
549*0957b409SSimon J. Gerraty {
550*0957b409SSimon J. Gerraty 	int retcode;
551*0957b409SSimon J. Gerraty 	int verbose;
552*0957b409SSimon J. Gerraty 	int i, num_files;
553*0957b409SSimon J. Gerraty 	outspec os;
554*0957b409SSimon J. Gerraty 	unsigned char *buf;
555*0957b409SSimon J. Gerraty 	size_t len;
556*0957b409SSimon J. Gerraty 	pem_object *pos;
557*0957b409SSimon J. Gerraty 	const char *kgen_spec;
558*0957b409SSimon J. Gerraty 
559*0957b409SSimon J. Gerraty 	retcode = 0;
560*0957b409SSimon J. Gerraty 	verbose = 1;
561*0957b409SSimon J. Gerraty 	os.print_text = 0;
562*0957b409SSimon J. Gerraty 	os.print_C = 0;
563*0957b409SSimon J. Gerraty 	os.rawder = NULL;
564*0957b409SSimon J. Gerraty 	os.rawpem = NULL;
565*0957b409SSimon J. Gerraty 	os.pk8der = NULL;
566*0957b409SSimon J. Gerraty 	os.pk8pem = NULL;
567*0957b409SSimon J. Gerraty 	num_files = 0;
568*0957b409SSimon J. Gerraty 	buf = NULL;
569*0957b409SSimon J. Gerraty 	pos = NULL;
570*0957b409SSimon J. Gerraty 	kgen_spec = NULL;
571*0957b409SSimon J. Gerraty 	for (i = 0; i < argc; i ++) {
572*0957b409SSimon J. Gerraty 		const char *arg;
573*0957b409SSimon J. Gerraty 
574*0957b409SSimon J. Gerraty 		arg = argv[i];
575*0957b409SSimon J. Gerraty 		if (arg[0] != '-') {
576*0957b409SSimon J. Gerraty 			num_files ++;
577*0957b409SSimon J. Gerraty 			continue;
578*0957b409SSimon J. Gerraty 		}
579*0957b409SSimon J. Gerraty 		argv[i] = NULL;
580*0957b409SSimon J. Gerraty 		if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
581*0957b409SSimon J. Gerraty 			verbose = 1;
582*0957b409SSimon J. Gerraty 		} else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
583*0957b409SSimon J. Gerraty 			verbose = 0;
584*0957b409SSimon J. Gerraty 		} else if (eqstr(arg, "-text")) {
585*0957b409SSimon J. Gerraty 			os.print_text = 1;
586*0957b409SSimon J. Gerraty 		} else if (eqstr(arg, "-C")) {
587*0957b409SSimon J. Gerraty 			os.print_C = 1;
588*0957b409SSimon J. Gerraty 		} else if (eqstr(arg, "-rawder")) {
589*0957b409SSimon J. Gerraty 			if (++ i >= argc) {
590*0957b409SSimon J. Gerraty 				fprintf(stderr,
591*0957b409SSimon J. Gerraty 					"ERROR: no argument for '-rawder'\n");
592*0957b409SSimon J. Gerraty 				usage_skey();
593*0957b409SSimon J. Gerraty 				goto skey_exit_error;
594*0957b409SSimon J. Gerraty 			}
595*0957b409SSimon J. Gerraty 			if (os.rawder != NULL) {
596*0957b409SSimon J. Gerraty 				fprintf(stderr,
597*0957b409SSimon J. Gerraty 					"ERROR: multiple '-rawder' options\n");
598*0957b409SSimon J. Gerraty 				usage_skey();
599*0957b409SSimon J. Gerraty 				goto skey_exit_error;
600*0957b409SSimon J. Gerraty 			}
601*0957b409SSimon J. Gerraty 			os.rawder = argv[i];
602*0957b409SSimon J. Gerraty 			argv[i] = NULL;
603*0957b409SSimon J. Gerraty 		} else if (eqstr(arg, "-rawpem")) {
604*0957b409SSimon J. Gerraty 			if (++ i >= argc) {
605*0957b409SSimon J. Gerraty 				fprintf(stderr,
606*0957b409SSimon J. Gerraty 					"ERROR: no argument for '-rawpem'\n");
607*0957b409SSimon J. Gerraty 				usage_skey();
608*0957b409SSimon J. Gerraty 				goto skey_exit_error;
609*0957b409SSimon J. Gerraty 			}
610*0957b409SSimon J. Gerraty 			if (os.rawpem != NULL) {
611*0957b409SSimon J. Gerraty 				fprintf(stderr,
612*0957b409SSimon J. Gerraty 					"ERROR: multiple '-rawpem' options\n");
613*0957b409SSimon J. Gerraty 				usage_skey();
614*0957b409SSimon J. Gerraty 				goto skey_exit_error;
615*0957b409SSimon J. Gerraty 			}
616*0957b409SSimon J. Gerraty 			os.rawpem = argv[i];
617*0957b409SSimon J. Gerraty 			argv[i] = NULL;
618*0957b409SSimon J. Gerraty 		} else if (eqstr(arg, "-pk8der")) {
619*0957b409SSimon J. Gerraty 			if (++ i >= argc) {
620*0957b409SSimon J. Gerraty 				fprintf(stderr,
621*0957b409SSimon J. Gerraty 					"ERROR: no argument for '-pk8der'\n");
622*0957b409SSimon J. Gerraty 				usage_skey();
623*0957b409SSimon J. Gerraty 				goto skey_exit_error;
624*0957b409SSimon J. Gerraty 			}
625*0957b409SSimon J. Gerraty 			if (os.pk8der != NULL) {
626*0957b409SSimon J. Gerraty 				fprintf(stderr,
627*0957b409SSimon J. Gerraty 					"ERROR: multiple '-pk8der' options\n");
628*0957b409SSimon J. Gerraty 				usage_skey();
629*0957b409SSimon J. Gerraty 				goto skey_exit_error;
630*0957b409SSimon J. Gerraty 			}
631*0957b409SSimon J. Gerraty 			os.pk8der = argv[i];
632*0957b409SSimon J. Gerraty 			argv[i] = NULL;
633*0957b409SSimon J. Gerraty 		} else if (eqstr(arg, "-pk8pem")) {
634*0957b409SSimon J. Gerraty 			if (++ i >= argc) {
635*0957b409SSimon J. Gerraty 				fprintf(stderr,
636*0957b409SSimon J. Gerraty 					"ERROR: no argument for '-pk8pem'\n");
637*0957b409SSimon J. Gerraty 				usage_skey();
638*0957b409SSimon J. Gerraty 				goto skey_exit_error;
639*0957b409SSimon J. Gerraty 			}
640*0957b409SSimon J. Gerraty 			if (os.pk8pem != NULL) {
641*0957b409SSimon J. Gerraty 				fprintf(stderr,
642*0957b409SSimon J. Gerraty 					"ERROR: multiple '-pk8pem' options\n");
643*0957b409SSimon J. Gerraty 				usage_skey();
644*0957b409SSimon J. Gerraty 				goto skey_exit_error;
645*0957b409SSimon J. Gerraty 			}
646*0957b409SSimon J. Gerraty 			os.pk8pem = argv[i];
647*0957b409SSimon J. Gerraty 			argv[i] = NULL;
648*0957b409SSimon J. Gerraty 		} else if (eqstr(arg, "-gen")) {
649*0957b409SSimon J. Gerraty 			if (++ i >= argc) {
650*0957b409SSimon J. Gerraty 				fprintf(stderr,
651*0957b409SSimon J. Gerraty 					"ERROR: no argument for '-gen'\n");
652*0957b409SSimon J. Gerraty 				usage_skey();
653*0957b409SSimon J. Gerraty 				goto skey_exit_error;
654*0957b409SSimon J. Gerraty 			}
655*0957b409SSimon J. Gerraty 			if (kgen_spec != NULL) {
656*0957b409SSimon J. Gerraty 				fprintf(stderr,
657*0957b409SSimon J. Gerraty 					"ERROR: multiple '-gen' options\n");
658*0957b409SSimon J. Gerraty 				usage_skey();
659*0957b409SSimon J. Gerraty 				goto skey_exit_error;
660*0957b409SSimon J. Gerraty 			}
661*0957b409SSimon J. Gerraty 			kgen_spec = argv[i];
662*0957b409SSimon J. Gerraty 			argv[i] = NULL;
663*0957b409SSimon J. Gerraty 		} else if (eqstr(arg, "-list")) {
664*0957b409SSimon J. Gerraty 			list_curves();
665*0957b409SSimon J. Gerraty 			goto skey_exit;
666*0957b409SSimon J. Gerraty 		} else {
667*0957b409SSimon J. Gerraty 			fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
668*0957b409SSimon J. Gerraty 			usage_skey();
669*0957b409SSimon J. Gerraty 			goto skey_exit_error;
670*0957b409SSimon J. Gerraty 		}
671*0957b409SSimon J. Gerraty 	}
672*0957b409SSimon J. Gerraty 	if (kgen_spec != NULL) {
673*0957b409SSimon J. Gerraty 		unsigned rsa_size;
674*0957b409SSimon J. Gerraty 		uint32_t rsa_pubexp;
675*0957b409SSimon J. Gerraty 		int curve;
676*0957b409SSimon J. Gerraty 
677*0957b409SSimon J. Gerraty 		if (num_files != 0) {
678*0957b409SSimon J. Gerraty 			fprintf(stderr,
679*0957b409SSimon J. Gerraty 				"ERROR: key files provided while generating\n");
680*0957b409SSimon J. Gerraty 			usage_skey();
681*0957b409SSimon J. Gerraty 			goto skey_exit_error;
682*0957b409SSimon J. Gerraty 		}
683*0957b409SSimon J. Gerraty 
684*0957b409SSimon J. Gerraty 		if (parse_rsa_spec(kgen_spec, &rsa_size, &rsa_pubexp)) {
685*0957b409SSimon J. Gerraty 			if (!keygen_rsa(rsa_size, rsa_pubexp, &os)) {
686*0957b409SSimon J. Gerraty 				goto skey_exit_error;
687*0957b409SSimon J. Gerraty 			}
688*0957b409SSimon J. Gerraty 		} else if (parse_ec_spec(kgen_spec, &curve)) {
689*0957b409SSimon J. Gerraty 			if (!keygen_ec(curve, &os)) {
690*0957b409SSimon J. Gerraty 				goto skey_exit_error;
691*0957b409SSimon J. Gerraty 			}
692*0957b409SSimon J. Gerraty 		} else {
693*0957b409SSimon J. Gerraty 			fprintf(stderr,
694*0957b409SSimon J. Gerraty 				"ERROR: unknown key specification: '%s'\n",
695*0957b409SSimon J. Gerraty 				kgen_spec);
696*0957b409SSimon J. Gerraty 			usage_skey();
697*0957b409SSimon J. Gerraty 			goto skey_exit_error;
698*0957b409SSimon J. Gerraty 		}
699*0957b409SSimon J. Gerraty 	} else if (num_files == 0) {
700*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR: no private key provided\n");
701*0957b409SSimon J. Gerraty 		usage_skey();
702*0957b409SSimon J. Gerraty 		goto skey_exit_error;
703*0957b409SSimon J. Gerraty 	}
704*0957b409SSimon J. Gerraty 
705*0957b409SSimon J. Gerraty 	for (i = 0; i < argc; i ++) {
706*0957b409SSimon J. Gerraty 		const char *fname;
707*0957b409SSimon J. Gerraty 
708*0957b409SSimon J. Gerraty 		fname = argv[i];
709*0957b409SSimon J. Gerraty 		if (fname == NULL) {
710*0957b409SSimon J. Gerraty 			continue;
711*0957b409SSimon J. Gerraty 		}
712*0957b409SSimon J. Gerraty 		buf = read_file(fname, &len);
713*0957b409SSimon J. Gerraty 		if (buf == NULL) {
714*0957b409SSimon J. Gerraty 			goto skey_exit_error;
715*0957b409SSimon J. Gerraty 		}
716*0957b409SSimon J. Gerraty 		if (looks_like_DER(buf, len)) {
717*0957b409SSimon J. Gerraty 			if (verbose) {
718*0957b409SSimon J. Gerraty 				fprintf(stderr, "File '%s': ASN.1/DER object\n",
719*0957b409SSimon J. Gerraty 					fname);
720*0957b409SSimon J. Gerraty 			}
721*0957b409SSimon J. Gerraty 			if (!decode_key(buf, len, &os)) {
722*0957b409SSimon J. Gerraty 				goto skey_exit_error;
723*0957b409SSimon J. Gerraty 			}
724*0957b409SSimon J. Gerraty 		} else {
725*0957b409SSimon J. Gerraty 			size_t u, num;
726*0957b409SSimon J. Gerraty 
727*0957b409SSimon J. Gerraty 			if (verbose) {
728*0957b409SSimon J. Gerraty 				fprintf(stderr, "File '%s': decoding as PEM\n",
729*0957b409SSimon J. Gerraty 					fname);
730*0957b409SSimon J. Gerraty 			}
731*0957b409SSimon J. Gerraty 			pos = decode_pem(buf, len, &num);
732*0957b409SSimon J. Gerraty 			if (pos == NULL) {
733*0957b409SSimon J. Gerraty 				goto skey_exit_error;
734*0957b409SSimon J. Gerraty 			}
735*0957b409SSimon J. Gerraty 			for (u = 0; pos[u].name; u ++) {
736*0957b409SSimon J. Gerraty 				const char *name;
737*0957b409SSimon J. Gerraty 
738*0957b409SSimon J. Gerraty 				name = pos[u].name;
739*0957b409SSimon J. Gerraty 				if (eqstr(name, "RSA PRIVATE KEY")
740*0957b409SSimon J. Gerraty 					|| eqstr(name, "EC PRIVATE KEY")
741*0957b409SSimon J. Gerraty 					|| eqstr(name, "PRIVATE KEY"))
742*0957b409SSimon J. Gerraty 				{
743*0957b409SSimon J. Gerraty 					if (!decode_key(pos[u].data,
744*0957b409SSimon J. Gerraty 						pos[u].data_len, &os))
745*0957b409SSimon J. Gerraty 					{
746*0957b409SSimon J. Gerraty 						goto skey_exit_error;
747*0957b409SSimon J. Gerraty 					}
748*0957b409SSimon J. Gerraty 				} else {
749*0957b409SSimon J. Gerraty 					if (verbose) {
750*0957b409SSimon J. Gerraty 						fprintf(stderr,
751*0957b409SSimon J. Gerraty 							"(skipping '%s')\n",
752*0957b409SSimon J. Gerraty 							name);
753*0957b409SSimon J. Gerraty 					}
754*0957b409SSimon J. Gerraty 				}
755*0957b409SSimon J. Gerraty 			}
756*0957b409SSimon J. Gerraty 			for (u = 0; pos[u].name; u ++) {
757*0957b409SSimon J. Gerraty 				free_pem_object_contents(&pos[u]);
758*0957b409SSimon J. Gerraty 			}
759*0957b409SSimon J. Gerraty 			xfree(pos);
760*0957b409SSimon J. Gerraty 			pos = NULL;
761*0957b409SSimon J. Gerraty 		}
762*0957b409SSimon J. Gerraty 		xfree(buf);
763*0957b409SSimon J. Gerraty 		buf = NULL;
764*0957b409SSimon J. Gerraty 	}
765*0957b409SSimon J. Gerraty 
766*0957b409SSimon J. Gerraty 	/*
767*0957b409SSimon J. Gerraty 	 * Release allocated structures.
768*0957b409SSimon J. Gerraty 	 */
769*0957b409SSimon J. Gerraty skey_exit:
770*0957b409SSimon J. Gerraty 	xfree(buf);
771*0957b409SSimon J. Gerraty 	if (pos != NULL) {
772*0957b409SSimon J. Gerraty 		size_t u;
773*0957b409SSimon J. Gerraty 
774*0957b409SSimon J. Gerraty 		for (u = 0; pos[u].name; u ++) {
775*0957b409SSimon J. Gerraty 			free_pem_object_contents(&pos[u]);
776*0957b409SSimon J. Gerraty 		}
777*0957b409SSimon J. Gerraty 		xfree(pos);
778*0957b409SSimon J. Gerraty 	}
779*0957b409SSimon J. Gerraty 	return retcode;
780*0957b409SSimon J. Gerraty 
781*0957b409SSimon J. Gerraty skey_exit_error:
782*0957b409SSimon J. Gerraty 	retcode = -1;
783*0957b409SSimon J. Gerraty 	goto skey_exit;
784*0957b409SSimon J. Gerraty }
785