Skip to content
Open
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ Enables debugging printf's for store.
Removes default implementation of storage functions.
See wolfpkcs11/store.h for prototypes of functions to implement.

#### Define WOLFPKCS11_CUSTOM_STORE_CLOSE_COMMIT

With `WOLFPKCS11_CUSTOM_STORE`, records that were written are closed with
`wolfPKCS11_Store_CloseCommit()` instead of `wolfPKCS11_Store_Close()`, so the
custom store must implement it too (`wolfPKCS11_Store_Close()` still closes
records that were read). It returns non-zero when the record did not commit,
for example when a flash write failed. wolfPKCS11 then fails the operation and
writes all records of the token on its next store. Without this define, the
custom store cannot report a failed commit.

#### Define WOLFPKCS11_KEYPAIR_GEN_COMMON_LABEL

Sets the private key's label against the public key when generating key pairs.
Expand Down
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,15 @@ AX_HARDEN_CC_COMPILER_FLAGS

OPTION_FLAGS="$CFLAGS $CPPFLAGS $AM_CFLAGS"

# The custom store tests build the library with WOLFPKCS11_CUSTOM_STORE. NSS
# needs the file store, and a custom store cannot be built with the TPM store.
AS_CASE([" $OPTION_FLAGS "],
[*" -DWOLFPKCS11_NSS "*|*" -DWOLFPKCS11_TPM_STORE "*],
[ENABLED_CUSTOM_STORE_TEST=no],
[ENABLED_CUSTOM_STORE_TEST=yes])
AM_CONDITIONAL([BUILD_CUSTOM_STORE_TEST],
[test "x$ENABLED_CUSTOM_STORE_TEST" = "xyes"])

AS_IF([test "x$WOLFPKCS11_STATIC_WOLFSSL" = "xyes"],
[WOLFPKCS11_CONFIG_LIBS="$LIBS -lm"
LIBS="$STATIC_WOLFSSL $LIBS -lm"],
Expand Down
65 changes: 57 additions & 8 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2999,7 +2999,7 @@ static int wolfPKCS11_Store_TpmCommit(WP11_TpmStore* tpmStore)
* @return 0 on success.
* @return Other value when written data could not be committed.
*/
static int wolfPKCS11_Store_CloseCommit(void* store, int commit)
int wolfPKCS11_Store_CloseCommit(void* store, int commit)
{
int ret = 0;
#ifdef WOLFPKCS11_TPM_STORE
Expand Down Expand Up @@ -3220,6 +3220,24 @@ int wolfPKCS11_Store_Write(void* store, unsigned char* buffer, int len)
}
#endif

#ifdef WOLFPKCS11_CUSTOM_STORE
/*
* Maps a custom store's failure to open to BUFFER_E.
* The store's own code could otherwise read as another status, such as
* PIN_INVALID_E. NOT_AVAILABLE_E is kept: the data is not stored.
*
* @param [in] ret Result of opening.
* @return 0 or NOT_AVAILABLE_E as passed in.
* @return BUFFER_E for any other failure.
*/
static int wp11_storage_open_ret(int ret)
{
if (ret != 0 && ret != NOT_AVAILABLE_E)
ret = BUFFER_E;
return ret;
}
#endif

/*
* Opens access to location to read token data.
*
Expand All @@ -3236,7 +3254,12 @@ int wolfPKCS11_Store_Write(void* store, unsigned char* buffer, int len)
static int wp11_storage_open_readonly(int type, CK_ULONG id1, CK_ULONG id2,
void** storage)
{
return wolfPKCS11_Store_Open(type, id1, id2, 1, storage);
int ret = wolfPKCS11_Store_Open(type, id1, id2, 1, storage);

#ifdef WOLFPKCS11_CUSTOM_STORE
ret = wp11_storage_open_ret(ret);
#endif
return ret;
}

/*
Expand All @@ -3257,8 +3280,14 @@ static int wp11_storage_open(int type, CK_ULONG id1, CK_ULONG id2,
#ifdef WOLFPKCS11_TPM_STORE
return wolfPKCS11_Store_OpenSz(type, id1, id2, 0, variableSz, storage);
#else
int ret;

(void)variableSz; /* not used */
return wolfPKCS11_Store_Open(type, id1, id2, 0, storage);
ret = wolfPKCS11_Store_Open(type, id1, id2, 0, storage);
#ifdef WOLFPKCS11_CUSTOM_STORE
ret = wp11_storage_open_ret(ret);
#endif
return ret;
#endif
}

Expand Down Expand Up @@ -3289,11 +3318,19 @@ static void wp11_storage_close(void* storage)
*/
static int wp11_storage_close_write(void* storage, int ret)
{
#ifdef WOLFPKCS11_CUSTOM_STORE
#if defined(WOLFPKCS11_CUSTOM_STORE) && \
!defined(WOLFPKCS11_CUSTOM_STORE_CLOSE_COMMIT)
/* The store cannot report a failed commit. */
wolfPKCS11_Store_Close(storage);
#else
int closeRet = wolfPKCS11_Store_CloseCommit(storage, ret == 0);

#ifdef WOLFPKCS11_CUSTOM_STORE
/* Any failure means the data did not commit. A custom store's code could
* otherwise read as NOT_AVAILABLE_E, which means storage is turned off. */
if (closeRet != 0)
closeRet = BUFFER_E;
#endif
if (ret == 0)
ret = closeRet;
#endif
Expand Down Expand Up @@ -3351,6 +3388,13 @@ static int wp11_storage_read(void* storage, unsigned char* buffer, int len)
/* Failed to read all data. */
ret = BUFFER_E;
}
#ifdef WOLFPKCS11_CUSTOM_STORE
else {
/* A custom store's failure code could otherwise read as another
* status, such as PIN_INVALID_E. */
ret = BUFFER_E;
}
#endif

return ret;
}
Expand Down Expand Up @@ -3425,8 +3469,9 @@ static int wp11_storage_write(void* storage, unsigned char* buffer, int len)
/* All written successfully. */
ret = 0;
}
else if (ret == 0) {
/* Failed to write all data. */
else {
/* Failed to write all data. A custom store's failure code could
* otherwise read as another status, such as PIN_INVALID_E. */
ret = BUFFER_E;
}

Expand Down Expand Up @@ -5007,7 +5052,9 @@ static int wp11_Object_Load_Data(WP11_Object* object, int tokenId, int objId)
object->data.genericData.objectIdLen = (word32)tempLen;
}

wp11_storage_close(storage);
/* Nothing to close when the open failed. */
if (storage != NULL)
wp11_storage_close(storage);

return ret;
}
Expand Down Expand Up @@ -5577,7 +5624,9 @@ static int wp11_Object_Store_Data(WP11_Object* object, int tokenId, int objId)
object->data.genericData.objectIdLen);
}

ret = wp11_storage_close_write(storage, ret);
/* Nothing to close when the open failed. */
if (storage != NULL)
ret = wp11_storage_close_write(storage, ret);

return ret;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ The test files have the following purposes:
* `pkcs11mtt.c`: is the multithreaded tests
* `pkcs11str.c`: is a storage test
* `debug_test.c`: is a debug functionality demonstration test
* `custom_store_test.c`: is a custom store (`WOLFPKCS11_CUSTOM_STORE`) test. It
builds the library sources in, with and without
`WOLFPKCS11_CUSTOM_STORE_CLOSE_COMMIT`
Loading
Loading