Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of invalid object handles #495

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,17 @@ static void p11prov_obj_refresh(P11PROV_OBJ *obj)
p11prov_return_session(session);
}

CK_RV p11prov_obj_refresh_invalid(P11PROV_OBJ *obj)
{
obj->handle = CK_INVALID_HANDLE;
obj->cached = CK_INVALID_HANDLE;
p11prov_obj_refresh(obj);
if (obj->handle == CK_INVALID_HANDLE) {
return CKR_OBJECT_HANDLE_INVALID;
}
return CKR_OK;
}

#define SECRET_KEY_ATTRS 2
P11PROV_OBJ *p11prov_create_secret_key(P11PROV_CTX *provctx,
P11PROV_SESSION *session,
Expand Down
1 change: 1 addition & 0 deletions src/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ CK_RV p11prov_obj_from_handle(P11PROV_CTX *ctx, P11PROV_SESSION *session,
CK_RV p11prov_obj_find(P11PROV_CTX *provctx, P11PROV_SESSION *session,
CK_SLOT_ID slotid, P11PROV_URI *uri,
store_obj_callback cb, void *cb_ctx);
CK_RV p11prov_obj_refresh_invalid(P11PROV_OBJ *obj);
P11PROV_OBJ *p11prov_create_secret_key(P11PROV_CTX *provctx,
P11PROV_SESSION *session,
bool session_key, unsigned char *secret,
Expand Down
9 changes: 9 additions & 0 deletions src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ static CK_RV slot_login(P11PROV_SLOT *slot, P11PROV_URI *uri,
}

/* we acquired the session, check that it is ok */
p11prov_set_error_mark(pool->provctx);
ret = session_check(session, session->flags);
if (ret != CKR_OK) {
num_open_sessions--;
Expand All @@ -816,9 +817,13 @@ static CK_RV slot_login(P11PROV_SLOT *slot, P11PROV_URI *uri,
ret = token_session_open(session, flags);
if (ret == CKR_OK) {
num_open_sessions++;
p11prov_pop_error_to_mark(pool->provctx);
} else {
p11prov_clear_last_error_mark(pool->provctx);
goto done;
}
} else {
p11prov_clear_last_error_mark(pool->provctx);
}

if (is_login_state(session->state)) {
Expand Down Expand Up @@ -1008,6 +1013,7 @@ CK_RV p11prov_get_session(P11PROV_CTX *provctx, CK_SLOT_ID *slotid,

ret = fetch_session(pool, flags, false, &session);
if (ret == CKR_OK) {
p11prov_set_error_mark(pool->provctx);
ret = session_check(session, flags);
if (ret != CKR_OK) {
num_open_sessions--;
Expand All @@ -1017,8 +1023,11 @@ CK_RV p11prov_get_session(P11PROV_CTX *provctx, CK_SLOT_ID *slotid,
ret = token_session_open(session, flags);
if (ret == CKR_OK) {
num_open_sessions++;
p11prov_pop_error_to_mark(pool->provctx);
goto done;
}
}
p11prov_clear_last_error_mark(pool->provctx);
}

done:
Expand Down
16 changes: 14 additions & 2 deletions src/signature.c
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ static CK_RV p11prov_sig_operate_init(P11PROV_SIG_CTX *sigctx, bool digest_op,
}

done:
if (ret != CKR_OK) {
if (ret != CKR_OK && ret != CKR_OBJECT_HANDLE_INVALID) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you do the refreshing in this function, as you should, then this will go away, as it should.
This exception here is a trap for any of the callers.

p11prov_return_session(session);
} else {
*_session = session;
Expand Down Expand Up @@ -935,10 +935,22 @@ static CK_RV p11prov_sig_operate(P11PROV_SIG_CTX *sigctx, unsigned char *sig,
tbslen += mech->der_digestinfo_len;
}

p11prov_set_error_mark(sigctx->provctx);
ret = p11prov_sig_operate_init(sigctx, false, &session);
if (ret != CKR_OK) {
return ret;
if (ret == CKR_OBJECT_HANDLE_INVALID && p11prov_obj_refresh_invalid(sigctx->key) == CKR_OK) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should move this whole section directly in p11prov_sig_operate_init()

ret = p11prov_sig_operate_init(sigctx, false, &session);
}
if (ret != CKR_OK) {
p11prov_clear_last_error_mark(sigctx->provctx);
return ret;
} else {
p11prov_pop_error_to_mark(sigctx->provctx);
}
} else {
p11prov_clear_last_error_mark(sigctx->provctx);
}

sess = p11prov_session_handle(session);

if (sigctx->operation == CKF_SIGN) {
Expand Down
Loading