Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 1 | /* |
| 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 Carriere | fa7e34f | 2020-02-04 15:34:16 +0100 | [diff] [blame^] | 14 | #include <ck_debug.h> |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 15 | #include <inttypes.h> |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 16 | #include <pkcs11.h> |
Etienne Carriere | fa7e34f | 2020-02-04 15:34:16 +0100 | [diff] [blame^] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 20 | |
| 21 | #include "xtest_test.h" |
| 22 | #include "xtest_helpers.h" |
| 23 | |
| 24 | static 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 Carriere | 21f4e3c | 2020-02-05 15:40:24 +0100 | [diff] [blame] | 45 | |
| 46 | rv = C_Finalize(NULL); |
| 47 | ADBG_EXPECT_CK_RESULT(c, CKR_CRYPTOKI_NOT_INITIALIZED, rv); |
Etienne Carriere | 109c1d7 | 2019-01-09 11:02:02 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | ADBG_CASE_DEFINE(pkcs11, 1000, xtest_tee_test_1000, |
| 51 | "Initialize and close Cryptoki library"); |
Etienne Carriere | fa7e34f | 2020-02-04 15:34:16 +0100 | [diff] [blame^] | 52 | |
| 53 | static 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 | |
| 130 | out: |
| 131 | free(slot_ids); |
| 132 | |
| 133 | rv = C_Finalize(NULL); |
| 134 | ADBG_EXPECT_CK_OK(c, rv); |
| 135 | } |
| 136 | |
| 137 | ADBG_CASE_DEFINE(pkcs11, 1001, xtest_tee_test_1001, |
| 138 | "PKCS11: List PKCS#11 slots and get information from"); |