blob: e074175bbb74fcffd97aa4e6bb458b154de60035 [file] [log] [blame]
djm@openbsd.orga71ba582015-05-27 05:15:02 +00001/* $OpenBSD: ssh-pkcs11.c,v 1.19 2015/05/27 05:15:02 djm Exp $ */
Damien Miller7ea845e2010-02-12 09:21:02 +11002/*
3 * Copyright (c) 2010 Markus Friedl. All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
Damien Miller8ad0fbd2010-02-12 09:49:06 +110018#include "includes.h"
19
Damien Millerdfa41562010-02-12 10:06:28 +110020#ifdef ENABLE_PKCS11
21
Damien Miller7ea845e2010-02-12 09:21:02 +110022#include <sys/types.h>
Damien Miller8ad0fbd2010-02-12 09:49:06 +110023#ifdef HAVE_SYS_TIME_H
24# include <sys/time.h>
25#endif
Damien Miller7ea845e2010-02-12 09:21:02 +110026#include <stdarg.h>
27#include <stdio.h>
28
29#include <string.h>
30#include <dlfcn.h>
31
Damien Miller8ad0fbd2010-02-12 09:49:06 +110032#include "openbsd-compat/sys-queue.h"
33
Damien Millerd2252c72013-11-04 07:41:48 +110034#include <openssl/x509.h>
35
Damien Miller7ea845e2010-02-12 09:21:02 +110036#define CRYPTOKI_COMPAT
37#include "pkcs11.h"
38
39#include "log.h"
40#include "misc.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000041#include "sshkey.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110042#include "ssh-pkcs11.h"
43#include "xmalloc.h"
44
45struct pkcs11_slotinfo {
46 CK_TOKEN_INFO token;
47 CK_SESSION_HANDLE session;
48 int logged_in;
49};
50
51struct pkcs11_provider {
52 char *name;
53 void *handle;
54 CK_FUNCTION_LIST *function_list;
55 CK_INFO info;
56 CK_ULONG nslots;
57 CK_SLOT_ID *slotlist;
58 struct pkcs11_slotinfo *slotinfo;
59 int valid;
60 int refcount;
61 TAILQ_ENTRY(pkcs11_provider) next;
62};
63
64TAILQ_HEAD(, pkcs11_provider) pkcs11_providers;
65
66struct pkcs11_key {
67 struct pkcs11_provider *provider;
68 CK_ULONG slotidx;
69 int (*orig_finish)(RSA *rsa);
70 RSA_METHOD rsa_method;
71 char *keyid;
72 int keyid_len;
73};
74
75int pkcs11_interactive = 0;
76
77int
78pkcs11_init(int interactive)
79{
80 pkcs11_interactive = interactive;
81 TAILQ_INIT(&pkcs11_providers);
82 return (0);
83}
84
85/*
86 * finalize a provider shared libarary, it's no longer usable.
87 * however, there might still be keys referencing this provider,
88 * so the actuall freeing of memory is handled by pkcs11_provider_unref().
89 * this is called when a provider gets unregistered.
90 */
91static void
92pkcs11_provider_finalize(struct pkcs11_provider *p)
93{
94 CK_RV rv;
95 CK_ULONG i;
96
97 debug("pkcs11_provider_finalize: %p refcount %d valid %d",
98 p, p->refcount, p->valid);
99 if (!p->valid)
100 return;
101 for (i = 0; i < p->nslots; i++) {
102 if (p->slotinfo[i].session &&
103 (rv = p->function_list->C_CloseSession(
104 p->slotinfo[i].session)) != CKR_OK)
105 error("C_CloseSession failed: %lu", rv);
106 }
107 if ((rv = p->function_list->C_Finalize(NULL)) != CKR_OK)
108 error("C_Finalize failed: %lu", rv);
109 p->valid = 0;
110 p->function_list = NULL;
111 dlclose(p->handle);
112}
113
114/*
115 * remove a reference to the provider.
116 * called when a key gets destroyed or when the provider is unregistered.
117 */
118static void
119pkcs11_provider_unref(struct pkcs11_provider *p)
120{
121 debug("pkcs11_provider_unref: %p refcount %d", p, p->refcount);
122 if (--p->refcount <= 0) {
123 if (p->valid)
124 error("pkcs11_provider_unref: %p still valid", p);
Darren Tuckera627d422013-06-02 07:31:17 +1000125 free(p->slotlist);
126 free(p->slotinfo);
127 free(p);
Damien Miller7ea845e2010-02-12 09:21:02 +1100128 }
129}
130
131/* unregister all providers, keys might still point to the providers */
132void
133pkcs11_terminate(void)
134{
135 struct pkcs11_provider *p;
136
137 while ((p = TAILQ_FIRST(&pkcs11_providers)) != NULL) {
138 TAILQ_REMOVE(&pkcs11_providers, p, next);
139 pkcs11_provider_finalize(p);
140 pkcs11_provider_unref(p);
141 }
142}
143
144/* lookup provider by name */
145static struct pkcs11_provider *
146pkcs11_provider_lookup(char *provider_id)
147{
148 struct pkcs11_provider *p;
149
150 TAILQ_FOREACH(p, &pkcs11_providers, next) {
151 debug("check %p %s", p, p->name);
152 if (!strcmp(provider_id, p->name))
153 return (p);
154 }
155 return (NULL);
156}
157
158/* unregister provider by name */
159int
160pkcs11_del_provider(char *provider_id)
161{
162 struct pkcs11_provider *p;
163
164 if ((p = pkcs11_provider_lookup(provider_id)) != NULL) {
165 TAILQ_REMOVE(&pkcs11_providers, p, next);
166 pkcs11_provider_finalize(p);
167 pkcs11_provider_unref(p);
168 return (0);
169 }
170 return (-1);
171}
172
173/* openssl callback for freeing an RSA key */
174static int
175pkcs11_rsa_finish(RSA *rsa)
176{
177 struct pkcs11_key *k11;
178 int rv = -1;
179
180 if ((k11 = RSA_get_app_data(rsa)) != NULL) {
181 if (k11->orig_finish)
182 rv = k11->orig_finish(rsa);
183 if (k11->provider)
184 pkcs11_provider_unref(k11->provider);
Darren Tuckera627d422013-06-02 07:31:17 +1000185 free(k11->keyid);
186 free(k11);
Damien Miller7ea845e2010-02-12 09:21:02 +1100187 }
188 return (rv);
189}
190
Damien Miller031c9102010-04-16 15:54:44 +1000191/* find a single 'obj' for given attributes */
192static int
193pkcs11_find(struct pkcs11_provider *p, CK_ULONG slotidx, CK_ATTRIBUTE *attr,
194 CK_ULONG nattr, CK_OBJECT_HANDLE *obj)
195{
196 CK_FUNCTION_LIST *f;
197 CK_SESSION_HANDLE session;
198 CK_ULONG nfound = 0;
199 CK_RV rv;
200 int ret = -1;
201
202 f = p->function_list;
203 session = p->slotinfo[slotidx].session;
204 if ((rv = f->C_FindObjectsInit(session, attr, nattr)) != CKR_OK) {
205 error("C_FindObjectsInit failed (nattr %lu): %lu", nattr, rv);
206 return (-1);
207 }
208 if ((rv = f->C_FindObjects(session, obj, 1, &nfound)) != CKR_OK ||
209 nfound != 1) {
210 debug("C_FindObjects failed (nfound %lu nattr %lu): %lu",
211 nfound, nattr, rv);
212 } else
213 ret = 0;
214 if ((rv = f->C_FindObjectsFinal(session)) != CKR_OK)
215 error("C_FindObjectsFinal failed: %lu", rv);
216 return (ret);
217}
218
Damien Miller7ea845e2010-02-12 09:21:02 +1100219/* openssl callback doing the actual signing operation */
220static int
221pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
222 int padding)
223{
224 struct pkcs11_key *k11;
225 struct pkcs11_slotinfo *si;
226 CK_FUNCTION_LIST *f;
227 CK_OBJECT_HANDLE obj;
Damien Miller031c9102010-04-16 15:54:44 +1000228 CK_ULONG tlen = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100229 CK_RV rv;
Damien Millerd2252c72013-11-04 07:41:48 +1100230 CK_OBJECT_CLASS private_key_class = CKO_PRIVATE_KEY;
Damien Miller8ad0fbd2010-02-12 09:49:06 +1100231 CK_BBOOL true_val = CK_TRUE;
Damien Miller7ea845e2010-02-12 09:21:02 +1100232 CK_MECHANISM mech = {
233 CKM_RSA_PKCS, NULL_PTR, 0
234 };
235 CK_ATTRIBUTE key_filter[] = {
Damien Miller61c5c232013-11-07 11:34:14 +1100236 {CKA_CLASS, NULL, sizeof(private_key_class) },
Damien Miller7ea845e2010-02-12 09:21:02 +1100237 {CKA_ID, NULL, 0},
Damien Miller61c5c232013-11-07 11:34:14 +1100238 {CKA_SIGN, NULL, sizeof(true_val) }
Damien Miller7ea845e2010-02-12 09:21:02 +1100239 };
djm@openbsd.orga71ba582015-05-27 05:15:02 +0000240 char *pin = NULL, prompt[1024];
Damien Miller7ea845e2010-02-12 09:21:02 +1100241 int rval = -1;
242
Damien Miller61c5c232013-11-07 11:34:14 +1100243 key_filter[0].pValue = &private_key_class;
244 key_filter[2].pValue = &true_val;
245
Damien Miller7ea845e2010-02-12 09:21:02 +1100246 if ((k11 = RSA_get_app_data(rsa)) == NULL) {
247 error("RSA_get_app_data failed for rsa %p", rsa);
248 return (-1);
249 }
250 if (!k11->provider || !k11->provider->valid) {
251 error("no pkcs11 (valid) provider for rsa %p", rsa);
252 return (-1);
253 }
254 f = k11->provider->function_list;
255 si = &k11->provider->slotinfo[k11->slotidx];
256 if ((si->token.flags & CKF_LOGIN_REQUIRED) && !si->logged_in) {
257 if (!pkcs11_interactive) {
djm@openbsd.orga71ba582015-05-27 05:15:02 +0000258 error("need pin entry%s", (si->token.flags &
259 CKF_PROTECTED_AUTHENTICATION_PATH) ?
260 " on reader keypad" : "");
Damien Miller7ea845e2010-02-12 09:21:02 +1100261 return (-1);
262 }
djm@openbsd.orga71ba582015-05-27 05:15:02 +0000263 if (si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH)
264 verbose("Deferring PIN entry to reader keypad.");
265 else {
266 snprintf(prompt, sizeof(prompt),
267 "Enter PIN for '%s': ", si->token.label);
268 pin = read_passphrase(prompt, RP_ALLOW_EOF);
269 if (pin == NULL)
270 return (-1); /* bail out */
271 }
272 rv = f->C_Login(si->session, CKU_USER, (u_char *)pin,
273 (pin != NULL) ? strlen(pin) : 0);
274 if (pin != NULL) {
275 explicit_bzero(pin, strlen(pin));
Darren Tuckera627d422013-06-02 07:31:17 +1000276 free(pin);
djm@openbsd.orga71ba582015-05-27 05:15:02 +0000277 }
278 if (rv != CKR_OK && rv != CKR_USER_ALREADY_LOGGED_IN) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100279 error("C_Login failed: %lu", rv);
280 return (-1);
281 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100282 si->logged_in = 1;
283 }
284 key_filter[1].pValue = k11->keyid;
285 key_filter[1].ulValueLen = k11->keyid_len;
Damien Miller031c9102010-04-16 15:54:44 +1000286 /* try to find object w/CKA_SIGN first, retry w/o */
287 if (pkcs11_find(k11->provider, k11->slotidx, key_filter, 3, &obj) < 0 &&
288 pkcs11_find(k11->provider, k11->slotidx, key_filter, 2, &obj) < 0) {
289 error("cannot find private key");
Damien Miller7ea845e2010-02-12 09:21:02 +1100290 } else if ((rv = f->C_SignInit(si->session, &mech, obj)) != CKR_OK) {
291 error("C_SignInit failed: %lu", rv);
292 } else {
293 /* XXX handle CKR_BUFFER_TOO_SMALL */
294 tlen = RSA_size(rsa);
295 rv = f->C_Sign(si->session, (CK_BYTE *)from, flen, to, &tlen);
296 if (rv == CKR_OK)
297 rval = tlen;
298 else
299 error("C_Sign failed: %lu", rv);
300 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100301 return (rval);
302}
303
304static int
305pkcs11_rsa_private_decrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
306 int padding)
307{
308 return (-1);
309}
310
311/* redirect private key operations for rsa key to pkcs11 token */
312static int
313pkcs11_rsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
314 CK_ATTRIBUTE *keyid_attrib, RSA *rsa)
315{
316 struct pkcs11_key *k11;
317 const RSA_METHOD *def = RSA_get_default_method();
318
319 k11 = xcalloc(1, sizeof(*k11));
320 k11->provider = provider;
321 provider->refcount++; /* provider referenced by RSA key */
322 k11->slotidx = slotidx;
323 /* identify key object on smartcard */
324 k11->keyid_len = keyid_attrib->ulValueLen;
325 k11->keyid = xmalloc(k11->keyid_len);
326 memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
327 k11->orig_finish = def->finish;
328 memcpy(&k11->rsa_method, def, sizeof(k11->rsa_method));
329 k11->rsa_method.name = "pkcs11";
330 k11->rsa_method.rsa_priv_enc = pkcs11_rsa_private_encrypt;
331 k11->rsa_method.rsa_priv_dec = pkcs11_rsa_private_decrypt;
332 k11->rsa_method.finish = pkcs11_rsa_finish;
333 RSA_set_method(rsa, &k11->rsa_method);
334 RSA_set_app_data(rsa, k11);
335 return (0);
336}
337
338/* remove trailing spaces */
339static void
Damien Miller746d1a62013-07-18 16:13:02 +1000340rmspace(u_char *buf, size_t len)
Damien Miller7ea845e2010-02-12 09:21:02 +1100341{
342 size_t i;
343
344 if (!len)
345 return;
346 for (i = len - 1; i > 0; i--)
347 if (i == len - 1 || buf[i] == ' ')
348 buf[i] = '\0';
349 else
350 break;
351}
352
353/*
354 * open a pkcs11 session and login if required.
355 * if pin == NULL we delay login until key use
356 */
357static int
358pkcs11_open_session(struct pkcs11_provider *p, CK_ULONG slotidx, char *pin)
359{
360 CK_RV rv;
361 CK_FUNCTION_LIST *f;
362 CK_SESSION_HANDLE session;
363 int login_required;
364
365 f = p->function_list;
366 login_required = p->slotinfo[slotidx].token.flags & CKF_LOGIN_REQUIRED;
367 if (pin && login_required && !strlen(pin)) {
368 error("pin required");
369 return (-1);
370 }
371 if ((rv = f->C_OpenSession(p->slotlist[slotidx], CKF_RW_SESSION|
372 CKF_SERIAL_SESSION, NULL, NULL, &session))
373 != CKR_OK) {
374 error("C_OpenSession failed: %lu", rv);
375 return (-1);
376 }
377 if (login_required && pin) {
djm@openbsd.orgcb3bde32015-02-02 22:48:53 +0000378 rv = f->C_Login(session, CKU_USER,
deraadt@openbsd.orgce4f59b2015-02-03 08:07:20 +0000379 (u_char *)pin, strlen(pin));
djm@openbsd.orgcb3bde32015-02-02 22:48:53 +0000380 if (rv != CKR_OK && rv != CKR_USER_ALREADY_LOGGED_IN) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100381 error("C_Login failed: %lu", rv);
382 if ((rv = f->C_CloseSession(session)) != CKR_OK)
383 error("C_CloseSession failed: %lu", rv);
384 return (-1);
385 }
386 p->slotinfo[slotidx].logged_in = 1;
387 }
388 p->slotinfo[slotidx].session = session;
389 return (0);
390}
391
392/*
393 * lookup public keys for token in slot identified by slotidx,
394 * add 'wrapped' public keys to the 'keysp' array and increment nkeys.
395 * keysp points to an (possibly empty) array with *nkeys keys.
396 */
Damien Millerd2252c72013-11-04 07:41:48 +1100397static int pkcs11_fetch_keys_filter(struct pkcs11_provider *, CK_ULONG,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000398 CK_ATTRIBUTE [], CK_ATTRIBUTE [3], struct sshkey ***, int *)
Damien Miller686c7d92014-05-15 14:37:03 +1000399 __attribute__((__bounded__(__minbytes__,4, 3 * sizeof(CK_ATTRIBUTE))));
Damien Millerd2252c72013-11-04 07:41:48 +1100400
Damien Miller7ea845e2010-02-12 09:21:02 +1100401static int
Damien Millerd2252c72013-11-04 07:41:48 +1100402pkcs11_fetch_keys(struct pkcs11_provider *p, CK_ULONG slotidx,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000403 struct sshkey ***keysp, int *nkeys)
Damien Millerd2252c72013-11-04 07:41:48 +1100404{
405 CK_OBJECT_CLASS pubkey_class = CKO_PUBLIC_KEY;
406 CK_OBJECT_CLASS cert_class = CKO_CERTIFICATE;
407 CK_ATTRIBUTE pubkey_filter[] = {
Damien Miller61c5c232013-11-07 11:34:14 +1100408 { CKA_CLASS, NULL, sizeof(pubkey_class) }
Damien Millerd2252c72013-11-04 07:41:48 +1100409 };
410 CK_ATTRIBUTE cert_filter[] = {
Damien Miller61c5c232013-11-07 11:34:14 +1100411 { CKA_CLASS, NULL, sizeof(cert_class) }
Damien Millerd2252c72013-11-04 07:41:48 +1100412 };
413 CK_ATTRIBUTE pubkey_attribs[] = {
414 { CKA_ID, NULL, 0 },
415 { CKA_MODULUS, NULL, 0 },
416 { CKA_PUBLIC_EXPONENT, NULL, 0 }
417 };
418 CK_ATTRIBUTE cert_attribs[] = {
419 { CKA_ID, NULL, 0 },
420 { CKA_SUBJECT, NULL, 0 },
421 { CKA_VALUE, NULL, 0 }
422 };
Damien Miller61c5c232013-11-07 11:34:14 +1100423 pubkey_filter[0].pValue = &pubkey_class;
424 cert_filter[0].pValue = &cert_class;
Damien Millerd2252c72013-11-04 07:41:48 +1100425
426 if (pkcs11_fetch_keys_filter(p, slotidx, pubkey_filter, pubkey_attribs,
427 keysp, nkeys) < 0 ||
428 pkcs11_fetch_keys_filter(p, slotidx, cert_filter, cert_attribs,
429 keysp, nkeys) < 0)
430 return (-1);
431 return (0);
432}
433
434static int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000435pkcs11_key_included(struct sshkey ***keysp, int *nkeys, struct sshkey *key)
Damien Millerd2252c72013-11-04 07:41:48 +1100436{
437 int i;
438
439 for (i = 0; i < *nkeys; i++)
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000440 if (sshkey_equal(key, (*keysp)[i]))
Damien Millerd2252c72013-11-04 07:41:48 +1100441 return (1);
442 return (0);
443}
444
445static int
446pkcs11_fetch_keys_filter(struct pkcs11_provider *p, CK_ULONG slotidx,
447 CK_ATTRIBUTE filter[], CK_ATTRIBUTE attribs[3],
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000448 struct sshkey ***keysp, int *nkeys)
Damien Miller7ea845e2010-02-12 09:21:02 +1100449{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000450 struct sshkey *key;
Damien Miller7ea845e2010-02-12 09:21:02 +1100451 RSA *rsa;
Damien Millerd2252c72013-11-04 07:41:48 +1100452 X509 *x509;
453 EVP_PKEY *evp;
Damien Miller7ea845e2010-02-12 09:21:02 +1100454 int i;
Damien Millerd2252c72013-11-04 07:41:48 +1100455 const u_char *cp;
Damien Miller7ea845e2010-02-12 09:21:02 +1100456 CK_RV rv;
457 CK_OBJECT_HANDLE obj;
458 CK_ULONG nfound;
459 CK_SESSION_HANDLE session;
460 CK_FUNCTION_LIST *f;
Tim Rice179eee02010-03-04 12:48:05 -0800461
Damien Miller7ea845e2010-02-12 09:21:02 +1100462 f = p->function_list;
463 session = p->slotinfo[slotidx].session;
464 /* setup a filter the looks for public keys */
Damien Millerd2252c72013-11-04 07:41:48 +1100465 if ((rv = f->C_FindObjectsInit(session, filter, 1)) != CKR_OK) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100466 error("C_FindObjectsInit failed: %lu", rv);
467 return (-1);
468 }
469 while (1) {
470 /* XXX 3 attributes in attribs[] */
471 for (i = 0; i < 3; i++) {
472 attribs[i].pValue = NULL;
473 attribs[i].ulValueLen = 0;
474 }
475 if ((rv = f->C_FindObjects(session, &obj, 1, &nfound)) != CKR_OK
476 || nfound == 0)
477 break;
478 /* found a key, so figure out size of the attributes */
479 if ((rv = f->C_GetAttributeValue(session, obj, attribs, 3))
480 != CKR_OK) {
481 error("C_GetAttributeValue failed: %lu", rv);
482 continue;
483 }
Damien Miller4fe686d2010-06-26 09:36:10 +1000484 /* check that none of the attributes are zero length */
485 if (attribs[0].ulValueLen == 0 ||
486 attribs[1].ulValueLen == 0 ||
487 attribs[2].ulValueLen == 0) {
488 continue;
489 }
490 /* allocate buffers for attributes */
Damien Miller7ea845e2010-02-12 09:21:02 +1100491 for (i = 0; i < 3; i++)
492 attribs[i].pValue = xmalloc(attribs[i].ulValueLen);
Damien Millerd2252c72013-11-04 07:41:48 +1100493 /*
494 * retrieve ID, modulus and public exponent of RSA key,
495 * or ID, subject and value for certificates.
496 */
497 rsa = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100498 if ((rv = f->C_GetAttributeValue(session, obj, attribs, 3))
499 != CKR_OK) {
500 error("C_GetAttributeValue failed: %lu", rv);
Damien Millerd2252c72013-11-04 07:41:48 +1100501 } else if (attribs[1].type == CKA_MODULUS ) {
502 if ((rsa = RSA_new()) == NULL) {
503 error("RSA_new failed");
504 } else {
505 rsa->n = BN_bin2bn(attribs[1].pValue,
506 attribs[1].ulValueLen, NULL);
507 rsa->e = BN_bin2bn(attribs[2].pValue,
508 attribs[2].ulValueLen, NULL);
509 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100510 } else {
Damien Millerd2252c72013-11-04 07:41:48 +1100511 cp = attribs[2].pValue;
512 if ((x509 = X509_new()) == NULL) {
513 error("X509_new failed");
514 } else if (d2i_X509(&x509, &cp, attribs[2].ulValueLen)
515 == NULL) {
516 error("d2i_X509 failed");
517 } else if ((evp = X509_get_pubkey(x509)) == NULL ||
518 evp->type != EVP_PKEY_RSA ||
519 evp->pkey.rsa == NULL) {
520 debug("X509_get_pubkey failed or no rsa");
521 } else if ((rsa = RSAPublicKey_dup(evp->pkey.rsa))
522 == NULL) {
523 error("RSAPublicKey_dup");
524 }
525 if (x509)
526 X509_free(x509);
527 }
528 if (rsa && rsa->n && rsa->e &&
529 pkcs11_rsa_wrap(p, slotidx, &attribs[0], rsa) == 0) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000530 key = sshkey_new(KEY_UNSPEC);
Damien Millerd2252c72013-11-04 07:41:48 +1100531 key->rsa = rsa;
532 key->type = KEY_RSA;
Damien Miller86687062014-07-02 15:28:02 +1000533 key->flags |= SSHKEY_FLAG_EXT;
Damien Millerd2252c72013-11-04 07:41:48 +1100534 if (pkcs11_key_included(keysp, nkeys, key)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000535 sshkey_free(key);
Damien Millerd2252c72013-11-04 07:41:48 +1100536 } else {
Damien Miller7ea845e2010-02-12 09:21:02 +1100537 /* expand key array and add key */
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000538 *keysp = xreallocarray(*keysp, *nkeys + 1,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000539 sizeof(struct sshkey *));
Damien Miller7ea845e2010-02-12 09:21:02 +1100540 (*keysp)[*nkeys] = key;
541 *nkeys = *nkeys + 1;
542 debug("have %d keys", *nkeys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100543 }
Damien Millerd2252c72013-11-04 07:41:48 +1100544 } else if (rsa) {
545 RSA_free(rsa);
Damien Miller7ea845e2010-02-12 09:21:02 +1100546 }
547 for (i = 0; i < 3; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000548 free(attribs[i].pValue);
Damien Miller7ea845e2010-02-12 09:21:02 +1100549 }
550 if ((rv = f->C_FindObjectsFinal(session)) != CKR_OK)
551 error("C_FindObjectsFinal failed: %lu", rv);
552 return (0);
553}
554
555/* register a new provider, fails if provider already exists */
556int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000557pkcs11_add_provider(char *provider_id, char *pin, struct sshkey ***keyp)
Damien Miller7ea845e2010-02-12 09:21:02 +1100558{
559 int nkeys, need_finalize = 0;
560 struct pkcs11_provider *p = NULL;
561 void *handle = NULL;
562 CK_RV (*getfunctionlist)(CK_FUNCTION_LIST **);
563 CK_RV rv;
564 CK_FUNCTION_LIST *f = NULL;
565 CK_TOKEN_INFO *token;
566 CK_ULONG i;
567
568 *keyp = NULL;
569 if (pkcs11_provider_lookup(provider_id) != NULL) {
570 error("provider already registered: %s", provider_id);
571 goto fail;
572 }
573 /* open shared pkcs11-libarary */
574 if ((handle = dlopen(provider_id, RTLD_NOW)) == NULL) {
575 error("dlopen %s failed: %s", provider_id, dlerror());
576 goto fail;
577 }
578 if ((getfunctionlist = dlsym(handle, "C_GetFunctionList")) == NULL) {
579 error("dlsym(C_GetFunctionList) failed: %s", dlerror());
580 goto fail;
581 }
582 p = xcalloc(1, sizeof(*p));
583 p->name = xstrdup(provider_id);
584 p->handle = handle;
585 /* setup the pkcs11 callbacks */
586 if ((rv = (*getfunctionlist)(&f)) != CKR_OK) {
587 error("C_GetFunctionList failed: %lu", rv);
588 goto fail;
589 }
590 p->function_list = f;
591 if ((rv = f->C_Initialize(NULL)) != CKR_OK) {
592 error("C_Initialize failed: %lu", rv);
593 goto fail;
594 }
595 need_finalize = 1;
596 if ((rv = f->C_GetInfo(&p->info)) != CKR_OK) {
597 error("C_GetInfo failed: %lu", rv);
598 goto fail;
599 }
600 rmspace(p->info.manufacturerID, sizeof(p->info.manufacturerID));
601 rmspace(p->info.libraryDescription, sizeof(p->info.libraryDescription));
602 debug("manufacturerID <%s> cryptokiVersion %d.%d"
603 " libraryDescription <%s> libraryVersion %d.%d",
604 p->info.manufacturerID,
605 p->info.cryptokiVersion.major,
606 p->info.cryptokiVersion.minor,
607 p->info.libraryDescription,
608 p->info.libraryVersion.major,
609 p->info.libraryVersion.minor);
610 if ((rv = f->C_GetSlotList(CK_TRUE, NULL, &p->nslots)) != CKR_OK) {
611 error("C_GetSlotList failed: %lu", rv);
612 goto fail;
613 }
614 if (p->nslots == 0) {
615 error("no slots");
616 goto fail;
617 }
618 p->slotlist = xcalloc(p->nslots, sizeof(CK_SLOT_ID));
619 if ((rv = f->C_GetSlotList(CK_TRUE, p->slotlist, &p->nslots))
620 != CKR_OK) {
621 error("C_GetSlotList failed: %lu", rv);
622 goto fail;
623 }
624 p->slotinfo = xcalloc(p->nslots, sizeof(struct pkcs11_slotinfo));
625 p->valid = 1;
626 nkeys = 0;
627 for (i = 0; i < p->nslots; i++) {
628 token = &p->slotinfo[i].token;
629 if ((rv = f->C_GetTokenInfo(p->slotlist[i], token))
630 != CKR_OK) {
631 error("C_GetTokenInfo failed: %lu", rv);
632 continue;
633 }
634 rmspace(token->label, sizeof(token->label));
635 rmspace(token->manufacturerID, sizeof(token->manufacturerID));
636 rmspace(token->model, sizeof(token->model));
637 rmspace(token->serialNumber, sizeof(token->serialNumber));
638 debug("label <%s> manufacturerID <%s> model <%s> serial <%s>"
639 " flags 0x%lx",
640 token->label, token->manufacturerID, token->model,
641 token->serialNumber, token->flags);
642 /* open session, login with pin and retrieve public keys */
643 if (pkcs11_open_session(p, i, pin) == 0)
644 pkcs11_fetch_keys(p, i, keyp, &nkeys);
645 }
646 if (nkeys > 0) {
647 TAILQ_INSERT_TAIL(&pkcs11_providers, p, next);
648 p->refcount++; /* add to provider list */
649 return (nkeys);
650 }
651 error("no keys");
652 /* don't add the provider, since it does not have any keys */
653fail:
654 if (need_finalize && (rv = f->C_Finalize(NULL)) != CKR_OK)
655 error("C_Finalize failed: %lu", rv);
656 if (p) {
Darren Tuckera627d422013-06-02 07:31:17 +1000657 free(p->slotlist);
658 free(p->slotinfo);
659 free(p);
Damien Miller7ea845e2010-02-12 09:21:02 +1100660 }
661 if (handle)
662 dlclose(handle);
663 return (-1);
664}
Damien Millerdfa41562010-02-12 10:06:28 +1100665
Darren Tucker0dd24e02011-09-04 19:59:26 +1000666#else
667
668int
669pkcs11_init(int interactive)
670{
671 return (0);
672}
673
674void
675pkcs11_terminate(void)
676{
677 return;
678}
679
Damien Millerdfa41562010-02-12 10:06:28 +1100680#endif /* ENABLE_PKCS11 */