Skip to content

Commit 42da9cc

Browse files
committed
Store the current object ID in token
When a token is stored and then reloaded during `C_Initialize`, the token object IDs are reset to zero. Which means new objects will end up with the same IDs as loaded objects. This is probably a "bad thing", so let's not do that. With this patch the next object ID is stored with the token.
1 parent 1c3df16 commit 42da9cc

5 files changed

Lines changed: 558 additions & 6 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ coverage.info
3434
tests/pkcs11test
3535
tests/pkcs11mtt
3636
tests/pkcs11str
37+
tests/object_id_uniqueness_test
38+
tests/rsa_session_persistence_test
39+
tests/debug_test
40+
tests/token_path_test
3741
examples/add_aes_key
3842
examples/add_hmac_key
3943
examples/add_rsa_key

src/internal.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ typedef struct WP11_Token {
477477
WP11_Object* object; /* Linked list of token objects */
478478
int objCnt; /* Count of objects on token */
479479
int tokenFlags; /* Flags for token */
480+
int nextObjId;
480481
} WP11_Token;
481482

482483
struct WP11_Slot {
@@ -486,7 +487,6 @@ struct WP11_Slot {
486487
WP11_Lock lock; /* Lock for access to slot info */
487488

488489
int devId;
489-
int nextObjId;
490490
#ifdef WOLFPKCS11_TPM
491491
WOLFTPM2_DEV tpmDev;
492492
WOLFTPM2_KEY tpmSrk;
@@ -880,6 +880,7 @@ static int wolfPKCS11_Store_GetMaxSize(int type, int variableSz)
880880
FIELD_SIZE(WP11_Token, seed) +
881881
FIELD_SIZE(WP11_Token, objCnt) +
882882
FIELD_SIZE(WP11_Token, tokenFlags) +
883+
FIELD_SIZE(WP11_Token, nextObjId) +
883884
variableSz /* soPinLen + userPinLen + (objCnt * long) */
884885
;
885886
break;
@@ -3987,6 +3988,7 @@ static int wp11_Token_Init(WP11_Token* token, const char* label)
39873988
if (ret == 0) {
39883989
token->state = WP11_TOKEN_STATE_INITIALIZED;
39893990
token->loginState = WP11_APP_STATE_RW_PUBLIC;
3991+
token->nextObjId = 1;
39903992
XMEMCPY(token->label, label, sizeof(token->label));
39913993
}
39923994

@@ -4130,8 +4132,16 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
41304132
if (token->soPinLen > 0) {
41314133
token->tokenFlags |= WP11_TOKEN_FLAG_SO_PIN_SET;
41324134
}
4135+
token->nextObjId = 1;
41334136
ret = 0;
41344137
}
4138+
else {
4139+
ret = wp11_storage_read_int(storage, &token->nextObjId);
4140+
if (ret == BUFFER_E || token->nextObjId == 0) {
4141+
token->nextObjId = 1;
4142+
ret = 0;
4143+
}
4144+
}
41354145
}
41364146

41374147
wp11_storage_close(storage);
@@ -4264,6 +4274,11 @@ static int wp11_Token_Store(WP11_Token* token, int tokenId)
42644274
ret = wp11_storage_write_int(storage, token->tokenFlags);
42654275
}
42664276

4277+
if (ret == 0) {
4278+
/* Write next object id. (4) */
4279+
ret = wp11_storage_write_int(storage, token->nextObjId);
4280+
}
4281+
42674282
wp11_storage_close(storage);
42684283

42694284
object = token->object;
@@ -4414,7 +4429,6 @@ static int wp11_Slot_Init(WP11_Slot* slot, int id)
44144429

44154430
XMEMSET(slot, 0, sizeof(*slot));
44164431
slot->id = id;
4417-
slot->nextObjId = 1;
44184432
slot->token.state = WP11_TOKEN_STATE_UNKNOWN;
44194433
slot->token.tokenFlags = 0;
44204434

@@ -6074,7 +6088,7 @@ int WP11_Session_AddObject(WP11_Session* session, int onToken,
60746088
/* Get next item in list after this object has been added. */
60756089
next = token->object;
60766090
/* Determine handle value */
6077-
object->handle = OBJ_HANDLE(onToken, session->slot->nextObjId++);
6091+
object->handle = OBJ_HANDLE(onToken, token->nextObjId++);
60786092
object->next = next;
60796093
token->object = object;
60806094
}
@@ -6092,7 +6106,7 @@ int WP11_Session_AddObject(WP11_Session* session, int onToken,
60926106
/* Get next item in list after this object has been added. */
60936107
next = session->object;
60946108
/* Determine handle value */
6095-
object->handle = OBJ_HANDLE(onToken, session->slot->nextObjId++);
6109+
object->handle = OBJ_HANDLE(onToken, token->nextObjId++);
60966110
object->next = next;
60976111
session->object = object;
60986112
object->session = session;

tests/include.am

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ noinst_PROGRAMS += tests/debug_test
3131
tests_debug_test_SOURCES = tests/debug_test.c
3232
tests_debug_test_LDADD =
3333

34+
check_PROGRAMS += tests/object_id_uniqueness_test
35+
noinst_PROGRAMS += tests/object_id_uniqueness_test
36+
tests_object_id_uniqueness_test_SOURCES = tests/object_id_uniqueness_test.c
37+
tests_object_id_uniqueness_test_LDADD =
38+
3439
if BUILD_STATIC
3540
tests_pkcs11test_LDADD += src/libwolfpkcs11.la
3641
tests_pkcs11mtt_LDADD += src/libwolfpkcs11.la
3742
tests_pkcs11str_LDADD += src/libwolfpkcs11.la
3843
tests_token_path_test_LDADD += src/libwolfpkcs11.la
3944
tests_rsa_session_persistence_test_LDADD += src/libwolfpkcs11.la
4045
tests_debug_test_LDADD += src/libwolfpkcs11.la
46+
tests_object_id_uniqueness_test_LDADD += src/libwolfpkcs11.la
4147
else
4248
tests_debug_test_LDADD += src/libwolfpkcs11.la
49+
tests_object_id_uniqueness_test_LDADD += src/libwolfpkcs11.la
4350
endif
4451

4552
EXTRA_DIST += tests/unit.h \

0 commit comments

Comments
 (0)