xref: /freebsd/crypto/openssh/ssh-pkcs11.c (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1 /* $OpenBSD: ssh-pkcs11.c,v 1.54 2021/08/11 05:20:17 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 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
25 #endif
26 
27 #include <sys/types.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 
31 #include <ctype.h>
32 #include <string.h>
33 #include <dlfcn.h>
34 
35 #include "openbsd-compat/sys-queue.h"
36 #include "openbsd-compat/openssl-compat.h"
37 
38 #include <openssl/ecdsa.h>
39 #include <openssl/x509.h>
40 #include <openssl/err.h>
41 
42 #define CRYPTOKI_COMPAT
43 #include "pkcs11.h"
44 
45 #include "log.h"
46 #include "misc.h"
47 #include "sshkey.h"
48 #include "ssh-pkcs11.h"
49 #include "digest.h"
50 #include "xmalloc.h"
51 
52 struct pkcs11_slotinfo {
53 	CK_TOKEN_INFO		token;
54 	CK_SESSION_HANDLE	session;
55 	int			logged_in;
56 };
57 
58 struct pkcs11_provider {
59 	char			*name;
60 	void			*handle;
61 	CK_FUNCTION_LIST	*function_list;
62 	CK_INFO			info;
63 	CK_ULONG		nslots;
64 	CK_SLOT_ID		*slotlist;
65 	struct pkcs11_slotinfo	*slotinfo;
66 	int			valid;
67 	int			refcount;
68 	TAILQ_ENTRY(pkcs11_provider) next;
69 };
70 
71 TAILQ_HEAD(, pkcs11_provider) pkcs11_providers;
72 
73 struct pkcs11_key {
74 	struct pkcs11_provider	*provider;
75 	CK_ULONG		slotidx;
76 	char			*keyid;
77 	int			keyid_len;
78 };
79 
80 int pkcs11_interactive = 0;
81 
82 #ifdef HAVE_EC_KEY_METHOD_NEW
83 static void
84 ossl_error(const char *msg)
85 {
86 	unsigned long    e;
87 
88 	error_f("%s", msg);
89 	while ((e = ERR_get_error()) != 0)
90 		error_f("libcrypto error: %s", ERR_error_string(e, NULL));
91 }
92 #endif /* HAVE_EC_KEY_METHOD_NEW */
93 
94 int
95 pkcs11_init(int interactive)
96 {
97 	pkcs11_interactive = interactive;
98 	TAILQ_INIT(&pkcs11_providers);
99 	return (0);
100 }
101 
102 /*
103  * finalize a provider shared library, it's no longer usable.
104  * however, there might still be keys referencing this provider,
105  * so the actual freeing of memory is handled by pkcs11_provider_unref().
106  * this is called when a provider gets unregistered.
107  */
108 static void
109 pkcs11_provider_finalize(struct pkcs11_provider *p)
110 {
111 	CK_RV rv;
112 	CK_ULONG i;
113 
114 	debug_f("provider \"%s\" refcount %d valid %d",
115 	    p->name, p->refcount, p->valid);
116 	if (!p->valid)
117 		return;
118 	for (i = 0; i < p->nslots; i++) {
119 		if (p->slotinfo[i].session &&
120 		    (rv = p->function_list->C_CloseSession(
121 		    p->slotinfo[i].session)) != CKR_OK)
122 			error("C_CloseSession failed: %lu", rv);
123 	}
124 	if ((rv = p->function_list->C_Finalize(NULL)) != CKR_OK)
125 		error("C_Finalize failed: %lu", rv);
126 	p->valid = 0;
127 	p->function_list = NULL;
128 	dlclose(p->handle);
129 }
130 
131 /*
132  * remove a reference to the provider.
133  * called when a key gets destroyed or when the provider is unregistered.
134  */
135 static void
136 pkcs11_provider_unref(struct pkcs11_provider *p)
137 {
138 	debug_f("provider \"%s\" refcount %d", p->name, p->refcount);
139 	if (--p->refcount <= 0) {
140 		if (p->valid)
141 			error_f("provider \"%s\" still valid", p->name);
142 		free(p->name);
143 		free(p->slotlist);
144 		free(p->slotinfo);
145 		free(p);
146 	}
147 }
148 
149 /* unregister all providers, keys might still point to the providers */
150 void
151 pkcs11_terminate(void)
152 {
153 	struct pkcs11_provider *p;
154 
155 	while ((p = TAILQ_FIRST(&pkcs11_providers)) != NULL) {
156 		TAILQ_REMOVE(&pkcs11_providers, p, next);
157 		pkcs11_provider_finalize(p);
158 		pkcs11_provider_unref(p);
159 	}
160 }
161 
162 /* lookup provider by name */
163 static struct pkcs11_provider *
164 pkcs11_provider_lookup(char *provider_id)
165 {
166 	struct pkcs11_provider *p;
167 
168 	TAILQ_FOREACH(p, &pkcs11_providers, next) {
169 		debug("check provider \"%s\"", p->name);
170 		if (!strcmp(provider_id, p->name))
171 			return (p);
172 	}
173 	return (NULL);
174 }
175 
176 /* unregister provider by name */
177 int
178 pkcs11_del_provider(char *provider_id)
179 {
180 	struct pkcs11_provider *p;
181 
182 	if ((p = pkcs11_provider_lookup(provider_id)) != NULL) {
183 		TAILQ_REMOVE(&pkcs11_providers, p, next);
184 		pkcs11_provider_finalize(p);
185 		pkcs11_provider_unref(p);
186 		return (0);
187 	}
188 	return (-1);
189 }
190 
191 static RSA_METHOD *rsa_method;
192 static int rsa_idx = 0;
193 #ifdef HAVE_EC_KEY_METHOD_NEW
194 static EC_KEY_METHOD *ec_key_method;
195 static int ec_key_idx = 0;
196 #endif
197 
198 /* release a wrapped object */
199 static void
200 pkcs11_k11_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx,
201     long argl, void *argp)
202 {
203 	struct pkcs11_key	*k11 = ptr;
204 
205 	debug_f("parent %p ptr %p idx %d", parent, ptr, idx);
206 	if (k11 == NULL)
207 		return;
208 	if (k11->provider)
209 		pkcs11_provider_unref(k11->provider);
210 	free(k11->keyid);
211 	free(k11);
212 }
213 
214 /* find a single 'obj' for given attributes */
215 static int
216 pkcs11_find(struct pkcs11_provider *p, CK_ULONG slotidx, CK_ATTRIBUTE *attr,
217     CK_ULONG nattr, CK_OBJECT_HANDLE *obj)
218 {
219 	CK_FUNCTION_LIST	*f;
220 	CK_SESSION_HANDLE	session;
221 	CK_ULONG		nfound = 0;
222 	CK_RV			rv;
223 	int			ret = -1;
224 
225 	f = p->function_list;
226 	session = p->slotinfo[slotidx].session;
227 	if ((rv = f->C_FindObjectsInit(session, attr, nattr)) != CKR_OK) {
228 		error("C_FindObjectsInit failed (nattr %lu): %lu", nattr, rv);
229 		return (-1);
230 	}
231 	if ((rv = f->C_FindObjects(session, obj, 1, &nfound)) != CKR_OK ||
232 	    nfound != 1) {
233 		debug("C_FindObjects failed (nfound %lu nattr %lu): %lu",
234 		    nfound, nattr, rv);
235 	} else
236 		ret = 0;
237 	if ((rv = f->C_FindObjectsFinal(session)) != CKR_OK)
238 		error("C_FindObjectsFinal failed: %lu", rv);
239 	return (ret);
240 }
241 
242 static int
243 pkcs11_login_slot(struct pkcs11_provider *provider, struct pkcs11_slotinfo *si,
244     CK_USER_TYPE type)
245 {
246 	char			*pin = NULL, prompt[1024];
247 	CK_RV			 rv;
248 
249 	if (provider == NULL || si == NULL || !provider->valid) {
250 		error("no pkcs11 (valid) provider found");
251 		return (-1);
252 	}
253 
254 	if (!pkcs11_interactive) {
255 		error("need pin entry%s",
256 		    (si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH) ?
257 		    " on reader keypad" : "");
258 		return (-1);
259 	}
260 	if (si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH)
261 		verbose("Deferring PIN entry to reader keypad.");
262 	else {
263 		snprintf(prompt, sizeof(prompt), "Enter PIN for '%s': ",
264 		    si->token.label);
265 		if ((pin = read_passphrase(prompt, RP_ALLOW_EOF)) == NULL) {
266 			debug_f("no pin specified");
267 			return (-1);	/* bail out */
268 		}
269 	}
270 	rv = provider->function_list->C_Login(si->session, type, (u_char *)pin,
271 	    (pin != NULL) ? strlen(pin) : 0);
272 	if (pin != NULL)
273 		freezero(pin, strlen(pin));
274 
275 	switch (rv) {
276 	case CKR_OK:
277 	case CKR_USER_ALREADY_LOGGED_IN:
278 		/* success */
279 		break;
280 	case CKR_PIN_LEN_RANGE:
281 		error("PKCS#11 login failed: PIN length out of range");
282 		return -1;
283 	case CKR_PIN_INCORRECT:
284 		error("PKCS#11 login failed: PIN incorrect");
285 		return -1;
286 	case CKR_PIN_LOCKED:
287 		error("PKCS#11 login failed: PIN locked");
288 		return -1;
289 	default:
290 		error("PKCS#11 login failed: error %lu", rv);
291 		return -1;
292 	}
293 	si->logged_in = 1;
294 	return (0);
295 }
296 
297 static int
298 pkcs11_login(struct pkcs11_key *k11, CK_USER_TYPE type)
299 {
300 	if (k11 == NULL || k11->provider == NULL || !k11->provider->valid) {
301 		error("no pkcs11 (valid) provider found");
302 		return (-1);
303 	}
304 
305 	return pkcs11_login_slot(k11->provider,
306 	    &k11->provider->slotinfo[k11->slotidx], type);
307 }
308 
309 
310 static int
311 pkcs11_check_obj_bool_attrib(struct pkcs11_key *k11, CK_OBJECT_HANDLE obj,
312     CK_ATTRIBUTE_TYPE type, int *val)
313 {
314 	struct pkcs11_slotinfo	*si;
315 	CK_FUNCTION_LIST	*f;
316 	CK_BBOOL		flag = 0;
317 	CK_ATTRIBUTE		attr;
318 	CK_RV			 rv;
319 
320 	*val = 0;
321 
322 	if (!k11->provider || !k11->provider->valid) {
323 		error("no pkcs11 (valid) provider found");
324 		return (-1);
325 	}
326 
327 	f = k11->provider->function_list;
328 	si = &k11->provider->slotinfo[k11->slotidx];
329 
330 	attr.type = type;
331 	attr.pValue = &flag;
332 	attr.ulValueLen = sizeof(flag);
333 
334 	rv = f->C_GetAttributeValue(si->session, obj, &attr, 1);
335 	if (rv != CKR_OK) {
336 		error("C_GetAttributeValue failed: %lu", rv);
337 		return (-1);
338 	}
339 	*val = flag != 0;
340 	debug_f("provider \"%s\" slot %lu object %lu: attrib %lu = %d",
341 	    k11->provider->name, k11->slotidx, obj, type, *val);
342 	return (0);
343 }
344 
345 static int
346 pkcs11_get_key(struct pkcs11_key *k11, CK_MECHANISM_TYPE mech_type)
347 {
348 	struct pkcs11_slotinfo	*si;
349 	CK_FUNCTION_LIST	*f;
350 	CK_OBJECT_HANDLE	 obj;
351 	CK_RV			 rv;
352 	CK_OBJECT_CLASS		 private_key_class;
353 	CK_BBOOL		 true_val;
354 	CK_MECHANISM		 mech;
355 	CK_ATTRIBUTE		 key_filter[3];
356 	int			 always_auth = 0;
357 	int			 did_login = 0;
358 
359 	if (!k11->provider || !k11->provider->valid) {
360 		error("no pkcs11 (valid) provider found");
361 		return (-1);
362 	}
363 
364 	f = k11->provider->function_list;
365 	si = &k11->provider->slotinfo[k11->slotidx];
366 
367 	if ((si->token.flags & CKF_LOGIN_REQUIRED) && !si->logged_in) {
368 		if (pkcs11_login(k11, CKU_USER) < 0) {
369 			error("login failed");
370 			return (-1);
371 		}
372 		did_login = 1;
373 	}
374 
375 	memset(&key_filter, 0, sizeof(key_filter));
376 	private_key_class = CKO_PRIVATE_KEY;
377 	key_filter[0].type = CKA_CLASS;
378 	key_filter[0].pValue = &private_key_class;
379 	key_filter[0].ulValueLen = sizeof(private_key_class);
380 
381 	key_filter[1].type = CKA_ID;
382 	key_filter[1].pValue = k11->keyid;
383 	key_filter[1].ulValueLen = k11->keyid_len;
384 
385 	true_val = CK_TRUE;
386 	key_filter[2].type = CKA_SIGN;
387 	key_filter[2].pValue = &true_val;
388 	key_filter[2].ulValueLen = sizeof(true_val);
389 
390 	/* try to find object w/CKA_SIGN first, retry w/o */
391 	if (pkcs11_find(k11->provider, k11->slotidx, key_filter, 3, &obj) < 0 &&
392 	    pkcs11_find(k11->provider, k11->slotidx, key_filter, 2, &obj) < 0) {
393 		error("cannot find private key");
394 		return (-1);
395 	}
396 
397 	memset(&mech, 0, sizeof(mech));
398 	mech.mechanism = mech_type;
399 	mech.pParameter = NULL_PTR;
400 	mech.ulParameterLen = 0;
401 
402 	if ((rv = f->C_SignInit(si->session, &mech, obj)) != CKR_OK) {
403 		error("C_SignInit failed: %lu", rv);
404 		return (-1);
405 	}
406 
407 	pkcs11_check_obj_bool_attrib(k11, obj, CKA_ALWAYS_AUTHENTICATE,
408 	    &always_auth); /* ignore errors here */
409 	if (always_auth && !did_login) {
410 		debug_f("always-auth key");
411 		if (pkcs11_login(k11, CKU_CONTEXT_SPECIFIC) < 0) {
412 			error("login failed for always-auth key");
413 			return (-1);
414 		}
415 	}
416 
417 	return (0);
418 }
419 
420 /* openssl callback doing the actual signing operation */
421 static int
422 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
423     int padding)
424 {
425 	struct pkcs11_key	*k11;
426 	struct pkcs11_slotinfo	*si;
427 	CK_FUNCTION_LIST	*f;
428 	CK_ULONG		tlen = 0;
429 	CK_RV			rv;
430 	int			rval = -1;
431 
432 	if ((k11 = RSA_get_ex_data(rsa, rsa_idx)) == NULL) {
433 		error("RSA_get_ex_data failed");
434 		return (-1);
435 	}
436 
437 	if (pkcs11_get_key(k11, CKM_RSA_PKCS) == -1) {
438 		error("pkcs11_get_key failed");
439 		return (-1);
440 	}
441 
442 	f = k11->provider->function_list;
443 	si = &k11->provider->slotinfo[k11->slotidx];
444 	tlen = RSA_size(rsa);
445 
446 	/* XXX handle CKR_BUFFER_TOO_SMALL */
447 	rv = f->C_Sign(si->session, (CK_BYTE *)from, flen, to, &tlen);
448 	if (rv == CKR_OK)
449 		rval = tlen;
450 	else
451 		error("C_Sign failed: %lu", rv);
452 
453 	return (rval);
454 }
455 
456 static int
457 pkcs11_rsa_private_decrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
458     int padding)
459 {
460 	return (-1);
461 }
462 
463 static int
464 pkcs11_rsa_start_wrapper(void)
465 {
466 	if (rsa_method != NULL)
467 		return (0);
468 	rsa_method = RSA_meth_dup(RSA_get_default_method());
469 	if (rsa_method == NULL)
470 		return (-1);
471 	rsa_idx = RSA_get_ex_new_index(0, "ssh-pkcs11-rsa",
472 	    NULL, NULL, pkcs11_k11_free);
473 	if (rsa_idx == -1)
474 		return (-1);
475 	if (!RSA_meth_set1_name(rsa_method, "pkcs11") ||
476 	    !RSA_meth_set_priv_enc(rsa_method, pkcs11_rsa_private_encrypt) ||
477 	    !RSA_meth_set_priv_dec(rsa_method, pkcs11_rsa_private_decrypt)) {
478 		error_f("setup pkcs11 method failed");
479 		return (-1);
480 	}
481 	return (0);
482 }
483 
484 /* redirect private key operations for rsa key to pkcs11 token */
485 static int
486 pkcs11_rsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
487     CK_ATTRIBUTE *keyid_attrib, RSA *rsa)
488 {
489 	struct pkcs11_key	*k11;
490 
491 	if (pkcs11_rsa_start_wrapper() == -1)
492 		return (-1);
493 
494 	k11 = xcalloc(1, sizeof(*k11));
495 	k11->provider = provider;
496 	provider->refcount++;	/* provider referenced by RSA key */
497 	k11->slotidx = slotidx;
498 	/* identify key object on smartcard */
499 	k11->keyid_len = keyid_attrib->ulValueLen;
500 	if (k11->keyid_len > 0) {
501 		k11->keyid = xmalloc(k11->keyid_len);
502 		memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
503 	}
504 
505 	RSA_set_method(rsa, rsa_method);
506 	RSA_set_ex_data(rsa, rsa_idx, k11);
507 	return (0);
508 }
509 
510 #ifdef HAVE_EC_KEY_METHOD_NEW
511 /* openssl callback doing the actual signing operation */
512 static ECDSA_SIG *
513 ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
514     const BIGNUM *rp, EC_KEY *ec)
515 {
516 	struct pkcs11_key	*k11;
517 	struct pkcs11_slotinfo	*si;
518 	CK_FUNCTION_LIST	*f;
519 	CK_ULONG		siglen = 0, bnlen;
520 	CK_RV			rv;
521 	ECDSA_SIG		*ret = NULL;
522 	u_char			*sig;
523 	BIGNUM			*r = NULL, *s = NULL;
524 
525 	if ((k11 = EC_KEY_get_ex_data(ec, ec_key_idx)) == NULL) {
526 		ossl_error("EC_KEY_get_key_method_data failed for ec");
527 		return (NULL);
528 	}
529 
530 	if (pkcs11_get_key(k11, CKM_ECDSA) == -1) {
531 		error("pkcs11_get_key failed");
532 		return (NULL);
533 	}
534 
535 	f = k11->provider->function_list;
536 	si = &k11->provider->slotinfo[k11->slotidx];
537 
538 	siglen = ECDSA_size(ec);
539 	sig = xmalloc(siglen);
540 
541 	/* XXX handle CKR_BUFFER_TOO_SMALL */
542 	rv = f->C_Sign(si->session, (CK_BYTE *)dgst, dgst_len, sig, &siglen);
543 	if (rv != CKR_OK) {
544 		error("C_Sign failed: %lu", rv);
545 		goto done;
546 	}
547 	if (siglen < 64 || siglen > 132 || siglen % 2) {
548 		ossl_error("d2i_ECDSA_SIG failed");
549 		goto done;
550 	}
551 	bnlen = siglen/2;
552 	if ((ret = ECDSA_SIG_new()) == NULL) {
553 		error("ECDSA_SIG_new failed");
554 		goto done;
555 	}
556 	if ((r = BN_bin2bn(sig, bnlen, NULL)) == NULL ||
557 	    (s = BN_bin2bn(sig+bnlen, bnlen, NULL)) == NULL) {
558 		ossl_error("d2i_ECDSA_SIG failed");
559 		ECDSA_SIG_free(ret);
560 		ret = NULL;
561 		goto done;
562 	}
563 	if (!ECDSA_SIG_set0(ret, r, s)) {
564 		error_f("ECDSA_SIG_set0 failed");
565 		ECDSA_SIG_free(ret);
566 		ret = NULL;
567 		goto done;
568 	}
569 	r = s = NULL; /* now owned by ret */
570 	/* success */
571  done:
572 	BN_free(r);
573 	BN_free(s);
574 	free(sig);
575 
576 	return (ret);
577 }
578 
579 static int
580 pkcs11_ecdsa_start_wrapper(void)
581 {
582 	int (*orig_sign)(int, const unsigned char *, int, unsigned char *,
583 	    unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL;
584 
585 	if (ec_key_method != NULL)
586 		return (0);
587 	ec_key_idx = EC_KEY_get_ex_new_index(0, "ssh-pkcs11-ecdsa",
588 	    NULL, NULL, pkcs11_k11_free);
589 	if (ec_key_idx == -1)
590 		return (-1);
591 	ec_key_method = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
592 	if (ec_key_method == NULL)
593 		return (-1);
594 	EC_KEY_METHOD_get_sign(ec_key_method, &orig_sign, NULL, NULL);
595 	EC_KEY_METHOD_set_sign(ec_key_method, orig_sign, NULL, ecdsa_do_sign);
596 	return (0);
597 }
598 
599 static int
600 pkcs11_ecdsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
601     CK_ATTRIBUTE *keyid_attrib, EC_KEY *ec)
602 {
603 	struct pkcs11_key	*k11;
604 
605 	if (pkcs11_ecdsa_start_wrapper() == -1)
606 		return (-1);
607 
608 	k11 = xcalloc(1, sizeof(*k11));
609 	k11->provider = provider;
610 	provider->refcount++;	/* provider referenced by ECDSA key */
611 	k11->slotidx = slotidx;
612 	/* identify key object on smartcard */
613 	k11->keyid_len = keyid_attrib->ulValueLen;
614 	k11->keyid = xmalloc(k11->keyid_len);
615 	memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
616 
617 	EC_KEY_set_method(ec, ec_key_method);
618 	EC_KEY_set_ex_data(ec, ec_key_idx, k11);
619 
620 	return (0);
621 }
622 #endif /* HAVE_EC_KEY_METHOD_NEW */
623 
624 /* remove trailing spaces */
625 static void
626 rmspace(u_char *buf, size_t len)
627 {
628 	size_t i;
629 
630 	if (!len)
631 		return;
632 	for (i = len - 1;  i > 0; i--)
633 		if (i == len - 1 || buf[i] == ' ')
634 			buf[i] = '\0';
635 		else
636 			break;
637 }
638 
639 /*
640  * open a pkcs11 session and login if required.
641  * if pin == NULL we delay login until key use
642  */
643 static int
644 pkcs11_open_session(struct pkcs11_provider *p, CK_ULONG slotidx, char *pin,
645     CK_ULONG user)
646 {
647 	struct pkcs11_slotinfo	*si;
648 	CK_FUNCTION_LIST	*f;
649 	CK_RV			rv;
650 	CK_SESSION_HANDLE	session;
651 	int			login_required, ret;
652 
653 	f = p->function_list;
654 	si = &p->slotinfo[slotidx];
655 
656 	login_required = si->token.flags & CKF_LOGIN_REQUIRED;
657 
658 	/* fail early before opening session */
659 	if (login_required && !pkcs11_interactive &&
660 	    (pin == NULL || strlen(pin) == 0)) {
661 		error("pin required");
662 		return (-SSH_PKCS11_ERR_PIN_REQUIRED);
663 	}
664 	if ((rv = f->C_OpenSession(p->slotlist[slotidx], CKF_RW_SESSION|
665 	    CKF_SERIAL_SESSION, NULL, NULL, &session)) != CKR_OK) {
666 		error("C_OpenSession failed: %lu", rv);
667 		return (-1);
668 	}
669 	if (login_required && pin != NULL && strlen(pin) != 0) {
670 		rv = f->C_Login(session, user, (u_char *)pin, strlen(pin));
671 		if (rv != CKR_OK && rv != CKR_USER_ALREADY_LOGGED_IN) {
672 			error("C_Login failed: %lu", rv);
673 			ret = (rv == CKR_PIN_LOCKED) ?
674 			    -SSH_PKCS11_ERR_PIN_LOCKED :
675 			    -SSH_PKCS11_ERR_LOGIN_FAIL;
676 			if ((rv = f->C_CloseSession(session)) != CKR_OK)
677 				error("C_CloseSession failed: %lu", rv);
678 			return (ret);
679 		}
680 		si->logged_in = 1;
681 	}
682 	si->session = session;
683 	return (0);
684 }
685 
686 static int
687 pkcs11_key_included(struct sshkey ***keysp, int *nkeys, struct sshkey *key)
688 {
689 	int i;
690 
691 	for (i = 0; i < *nkeys; i++)
692 		if (sshkey_equal(key, (*keysp)[i]))
693 			return (1);
694 	return (0);
695 }
696 
697 #ifdef HAVE_EC_KEY_METHOD_NEW
698 static struct sshkey *
699 pkcs11_fetch_ecdsa_pubkey(struct pkcs11_provider *p, CK_ULONG slotidx,
700     CK_OBJECT_HANDLE *obj)
701 {
702 	CK_ATTRIBUTE		 key_attr[3];
703 	CK_SESSION_HANDLE	 session;
704 	CK_FUNCTION_LIST	*f = NULL;
705 	CK_RV			 rv;
706 	ASN1_OCTET_STRING	*octet = NULL;
707 	EC_KEY			*ec = NULL;
708 	EC_GROUP		*group = NULL;
709 	struct sshkey		*key = NULL;
710 	const unsigned char	*attrp = NULL;
711 	int			 i;
712 	int			 nid;
713 
714 	memset(&key_attr, 0, sizeof(key_attr));
715 	key_attr[0].type = CKA_ID;
716 	key_attr[1].type = CKA_EC_POINT;
717 	key_attr[2].type = CKA_EC_PARAMS;
718 
719 	session = p->slotinfo[slotidx].session;
720 	f = p->function_list;
721 
722 	/* figure out size of the attributes */
723 	rv = f->C_GetAttributeValue(session, *obj, key_attr, 3);
724 	if (rv != CKR_OK) {
725 		error("C_GetAttributeValue failed: %lu", rv);
726 		return (NULL);
727 	}
728 
729 	/*
730 	 * Allow CKA_ID (always first attribute) to be empty, but
731 	 * ensure that none of the others are zero length.
732 	 * XXX assumes CKA_ID is always first.
733 	 */
734 	if (key_attr[1].ulValueLen == 0 ||
735 	    key_attr[2].ulValueLen == 0) {
736 		error("invalid attribute length");
737 		return (NULL);
738 	}
739 
740 	/* allocate buffers for attributes */
741 	for (i = 0; i < 3; i++)
742 		if (key_attr[i].ulValueLen > 0)
743 			key_attr[i].pValue = xcalloc(1, key_attr[i].ulValueLen);
744 
745 	/* retrieve ID, public point and curve parameters of EC key */
746 	rv = f->C_GetAttributeValue(session, *obj, key_attr, 3);
747 	if (rv != CKR_OK) {
748 		error("C_GetAttributeValue failed: %lu", rv);
749 		goto fail;
750 	}
751 
752 	ec = EC_KEY_new();
753 	if (ec == NULL) {
754 		error("EC_KEY_new failed");
755 		goto fail;
756 	}
757 
758 	attrp = key_attr[2].pValue;
759 	group = d2i_ECPKParameters(NULL, &attrp, key_attr[2].ulValueLen);
760 	if (group == NULL) {
761 		ossl_error("d2i_ECPKParameters failed");
762 		goto fail;
763 	}
764 
765 	if (EC_KEY_set_group(ec, group) == 0) {
766 		ossl_error("EC_KEY_set_group failed");
767 		goto fail;
768 	}
769 
770 	if (key_attr[1].ulValueLen <= 2) {
771 		error("CKA_EC_POINT too small");
772 		goto fail;
773 	}
774 
775 	attrp = key_attr[1].pValue;
776 	octet = d2i_ASN1_OCTET_STRING(NULL, &attrp, key_attr[1].ulValueLen);
777 	if (octet == NULL) {
778 		ossl_error("d2i_ASN1_OCTET_STRING failed");
779 		goto fail;
780 	}
781 	attrp = octet->data;
782 	if (o2i_ECPublicKey(&ec, &attrp, octet->length) == NULL) {
783 		ossl_error("o2i_ECPublicKey failed");
784 		goto fail;
785 	}
786 
787 	nid = sshkey_ecdsa_key_to_nid(ec);
788 	if (nid < 0) {
789 		error("couldn't get curve nid");
790 		goto fail;
791 	}
792 
793 	if (pkcs11_ecdsa_wrap(p, slotidx, &key_attr[0], ec))
794 		goto fail;
795 
796 	key = sshkey_new(KEY_UNSPEC);
797 	if (key == NULL) {
798 		error("sshkey_new failed");
799 		goto fail;
800 	}
801 
802 	key->ecdsa = ec;
803 	key->ecdsa_nid = nid;
804 	key->type = KEY_ECDSA;
805 	key->flags |= SSHKEY_FLAG_EXT;
806 	ec = NULL;	/* now owned by key */
807 
808 fail:
809 	for (i = 0; i < 3; i++)
810 		free(key_attr[i].pValue);
811 	if (ec)
812 		EC_KEY_free(ec);
813 	if (group)
814 		EC_GROUP_free(group);
815 	if (octet)
816 		ASN1_OCTET_STRING_free(octet);
817 
818 	return (key);
819 }
820 #endif /* HAVE_EC_KEY_METHOD_NEW */
821 
822 static struct sshkey *
823 pkcs11_fetch_rsa_pubkey(struct pkcs11_provider *p, CK_ULONG slotidx,
824     CK_OBJECT_HANDLE *obj)
825 {
826 	CK_ATTRIBUTE		 key_attr[3];
827 	CK_SESSION_HANDLE	 session;
828 	CK_FUNCTION_LIST	*f = NULL;
829 	CK_RV			 rv;
830 	RSA			*rsa = NULL;
831 	BIGNUM			*rsa_n, *rsa_e;
832 	struct sshkey		*key = NULL;
833 	int			 i;
834 
835 	memset(&key_attr, 0, sizeof(key_attr));
836 	key_attr[0].type = CKA_ID;
837 	key_attr[1].type = CKA_MODULUS;
838 	key_attr[2].type = CKA_PUBLIC_EXPONENT;
839 
840 	session = p->slotinfo[slotidx].session;
841 	f = p->function_list;
842 
843 	/* figure out size of the attributes */
844 	rv = f->C_GetAttributeValue(session, *obj, key_attr, 3);
845 	if (rv != CKR_OK) {
846 		error("C_GetAttributeValue failed: %lu", rv);
847 		return (NULL);
848 	}
849 
850 	/*
851 	 * Allow CKA_ID (always first attribute) to be empty, but
852 	 * ensure that none of the others are zero length.
853 	 * XXX assumes CKA_ID is always first.
854 	 */
855 	if (key_attr[1].ulValueLen == 0 ||
856 	    key_attr[2].ulValueLen == 0) {
857 		error("invalid attribute length");
858 		return (NULL);
859 	}
860 
861 	/* allocate buffers for attributes */
862 	for (i = 0; i < 3; i++)
863 		if (key_attr[i].ulValueLen > 0)
864 			key_attr[i].pValue = xcalloc(1, key_attr[i].ulValueLen);
865 
866 	/* retrieve ID, modulus and public exponent of RSA key */
867 	rv = f->C_GetAttributeValue(session, *obj, key_attr, 3);
868 	if (rv != CKR_OK) {
869 		error("C_GetAttributeValue failed: %lu", rv);
870 		goto fail;
871 	}
872 
873 	rsa = RSA_new();
874 	if (rsa == NULL) {
875 		error("RSA_new failed");
876 		goto fail;
877 	}
878 
879 	rsa_n = BN_bin2bn(key_attr[1].pValue, key_attr[1].ulValueLen, NULL);
880 	rsa_e = BN_bin2bn(key_attr[2].pValue, key_attr[2].ulValueLen, NULL);
881 	if (rsa_n == NULL || rsa_e == NULL) {
882 		error("BN_bin2bn failed");
883 		goto fail;
884 	}
885 	if (!RSA_set0_key(rsa, rsa_n, rsa_e, NULL))
886 		fatal_f("set key");
887 	rsa_n = rsa_e = NULL; /* transferred */
888 
889 	if (pkcs11_rsa_wrap(p, slotidx, &key_attr[0], rsa))
890 		goto fail;
891 
892 	key = sshkey_new(KEY_UNSPEC);
893 	if (key == NULL) {
894 		error("sshkey_new failed");
895 		goto fail;
896 	}
897 
898 	key->rsa = rsa;
899 	key->type = KEY_RSA;
900 	key->flags |= SSHKEY_FLAG_EXT;
901 	rsa = NULL;	/* now owned by key */
902 
903 fail:
904 	for (i = 0; i < 3; i++)
905 		free(key_attr[i].pValue);
906 	RSA_free(rsa);
907 
908 	return (key);
909 }
910 
911 static int
912 pkcs11_fetch_x509_pubkey(struct pkcs11_provider *p, CK_ULONG slotidx,
913     CK_OBJECT_HANDLE *obj, struct sshkey **keyp, char **labelp)
914 {
915 	CK_ATTRIBUTE		 cert_attr[3];
916 	CK_SESSION_HANDLE	 session;
917 	CK_FUNCTION_LIST	*f = NULL;
918 	CK_RV			 rv;
919 	X509			*x509 = NULL;
920 	X509_NAME		*x509_name = NULL;
921 	EVP_PKEY		*evp;
922 	RSA			*rsa = NULL;
923 #ifdef OPENSSL_HAS_ECC
924 	EC_KEY			*ec = NULL;
925 #endif
926 	struct sshkey		*key = NULL;
927 	int			 i;
928 #ifdef HAVE_EC_KEY_METHOD_NEW
929 	int			 nid;
930 #endif
931 	const u_char		*cp;
932 	char			*subject = NULL;
933 
934 	*keyp = NULL;
935 	*labelp = NULL;
936 
937 	memset(&cert_attr, 0, sizeof(cert_attr));
938 	cert_attr[0].type = CKA_ID;
939 	cert_attr[1].type = CKA_SUBJECT;
940 	cert_attr[2].type = CKA_VALUE;
941 
942 	session = p->slotinfo[slotidx].session;
943 	f = p->function_list;
944 
945 	/* figure out size of the attributes */
946 	rv = f->C_GetAttributeValue(session, *obj, cert_attr, 3);
947 	if (rv != CKR_OK) {
948 		error("C_GetAttributeValue failed: %lu", rv);
949 		return -1;
950 	}
951 
952 	/*
953 	 * Allow CKA_ID (always first attribute) to be empty, but
954 	 * ensure that none of the others are zero length.
955 	 * XXX assumes CKA_ID is always first.
956 	 */
957 	if (cert_attr[1].ulValueLen == 0 ||
958 	    cert_attr[2].ulValueLen == 0) {
959 		error("invalid attribute length");
960 		return -1;
961 	}
962 
963 	/* allocate buffers for attributes */
964 	for (i = 0; i < 3; i++)
965 		if (cert_attr[i].ulValueLen > 0)
966 			cert_attr[i].pValue = xcalloc(1, cert_attr[i].ulValueLen);
967 
968 	/* retrieve ID, subject and value of certificate */
969 	rv = f->C_GetAttributeValue(session, *obj, cert_attr, 3);
970 	if (rv != CKR_OK) {
971 		error("C_GetAttributeValue failed: %lu", rv);
972 		goto out;
973 	}
974 
975 	/* Decode DER-encoded cert subject */
976 	cp = cert_attr[1].pValue;
977 	if ((x509_name = d2i_X509_NAME(NULL, &cp,
978 	    cert_attr[1].ulValueLen)) == NULL ||
979 	    (subject = X509_NAME_oneline(x509_name, NULL, 0)) == NULL)
980 		subject = xstrdup("invalid subject");
981 	X509_NAME_free(x509_name);
982 
983 	cp = cert_attr[2].pValue;
984 	if ((x509 = d2i_X509(NULL, &cp, cert_attr[2].ulValueLen)) == NULL) {
985 		error("d2i_x509 failed");
986 		goto out;
987 	}
988 
989 	if ((evp = X509_get_pubkey(x509)) == NULL) {
990 		error("X509_get_pubkey failed");
991 		goto out;
992 	}
993 
994 	if (EVP_PKEY_base_id(evp) == EVP_PKEY_RSA) {
995 		if (EVP_PKEY_get0_RSA(evp) == NULL) {
996 			error("invalid x509; no rsa key");
997 			goto out;
998 		}
999 		if ((rsa = RSAPublicKey_dup(EVP_PKEY_get0_RSA(evp))) == NULL) {
1000 			error("RSAPublicKey_dup failed");
1001 			goto out;
1002 		}
1003 
1004 		if (pkcs11_rsa_wrap(p, slotidx, &cert_attr[0], rsa))
1005 			goto out;
1006 
1007 		key = sshkey_new(KEY_UNSPEC);
1008 		if (key == NULL) {
1009 			error("sshkey_new failed");
1010 			goto out;
1011 		}
1012 
1013 		key->rsa = rsa;
1014 		key->type = KEY_RSA;
1015 		key->flags |= SSHKEY_FLAG_EXT;
1016 		rsa = NULL;	/* now owned by key */
1017 #ifdef HAVE_EC_KEY_METHOD_NEW
1018 	} else if (EVP_PKEY_base_id(evp) == EVP_PKEY_EC) {
1019 		if (EVP_PKEY_get0_EC_KEY(evp) == NULL) {
1020 			error("invalid x509; no ec key");
1021 			goto out;
1022 		}
1023 		if ((ec = EC_KEY_dup(EVP_PKEY_get0_EC_KEY(evp))) == NULL) {
1024 			error("EC_KEY_dup failed");
1025 			goto out;
1026 		}
1027 
1028 		nid = sshkey_ecdsa_key_to_nid(ec);
1029 		if (nid < 0) {
1030 			error("couldn't get curve nid");
1031 			goto out;
1032 		}
1033 
1034 		if (pkcs11_ecdsa_wrap(p, slotidx, &cert_attr[0], ec))
1035 			goto out;
1036 
1037 		key = sshkey_new(KEY_UNSPEC);
1038 		if (key == NULL) {
1039 			error("sshkey_new failed");
1040 			goto out;
1041 		}
1042 
1043 		key->ecdsa = ec;
1044 		key->ecdsa_nid = nid;
1045 		key->type = KEY_ECDSA;
1046 		key->flags |= SSHKEY_FLAG_EXT;
1047 		ec = NULL;	/* now owned by key */
1048 #endif /* HAVE_EC_KEY_METHOD_NEW */
1049 	} else {
1050 		error("unknown certificate key type");
1051 		goto out;
1052 	}
1053  out:
1054 	for (i = 0; i < 3; i++)
1055 		free(cert_attr[i].pValue);
1056 	X509_free(x509);
1057 	RSA_free(rsa);
1058 #ifdef OPENSSL_HAS_ECC
1059 	EC_KEY_free(ec);
1060 #endif
1061 	if (key == NULL) {
1062 		free(subject);
1063 		return -1;
1064 	}
1065 	/* success */
1066 	*keyp = key;
1067 	*labelp = subject;
1068 	return 0;
1069 }
1070 
1071 #if 0
1072 static int
1073 have_rsa_key(const RSA *rsa)
1074 {
1075 	const BIGNUM *rsa_n, *rsa_e;
1076 
1077 	RSA_get0_key(rsa, &rsa_n, &rsa_e, NULL);
1078 	return rsa_n != NULL && rsa_e != NULL;
1079 }
1080 #endif
1081 
1082 static void
1083 note_key(struct pkcs11_provider *p, CK_ULONG slotidx, const char *context,
1084     struct sshkey *key)
1085 {
1086 	char *fp;
1087 
1088 	if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
1089 	    SSH_FP_DEFAULT)) == NULL) {
1090 		error_f("sshkey_fingerprint failed");
1091 		return;
1092 	}
1093 	debug2("%s: provider %s slot %lu: %s %s", context, p->name,
1094 	    (u_long)slotidx, sshkey_type(key), fp);
1095 	free(fp);
1096 }
1097 
1098 /*
1099  * lookup certificates for token in slot identified by slotidx,
1100  * add 'wrapped' public keys to the 'keysp' array and increment nkeys.
1101  * keysp points to an (possibly empty) array with *nkeys keys.
1102  */
1103 static int
1104 pkcs11_fetch_certs(struct pkcs11_provider *p, CK_ULONG slotidx,
1105     struct sshkey ***keysp, char ***labelsp, int *nkeys)
1106 {
1107 	struct sshkey		*key = NULL;
1108 	CK_OBJECT_CLASS		 key_class;
1109 	CK_ATTRIBUTE		 key_attr[1];
1110 	CK_SESSION_HANDLE	 session;
1111 	CK_FUNCTION_LIST	*f = NULL;
1112 	CK_RV			 rv;
1113 	CK_OBJECT_HANDLE	 obj;
1114 	CK_ULONG		 n = 0;
1115 	int			 ret = -1;
1116 	char			*label;
1117 
1118 	memset(&key_attr, 0, sizeof(key_attr));
1119 	memset(&obj, 0, sizeof(obj));
1120 
1121 	key_class = CKO_CERTIFICATE;
1122 	key_attr[0].type = CKA_CLASS;
1123 	key_attr[0].pValue = &key_class;
1124 	key_attr[0].ulValueLen = sizeof(key_class);
1125 
1126 	session = p->slotinfo[slotidx].session;
1127 	f = p->function_list;
1128 
1129 	rv = f->C_FindObjectsInit(session, key_attr, 1);
1130 	if (rv != CKR_OK) {
1131 		error("C_FindObjectsInit failed: %lu", rv);
1132 		goto fail;
1133 	}
1134 
1135 	while (1) {
1136 		CK_CERTIFICATE_TYPE	ck_cert_type;
1137 
1138 		rv = f->C_FindObjects(session, &obj, 1, &n);
1139 		if (rv != CKR_OK) {
1140 			error("C_FindObjects failed: %lu", rv);
1141 			goto fail;
1142 		}
1143 		if (n == 0)
1144 			break;
1145 
1146 		memset(&ck_cert_type, 0, sizeof(ck_cert_type));
1147 		memset(&key_attr, 0, sizeof(key_attr));
1148 		key_attr[0].type = CKA_CERTIFICATE_TYPE;
1149 		key_attr[0].pValue = &ck_cert_type;
1150 		key_attr[0].ulValueLen = sizeof(ck_cert_type);
1151 
1152 		rv = f->C_GetAttributeValue(session, obj, key_attr, 1);
1153 		if (rv != CKR_OK) {
1154 			error("C_GetAttributeValue failed: %lu", rv);
1155 			goto fail;
1156 		}
1157 
1158 		key = NULL;
1159 		label = NULL;
1160 		switch (ck_cert_type) {
1161 		case CKC_X_509:
1162 			if (pkcs11_fetch_x509_pubkey(p, slotidx, &obj,
1163 			    &key, &label) != 0) {
1164 				error("failed to fetch key");
1165 				continue;
1166 			}
1167 			break;
1168 		default:
1169 			error("skipping unsupported certificate type %lu",
1170 			    ck_cert_type);
1171 			continue;
1172 		}
1173 		note_key(p, slotidx, __func__, key);
1174 		if (pkcs11_key_included(keysp, nkeys, key)) {
1175 			debug2_f("key already included");;
1176 			sshkey_free(key);
1177 		} else {
1178 			/* expand key array and add key */
1179 			*keysp = xrecallocarray(*keysp, *nkeys,
1180 			    *nkeys + 1, sizeof(struct sshkey *));
1181 			(*keysp)[*nkeys] = key;
1182 			if (labelsp != NULL) {
1183 				*labelsp = xrecallocarray(*labelsp, *nkeys,
1184 				    *nkeys + 1, sizeof(char *));
1185 				(*labelsp)[*nkeys] = xstrdup((char *)label);
1186 			}
1187 			*nkeys = *nkeys + 1;
1188 			debug("have %d keys", *nkeys);
1189 		}
1190 	}
1191 
1192 	ret = 0;
1193 fail:
1194 	rv = f->C_FindObjectsFinal(session);
1195 	if (rv != CKR_OK) {
1196 		error("C_FindObjectsFinal failed: %lu", rv);
1197 		ret = -1;
1198 	}
1199 
1200 	return (ret);
1201 }
1202 
1203 /*
1204  * lookup public keys for token in slot identified by slotidx,
1205  * add 'wrapped' public keys to the 'keysp' array and increment nkeys.
1206  * keysp points to an (possibly empty) array with *nkeys keys.
1207  */
1208 static int
1209 pkcs11_fetch_keys(struct pkcs11_provider *p, CK_ULONG slotidx,
1210     struct sshkey ***keysp, char ***labelsp, int *nkeys)
1211 {
1212 	struct sshkey		*key = NULL;
1213 	CK_OBJECT_CLASS		 key_class;
1214 	CK_ATTRIBUTE		 key_attr[2];
1215 	CK_SESSION_HANDLE	 session;
1216 	CK_FUNCTION_LIST	*f = NULL;
1217 	CK_RV			 rv;
1218 	CK_OBJECT_HANDLE	 obj;
1219 	CK_ULONG		 n = 0;
1220 	int			 ret = -1;
1221 
1222 	memset(&key_attr, 0, sizeof(key_attr));
1223 	memset(&obj, 0, sizeof(obj));
1224 
1225 	key_class = CKO_PUBLIC_KEY;
1226 	key_attr[0].type = CKA_CLASS;
1227 	key_attr[0].pValue = &key_class;
1228 	key_attr[0].ulValueLen = sizeof(key_class);
1229 
1230 	session = p->slotinfo[slotidx].session;
1231 	f = p->function_list;
1232 
1233 	rv = f->C_FindObjectsInit(session, key_attr, 1);
1234 	if (rv != CKR_OK) {
1235 		error("C_FindObjectsInit failed: %lu", rv);
1236 		goto fail;
1237 	}
1238 
1239 	while (1) {
1240 		CK_KEY_TYPE	ck_key_type;
1241 		CK_UTF8CHAR	label[256];
1242 
1243 		rv = f->C_FindObjects(session, &obj, 1, &n);
1244 		if (rv != CKR_OK) {
1245 			error("C_FindObjects failed: %lu", rv);
1246 			goto fail;
1247 		}
1248 		if (n == 0)
1249 			break;
1250 
1251 		memset(&ck_key_type, 0, sizeof(ck_key_type));
1252 		memset(&key_attr, 0, sizeof(key_attr));
1253 		key_attr[0].type = CKA_KEY_TYPE;
1254 		key_attr[0].pValue = &ck_key_type;
1255 		key_attr[0].ulValueLen = sizeof(ck_key_type);
1256 		key_attr[1].type = CKA_LABEL;
1257 		key_attr[1].pValue = &label;
1258 		key_attr[1].ulValueLen = sizeof(label) - 1;
1259 
1260 		rv = f->C_GetAttributeValue(session, obj, key_attr, 2);
1261 		if (rv != CKR_OK) {
1262 			error("C_GetAttributeValue failed: %lu", rv);
1263 			goto fail;
1264 		}
1265 
1266 		label[key_attr[1].ulValueLen] = '\0';
1267 
1268 		switch (ck_key_type) {
1269 		case CKK_RSA:
1270 			key = pkcs11_fetch_rsa_pubkey(p, slotidx, &obj);
1271 			break;
1272 #ifdef HAVE_EC_KEY_METHOD_NEW
1273 		case CKK_ECDSA:
1274 			key = pkcs11_fetch_ecdsa_pubkey(p, slotidx, &obj);
1275 			break;
1276 #endif /* HAVE_EC_KEY_METHOD_NEW */
1277 		default:
1278 			/* XXX print key type? */
1279 			key = NULL;
1280 			error("skipping unsupported key type");
1281 		}
1282 
1283 		if (key == NULL) {
1284 			error("failed to fetch key");
1285 			continue;
1286 		}
1287 		note_key(p, slotidx, __func__, key);
1288 		if (pkcs11_key_included(keysp, nkeys, key)) {
1289 			debug2_f("key already included");;
1290 			sshkey_free(key);
1291 		} else {
1292 			/* expand key array and add key */
1293 			*keysp = xrecallocarray(*keysp, *nkeys,
1294 			    *nkeys + 1, sizeof(struct sshkey *));
1295 			(*keysp)[*nkeys] = key;
1296 			if (labelsp != NULL) {
1297 				*labelsp = xrecallocarray(*labelsp, *nkeys,
1298 				    *nkeys + 1, sizeof(char *));
1299 				(*labelsp)[*nkeys] = xstrdup((char *)label);
1300 			}
1301 			*nkeys = *nkeys + 1;
1302 			debug("have %d keys", *nkeys);
1303 		}
1304 	}
1305 
1306 	ret = 0;
1307 fail:
1308 	rv = f->C_FindObjectsFinal(session);
1309 	if (rv != CKR_OK) {
1310 		error("C_FindObjectsFinal failed: %lu", rv);
1311 		ret = -1;
1312 	}
1313 
1314 	return (ret);
1315 }
1316 
1317 #ifdef WITH_PKCS11_KEYGEN
1318 #define FILL_ATTR(attr, idx, typ, val, len) \
1319 	{ (attr[idx]).type=(typ); (attr[idx]).pValue=(val); (attr[idx]).ulValueLen=len; idx++; }
1320 
1321 static struct sshkey *
1322 pkcs11_rsa_generate_private_key(struct pkcs11_provider *p, CK_ULONG slotidx,
1323     char *label, CK_ULONG bits, CK_BYTE keyid, u_int32_t *err)
1324 {
1325 	struct pkcs11_slotinfo	*si;
1326 	char			*plabel = label ? label : "";
1327 	int			 npub = 0, npriv = 0;
1328 	CK_RV			 rv;
1329 	CK_FUNCTION_LIST	*f;
1330 	CK_SESSION_HANDLE	 session;
1331 	CK_BBOOL		 true_val = CK_TRUE, false_val = CK_FALSE;
1332 	CK_OBJECT_HANDLE	 pubKey, privKey;
1333 	CK_ATTRIBUTE		 tpub[16], tpriv[16];
1334 	CK_MECHANISM		 mech = {
1335 	    CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0
1336 	};
1337 	CK_BYTE			 pubExponent[] = {
1338 	    0x01, 0x00, 0x01 /* RSA_F4 in bytes */
1339 	};
1340 	pubkey_filter[0].pValue = &pubkey_class;
1341 	cert_filter[0].pValue = &cert_class;
1342 
1343 	*err = 0;
1344 
1345 	FILL_ATTR(tpub, npub, CKA_TOKEN, &true_val, sizeof(true_val));
1346 	FILL_ATTR(tpub, npub, CKA_LABEL, plabel, strlen(plabel));
1347 	FILL_ATTR(tpub, npub, CKA_ENCRYPT, &false_val, sizeof(false_val));
1348 	FILL_ATTR(tpub, npub, CKA_VERIFY, &true_val, sizeof(true_val));
1349 	FILL_ATTR(tpub, npub, CKA_VERIFY_RECOVER, &false_val,
1350 	    sizeof(false_val));
1351 	FILL_ATTR(tpub, npub, CKA_WRAP, &false_val, sizeof(false_val));
1352 	FILL_ATTR(tpub, npub, CKA_DERIVE, &false_val, sizeof(false_val));
1353 	FILL_ATTR(tpub, npub, CKA_MODULUS_BITS, &bits, sizeof(bits));
1354 	FILL_ATTR(tpub, npub, CKA_PUBLIC_EXPONENT, pubExponent,
1355 	    sizeof(pubExponent));
1356 	FILL_ATTR(tpub, npub, CKA_ID, &keyid, sizeof(keyid));
1357 
1358 	FILL_ATTR(tpriv, npriv, CKA_TOKEN,  &true_val, sizeof(true_val));
1359 	FILL_ATTR(tpriv, npriv, CKA_LABEL,  plabel, strlen(plabel));
1360 	FILL_ATTR(tpriv, npriv, CKA_PRIVATE,  &true_val, sizeof(true_val));
1361 	FILL_ATTR(tpriv, npriv, CKA_SENSITIVE,  &true_val, sizeof(true_val));
1362 	FILL_ATTR(tpriv, npriv, CKA_DECRYPT,  &false_val, sizeof(false_val));
1363 	FILL_ATTR(tpriv, npriv, CKA_SIGN,  &true_val, sizeof(true_val));
1364 	FILL_ATTR(tpriv, npriv, CKA_SIGN_RECOVER,  &false_val,
1365 	    sizeof(false_val));
1366 	FILL_ATTR(tpriv, npriv, CKA_UNWRAP,  &false_val, sizeof(false_val));
1367 	FILL_ATTR(tpriv, npriv, CKA_DERIVE,  &false_val, sizeof(false_val));
1368 	FILL_ATTR(tpriv, npriv, CKA_ID, &keyid, sizeof(keyid));
1369 
1370 	f = p->function_list;
1371 	si = &p->slotinfo[slotidx];
1372 	session = si->session;
1373 
1374 	if ((rv = f->C_GenerateKeyPair(session, &mech, tpub, npub, tpriv, npriv,
1375 	    &pubKey, &privKey)) != CKR_OK) {
1376 		error_f("key generation failed: error 0x%lx", rv);
1377 		*err = rv;
1378 		return NULL;
1379 	}
1380 
1381 	return pkcs11_fetch_rsa_pubkey(p, slotidx, &pubKey);
1382 }
1383 
1384 static int
1385 pkcs11_decode_hex(const char *hex, unsigned char **dest, size_t *rlen)
1386 {
1387 	size_t	i, len;
1388 	char	ptr[3];
1389 
1390 	if (dest)
1391 		*dest = NULL;
1392 	if (rlen)
1393 		*rlen = 0;
1394 
1395 	if ((len = strlen(hex)) % 2)
1396 		return -1;
1397 	len /= 2;
1398 
1399 	*dest = xmalloc(len);
1400 
1401 	ptr[2] = '\0';
1402 	for (i = 0; i < len; i++) {
1403 		ptr[0] = hex[2 * i];
1404 		ptr[1] = hex[(2 * i) + 1];
1405 		if (!isxdigit(ptr[0]) || !isxdigit(ptr[1]))
1406 			return -1;
1407 		(*dest)[i] = (unsigned char)strtoul(ptr, NULL, 16);
1408 	}
1409 
1410 	if (rlen)
1411 		*rlen = len;
1412 
1413 	return 0;
1414 }
1415 
1416 static struct ec_curve_info {
1417 	const char	*name;
1418 	const char	*oid;
1419 	const char	*oid_encoded;
1420 	size_t		 size;
1421 } ec_curve_infos[] = {
1422 	{"prime256v1",	"1.2.840.10045.3.1.7",	"06082A8648CE3D030107", 256},
1423 	{"secp384r1",	"1.3.132.0.34",		"06052B81040022",	384},
1424 	{"secp521r1",	"1.3.132.0.35",		"06052B81040023",	521},
1425 	{NULL,		NULL,			NULL,			0},
1426 };
1427 
1428 static struct sshkey *
1429 pkcs11_ecdsa_generate_private_key(struct pkcs11_provider *p, CK_ULONG slotidx,
1430     char *label, CK_ULONG bits, CK_BYTE keyid, u_int32_t *err)
1431 {
1432 	struct pkcs11_slotinfo	*si;
1433 	char			*plabel = label ? label : "";
1434 	int			 i;
1435 	size_t			 ecparams_size;
1436 	unsigned char		*ecparams = NULL;
1437 	int			 npub = 0, npriv = 0;
1438 	CK_RV			 rv;
1439 	CK_FUNCTION_LIST	*f;
1440 	CK_SESSION_HANDLE	 session;
1441 	CK_BBOOL		 true_val = CK_TRUE, false_val = CK_FALSE;
1442 	CK_OBJECT_HANDLE	 pubKey, privKey;
1443 	CK_MECHANISM		 mech = {
1444 	    CKM_EC_KEY_PAIR_GEN, NULL_PTR, 0
1445 	};
1446 	CK_ATTRIBUTE		 tpub[16], tpriv[16];
1447 
1448 	*err = 0;
1449 
1450 	for (i = 0; ec_curve_infos[i].name; i++) {
1451 		if (ec_curve_infos[i].size == bits)
1452 			break;
1453 	}
1454 	if (!ec_curve_infos[i].name) {
1455 		error_f("invalid key size %lu", bits);
1456 		return NULL;
1457 	}
1458 	if (pkcs11_decode_hex(ec_curve_infos[i].oid_encoded, &ecparams,
1459 	    &ecparams_size) == -1) {
1460 		error_f("invalid oid");
1461 		return NULL;
1462 	}
1463 
1464 	FILL_ATTR(tpub, npub, CKA_TOKEN, &true_val, sizeof(true_val));
1465 	FILL_ATTR(tpub, npub, CKA_LABEL, plabel, strlen(plabel));
1466 	FILL_ATTR(tpub, npub, CKA_ENCRYPT, &false_val, sizeof(false_val));
1467 	FILL_ATTR(tpub, npub, CKA_VERIFY, &true_val, sizeof(true_val));
1468 	FILL_ATTR(tpub, npub, CKA_VERIFY_RECOVER, &false_val,
1469 	    sizeof(false_val));
1470 	FILL_ATTR(tpub, npub, CKA_WRAP, &false_val, sizeof(false_val));
1471 	FILL_ATTR(tpub, npub, CKA_DERIVE, &false_val, sizeof(false_val));
1472 	FILL_ATTR(tpub, npub, CKA_EC_PARAMS, ecparams, ecparams_size);
1473 	FILL_ATTR(tpub, npub, CKA_ID, &keyid, sizeof(keyid));
1474 
1475 	FILL_ATTR(tpriv, npriv, CKA_TOKEN, &true_val, sizeof(true_val));
1476 	FILL_ATTR(tpriv, npriv, CKA_LABEL, plabel, strlen(plabel));
1477 	FILL_ATTR(tpriv, npriv, CKA_PRIVATE, &true_val, sizeof(true_val));
1478 	FILL_ATTR(tpriv, npriv, CKA_SENSITIVE, &true_val, sizeof(true_val));
1479 	FILL_ATTR(tpriv, npriv, CKA_DECRYPT, &false_val, sizeof(false_val));
1480 	FILL_ATTR(tpriv, npriv, CKA_SIGN, &true_val, sizeof(true_val));
1481 	FILL_ATTR(tpriv, npriv, CKA_SIGN_RECOVER, &false_val,
1482 	    sizeof(false_val));
1483 	FILL_ATTR(tpriv, npriv, CKA_UNWRAP, &false_val, sizeof(false_val));
1484 	FILL_ATTR(tpriv, npriv, CKA_DERIVE, &false_val, sizeof(false_val));
1485 	FILL_ATTR(tpriv, npriv, CKA_ID, &keyid, sizeof(keyid));
1486 
1487 	f = p->function_list;
1488 	si = &p->slotinfo[slotidx];
1489 	session = si->session;
1490 
1491 	if ((rv = f->C_GenerateKeyPair(session, &mech, tpub, npub, tpriv, npriv,
1492 	    &pubKey, &privKey)) != CKR_OK) {
1493 		error_f("key generation failed: error 0x%lx", rv);
1494 		*err = rv;
1495 		return NULL;
1496 	}
1497 
1498 	return pkcs11_fetch_ecdsa_pubkey(p, slotidx, &pubKey);
1499 }
1500 #endif /* WITH_PKCS11_KEYGEN */
1501 
1502 /*
1503  * register a new provider, fails if provider already exists. if
1504  * keyp is provided, fetch keys.
1505  */
1506 static int
1507 pkcs11_register_provider(char *provider_id, char *pin,
1508     struct sshkey ***keyp, char ***labelsp,
1509     struct pkcs11_provider **providerp, CK_ULONG user)
1510 {
1511 	int nkeys, need_finalize = 0;
1512 	int ret = -1;
1513 	struct pkcs11_provider *p = NULL;
1514 	void *handle = NULL;
1515 	CK_RV (*getfunctionlist)(CK_FUNCTION_LIST **);
1516 	CK_RV rv;
1517 	CK_FUNCTION_LIST *f = NULL;
1518 	CK_TOKEN_INFO *token;
1519 	CK_ULONG i;
1520 
1521 	if (providerp == NULL)
1522 		goto fail;
1523 	*providerp = NULL;
1524 
1525 	if (keyp != NULL)
1526 		*keyp = NULL;
1527 	if (labelsp != NULL)
1528 		*labelsp = NULL;
1529 
1530 	if (pkcs11_provider_lookup(provider_id) != NULL) {
1531 		debug_f("provider already registered: %s", provider_id);
1532 		goto fail;
1533 	}
1534 	/* open shared pkcs11-library */
1535 	if ((handle = dlopen(provider_id, RTLD_NOW)) == NULL) {
1536 		error("dlopen %s failed: %s", provider_id, dlerror());
1537 		goto fail;
1538 	}
1539 	if ((getfunctionlist = dlsym(handle, "C_GetFunctionList")) == NULL) {
1540 		error("dlsym(C_GetFunctionList) failed: %s", dlerror());
1541 		goto fail;
1542 	}
1543 	p = xcalloc(1, sizeof(*p));
1544 	p->name = xstrdup(provider_id);
1545 	p->handle = handle;
1546 	/* setup the pkcs11 callbacks */
1547 	if ((rv = (*getfunctionlist)(&f)) != CKR_OK) {
1548 		error("C_GetFunctionList for provider %s failed: %lu",
1549 		    provider_id, rv);
1550 		goto fail;
1551 	}
1552 	p->function_list = f;
1553 	if ((rv = f->C_Initialize(NULL)) != CKR_OK) {
1554 		error("C_Initialize for provider %s failed: %lu",
1555 		    provider_id, rv);
1556 		goto fail;
1557 	}
1558 	need_finalize = 1;
1559 	if ((rv = f->C_GetInfo(&p->info)) != CKR_OK) {
1560 		error("C_GetInfo for provider %s failed: %lu",
1561 		    provider_id, rv);
1562 		goto fail;
1563 	}
1564 	rmspace(p->info.manufacturerID, sizeof(p->info.manufacturerID));
1565 	rmspace(p->info.libraryDescription, sizeof(p->info.libraryDescription));
1566 	debug("provider %s: manufacturerID <%s> cryptokiVersion %d.%d"
1567 	    " libraryDescription <%s> libraryVersion %d.%d",
1568 	    provider_id,
1569 	    p->info.manufacturerID,
1570 	    p->info.cryptokiVersion.major,
1571 	    p->info.cryptokiVersion.minor,
1572 	    p->info.libraryDescription,
1573 	    p->info.libraryVersion.major,
1574 	    p->info.libraryVersion.minor);
1575 	if ((rv = f->C_GetSlotList(CK_TRUE, NULL, &p->nslots)) != CKR_OK) {
1576 		error("C_GetSlotList failed: %lu", rv);
1577 		goto fail;
1578 	}
1579 	if (p->nslots == 0) {
1580 		debug_f("provider %s returned no slots", provider_id);
1581 		ret = -SSH_PKCS11_ERR_NO_SLOTS;
1582 		goto fail;
1583 	}
1584 	p->slotlist = xcalloc(p->nslots, sizeof(CK_SLOT_ID));
1585 	if ((rv = f->C_GetSlotList(CK_TRUE, p->slotlist, &p->nslots))
1586 	    != CKR_OK) {
1587 		error("C_GetSlotList for provider %s failed: %lu",
1588 		    provider_id, rv);
1589 		goto fail;
1590 	}
1591 	p->slotinfo = xcalloc(p->nslots, sizeof(struct pkcs11_slotinfo));
1592 	p->valid = 1;
1593 	nkeys = 0;
1594 	for (i = 0; i < p->nslots; i++) {
1595 		token = &p->slotinfo[i].token;
1596 		if ((rv = f->C_GetTokenInfo(p->slotlist[i], token))
1597 		    != CKR_OK) {
1598 			error("C_GetTokenInfo for provider %s slot %lu "
1599 			    "failed: %lu", provider_id, (u_long)i, rv);
1600 			continue;
1601 		}
1602 		if ((token->flags & CKF_TOKEN_INITIALIZED) == 0) {
1603 			debug2_f("ignoring uninitialised token in "
1604 			    "provider %s slot %lu", provider_id, (u_long)i);
1605 			continue;
1606 		}
1607 		rmspace(token->label, sizeof(token->label));
1608 		rmspace(token->manufacturerID, sizeof(token->manufacturerID));
1609 		rmspace(token->model, sizeof(token->model));
1610 		rmspace(token->serialNumber, sizeof(token->serialNumber));
1611 		debug("provider %s slot %lu: label <%s> manufacturerID <%s> "
1612 		    "model <%s> serial <%s> flags 0x%lx",
1613 		    provider_id, (unsigned long)i,
1614 		    token->label, token->manufacturerID, token->model,
1615 		    token->serialNumber, token->flags);
1616 		/*
1617 		 * open session, login with pin and retrieve public
1618 		 * keys (if keyp is provided)
1619 		 */
1620 		if ((ret = pkcs11_open_session(p, i, pin, user)) != 0 ||
1621 		    keyp == NULL)
1622 			continue;
1623 		pkcs11_fetch_keys(p, i, keyp, labelsp, &nkeys);
1624 		pkcs11_fetch_certs(p, i, keyp, labelsp, &nkeys);
1625 		if (nkeys == 0 && !p->slotinfo[i].logged_in &&
1626 		    pkcs11_interactive) {
1627 			/*
1628 			 * Some tokens require login before they will
1629 			 * expose keys.
1630 			 */
1631 			if (pkcs11_login_slot(p, &p->slotinfo[i],
1632 			    CKU_USER) < 0) {
1633 				error("login failed");
1634 				continue;
1635 			}
1636 			pkcs11_fetch_keys(p, i, keyp, labelsp, &nkeys);
1637 			pkcs11_fetch_certs(p, i, keyp, labelsp, &nkeys);
1638 		}
1639 	}
1640 
1641 	/* now owned by caller */
1642 	*providerp = p;
1643 
1644 	TAILQ_INSERT_TAIL(&pkcs11_providers, p, next);
1645 	p->refcount++;	/* add to provider list */
1646 
1647 	return (nkeys);
1648 fail:
1649 	if (need_finalize && (rv = f->C_Finalize(NULL)) != CKR_OK)
1650 		error("C_Finalize for provider %s failed: %lu",
1651 		    provider_id, rv);
1652 	if (p) {
1653 		free(p->name);
1654 		free(p->slotlist);
1655 		free(p->slotinfo);
1656 		free(p);
1657 	}
1658 	if (handle)
1659 		dlclose(handle);
1660 	if (ret > 0)
1661 		ret = -1;
1662 	return (ret);
1663 }
1664 
1665 /*
1666  * register a new provider and get number of keys hold by the token,
1667  * fails if provider already exists
1668  */
1669 int
1670 pkcs11_add_provider(char *provider_id, char *pin, struct sshkey ***keyp,
1671     char ***labelsp)
1672 {
1673 	struct pkcs11_provider *p = NULL;
1674 	int nkeys;
1675 
1676 	nkeys = pkcs11_register_provider(provider_id, pin, keyp, labelsp,
1677 	    &p, CKU_USER);
1678 
1679 	/* no keys found or some other error, de-register provider */
1680 	if (nkeys <= 0 && p != NULL) {
1681 		TAILQ_REMOVE(&pkcs11_providers, p, next);
1682 		pkcs11_provider_finalize(p);
1683 		pkcs11_provider_unref(p);
1684 	}
1685 	if (nkeys == 0)
1686 		debug_f("provider %s returned no keys", provider_id);
1687 
1688 	return (nkeys);
1689 }
1690 
1691 #ifdef WITH_PKCS11_KEYGEN
1692 struct sshkey *
1693 pkcs11_gakp(char *provider_id, char *pin, unsigned int slotidx, char *label,
1694     unsigned int type, unsigned int bits, unsigned char keyid, u_int32_t *err)
1695 {
1696 	struct pkcs11_provider	*p = NULL;
1697 	struct pkcs11_slotinfo	*si;
1698 	CK_FUNCTION_LIST	*f;
1699 	CK_SESSION_HANDLE	 session;
1700 	struct sshkey		*k = NULL;
1701 	int			 ret = -1, reset_pin = 0, reset_provider = 0;
1702 	CK_RV			 rv;
1703 
1704 	*err = 0;
1705 
1706 	if ((p = pkcs11_provider_lookup(provider_id)) != NULL)
1707 		debug_f("provider \"%s\" available", provider_id);
1708 	else if ((ret = pkcs11_register_provider(provider_id, pin, NULL, NULL,
1709 	    &p, CKU_SO)) < 0) {
1710 		debug_f("could not register provider %s", provider_id);
1711 		goto out;
1712 	} else
1713 		reset_provider = 1;
1714 
1715 	f = p->function_list;
1716 	si = &p->slotinfo[slotidx];
1717 	session = si->session;
1718 
1719 	if ((rv = f->C_SetOperationState(session , pin, strlen(pin),
1720 	    CK_INVALID_HANDLE, CK_INVALID_HANDLE)) != CKR_OK) {
1721 		debug_f("could not supply SO pin: %lu", rv);
1722 		reset_pin = 0;
1723 	} else
1724 		reset_pin = 1;
1725 
1726 	switch (type) {
1727 	case KEY_RSA:
1728 		if ((k = pkcs11_rsa_generate_private_key(p, slotidx, label,
1729 		    bits, keyid, err)) == NULL) {
1730 			debug_f("failed to generate RSA key");
1731 			goto out;
1732 		}
1733 		break;
1734 	case KEY_ECDSA:
1735 		if ((k = pkcs11_ecdsa_generate_private_key(p, slotidx, label,
1736 		    bits, keyid, err)) == NULL) {
1737 			debug_f("failed to generate ECDSA key");
1738 			goto out;
1739 		}
1740 		break;
1741 	default:
1742 		*err = SSH_PKCS11_ERR_GENERIC;
1743 		debug_f("unknown type %d", type);
1744 		goto out;
1745 	}
1746 
1747 out:
1748 	if (reset_pin)
1749 		f->C_SetOperationState(session , NULL, 0, CK_INVALID_HANDLE,
1750 		    CK_INVALID_HANDLE);
1751 
1752 	if (reset_provider)
1753 		pkcs11_del_provider(provider_id);
1754 
1755 	return (k);
1756 }
1757 
1758 struct sshkey *
1759 pkcs11_destroy_keypair(char *provider_id, char *pin, unsigned long slotidx,
1760     unsigned char keyid, u_int32_t *err)
1761 {
1762 	struct pkcs11_provider	*p = NULL;
1763 	struct pkcs11_slotinfo	*si;
1764 	struct sshkey		*k = NULL;
1765 	int			 reset_pin = 0, reset_provider = 0;
1766 	CK_ULONG		 nattrs;
1767 	CK_FUNCTION_LIST	*f;
1768 	CK_SESSION_HANDLE	 session;
1769 	CK_ATTRIBUTE		 attrs[16];
1770 	CK_OBJECT_CLASS		 key_class;
1771 	CK_KEY_TYPE		 key_type;
1772 	CK_OBJECT_HANDLE	 obj = CK_INVALID_HANDLE;
1773 	CK_RV			 rv;
1774 
1775 	*err = 0;
1776 
1777 	if ((p = pkcs11_provider_lookup(provider_id)) != NULL) {
1778 		debug_f("using provider \"%s\"", provider_id);
1779 	} else if (pkcs11_register_provider(provider_id, pin, NULL, NULL, &p,
1780 	    CKU_SO) < 0) {
1781 		debug_f("could not register provider %s",
1782 		    provider_id);
1783 		goto out;
1784 	} else
1785 		reset_provider = 1;
1786 
1787 	f = p->function_list;
1788 	si = &p->slotinfo[slotidx];
1789 	session = si->session;
1790 
1791 	if ((rv = f->C_SetOperationState(session , pin, strlen(pin),
1792 	    CK_INVALID_HANDLE, CK_INVALID_HANDLE)) != CKR_OK) {
1793 		debug_f("could not supply SO pin: %lu", rv);
1794 		reset_pin = 0;
1795 	} else
1796 		reset_pin = 1;
1797 
1798 	/* private key */
1799 	nattrs = 0;
1800 	key_class = CKO_PRIVATE_KEY;
1801 	FILL_ATTR(attrs, nattrs, CKA_CLASS, &key_class, sizeof(key_class));
1802 	FILL_ATTR(attrs, nattrs, CKA_ID, &keyid, sizeof(keyid));
1803 
1804 	if (pkcs11_find(p, slotidx, attrs, nattrs, &obj) == 0 &&
1805 	    obj != CK_INVALID_HANDLE) {
1806 		if ((rv = f->C_DestroyObject(session, obj)) != CKR_OK) {
1807 			debug_f("could not destroy private key 0x%hhx",
1808 			    keyid);
1809 			*err = rv;
1810 			goto out;
1811 		}
1812 	}
1813 
1814 	/* public key */
1815 	nattrs = 0;
1816 	key_class = CKO_PUBLIC_KEY;
1817 	FILL_ATTR(attrs, nattrs, CKA_CLASS, &key_class, sizeof(key_class));
1818 	FILL_ATTR(attrs, nattrs, CKA_ID, &keyid, sizeof(keyid));
1819 
1820 	if (pkcs11_find(p, slotidx, attrs, nattrs, &obj) == 0 &&
1821 	    obj != CK_INVALID_HANDLE) {
1822 
1823 		/* get key type */
1824 		nattrs = 0;
1825 		FILL_ATTR(attrs, nattrs, CKA_KEY_TYPE, &key_type,
1826 		    sizeof(key_type));
1827 		rv = f->C_GetAttributeValue(session, obj, attrs, nattrs);
1828 		if (rv != CKR_OK) {
1829 			debug_f("could not get key type of public key 0x%hhx",
1830 			    keyid);
1831 			*err = rv;
1832 			key_type = -1;
1833 		}
1834 		if (key_type == CKK_RSA)
1835 			k = pkcs11_fetch_rsa_pubkey(p, slotidx, &obj);
1836 		else if (key_type == CKK_ECDSA)
1837 			k = pkcs11_fetch_ecdsa_pubkey(p, slotidx, &obj);
1838 
1839 		if ((rv = f->C_DestroyObject(session, obj)) != CKR_OK) {
1840 			debug_f("could not destroy public key 0x%hhx", keyid);
1841 			*err = rv;
1842 			goto out;
1843 		}
1844 	}
1845 
1846 out:
1847 	if (reset_pin)
1848 		f->C_SetOperationState(session , NULL, 0, CK_INVALID_HANDLE,
1849 		    CK_INVALID_HANDLE);
1850 
1851 	if (reset_provider)
1852 		pkcs11_del_provider(provider_id);
1853 
1854 	return (k);
1855 }
1856 #endif /* WITH_PKCS11_KEYGEN */
1857 #else /* ENABLE_PKCS11 */
1858 
1859 #include <sys/types.h>
1860 #include <stdarg.h>
1861 #include <stdio.h>
1862 
1863 #include "log.h"
1864 #include "sshkey.h"
1865 
1866 int
1867 pkcs11_init(int interactive)
1868 {
1869 	error("%s: dlopen() not supported", __func__);
1870 	return (-1);
1871 }
1872 
1873 int
1874 pkcs11_add_provider(char *provider_id, char *pin, struct sshkey ***keyp,
1875     char ***labelsp)
1876 {
1877 	error("%s: dlopen() not supported", __func__);
1878 	return (-1);
1879 }
1880 
1881 void
1882 pkcs11_terminate(void)
1883 {
1884 	error("%s: dlopen() not supported", __func__);
1885 }
1886 #endif /* ENABLE_PKCS11 */
1887