Skip to content

Commit bc4a74a

Browse files
ambarusehristev
authored andcommitted
cryptoauthlib: Add pkcs11 fixes
The first patch fixes the deadlock in pkcs11_token_init. The code tried to grab a mutex twice. The second patch fixes how the user's PIN is handled. The user's PIN is eventually saved in the ATECCx08 device in a 32 byte data slot. Signed-off-by: Tudor Ambarus <[email protected]>
1 parent 8c461b2 commit bc4a74a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
From 9b56d82f3ee6ea4b6d82b08ee252c40c76f19a76 Mon Sep 17 00:00:00 2001
2+
From: Tudor Ambarus <[email protected]>
3+
Date: Mon, 18 Jan 2021 17:32:23 +0200
4+
Subject: [PATCH 1/2] pkcs11: Fix deadlock in pkcs11_token_init
5+
6+
pkcs11_token_init() grabs the pContext->mutex at the beginning
7+
of the function, locking the library. Later in the same function,
8+
the code tries to grab the same lock, while already held. Mutexes
9+
are not recursive in linux, resulting in a deadlock. Drop the
10+
second attempt of grabbing the lock.
11+
12+
Signed-off-by: Tudor Ambarus <[email protected]>
13+
---
14+
lib/pkcs11/pkcs11_token.c | 6 +-----
15+
1 file changed, 1 insertion(+), 5 deletions(-)
16+
17+
diff --git a/lib/pkcs11/pkcs11_token.c b/lib/pkcs11/pkcs11_token.c
18+
index 7bcfd629181c..a40f822c93ec 100644
19+
--- a/lib/pkcs11/pkcs11_token.c
20+
+++ b/lib/pkcs11/pkcs11_token.c
21+
@@ -242,11 +242,7 @@ CK_RV pkcs11_token_init(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinL
22+
{
23+
if (64 != ulPinLen)
24+
{
25+
- if (CKR_OK == (rv = pkcs11_lock_context(pLibCtx)))
26+
- {
27+
- rv = pkcs11_util_convert_rv(atcab_read_serial_number(buf));
28+
- (void)pkcs11_unlock_context(pLibCtx);
29+
- }
30+
+ rv = pkcs11_util_convert_rv(atcab_read_serial_number(buf));
31+
32+
if (CKR_OK == rv)
33+
{
34+
--
35+
2.17.1
36+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
From 8dc0004e154753913f7c7fceb034db4c0e2a7784 Mon Sep 17 00:00:00 2001
2+
From: Tudor Ambarus <[email protected]>
3+
Date: Thu, 21 Jan 2021 15:09:44 +0200
4+
Subject: [PATCH 2/2] pkcs11: Fix user's PIN usage
5+
6+
1/ The PIN size is 32 bytes, update buffer length accordingly
7+
2/ Add condition to check so_pin_handle is not pointing to
8+
default value
9+
3/ Update KeyLen condition in pkcs11_token_convert_pin_to_key.
10+
11+
Signed-off-by: Tudor Ambarus <[email protected]>
12+
---
13+
lib/pkcs11/pkcs11_token.c | 6 +++---
14+
1 file changed, 3 insertions(+), 3 deletions(-)
15+
16+
diff --git a/lib/pkcs11/pkcs11_token.c b/lib/pkcs11/pkcs11_token.c
17+
index a40f822c93ec..92b633220513 100644
18+
--- a/lib/pkcs11/pkcs11_token.c
19+
+++ b/lib/pkcs11/pkcs11_token.c
20+
@@ -125,7 +125,7 @@ CK_RV pkcs11_token_init(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinL
21+
{
22+
#if PKCS11_TOKEN_INIT_SUPPORT
23+
CK_RV rv;
24+
- uint8_t buf[34];
25+
+ uint8_t buf[32];
26+
uint8_t * pConfig = NULL;
27+
bool lock = false;
28+
pkcs11_lib_ctx_ptr pLibCtx;
29+
@@ -255,7 +255,7 @@ CK_RV pkcs11_token_init(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinL
30+
rv = pkcs11_token_convert_pin_to_key(pPin, ulPinLen, NULL, 0, buf, buflen);
31+
}
32+
33+
- if (CKR_OK == rv)
34+
+ if ((CKR_OK == rv) && (pSlotCtx->so_pin_handle != 0xFFFF))
35+
{
36+
if (atcab_is_ca_device(pSlotCtx->interface_config.devtype))
37+
{
38+
@@ -577,7 +577,7 @@ CK_RV pkcs11_token_convert_pin_to_key(
39+
{
40+
ATCA_STATUS status = ATCA_SUCCESS;
41+
42+
- if (!pPin || !ulPinLen || !pKey || 32 != ulKeyLen)
43+
+ if (!pPin || !ulPinLen || !pKey || 32 > ulKeyLen)
44+
{
45+
return CKR_ARGUMENTS_BAD;
46+
}
47+
--
48+
2.17.1
49+

0 commit comments

Comments
 (0)