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