2 * Copyright (c) 2004 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 #define P11_SESSION_IN_USE 2
47 #define P11_LOGIN_REQ 4
48 #define P11_LOGIN_DONE 8
49 #define P11_TOKEN_PRESENT 16
50 CK_SESSION_HANDLE session;
57 CK_MECHANISM_TYPE_PTR list;
59 CK_MECHANISM_INFO_PTR *infos;
65 CK_FUNCTION_LIST_PTR funcs;
68 struct p11_slot *slot;
71 #define P11FUNC(module,f,args) (*(module)->funcs->C_##f)args
73 static int p11_get_session(hx509_context,
78 static int p11_put_session(struct p11_module *,
81 static void p11_release_module(struct p11_module *);
83 static int p11_list_keys(hx509_context,
96 struct p11_slot *slot;
97 CK_OBJECT_HANDLE private_key;
98 CK_OBJECT_HANDLE public_key;
102 p11_rsa_public_encrypt(int flen,
103 const unsigned char *from,
112 p11_rsa_public_decrypt(int flen,
113 const unsigned char *from,
123 p11_rsa_private_encrypt(int flen,
124 const unsigned char *from,
129 struct p11_rsa *p11rsa = RSA_get_app_data(rsa);
130 CK_OBJECT_HANDLE key = p11rsa->private_key;
131 CK_SESSION_HANDLE session;
132 CK_MECHANISM mechanism;
136 if (padding != RSA_PKCS1_PADDING)
139 memset(&mechanism, 0, sizeof(mechanism));
140 mechanism.mechanism = CKM_RSA_PKCS;
142 ck_sigsize = RSA_size(rsa);
144 ret = p11_get_session(NULL, p11rsa->p, p11rsa->slot, NULL, &session);
148 ret = P11FUNC(p11rsa->p, SignInit, (session, &mechanism, key));
150 p11_put_session(p11rsa->p, p11rsa->slot, session);
154 ret = P11FUNC(p11rsa->p, Sign,
155 (session, (CK_BYTE *)(intptr_t)from, flen, to, &ck_sigsize));
156 p11_put_session(p11rsa->p, p11rsa->slot, session);
164 p11_rsa_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
165 RSA * rsa, int padding)
167 struct p11_rsa *p11rsa = RSA_get_app_data(rsa);
168 CK_OBJECT_HANDLE key = p11rsa->private_key;
169 CK_SESSION_HANDLE session;
170 CK_MECHANISM mechanism;
174 if (padding != RSA_PKCS1_PADDING)
177 memset(&mechanism, 0, sizeof(mechanism));
178 mechanism.mechanism = CKM_RSA_PKCS;
180 ck_sigsize = RSA_size(rsa);
182 ret = p11_get_session(NULL, p11rsa->p, p11rsa->slot, NULL, &session);
186 ret = P11FUNC(p11rsa->p, DecryptInit, (session, &mechanism, key));
188 p11_put_session(p11rsa->p, p11rsa->slot, session);
192 ret = P11FUNC(p11rsa->p, Decrypt,
193 (session, (CK_BYTE *)(intptr_t)from, flen, to, &ck_sigsize));
194 p11_put_session(p11rsa->p, p11rsa->slot, session);
202 p11_rsa_init(RSA *rsa)
208 p11_rsa_finish(RSA *rsa)
210 struct p11_rsa *p11rsa = RSA_get_app_data(rsa);
211 p11_release_module(p11rsa->p);
216 static const RSA_METHOD *
217 get_p11_rsa_pkcs1_method(void)
219 static const RSA_METHOD *p11_rsa_pkcs1_method;
220 RSA_METHOD *new_method;
222 if (p11_rsa_pkcs1_method != NULL)
223 return p11_rsa_pkcs1_method;
225 new_method = RSA_meth_new("hx509 PKCS11 PKCS#1 RSA", 0);
226 if (new_method == NULL)
229 if (RSA_meth_set_pub_enc(new_method, p11_rsa_public_encrypt) != 1)
232 if (RSA_meth_set_pub_dec(new_method, p11_rsa_public_decrypt) != 1)
235 if (RSA_meth_set_priv_enc(new_method, p11_rsa_private_encrypt) != 1)
238 if (RSA_meth_set_priv_dec(new_method, p11_rsa_private_decrypt) != 1)
241 if (RSA_meth_set_init(new_method, p11_rsa_init) != 1)
244 if (RSA_meth_set_finish(new_method, p11_rsa_finish) != 1)
248 * This might overwrite a previously-created method if multiple
249 * threads invoke this concurrently which will leak memory.
251 p11_rsa_pkcs1_method = new_method;
252 return p11_rsa_pkcs1_method;
254 RSA_meth_free(new_method);
263 p11_mech_info(hx509_context context,
264 struct p11_module *p,
265 struct p11_slot *slot,
271 ret = P11FUNC(p, GetMechanismList, (slot->id, NULL_PTR, &i));
273 hx509_set_error_string(context, 0, HX509_PKCS11_NO_MECH,
274 "Failed to get mech list count for slot %d",
276 return HX509_PKCS11_NO_MECH;
279 hx509_set_error_string(context, 0, HX509_PKCS11_NO_MECH,
280 "no mech supported for slot %d", num);
281 return HX509_PKCS11_NO_MECH;
283 slot->mechs.list = calloc(i, sizeof(slot->mechs.list[0]));
284 if (slot->mechs.list == NULL) {
285 hx509_set_error_string(context, 0, ENOMEM,
290 ret = P11FUNC(p, GetMechanismList, (slot->id, slot->mechs.list, &i));
292 hx509_set_error_string(context, 0, HX509_PKCS11_NO_MECH,
293 "Failed to get mech list for slot %d",
295 return HX509_PKCS11_NO_MECH;
297 assert(i == slot->mechs.num);
299 slot->mechs.infos = calloc(i, sizeof(*slot->mechs.infos));
300 if (slot->mechs.list == NULL) {
301 hx509_set_error_string(context, 0, ENOMEM,
306 for (i = 0; i < slot->mechs.num; i++) {
307 slot->mechs.infos[i] = calloc(1, sizeof(*(slot->mechs.infos[0])));
308 if (slot->mechs.infos[i] == NULL) {
309 hx509_set_error_string(context, 0, ENOMEM,
313 ret = P11FUNC(p, GetMechanismInfo, (slot->id, slot->mechs.list[i],
314 slot->mechs.infos[i]));
316 hx509_set_error_string(context, 0, HX509_PKCS11_NO_MECH,
317 "Failed to get mech info for slot %d",
319 return HX509_PKCS11_NO_MECH;
327 p11_init_slot(hx509_context context,
328 struct p11_module *p,
332 struct p11_slot *slot)
334 CK_SESSION_HANDLE session;
335 CK_SLOT_INFO slot_info;
336 CK_TOKEN_INFO token_info;
343 ret = P11FUNC(p, GetSlotInfo, (slot->id, &slot_info));
345 hx509_set_error_string(context, 0, HX509_PKCS11_TOKEN_CONFUSED,
346 "Failed to init PKCS11 slot %d",
348 return HX509_PKCS11_TOKEN_CONFUSED;
351 for (i = sizeof(slot_info.slotDescription) - 1; i > 0; i--) {
352 char c = slot_info.slotDescription[i];
353 if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\0')
359 asprintf(&slot->name, "%.*s",
360 (int)i, slot_info.slotDescription);
362 if ((slot_info.flags & CKF_TOKEN_PRESENT) == 0)
365 ret = P11FUNC(p, GetTokenInfo, (slot->id, &token_info));
367 hx509_set_error_string(context, 0, HX509_PKCS11_NO_TOKEN,
368 "Failed to init PKCS11 slot %d "
371 return HX509_PKCS11_NO_TOKEN;
373 slot->flags |= P11_TOKEN_PRESENT;
375 if (token_info.flags & CKF_LOGIN_REQUIRED)
376 slot->flags |= P11_LOGIN_REQ;
378 ret = p11_get_session(context, p, slot, lock, &session);
382 ret = p11_mech_info(context, p, slot, num);
386 ret = p11_list_keys(context, p, slot, session, lock, &slot->certs);
388 p11_put_session(p, slot, session);
394 p11_get_session(hx509_context context,
395 struct p11_module *p,
396 struct p11_slot *slot,
398 CK_SESSION_HANDLE *psession)
402 if (slot->flags & P11_SESSION_IN_USE)
403 _hx509_abort("slot already in session");
405 if (slot->flags & P11_SESSION) {
406 slot->flags |= P11_SESSION_IN_USE;
407 *psession = slot->session;
411 ret = P11FUNC(p, OpenSession, (slot->id,
418 hx509_set_error_string(context, 0, HX509_PKCS11_OPEN_SESSION,
419 "Failed to OpenSession for slot id %d "
420 "with error: 0x%08x",
422 return HX509_PKCS11_OPEN_SESSION;
425 slot->flags |= P11_SESSION;
428 * If we have have to login, and haven't tried before and have a
429 * prompter or known to work pin code.
431 * This code is very conversative and only uses the prompter in
432 * the hx509_lock, the reason is that it's bad to try many
433 * passwords on a pkcs11 token, it might lock up and have to be
434 * unlocked by a administrator.
436 * XXX try harder to not use pin several times on the same card.
439 if ( (slot->flags & P11_LOGIN_REQ)
440 && (slot->flags & P11_LOGIN_DONE) == 0
441 && (lock || slot->pin))
447 if (slot->pin == NULL) {
449 memset(&prompt, 0, sizeof(prompt));
451 asprintf(&str, "PIN code for %s: ", slot->name);
453 prompt.type = HX509_PROMPT_TYPE_PASSWORD;
454 prompt.reply.data = pin;
455 prompt.reply.length = sizeof(pin);
457 ret = hx509_lock_prompt(lock, &prompt);
461 hx509_set_error_string(context, 0, ret,
462 "Failed to get pin code for slot "
463 "id %d with error: %d",
469 strlcpy(pin, slot->pin, sizeof(pin));
472 ret = P11FUNC(p, Login, (slot->session, CKU_USER,
473 (unsigned char*)pin, strlen(pin)));
476 hx509_set_error_string(context, 0, HX509_PKCS11_LOGIN,
477 "Failed to login on slot id %d "
478 "with error: 0x%08x",
480 return HX509_PKCS11_LOGIN;
482 slot->flags |= P11_LOGIN_DONE;
484 if (slot->pin == NULL) {
485 slot->pin = strdup(pin);
486 if (slot->pin == NULL) {
488 hx509_set_error_string(context, 0, ENOMEM,
494 slot->flags |= P11_LOGIN_DONE;
496 slot->flags |= P11_SESSION_IN_USE;
498 *psession = slot->session;
504 p11_put_session(struct p11_module *p,
505 struct p11_slot *slot,
506 CK_SESSION_HANDLE session)
508 if ((slot->flags & P11_SESSION_IN_USE) == 0)
509 _hx509_abort("slot not in session");
510 slot->flags &= ~P11_SESSION_IN_USE;
516 iterate_entries(hx509_context context,
517 struct p11_module *p, struct p11_slot *slot,
518 CK_SESSION_HANDLE session,
519 CK_ATTRIBUTE *search_data, int num_search_data,
520 CK_ATTRIBUTE *query, int num_query,
521 int (*func)(hx509_context,
522 struct p11_module *, struct p11_slot *,
523 CK_SESSION_HANDLE session,
524 CK_OBJECT_HANDLE object,
525 void *, CK_ATTRIBUTE *, int), void *ptr)
527 CK_OBJECT_HANDLE object;
528 CK_ULONG object_count;
531 ret = P11FUNC(p, FindObjectsInit, (session, search_data, num_search_data));
536 ret = P11FUNC(p, FindObjects, (session, &object, 1, &object_count));
540 if (object_count == 0)
543 for (i = 0; i < num_query; i++)
544 query[i].pValue = NULL;
546 ret = P11FUNC(p, GetAttributeValue,
547 (session, object, query, num_query));
551 for (i = 0; i < num_query; i++) {
552 query[i].pValue = malloc(query[i].ulValueLen);
553 if (query[i].pValue == NULL) {
558 ret = P11FUNC(p, GetAttributeValue,
559 (session, object, query, num_query));
565 ret = (*func)(context, p, slot, session, object, ptr, query, num_query);
569 for (i = 0; i < num_query; i++) {
571 free(query[i].pValue);
572 query[i].pValue = NULL;
577 for (i = 0; i < num_query; i++) {
579 free(query[i].pValue);
580 query[i].pValue = NULL;
583 ret2 = P11FUNC(p, FindObjectsFinal, (session));
584 if (ret2 != CKR_OK) {
592 getattr_bn(struct p11_module *p,
593 struct p11_slot *slot,
594 CK_SESSION_HANDLE session,
595 CK_OBJECT_HANDLE object,
604 query.ulValueLen = 0;
606 ret = P11FUNC(p, GetAttributeValue,
607 (session, object, &query, 1));
611 query.pValue = malloc(query.ulValueLen);
613 ret = P11FUNC(p, GetAttributeValue,
614 (session, object, &query, 1));
619 bn = BN_bin2bn(query.pValue, query.ulValueLen, NULL);
626 collect_private_key(hx509_context context,
627 struct p11_module *p, struct p11_slot *slot,
628 CK_SESSION_HANDLE session,
629 CK_OBJECT_HANDLE object,
630 void *ptr, CK_ATTRIBUTE *query, int num_query)
632 struct hx509_collector *collector = ptr;
633 hx509_private_key key;
634 heim_octet_string localKeyId;
636 const RSA_METHOD *meth;
639 struct p11_rsa *p11rsa;
641 localKeyId.data = query[0].pValue;
642 localKeyId.length = query[0].ulValueLen;
644 ret = hx509_private_key_init(&key, NULL, NULL);
650 _hx509_abort("out of memory");
653 * The exponent and modulus should always be present according to
654 * the pkcs11 specification, but some smartcards leaves it out,
655 * let ignore any failure to fetch it.
657 n = getattr_bn(p, slot, session, object, CKA_MODULUS);
658 e = getattr_bn(p, slot, session, object, CKA_PUBLIC_EXPONENT);
659 if (RSA_set0_key(rsa, n, e, NULL) != 1) {
663 hx509_private_key_free(&key);
667 p11rsa = calloc(1, sizeof(*p11rsa));
669 _hx509_abort("out of memory");
673 p11rsa->private_key = object;
676 _hx509_abort("pkcs11 ref == 0 on alloc");
678 if (p->ref == UINT_MAX)
679 _hx509_abort("pkcs11 ref == UINT_MAX on alloc");
681 meth = get_p11_rsa_pkcs1_method();
683 _hx509_abort("failed to create RSA method");
684 RSA_set_method(rsa, meth);
685 ret = RSA_set_app_data(rsa, p11rsa);
687 _hx509_abort("RSA_set_app_data");
689 hx509_private_key_assign_rsa(key, rsa);
691 ret = _hx509_collector_private_key_add(context,
693 hx509_signature_rsa(),
699 hx509_private_key_free(&key);
706 p11_cert_release(hx509_cert cert, void *ctx)
708 struct p11_module *p = ctx;
709 p11_release_module(p);
714 collect_cert(hx509_context context,
715 struct p11_module *p, struct p11_slot *slot,
716 CK_SESSION_HANDLE session,
717 CK_OBJECT_HANDLE object,
718 void *ptr, CK_ATTRIBUTE *query, int num_query)
720 struct hx509_collector *collector = ptr;
724 if ((CK_LONG)query[0].ulValueLen == -1 ||
725 (CK_LONG)query[1].ulValueLen == -1)
730 ret = hx509_cert_init_data(context, query[1].pValue,
731 query[1].ulValueLen, &cert);
736 _hx509_abort("pkcs11 ref == 0 on alloc");
738 if (p->ref == UINT_MAX)
739 _hx509_abort("pkcs11 ref to high");
741 _hx509_cert_set_release(cert, p11_cert_release, p);
744 heim_octet_string data;
746 data.data = query[0].pValue;
747 data.length = query[0].ulValueLen;
749 _hx509_set_cert_attribute(context,
751 &asn1_oid_id_pkcs_9_at_localKeyId,
755 if ((CK_LONG)query[2].ulValueLen != -1) {
758 asprintf(&str, "%.*s",
759 (int)query[2].ulValueLen, (char *)query[2].pValue);
761 hx509_cert_set_friendly_name(cert, str);
766 ret = _hx509_collector_certs_add(context, collector, cert);
767 hx509_cert_free(cert);
774 p11_list_keys(hx509_context context,
775 struct p11_module *p,
776 struct p11_slot *slot,
777 CK_SESSION_HANDLE session,
781 struct hx509_collector *collector;
782 CK_OBJECT_CLASS key_class;
783 CK_ATTRIBUTE search_data[] = {
784 {CKA_CLASS, NULL, 0},
786 CK_ATTRIBUTE query_data[3] = {
788 {CKA_VALUE, NULL, 0},
793 search_data[0].pValue = &key_class;
794 search_data[0].ulValueLen = sizeof(key_class);
797 lock = _hx509_empty_lock;
799 ret = _hx509_collector_alloc(context, lock, &collector);
803 key_class = CKO_PRIVATE_KEY;
804 ret = iterate_entries(context, p, slot, session,
807 collect_private_key, collector);
811 key_class = CKO_CERTIFICATE;
812 ret = iterate_entries(context, p, slot, session,
815 collect_cert, collector);
819 ret = _hx509_collector_collect_certs(context, collector, &slot->certs);
822 _hx509_collector_free(collector);
829 p11_init(hx509_context context,
830 hx509_certs certs, void **data, int flags,
831 const char *residue, hx509_lock lock)
833 CK_C_GetFunctionList getFuncs;
834 struct p11_module *p;
840 list = strdup(residue);
844 p = calloc(1, sizeof(*p));
852 str = strchr(list, ',');
857 strnext = strchr(str, ',');
861 if (strncasecmp(str, "slot=", 5) == 0)
862 p->selected_slot = atoi(str + 5);
867 p->dl_handle = dlopen(list, RTLD_NOW);
869 if (p->dl_handle == NULL) {
870 ret = HX509_PKCS11_LOAD;
871 hx509_set_error_string(context, 0, ret,
872 "Failed to open %s: %s", list, dlerror());
876 getFuncs = (CK_C_GetFunctionList) dlsym(p->dl_handle, "C_GetFunctionList");
877 if (getFuncs == NULL) {
878 ret = HX509_PKCS11_LOAD;
879 hx509_set_error_string(context, 0, ret,
880 "C_GetFunctionList missing in %s: %s",
885 ret = (*getFuncs)(&p->funcs);
887 ret = HX509_PKCS11_LOAD;
888 hx509_set_error_string(context, 0, ret,
889 "C_GetFunctionList failed in %s", list);
893 ret = P11FUNC(p, Initialize, (NULL_PTR));
895 ret = HX509_PKCS11_TOKEN_CONFUSED;
896 hx509_set_error_string(context, 0, ret,
897 "Failed initialize the PKCS11 module");
901 ret = P11FUNC(p, GetSlotList, (FALSE, NULL, &p->num_slots));
903 ret = HX509_PKCS11_TOKEN_CONFUSED;
904 hx509_set_error_string(context, 0, ret,
905 "Failed to get number of PKCS11 slots");
909 if (p->num_slots == 0) {
910 ret = HX509_PKCS11_NO_SLOT;
911 hx509_set_error_string(context, 0, ret,
912 "Selected PKCS11 module have no slots");
918 CK_SLOT_ID_PTR slot_ids;
922 slot_ids = malloc(p->num_slots * sizeof(*slot_ids));
923 if (slot_ids == NULL) {
924 hx509_clear_error_string(context);
929 ret = P11FUNC(p, GetSlotList, (FALSE, slot_ids, &p->num_slots));
932 hx509_set_error_string(context, 0, HX509_PKCS11_TOKEN_CONFUSED,
933 "Failed getting slot-list from "
935 ret = HX509_PKCS11_TOKEN_CONFUSED;
939 p->slot = calloc(p->num_slots, sizeof(p->slot[0]));
940 if (p->slot == NULL) {
942 hx509_set_error_string(context, 0, ENOMEM,
943 "Failed to get memory for slot-list");
948 for (i = 0; i < p->num_slots; i++) {
949 ret = p11_init_slot(context, p, lock, slot_ids[i], i, &p->slot[i]);
952 if (p->slot[i].flags & P11_TOKEN_PRESENT)
958 if (num_tokens == 0) {
959 ret = HX509_PKCS11_NO_TOKEN;
968 p11_release_module(p);
973 p11_release_module(struct p11_module *p)
978 _hx509_abort("pkcs11 ref to low");
982 for (i = 0; i < p->num_slots; i++) {
983 if (p->slot[i].flags & P11_SESSION_IN_USE)
984 _hx509_abort("pkcs11 module release while session in use");
985 if (p->slot[i].flags & P11_SESSION) {
986 P11FUNC(p, CloseSession, (p->slot[i].session));
990 free(p->slot[i].name);
991 if (p->slot[i].pin) {
992 memset(p->slot[i].pin, 0, strlen(p->slot[i].pin));
993 free(p->slot[i].pin);
995 if (p->slot[i].mechs.num) {
996 free(p->slot[i].mechs.list);
998 if (p->slot[i].mechs.infos) {
1001 for (j = 0 ; j < p->slot[i].mechs.num ; j++)
1002 free(p->slot[i].mechs.infos[j]);
1003 free(p->slot[i].mechs.infos);
1010 P11FUNC(p, Finalize, (NULL));
1013 dlclose(p->dl_handle);
1015 memset(p, 0, sizeof(*p));
1020 p11_free(hx509_certs certs, void *data)
1022 struct p11_module *p = data;
1025 for (i = 0; i < p->num_slots; i++) {
1026 if (p->slot[i].certs)
1027 hx509_certs_free(&p->slot[i].certs);
1029 p11_release_module(p);
1039 p11_iter_start(hx509_context context,
1040 hx509_certs certs, void *data, void **cursor)
1042 struct p11_module *p = data;
1043 struct p11_cursor *c;
1047 c = malloc(sizeof(*c));
1049 hx509_clear_error_string(context);
1052 ret = hx509_certs_init(context, "MEMORY:pkcs11-iter", 0, NULL, &c->certs);
1058 for (i = 0 ; i < p->num_slots; i++) {
1059 if (p->slot[i].certs == NULL)
1061 ret = hx509_certs_merge(context, c->certs, p->slot[i].certs);
1063 hx509_certs_free(&c->certs);
1069 ret = hx509_certs_start_seq(context, c->certs, &c->cursor);
1071 hx509_certs_free(&c->certs);
1081 p11_iter(hx509_context context,
1082 hx509_certs certs, void *data, void *cursor, hx509_cert *cert)
1084 struct p11_cursor *c = cursor;
1085 return hx509_certs_next_cert(context, c->certs, c->cursor, cert);
1089 p11_iter_end(hx509_context context,
1090 hx509_certs certs, void *data, void *cursor)
1092 struct p11_cursor *c = cursor;
1094 ret = hx509_certs_end_seq(context, c->certs, c->cursor);
1095 hx509_certs_free(&c->certs);
1100 #define MECHFLAG(x) { "unknown-flag-" #x, x }
1101 static struct units mechflags[] = {
1102 MECHFLAG(0x80000000),
1103 MECHFLAG(0x40000000),
1104 MECHFLAG(0x20000000),
1105 MECHFLAG(0x10000000),
1106 MECHFLAG(0x08000000),
1107 MECHFLAG(0x04000000),
1108 {"ec-compress", 0x2000000 },
1109 {"ec-uncompress", 0x1000000 },
1110 {"ec-namedcurve", 0x0800000 },
1111 {"ec-ecparameters", 0x0400000 },
1112 {"ec-f-2m", 0x0200000 },
1113 {"ec-f-p", 0x0100000 },
1114 {"derive", 0x0080000 },
1115 {"unwrap", 0x0040000 },
1116 {"wrap", 0x0020000 },
1117 {"genereate-key-pair", 0x0010000 },
1118 {"generate", 0x0008000 },
1119 {"verify-recover", 0x0004000 },
1120 {"verify", 0x0002000 },
1121 {"sign-recover", 0x0001000 },
1122 {"sign", 0x0000800 },
1123 {"digest", 0x0000400 },
1124 {"decrypt", 0x0000200 },
1125 {"encrypt", 0x0000100 },
1139 p11_printinfo(hx509_context context,
1142 int (*func)(void *, const char *),
1145 struct p11_module *p = data;
1148 _hx509_pi_printf(func, ctx, "pkcs11 driver with %d slot%s",
1149 p->num_slots, p->num_slots > 1 ? "s" : "");
1151 for (i = 0; i < p->num_slots; i++) {
1152 struct p11_slot *s = &p->slot[i];
1154 _hx509_pi_printf(func, ctx, "slot %d: id: %d name: %s flags: %08x",
1155 i, (int)s->id, s->name, s->flags);
1157 _hx509_pi_printf(func, ctx, "number of supported mechanisms: %lu",
1158 (unsigned long)s->mechs.num);
1159 for (j = 0; j < s->mechs.num; j++) {
1160 const char *mechname = "unknown";
1161 char flags[256], unknownname[40];
1162 #define MECHNAME(s,n) case s: mechname = n; break
1163 switch(s->mechs.list[j]) {
1164 MECHNAME(CKM_RSA_PKCS_KEY_PAIR_GEN, "rsa-pkcs-key-pair-gen");
1165 MECHNAME(CKM_RSA_PKCS, "rsa-pkcs");
1166 MECHNAME(CKM_RSA_X_509, "rsa-x-509");
1167 MECHNAME(CKM_MD5_RSA_PKCS, "md5-rsa-pkcs");
1168 MECHNAME(CKM_SHA1_RSA_PKCS, "sha1-rsa-pkcs");
1169 MECHNAME(CKM_SHA256_RSA_PKCS, "sha256-rsa-pkcs");
1170 MECHNAME(CKM_SHA384_RSA_PKCS, "sha384-rsa-pkcs");
1171 MECHNAME(CKM_SHA512_RSA_PKCS, "sha512-rsa-pkcs");
1172 MECHNAME(CKM_RIPEMD160_RSA_PKCS, "ripemd160-rsa-pkcs");
1173 MECHNAME(CKM_RSA_PKCS_OAEP, "rsa-pkcs-oaep");
1174 MECHNAME(CKM_SHA512_HMAC, "sha512-hmac");
1175 MECHNAME(CKM_SHA512, "sha512");
1176 MECHNAME(CKM_SHA384_HMAC, "sha384-hmac");
1177 MECHNAME(CKM_SHA384, "sha384");
1178 MECHNAME(CKM_SHA256_HMAC, "sha256-hmac");
1179 MECHNAME(CKM_SHA256, "sha256");
1180 MECHNAME(CKM_SHA_1, "sha1");
1181 MECHNAME(CKM_MD5, "md5");
1182 MECHNAME(CKM_RIPEMD160, "ripemd-160");
1183 MECHNAME(CKM_DES_ECB, "des-ecb");
1184 MECHNAME(CKM_DES_CBC, "des-cbc");
1185 MECHNAME(CKM_AES_ECB, "aes-ecb");
1186 MECHNAME(CKM_AES_CBC, "aes-cbc");
1187 MECHNAME(CKM_DH_PKCS_PARAMETER_GEN, "dh-pkcs-parameter-gen");
1189 snprintf(unknownname, sizeof(unknownname),
1191 (unsigned long)s->mechs.list[j]);
1192 mechname = unknownname;
1196 unparse_flags(s->mechs.infos[j]->flags, mechflags,
1197 flags, sizeof(flags));
1199 _hx509_pi_printf(func, ctx, " %s: %s", mechname, flags);
1206 static struct hx509_keyset_ops keyset_pkcs11 = {
1220 #endif /* HAVE_DLOPEN */
1223 _hx509_ks_pkcs11_register(hx509_context context)
1226 _hx509_ks_register(context, &keyset_pkcs11);