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
*)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
*)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 p11_rsa_pkcs1_method
= {
217 "hx509 PKCS11 PKCS#1 RSA",
218 p11_rsa_public_encrypt
,
219 p11_rsa_public_decrypt
,
220 p11_rsa_private_encrypt
,
221 p11_rsa_private_decrypt
,
237 p11_mech_info(hx509_context context
,
238 struct p11_module
*p
,
239 struct p11_slot
*slot
,
245 ret
= P11FUNC(p
, GetMechanismList
, (slot
->id
, NULL_PTR
, &i
));
247 hx509_set_error_string(context
, 0, HX509_PKCS11_NO_MECH
,
248 "Failed to get mech list count for slot %d",
250 return HX509_PKCS11_NO_MECH
;
253 hx509_set_error_string(context
, 0, HX509_PKCS11_NO_MECH
,
254 "no mech supported for slot %d", num
);
255 return HX509_PKCS11_NO_MECH
;
257 slot
->mechs
.list
= calloc(i
, sizeof(slot
->mechs
.list
[0]));
258 if (slot
->mechs
.list
== NULL
) {
259 hx509_set_error_string(context
, 0, ENOMEM
,
264 ret
= P11FUNC(p
, GetMechanismList
, (slot
->id
, slot
->mechs
.list
, &i
));
266 hx509_set_error_string(context
, 0, HX509_PKCS11_NO_MECH
,
267 "Failed to get mech list for slot %d",
269 return HX509_PKCS11_NO_MECH
;
271 assert(i
== slot
->mechs
.num
);
273 slot
->mechs
.infos
= calloc(i
, sizeof(*slot
->mechs
.infos
));
274 if (slot
->mechs
.list
== NULL
) {
275 hx509_set_error_string(context
, 0, ENOMEM
,
280 for (i
= 0; i
< slot
->mechs
.num
; i
++) {
281 slot
->mechs
.infos
[i
] = calloc(1, sizeof(*(slot
->mechs
.infos
[0])));
282 if (slot
->mechs
.infos
[i
] == NULL
) {
283 hx509_set_error_string(context
, 0, ENOMEM
,
287 ret
= P11FUNC(p
, GetMechanismInfo
, (slot
->id
, slot
->mechs
.list
[i
],
288 slot
->mechs
.infos
[i
]));
290 hx509_set_error_string(context
, 0, HX509_PKCS11_NO_MECH
,
291 "Failed to get mech info for slot %d",
293 return HX509_PKCS11_NO_MECH
;
301 p11_init_slot(hx509_context context
,
302 struct p11_module
*p
,
306 struct p11_slot
*slot
)
308 CK_SESSION_HANDLE session
;
309 CK_SLOT_INFO slot_info
;
310 CK_TOKEN_INFO token_info
;
317 ret
= P11FUNC(p
, GetSlotInfo
, (slot
->id
, &slot_info
));
319 hx509_set_error_string(context
, 0, HX509_PKCS11_TOKEN_CONFUSED
,
320 "Failed to init PKCS11 slot %d",
322 return HX509_PKCS11_TOKEN_CONFUSED
;
325 for (i
= sizeof(slot_info
.slotDescription
) - 1; i
> 0; i
--) {
326 char c
= slot_info
.slotDescription
[i
];
327 if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r' || c
== '\0')
333 asprintf(&slot
->name
, "%.*s",
334 (int)i
, slot_info
.slotDescription
);
336 if ((slot_info
.flags
& CKF_TOKEN_PRESENT
) == 0)
339 ret
= P11FUNC(p
, GetTokenInfo
, (slot
->id
, &token_info
));
341 hx509_set_error_string(context
, 0, HX509_PKCS11_NO_TOKEN
,
342 "Failed to init PKCS11 slot %d "
345 return HX509_PKCS11_NO_TOKEN
;
347 slot
->flags
|= P11_TOKEN_PRESENT
;
349 if (token_info
.flags
& CKF_LOGIN_REQUIRED
)
350 slot
->flags
|= P11_LOGIN_REQ
;
352 ret
= p11_get_session(context
, p
, slot
, lock
, &session
);
356 ret
= p11_mech_info(context
, p
, slot
, num
);
360 ret
= p11_list_keys(context
, p
, slot
, session
, lock
, &slot
->certs
);
362 p11_put_session(p
, slot
, session
);
368 p11_get_session(hx509_context context
,
369 struct p11_module
*p
,
370 struct p11_slot
*slot
,
372 CK_SESSION_HANDLE
*psession
)
376 if (slot
->flags
& P11_SESSION_IN_USE
)
377 _hx509_abort("slot already in session");
379 if (slot
->flags
& P11_SESSION
) {
380 slot
->flags
|= P11_SESSION_IN_USE
;
381 *psession
= slot
->session
;
385 ret
= P11FUNC(p
, OpenSession
, (slot
->id
,
392 hx509_set_error_string(context
, 0, HX509_PKCS11_OPEN_SESSION
,
393 "Failed to OpenSession for slot id %d "
394 "with error: 0x%08x",
396 return HX509_PKCS11_OPEN_SESSION
;
399 slot
->flags
|= P11_SESSION
;
402 * If we have have to login, and haven't tried before and have a
403 * prompter or known to work pin code.
405 * This code is very conversative and only uses the prompter in
406 * the hx509_lock, the reason is that it's bad to try many
407 * passwords on a pkcs11 token, it might lock up and have to be
408 * unlocked by a administrator.
410 * XXX try harder to not use pin several times on the same card.
413 if ( (slot
->flags
& P11_LOGIN_REQ
)
414 && (slot
->flags
& P11_LOGIN_DONE
) == 0
415 && (lock
|| slot
->pin
))
421 if (slot
->pin
== NULL
) {
423 memset(&prompt
, 0, sizeof(prompt
));
425 asprintf(&str
, "PIN code for %s: ", slot
->name
);
427 prompt
.type
= HX509_PROMPT_TYPE_PASSWORD
;
428 prompt
.reply
.data
= pin
;
429 prompt
.reply
.length
= sizeof(pin
);
431 ret
= hx509_lock_prompt(lock
, &prompt
);
435 hx509_set_error_string(context
, 0, ret
,
436 "Failed to get pin code for slot "
437 "id %d with error: %d",
443 strlcpy(pin
, slot
->pin
, sizeof(pin
));
446 ret
= P11FUNC(p
, Login
, (slot
->session
, CKU_USER
,
447 (unsigned char*)pin
, strlen(pin
)));
450 hx509_set_error_string(context
, 0, HX509_PKCS11_LOGIN
,
451 "Failed to login on slot id %d "
452 "with error: 0x%08x",
454 return HX509_PKCS11_LOGIN
;
456 slot
->flags
|= P11_LOGIN_DONE
;
458 if (slot
->pin
== NULL
) {
459 slot
->pin
= strdup(pin
);
460 if (slot
->pin
== NULL
) {
462 hx509_set_error_string(context
, 0, ENOMEM
,
468 slot
->flags
|= P11_LOGIN_DONE
;
470 slot
->flags
|= P11_SESSION_IN_USE
;
472 *psession
= slot
->session
;
478 p11_put_session(struct p11_module
*p
,
479 struct p11_slot
*slot
,
480 CK_SESSION_HANDLE session
)
482 if ((slot
->flags
& P11_SESSION_IN_USE
) == 0)
483 _hx509_abort("slot not in session");
484 slot
->flags
&= ~P11_SESSION_IN_USE
;
490 iterate_entries(hx509_context context
,
491 struct p11_module
*p
, struct p11_slot
*slot
,
492 CK_SESSION_HANDLE session
,
493 CK_ATTRIBUTE
*search_data
, int num_search_data
,
494 CK_ATTRIBUTE
*query
, int num_query
,
495 int (*func
)(hx509_context
,
496 struct p11_module
*, struct p11_slot
*,
497 CK_SESSION_HANDLE session
,
498 CK_OBJECT_HANDLE object
,
499 void *, CK_ATTRIBUTE
*, int), void *ptr
)
501 CK_OBJECT_HANDLE object
;
502 CK_ULONG object_count
;
505 ret
= P11FUNC(p
, FindObjectsInit
, (session
, search_data
, num_search_data
));
510 ret
= P11FUNC(p
, FindObjects
, (session
, &object
, 1, &object_count
));
514 if (object_count
== 0)
517 for (i
= 0; i
< num_query
; i
++)
518 query
[i
].pValue
= NULL
;
520 ret
= P11FUNC(p
, GetAttributeValue
,
521 (session
, object
, query
, num_query
));
525 for (i
= 0; i
< num_query
; i
++) {
526 query
[i
].pValue
= malloc(query
[i
].ulValueLen
);
527 if (query
[i
].pValue
== NULL
) {
532 ret
= P11FUNC(p
, GetAttributeValue
,
533 (session
, object
, query
, num_query
));
539 ret
= (*func
)(context
, p
, slot
, session
, object
, ptr
, query
, num_query
);
543 for (i
= 0; i
< num_query
; i
++) {
545 free(query
[i
].pValue
);
546 query
[i
].pValue
= NULL
;
551 for (i
= 0; i
< num_query
; i
++) {
553 free(query
[i
].pValue
);
554 query
[i
].pValue
= NULL
;
557 ret2
= P11FUNC(p
, FindObjectsFinal
, (session
));
558 if (ret2
!= CKR_OK
) {
566 getattr_bn(struct p11_module
*p
,
567 struct p11_slot
*slot
,
568 CK_SESSION_HANDLE session
,
569 CK_OBJECT_HANDLE object
,
578 query
.ulValueLen
= 0;
580 ret
= P11FUNC(p
, GetAttributeValue
,
581 (session
, object
, &query
, 1));
585 query
.pValue
= malloc(query
.ulValueLen
);
587 ret
= P11FUNC(p
, GetAttributeValue
,
588 (session
, object
, &query
, 1));
593 bn
= BN_bin2bn(query
.pValue
, query
.ulValueLen
, NULL
);
600 collect_private_key(hx509_context context
,
601 struct p11_module
*p
, struct p11_slot
*slot
,
602 CK_SESSION_HANDLE session
,
603 CK_OBJECT_HANDLE object
,
604 void *ptr
, CK_ATTRIBUTE
*query
, int num_query
)
606 struct hx509_collector
*collector
= ptr
;
607 hx509_private_key key
;
608 heim_octet_string localKeyId
;
611 struct p11_rsa
*p11rsa
;
613 localKeyId
.data
= query
[0].pValue
;
614 localKeyId
.length
= query
[0].ulValueLen
;
616 ret
= _hx509_private_key_init(&key
, NULL
, NULL
);
622 _hx509_abort("out of memory");
625 * The exponent and modulus should always be present according to
626 * the pkcs11 specification, but some smartcards leaves it out,
627 * let ignore any failure to fetch it.
629 rsa
->n
= getattr_bn(p
, slot
, session
, object
, CKA_MODULUS
);
630 rsa
->e
= getattr_bn(p
, slot
, session
, object
, CKA_PUBLIC_EXPONENT
);
632 p11rsa
= calloc(1, sizeof(*p11rsa
));
634 _hx509_abort("out of memory");
638 p11rsa
->private_key
= object
;
641 _hx509_abort("pkcs11 ref == 0 on alloc");
643 if (p
->ref
== UINT_MAX
)
644 _hx509_abort("pkcs11 ref == UINT_MAX on alloc");
646 RSA_set_method(rsa
, &p11_rsa_pkcs1_method
);
647 ret
= RSA_set_app_data(rsa
, p11rsa
);
649 _hx509_abort("RSA_set_app_data");
651 _hx509_private_key_assign_rsa(key
, rsa
);
653 ret
= _hx509_collector_private_key_add(context
,
655 hx509_signature_rsa(),
661 _hx509_private_key_free(&key
);
668 p11_cert_release(hx509_cert cert
, void *ctx
)
670 struct p11_module
*p
= ctx
;
671 p11_release_module(p
);
676 collect_cert(hx509_context context
,
677 struct p11_module
*p
, struct p11_slot
*slot
,
678 CK_SESSION_HANDLE session
,
679 CK_OBJECT_HANDLE object
,
680 void *ptr
, CK_ATTRIBUTE
*query
, int num_query
)
682 struct hx509_collector
*collector
= ptr
;
686 if ((CK_LONG
)query
[0].ulValueLen
== -1 ||
687 (CK_LONG
)query
[1].ulValueLen
== -1)
692 ret
= hx509_cert_init_data(context
, query
[1].pValue
,
693 query
[1].ulValueLen
, &cert
);
698 _hx509_abort("pkcs11 ref == 0 on alloc");
700 if (p
->ref
== UINT_MAX
)
701 _hx509_abort("pkcs11 ref to high");
703 _hx509_cert_set_release(cert
, p11_cert_release
, p
);
706 heim_octet_string data
;
708 data
.data
= query
[0].pValue
;
709 data
.length
= query
[0].ulValueLen
;
711 _hx509_set_cert_attribute(context
,
713 &asn1_oid_id_pkcs_9_at_localKeyId
,
717 if ((CK_LONG
)query
[2].ulValueLen
!= -1) {
720 asprintf(&str
, "%.*s",
721 (int)query
[2].ulValueLen
, (char *)query
[2].pValue
);
723 hx509_cert_set_friendly_name(cert
, str
);
728 ret
= _hx509_collector_certs_add(context
, collector
, cert
);
729 hx509_cert_free(cert
);
736 p11_list_keys(hx509_context context
,
737 struct p11_module
*p
,
738 struct p11_slot
*slot
,
739 CK_SESSION_HANDLE session
,
743 struct hx509_collector
*collector
;
744 CK_OBJECT_CLASS key_class
;
745 CK_ATTRIBUTE search_data
[] = {
746 {CKA_CLASS
, NULL
, 0},
748 CK_ATTRIBUTE query_data
[3] = {
750 {CKA_VALUE
, NULL
, 0},
755 search_data
[0].pValue
= &key_class
;
756 search_data
[0].ulValueLen
= sizeof(key_class
);
759 lock
= _hx509_empty_lock
;
761 ret
= _hx509_collector_alloc(context
, lock
, &collector
);
765 key_class
= CKO_PRIVATE_KEY
;
766 ret
= iterate_entries(context
, p
, slot
, session
,
769 collect_private_key
, collector
);
773 key_class
= CKO_CERTIFICATE
;
774 ret
= iterate_entries(context
, p
, slot
, session
,
777 collect_cert
, collector
);
781 ret
= _hx509_collector_collect_certs(context
, collector
, &slot
->certs
);
784 _hx509_collector_free(collector
);
791 p11_init(hx509_context context
,
792 hx509_certs certs
, void **data
, int flags
,
793 const char *residue
, hx509_lock lock
)
795 CK_C_GetFunctionList getFuncs
;
796 struct p11_module
*p
;
802 list
= strdup(residue
);
806 p
= calloc(1, sizeof(*p
));
814 str
= strchr(list
, ',');
819 strnext
= strchr(str
, ',');
823 if (strncasecmp(str
, "slot=", 5) == 0)
824 p
->selected_slot
= atoi(str
+ 5);
829 p
->dl_handle
= dlopen(list
, RTLD_NOW
);
831 if (p
->dl_handle
== NULL
) {
832 ret
= HX509_PKCS11_LOAD
;
833 hx509_set_error_string(context
, 0, ret
,
834 "Failed to open %s: %s", list
, dlerror());
838 getFuncs
= (CK_C_GetFunctionList
) dlsym(p
->dl_handle
, "C_GetFunctionList");
839 if (getFuncs
== NULL
) {
840 ret
= HX509_PKCS11_LOAD
;
841 hx509_set_error_string(context
, 0, ret
,
842 "C_GetFunctionList missing in %s: %s",
847 ret
= (*getFuncs
)(&p
->funcs
);
849 ret
= HX509_PKCS11_LOAD
;
850 hx509_set_error_string(context
, 0, ret
,
851 "C_GetFunctionList failed in %s", list
);
855 ret
= P11FUNC(p
, Initialize
, (NULL_PTR
));
857 ret
= HX509_PKCS11_TOKEN_CONFUSED
;
858 hx509_set_error_string(context
, 0, ret
,
859 "Failed initialize the PKCS11 module");
863 ret
= P11FUNC(p
, GetSlotList
, (FALSE
, NULL
, &p
->num_slots
));
865 ret
= HX509_PKCS11_TOKEN_CONFUSED
;
866 hx509_set_error_string(context
, 0, ret
,
867 "Failed to get number of PKCS11 slots");
871 if (p
->num_slots
== 0) {
872 ret
= HX509_PKCS11_NO_SLOT
;
873 hx509_set_error_string(context
, 0, ret
,
874 "Selected PKCS11 module have no slots");
880 CK_SLOT_ID_PTR slot_ids
;
881 int i
, num_tokens
= 0;
883 slot_ids
= malloc(p
->num_slots
* sizeof(*slot_ids
));
884 if (slot_ids
== NULL
) {
885 hx509_clear_error_string(context
);
890 ret
= P11FUNC(p
, GetSlotList
, (FALSE
, slot_ids
, &p
->num_slots
));
893 hx509_set_error_string(context
, 0, HX509_PKCS11_TOKEN_CONFUSED
,
894 "Failed getting slot-list from "
896 ret
= HX509_PKCS11_TOKEN_CONFUSED
;
900 p
->slot
= calloc(p
->num_slots
, sizeof(p
->slot
[0]));
901 if (p
->slot
== NULL
) {
903 hx509_set_error_string(context
, 0, ENOMEM
,
904 "Failed to get memory for slot-list");
909 for (i
= 0; i
< p
->num_slots
; i
++) {
910 ret
= p11_init_slot(context
, p
, lock
, slot_ids
[i
], i
, &p
->slot
[i
]);
913 if (p
->slot
[i
].flags
& P11_TOKEN_PRESENT
)
919 if (num_tokens
== 0) {
920 ret
= HX509_PKCS11_NO_TOKEN
;
929 p11_release_module(p
);
934 p11_release_module(struct p11_module
*p
)
939 _hx509_abort("pkcs11 ref to low");
943 for (i
= 0; i
< p
->num_slots
; i
++) {
944 if (p
->slot
[i
].flags
& P11_SESSION_IN_USE
)
945 _hx509_abort("pkcs11 module release while session in use");
946 if (p
->slot
[i
].flags
& P11_SESSION
) {
947 P11FUNC(p
, CloseSession
, (p
->slot
[i
].session
));
951 free(p
->slot
[i
].name
);
952 if (p
->slot
[i
].pin
) {
953 memset(p
->slot
[i
].pin
, 0, strlen(p
->slot
[i
].pin
));
954 free(p
->slot
[i
].pin
);
956 if (p
->slot
[i
].mechs
.num
) {
957 free(p
->slot
[i
].mechs
.list
);
959 if (p
->slot
[i
].mechs
.infos
) {
962 for (j
= 0 ; j
< p
->slot
[i
].mechs
.num
; j
++)
963 free(p
->slot
[i
].mechs
.infos
[j
]);
964 free(p
->slot
[i
].mechs
.infos
);
971 P11FUNC(p
, Finalize
, (NULL
));
974 dlclose(p
->dl_handle
);
976 memset(p
, 0, sizeof(*p
));
981 p11_free(hx509_certs certs
, void *data
)
983 struct p11_module
*p
= data
;
986 for (i
= 0; i
< p
->num_slots
; i
++) {
987 if (p
->slot
[i
].certs
)
988 hx509_certs_free(&p
->slot
[i
].certs
);
990 p11_release_module(p
);
1000 p11_iter_start(hx509_context context
,
1001 hx509_certs certs
, void *data
, void **cursor
)
1003 struct p11_module
*p
= data
;
1004 struct p11_cursor
*c
;
1007 c
= malloc(sizeof(*c
));
1009 hx509_clear_error_string(context
);
1012 ret
= hx509_certs_init(context
, "MEMORY:pkcs11-iter", 0, NULL
, &c
->certs
);
1018 for (i
= 0 ; i
< p
->num_slots
; i
++) {
1019 if (p
->slot
[i
].certs
== NULL
)
1021 ret
= hx509_certs_merge(context
, c
->certs
, p
->slot
[i
].certs
);
1023 hx509_certs_free(&c
->certs
);
1029 ret
= hx509_certs_start_seq(context
, c
->certs
, &c
->cursor
);
1031 hx509_certs_free(&c
->certs
);
1041 p11_iter(hx509_context context
,
1042 hx509_certs certs
, void *data
, void *cursor
, hx509_cert
*cert
)
1044 struct p11_cursor
*c
= cursor
;
1045 return hx509_certs_next_cert(context
, c
->certs
, c
->cursor
, cert
);
1049 p11_iter_end(hx509_context context
,
1050 hx509_certs certs
, void *data
, void *cursor
)
1052 struct p11_cursor
*c
= cursor
;
1054 ret
= hx509_certs_end_seq(context
, c
->certs
, c
->cursor
);
1055 hx509_certs_free(&c
->certs
);
1060 #define MECHFLAG(x) { "unknown-flag-" #x, x }
1061 static struct units mechflags
[] = {
1062 MECHFLAG(0x80000000),
1063 MECHFLAG(0x40000000),
1064 MECHFLAG(0x20000000),
1065 MECHFLAG(0x10000000),
1066 MECHFLAG(0x08000000),
1067 MECHFLAG(0x04000000),
1068 {"ec-compress", 0x2000000 },
1069 {"ec-uncompress", 0x1000000 },
1070 {"ec-namedcurve", 0x0800000 },
1071 {"ec-ecparameters", 0x0400000 },
1072 {"ec-f-2m", 0x0200000 },
1073 {"ec-f-p", 0x0100000 },
1074 {"derive", 0x0080000 },
1075 {"unwrap", 0x0040000 },
1076 {"wrap", 0x0020000 },
1077 {"genereate-key-pair", 0x0010000 },
1078 {"generate", 0x0008000 },
1079 {"verify-recover", 0x0004000 },
1080 {"verify", 0x0002000 },
1081 {"sign-recover", 0x0001000 },
1082 {"sign", 0x0000800 },
1083 {"digest", 0x0000400 },
1084 {"decrypt", 0x0000200 },
1085 {"encrypt", 0x0000100 },
1099 p11_printinfo(hx509_context context
,
1102 int (*func
)(void *, const char *),
1105 struct p11_module
*p
= data
;
1108 _hx509_pi_printf(func
, ctx
, "pkcs11 driver with %d slot%s",
1109 p
->num_slots
, p
->num_slots
> 1 ? "s" : "");
1111 for (i
= 0; i
< p
->num_slots
; i
++) {
1112 struct p11_slot
*s
= &p
->slot
[i
];
1114 _hx509_pi_printf(func
, ctx
, "slot %d: id: %d name: %s flags: %08x",
1115 i
, (int)s
->id
, s
->name
, s
->flags
);
1117 _hx509_pi_printf(func
, ctx
, "number of supported mechanisms: %lu",
1118 (unsigned long)s
->mechs
.num
);
1119 for (j
= 0; j
< s
->mechs
.num
; j
++) {
1120 const char *mechname
= "unknown";
1121 char flags
[256], unknownname
[40];
1122 #define MECHNAME(s,n) case s: mechname = n; break
1123 switch(s
->mechs
.list
[j
]) {
1124 MECHNAME(CKM_RSA_PKCS_KEY_PAIR_GEN
, "rsa-pkcs-key-pair-gen");
1125 MECHNAME(CKM_RSA_PKCS
, "rsa-pkcs");
1126 MECHNAME(CKM_RSA_X_509
, "rsa-x-509");
1127 MECHNAME(CKM_MD5_RSA_PKCS
, "md5-rsa-pkcs");
1128 MECHNAME(CKM_SHA1_RSA_PKCS
, "sha1-rsa-pkcs");
1129 MECHNAME(CKM_SHA256_RSA_PKCS
, "sha256-rsa-pkcs");
1130 MECHNAME(CKM_SHA384_RSA_PKCS
, "sha384-rsa-pkcs");
1131 MECHNAME(CKM_SHA512_RSA_PKCS
, "sha512-rsa-pkcs");
1132 MECHNAME(CKM_RIPEMD160_RSA_PKCS
, "ripemd160-rsa-pkcs");
1133 MECHNAME(CKM_RSA_PKCS_OAEP
, "rsa-pkcs-oaep");
1134 MECHNAME(CKM_SHA512_HMAC
, "sha512-hmac");
1135 MECHNAME(CKM_SHA512
, "sha512");
1136 MECHNAME(CKM_SHA384_HMAC
, "sha384-hmac");
1137 MECHNAME(CKM_SHA384
, "sha384");
1138 MECHNAME(CKM_SHA256_HMAC
, "sha256-hmac");
1139 MECHNAME(CKM_SHA256
, "sha256");
1140 MECHNAME(CKM_SHA_1
, "sha1");
1141 MECHNAME(CKM_MD5
, "md5");
1142 MECHNAME(CKM_RIPEMD160
, "ripemd-160");
1143 MECHNAME(CKM_DES_ECB
, "des-ecb");
1144 MECHNAME(CKM_DES_CBC
, "des-cbc");
1145 MECHNAME(CKM_AES_ECB
, "aes-ecb");
1146 MECHNAME(CKM_AES_CBC
, "aes-cbc");
1147 MECHNAME(CKM_DH_PKCS_PARAMETER_GEN
, "dh-pkcs-parameter-gen");
1149 snprintf(unknownname
, sizeof(unknownname
),
1151 (unsigned long)s
->mechs
.list
[j
]);
1152 mechname
= unknownname
;
1156 unparse_flags(s
->mechs
.infos
[j
]->flags
, mechflags
,
1157 flags
, sizeof(flags
));
1159 _hx509_pi_printf(func
, ctx
, " %s: %s", mechname
, flags
);
1166 static struct hx509_keyset_ops keyset_pkcs11
= {
1180 #endif /* HAVE_DLOPEN */
1183 _hx509_ks_pkcs11_register(hx509_context context
)
1186 _hx509_ks_register(context
, &keyset_pkcs11
);