1 /* Test for PKCS#11 calls. */
2 
3 #include <stdio.h>
4 #include <security/cryptoki.h>
5 #include <security/pkcs11.h>
6 
7 int main(void)
8 {
9    CK_RV ret = C_Initialize(NULL);
10    if (ret != CKR_OK) {
11       fprintf(stderr, "Initialize: %lu\n", ret);
12       return 1;
13    }
14 
15    CK_ULONG slot_count;
16    ret = C_GetSlotList(0, NULL, &slot_count);
17    if (ret != CKR_OK) {
18       fprintf(stderr, "GetSlotList(NULL): %lu\n", ret);
19       return 1;
20    }
21 
22    CK_SLOT_ID_PTR slots = malloc(slot_count * sizeof(CK_SLOT_ID));
23    if (slots == NULL) {
24       fprintf(stderr, "malloc(slots)\n");
25       return 1;
26    }
27 
28    ret = C_GetSlotList(0, slots, &slot_count);
29    if (ret != CKR_OK) {
30       fprintf(stderr, "GetSlotList(slots): %lu\n", ret);
31       return 1;
32    }
33 
34    CK_ULONG i;
35    for (i = 0; i < slot_count; i++) {
36       CK_SLOT_ID slot_id = slots[i];
37 
38       CK_ULONG mech_count;
39       ret = C_GetMechanismList(slot_id, NULL, &mech_count);
40       if (ret != CKR_OK) {
41          fprintf(stderr, "GetMechanismList(NULL): %lu\n", ret);
42          return 1;
43       }
44 
45       CK_MECHANISM_TYPE_PTR mechs = malloc(mech_count * sizeof(CK_MECHANISM_TYPE));
46       if (slots == NULL) {
47          fprintf(stderr, "malloc(mechs)\n");
48          return 1;
49       }
50 
51       ret = C_GetMechanismList(slot_id, mechs, &mech_count);
52       if (ret != CKR_OK) {
53          fprintf(stderr, "GetMechanismList(mechs): %lu\n", ret);
54          return 1;
55       }
56 
57       free(mechs);
58    }
59 
60    free(slots);
61    C_Finalize(NULL_PTR);
62    return 0;
63 }
64 
65