Annotation of src/usr.bin/ssh/ssh-pkcs11.c, Revision 1.14
1.14 ! djm 1: /* $OpenBSD: ssh-pkcs11.c,v 1.13 2014/05/02 03:27:54 djm Exp $ */
1.1 markus 2: /*
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:
18: #include <sys/types.h>
19: #include <sys/queue.h>
20: #include <stdarg.h>
21: #include <stdio.h>
22:
23: #include <string.h>
24: #include <dlfcn.h>
25:
1.9 markus 26: #include <openssl/x509.h>
27:
1.1 markus 28: #define CRYPTOKI_COMPAT
29: #include "pkcs11.h"
30:
31: #include "log.h"
32: #include "misc.h"
33: #include "key.h"
34: #include "ssh-pkcs11.h"
35: #include "xmalloc.h"
36:
37: struct pkcs11_slotinfo {
38: CK_TOKEN_INFO token;
39: CK_SESSION_HANDLE session;
40: int logged_in;
41: };
42:
43: struct pkcs11_provider {
44: char *name;
45: void *handle;
46: CK_FUNCTION_LIST *function_list;
47: CK_INFO info;
48: CK_ULONG nslots;
49: CK_SLOT_ID *slotlist;
50: struct pkcs11_slotinfo *slotinfo;
51: int valid;
52: int refcount;
53: TAILQ_ENTRY(pkcs11_provider) next;
54: };
55:
56: TAILQ_HEAD(, pkcs11_provider) pkcs11_providers;
57:
58: struct pkcs11_key {
59: struct pkcs11_provider *provider;
60: CK_ULONG slotidx;
61: int (*orig_finish)(RSA *rsa);
62: RSA_METHOD rsa_method;
63: char *keyid;
64: int keyid_len;
65: };
66:
67: int pkcs11_interactive = 0;
68:
69: int
70: pkcs11_init(int interactive)
71: {
72: pkcs11_interactive = interactive;
73: TAILQ_INIT(&pkcs11_providers);
74: return (0);
75: }
76:
77: /*
78: * finalize a provider shared libarary, it's no longer usable.
79: * however, there might still be keys referencing this provider,
80: * so the actuall freeing of memory is handled by pkcs11_provider_unref().
81: * this is called when a provider gets unregistered.
82: */
83: static void
84: pkcs11_provider_finalize(struct pkcs11_provider *p)
85: {
86: CK_RV rv;
87: CK_ULONG i;
88:
89: debug("pkcs11_provider_finalize: %p refcount %d valid %d",
90: p, p->refcount, p->valid);
91: if (!p->valid)
92: return;
93: for (i = 0; i < p->nslots; i++) {
94: if (p->slotinfo[i].session &&
95: (rv = p->function_list->C_CloseSession(
96: p->slotinfo[i].session)) != CKR_OK)
97: error("C_CloseSession failed: %lu", rv);
98: }
99: if ((rv = p->function_list->C_Finalize(NULL)) != CKR_OK)
100: error("C_Finalize failed: %lu", rv);
101: p->valid = 0;
102: p->function_list = NULL;
1.3 deraadt 103: #ifdef HAVE_DLOPEN
1.1 markus 104: dlclose(p->handle);
1.3 deraadt 105: #endif
1.1 markus 106: }
107:
108: /*
109: * remove a reference to the provider.
110: * called when a key gets destroyed or when the provider is unregistered.
111: */
112: static void
113: pkcs11_provider_unref(struct pkcs11_provider *p)
114: {
115: debug("pkcs11_provider_unref: %p refcount %d", p, p->refcount);
116: if (--p->refcount <= 0) {
117: if (p->valid)
118: error("pkcs11_provider_unref: %p still valid", p);
1.7 djm 119: free(p->slotlist);
120: free(p->slotinfo);
121: free(p);
1.1 markus 122: }
123: }
124:
125: /* unregister all providers, keys might still point to the providers */
126: void
127: pkcs11_terminate(void)
128: {
129: struct pkcs11_provider *p;
130:
131: while ((p = TAILQ_FIRST(&pkcs11_providers)) != NULL) {
132: TAILQ_REMOVE(&pkcs11_providers, p, next);
133: pkcs11_provider_finalize(p);
134: pkcs11_provider_unref(p);
135: }
136: }
137:
138: /* lookup provider by name */
139: static struct pkcs11_provider *
140: pkcs11_provider_lookup(char *provider_id)
141: {
142: struct pkcs11_provider *p;
143:
144: TAILQ_FOREACH(p, &pkcs11_providers, next) {
145: debug("check %p %s", p, p->name);
146: if (!strcmp(provider_id, p->name))
147: return (p);
148: }
149: return (NULL);
150: }
151:
152: /* unregister provider by name */
153: int
154: pkcs11_del_provider(char *provider_id)
155: {
156: struct pkcs11_provider *p;
157:
158: if ((p = pkcs11_provider_lookup(provider_id)) != NULL) {
159: TAILQ_REMOVE(&pkcs11_providers, p, next);
160: pkcs11_provider_finalize(p);
161: pkcs11_provider_unref(p);
162: return (0);
163: }
164: return (-1);
165: }
166:
167: /* openssl callback for freeing an RSA key */
168: static int
169: pkcs11_rsa_finish(RSA *rsa)
170: {
171: struct pkcs11_key *k11;
172: int rv = -1;
173:
174: if ((k11 = RSA_get_app_data(rsa)) != NULL) {
175: if (k11->orig_finish)
176: rv = k11->orig_finish(rsa);
177: if (k11->provider)
178: pkcs11_provider_unref(k11->provider);
1.7 djm 179: free(k11->keyid);
180: free(k11);
1.1 markus 181: }
182: return (rv);
183: }
184:
1.5 markus 185: /* find a single 'obj' for given attributes */
186: static int
187: pkcs11_find(struct pkcs11_provider *p, CK_ULONG slotidx, CK_ATTRIBUTE *attr,
188: CK_ULONG nattr, CK_OBJECT_HANDLE *obj)
189: {
190: CK_FUNCTION_LIST *f;
191: CK_SESSION_HANDLE session;
192: CK_ULONG nfound = 0;
193: CK_RV rv;
194: int ret = -1;
195:
196: f = p->function_list;
197: session = p->slotinfo[slotidx].session;
198: if ((rv = f->C_FindObjectsInit(session, attr, nattr)) != CKR_OK) {
199: error("C_FindObjectsInit failed (nattr %lu): %lu", nattr, rv);
200: return (-1);
201: }
202: if ((rv = f->C_FindObjects(session, obj, 1, &nfound)) != CKR_OK ||
203: nfound != 1) {
204: debug("C_FindObjects failed (nfound %lu nattr %lu): %lu",
205: nfound, nattr, rv);
206: } else
207: ret = 0;
208: if ((rv = f->C_FindObjectsFinal(session)) != CKR_OK)
209: error("C_FindObjectsFinal failed: %lu", rv);
210: return (ret);
211: }
212:
1.1 markus 213: /* openssl callback doing the actual signing operation */
214: static int
215: pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
216: int padding)
217: {
218: struct pkcs11_key *k11;
219: struct pkcs11_slotinfo *si;
220: CK_FUNCTION_LIST *f;
221: CK_OBJECT_HANDLE obj;
1.5 markus 222: CK_ULONG tlen = 0;
1.1 markus 223: CK_RV rv;
224: CK_OBJECT_CLASS private_key_class = CKO_PRIVATE_KEY;
1.10 djm 225: CK_BBOOL true_val = CK_TRUE;
1.1 markus 226: CK_MECHANISM mech = {
227: CKM_RSA_PKCS, NULL_PTR, 0
228: };
229: CK_ATTRIBUTE key_filter[] = {
230: {CKA_CLASS, &private_key_class, sizeof(private_key_class) },
231: {CKA_ID, NULL, 0},
1.10 djm 232: {CKA_SIGN, &true_val, sizeof(true_val) }
1.1 markus 233: };
234: char *pin, prompt[1024];
235: int rval = -1;
236:
237: if ((k11 = RSA_get_app_data(rsa)) == NULL) {
238: error("RSA_get_app_data failed for rsa %p", rsa);
239: return (-1);
240: }
241: if (!k11->provider || !k11->provider->valid) {
242: error("no pkcs11 (valid) provider for rsa %p", rsa);
243: return (-1);
244: }
245: f = k11->provider->function_list;
246: si = &k11->provider->slotinfo[k11->slotidx];
247: if ((si->token.flags & CKF_LOGIN_REQUIRED) && !si->logged_in) {
248: if (!pkcs11_interactive) {
249: error("need pin");
250: return (-1);
251: }
252: snprintf(prompt, sizeof(prompt), "Enter PIN for '%s': ",
253: si->token.label);
254: pin = read_passphrase(prompt, RP_ALLOW_EOF);
255: if (pin == NULL)
256: return (-1); /* bail out */
1.8 djm 257: if ((rv = f->C_Login(si->session, CKU_USER,
258: (u_char *)pin, strlen(pin))) != CKR_OK) {
1.7 djm 259: free(pin);
1.1 markus 260: error("C_Login failed: %lu", rv);
261: return (-1);
262: }
1.7 djm 263: free(pin);
1.1 markus 264: si->logged_in = 1;
265: }
266: key_filter[1].pValue = k11->keyid;
267: key_filter[1].ulValueLen = k11->keyid_len;
1.5 markus 268: /* try to find object w/CKA_SIGN first, retry w/o */
269: if (pkcs11_find(k11->provider, k11->slotidx, key_filter, 3, &obj) < 0 &&
270: pkcs11_find(k11->provider, k11->slotidx, key_filter, 2, &obj) < 0) {
271: error("cannot find private key");
1.1 markus 272: } else if ((rv = f->C_SignInit(si->session, &mech, obj)) != CKR_OK) {
273: error("C_SignInit failed: %lu", rv);
274: } else {
275: /* XXX handle CKR_BUFFER_TOO_SMALL */
276: tlen = RSA_size(rsa);
277: rv = f->C_Sign(si->session, (CK_BYTE *)from, flen, to, &tlen);
278: if (rv == CKR_OK)
279: rval = tlen;
280: else
281: error("C_Sign failed: %lu", rv);
282: }
283: return (rval);
284: }
285:
286: static int
287: pkcs11_rsa_private_decrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
288: int padding)
289: {
290: return (-1);
291: }
292:
293: /* redirect private key operations for rsa key to pkcs11 token */
294: static int
295: pkcs11_rsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
296: CK_ATTRIBUTE *keyid_attrib, RSA *rsa)
297: {
298: struct pkcs11_key *k11;
299: const RSA_METHOD *def = RSA_get_default_method();
300:
301: k11 = xcalloc(1, sizeof(*k11));
302: k11->provider = provider;
303: provider->refcount++; /* provider referenced by RSA key */
304: k11->slotidx = slotidx;
305: /* identify key object on smartcard */
306: k11->keyid_len = keyid_attrib->ulValueLen;
307: k11->keyid = xmalloc(k11->keyid_len);
308: memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
309: k11->orig_finish = def->finish;
310: memcpy(&k11->rsa_method, def, sizeof(k11->rsa_method));
311: k11->rsa_method.name = "pkcs11";
312: k11->rsa_method.rsa_priv_enc = pkcs11_rsa_private_encrypt;
313: k11->rsa_method.rsa_priv_dec = pkcs11_rsa_private_decrypt;
314: k11->rsa_method.finish = pkcs11_rsa_finish;
315: RSA_set_method(rsa, &k11->rsa_method);
316: RSA_set_app_data(rsa, k11);
317: return (0);
318: }
319:
320: /* remove trailing spaces */
321: static void
1.8 djm 322: rmspace(u_char *buf, size_t len)
1.1 markus 323: {
324: size_t i;
325:
326: if (!len)
327: return;
328: for (i = len - 1; i > 0; i--)
329: if (i == len - 1 || buf[i] == ' ')
330: buf[i] = '\0';
331: else
332: break;
333: }
334:
335: /*
336: * open a pkcs11 session and login if required.
337: * if pin == NULL we delay login until key use
338: */
339: static int
340: pkcs11_open_session(struct pkcs11_provider *p, CK_ULONG slotidx, char *pin)
341: {
342: CK_RV rv;
343: CK_FUNCTION_LIST *f;
344: CK_SESSION_HANDLE session;
345: int login_required;
346:
347: f = p->function_list;
348: login_required = p->slotinfo[slotidx].token.flags & CKF_LOGIN_REQUIRED;
349: if (pin && login_required && !strlen(pin)) {
350: error("pin required");
351: return (-1);
352: }
353: if ((rv = f->C_OpenSession(p->slotlist[slotidx], CKF_RW_SESSION|
354: CKF_SERIAL_SESSION, NULL, NULL, &session))
355: != CKR_OK) {
356: error("C_OpenSession failed: %lu", rv);
357: return (-1);
358: }
359: if (login_required && pin) {
1.8 djm 360: if ((rv = f->C_Login(session, CKU_USER,
361: (u_char *)pin, strlen(pin))) != CKR_OK) {
1.1 markus 362: error("C_Login failed: %lu", rv);
363: if ((rv = f->C_CloseSession(session)) != CKR_OK)
364: error("C_CloseSession failed: %lu", rv);
365: return (-1);
366: }
367: p->slotinfo[slotidx].logged_in = 1;
368: }
369: p->slotinfo[slotidx].session = session;
370: return (0);
371: }
372:
373: /*
374: * lookup public keys for token in slot identified by slotidx,
375: * add 'wrapped' public keys to the 'keysp' array and increment nkeys.
376: * keysp points to an (possibly empty) array with *nkeys keys.
377: */
1.9 markus 378: static int pkcs11_fetch_keys_filter(struct pkcs11_provider *, CK_ULONG,
379: CK_ATTRIBUTE [], CK_ATTRIBUTE [3], Key ***, int *)
1.13 djm 380: __attribute__((__bounded__(__minbytes__,4, 3 * sizeof(CK_ATTRIBUTE))));
1.9 markus 381:
382: static int
383: pkcs11_fetch_keys(struct pkcs11_provider *p, CK_ULONG slotidx,
384: Key ***keysp, int *nkeys)
385: {
386: CK_OBJECT_CLASS pubkey_class = CKO_PUBLIC_KEY;
387: CK_OBJECT_CLASS cert_class = CKO_CERTIFICATE;
388: CK_ATTRIBUTE pubkey_filter[] = {
389: { CKA_CLASS, &pubkey_class, sizeof(pubkey_class) }
390: };
391: CK_ATTRIBUTE cert_filter[] = {
392: { CKA_CLASS, &cert_class, sizeof(cert_class) }
393: };
394: CK_ATTRIBUTE pubkey_attribs[] = {
395: { CKA_ID, NULL, 0 },
396: { CKA_MODULUS, NULL, 0 },
397: { CKA_PUBLIC_EXPONENT, NULL, 0 }
398: };
399: CK_ATTRIBUTE cert_attribs[] = {
400: { CKA_ID, NULL, 0 },
401: { CKA_SUBJECT, NULL, 0 },
402: { CKA_VALUE, NULL, 0 }
403: };
404:
405: if (pkcs11_fetch_keys_filter(p, slotidx, pubkey_filter, pubkey_attribs,
406: keysp, nkeys) < 0 ||
407: pkcs11_fetch_keys_filter(p, slotidx, cert_filter, cert_attribs,
408: keysp, nkeys) < 0)
409: return (-1);
410: return (0);
411: }
412:
1.1 markus 413: static int
1.9 markus 414: pkcs11_key_included(Key ***keysp, int *nkeys, Key *key)
415: {
416: int i;
417:
418: for (i = 0; i < *nkeys; i++)
1.11 markus 419: if (key_equal(key, (*keysp)[i]))
1.9 markus 420: return (1);
421: return (0);
422: }
423:
424: static int
425: pkcs11_fetch_keys_filter(struct pkcs11_provider *p, CK_ULONG slotidx,
426: CK_ATTRIBUTE filter[], CK_ATTRIBUTE attribs[3],
427: Key ***keysp, int *nkeys)
1.1 markus 428: {
429: Key *key;
430: RSA *rsa;
1.9 markus 431: X509 *x509;
432: EVP_PKEY *evp;
1.1 markus 433: int i;
1.9 markus 434: const u_char *cp;
1.1 markus 435: CK_RV rv;
436: CK_OBJECT_HANDLE obj;
437: CK_ULONG nfound;
438: CK_SESSION_HANDLE session;
439: CK_FUNCTION_LIST *f;
440:
441: f = p->function_list;
442: session = p->slotinfo[slotidx].session;
443: /* setup a filter the looks for public keys */
1.9 markus 444: if ((rv = f->C_FindObjectsInit(session, filter, 1)) != CKR_OK) {
1.1 markus 445: error("C_FindObjectsInit failed: %lu", rv);
446: return (-1);
447: }
448: while (1) {
449: /* XXX 3 attributes in attribs[] */
450: for (i = 0; i < 3; i++) {
451: attribs[i].pValue = NULL;
452: attribs[i].ulValueLen = 0;
453: }
454: if ((rv = f->C_FindObjects(session, &obj, 1, &nfound)) != CKR_OK
455: || nfound == 0)
456: break;
457: /* found a key, so figure out size of the attributes */
458: if ((rv = f->C_GetAttributeValue(session, obj, attribs, 3))
459: != CKR_OK) {
460: error("C_GetAttributeValue failed: %lu", rv);
461: continue;
462: }
1.6 markus 463: /* check that none of the attributes are zero length */
464: if (attribs[0].ulValueLen == 0 ||
465: attribs[1].ulValueLen == 0 ||
466: attribs[2].ulValueLen == 0) {
467: continue;
468: }
469: /* allocate buffers for attributes */
1.1 markus 470: for (i = 0; i < 3; i++)
471: attribs[i].pValue = xmalloc(attribs[i].ulValueLen);
1.9 markus 472: /*
473: * retrieve ID, modulus and public exponent of RSA key,
474: * or ID, subject and value for certificates.
475: */
476: rsa = NULL;
1.1 markus 477: if ((rv = f->C_GetAttributeValue(session, obj, attribs, 3))
478: != CKR_OK) {
479: error("C_GetAttributeValue failed: %lu", rv);
1.9 markus 480: } else if (attribs[1].type == CKA_MODULUS ) {
481: if ((rsa = RSA_new()) == NULL) {
482: error("RSA_new failed");
483: } else {
484: rsa->n = BN_bin2bn(attribs[1].pValue,
485: attribs[1].ulValueLen, NULL);
486: rsa->e = BN_bin2bn(attribs[2].pValue,
487: attribs[2].ulValueLen, NULL);
488: }
1.1 markus 489: } else {
1.9 markus 490: cp = attribs[2].pValue;
491: if ((x509 = X509_new()) == NULL) {
492: error("X509_new failed");
493: } else if (d2i_X509(&x509, &cp, attribs[2].ulValueLen)
494: == NULL) {
495: error("d2i_X509 failed");
496: } else if ((evp = X509_get_pubkey(x509)) == NULL ||
497: evp->type != EVP_PKEY_RSA ||
498: evp->pkey.rsa == NULL) {
499: debug("X509_get_pubkey failed or no rsa");
500: } else if ((rsa = RSAPublicKey_dup(evp->pkey.rsa))
501: == NULL) {
502: error("RSAPublicKey_dup");
503: }
504: if (x509)
505: X509_free(x509);
506: }
507: if (rsa && rsa->n && rsa->e &&
508: pkcs11_rsa_wrap(p, slotidx, &attribs[0], rsa) == 0) {
509: key = key_new(KEY_UNSPEC);
510: key->rsa = rsa;
511: key->type = KEY_RSA;
1.14 ! djm 512: key->flags |= SSHKEY_FLAG_EXT;
1.9 markus 513: if (pkcs11_key_included(keysp, nkeys, key)) {
514: key_free(key);
515: } else {
1.1 markus 516: /* expand key array and add key */
517: *keysp = xrealloc(*keysp, *nkeys + 1,
518: sizeof(Key *));
519: (*keysp)[*nkeys] = key;
520: *nkeys = *nkeys + 1;
521: debug("have %d keys", *nkeys);
522: }
1.9 markus 523: } else if (rsa) {
524: RSA_free(rsa);
1.1 markus 525: }
526: for (i = 0; i < 3; i++)
1.7 djm 527: free(attribs[i].pValue);
1.1 markus 528: }
529: if ((rv = f->C_FindObjectsFinal(session)) != CKR_OK)
530: error("C_FindObjectsFinal failed: %lu", rv);
531: return (0);
532: }
533:
1.2 markus 534: #ifdef HAVE_DLOPEN
1.1 markus 535: /* register a new provider, fails if provider already exists */
536: int
537: pkcs11_add_provider(char *provider_id, char *pin, Key ***keyp)
538: {
539: int nkeys, need_finalize = 0;
540: struct pkcs11_provider *p = NULL;
541: void *handle = NULL;
542: CK_RV (*getfunctionlist)(CK_FUNCTION_LIST **);
543: CK_RV rv;
544: CK_FUNCTION_LIST *f = NULL;
545: CK_TOKEN_INFO *token;
546: CK_ULONG i;
547:
548: *keyp = NULL;
549: if (pkcs11_provider_lookup(provider_id) != NULL) {
550: error("provider already registered: %s", provider_id);
551: goto fail;
552: }
553: /* open shared pkcs11-libarary */
554: if ((handle = dlopen(provider_id, RTLD_NOW)) == NULL) {
555: error("dlopen %s failed: %s", provider_id, dlerror());
556: goto fail;
557: }
558: if ((getfunctionlist = dlsym(handle, "C_GetFunctionList")) == NULL) {
559: error("dlsym(C_GetFunctionList) failed: %s", dlerror());
560: goto fail;
561: }
562: p = xcalloc(1, sizeof(*p));
563: p->name = xstrdup(provider_id);
564: p->handle = handle;
565: /* setup the pkcs11 callbacks */
566: if ((rv = (*getfunctionlist)(&f)) != CKR_OK) {
567: error("C_GetFunctionList failed: %lu", rv);
568: goto fail;
569: }
570: p->function_list = f;
571: if ((rv = f->C_Initialize(NULL)) != CKR_OK) {
572: error("C_Initialize failed: %lu", rv);
573: goto fail;
574: }
575: need_finalize = 1;
576: if ((rv = f->C_GetInfo(&p->info)) != CKR_OK) {
577: error("C_GetInfo failed: %lu", rv);
578: goto fail;
579: }
580: rmspace(p->info.manufacturerID, sizeof(p->info.manufacturerID));
581: rmspace(p->info.libraryDescription, sizeof(p->info.libraryDescription));
582: debug("manufacturerID <%s> cryptokiVersion %d.%d"
583: " libraryDescription <%s> libraryVersion %d.%d",
584: p->info.manufacturerID,
585: p->info.cryptokiVersion.major,
586: p->info.cryptokiVersion.minor,
587: p->info.libraryDescription,
588: p->info.libraryVersion.major,
589: p->info.libraryVersion.minor);
590: if ((rv = f->C_GetSlotList(CK_TRUE, NULL, &p->nslots)) != CKR_OK) {
591: error("C_GetSlotList failed: %lu", rv);
592: goto fail;
593: }
594: if (p->nslots == 0) {
595: error("no slots");
596: goto fail;
597: }
598: p->slotlist = xcalloc(p->nslots, sizeof(CK_SLOT_ID));
599: if ((rv = f->C_GetSlotList(CK_TRUE, p->slotlist, &p->nslots))
600: != CKR_OK) {
601: error("C_GetSlotList failed: %lu", rv);
602: goto fail;
603: }
604: p->slotinfo = xcalloc(p->nslots, sizeof(struct pkcs11_slotinfo));
605: p->valid = 1;
606: nkeys = 0;
607: for (i = 0; i < p->nslots; i++) {
608: token = &p->slotinfo[i].token;
609: if ((rv = f->C_GetTokenInfo(p->slotlist[i], token))
610: != CKR_OK) {
611: error("C_GetTokenInfo failed: %lu", rv);
612: continue;
613: }
614: rmspace(token->label, sizeof(token->label));
615: rmspace(token->manufacturerID, sizeof(token->manufacturerID));
616: rmspace(token->model, sizeof(token->model));
617: rmspace(token->serialNumber, sizeof(token->serialNumber));
618: debug("label <%s> manufacturerID <%s> model <%s> serial <%s>"
619: " flags 0x%lx",
620: token->label, token->manufacturerID, token->model,
621: token->serialNumber, token->flags);
622: /* open session, login with pin and retrieve public keys */
623: if (pkcs11_open_session(p, i, pin) == 0)
624: pkcs11_fetch_keys(p, i, keyp, &nkeys);
625: }
626: if (nkeys > 0) {
627: TAILQ_INSERT_TAIL(&pkcs11_providers, p, next);
628: p->refcount++; /* add to provider list */
629: return (nkeys);
630: }
631: error("no keys");
632: /* don't add the provider, since it does not have any keys */
633: fail:
634: if (need_finalize && (rv = f->C_Finalize(NULL)) != CKR_OK)
635: error("C_Finalize failed: %lu", rv);
636: if (p) {
1.7 djm 637: free(p->slotlist);
638: free(p->slotinfo);
639: free(p);
1.1 markus 640: }
641: if (handle)
642: dlclose(handle);
643: return (-1);
644: }
1.2 markus 645: #else
646: int
647: pkcs11_add_provider(char *provider_id, char *pin, Key ***keyp)
648: {
649: error("dlopen() not supported");
650: return (-1);
651: }
652: #endif