xref: /openbsd-src/usr.bin/ssh/ssh-pkcs11-client.c (revision 5411e7691687441b2e28282ca3a1c531c6b949c4)
1 /* $OpenBSD: ssh-pkcs11-client.c,v 1.20 2024/08/15 00:51:51 djm Exp $ */
2 /*
3  * Copyright (c) 2010 Markus Friedl.  All rights reserved.
4  * Copyright (c) 2014 Pedro Martelletto. All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/socket.h>
22 
23 #include <stdarg.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <limits.h>
28 
29 #include <openssl/ecdsa.h>
30 #include <openssl/rsa.h>
31 
32 #include "pathnames.h"
33 #include "xmalloc.h"
34 #include "sshbuf.h"
35 #include "log.h"
36 #include "misc.h"
37 #include "sshkey.h"
38 #include "authfd.h"
39 #include "atomicio.h"
40 #include "ssh-pkcs11.h"
41 #include "ssherr.h"
42 
43 /* borrows code from sftp-server and ssh-agent */
44 
45 /*
46  * Maintain a list of ssh-pkcs11-helper subprocesses. These may be looked up
47  * by provider path or their unique EC/RSA METHOD pointers.
48  */
49 struct helper {
50 	char *path;
51 	pid_t pid;
52 	int fd;
53 	RSA_METHOD *rsa_meth;
54 	EC_KEY_METHOD *ec_meth;
55 	int (*rsa_finish)(RSA *rsa);
56 	void (*ec_finish)(EC_KEY *key);
57 	size_t nrsa, nec; /* number of active keys of each type */
58 };
59 static struct helper **helpers;
60 static size_t nhelpers;
61 
62 static struct helper *
63 helper_by_provider(const char *path)
64 {
65 	size_t i;
66 
67 	for (i = 0; i < nhelpers; i++) {
68 		if (helpers[i] == NULL || helpers[i]->path == NULL ||
69 		    helpers[i]->fd == -1)
70 			continue;
71 		if (strcmp(helpers[i]->path, path) == 0)
72 			return helpers[i];
73 	}
74 	return NULL;
75 }
76 
77 static struct helper *
78 helper_by_rsa(const RSA *rsa)
79 {
80 	size_t i;
81 	const RSA_METHOD *meth;
82 
83 	if ((meth = RSA_get_method(rsa)) == NULL)
84 		return NULL;
85 	for (i = 0; i < nhelpers; i++) {
86 		if (helpers[i] != NULL && helpers[i]->rsa_meth == meth)
87 			return helpers[i];
88 	}
89 	return NULL;
90 
91 }
92 
93 static struct helper *
94 helper_by_ec(const EC_KEY *ec)
95 {
96 	size_t i;
97 	const EC_KEY_METHOD *meth;
98 
99 	if ((meth = EC_KEY_get_method(ec)) == NULL)
100 		return NULL;
101 	for (i = 0; i < nhelpers; i++) {
102 		if (helpers[i] != NULL && helpers[i]->ec_meth == meth)
103 			return helpers[i];
104 	}
105 	return NULL;
106 
107 }
108 
109 static void
110 helper_free(struct helper *helper)
111 {
112 	size_t i;
113 	int found = 0;
114 
115 	if (helper == NULL)
116 		return;
117 	if (helper->path == NULL || helper->ec_meth == NULL ||
118 	    helper->rsa_meth == NULL)
119 		fatal_f("inconsistent helper");
120 	debug3_f("free helper for provider %s", helper->path);
121 	for (i = 0; i < nhelpers; i++) {
122 		if (helpers[i] == helper) {
123 			if (found)
124 				fatal_f("helper recorded more than once");
125 			found = 1;
126 		}
127 		else if (found)
128 			helpers[i - 1] = helpers[i];
129 	}
130 	if (found) {
131 		helpers = xrecallocarray(helpers, nhelpers,
132 		    nhelpers - 1, sizeof(*helpers));
133 		nhelpers--;
134 	}
135 	free(helper->path);
136 	EC_KEY_METHOD_free(helper->ec_meth);
137 	RSA_meth_free(helper->rsa_meth);
138 	free(helper);
139 }
140 
141 static void
142 helper_terminate(struct helper *helper)
143 {
144 	if (helper == NULL) {
145 		return;
146 	} else if (helper->fd == -1) {
147 		debug3_f("already terminated");
148 	} else {
149 		debug3_f("terminating helper for %s; "
150 		    "remaining %zu RSA %zu ECDSA",
151 		    helper->path, helper->nrsa, helper->nec);
152 		close(helper->fd);
153 		/* XXX waitpid() */
154 		helper->fd = -1;
155 		helper->pid = -1;
156 	}
157 	/*
158 	 * Don't delete the helper entry until there are no remaining keys
159 	 * that reference it. Otherwise, any signing operation would call
160 	 * a free'd METHOD pointer and that would be bad.
161 	 */
162 	if (helper->nrsa == 0 && helper->nec == 0)
163 		helper_free(helper);
164 }
165 
166 static void
167 send_msg(int fd, struct sshbuf *m)
168 {
169 	u_char buf[4];
170 	size_t mlen = sshbuf_len(m);
171 	int r;
172 
173 	if (fd == -1)
174 		return;
175 	POKE_U32(buf, mlen);
176 	if (atomicio(vwrite, fd, buf, 4) != 4 ||
177 	    atomicio(vwrite, fd, sshbuf_mutable_ptr(m),
178 	    sshbuf_len(m)) != sshbuf_len(m))
179 		error("write to helper failed");
180 	if ((r = sshbuf_consume(m, mlen)) != 0)
181 		fatal_fr(r, "consume");
182 }
183 
184 static int
185 recv_msg(int fd, struct sshbuf *m)
186 {
187 	u_int l, len;
188 	u_char c, buf[1024];
189 	int r;
190 
191 	sshbuf_reset(m);
192 	if (fd == -1)
193 		return 0; /* XXX */
194 	if ((len = atomicio(read, fd, buf, 4)) != 4) {
195 		error("read from helper failed: %u", len);
196 		return (0); /* XXX */
197 	}
198 	len = PEEK_U32(buf);
199 	if (len > 256 * 1024)
200 		fatal("response too long: %u", len);
201 	/* read len bytes into m */
202 	while (len > 0) {
203 		l = len;
204 		if (l > sizeof(buf))
205 			l = sizeof(buf);
206 		if (atomicio(read, fd, buf, l) != l) {
207 			error("response from helper failed.");
208 			return (0); /* XXX */
209 		}
210 		if ((r = sshbuf_put(m, buf, l)) != 0)
211 			fatal_fr(r, "sshbuf_put");
212 		len -= l;
213 	}
214 	if ((r = sshbuf_get_u8(m, &c)) != 0)
215 		fatal_fr(r, "parse type");
216 	return c;
217 }
218 
219 int
220 pkcs11_init(int interactive)
221 {
222 	return 0;
223 }
224 
225 void
226 pkcs11_terminate(void)
227 {
228 	size_t i;
229 
230 	debug3_f("terminating %zu helpers", nhelpers);
231 	for (i = 0; i < nhelpers; i++)
232 		helper_terminate(helpers[i]);
233 }
234 
235 static int
236 rsa_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa, int padding)
237 {
238 	struct sshkey *key = NULL;
239 	struct sshbuf *msg = NULL;
240 	u_char *blob = NULL, *signature = NULL;
241 	size_t blen, slen = 0;
242 	int r, ret = -1;
243 	struct helper *helper;
244 
245 	if ((helper = helper_by_rsa(rsa)) == NULL || helper->fd == -1)
246 		fatal_f("no helper for PKCS11 key");
247 	debug3_f("signing with PKCS11 provider %s", helper->path);
248 	if (padding != RSA_PKCS1_PADDING)
249 		goto fail;
250 	if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
251 		error_f("sshkey_new failed");
252 		goto fail;
253 	}
254 	if ((key->pkey = EVP_PKEY_new()) == NULL ||
255 	   EVP_PKEY_set1_RSA(key->pkey, rsa) != 1) {
256 		error_f("pkey setup failed");
257 		goto fail;
258 	}
259 
260 	key->type = KEY_RSA;
261 	if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
262 		error_fr(r, "encode key");
263 		goto fail;
264 	}
265 	if ((msg = sshbuf_new()) == NULL)
266 		fatal_f("sshbuf_new failed");
267 	if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
268 	    (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
269 	    (r = sshbuf_put_string(msg, from, flen)) != 0 ||
270 	    (r = sshbuf_put_u32(msg, 0)) != 0)
271 		fatal_fr(r, "compose");
272 	send_msg(helper->fd, msg);
273 	sshbuf_reset(msg);
274 
275 	if (recv_msg(helper->fd, msg) == SSH2_AGENT_SIGN_RESPONSE) {
276 		if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
277 			fatal_fr(r, "parse");
278 		if (slen <= (size_t)RSA_size(rsa)) {
279 			memcpy(to, signature, slen);
280 			ret = slen;
281 		}
282 		free(signature);
283 	}
284  fail:
285 	free(blob);
286 	sshkey_free(key);
287 	sshbuf_free(msg);
288 	return (ret);
289 }
290 
291 static int
292 rsa_finish(RSA *rsa)
293 {
294 	struct helper *helper;
295 
296 	if ((helper = helper_by_rsa(rsa)) == NULL)
297 		fatal_f("no helper for PKCS11 key");
298 	debug3_f("free PKCS11 RSA key for provider %s", helper->path);
299 	if (helper->rsa_finish != NULL)
300 		helper->rsa_finish(rsa);
301 	if (helper->nrsa == 0)
302 		fatal_f("RSA refcount error");
303 	helper->nrsa--;
304 	debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
305 	    helper->path, helper->nrsa, helper->nec);
306 	if (helper->nrsa == 0 && helper->nec == 0)
307 		helper_terminate(helper);
308 	return 1;
309 }
310 
311 static ECDSA_SIG *
312 ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
313     const BIGNUM *rp, EC_KEY *ec)
314 {
315 	struct sshkey *key = NULL;
316 	struct sshbuf *msg = NULL;
317 	ECDSA_SIG *ret = NULL;
318 	const u_char *cp;
319 	u_char *blob = NULL, *signature = NULL;
320 	size_t blen, slen = 0;
321 	int r, nid;
322 	struct helper *helper;
323 
324 	if ((helper = helper_by_ec(ec)) == NULL || helper->fd == -1)
325 		fatal_f("no helper for PKCS11 key");
326 	debug3_f("signing with PKCS11 provider %s", helper->path);
327 
328 	if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
329 		error_f("sshkey_new failed");
330 		goto fail;
331 	}
332 	if ((key->pkey = EVP_PKEY_new()) == NULL ||
333 	    EVP_PKEY_set1_EC_KEY(key->pkey, ec) != 1) {
334 		error("pkey setup failed");
335 		goto fail;
336 	}
337 	if ((nid = sshkey_ecdsa_pkey_to_nid(key->pkey)) < 0) {
338 		error("couldn't get curve nid");
339 		goto fail;
340 	}
341 	key->ecdsa_nid = nid;
342 	key->type = KEY_ECDSA;
343 
344 	if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
345 		error_fr(r, "encode key");
346 		goto fail;
347 	}
348 	if ((msg = sshbuf_new()) == NULL)
349 		fatal_f("sshbuf_new failed");
350 	if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
351 	    (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
352 	    (r = sshbuf_put_string(msg, dgst, dgst_len)) != 0 ||
353 	    (r = sshbuf_put_u32(msg, 0)) != 0)
354 		fatal_fr(r, "compose");
355 	send_msg(helper->fd, msg);
356 	sshbuf_reset(msg);
357 
358 	if (recv_msg(helper->fd, msg) == SSH2_AGENT_SIGN_RESPONSE) {
359 		if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
360 			fatal_fr(r, "parse");
361 		cp = signature;
362 		ret = d2i_ECDSA_SIG(NULL, &cp, slen);
363 		free(signature);
364 	}
365 
366  fail:
367 	free(blob);
368 	sshkey_free(key);
369 	sshbuf_free(msg);
370 	return (ret);
371 }
372 
373 static void
374 ecdsa_do_finish(EC_KEY *ec)
375 {
376 	struct helper *helper;
377 
378 	if ((helper = helper_by_ec(ec)) == NULL)
379 		fatal_f("no helper for PKCS11 key");
380 	debug3_f("free PKCS11 ECDSA key for provider %s", helper->path);
381 	if (helper->ec_finish != NULL)
382 		helper->ec_finish(ec);
383 	if (helper->nec == 0)
384 		fatal_f("ECDSA refcount error");
385 	helper->nec--;
386 	debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
387 	    helper->path, helper->nrsa, helper->nec);
388 	if (helper->nrsa == 0 && helper->nec == 0)
389 		helper_terminate(helper);
390 }
391 
392 /* redirect private key crypto operations to the ssh-pkcs11-helper */
393 static void
394 wrap_key(struct helper *helper, struct sshkey *k)
395 {
396 	RSA *rsa = NULL;
397 	EC_KEY *ecdsa = NULL;
398 
399 	debug3_f("wrap %s for provider %s", sshkey_type(k), helper->path);
400 	if (k->type == KEY_RSA) {
401 		if ((rsa = EVP_PKEY_get1_RSA(k->pkey)) == NULL)
402 			fatal_f("no RSA key");
403 		if (RSA_set_method(rsa, helper->rsa_meth) != 1)
404 			fatal_f("RSA_set_method failed");
405 		if (helper->nrsa++ >= INT_MAX)
406 			fatal_f("RSA refcount error");
407 		if (EVP_PKEY_set1_RSA(k->pkey, rsa) != 1)
408 			fatal_f("EVP_PKEY_set1_RSA failed");
409 		RSA_free(rsa);
410 	} else if (k->type == KEY_ECDSA) {
411 		if ((ecdsa = EVP_PKEY_get1_EC_KEY(k->pkey)) == NULL)
412 			fatal_f("no ECDSA key");
413 		if (EC_KEY_set_method(ecdsa, helper->ec_meth) != 1)
414 			fatal_f("EC_KEY_set_method failed");
415 		if (helper->nec++ >= INT_MAX)
416 			fatal_f("EC refcount error");
417 		if (EVP_PKEY_set1_EC_KEY(k->pkey, ecdsa) != 1)
418 			fatal_f("EVP_PKEY_set1_EC_KEY failed");
419 		EC_KEY_free(ecdsa);
420 	} else
421 		fatal_f("unknown key type");
422 	k->flags |= SSHKEY_FLAG_EXT;
423 	debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
424 	    helper->path, helper->nrsa, helper->nec);
425 }
426 
427 /*
428  * Make a private PKCS#11-backed certificate by grafting a previously-loaded
429  * PKCS#11 private key and a public certificate key.
430  */
431 int
432 pkcs11_make_cert(const struct sshkey *priv,
433     const struct sshkey *certpub, struct sshkey **certprivp)
434 {
435 	struct helper *helper = NULL;
436 	struct sshkey *ret;
437 	int r;
438 	RSA *rsa_priv = NULL, *rsa_cert = NULL;
439 	EC_KEY *ec_priv = NULL, *ec_cert = NULL;
440 
441 	debug3_f("private key type %s cert type %s", sshkey_type(priv),
442 	    sshkey_type(certpub));
443 	*certprivp = NULL;
444 	if (!sshkey_is_cert(certpub) || sshkey_is_cert(priv) ||
445 	    !sshkey_equal_public(priv, certpub)) {
446 		error_f("private key %s doesn't match cert %s",
447 		    sshkey_type(priv), sshkey_type(certpub));
448 		return SSH_ERR_INVALID_ARGUMENT;
449 	}
450 	*certprivp = NULL;
451 	if (priv->type == KEY_RSA) {
452 		if ((rsa_priv = EVP_PKEY_get1_RSA(priv->pkey)) == NULL)
453 			fatal_f("no RSA pkey");
454 		if ((helper = helper_by_rsa(rsa_priv)) == NULL ||
455 		    helper->fd == -1)
456 			fatal_f("no helper for PKCS11 RSA key");
457 		if ((r = sshkey_from_private(priv, &ret)) != 0)
458 			fatal_fr(r, "copy key");
459 		if ((rsa_cert = EVP_PKEY_get1_RSA(ret->pkey)) == NULL)
460 			fatal_f("no RSA cert pkey");
461 		if (RSA_set_method(rsa_cert, helper->rsa_meth) != 1)
462 			fatal_f("RSA_set_method failed");
463 		if (helper->nrsa++ >= INT_MAX)
464 			fatal_f("RSA refcount error");
465 		if (EVP_PKEY_set1_RSA(ret->pkey, rsa_cert) != 1)
466 			fatal_f("EVP_PKEY_set1_RSA failed");
467 		RSA_free(rsa_priv);
468 		RSA_free(rsa_cert);
469 	} else if (priv->type == KEY_ECDSA) {
470 		if ((ec_priv = EVP_PKEY_get1_EC_KEY(priv->pkey)) == NULL)
471 			fatal_f("no EC pkey");
472 		if ((helper = helper_by_ec(ec_priv)) == NULL ||
473 		    helper->fd == -1)
474 			fatal_f("no helper for PKCS11 EC key");
475 		if ((r = sshkey_from_private(priv, &ret)) != 0)
476 			fatal_fr(r, "copy key");
477 		if ((ec_cert = EVP_PKEY_get1_EC_KEY(ret->pkey)) == NULL)
478 			fatal_f("no EC cert pkey");
479 		if (EC_KEY_set_method(ec_cert, helper->ec_meth) != 1)
480 			fatal_f("EC_KEY_set_method failed");
481 		if (helper->nec++ >= INT_MAX)
482 			fatal_f("EC refcount error");
483 		if (EVP_PKEY_set1_EC_KEY(ret->pkey, ec_cert) != 1)
484 			fatal_f("EVP_PKEY_set1_EC_KEY failed");
485 		EC_KEY_free(ec_priv);
486 		EC_KEY_free(ec_cert);
487 	} else
488 		fatal_f("unknown key type %s", sshkey_type(priv));
489 
490 	ret->flags |= SSHKEY_FLAG_EXT;
491 	if ((r = sshkey_to_certified(ret)) != 0 ||
492 	    (r = sshkey_cert_copy(certpub, ret)) != 0)
493 		fatal_fr(r, "graft certificate");
494 	debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
495 	    helper->path, helper->nrsa, helper->nec);
496 	/* success */
497 	*certprivp = ret;
498 	return 0;
499 }
500 
501 static int
502 pkcs11_start_helper_methods(struct helper *helper)
503 {
504 	int (*ec_init)(EC_KEY *key);
505 	int (*ec_copy)(EC_KEY *dest, const EC_KEY *src);
506 	int (*ec_set_group)(EC_KEY *key, const EC_GROUP *grp);
507 	int (*ec_set_private)(EC_KEY *key, const BIGNUM *priv_key);
508 	int (*ec_set_public)(EC_KEY *key, const EC_POINT *pub_key);
509 	int (*ec_sign)(int, const unsigned char *, int, unsigned char *,
510 	    unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL;
511 	RSA_METHOD *rsa_meth;
512 	EC_KEY_METHOD *ec_meth;
513 
514 	if ((ec_meth = EC_KEY_METHOD_new(EC_KEY_OpenSSL())) == NULL)
515 		return -1;
516 	EC_KEY_METHOD_get_sign(ec_meth, &ec_sign, NULL, NULL);
517 	EC_KEY_METHOD_set_sign(ec_meth, ec_sign, NULL, ecdsa_do_sign);
518 	EC_KEY_METHOD_get_init(ec_meth, &ec_init, &helper->ec_finish,
519 	    &ec_copy, &ec_set_group, &ec_set_private, &ec_set_public);
520 	EC_KEY_METHOD_set_init(ec_meth, ec_init, ecdsa_do_finish,
521 	    ec_copy, ec_set_group, ec_set_private, ec_set_public);
522 
523 	if ((rsa_meth = RSA_meth_dup(RSA_get_default_method())) == NULL)
524 		fatal_f("RSA_meth_dup failed");
525 	helper->rsa_finish = RSA_meth_get_finish(rsa_meth);
526 	if (!RSA_meth_set1_name(rsa_meth, "ssh-pkcs11-helper") ||
527 	    !RSA_meth_set_priv_enc(rsa_meth, rsa_encrypt) ||
528 	    !RSA_meth_set_finish(rsa_meth, rsa_finish))
529 		fatal_f("failed to prepare method");
530 
531 	helper->ec_meth = ec_meth;
532 	helper->rsa_meth = rsa_meth;
533 	return 0;
534 }
535 
536 static struct helper *
537 pkcs11_start_helper(const char *path)
538 {
539 	int pair[2];
540 	char *prog, *verbosity = NULL;
541 	struct helper *helper;
542 	pid_t pid;
543 
544 	if (nhelpers >= INT_MAX)
545 		fatal_f("too many helpers");
546 	debug3_f("start helper for %s", path);
547 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
548 		error_f("socketpair: %s", strerror(errno));
549 		return NULL;
550 	}
551 	helper = xcalloc(1, sizeof(*helper));
552 	if (pkcs11_start_helper_methods(helper) == -1) {
553 		error_f("pkcs11_start_helper_methods failed");
554 		goto fail;
555 	}
556 	if ((pid = fork()) == -1) {
557 		error_f("fork: %s", strerror(errno));
558  fail:
559 		close(pair[0]);
560 		close(pair[1]);
561 		RSA_meth_free(helper->rsa_meth);
562 		EC_KEY_METHOD_free(helper->ec_meth);
563 		free(helper);
564 		return NULL;
565 	} else if (pid == 0) {
566 		if ((dup2(pair[1], STDIN_FILENO) == -1) ||
567 		    (dup2(pair[1], STDOUT_FILENO) == -1)) {
568 			fprintf(stderr, "dup2: %s\n", strerror(errno));
569 			_exit(1);
570 		}
571 		close(pair[0]);
572 		close(pair[1]);
573 		prog = getenv("SSH_PKCS11_HELPER");
574 		if (prog == NULL || strlen(prog) == 0)
575 			prog = _PATH_SSH_PKCS11_HELPER;
576 		if (log_level_get() >= SYSLOG_LEVEL_DEBUG1)
577 			verbosity = "-vvv";
578 		debug_f("starting %s %s", prog,
579 		    verbosity == NULL ? "" : verbosity);
580 		execlp(prog, prog, verbosity, (char *)NULL);
581 		fprintf(stderr, "exec: %s: %s\n", prog, strerror(errno));
582 		_exit(1);
583 	}
584 	close(pair[1]);
585 	helper->fd = pair[0];
586 	helper->path = xstrdup(path);
587 	helper->pid = pid;
588 	debug3_f("helper %zu for \"%s\" on fd %d pid %ld", nhelpers,
589 	    helper->path, helper->fd, (long)helper->pid);
590 	helpers = xrecallocarray(helpers, nhelpers,
591 	    nhelpers + 1, sizeof(*helpers));
592 	helpers[nhelpers++] = helper;
593 	return helper;
594 }
595 
596 int
597 pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp,
598     char ***labelsp)
599 {
600 	struct sshkey *k;
601 	int r, type;
602 	u_char *blob;
603 	char *label;
604 	size_t blen;
605 	u_int nkeys, i;
606 	struct sshbuf *msg;
607 	struct helper *helper;
608 
609 	if ((helper = helper_by_provider(name)) == NULL &&
610 	    (helper = pkcs11_start_helper(name)) == NULL)
611 		return -1;
612 
613 	if ((msg = sshbuf_new()) == NULL)
614 		fatal_f("sshbuf_new failed");
615 	if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 ||
616 	    (r = sshbuf_put_cstring(msg, name)) != 0 ||
617 	    (r = sshbuf_put_cstring(msg, pin)) != 0)
618 		fatal_fr(r, "compose");
619 	send_msg(helper->fd, msg);
620 	sshbuf_reset(msg);
621 
622 	type = recv_msg(helper->fd, msg);
623 	if (type == SSH2_AGENT_IDENTITIES_ANSWER) {
624 		if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
625 			fatal_fr(r, "parse nkeys");
626 		*keysp = xcalloc(nkeys, sizeof(struct sshkey *));
627 		if (labelsp)
628 			*labelsp = xcalloc(nkeys, sizeof(char *));
629 		for (i = 0; i < nkeys; i++) {
630 			/* XXX clean up properly instead of fatal() */
631 			if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 ||
632 			    (r = sshbuf_get_cstring(msg, &label, NULL)) != 0)
633 				fatal_fr(r, "parse key");
634 			if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
635 				fatal_fr(r, "decode key");
636 			wrap_key(helper, k);
637 			(*keysp)[i] = k;
638 			if (labelsp)
639 				(*labelsp)[i] = label;
640 			else
641 				free(label);
642 			free(blob);
643 		}
644 	} else if (type == SSH2_AGENT_FAILURE) {
645 		if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
646 			nkeys = -1;
647 	} else {
648 		nkeys = -1;
649 	}
650 	sshbuf_free(msg);
651 	return (nkeys);
652 }
653 
654 int
655 pkcs11_del_provider(char *name)
656 {
657 	struct helper *helper;
658 
659 	/*
660 	 * ssh-agent deletes keys before calling this, so the helper entry
661 	 * should be gone before we get here.
662 	 */
663 	debug3_f("delete %s", name);
664 	if ((helper = helper_by_provider(name)) != NULL)
665 		helper_terminate(helper);
666 	return 0;
667 }
668