Skip to content

Commit

Permalink
C_TokenInit: Fix double init issues
Browse files Browse the repository at this point in the history
The spec allows for re-initilization of existing tokens, however, the
current implementation does not allow for this, so return
CKR_DEVICE_ERROR for now and follow up with bug #576.

Fixes: #577

Signed-off-by: William Roberts <william.c.roberts@intel.com>
  • Loading branch information
William Roberts committed Aug 3, 2020
1 parent 14aed31 commit f145c7a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib/token.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ CK_RV token_init(token *t, CK_BYTE_PTR pin, CK_ULONG pin_len, CK_BYTE_PTR label)

CK_RV rv = CKR_GENERAL_ERROR;

/* Bug:
* https://github.com/tpm2-software/tpm2-pkcs11/issues/576
*/
if (t->config.is_initialized) {
LOGE("Tokens cannot be re-initialized");
return CKR_DEVICE_ERROR;
}

twist newauth = NULL;
twist newsalthex = NULL;

Expand Down
15 changes: 15 additions & 0 deletions test/integration/pkcs-login-logout.int.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,17 @@ static void test_so_state_pin_init_good(void **state) {
logout(handle);
}

static void test_double_init_token(void **state) {

test_info *ti = test_info_from_state(state);
CK_SLOT_ID slot = ti->slots[0].slot_id;

unsigned char sopin[] = GOOD_SOPIN;
CK_BYTE label[32 + 1] = "doubleinittoken ";
CK_RV rv = C_InitToken(slot, sopin, sizeof(sopin) - 1, label);
assert_int_equal(rv, CKR_DEVICE_ERROR);
}

int main() {

const struct CMUnitTest tests[] = {
Expand Down Expand Up @@ -705,6 +716,10 @@ int main() {
*/
cmocka_unit_test_setup_teardown(test_so_state_pin_init_good,
test_setup_rw, test_teardown),

/* These work on new tokens via slots and thus don't matter the order */
cmocka_unit_test_setup_teardown(test_double_init_token,
test_setup_ro, test_teardown),
};

return cmocka_run_group_tests(tests, _group_setup_locking, _group_teardown);
Expand Down

0 comments on commit f145c7a

Please sign in to comment.