Skip to content

Commit a2b4602

Browse files
committed
Fixes for TPM based RSA key reloading. ZD 20045. Improve WP11_Rsa_GenerateKeyPair exponent logic. Fix for missing HAVE_GETPID in random.h WC_RNG.
1 parent e08d74c commit a2b4602

6 files changed

Lines changed: 671 additions & 7 deletions

File tree

.github/workflows/unit-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: ./.github/workflows/build-workflow.yml
2121
with:
2222
config: --enable-singlethreaded --enable-wolftpm --disable-dh C_EXTRA_FLAGS="-DWOLFPKCS11_TPM_STORE"
23-
check: ./tests/pkcs11str && ./tests/pkcs11test
23+
check: ./tests/pkcs11str && ./tests/pkcs11test && ./tests/rsa_session_persistence_test
2424

2525
no_rsa:
2626
uses: ./.github/workflows/build-workflow.yml

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ tests/wp11_symmkey_*
6060
tests/wp11_token_*
6161
tests/wp11_obj_*
6262
tests/token_path_test
63+
tests/rsa_session_persistence_test
6364

6465
IDE/VisualStudio/.vs

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ AC_CHECK_SIZEOF([long long], 8)
7575
AC_CHECK_SIZEOF([long], 4)
7676

7777
# Check headers/libs
78-
AC_CHECK_FUNCS([gethostbyname getaddrinfo gettimeofday inet_ntoa memset socket])
78+
AC_CHECK_FUNCS([gethostbyname getaddrinfo gettimeofday inet_ntoa memset socket getpid])
7979
AC_CHECK_LIB([network],[socket])
8080

8181
# DEBUG

src/internal.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22-
#include "wolfpkcs11/pkcs11.h"
2322
#ifdef HAVE_CONFIG_H
2423
#include <wolfpkcs11/config.h>
2524
#endif
@@ -2421,9 +2420,21 @@ static int wp11_Object_Decode_RsaKey(WP11_Object* object)
24212420
}
24222421
if (ret == 0) {
24232422
/* load public portion into wolf RsaKey structure */
2423+
object->slot->tpmCtx.rsaKey = (WOLFTPM2_KEY*)&object->tpmKey;
24242424
ret = wolfTPM2_RsaKey_TpmToWolf(&object->slot->tpmDev,
24252425
(WOLFTPM2_KEY*)&object->tpmKey, &object->data.rsaKey);
24262426
}
2427+
if (ret == 0) {
2428+
/* load key into TPM (get handle) */
2429+
if (object->tpmKey.priv.size == 0) {
2430+
ret = wolfTPM2_LoadPublicKey(&object->slot->tpmDev,
2431+
(WOLFTPM2_KEY*)&object->tpmKey, &object->tpmKey.pub);
2432+
}
2433+
else {
2434+
ret = wolfTPM2_LoadKey(&object->slot->tpmDev, &object->tpmKey,
2435+
&object->slot->tpmCtx.storageKey->handle);
2436+
}
2437+
}
24272438
}
24282439
else
24292440
#endif
@@ -8243,21 +8254,22 @@ int WP11_Rsa_GenerateKeyPair(WP11_Object* pub, WP11_Object* priv,
82438254
{
82448255
int ret = 0;
82458256
unsigned char eData[sizeof(long)];
8246-
int i;
8257+
int i, eSz;
82478258
long e = 0;
82488259
WC_RNG rng;
82498260

82508261
/* Use public exponent if public key has one set. */
82518262
if (!mp_iszero(&pub->data.rsaKey.e)) {
82528263
XMEMSET(eData, 0, sizeof(eData));
82538264
/* Public exponent must be size of a long for API. */
8254-
if (mp_unsigned_bin_size(&pub->data.rsaKey.e) > (int)sizeof(eData))
8265+
eSz = mp_unsigned_bin_size(&pub->data.rsaKey.e);
8266+
if (eSz > (int)sizeof(eData))
82558267
ret = BAD_FUNC_ARG;
82568268
if (ret == 0)
8257-
ret = mp_to_unsigned_bin(&pub->data.rsaKey.e, eData);
8269+
ret = mp_to_unsigned_bin_len(&pub->data.rsaKey.e, eData, eSz);
82588270
if (ret == 0) {
82598271
/* Convert big-endian data into number. */
8260-
for (i = sizeof(eData) - 1; i >= 0; i--) {
8272+
for (i = eSz - 1; i >= 0; i--) {
82618273
e <<= 8;
82628274
e |= eData[i];
82638275
}

tests/include.am

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ noinst_PROGRAMS += tests/token_path_test
2121
tests_token_path_test_SOURCES = tests/token_path_test.c
2222
tests_token_path_test_LDADD =
2323

24+
check_PROGRAMS += tests/rsa_session_persistence_test
25+
noinst_PROGRAMS += tests/rsa_session_persistence_test
26+
tests_rsa_session_persistence_test_SOURCES = tests/rsa_session_persistence_test.c
27+
tests_rsa_session_persistence_test_LDADD =
28+
2429
if BUILD_STATIC
2530
tests_pkcs11test_LDADD += src/libwolfpkcs11.la
2631
tests_pkcs11mtt_LDADD += src/libwolfpkcs11.la
2732
tests_pkcs11str_LDADD += src/libwolfpkcs11.la
2833
tests_token_path_test_LDADD += src/libwolfpkcs11.la
34+
tests_rsa_session_persistence_test_LDADD += src/libwolfpkcs11.la
2935
endif
3036

3137
EXTRA_DIST += tests/unit.h \

0 commit comments

Comments
 (0)