blob: a3edb8f61a20ea553eaef375c5b91dad98bc23a1 [file] [log] [blame]
Etienne Carriere109c1d72019-01-09 11:02:02 +01001/*
2 * Copyright (c) 2018, Linaro Limited
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Etienne Carrierefa7e34f2020-02-04 15:34:16 +010014#include <ck_debug.h>
Etienne Carriere109c1d72019-01-09 11:02:02 +010015#include <inttypes.h>
Etienne Carriere109c1d72019-01-09 11:02:02 +010016#include <pkcs11.h>
Etienne Carrierefa7e34f2020-02-04 15:34:16 +010017#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Etienne Carriere109c1d72019-01-09 11:02:02 +010020
21#include "xtest_test.h"
22#include "xtest_helpers.h"
23
24static void xtest_tee_test_1000(ADBG_Case_t *c)
25{
26 CK_RV rv;
27
28 rv = C_Initialize(NULL);
29 if (!ADBG_EXPECT_CK_OK(c, rv))
30 return;
31
32 rv = C_Finalize(NULL);
33 if (!ADBG_EXPECT_CK_OK(c, rv))
34 return;
35
36 rv = C_Initialize(NULL);
37 if (!ADBG_EXPECT_CK_OK(c, rv))
38 return;
39
40 rv = C_Initialize(NULL);
41 ADBG_EXPECT_CK_RESULT(c, CKR_CRYPTOKI_ALREADY_INITIALIZED, rv);
42
43 rv = C_Finalize(NULL);
44 ADBG_EXPECT_CK_OK(c, rv);
Etienne Carriere21f4e3c2020-02-05 15:40:24 +010045
46 rv = C_Finalize(NULL);
47 ADBG_EXPECT_CK_RESULT(c, CKR_CRYPTOKI_NOT_INITIALIZED, rv);
Etienne Carriere109c1d72019-01-09 11:02:02 +010048}
49
50ADBG_CASE_DEFINE(pkcs11, 1000, xtest_tee_test_1000,
51 "Initialize and close Cryptoki library");
Etienne Carrierefa7e34f2020-02-04 15:34:16 +010052
53static void xtest_tee_test_1001(ADBG_Case_t *c)
54{
55 CK_RV rv = CKR_GENERAL_ERROR;
56 CK_SLOT_ID_PTR slot_ids = NULL;
57 CK_ULONG slot_count = 0;
58 CK_ULONG present_slot_count = 0;
59 CK_INFO lib_info = { };
60 CK_SLOT_INFO slot_info = { };
61 CK_FUNCTION_LIST_PTR ckfunc_list = NULL;
62 size_t i = 0;
63 CK_SLOT_ID max_slot_id = 0;
64
65 rv = C_Initialize(NULL);
66 if (!ADBG_EXPECT_CK_OK(c, rv))
67 return;
68
69 rv = C_GetFunctionList(&ckfunc_list);
70 if (!ADBG_EXPECT_CK_OK(c, rv))
71 goto out;
72
73 if (!ADBG_EXPECT_NOT_NULL(c, ckfunc_list->C_GetInfo) ||
74 !ADBG_EXPECT_NOT_NULL(c, ckfunc_list->C_GetSlotList) ||
75 !ADBG_EXPECT_NOT_NULL(c, ckfunc_list->C_GetSlotInfo))
76 goto out;
77
78 rv = C_GetInfo(&lib_info);
79 if (!ADBG_EXPECT_CK_OK(c, rv))
80 goto out;
81
82 rv = C_GetSlotList(0, NULL, &slot_count);
83 if (!ADBG_EXPECT_CK_OK(c, rv))
84 goto out;
85
86 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, slot_count, !=, 0))
87 goto out;
88
89 rv = C_GetSlotList(1, NULL, &present_slot_count);
90 if (!ADBG_EXPECT_CK_OK(c, rv))
91 goto out;
92
93 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, slot_count, ==,
94 present_slot_count))
95 goto out;
96
97 slot_ids = calloc(slot_count, sizeof(CK_SLOT_ID));
98 if (!ADBG_EXPECT_NOT_NULL(c, slot_ids))
99 goto out;
100
101 slot_count--;
102 rv = C_GetSlotList(1, slot_ids, &slot_count);
103 if (!ADBG_EXPECT_CK_RESULT(c, CKR_BUFFER_TOO_SMALL, rv))
104 goto out;
105
106 rv = C_GetSlotList(1, slot_ids, &slot_count);
107 if (!ADBG_EXPECT_CK_OK(c, rv))
108 goto out;
109
110 for (i = 0; i < slot_count; i++) {
111 CK_SLOT_ID slot = slot_ids[i];
112
113 rv = C_GetSlotInfo(slot, &slot_info);
114 if (!ADBG_EXPECT_CK_OK(c, rv))
115 goto out;
116
117 if (max_slot_id < slot)
118 max_slot_id = slot;
119 }
120
121 /* Test invalid slot/token IDs */
122 rv = C_GetSlotInfo(max_slot_id + 1, &slot_info);
123 if (!ADBG_EXPECT_CK_RESULT(c, CKR_SLOT_ID_INVALID, rv))
124 goto out;
125
126 rv = C_GetSlotInfo(ULONG_MAX, &slot_info);
127 if (!ADBG_EXPECT_CK_RESULT(c, CKR_SLOT_ID_INVALID, rv))
128 goto out;
129
130out:
131 free(slot_ids);
132
133 rv = C_Finalize(NULL);
134 ADBG_EXPECT_CK_OK(c, rv);
135}
136
137ADBG_CASE_DEFINE(pkcs11, 1001, xtest_tee_test_1001,
138 "PKCS11: List PKCS#11 slots and get information from");