xref: /minix3/crypto/external/bsd/netpgp/dist/src/lib/ssh2pgp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /*-
2ebfedea0SLionel Sambuc  * Copyright (c) 2009 The NetBSD Foundation, Inc.
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
6ebfedea0SLionel Sambuc  * by Alistair Crooks (agc@NetBSD.org)
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
14ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
15ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18ebfedea0SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19ebfedea0SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21ebfedea0SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24ebfedea0SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25ebfedea0SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27ebfedea0SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
28ebfedea0SLionel Sambuc  */
29ebfedea0SLionel Sambuc #include "config.h"
30ebfedea0SLionel Sambuc 
31ebfedea0SLionel Sambuc #ifdef HAVE_SYS_CDEFS_H
32ebfedea0SLionel Sambuc #include <sys/cdefs.h>
33ebfedea0SLionel Sambuc #endif
34ebfedea0SLionel Sambuc 
35ebfedea0SLionel Sambuc #include <sys/types.h>
36ebfedea0SLionel Sambuc #include <sys/stat.h>
37ebfedea0SLionel Sambuc #include <sys/param.h>
38ebfedea0SLionel Sambuc 
39ebfedea0SLionel Sambuc #include <netinet/in.h>
40ebfedea0SLionel Sambuc 
41ebfedea0SLionel Sambuc #include <arpa/inet.h>
42ebfedea0SLionel Sambuc 
43ebfedea0SLionel Sambuc #include <ctype.h>
44ebfedea0SLionel Sambuc #include <inttypes.h>
45ebfedea0SLionel Sambuc #include <stdio.h>
46ebfedea0SLionel Sambuc #include <stdlib.h>
47ebfedea0SLionel Sambuc #include <string.h>
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc #ifdef HAVE_UNISTD_H
50ebfedea0SLionel Sambuc #include <unistd.h>
51ebfedea0SLionel Sambuc #endif
52ebfedea0SLionel Sambuc 
53ebfedea0SLionel Sambuc #ifdef HAVE_LIMITS_H
54ebfedea0SLionel Sambuc #include <limits.h>
55ebfedea0SLionel Sambuc #endif
56ebfedea0SLionel Sambuc 
57ebfedea0SLionel Sambuc #ifdef HAVE_OPENSSL_CAST_H
58ebfedea0SLionel Sambuc #include <openssl/cast.h>
59ebfedea0SLionel Sambuc #endif
60ebfedea0SLionel Sambuc 
61ebfedea0SLionel Sambuc #include <openssl/pem.h>
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc #include "bufgap.h"
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc #include "packet-parse.h"
66ebfedea0SLionel Sambuc #include "netpgpdefs.h"
67ebfedea0SLionel Sambuc #include "netpgpsdk.h"
68ebfedea0SLionel Sambuc #include "crypto.h"
69ebfedea0SLionel Sambuc #include "netpgpdigest.h"
70ebfedea0SLionel Sambuc #include "ssh2pgp.h"
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc /* structure for earching for constant strings */
73ebfedea0SLionel Sambuc typedef struct str_t {
74ebfedea0SLionel Sambuc 	const char	*s;		/* string */
75ebfedea0SLionel Sambuc 	size_t		 len;		/* its length */
76ebfedea0SLionel Sambuc 	int		 type;		/* return type */
77ebfedea0SLionel Sambuc } str_t;
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc #ifndef USE_ARG
80ebfedea0SLionel Sambuc #define USE_ARG(x)	/*LINTED*/(void)&x
81ebfedea0SLionel Sambuc #endif
82ebfedea0SLionel Sambuc 
83ebfedea0SLionel Sambuc static const uint8_t	base64s[] =
84ebfedea0SLionel Sambuc /* 000 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
85ebfedea0SLionel Sambuc /* 016 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
86ebfedea0SLionel Sambuc /* 032 */       "\0\0\0\0\0\0\0\0\0\0\0?\0\0\0@"
87ebfedea0SLionel Sambuc /* 048 */       "56789:;<=>\0\0\0\0\0\0"
88ebfedea0SLionel Sambuc /* 064 */       "\0\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17"
89ebfedea0SLionel Sambuc /* 080 */       "\20\21\22\23\24\25\26\27\30\31\32\0\0\0\0\0"
90ebfedea0SLionel Sambuc /* 096 */       "\0\33\34\35\36\37 !\"#$%&'()"
91ebfedea0SLionel Sambuc /* 112 */       "*+,-./01234\0\0\0\0\0"
92ebfedea0SLionel Sambuc /* 128 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
93ebfedea0SLionel Sambuc /* 144 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
94ebfedea0SLionel Sambuc /* 160 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
95ebfedea0SLionel Sambuc /* 176 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
96ebfedea0SLionel Sambuc /* 192 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
97ebfedea0SLionel Sambuc /* 208 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
98ebfedea0SLionel Sambuc /* 224 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
99ebfedea0SLionel Sambuc /* 240 */       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc /* short function to decode from base64 */
103ebfedea0SLionel Sambuc /* inspired by an ancient copy of b64.c, then rewritten, the bugs are all mine */
104ebfedea0SLionel Sambuc static int
frombase64(char * dst,const char * src,size_t size,int flag)105ebfedea0SLionel Sambuc frombase64(char *dst, const char *src, size_t size, int flag)
106ebfedea0SLionel Sambuc {
107ebfedea0SLionel Sambuc 	uint8_t	out[3];
108ebfedea0SLionel Sambuc 	uint8_t	in[4];
109ebfedea0SLionel Sambuc 	uint8_t	b;
110ebfedea0SLionel Sambuc 	size_t	srcc;
111ebfedea0SLionel Sambuc 	int	dstc;
112ebfedea0SLionel Sambuc 	int	gotc;
113ebfedea0SLionel Sambuc 	int	i;
114ebfedea0SLionel Sambuc 
115ebfedea0SLionel Sambuc 	USE_ARG(flag);
116ebfedea0SLionel Sambuc 	for (dstc = 0, srcc = 0 ; srcc < size; ) {
117ebfedea0SLionel Sambuc 		for (gotc = 0, i = 0; i < 4 && srcc < size; i++) {
118ebfedea0SLionel Sambuc 			for (b = 0x0; srcc < size && b == 0x0 ; ) {
119ebfedea0SLionel Sambuc 				b = base64s[(unsigned)src[srcc++]];
120ebfedea0SLionel Sambuc 			}
121ebfedea0SLionel Sambuc 			if (srcc < size) {
122ebfedea0SLionel Sambuc 				gotc += 1;
123ebfedea0SLionel Sambuc 				if (b) {
124ebfedea0SLionel Sambuc 					in[i] = (uint8_t)(b - 1);
125ebfedea0SLionel Sambuc 				}
126ebfedea0SLionel Sambuc 			} else {
127ebfedea0SLionel Sambuc 				in[i] = 0x0;
128ebfedea0SLionel Sambuc 			}
129ebfedea0SLionel Sambuc 		}
130ebfedea0SLionel Sambuc 		if (gotc) {
131ebfedea0SLionel Sambuc 			out[0] = (uint8_t)((unsigned)in[0] << 2 |
132ebfedea0SLionel Sambuc 						(unsigned)in[1] >> 4);
133ebfedea0SLionel Sambuc 			out[1] = (uint8_t)((unsigned)in[1] << 4 |
134ebfedea0SLionel Sambuc 						(unsigned)in[2] >> 2);
135ebfedea0SLionel Sambuc 			out[2] = (uint8_t)(((in[2] << 6) & 0xc0) | in[3]);
136ebfedea0SLionel Sambuc 			for (i = 0; i < gotc - 1; i++) {
137ebfedea0SLionel Sambuc 				*dst++ = out[i];
138ebfedea0SLionel Sambuc 			}
139ebfedea0SLionel Sambuc 			dstc += gotc - 1;
140ebfedea0SLionel Sambuc 		}
141ebfedea0SLionel Sambuc 	}
142ebfedea0SLionel Sambuc 	return dstc;
143ebfedea0SLionel Sambuc }
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc /* get a bignum from the buffer gap */
146ebfedea0SLionel Sambuc static BIGNUM *
getbignum(bufgap_t * bg,char * buf,const char * header)147ebfedea0SLionel Sambuc getbignum(bufgap_t *bg, char *buf, const char *header)
148ebfedea0SLionel Sambuc {
149ebfedea0SLionel Sambuc 	uint32_t	 len;
150ebfedea0SLionel Sambuc 	BIGNUM		*bignum;
151ebfedea0SLionel Sambuc 
152ebfedea0SLionel Sambuc 	(void) bufgap_getbin(bg, &len, sizeof(len));
153ebfedea0SLionel Sambuc 	len = ntohl(len);
154ebfedea0SLionel Sambuc 	(void) bufgap_seek(bg, sizeof(len), BGFromHere, BGByte);
155ebfedea0SLionel Sambuc 	(void) bufgap_getbin(bg, buf, len);
156ebfedea0SLionel Sambuc 	bignum = BN_bin2bn((const uint8_t *)buf, (int)len, NULL);
157ebfedea0SLionel Sambuc 	if (pgp_get_debug_level(__FILE__)) {
158ebfedea0SLionel Sambuc 		hexdump(stderr, header, (const uint8_t *)(void *)buf, len);
159ebfedea0SLionel Sambuc 	}
160ebfedea0SLionel Sambuc 	(void) bufgap_seek(bg, len, BGFromHere, BGByte);
161ebfedea0SLionel Sambuc 	return bignum;
162ebfedea0SLionel Sambuc }
163ebfedea0SLionel Sambuc 
164ebfedea0SLionel Sambuc #if 0
165ebfedea0SLionel Sambuc static int
166ebfedea0SLionel Sambuc putbignum(bufgap_t *bg, BIGNUM *bignum)
167ebfedea0SLionel Sambuc {
168ebfedea0SLionel Sambuc 	uint32_t	 len;
169ebfedea0SLionel Sambuc 
170ebfedea0SLionel Sambuc 	len = BN_num_bytes(bignum);
171ebfedea0SLionel Sambuc 	(void) bufgap_insert(bg, &len, sizeof(len));
172ebfedea0SLionel Sambuc 	(void) bufgap_insert(bg, buf, len);
173ebfedea0SLionel Sambuc 	bignum = BN_bin2bn((const uint8_t *)buf, (int)len, NULL);
174ebfedea0SLionel Sambuc 	if (pgp_get_debug_level(__FILE__)) {
175ebfedea0SLionel Sambuc 		hexdump(stderr, header, buf, (int)len);
176ebfedea0SLionel Sambuc 	}
177ebfedea0SLionel Sambuc 	(void) bufgap_seek(bg, len, BGFromHere, BGByte);
178ebfedea0SLionel Sambuc 	return bignum;
179ebfedea0SLionel Sambuc }
180ebfedea0SLionel Sambuc #endif
181ebfedea0SLionel Sambuc 
182ebfedea0SLionel Sambuc static str_t	pkatypes[] = {
183ebfedea0SLionel Sambuc 	{	"ssh-rsa",	7,	PGP_PKA_RSA	},
184ebfedea0SLionel Sambuc 	{	"ssh-dss",	7,	PGP_PKA_DSA	},
185ebfedea0SLionel Sambuc 	{	"ssh-dsa",	7,	PGP_PKA_DSA	},
186ebfedea0SLionel Sambuc 	{	NULL,		0,	0		}
187ebfedea0SLionel Sambuc };
188ebfedea0SLionel Sambuc 
189ebfedea0SLionel Sambuc /* look for a string in the given array */
190ebfedea0SLionel Sambuc static int
findstr(str_t * array,const char * name)191ebfedea0SLionel Sambuc findstr(str_t *array, const char *name)
192ebfedea0SLionel Sambuc {
193ebfedea0SLionel Sambuc 	str_t	*sp;
194ebfedea0SLionel Sambuc 
195ebfedea0SLionel Sambuc 	for (sp = array ; sp->s ; sp++) {
196ebfedea0SLionel Sambuc 		if (strncmp(name, sp->s, sp->len) == 0) {
197ebfedea0SLionel Sambuc 			return sp->type;
198ebfedea0SLionel Sambuc 		}
199ebfedea0SLionel Sambuc 	}
200ebfedea0SLionel Sambuc 	return -1;
201ebfedea0SLionel Sambuc }
202ebfedea0SLionel Sambuc 
203ebfedea0SLionel Sambuc /* convert an ssh (host) pubkey to a pgp pubkey */
204ebfedea0SLionel Sambuc int
pgp_ssh2pubkey(pgp_io_t * io,const char * f,pgp_key_t * key,pgp_hash_alg_t hashtype)205ebfedea0SLionel Sambuc pgp_ssh2pubkey(pgp_io_t *io, const char *f, pgp_key_t *key, pgp_hash_alg_t hashtype)
206ebfedea0SLionel Sambuc {
207ebfedea0SLionel Sambuc 	pgp_pubkey_t	*pubkey;
208ebfedea0SLionel Sambuc 	struct stat	 st;
209ebfedea0SLionel Sambuc 	bufgap_t	 bg;
210ebfedea0SLionel Sambuc 	uint32_t	 len;
211ebfedea0SLionel Sambuc 	int64_t		 off;
212ebfedea0SLionel Sambuc 	uint8_t		*userid;
213ebfedea0SLionel Sambuc 	char		 hostname[256];
214ebfedea0SLionel Sambuc 	char		 owner[256];
215ebfedea0SLionel Sambuc 	char		*space;
216ebfedea0SLionel Sambuc 	char	 	*buf;
217ebfedea0SLionel Sambuc 	char	 	*bin;
218ebfedea0SLionel Sambuc 	int		 ok;
219ebfedea0SLionel Sambuc 	int		 cc;
220ebfedea0SLionel Sambuc 
221ebfedea0SLionel Sambuc 	(void) memset(&bg, 0x0, sizeof(bg));
222ebfedea0SLionel Sambuc 	if (!bufgap_open(&bg, f)) {
223ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "pgp_ssh2pubkey: can't open '%s'\n", f);
224ebfedea0SLionel Sambuc 		return 0;
225ebfedea0SLionel Sambuc 	}
226ebfedea0SLionel Sambuc 	(void)stat(f, &st);
227ebfedea0SLionel Sambuc 	if ((buf = calloc(1, (size_t)st.st_size)) == NULL) {
228ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "can't calloc %zu bytes for '%s'\n", (size_t)st.st_size, f);
229ebfedea0SLionel Sambuc 		bufgap_close(&bg);
230ebfedea0SLionel Sambuc 		return 0;
231ebfedea0SLionel Sambuc 	}
232ebfedea0SLionel Sambuc 	if ((bin = calloc(1, (size_t)st.st_size)) == NULL) {
233ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "can't calloc %zu bytes for '%s'\n", (size_t)st.st_size, f);
234ebfedea0SLionel Sambuc 		(void) free(buf);
235ebfedea0SLionel Sambuc 		bufgap_close(&bg);
236ebfedea0SLionel Sambuc 		return 0;
237ebfedea0SLionel Sambuc 	}
238ebfedea0SLionel Sambuc 
239ebfedea0SLionel Sambuc 	/* move past ascii type of key */
240ebfedea0SLionel Sambuc 	while (bufgap_peek(&bg, 0) != ' ') {
241ebfedea0SLionel Sambuc 		bufgap_seek(&bg, 1, BGFromHere, BGByte);
242ebfedea0SLionel Sambuc 	}
243ebfedea0SLionel Sambuc 	bufgap_seek(&bg, 1, BGFromHere, BGByte);
244ebfedea0SLionel Sambuc 	off = bufgap_tell(&bg, BGFromBOF, BGByte);
245ebfedea0SLionel Sambuc 
246ebfedea0SLionel Sambuc 	if (bufgap_size(&bg, BGByte) - off < 10) {
247ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "bad key file '%s'\n", f);
248ebfedea0SLionel Sambuc 		(void) free(buf);
249ebfedea0SLionel Sambuc 		bufgap_close(&bg);
250ebfedea0SLionel Sambuc 		return 0;
251ebfedea0SLionel Sambuc 	}
252ebfedea0SLionel Sambuc 
253ebfedea0SLionel Sambuc 	/* convert from base64 to binary */
254ebfedea0SLionel Sambuc 	cc = bufgap_getbin(&bg, buf, (size_t)bg.bcc);
255ebfedea0SLionel Sambuc 	if ((space = strchr(buf, ' ')) != NULL) {
256ebfedea0SLionel Sambuc 		cc = (int)(space - buf);
257ebfedea0SLionel Sambuc 	}
258ebfedea0SLionel Sambuc 	if (pgp_get_debug_level(__FILE__)) {
259ebfedea0SLionel Sambuc 		hexdump(stderr, NULL, (const uint8_t *)(const void *)buf, (size_t)cc);
260ebfedea0SLionel Sambuc 	}
261ebfedea0SLionel Sambuc 	cc = frombase64(bin, buf, (size_t)cc, 0);
262ebfedea0SLionel Sambuc 	if (pgp_get_debug_level(__FILE__)) {
263ebfedea0SLionel Sambuc 		hexdump(stderr, "decoded base64:", (const uint8_t *)(const void *)bin, (size_t)cc);
264ebfedea0SLionel Sambuc 	}
265ebfedea0SLionel Sambuc 	bufgap_delete(&bg, (uint64_t)bufgap_tell(&bg, BGFromEOF, BGByte));
266ebfedea0SLionel Sambuc 	bufgap_insert(&bg, bin, cc);
267ebfedea0SLionel Sambuc 	bufgap_seek(&bg, off, BGFromBOF, BGByte);
268ebfedea0SLionel Sambuc 
269ebfedea0SLionel Sambuc 	/* get the type of key */
270ebfedea0SLionel Sambuc 	(void) bufgap_getbin(&bg, &len, sizeof(len));
271ebfedea0SLionel Sambuc 	len = ntohl(len);
272ebfedea0SLionel Sambuc 	(void) bufgap_seek(&bg, sizeof(len), BGFromHere, BGByte);
273ebfedea0SLionel Sambuc 	(void) bufgap_getbin(&bg, buf, len);
274ebfedea0SLionel Sambuc 	(void) bufgap_seek(&bg, len, BGFromHere, BGByte);
275ebfedea0SLionel Sambuc 
276ebfedea0SLionel Sambuc 	(void) memset(key, 0x0, sizeof(*key));
277ebfedea0SLionel Sambuc 	pubkey = &key->key.seckey.pubkey;
278ebfedea0SLionel Sambuc 	pubkey->version = PGP_V4;
279*0a6a1f1dSLionel Sambuc 	pubkey->birthtime = 0;
280ebfedea0SLionel Sambuc 	/* get key type */
281ebfedea0SLionel Sambuc 	ok = 1;
282ebfedea0SLionel Sambuc 	switch (pubkey->alg = findstr(pkatypes, buf)) {
283ebfedea0SLionel Sambuc 	case PGP_PKA_RSA:
284ebfedea0SLionel Sambuc 		/* get the 'e' param of the key */
285ebfedea0SLionel Sambuc 		pubkey->key.rsa.e = getbignum(&bg, buf, "RSA E");
286ebfedea0SLionel Sambuc 		/* get the 'n' param of the key */
287ebfedea0SLionel Sambuc 		pubkey->key.rsa.n = getbignum(&bg, buf, "RSA N");
288ebfedea0SLionel Sambuc 		break;
289ebfedea0SLionel Sambuc 	case PGP_PKA_DSA:
290ebfedea0SLionel Sambuc 		/* get the 'p' param of the key */
291ebfedea0SLionel Sambuc 		pubkey->key.dsa.p = getbignum(&bg, buf, "DSA P");
292ebfedea0SLionel Sambuc 		/* get the 'q' param of the key */
293ebfedea0SLionel Sambuc 		pubkey->key.dsa.q = getbignum(&bg, buf, "DSA Q");
294ebfedea0SLionel Sambuc 		/* get the 'g' param of the key */
295ebfedea0SLionel Sambuc 		pubkey->key.dsa.g = getbignum(&bg, buf, "DSA G");
296ebfedea0SLionel Sambuc 		/* get the 'y' param of the key */
297ebfedea0SLionel Sambuc 		pubkey->key.dsa.y = getbignum(&bg, buf, "DSA Y");
298ebfedea0SLionel Sambuc 		break;
299ebfedea0SLionel Sambuc 	default:
300ebfedea0SLionel Sambuc 		(void) fprintf(stderr, "Unrecognised pubkey type %d for '%s'\n",
301ebfedea0SLionel Sambuc 				pubkey->alg, f);
302ebfedea0SLionel Sambuc 		ok = 0;
303ebfedea0SLionel Sambuc 		break;
304ebfedea0SLionel Sambuc 	}
305ebfedea0SLionel Sambuc 
306ebfedea0SLionel Sambuc 	/* check for stragglers */
307ebfedea0SLionel Sambuc 	if (ok && bufgap_tell(&bg, BGFromEOF, BGByte) > 0) {
308ebfedea0SLionel Sambuc 		printf("%"PRIi64" bytes left\n", bufgap_tell(&bg, BGFromEOF, BGByte));
309ebfedea0SLionel Sambuc 		printf("[%s]\n", bufgap_getstr(&bg));
310ebfedea0SLionel Sambuc 		ok = 0;
311ebfedea0SLionel Sambuc 	}
312ebfedea0SLionel Sambuc 	if (ok) {
313ebfedea0SLionel Sambuc 		(void) memset(&userid, 0x0, sizeof(userid));
314ebfedea0SLionel Sambuc 		(void) gethostname(hostname, sizeof(hostname));
315ebfedea0SLionel Sambuc 		if (strlen(space + 1) - 1 == 0) {
316ebfedea0SLionel Sambuc 			(void) snprintf(owner, sizeof(owner), "<root@%s>",
317ebfedea0SLionel Sambuc 					hostname);
318ebfedea0SLionel Sambuc 		} else {
319ebfedea0SLionel Sambuc 			(void) snprintf(owner, sizeof(owner), "<%.*s>",
320ebfedea0SLionel Sambuc 				(int)strlen(space + 1) - 1,
321ebfedea0SLionel Sambuc 				space + 1);
322ebfedea0SLionel Sambuc 		}
323ebfedea0SLionel Sambuc 		(void) pgp_asprintf((char **)(void *)&userid,
324ebfedea0SLionel Sambuc 						"%s (%s) %s",
325ebfedea0SLionel Sambuc 						hostname,
326ebfedea0SLionel Sambuc 						f,
327ebfedea0SLionel Sambuc 						owner);
328ebfedea0SLionel Sambuc 		pgp_keyid(key->sigid, sizeof(key->sigid), pubkey, hashtype);
329ebfedea0SLionel Sambuc 		pgp_add_userid(key, userid);
330ebfedea0SLionel Sambuc 		pgp_fingerprint(&key->sigfingerprint, pubkey, hashtype);
331ebfedea0SLionel Sambuc 		free(userid);
332ebfedea0SLionel Sambuc 		if (pgp_get_debug_level(__FILE__)) {
333ebfedea0SLionel Sambuc 			/*pgp_print_keydata(io, keyring, key, "pub", pubkey, 0);*/
334ebfedea0SLionel Sambuc 			__PGP_USED(io); /* XXX */
335ebfedea0SLionel Sambuc 		}
336ebfedea0SLionel Sambuc 	}
337ebfedea0SLionel Sambuc 	(void) free(bin);
338ebfedea0SLionel Sambuc 	(void) free(buf);
339ebfedea0SLionel Sambuc 	bufgap_close(&bg);
340ebfedea0SLionel Sambuc 	return ok;
341ebfedea0SLionel Sambuc }
342ebfedea0SLionel Sambuc 
343ebfedea0SLionel Sambuc /* convert an ssh (host) seckey to a pgp seckey */
344ebfedea0SLionel Sambuc int
pgp_ssh2seckey(pgp_io_t * io,const char * f,pgp_key_t * key,pgp_pubkey_t * pubkey,pgp_hash_alg_t hashtype)345ebfedea0SLionel Sambuc pgp_ssh2seckey(pgp_io_t *io, const char *f, pgp_key_t *key, pgp_pubkey_t *pubkey, pgp_hash_alg_t hashtype)
346ebfedea0SLionel Sambuc {
347ebfedea0SLionel Sambuc 	pgp_crypt_t	crypted;
348ebfedea0SLionel Sambuc 	pgp_hash_t	hash;
349ebfedea0SLionel Sambuc 	unsigned	done = 0;
350ebfedea0SLionel Sambuc 	unsigned	i = 0;
351ebfedea0SLionel Sambuc 	uint8_t		sesskey[CAST_KEY_LENGTH];
352ebfedea0SLionel Sambuc 	uint8_t		hashed[PGP_SHA1_HASH_SIZE];
353ebfedea0SLionel Sambuc 	BIGNUM		*tmp;
354ebfedea0SLionel Sambuc 
355ebfedea0SLionel Sambuc 	__PGP_USED(io);
356ebfedea0SLionel Sambuc 	/* XXX - check for rsa/dsa */
357ebfedea0SLionel Sambuc 	if (!openssl_read_pem_seckey(f, key, "ssh-rsa", 0)) {
358ebfedea0SLionel Sambuc 		return 0;
359ebfedea0SLionel Sambuc 	}
360ebfedea0SLionel Sambuc 	if (pgp_get_debug_level(__FILE__)) {
361ebfedea0SLionel Sambuc 		/*pgp_print_keydata(io, key, "sec", &key->key.seckey.pubkey, 0);*/
362ebfedea0SLionel Sambuc 		/* XXX */
363ebfedea0SLionel Sambuc 	}
364ebfedea0SLionel Sambuc 	/* let's add some sane defaults */
365ebfedea0SLionel Sambuc 	(void) memcpy(&key->key.seckey.pubkey, pubkey, sizeof(*pubkey));
366ebfedea0SLionel Sambuc 	key->key.seckey.s2k_usage = PGP_S2KU_ENCRYPTED_AND_HASHED;
367ebfedea0SLionel Sambuc 	key->key.seckey.alg = PGP_SA_CAST5;
368ebfedea0SLionel Sambuc 	key->key.seckey.s2k_specifier = PGP_S2KS_SALTED;
369ebfedea0SLionel Sambuc 	key->key.seckey.hash_alg = PGP_HASH_SHA1;
370ebfedea0SLionel Sambuc 	if (key->key.seckey.pubkey.alg == PGP_PKA_RSA) {
371ebfedea0SLionel Sambuc 		/* openssh and openssl have p and q swapped */
372ebfedea0SLionel Sambuc 		tmp = key->key.seckey.key.rsa.p;
373ebfedea0SLionel Sambuc 		key->key.seckey.key.rsa.p = key->key.seckey.key.rsa.q;
374ebfedea0SLionel Sambuc 		key->key.seckey.key.rsa.q = tmp;
375ebfedea0SLionel Sambuc 	}
376ebfedea0SLionel Sambuc 	for (done = 0, i = 0; done < CAST_KEY_LENGTH; i++) {
377ebfedea0SLionel Sambuc 		unsigned 	j;
378ebfedea0SLionel Sambuc 		uint8_t		zero = 0;
379ebfedea0SLionel Sambuc 		int             needed;
380ebfedea0SLionel Sambuc 		int             size;
381ebfedea0SLionel Sambuc 
382ebfedea0SLionel Sambuc 		needed = CAST_KEY_LENGTH - done;
383ebfedea0SLionel Sambuc 		size = MIN(needed, PGP_SHA1_HASH_SIZE);
384ebfedea0SLionel Sambuc 
385ebfedea0SLionel Sambuc 		pgp_hash_any(&hash, key->key.seckey.hash_alg);
386ebfedea0SLionel Sambuc 		if (!hash.init(&hash)) {
387ebfedea0SLionel Sambuc 			(void) fprintf(stderr, "write_seckey_body: bad alloc\n");
388ebfedea0SLionel Sambuc 			return 0;
389ebfedea0SLionel Sambuc 		}
390ebfedea0SLionel Sambuc 
391ebfedea0SLionel Sambuc 		/* preload if iterating  */
392ebfedea0SLionel Sambuc 		for (j = 0; j < i; j++) {
393ebfedea0SLionel Sambuc 			/*
394ebfedea0SLionel Sambuc 			 * Coverity shows a DEADCODE error on this
395ebfedea0SLionel Sambuc 			 * line. This is expected since the hardcoded
396ebfedea0SLionel Sambuc 			 * use of SHA1 and CAST5 means that it will
397ebfedea0SLionel Sambuc 			 * not used. This will change however when
398ebfedea0SLionel Sambuc 			 * other algorithms are supported.
399ebfedea0SLionel Sambuc 			 */
400ebfedea0SLionel Sambuc 			hash.add(&hash, &zero, 1);
401ebfedea0SLionel Sambuc 		}
402ebfedea0SLionel Sambuc 
403ebfedea0SLionel Sambuc 		if (key->key.seckey.s2k_specifier == PGP_S2KS_SALTED) {
404ebfedea0SLionel Sambuc 			hash.add(&hash, key->key.seckey.salt, PGP_SALT_SIZE);
405ebfedea0SLionel Sambuc 		}
406ebfedea0SLionel Sambuc 		hash.finish(&hash, hashed);
407ebfedea0SLionel Sambuc 
408ebfedea0SLionel Sambuc 		/*
409ebfedea0SLionel Sambuc 		 * if more in hash than is needed by session key, use
410ebfedea0SLionel Sambuc 		 * the leftmost octets
411ebfedea0SLionel Sambuc 		 */
412ebfedea0SLionel Sambuc 		(void) memcpy(&sesskey[i * PGP_SHA1_HASH_SIZE],
413ebfedea0SLionel Sambuc 				hashed, (unsigned)size);
414ebfedea0SLionel Sambuc 		done += (unsigned)size;
415ebfedea0SLionel Sambuc 		if (done > CAST_KEY_LENGTH) {
416ebfedea0SLionel Sambuc 			(void) fprintf(stderr,
417ebfedea0SLionel Sambuc 				"write_seckey_body: short add\n");
418ebfedea0SLionel Sambuc 			return 0;
419ebfedea0SLionel Sambuc 		}
420ebfedea0SLionel Sambuc 	}
421ebfedea0SLionel Sambuc 	pgp_crypt_any(&crypted, key->key.seckey.alg);
422ebfedea0SLionel Sambuc 	crypted.set_iv(&crypted, key->key.seckey.iv);
423ebfedea0SLionel Sambuc 	crypted.set_crypt_key(&crypted, sesskey);
424ebfedea0SLionel Sambuc 	pgp_encrypt_init(&crypted);
425ebfedea0SLionel Sambuc 	key->key.seckey.pubkey.alg = PGP_PKA_RSA;
426ebfedea0SLionel Sambuc 	pgp_fingerprint(&key->sigfingerprint, pubkey, hashtype);
427ebfedea0SLionel Sambuc 	pgp_keyid(key->sigid, sizeof(key->sigid), pubkey, hashtype);
428ebfedea0SLionel Sambuc 	return 1;
429ebfedea0SLionel Sambuc }
430ebfedea0SLionel Sambuc 
431ebfedea0SLionel Sambuc /* read a key from the ssh file, and add it to a keyring */
432ebfedea0SLionel Sambuc int
pgp_ssh2_readkeys(pgp_io_t * io,pgp_keyring_t * pubring,pgp_keyring_t * secring,const char * pubfile,const char * secfile,unsigned hashtype)433ebfedea0SLionel Sambuc pgp_ssh2_readkeys(pgp_io_t *io, pgp_keyring_t *pubring,
434ebfedea0SLionel Sambuc 		pgp_keyring_t *secring, const char *pubfile,
435ebfedea0SLionel Sambuc 		const char *secfile, unsigned hashtype)
436ebfedea0SLionel Sambuc {
437ebfedea0SLionel Sambuc 	pgp_key_t		*pubkey;
438ebfedea0SLionel Sambuc 	pgp_key_t		*seckey;
439ebfedea0SLionel Sambuc 	pgp_key_t		 key;
440ebfedea0SLionel Sambuc 
441ebfedea0SLionel Sambuc 	pubkey = NULL;
442ebfedea0SLionel Sambuc 	(void) memset(&key, 0x0, sizeof(key));
443ebfedea0SLionel Sambuc 	if (pubfile) {
444ebfedea0SLionel Sambuc 		if (pgp_get_debug_level(__FILE__)) {
445ebfedea0SLionel Sambuc 			(void) fprintf(io->errs, "pgp_ssh2_readkeys: pubfile '%s'\n", pubfile);
446ebfedea0SLionel Sambuc 		}
447ebfedea0SLionel Sambuc 		if (!pgp_ssh2pubkey(io, pubfile, &key, (pgp_hash_alg_t)hashtype)) {
448ebfedea0SLionel Sambuc 			(void) fprintf(io->errs, "pgp_ssh2_readkeys: can't read pubkeys '%s'\n", pubfile);
449ebfedea0SLionel Sambuc 			return 0;
450ebfedea0SLionel Sambuc 		}
451ebfedea0SLionel Sambuc 		EXPAND_ARRAY(pubring, key);
452ebfedea0SLionel Sambuc 		pubkey = &pubring->keys[pubring->keyc++];
453ebfedea0SLionel Sambuc 		(void) memcpy(pubkey, &key, sizeof(key));
454ebfedea0SLionel Sambuc 		pubkey->type = PGP_PTAG_CT_PUBLIC_KEY;
455ebfedea0SLionel Sambuc 	}
456ebfedea0SLionel Sambuc 	if (secfile) {
457ebfedea0SLionel Sambuc 		if (pgp_get_debug_level(__FILE__)) {
458ebfedea0SLionel Sambuc 			(void) fprintf(io->errs, "pgp_ssh2_readkeys: secfile '%s'\n", secfile);
459ebfedea0SLionel Sambuc 		}
460ebfedea0SLionel Sambuc 		if (pubkey == NULL) {
461ebfedea0SLionel Sambuc 			pubkey = &pubring->keys[0];
462ebfedea0SLionel Sambuc 		}
463ebfedea0SLionel Sambuc 		if (!pgp_ssh2seckey(io, secfile, &key, &pubkey->key.pubkey, (pgp_hash_alg_t)hashtype)) {
464ebfedea0SLionel Sambuc 			(void) fprintf(io->errs, "pgp_ssh2_readkeys: can't read seckeys '%s'\n", secfile);
465ebfedea0SLionel Sambuc 			return 0;
466ebfedea0SLionel Sambuc 		}
467ebfedea0SLionel Sambuc 		EXPAND_ARRAY(secring, key);
468ebfedea0SLionel Sambuc 		seckey = &secring->keys[secring->keyc++];
469ebfedea0SLionel Sambuc 		(void) memcpy(seckey, &key, sizeof(key));
470ebfedea0SLionel Sambuc 		seckey->type = PGP_PTAG_CT_SECRET_KEY;
471ebfedea0SLionel Sambuc 	}
472ebfedea0SLionel Sambuc 	return 1;
473ebfedea0SLionel Sambuc }
474