xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c (revision 62a8debe1dc62962e18a1c918def78666141273b)
1 /*-
2  * Copyright (c) 2009 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Alistair Crooks (agc@NetBSD.org)
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "config.h"
30 
31 #ifdef HAVE_SYS_CDEFS_H
32 #include <sys/cdefs.h>
33 #endif
34 
35 #if defined(__NetBSD__)
36 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
37 __RCSID("$NetBSD: netpgp.c,v 1.42 2010/03/05 16:30:05 agc Exp $");
38 #endif
39 
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/param.h>
43 #include <sys/mman.h>
44 
45 #ifdef HAVE_SYS_RESOURCE_H
46 #include <sys/resource.h>
47 #endif
48 
49 #ifdef HAVE_FCNTL_H
50 #include <fcntl.h>
51 #endif
52 
53 #include <errno.h>
54 #include <regex.h>
55 #include <stdarg.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
59 
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
63 
64 #include <errno.h>
65 
66 #ifdef HAVE_LIMITS_H
67 #include <limits.h>
68 #endif
69 
70 #include <netpgp.h>
71 
72 #include "packet.h"
73 #include "packet-parse.h"
74 #include "keyring.h"
75 #include "errors.h"
76 #include "packet-show.h"
77 #include "create.h"
78 #include "netpgpsdk.h"
79 #include "memory.h"
80 #include "validate.h"
81 #include "readerwriter.h"
82 #include "netpgpdefs.h"
83 #include "crypto.h"
84 #include "ops-ssh.h"
85 #include "defs.h"
86 
87 /* read any gpg config file */
88 static int
89 conffile(netpgp_t *netpgp, char *homedir, char *userid, size_t length)
90 {
91 	regmatch_t	 matchv[10];
92 	regex_t		 keyre;
93 	char		 buf[BUFSIZ];
94 	FILE		*fp;
95 
96 	__OPS_USED(netpgp);
97 	(void) snprintf(buf, sizeof(buf), "%s/gpg.conf", homedir);
98 	if ((fp = fopen(buf, "r")) == NULL) {
99 		return 0;
100 	}
101 	(void) memset(&keyre, 0x0, sizeof(keyre));
102 	(void) regcomp(&keyre, "^[ \t]*default-key[ \t]+([0-9a-zA-F]+)",
103 		REG_EXTENDED);
104 	while (fgets(buf, sizeof(buf), fp) != NULL) {
105 		if (regexec(&keyre, buf, 10, matchv, 0) == 0) {
106 			(void) memcpy(userid, &buf[(int)matchv[1].rm_so],
107 				MIN((unsigned)(matchv[1].rm_eo -
108 						matchv[1].rm_so), length));
109 			if (netpgp->passfp == NULL) {
110 				(void) fprintf(stderr,
111 				"netpgp: default key set to \"%.*s\"\n",
112 				(int)(matchv[1].rm_eo - matchv[1].rm_so),
113 				&buf[(int)matchv[1].rm_so]);
114 			}
115 		}
116 	}
117 	(void) fclose(fp);
118 	return 1;
119 }
120 
121 /* small function to pretty print an 8-character raw userid */
122 static char    *
123 userid_to_id(const uint8_t *userid, char *id)
124 {
125 	static const char *hexes = "0123456789abcdef";
126 	int		   i;
127 
128 	for (i = 0; i < 8 ; i++) {
129 		id[i * 2] = hexes[(unsigned)(userid[i] & 0xf0) >> 4];
130 		id[(i * 2) + 1] = hexes[userid[i] & 0xf];
131 	}
132 	id[8 * 2] = 0x0;
133 	return id;
134 }
135 
136 /* print out the successful signature information */
137 static void
138 resultp(__ops_io_t *io,
139 	const char *f,
140 	__ops_validation_t *res,
141 	__ops_keyring_t *ring)
142 {
143 	const __ops_key_t	*pubkey;
144 	unsigned		 from;
145 	unsigned		 i;
146 	time_t			 t;
147 	char			 id[MAX_ID_LENGTH + 1];
148 
149 	for (i = 0; i < res->validc; i++) {
150 		(void) fprintf(io->res,
151 			"Good signature for %s made %s",
152 			(f) ? f : "<stdin>",
153 			ctime(&res->valid_sigs[i].birthtime));
154 		if (res->duration > 0) {
155 			t = res->birthtime + res->duration;
156 			(void) fprintf(io->res, "Valid until %s", ctime(&t));
157 		}
158 		(void) fprintf(io->res,
159 			"using %s key %s\n",
160 			__ops_show_pka(res->valid_sigs[i].key_alg),
161 			userid_to_id(res->valid_sigs[i].signer_id, id));
162 		from = 0;
163 		pubkey = __ops_getkeybyid(io, ring,
164 			(const uint8_t *) res->valid_sigs[i].signer_id,
165 			&from);
166 		__ops_print_keydata(io, pubkey, "pub", &pubkey->key.pubkey);
167 	}
168 }
169 
170 /* check there's enough space in the arrays */
171 static int
172 size_arrays(netpgp_t *netpgp, unsigned needed)
173 {
174 	char	**temp;
175 
176 	if (netpgp->size == 0) {
177 		/* only get here first time around */
178 		netpgp->size = needed;
179 		if ((netpgp->name = calloc(sizeof(char *), needed)) == NULL) {
180 			(void) fprintf(stderr, "size_arrays: bad alloc\n");
181 			return 0;
182 		}
183 		if ((netpgp->value = calloc(sizeof(char *), needed)) == NULL) {
184 			free(netpgp->name);
185 			(void) fprintf(stderr, "size_arrays: bad alloc\n");
186 			return 0;
187 		}
188 	} else if (netpgp->c == netpgp->size) {
189 		/* only uses 'needed' when filled array */
190 		netpgp->size += needed;
191 		temp = realloc(netpgp->name, sizeof(char *) * needed);
192 		if (temp == NULL) {
193 			(void) fprintf(stderr, "size_arrays: bad alloc\n");
194 			return 0;
195 		}
196 		netpgp->name = temp;
197 		temp = realloc(netpgp->value, sizeof(char *) * needed);
198 		if (temp == NULL) {
199 			(void) fprintf(stderr, "size_arrays: bad alloc\n");
200 			return 0;
201 		}
202 		netpgp->value = temp;
203 	}
204 	return 1;
205 }
206 
207 /* find the name in the array */
208 static int
209 findvar(netpgp_t *netpgp, const char *name)
210 {
211 	unsigned	i;
212 
213 	for (i = 0 ; i < netpgp->c && strcmp(netpgp->name[i], name) != 0; i++) {
214 	}
215 	return (i == netpgp->c) ? -1 : (int)i;
216 }
217 
218 /* read a keyring and return it */
219 static void *
220 readkeyring(netpgp_t *netpgp, const char *name)
221 {
222 	__ops_keyring_t	*keyring;
223 	const unsigned	 noarmor = 0;
224 	char		 f[MAXPATHLEN];
225 	char		*filename;
226 	char		*homedir;
227 
228 	homedir = netpgp_getvar(netpgp, "homedir");
229 	if ((filename = netpgp_getvar(netpgp, name)) == NULL) {
230 		(void) snprintf(f, sizeof(f), "%s/%s.gpg", homedir, name);
231 		filename = f;
232 	}
233 	if ((keyring = calloc(1, sizeof(*keyring))) == NULL) {
234 		(void) fprintf(stderr, "readkeyring: bad alloc\n");
235 		return NULL;
236 	}
237 	if (!__ops_keyring_fileread(keyring, noarmor, filename)) {
238 		free(keyring);
239 		(void) fprintf(stderr, "Can't read %s %s\n", name, filename);
240 		return NULL;
241 	}
242 	netpgp_setvar(netpgp, name, filename);
243 	return keyring;
244 }
245 
246 /* read keys from ssh key files */
247 static int
248 readsshkeys(netpgp_t *netpgp, char *homedir)
249 {
250 	__ops_keyring_t	*pubring;
251 	__ops_keyring_t	*secring;
252 	char		 f[MAXPATHLEN];
253 	char		*filename;
254 
255 	if ((filename = netpgp_getvar(netpgp, "sshkeyfile")) == NULL) {
256 		(void) snprintf(f, sizeof(f), "%s/.ssh/is_rsa.pub", homedir);
257 		filename = f;
258 	}
259 	if ((pubring = calloc(1, sizeof(*pubring))) == NULL) {
260 		(void) fprintf(stderr, "readsshkeys: bad alloc\n");
261 		return 0;
262 	}
263 	if (!__ops_ssh2_readkeys(netpgp->io, pubring, NULL, filename, NULL)) {
264 		free(pubring);
265 		(void) fprintf(stderr, "readsshkeys: can't read %s\n",
266 				filename);
267 		return 0;
268 	}
269 	if (netpgp->pubring == NULL) {
270 		netpgp->pubring = pubring;
271 	} else {
272 		__ops_append_keyring(netpgp->pubring, pubring);
273 	}
274 	netpgp_setvar(netpgp, "sshpubfile", filename);
275 	/* try to take the ".pub" off the end */
276 	if (filename == f) {
277 		f[strlen(f) - 4] = 0x0;
278 	} else {
279 		(void) snprintf(f, sizeof(f), "%.*s",
280 				(int)strlen(filename) - 4, filename);
281 		filename = f;
282 	}
283 	if ((secring = calloc(1, sizeof(*secring))) == NULL) {
284 		(void) fprintf(stderr, "readsshkeys: bad alloc\n");
285 		return 0;
286 	}
287 	if (__ops_ssh2_readkeys(netpgp->io, pubring, secring, NULL, filename)) {
288 		netpgp->secring = secring;
289 		netpgp_setvar(netpgp, "sshsecfile", filename);
290 	} else {
291 		(void) fprintf(stderr, "readsshkeys: can't read sec %s (%d)\n",
292 				filename, errno);
293 	}
294 	return 1;
295 }
296 
297 /* set ssh uid to first one in ring */
298 static void
299 set_ssh_userid(__ops_keyring_t *pubring, char *id, size_t len, int last)
300 {
301 	uint8_t	*src;
302 	int	 i;
303 	int	 n;
304 
305 	(void) memset(id, 0x0, len);
306 	src = pubring->keys[(last) ? pubring->keyc - 1 : 0].key_id;
307 	for (i = 0, n = 0 ; i < OPS_KEY_ID_SIZE ; i += 2) {
308 		n += snprintf(&id[n], len - n, "%02x%02x", src[i], src[i + 1]);
309 	}
310 	id[n] = 0x0;
311 }
312 
313 /* find the time - in a specific %Y-%m-%d format - using a regexp */
314 static int
315 grabdate(char *s, int64_t *t)
316 {
317 	static regex_t	r;
318 	static int	compiled;
319 	regmatch_t	matches[10];
320 	struct tm	tm;
321 
322 	if (!compiled) {
323 		compiled = 1;
324 		(void) regcomp(&r, "([0-9][0-9][0-9][0-9])[-/]([0-9][0-9])[-/]([0-9][0-9])", REG_EXTENDED);
325 	}
326 	if (regexec(&r, s, 10, matches, 0) == 0) {
327 		(void) memset(&tm, 0x0, sizeof(tm));
328 		tm.tm_year = (int)strtol(&s[(int)matches[1].rm_so], NULL, 10);
329 		tm.tm_mon = (int)strtol(&s[(int)matches[2].rm_so], NULL, 10) - 1;
330 		tm.tm_mday = (int)strtol(&s[(int)matches[3].rm_so], NULL, 10);
331 		*t = mktime(&tm);
332 		return 1;
333 	}
334 	return 0;
335 }
336 
337 /* get expiration in seconds */
338 static uint64_t
339 get_duration(char *s)
340 {
341 	uint64_t	 now;
342 	int64_t	 	 t;
343 	char		*mult;
344 
345 	if (s == NULL) {
346 		return 0;
347 	}
348 	now = strtoull(s, NULL, 10);
349 	if ((mult = strchr("hdwmy", s[strlen(s) - 1])) != NULL) {
350 		switch(*mult) {
351 		case 'h':
352 			return now * 60 * 60;
353 		case 'd':
354 			return now * 60 * 60 * 24;
355 		case 'w':
356 			return now * 60 * 60 * 24 * 7;
357 		case 'm':
358 			return now * 60 * 60 * 24 * 31;
359 		case 'y':
360 			return now * 60 * 60 * 24 * 365;
361 		}
362 	}
363 	if (grabdate(s, &t)) {
364 		return t;
365 	}
366 	return (uint64_t)strtoll(s, NULL, 10);
367 }
368 
369 /* get birthtime in seconds */
370 static int64_t
371 get_birthtime(char *s)
372 {
373 	int64_t	t;
374 
375 	if (s == NULL) {
376 		return time(NULL);
377 	}
378 	if (grabdate(s, &t)) {
379 		return t;
380 	}
381 	return (uint64_t)strtoll(s, NULL, 10);
382 }
383 
384 /***************************************************************************/
385 /* exported functions start here */
386 /***************************************************************************/
387 
388 /* initialise a netpgp_t structure */
389 int
390 netpgp_init(netpgp_t *netpgp)
391 {
392 	__ops_io_t	*io;
393 	char		 id[MAX_ID_LENGTH];
394 	char		*homedir;
395 	char		*userid;
396 	char		*stream;
397 	char		*passfd;
398 	char		*results;
399 	int		 coredumps;
400 	int		 last;
401 
402 #ifdef HAVE_SYS_RESOURCE_H
403 	struct rlimit	limit;
404 
405 	coredumps = netpgp_getvar(netpgp, "coredumps") != NULL;
406 	if (!coredumps) {
407 		(void) memset(&limit, 0x0, sizeof(limit));
408 		if (setrlimit(RLIMIT_CORE, &limit) != 0) {
409 			(void) fprintf(stderr,
410 			"netpgp: warning - can't turn off core dumps\n");
411 			coredumps = 1;
412 		}
413 	}
414 #else
415 	coredumps = 1;
416 #endif
417 	if ((io = calloc(1, sizeof(*io))) == NULL) {
418 		(void) fprintf(stderr, "netpgp_init: bad alloc\n");
419 		return 0;
420 	}
421 	io->outs = stdout;
422 	if ((stream = netpgp_getvar(netpgp, "stdout")) != NULL &&
423 	    strcmp(stream, "stderr") == 0) {
424 		io->outs = stderr;
425 	}
426 	io->errs = stderr;
427 	if ((stream = netpgp_getvar(netpgp, "stderr")) != NULL &&
428 	    strcmp(stream, "stdout") == 0) {
429 		io->errs = stdout;
430 	}
431 	if ((results = netpgp_getvar(netpgp, "results")) == NULL) {
432 		io->res = io->errs;
433 	} else if ((io->res = fopen(results, "w")) == NULL) {
434 		(void) fprintf(io->errs, "Can't open results %s for writing\n",
435 			results);
436 		free(io);
437 		return 0;
438 	}
439 	netpgp->io = io;
440 	if ((passfd = netpgp_getvar(netpgp, "pass-fd")) != NULL &&
441 	    (netpgp->passfp = fdopen(atoi(passfd), "r")) == NULL) {
442 		(void) fprintf(io->errs, "Can't open fd %s for reading\n",
443 			passfd);
444 		return 0;
445 	}
446 	if (coredumps) {
447 		(void) fprintf(io->errs,
448 			"netpgp: warning: core dumps enabled\n");
449 	}
450 	if ((homedir = netpgp_getvar(netpgp, "homedir")) == NULL) {
451 		(void) fprintf(io->errs, "netpgp: bad homedir\n");
452 		return 0;
453 	}
454 	/* read from either gpg files or ssh keys */
455 	if (netpgp_getvar(netpgp, "ssh keys") == NULL) {
456 		if ((userid = netpgp_getvar(netpgp, "userid")) == NULL) {
457 			(void) memset(id, 0x0, sizeof(id));
458 			(void) conffile(netpgp, homedir, id, sizeof(id));
459 			if (id[0] != 0x0) {
460 				netpgp_setvar(netpgp, "userid", userid = id);
461 			}
462 		}
463 		if (userid == NULL) {
464 			if (netpgp_getvar(netpgp, "need userid") != NULL) {
465 				(void) fprintf(io->errs,
466 						"Cannot find user id\n");
467 				return 0;
468 			}
469 		} else {
470 			(void) netpgp_setvar(netpgp, "userid", userid);
471 		}
472 		netpgp->pubring = readkeyring(netpgp, "pubring");
473 		if (netpgp->pubring == NULL) {
474 			(void) fprintf(io->errs, "Can't read pub keyring\n");
475 			return 0;
476 		}
477 		netpgp->secring = readkeyring(netpgp, "secring");
478 		if (netpgp->secring == NULL) {
479 			(void) fprintf(io->errs, "Can't read sec keyring\n");
480 			return 0;
481 		}
482 	} else {
483 		last = (netpgp->pubring != NULL);
484 		if (!readsshkeys(netpgp, homedir)) {
485 			(void) fprintf(io->errs, "Can't read ssh pub key\n");
486 			return 0;
487 		}
488 		if ((userid = netpgp_getvar(netpgp, "userid")) == NULL) {
489 			set_ssh_userid(netpgp->pubring, id, sizeof(id), last);
490 			netpgp_setvar(netpgp, "userid", userid = id);
491 		}
492 		if (userid == NULL) {
493 			if (netpgp_getvar(netpgp, "need userid") != NULL) {
494 				(void) fprintf(io->errs,
495 						"Cannot find user id\n");
496 				return 0;
497 			}
498 		} else {
499 			(void) netpgp_setvar(netpgp, "userid", userid);
500 		}
501 	}
502 	return 1;
503 }
504 
505 /* finish off with the netpgp_t struct */
506 int
507 netpgp_end(netpgp_t *netpgp)
508 {
509 	unsigned	i;
510 
511 	for (i = 0 ; i < netpgp->c ; i++) {
512 		if (netpgp->name[i] != NULL) {
513 			free(netpgp->name[i]);
514 		}
515 		if (netpgp->value[i] != NULL) {
516 			free(netpgp->value[i]);
517 		}
518 	}
519 	if (netpgp->name != NULL) {
520 		free(netpgp->name);
521 	}
522 	if (netpgp->value != NULL) {
523 		free(netpgp->value);
524 	}
525 	if (netpgp->pubring != NULL) {
526 		__ops_keyring_free(netpgp->pubring);
527 	}
528 	if (netpgp->secring != NULL) {
529 		__ops_keyring_free(netpgp->secring);
530 	}
531 	free(netpgp->io);
532 	return 1;
533 }
534 
535 /* list the keys in a keyring */
536 int
537 netpgp_list_keys(netpgp_t *netpgp)
538 {
539 	return __ops_keyring_list(netpgp->io, netpgp->pubring);
540 }
541 
542 DEFINE_ARRAY(strings_t, char *);
543 
544 #ifndef HKP_VERSION
545 #define HKP_VERSION	1
546 #endif
547 
548 /* find and list some keys in a keyring */
549 int
550 netpgp_match_keys(netpgp_t *netpgp, char *name, const char *fmt, void *vp)
551 {
552 	const __ops_key_t	*key;
553 	unsigned		 k;
554 	strings_t		 pubs;
555 	FILE			*fp = (FILE *)vp;
556 
557 	if (name[0] == '0' && name[1] == 'x') {
558 		name += 2;
559 	}
560 	(void) memset(&pubs, 0x0, sizeof(pubs));
561 	k = 0;
562 	do {
563 		key = __ops_getnextkeybyname(netpgp->io, netpgp->pubring,
564 						name, &k);
565 		if (key != NULL) {
566 			ALLOC(char *, pubs.v, pubs.size, pubs.c, 10, 10,
567 					"netpgp_match_keys", return 0);
568 			if (strcmp(fmt, "mr") == 0) {
569 				__ops_hkp_sprint_keydata(
570 						key, &pubs.v[pubs.c],
571 						&key->key.pubkey);
572 			} else {
573 				__ops_sprint_keydata(
574 						key, &pubs.v[pubs.c],
575 						"pub",
576 						&key->key.pubkey);
577 			}
578 			pubs.c += 1;
579 			k += 1;
580 		}
581 	} while (key != NULL);
582 	if (strcmp(fmt, "mr") == 0) {
583 		(void) fprintf(fp, "info:%d:%d\n", HKP_VERSION, pubs.c);
584 	} else {
585 		(void) fprintf(fp, "%d key%s found\n", pubs.c,
586 			(pubs.c == 1) ? "" : "s");
587 	}
588 	for (k = 0 ; k < pubs.c ; k++) {
589 		(void) fprintf(fp, "%s", pubs.v[k]);
590 		free(pubs.v[k]);
591 	}
592 	free(pubs.v);
593 	return pubs.c;
594 }
595 
596 /* find and list some public keys in a keyring */
597 int
598 netpgp_match_pubkeys(netpgp_t *netpgp, char *name, void *vp)
599 {
600 	const __ops_key_t	*key;
601 	unsigned		 k;
602 	strings_t		 pubs;
603 	FILE			*fp = (FILE *)vp;
604 
605 	(void) memset(&pubs, 0x0, sizeof(pubs));
606 	do {
607 		key = __ops_getnextkeybyname(netpgp->io, netpgp->pubring,
608 						name, &k);
609 		if (key != NULL) {
610 			char	out[1024 * 64];
611 
612 			ALLOC(char *, pubs.v, pubs.size, pubs.c, 10, 10,
613 					"netpgp_match_pubkeys", return 0);
614 			(void) __ops_sprint_pubkey(key, out, sizeof(out));
615 			pubs.v[pubs.c++] = netpgp_strdup(out);
616 			k += 1;
617 		}
618 	} while (key != NULL);
619 	(void) fprintf(fp, "info:%d:%d\n", HKP_VERSION, pubs.c);
620 	for (k = 0 ; k < pubs.c ; k++) {
621 		(void) fprintf(fp, "%s", pubs.v[k]);
622 		free(pubs.v[k]);
623 	}
624 	free(pubs.v);
625 	return pubs.c;
626 }
627 
628 /* find a key in a keyring */
629 int
630 netpgp_find_key(netpgp_t *netpgp, char *id)
631 {
632 	__ops_io_t	*io;
633 
634 	io = netpgp->io;
635 	if (id == NULL) {
636 		(void) fprintf(io->errs, "NULL id to search for\n");
637 		return 0;
638 	}
639 	return __ops_getkeybyname(netpgp->io, netpgp->pubring, id) != NULL;
640 }
641 
642 /* get a key in a keyring */
643 char *
644 netpgp_get_key(netpgp_t *netpgp, const char *name, const char *fmt)
645 {
646 	const __ops_key_t	*key;
647 	__ops_io_t		*io;
648 	char			*newkey;
649 
650 	io = netpgp->io;
651 	if (name == NULL) {
652 		name = netpgp_getvar(netpgp, "userid");
653 	} else if (name[0] == '0' && name[1] == 'x') {
654 		name += 2;
655 	}
656 	key = __ops_getkeybyname(netpgp->io, netpgp->pubring, name);
657 	if (key == NULL) {
658 		(void) fprintf(io->errs, "Can't find key '%s'\n", name);
659 		return NULL;
660 	}
661 	if (strcmp(fmt, "mr") == 0) {
662 		return (__ops_hkp_sprint_keydata(key, &newkey,
663 				&key->key.pubkey) > 0) ? newkey : NULL;
664 	}
665 	return (__ops_sprint_keydata(key, &newkey, "pub",
666 				&key->key.pubkey) > 0) ? newkey : NULL;
667 }
668 
669 /* export a given key */
670 char *
671 netpgp_export_key(netpgp_t *netpgp, char *name)
672 {
673 	const __ops_key_t	*key;
674 	__ops_io_t		*io;
675 
676 	io = netpgp->io;
677 	if (name == NULL) {
678 		name = netpgp_getvar(netpgp, "userid");
679 	} else if (name[0] == '0' && name[1] == 'x') {
680 		name += 2;
681 	}
682 	key = __ops_getkeybyname(io, netpgp->pubring, name);
683 	if (key == NULL) {
684 		(void) fprintf(io->errs,
685 			"Cannot find own key \"%s\" in keyring\n", name);
686 		return 0;
687 	}
688 	return __ops_export_key(io, key, NULL);
689 }
690 
691 /* import a key into our keyring */
692 int
693 netpgp_import_key(netpgp_t *netpgp, char *f)
694 {
695 	const unsigned	noarmor = 0;
696 	const unsigned	armor = 1;
697 	__ops_io_t	*io;
698 	int		done;
699 
700 	io = netpgp->io;
701 	if ((done = __ops_keyring_fileread(netpgp->pubring, noarmor, f)) == 0) {
702 		done = __ops_keyring_fileread(netpgp->pubring, armor, f);
703 	}
704 	if (!done) {
705 		(void) fprintf(io->errs, "Cannot import key from file %s\n",
706 				f);
707 		return 0;
708 	}
709 	return __ops_keyring_list(io, netpgp->pubring);
710 }
711 
712 /* generate a new key */
713 int
714 netpgp_generate_key(netpgp_t *netpgp, char *id, int numbits)
715 {
716 	__ops_key_t		*keypair;
717 	__ops_userid_t		 uid;
718 	__ops_output_t		*create;
719 	const unsigned		 noarmor = 0;
720 	__ops_io_t		*io;
721 	char			*ringfile;
722 	int             	 fd;
723 
724 	(void) memset(&uid, 0x0, sizeof(uid));
725 	io = netpgp->io;
726 	/* generate a new key for 'id' */
727 	uid.userid = (uint8_t *) id;
728 	keypair = __ops_rsa_new_selfsign_key(numbits, 65537UL, &uid);
729 	if (keypair == NULL) {
730 		(void) fprintf(io->errs, "Cannot generate key\n");
731 		return 0;
732 	}
733 	/* write public key, and try to re-read it */
734 	ringfile = netpgp_getvar(netpgp, "pubring");
735 	fd = __ops_setup_file_append(&create, ringfile);
736 	if (!__ops_write_xfer_pubkey(create, keypair, noarmor)) {
737 		(void) fprintf(io->errs, "Cannot write pubkey\n");
738 		return 0;
739 	}
740 	__ops_teardown_file_write(create, fd);
741 	__ops_keyring_free(netpgp->pubring);
742 	if (!__ops_keyring_fileread(netpgp->pubring, noarmor, ringfile)) {
743 		(void) fprintf(io->errs, "Cannot read pubring %s\n", ringfile);
744 		return 0;
745 	}
746 	/* write secret key, and try to re-read it */
747 	ringfile = netpgp_getvar(netpgp, "sec ring file");
748 	fd = __ops_setup_file_append(&create, ringfile);
749 	if (!__ops_write_xfer_seckey(create, keypair, NULL, 0, noarmor)) {
750 		(void) fprintf(io->errs, "Cannot write seckey\n");
751 		return 0;
752 	}
753 	__ops_teardown_file_write(create, fd);
754 	__ops_keyring_free(netpgp->secring);
755 	if (!__ops_keyring_fileread(netpgp->secring, noarmor, ringfile)) {
756 		(void) fprintf(io->errs, "Can't read secring %s\n", ringfile);
757 		return 0;
758 	}
759 	__ops_keydata_free(keypair);
760 	return 1;
761 }
762 
763 /* encrypt a file */
764 int
765 netpgp_encrypt_file(netpgp_t *netpgp,
766 			const char *userid,
767 			const char *f,
768 			char *out,
769 			int armored)
770 {
771 	const __ops_key_t	*keypair;
772 	const unsigned		 overwrite = 1;
773 	const char		*suffix;
774 	__ops_io_t		*io;
775 	char			 outname[MAXPATHLEN];
776 
777 	io = netpgp->io;
778 	if (f == NULL) {
779 		(void) fprintf(io->errs,
780 			"netpgp_encrypt_file: no filename specified\n");
781 		return 0;
782 	}
783 	if (userid == NULL) {
784 		userid = netpgp_getvar(netpgp, "userid");
785 	}
786 	suffix = (armored) ? ".asc" : ".gpg";
787 	keypair = __ops_getkeybyname(io, netpgp->pubring, userid);
788 	if (keypair == NULL) {
789 		(void) fprintf(io->errs, "Userid '%s' not found in keyring\n",
790 					userid);
791 		return 0;
792 	}
793 	if (out == NULL) {
794 		(void) snprintf(outname, sizeof(outname), "%s%s", f, suffix);
795 		out = outname;
796 	}
797 	return (int)__ops_encrypt_file(io, f, out, keypair, (unsigned)armored,
798 					overwrite);
799 }
800 
801 #define ARMOR_HEAD	"-----BEGIN PGP MESSAGE-----\r\n"
802 
803 /* decrypt a file */
804 int
805 netpgp_decrypt_file(netpgp_t *netpgp, const char *f, char *out, int armored)
806 {
807 	const unsigned	 overwrite = 1;
808 	__ops_io_t	*io;
809 	unsigned	 realarmour;
810 	FILE		*fp;
811 	char		 buf[BUFSIZ];
812 
813 	io = netpgp->io;
814 	if (f == NULL) {
815 		(void) fprintf(io->errs,
816 			"netpgp_decrypt_file: no filename specified\n");
817 		return 0;
818 	}
819 	realarmour = (unsigned)armored;
820 	if ((fp = fopen(f, "r")) == NULL) {
821 		(void) fprintf(io->errs,
822 			"netpgp_decrypt_file: can't open '%s'\n", f);
823 		return 0;
824 	}
825 	if (fgets(buf, sizeof(buf), fp) == NULL) {
826 		realarmour = 0;
827 	} else {
828 		realarmour = (strcmp(buf, ARMOR_HEAD) == 0);
829 	}
830 	(void) fclose(fp);
831 	return __ops_decrypt_file(netpgp->io, f, out, netpgp->secring,
832 				netpgp->pubring,
833 				(unsigned)realarmour, overwrite,
834 				netpgp->passfp, get_passphrase_cb);
835 }
836 
837 /* sign a file */
838 int
839 netpgp_sign_file(netpgp_t *netpgp,
840 		const char *userid,
841 		const char *f,
842 		char *out,
843 		int armored,
844 		int cleartext,
845 		int detached)
846 {
847 	const __ops_key_t	*keypair;
848 	const __ops_key_t	*pubkey;
849 	__ops_seckey_t		*seckey;
850 	const unsigned		 overwrite = 1;
851 	__ops_io_t		*io;
852 	char			*hashalg;
853 	int			 ret;
854 
855 	io = netpgp->io;
856 	if (f == NULL) {
857 		(void) fprintf(io->errs,
858 			"netpgp_sign_file: no filename specified\n");
859 		return 0;
860 	}
861 	if (userid == NULL) {
862 		userid = netpgp_getvar(netpgp, "userid");
863 	}
864 	/* get key with which to sign */
865 	keypair = __ops_getkeybyname(io, netpgp->secring, userid);
866 	if (keypair == NULL) {
867 		(void) fprintf(io->errs, "Userid '%s' not found in secring\n",
868 				userid);
869 		return 0;
870 	}
871 	ret = 1;
872 	do {
873 		if (netpgp->passfp == NULL) {
874 			/* print out the user id */
875 			pubkey = __ops_getkeybyname(io, netpgp->pubring, userid);
876 			if (pubkey == NULL) {
877 				(void) fprintf(io->errs,
878 					"netpgp: warning - using pubkey from secring\n");
879 				__ops_print_keydata(io, keypair, "pub",
880 					&keypair->key.seckey.pubkey);
881 			} else {
882 				__ops_print_keydata(io, pubkey, "pub", &pubkey->key.pubkey);
883 			}
884 		}
885 		if (netpgp_getvar(netpgp, "ssh keys") == NULL) {
886 			/* now decrypt key */
887 			seckey = __ops_decrypt_seckey(keypair, netpgp->passfp);
888 			if (seckey == NULL) {
889 				(void) fprintf(io->errs, "Bad passphrase\n");
890 			}
891 		} else {
892 			__ops_keyring_t	*secring;
893 
894 			secring = netpgp->secring;
895 			seckey = &secring->keys[0].key.seckey;
896 		}
897 	} while (seckey == NULL);
898 	/* sign file */
899 	hashalg = netpgp_getvar(netpgp, "hash");
900 	if (detached) {
901 		ret = __ops_sign_detached(io, f, out, seckey, hashalg,
902 				get_birthtime(netpgp_getvar(netpgp, "birthtime")),
903 				get_duration(netpgp_getvar(netpgp, "duration")));
904 	} else {
905 		ret = __ops_sign_file(io, f, out, seckey, hashalg,
906 				get_birthtime(netpgp_getvar(netpgp, "birthtime")),
907 				get_duration(netpgp_getvar(netpgp, "duration")),
908 				(unsigned)armored, (unsigned)cleartext,
909 				overwrite);
910 	}
911 	__ops_forget(seckey, sizeof(*seckey));
912 	return ret;
913 }
914 
915 /* verify a file */
916 int
917 netpgp_verify_file(netpgp_t *netpgp, const char *in, const char *out, int armored)
918 {
919 	__ops_validation_t	 result;
920 	__ops_io_t		*io;
921 
922 	(void) memset(&result, 0x0, sizeof(result));
923 	io = netpgp->io;
924 	if (in == NULL) {
925 		(void) fprintf(io->errs,
926 			"netpgp_verify_file: no filename specified\n");
927 		return 0;
928 	}
929 	if (__ops_validate_file(io, &result, in, out, armored,
930 						netpgp->pubring)) {
931 		resultp(io, in, &result, netpgp->pubring);
932 		return 1;
933 	}
934 	if (result.validc + result.invalidc + result.unknownc == 0) {
935 		(void) fprintf(io->errs,
936 		"\"%s\": No signatures found - is this a signed file?\n",
937 			in);
938 	} else if (result.invalidc == 0 && result.unknownc == 0) {
939 		(void) fprintf(io->errs,
940 			"\"%s\": file verification failure: invalid signature time\n", in);
941 	} else {
942 		(void) fprintf(io->errs,
943 "\"%s\": verification failure: %u invalid signatures, %u unknown signatures\n",
944 			in, result.invalidc, result.unknownc);
945 	}
946 	return 0;
947 }
948 
949 /* sign some memory */
950 int
951 netpgp_sign_memory(netpgp_t *netpgp,
952 		const char *userid,
953 		char *mem,
954 		size_t size,
955 		char *out,
956 		size_t outsize,
957 		const unsigned armored,
958 		const unsigned cleartext)
959 {
960 	const __ops_key_t	*keypair;
961 	const __ops_key_t	*pubkey;
962 	__ops_seckey_t		*seckey;
963 	__ops_memory_t		*signedmem;
964 	__ops_io_t		*io;
965 	char			*hashalg;
966 	int			 ret;
967 
968 	io = netpgp->io;
969 	if (mem == NULL) {
970 		(void) fprintf(io->errs,
971 			"netpgp_sign_memory: no memory to sign\n");
972 		return 0;
973 	}
974 	if (userid == NULL) {
975 		userid = netpgp_getvar(netpgp, "userid");
976 	}
977 	/* get key with which to sign */
978 	keypair = __ops_getkeybyname(io, netpgp->secring, userid);
979 	if (keypair == NULL) {
980 		(void) fprintf(io->errs, "Userid '%s' not found in keyring\n",
981 				userid);
982 		return 0;
983 	}
984 	ret = 1;
985 	do {
986 		if (netpgp->passfp == NULL) {
987 			/* print out the user id */
988 			pubkey = __ops_getkeybyname(io, netpgp->pubring, userid);
989 			if (pubkey == NULL) {
990 				(void) fprintf(io->errs,
991 					"netpgp: warning - using pubkey from secring\n");
992 				__ops_print_keydata(io, keypair, "pub",
993 					&keypair->key.seckey.pubkey);
994 			} else {
995 				__ops_print_keydata(io, pubkey, "pub", &pubkey->key.pubkey);
996 			}
997 		}
998 		/* now decrypt key */
999 		seckey = __ops_decrypt_seckey(keypair, netpgp->passfp);
1000 		if (seckey == NULL) {
1001 			(void) fprintf(io->errs, "Bad passphrase\n");
1002 		}
1003 	} while (seckey == NULL);
1004 	/* sign file */
1005 	(void) memset(out, 0x0, outsize);
1006 	hashalg = netpgp_getvar(netpgp, "hash");
1007 	signedmem = __ops_sign_buf(io, mem, size, seckey,
1008 				get_birthtime(netpgp_getvar(netpgp, "birthtime")),
1009 				get_duration(netpgp_getvar(netpgp, "duration")),
1010 				hashalg, armored, cleartext);
1011 	if (signedmem) {
1012 		size_t	m;
1013 
1014 		m = MIN(__ops_mem_len(signedmem), outsize);
1015 		(void) memcpy(out, __ops_mem_data(signedmem), m);
1016 		__ops_memory_free(signedmem);
1017 		ret = (int)m;
1018 	} else {
1019 		ret = 0;
1020 	}
1021 	__ops_forget(seckey, sizeof(*seckey));
1022 	return ret;
1023 }
1024 
1025 /* verify memory */
1026 int
1027 netpgp_verify_memory(netpgp_t *netpgp, const void *in, const size_t size,
1028 			void *out, size_t outsize, const int armored)
1029 {
1030 	__ops_validation_t	 result;
1031 	__ops_memory_t		*signedmem;
1032 	__ops_memory_t		*cat;
1033 	__ops_io_t		*io;
1034 	size_t			 m;
1035 	int			 ret;
1036 
1037 	(void) memset(&result, 0x0, sizeof(result));
1038 	io = netpgp->io;
1039 	if (in == NULL) {
1040 		(void) fprintf(io->errs,
1041 			"netpgp_verify_memory: no memory to verify\n");
1042 		return 0;
1043 	}
1044 	signedmem = __ops_memory_new();
1045 	__ops_memory_add(signedmem, in, size);
1046 	if (out) {
1047 		cat = __ops_memory_new();
1048 	}
1049 	ret = __ops_validate_mem(io, &result, signedmem,
1050 				(out) ? &cat : NULL,
1051 				armored, netpgp->pubring);
1052 	__ops_memory_free(signedmem);
1053 	if (ret) {
1054 		resultp(io, "<stdin>", &result, netpgp->pubring);
1055 		if (out) {
1056 			m = MIN(__ops_mem_len(cat), outsize);
1057 			(void) memcpy(out, __ops_mem_data(cat), m);
1058 			__ops_memory_free(cat);
1059 		} else {
1060 			m = 1;
1061 		}
1062 		return (int)m;
1063 	}
1064 	if (result.validc + result.invalidc + result.unknownc == 0) {
1065 		(void) fprintf(io->errs,
1066 		"No signatures found - is this memory signed?\n");
1067 	} else if (result.invalidc == 0 && result.unknownc == 0) {
1068 		(void) fprintf(io->errs,
1069 			"memory verification failure: invalid signature time\n");
1070 	} else {
1071 		(void) fprintf(io->errs,
1072 "memory verification failure: %u invalid signatures, %u unknown signatures\n",
1073 			result.invalidc, result.unknownc);
1074 	}
1075 	return 0;
1076 }
1077 
1078 /* encrypt some memory */
1079 int
1080 netpgp_encrypt_memory(netpgp_t *netpgp,
1081 			const char *userid,
1082 			void *in,
1083 			const size_t insize,
1084 			char *out,
1085 			size_t outsize,
1086 			int armored)
1087 {
1088 	const __ops_key_t	*keypair;
1089 	__ops_memory_t		*enc;
1090 	__ops_io_t		*io;
1091 	size_t			 m;
1092 
1093 	io = netpgp->io;
1094 	if (in == NULL) {
1095 		(void) fprintf(io->errs,
1096 			"netpgp_encrypt_buf: no memory to encrypt\n");
1097 		return 0;
1098 	}
1099 	if (userid == NULL) {
1100 		userid = netpgp_getvar(netpgp, "userid");
1101 	}
1102 	keypair = __ops_getkeybyname(io, netpgp->pubring, userid);
1103 	if (keypair == NULL) {
1104 		(void) fprintf(io->errs, "Userid '%s' not found in keyring\n",
1105 					userid);
1106 		return 0;
1107 	}
1108 	if (in == out) {
1109 		(void) fprintf(io->errs,
1110 			"netpgp_encrypt_buf: input and output bufs need to be different\n");
1111 		return 0;
1112 	}
1113 	if (outsize < insize) {
1114 		(void) fprintf(io->errs,
1115 			"netpgp_encrypt_buf: input size is larger than output size\n");
1116 		return 0;
1117 	}
1118 	enc = __ops_encrypt_buf(io, in, insize, keypair, (unsigned)armored);
1119 	m = MIN(__ops_mem_len(enc), outsize);
1120 	(void) memcpy(out, __ops_mem_data(enc), m);
1121 	__ops_memory_free(enc);
1122 	return (int)m;
1123 }
1124 
1125 /* decrypt a chunk of memory */
1126 int
1127 netpgp_decrypt_memory(netpgp_t *netpgp, const void *input, const size_t insize,
1128 			char *out, size_t outsize, const int armored)
1129 {
1130 	__ops_memory_t	*mem;
1131 	__ops_io_t	*io;
1132 	unsigned	 realarmour;
1133 	size_t		 m;
1134 
1135 	io = netpgp->io;
1136 	realarmour = (unsigned) armored;
1137 	if (input == NULL) {
1138 		(void) fprintf(io->errs,
1139 			"netpgp_decrypt_memory: no memory\n");
1140 		return 0;
1141 	}
1142 	realarmour = (strncmp(input, ARMOR_HEAD, sizeof(ARMOR_HEAD) - 1) == 0);
1143 	mem = __ops_decrypt_buf(netpgp->io, input, insize, netpgp->secring,
1144 				netpgp->pubring,
1145 				realarmour, netpgp->passfp,
1146 				get_passphrase_cb);
1147 	m = MIN(__ops_mem_len(mem), outsize);
1148 	(void) memcpy(out, __ops_mem_data(mem), m);
1149 	__ops_memory_free(mem);
1150 	return (int)m;
1151 }
1152 
1153 /* wrappers for the ops_debug_level functions we added to openpgpsdk */
1154 
1155 /* set the debugging level per filename */
1156 int
1157 netpgp_set_debug(const char *f)
1158 {
1159 	return __ops_set_debug_level(f);
1160 }
1161 
1162 /* get the debugging level per filename */
1163 int
1164 netpgp_get_debug(const char *f)
1165 {
1166 	return __ops_get_debug_level(f);
1167 }
1168 
1169 /* return the version for the library */
1170 const char *
1171 netpgp_get_info(const char *type)
1172 {
1173 	return __ops_get_info(type);
1174 }
1175 
1176 /* list all the packets in a file */
1177 int
1178 netpgp_list_packets(netpgp_t *netpgp, char *f, int armour, char *pubringname)
1179 {
1180 	__ops_keyring_t	*keyring;
1181 	const unsigned	 noarmor = 0;
1182 	struct stat	 st;
1183 	__ops_io_t	*io;
1184 	char		 ringname[MAXPATHLEN];
1185 	char		*homedir;
1186 	int		 ret;
1187 
1188 	io = netpgp->io;
1189 	if (f == NULL) {
1190 		(void) fprintf(io->errs, "No file containing packets\n");
1191 		return 0;
1192 	}
1193 	if (stat(f, &st) < 0) {
1194 		(void) fprintf(io->errs, "No such file '%s'\n", f);
1195 		return 0;
1196 	}
1197 	homedir = netpgp_getvar(netpgp, "homedir");
1198 	if (pubringname == NULL) {
1199 		(void) snprintf(ringname, sizeof(ringname),
1200 				"%s/pubring.gpg", homedir);
1201 		pubringname = ringname;
1202 	}
1203 	if ((keyring = calloc(1, sizeof(*keyring))) == NULL) {
1204 		(void) fprintf(io->errs, "netpgp_list_packets: bad alloc\n");
1205 		return 0;
1206 	}
1207 	if (!__ops_keyring_fileread(keyring, noarmor, pubringname)) {
1208 		free(keyring);
1209 		(void) fprintf(io->errs, "Cannot read pub keyring %s\n",
1210 			pubringname);
1211 		return 0;
1212 	}
1213 	netpgp->pubring = keyring;
1214 	netpgp_setvar(netpgp, "pubring", pubringname);
1215 	ret = __ops_list_packets(io, f, (unsigned)armour,
1216 					netpgp->secring,
1217 					netpgp->pubring,
1218 					netpgp->passfp,
1219 					get_passphrase_cb);
1220 	free(keyring);
1221 	return ret;
1222 }
1223 
1224 /* set a variable */
1225 int
1226 netpgp_setvar(netpgp_t *netpgp, const char *name, const char *value)
1227 {
1228 	int	i;
1229 
1230 	if ((i = findvar(netpgp, name)) < 0) {
1231 		/* add the element to the array */
1232 		if (size_arrays(netpgp, netpgp->size + 15)) {
1233 			netpgp->name[i = netpgp->c++] = netpgp_strdup(name);
1234 		}
1235 	} else {
1236 		/* replace the element in the array */
1237 		if (netpgp->value[i]) {
1238 			free(netpgp->value[i]);
1239 			netpgp->value[i] = NULL;
1240 		}
1241 	}
1242 	/* sanity checks for range of values */
1243 	if (strcmp(name, "hash") == 0 || strcmp(name, "algorithm") == 0) {
1244 		if (__ops_str_to_hash_alg(value) == OPS_HASH_UNKNOWN) {
1245 			return 0;
1246 		}
1247 	}
1248 	netpgp->value[i] = netpgp_strdup(value);
1249 	return 1;
1250 }
1251 
1252 /* get a variable's value (NULL if not set) */
1253 char *
1254 netpgp_getvar(netpgp_t *netpgp, const char *name)
1255 {
1256 	int	i;
1257 
1258 	return ((i = findvar(netpgp, name)) < 0) ? NULL : netpgp->value[i];
1259 }
1260 
1261 /* increment a value */
1262 int
1263 netpgp_incvar(netpgp_t *netpgp, const char *name, const int delta)
1264 {
1265 	char	*cp;
1266 	char	 num[16];
1267 	int	 val;
1268 
1269 	val = 0;
1270 	if ((cp = netpgp_getvar(netpgp, name)) != NULL) {
1271 		val = atoi(cp);
1272 	}
1273 	(void) snprintf(num, sizeof(num), "%d", val + delta);
1274 	netpgp_setvar(netpgp, name, num);
1275 	return 1;
1276 }
1277 
1278 /* set the home directory value to "home/subdir" */
1279 int
1280 netpgp_set_homedir(netpgp_t *netpgp, char *home, const char *subdir, const int quiet)
1281 {
1282 	struct stat	st;
1283 	char		d[MAXPATHLEN];
1284 
1285 	if (home == NULL) {
1286 		if (!quiet) {
1287 			(void) fprintf(stderr, "NULL HOME directory\n");
1288 		}
1289 		return 0;
1290 	}
1291 	(void) snprintf(d, sizeof(d), "%s%s", home, (subdir) ? subdir : "");
1292 	if (stat(d, &st) == 0) {
1293 		if ((st.st_mode & S_IFMT) == S_IFDIR) {
1294 			netpgp_setvar(netpgp, "homedir", d);
1295 			return 1;
1296 		}
1297 		(void) fprintf(stderr, "netpgp: homedir \"%s\" is not a dir\n",
1298 					d);
1299 		return 0;
1300 	}
1301 	if (!quiet) {
1302 		(void) fprintf(stderr,
1303 			"netpgp: warning homedir \"%s\" not found\n", d);
1304 	}
1305 	return 1;
1306 }
1307 
1308 /* validate all sigs in the pub keyring */
1309 int
1310 netpgp_validate_sigs(netpgp_t *netpgp)
1311 {
1312 	__ops_validation_t	result;
1313 
1314 	return (int)__ops_validate_all_sigs(&result, netpgp->pubring, NULL);
1315 }
1316 
1317 #if 0
1318 #include "sshkey.h"
1319 
1320 int
1321 netpgp_pgpkey_to_sshkey(netpgp_t *netpgp, char *name, SSHKey *sshkey)
1322 {
1323 	const __ops_key_t	*pgpkey;
1324 	unsigned		 k;
1325 
1326 	k = 0;
1327 	pgpkey = __ops_getnextkeybyname(netpgp->io, netpgp->pubring, name, &k);
1328 	if (pgpkey == NULL) {
1329 		pgpkey = __ops_getkeybyname(io, netpgp->pubring, userid);
1330 	}
1331 	if (pgpkey == NULL) {
1332 		(void) fprintf(stderr, "No key matching '%s'\n", name);
1333 		return 0;
1334 	}
1335 	switch(pgpkey->key.pubkey.alg) {
1336 	case OPS_PKA_RSA:
1337 		sshkey->type = KEY_RSA;
1338 		sshkey->rsa = calloc(1, sizeof(*sshkey->rsa);
1339 		if (sshkey->rsa == NULL) {
1340 			(void) fprintf(stderr, "RSA memory problems\n");
1341 			return 0;
1342 		}
1343 		sshkey->rsa->n = pgpkey->key.pubkey.key.rsa.n;
1344 		sshkey->rsa->e = pgpkey->key.pubkey.key.rsa.e;
1345 		sshkey->rsa->d = pgpkey->key.seckey.key.rsa.d;
1346 		sshkey->rsa->p = pgpkey->key.seckey.key.rsa.p;
1347 		sshkey->rsa->q = pgpkey->key.seckey.key.rsa.q;
1348 		sshkey->rsa->iqmp = pgpkey->key.seckey.key.rsa.u;
1349 		break;
1350 	case OPS_PKA_DSA:
1351 		sshkey->type = KEY_DSA;
1352 		sshkey->dsa = calloc(1, sizeof(*sshkey->dsa);
1353 		if (sshkey->dsa == NULL) {
1354 			(void) fprintf(stderr, "DSA memory problems\n");
1355 			return 0;
1356 		}
1357 		sshkey->rsa->n = pgpkey->key.pubkey.key.rsa.n;
1358 		key->dsa->p = pgpkey->key.pubkey.key.dsa.p;
1359 		key->dsa->q = pgpkey->key.pubkey.key.dsa.q;
1360 		key->dsa->g = pgpkey->key.pubkey.key.dsa.g;
1361 		key->dsa->pub_key = pgpkey->key.pubkey.key.dsa.y;
1362 		key->dsa->priv_key = pgpkey->key.seckey.key.dsa.x;
1363 		break;
1364 	default:
1365 		(void) fprintf(stderr, "weird type\n");
1366 		return 0;
1367 	}
1368 	return 1;
1369 }
1370 #endif
1371