xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
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.45 2010/04/14 00:22:21 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, ring, pubkey, "pub", &pubkey->key.pubkey, 0);
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, const int psigs)
538 {
539 	return __ops_keyring_list(netpgp->io, netpgp->pubring, psigs);
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, const int psigs)
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(netpgp->io, netpgp->pubring,
570 						key, &pubs.v[pubs.c],
571 						&key->key.pubkey, psigs);
572 			} else {
573 				__ops_sprint_keydata(netpgp->io, netpgp->pubring,
574 						key, &pubs.v[pubs.c],
575 						"pub",
576 						&key->key.pubkey, psigs);
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(netpgp->io, netpgp->pubring,
663 				key, &newkey,
664 				&key->key.pubkey,
665 				netpgp_getvar(netpgp, "subkey sigs") != NULL) > 0) ? newkey : NULL;
666 	}
667 	return (__ops_sprint_keydata(netpgp->io, netpgp->pubring,
668 				key, &newkey, "pub",
669 				&key->key.pubkey,
670 				netpgp_getvar(netpgp, "subkey sigs") != NULL) > 0) ? newkey : NULL;
671 }
672 
673 /* export a given key */
674 char *
675 netpgp_export_key(netpgp_t *netpgp, char *name)
676 {
677 	const __ops_key_t	*key;
678 	__ops_io_t		*io;
679 
680 	io = netpgp->io;
681 	if (name == NULL) {
682 		name = netpgp_getvar(netpgp, "userid");
683 	} else if (name[0] == '0' && name[1] == 'x') {
684 		name += 2;
685 	}
686 	key = __ops_getkeybyname(io, netpgp->pubring, name);
687 	if (key == NULL) {
688 		(void) fprintf(io->errs,
689 			"Cannot find own key \"%s\" in keyring\n", name);
690 		return 0;
691 	}
692 	return __ops_export_key(io, key, NULL);
693 }
694 
695 /* import a key into our keyring */
696 int
697 netpgp_import_key(netpgp_t *netpgp, char *f)
698 {
699 	const unsigned	noarmor = 0;
700 	const unsigned	armor = 1;
701 	__ops_io_t	*io;
702 	int		done;
703 
704 	io = netpgp->io;
705 	if ((done = __ops_keyring_fileread(netpgp->pubring, noarmor, f)) == 0) {
706 		done = __ops_keyring_fileread(netpgp->pubring, armor, f);
707 	}
708 	if (!done) {
709 		(void) fprintf(io->errs, "Cannot import key from file %s\n", f);
710 		return 0;
711 	}
712 	return __ops_keyring_list(io, netpgp->pubring, 0);
713 }
714 
715 /* generate a new key */
716 int
717 netpgp_generate_key(netpgp_t *netpgp, char *id, int numbits)
718 {
719 	__ops_userid_t		 uid;
720 	__ops_output_t		*create;
721 	const unsigned		 noarmor = 0;
722 	__ops_key_t		*key;
723 	__ops_io_t		*io;
724 	char			 newid[1024];
725 	char			 filename[MAXPATHLEN];
726 	char			 dir[MAXPATHLEN];
727 	char			*cp;
728 	char			*ringfile;
729 	int             	 fd;
730 
731 	(void) memset(&uid, 0x0, sizeof(uid));
732 	io = netpgp->io;
733 	/* generate a new key */
734 	if (id) {
735 		(void) snprintf(newid, sizeof(newid), "%s", id);
736 	} else {
737 		(void) snprintf(newid, sizeof(newid), "RSA %d-bit key <%s@localhost>", numbits, getenv("LOGNAME"));
738 	}
739 	uid.userid = (uint8_t *)newid;
740 	key = __ops_rsa_new_selfsign_key(numbits, 65537UL, &uid, netpgp_getvar(netpgp, "hash"));
741 	if (key == NULL) {
742 		(void) fprintf(io->errs, "Cannot generate key\n");
743 		return 0;
744 	}
745 	cp = NULL;
746 	__ops_sprint_keydata(netpgp->io, NULL, key, &cp, "pub", &key->key.seckey.pubkey, 0);
747 	(void) fprintf(stdout, "%s", cp);
748 	/* write public key, and try to re-read it */
749 	(void) snprintf(dir, sizeof(dir), "%s/%.16s", netpgp_getvar(netpgp, "homedir"), &cp[31]);
750 	(void) mkdir(dir, 0700);
751 	(void) fprintf(io->errs, "netpgp: generated keys in directory %s\n", dir);
752 	(void) snprintf(ringfile = filename, sizeof(filename), "%s/pubring.gpg", dir);
753 	if ((fd = __ops_setup_file_append(&create, ringfile)) < 0) {
754 		fd = __ops_setup_file_write(&create, ringfile, 0);
755 	}
756 	if (fd < 0) {
757 		(void) fprintf(io->errs, "can't open pubring '%s'\n", ringfile);
758 		return 0;
759 	}
760 	if (!__ops_write_xfer_pubkey(create, key, noarmor)) {
761 		(void) fprintf(io->errs, "Cannot write pubkey\n");
762 		return 0;
763 	}
764 	__ops_teardown_file_write(create, fd);
765 	__ops_keyring_free(netpgp->pubring);
766 	if (!__ops_keyring_fileread(netpgp->pubring, noarmor, ringfile)) {
767 		(void) fprintf(io->errs, "Cannot read pubring %s\n", ringfile);
768 		return 0;
769 	}
770 	/* write secret key, and try to re-read it */
771 	(void) snprintf(ringfile = filename, sizeof(filename), "%s/secring.gpg", dir);
772 	if ((fd = __ops_setup_file_append(&create, ringfile)) < 0) {
773 		fd = __ops_setup_file_write(&create, ringfile, 0);
774 	}
775 	if (fd < 0) {
776 		(void) fprintf(io->errs, "can't append secring '%s'\n", ringfile);
777 		return 0;
778 	}
779 	if (!__ops_write_xfer_seckey(create, key, NULL, 0, noarmor)) {
780 		(void) fprintf(io->errs, "Cannot write seckey\n");
781 		return 0;
782 	}
783 	__ops_teardown_file_write(create, fd);
784 	__ops_keyring_free(netpgp->secring);
785 	if (!__ops_keyring_fileread(netpgp->secring, noarmor, ringfile)) {
786 		(void) fprintf(io->errs, "Can't read secring %s\n", ringfile);
787 		return 0;
788 	}
789 	__ops_keydata_free(key);
790 	free(cp);
791 	return 1;
792 }
793 
794 /* encrypt a file */
795 int
796 netpgp_encrypt_file(netpgp_t *netpgp,
797 			const char *userid,
798 			const char *f,
799 			char *out,
800 			int armored)
801 {
802 	const __ops_key_t	*keypair;
803 	const unsigned		 overwrite = 1;
804 	const char		*suffix;
805 	__ops_io_t		*io;
806 	char			 outname[MAXPATHLEN];
807 
808 	io = netpgp->io;
809 	if (f == NULL) {
810 		(void) fprintf(io->errs,
811 			"netpgp_encrypt_file: no filename specified\n");
812 		return 0;
813 	}
814 	if (userid == NULL) {
815 		userid = netpgp_getvar(netpgp, "userid");
816 	}
817 	suffix = (armored) ? ".asc" : ".gpg";
818 	keypair = __ops_getkeybyname(io, netpgp->pubring, userid);
819 	if (keypair == NULL) {
820 		(void) fprintf(io->errs, "Userid '%s' not found in keyring\n",
821 					userid);
822 		return 0;
823 	}
824 	if (out == NULL) {
825 		(void) snprintf(outname, sizeof(outname), "%s%s", f, suffix);
826 		out = outname;
827 	}
828 	return (int)__ops_encrypt_file(io, f, out, keypair, (unsigned)armored,
829 					overwrite);
830 }
831 
832 #define ARMOR_HEAD	"-----BEGIN PGP MESSAGE-----\r\n"
833 
834 /* decrypt a file */
835 int
836 netpgp_decrypt_file(netpgp_t *netpgp, const char *f, char *out, int armored)
837 {
838 	const unsigned	 overwrite = 1;
839 	__ops_io_t	*io;
840 	unsigned	 realarmour;
841 	FILE		*fp;
842 	char		 buf[BUFSIZ];
843 
844 	io = netpgp->io;
845 	if (f == NULL) {
846 		(void) fprintf(io->errs,
847 			"netpgp_decrypt_file: no filename specified\n");
848 		return 0;
849 	}
850 	realarmour = (unsigned)armored;
851 	if ((fp = fopen(f, "r")) == NULL) {
852 		(void) fprintf(io->errs,
853 			"netpgp_decrypt_file: can't open '%s'\n", f);
854 		return 0;
855 	}
856 	if (fgets(buf, sizeof(buf), fp) == NULL) {
857 		realarmour = 0;
858 	} else {
859 		realarmour = (strcmp(buf, ARMOR_HEAD) == 0);
860 	}
861 	(void) fclose(fp);
862 	return __ops_decrypt_file(netpgp->io, f, out, netpgp->secring,
863 				netpgp->pubring,
864 				(unsigned)realarmour, overwrite,
865 				netpgp->passfp, get_passphrase_cb);
866 }
867 
868 /* sign a file */
869 int
870 netpgp_sign_file(netpgp_t *netpgp,
871 		const char *userid,
872 		const char *f,
873 		char *out,
874 		int armored,
875 		int cleartext,
876 		int detached)
877 {
878 	const __ops_key_t	*keypair;
879 	const __ops_key_t	*pubkey;
880 	__ops_seckey_t		*seckey;
881 	const unsigned		 overwrite = 1;
882 	__ops_io_t		*io;
883 	char			*hashalg;
884 	int			 ret;
885 
886 	io = netpgp->io;
887 	if (f == NULL) {
888 		(void) fprintf(io->errs,
889 			"netpgp_sign_file: no filename specified\n");
890 		return 0;
891 	}
892 	if (userid == NULL) {
893 		userid = netpgp_getvar(netpgp, "userid");
894 	}
895 	/* get key with which to sign */
896 	keypair = __ops_getkeybyname(io, netpgp->secring, userid);
897 	if (keypair == NULL) {
898 		(void) fprintf(io->errs, "Userid '%s' not found in secring\n",
899 				userid);
900 		return 0;
901 	}
902 	ret = 1;
903 	do {
904 		if (netpgp->passfp == NULL) {
905 			/* print out the user id */
906 			pubkey = __ops_getkeybyname(io, netpgp->pubring, userid);
907 			if (pubkey == NULL) {
908 				(void) fprintf(io->errs,
909 					"netpgp: warning - using pubkey from secring\n");
910 				__ops_print_keydata(io, netpgp->pubring, keypair, "pub",
911 					&keypair->key.seckey.pubkey, 0);
912 			} else {
913 				__ops_print_keydata(io, netpgp->pubring, pubkey, "pub", &pubkey->key.pubkey, 0);
914 			}
915 		}
916 		if (netpgp_getvar(netpgp, "ssh keys") == NULL) {
917 			/* now decrypt key */
918 			seckey = __ops_decrypt_seckey(keypair, netpgp->passfp);
919 			if (seckey == NULL) {
920 				(void) fprintf(io->errs, "Bad passphrase\n");
921 			}
922 		} else {
923 			__ops_keyring_t	*secring;
924 
925 			secring = netpgp->secring;
926 			seckey = &secring->keys[0].key.seckey;
927 		}
928 	} while (seckey == NULL);
929 	/* sign file */
930 	hashalg = netpgp_getvar(netpgp, "hash");
931 	if (detached) {
932 		ret = __ops_sign_detached(io, f, out, seckey, hashalg,
933 				get_birthtime(netpgp_getvar(netpgp, "birthtime")),
934 				get_duration(netpgp_getvar(netpgp, "duration")));
935 	} else {
936 		ret = __ops_sign_file(io, f, out, seckey, hashalg,
937 				get_birthtime(netpgp_getvar(netpgp, "birthtime")),
938 				get_duration(netpgp_getvar(netpgp, "duration")),
939 				(unsigned)armored, (unsigned)cleartext,
940 				overwrite);
941 	}
942 	__ops_forget(seckey, sizeof(*seckey));
943 	return ret;
944 }
945 
946 /* verify a file */
947 int
948 netpgp_verify_file(netpgp_t *netpgp, const char *in, const char *out, int armored)
949 {
950 	__ops_validation_t	 result;
951 	__ops_io_t		*io;
952 
953 	(void) memset(&result, 0x0, sizeof(result));
954 	io = netpgp->io;
955 	if (in == NULL) {
956 		(void) fprintf(io->errs,
957 			"netpgp_verify_file: no filename specified\n");
958 		return 0;
959 	}
960 	if (__ops_validate_file(io, &result, in, out, armored,
961 						netpgp->pubring)) {
962 		resultp(io, in, &result, netpgp->pubring);
963 		return 1;
964 	}
965 	if (result.validc + result.invalidc + result.unknownc == 0) {
966 		(void) fprintf(io->errs,
967 		"\"%s\": No signatures found - is this a signed file?\n",
968 			in);
969 	} else if (result.invalidc == 0 && result.unknownc == 0) {
970 		(void) fprintf(io->errs,
971 			"\"%s\": file verification failure: invalid signature time\n", in);
972 	} else {
973 		(void) fprintf(io->errs,
974 "\"%s\": verification failure: %u invalid signatures, %u unknown signatures\n",
975 			in, result.invalidc, result.unknownc);
976 	}
977 	return 0;
978 }
979 
980 /* sign some memory */
981 int
982 netpgp_sign_memory(netpgp_t *netpgp,
983 		const char *userid,
984 		char *mem,
985 		size_t size,
986 		char *out,
987 		size_t outsize,
988 		const unsigned armored,
989 		const unsigned cleartext)
990 {
991 	const __ops_key_t	*keypair;
992 	const __ops_key_t	*pubkey;
993 	__ops_seckey_t		*seckey;
994 	__ops_memory_t		*signedmem;
995 	__ops_io_t		*io;
996 	char			*hashalg;
997 	int			 ret;
998 
999 	io = netpgp->io;
1000 	if (mem == NULL) {
1001 		(void) fprintf(io->errs,
1002 			"netpgp_sign_memory: no memory to sign\n");
1003 		return 0;
1004 	}
1005 	if (userid == NULL) {
1006 		userid = netpgp_getvar(netpgp, "userid");
1007 	}
1008 	/* get key with which to sign */
1009 	keypair = __ops_getkeybyname(io, netpgp->secring, userid);
1010 	if (keypair == NULL) {
1011 		(void) fprintf(io->errs, "Userid '%s' not found in keyring\n",
1012 				userid);
1013 		return 0;
1014 	}
1015 	ret = 1;
1016 	do {
1017 		if (netpgp->passfp == NULL) {
1018 			/* print out the user id */
1019 			pubkey = __ops_getkeybyname(io, netpgp->pubring, userid);
1020 			if (pubkey == NULL) {
1021 				(void) fprintf(io->errs,
1022 					"netpgp: warning - using pubkey from secring\n");
1023 				__ops_print_keydata(io, netpgp->pubring, keypair, "pub",
1024 					&keypair->key.seckey.pubkey, 0);
1025 			} else {
1026 				__ops_print_keydata(io, netpgp->pubring, pubkey, "pub", &pubkey->key.pubkey, 0);
1027 			}
1028 		}
1029 		/* now decrypt key */
1030 		seckey = __ops_decrypt_seckey(keypair, netpgp->passfp);
1031 		if (seckey == NULL) {
1032 			(void) fprintf(io->errs, "Bad passphrase\n");
1033 		}
1034 	} while (seckey == NULL);
1035 	/* sign file */
1036 	(void) memset(out, 0x0, outsize);
1037 	hashalg = netpgp_getvar(netpgp, "hash");
1038 	signedmem = __ops_sign_buf(io, mem, size, seckey,
1039 				get_birthtime(netpgp_getvar(netpgp, "birthtime")),
1040 				get_duration(netpgp_getvar(netpgp, "duration")),
1041 				hashalg, armored, cleartext);
1042 	if (signedmem) {
1043 		size_t	m;
1044 
1045 		m = MIN(__ops_mem_len(signedmem), outsize);
1046 		(void) memcpy(out, __ops_mem_data(signedmem), m);
1047 		__ops_memory_free(signedmem);
1048 		ret = (int)m;
1049 	} else {
1050 		ret = 0;
1051 	}
1052 	__ops_forget(seckey, sizeof(*seckey));
1053 	return ret;
1054 }
1055 
1056 /* verify memory */
1057 int
1058 netpgp_verify_memory(netpgp_t *netpgp, const void *in, const size_t size,
1059 			void *out, size_t outsize, const int armored)
1060 {
1061 	__ops_validation_t	 result;
1062 	__ops_memory_t		*signedmem;
1063 	__ops_memory_t		*cat;
1064 	__ops_io_t		*io;
1065 	size_t			 m;
1066 	int			 ret;
1067 
1068 	(void) memset(&result, 0x0, sizeof(result));
1069 	io = netpgp->io;
1070 	if (in == NULL) {
1071 		(void) fprintf(io->errs,
1072 			"netpgp_verify_memory: no memory to verify\n");
1073 		return 0;
1074 	}
1075 	signedmem = __ops_memory_new();
1076 	__ops_memory_add(signedmem, in, size);
1077 	if (out) {
1078 		cat = __ops_memory_new();
1079 	}
1080 	ret = __ops_validate_mem(io, &result, signedmem,
1081 				(out) ? &cat : NULL,
1082 				armored, netpgp->pubring);
1083 	__ops_memory_free(signedmem);
1084 	if (ret) {
1085 		resultp(io, "<stdin>", &result, netpgp->pubring);
1086 		if (out) {
1087 			m = MIN(__ops_mem_len(cat), outsize);
1088 			(void) memcpy(out, __ops_mem_data(cat), m);
1089 			__ops_memory_free(cat);
1090 		} else {
1091 			m = 1;
1092 		}
1093 		return (int)m;
1094 	}
1095 	if (result.validc + result.invalidc + result.unknownc == 0) {
1096 		(void) fprintf(io->errs,
1097 		"No signatures found - is this memory signed?\n");
1098 	} else if (result.invalidc == 0 && result.unknownc == 0) {
1099 		(void) fprintf(io->errs,
1100 			"memory verification failure: invalid signature time\n");
1101 	} else {
1102 		(void) fprintf(io->errs,
1103 "memory verification failure: %u invalid signatures, %u unknown signatures\n",
1104 			result.invalidc, result.unknownc);
1105 	}
1106 	return 0;
1107 }
1108 
1109 /* encrypt some memory */
1110 int
1111 netpgp_encrypt_memory(netpgp_t *netpgp,
1112 			const char *userid,
1113 			void *in,
1114 			const size_t insize,
1115 			char *out,
1116 			size_t outsize,
1117 			int armored)
1118 {
1119 	const __ops_key_t	*keypair;
1120 	__ops_memory_t		*enc;
1121 	__ops_io_t		*io;
1122 	size_t			 m;
1123 
1124 	io = netpgp->io;
1125 	if (in == NULL) {
1126 		(void) fprintf(io->errs,
1127 			"netpgp_encrypt_buf: no memory to encrypt\n");
1128 		return 0;
1129 	}
1130 	if (userid == NULL) {
1131 		userid = netpgp_getvar(netpgp, "userid");
1132 	}
1133 	keypair = __ops_getkeybyname(io, netpgp->pubring, userid);
1134 	if (keypair == NULL) {
1135 		(void) fprintf(io->errs, "Userid '%s' not found in keyring\n",
1136 					userid);
1137 		return 0;
1138 	}
1139 	if (in == out) {
1140 		(void) fprintf(io->errs,
1141 			"netpgp_encrypt_buf: input and output bufs need to be different\n");
1142 		return 0;
1143 	}
1144 	if (outsize < insize) {
1145 		(void) fprintf(io->errs,
1146 			"netpgp_encrypt_buf: input size is larger than output size\n");
1147 		return 0;
1148 	}
1149 	enc = __ops_encrypt_buf(io, in, insize, keypair, (unsigned)armored);
1150 	m = MIN(__ops_mem_len(enc), outsize);
1151 	(void) memcpy(out, __ops_mem_data(enc), m);
1152 	__ops_memory_free(enc);
1153 	return (int)m;
1154 }
1155 
1156 /* decrypt a chunk of memory */
1157 int
1158 netpgp_decrypt_memory(netpgp_t *netpgp, const void *input, const size_t insize,
1159 			char *out, size_t outsize, const int armored)
1160 {
1161 	__ops_memory_t	*mem;
1162 	__ops_io_t	*io;
1163 	unsigned	 realarmour;
1164 	size_t		 m;
1165 
1166 	io = netpgp->io;
1167 	realarmour = (unsigned) armored;
1168 	if (input == NULL) {
1169 		(void) fprintf(io->errs,
1170 			"netpgp_decrypt_memory: no memory\n");
1171 		return 0;
1172 	}
1173 	realarmour = (strncmp(input, ARMOR_HEAD, sizeof(ARMOR_HEAD) - 1) == 0);
1174 	mem = __ops_decrypt_buf(netpgp->io, input, insize, netpgp->secring,
1175 				netpgp->pubring,
1176 				realarmour, netpgp->passfp,
1177 				get_passphrase_cb);
1178 	m = MIN(__ops_mem_len(mem), outsize);
1179 	(void) memcpy(out, __ops_mem_data(mem), m);
1180 	__ops_memory_free(mem);
1181 	return (int)m;
1182 }
1183 
1184 /* wrappers for the ops_debug_level functions we added to openpgpsdk */
1185 
1186 /* set the debugging level per filename */
1187 int
1188 netpgp_set_debug(const char *f)
1189 {
1190 	return __ops_set_debug_level(f);
1191 }
1192 
1193 /* get the debugging level per filename */
1194 int
1195 netpgp_get_debug(const char *f)
1196 {
1197 	return __ops_get_debug_level(f);
1198 }
1199 
1200 /* return the version for the library */
1201 const char *
1202 netpgp_get_info(const char *type)
1203 {
1204 	return __ops_get_info(type);
1205 }
1206 
1207 /* list all the packets in a file */
1208 int
1209 netpgp_list_packets(netpgp_t *netpgp, char *f, int armour, char *pubringname)
1210 {
1211 	__ops_keyring_t	*keyring;
1212 	const unsigned	 noarmor = 0;
1213 	struct stat	 st;
1214 	__ops_io_t	*io;
1215 	char		 ringname[MAXPATHLEN];
1216 	char		*homedir;
1217 	int		 ret;
1218 
1219 	io = netpgp->io;
1220 	if (f == NULL) {
1221 		(void) fprintf(io->errs, "No file containing packets\n");
1222 		return 0;
1223 	}
1224 	if (stat(f, &st) < 0) {
1225 		(void) fprintf(io->errs, "No such file '%s'\n", f);
1226 		return 0;
1227 	}
1228 	homedir = netpgp_getvar(netpgp, "homedir");
1229 	if (pubringname == NULL) {
1230 		(void) snprintf(ringname, sizeof(ringname),
1231 				"%s/pubring.gpg", homedir);
1232 		pubringname = ringname;
1233 	}
1234 	if ((keyring = calloc(1, sizeof(*keyring))) == NULL) {
1235 		(void) fprintf(io->errs, "netpgp_list_packets: bad alloc\n");
1236 		return 0;
1237 	}
1238 	if (!__ops_keyring_fileread(keyring, noarmor, pubringname)) {
1239 		free(keyring);
1240 		(void) fprintf(io->errs, "Cannot read pub keyring %s\n",
1241 			pubringname);
1242 		return 0;
1243 	}
1244 	netpgp->pubring = keyring;
1245 	netpgp_setvar(netpgp, "pubring", pubringname);
1246 	ret = __ops_list_packets(io, f, (unsigned)armour,
1247 					netpgp->secring,
1248 					netpgp->pubring,
1249 					netpgp->passfp,
1250 					get_passphrase_cb);
1251 	free(keyring);
1252 	return ret;
1253 }
1254 
1255 /* set a variable */
1256 int
1257 netpgp_setvar(netpgp_t *netpgp, const char *name, const char *value)
1258 {
1259 	int	i;
1260 
1261 	if ((i = findvar(netpgp, name)) < 0) {
1262 		/* add the element to the array */
1263 		if (size_arrays(netpgp, netpgp->size + 15)) {
1264 			netpgp->name[i = netpgp->c++] = netpgp_strdup(name);
1265 		}
1266 	} else {
1267 		/* replace the element in the array */
1268 		if (netpgp->value[i]) {
1269 			free(netpgp->value[i]);
1270 			netpgp->value[i] = NULL;
1271 		}
1272 	}
1273 	/* sanity checks for range of values */
1274 	if (strcmp(name, "hash") == 0 || strcmp(name, "algorithm") == 0) {
1275 		if (__ops_str_to_hash_alg(value) == OPS_HASH_UNKNOWN) {
1276 			return 0;
1277 		}
1278 	}
1279 	netpgp->value[i] = netpgp_strdup(value);
1280 	return 1;
1281 }
1282 
1283 /* unset a variable */
1284 int
1285 netpgp_unsetvar(netpgp_t *netpgp, const char *name)
1286 {
1287 	int	i;
1288 
1289 	if ((i = findvar(netpgp, name)) >= 0) {
1290 		if (netpgp->value[i]) {
1291 			free(netpgp->value[i]);
1292 			netpgp->value[i] = NULL;
1293 		}
1294 		netpgp->value[i] = NULL;
1295 		return 1;
1296 	}
1297 	return 0;
1298 }
1299 
1300 /* get a variable's value (NULL if not set) */
1301 char *
1302 netpgp_getvar(netpgp_t *netpgp, const char *name)
1303 {
1304 	int	i;
1305 
1306 	return ((i = findvar(netpgp, name)) < 0) ? NULL : netpgp->value[i];
1307 }
1308 
1309 /* increment a value */
1310 int
1311 netpgp_incvar(netpgp_t *netpgp, const char *name, const int delta)
1312 {
1313 	char	*cp;
1314 	char	 num[16];
1315 	int	 val;
1316 
1317 	val = 0;
1318 	if ((cp = netpgp_getvar(netpgp, name)) != NULL) {
1319 		val = atoi(cp);
1320 	}
1321 	(void) snprintf(num, sizeof(num), "%d", val + delta);
1322 	netpgp_setvar(netpgp, name, num);
1323 	return 1;
1324 }
1325 
1326 /* set the home directory value to "home/subdir" */
1327 int
1328 netpgp_set_homedir(netpgp_t *netpgp, char *home, const char *subdir, const int quiet)
1329 {
1330 	struct stat	st;
1331 	char		d[MAXPATHLEN];
1332 
1333 	if (home == NULL) {
1334 		if (!quiet) {
1335 			(void) fprintf(stderr, "NULL HOME directory\n");
1336 		}
1337 		return 0;
1338 	}
1339 	(void) snprintf(d, sizeof(d), "%s%s", home, (subdir) ? subdir : "");
1340 	if (stat(d, &st) == 0) {
1341 		if ((st.st_mode & S_IFMT) == S_IFDIR) {
1342 			netpgp_setvar(netpgp, "homedir", d);
1343 			return 1;
1344 		}
1345 		(void) fprintf(stderr, "netpgp: homedir \"%s\" is not a dir\n",
1346 					d);
1347 		return 0;
1348 	}
1349 	if (!quiet) {
1350 		(void) fprintf(stderr,
1351 			"netpgp: warning homedir \"%s\" not found\n", d);
1352 	}
1353 	return 1;
1354 }
1355 
1356 /* validate all sigs in the pub keyring */
1357 int
1358 netpgp_validate_sigs(netpgp_t *netpgp)
1359 {
1360 	__ops_validation_t	result;
1361 
1362 	return (int)__ops_validate_all_sigs(&result, netpgp->pubring, NULL);
1363 }
1364 
1365 #if 0
1366 #include "sshkey.h"
1367 
1368 int
1369 netpgp_pgpkey_to_sshkey(netpgp_t *netpgp, char *name, SSHKey *sshkey)
1370 {
1371 	const __ops_key_t	*pgpkey;
1372 	unsigned		 k;
1373 
1374 	k = 0;
1375 	pgpkey = __ops_getnextkeybyname(netpgp->io, netpgp->pubring, name, &k);
1376 	if (pgpkey == NULL) {
1377 		pgpkey = __ops_getkeybyname(io, netpgp->pubring, userid);
1378 	}
1379 	if (pgpkey == NULL) {
1380 		(void) fprintf(stderr, "No key matching '%s'\n", name);
1381 		return 0;
1382 	}
1383 	switch(pgpkey->key.pubkey.alg) {
1384 	case OPS_PKA_RSA:
1385 		sshkey->type = KEY_RSA;
1386 		sshkey->rsa = calloc(1, sizeof(*sshkey->rsa);
1387 		if (sshkey->rsa == NULL) {
1388 			(void) fprintf(stderr, "RSA memory problems\n");
1389 			return 0;
1390 		}
1391 		sshkey->rsa->n = pgpkey->key.pubkey.key.rsa.n;
1392 		sshkey->rsa->e = pgpkey->key.pubkey.key.rsa.e;
1393 		sshkey->rsa->d = pgpkey->key.seckey.key.rsa.d;
1394 		sshkey->rsa->p = pgpkey->key.seckey.key.rsa.p;
1395 		sshkey->rsa->q = pgpkey->key.seckey.key.rsa.q;
1396 		sshkey->rsa->iqmp = pgpkey->key.seckey.key.rsa.u;
1397 		break;
1398 	case OPS_PKA_DSA:
1399 		sshkey->type = KEY_DSA;
1400 		sshkey->dsa = calloc(1, sizeof(*sshkey->dsa);
1401 		if (sshkey->dsa == NULL) {
1402 			(void) fprintf(stderr, "DSA memory problems\n");
1403 			return 0;
1404 		}
1405 		sshkey->rsa->n = pgpkey->key.pubkey.key.rsa.n;
1406 		key->dsa->p = pgpkey->key.pubkey.key.dsa.p;
1407 		key->dsa->q = pgpkey->key.pubkey.key.dsa.q;
1408 		key->dsa->g = pgpkey->key.pubkey.key.dsa.g;
1409 		key->dsa->pub_key = pgpkey->key.pubkey.key.dsa.y;
1410 		key->dsa->priv_key = pgpkey->key.seckey.key.dsa.x;
1411 		break;
1412 	default:
1413 		(void) fprintf(stderr, "weird type\n");
1414 		return 0;
1415 	}
1416 	return 1;
1417 }
1418 #endif
1419