Skip to content

Commit cb645d3

Browse files
authored
Merge pull request #390 from dgarske/oldcrypt
Support for building wolfTPM against older wolfCrypt (like v4.7.0)
2 parents 415f714 + 88f57f3 commit cb645d3

File tree

7 files changed

+98
-35
lines changed

7 files changed

+98
-35
lines changed

.github/workflows/make-test-swtpm.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,29 @@ jobs:
202202
make check
203203
WOLFSSL_PATH=./wolfssl NO_FILESYSTEM=1 ./examples/run_examples.sh
204204
205+
# test with older wolfCrypt (v4.7.0)
206+
- uses: actions/checkout@master
207+
with:
208+
repository: wolfssl/wolfssl
209+
path: wolfssl-old
210+
ref: v4.7.0-stable
211+
- name: wolfssl old
212+
working-directory: ./wolfssl
213+
run: |
214+
./configure --enable-wolftpm CFLAGS="-DWOLFSSL_PUBLIC_MP -DWOLFSSL_TEST_CERT -DWOLFSSL_KEY_GEN"
215+
make
216+
sudo make install
217+
- name: wolftpm with old wolfssl
218+
# Old wolfSSL before PR #5075 does not support using a public key in place of private key with
219+
# crypto callbacks enabled.
220+
# To use PKCS7 or TLS Server a dummy private key must be used for older wolfSSL versions.
221+
# Use newer wolfSSL TLS client/server to resolve test certificate expirations
222+
run: |
223+
./configure --enable-swtpm
224+
make
225+
make check
226+
WOLFSSL_PATH=./wolfssl NO_PUBASPRIV=1 ./examples/run_examples.sh
227+
205228
# capture logs on failure
206229
- name: Upload failure logs
207230
if: failure()

examples/csr/csr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static int TPM2_CSR_Generate(WOLFTPM2_DEV* dev, int keyType, WOLFTPM2_KEY* key,
8787
#ifdef WOLFTPM2_NO_HEAP
8888
/* single shot API for CSR generation */
8989
rc = wolfTPM2_CSR_Generate_ex(dev, key, subject, keyUsage,
90-
CTC_FILETYPE_PEM, output, outputSz, sigType, makeSelfSignedCert,
90+
ENCODING_TYPE_PEM, output, outputSz, sigType, makeSelfSignedCert,
9191
devId);
9292
#else
9393
rc = wolfTPM2_CSR_SetSubject(dev, csr, subject);
@@ -104,7 +104,7 @@ static int TPM2_CSR_Generate(WOLFTPM2_DEV* dev, int keyType, WOLFTPM2_KEY* key,
104104
}
105105
}
106106
if (rc == 0) {
107-
rc = wolfTPM2_CSR_MakeAndSign_ex(dev, csr, key, CTC_FILETYPE_PEM,
107+
rc = wolfTPM2_CSR_MakeAndSign_ex(dev, csr, key, ENCODING_TYPE_PEM,
108108
output, outputSz, sigType, makeSelfSignedCert, devId);
109109
}
110110
#endif

examples/endorsement/get_ek_certs.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,13 @@ int TPM2_EndorsementCert_Example(void* userCtx, int argc, char *argv[])
278278
if (rc == 0) {
279279
/* Attempt to parse certificate */
280280
printf("Parsing certificate (%d bytes)\n", certSz);
281+
#ifdef WOLFSSL_TEST_CERT
282+
InitDecodedCert(&cert, certBuf, certSz, NULL);
283+
rc = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
284+
#else
281285
wc_InitDecodedCert(&cert, certBuf, certSz, NULL);
282286
rc = wc_ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
287+
#endif
283288
if (rc == 0) {
284289
printf("\tSuccessfully parsed\n");
285290

@@ -338,7 +343,11 @@ int TPM2_EndorsementCert_Example(void* userCtx, int argc, char *argv[])
338343
printf("Error parsing certificate! %s (%d)\n",
339344
TPM2_GetRCString(rc), rc);
340345
}
346+
#ifdef WOLFSSL_TEST_CERT
347+
FreeDecodedCert(&cert);
348+
#else
341349
wc_FreeDecodedCert(&cert);
350+
#endif
342351

343352
#ifndef WOLFCRYPT_ONLY
344353
if (rc == 0) {

examples/pkcs7/pkcs7.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@
4646

4747
/* The PKCS7 EX functions were added after v3.15.3 */
4848
#include <wolfssl/version.h>
49-
#if defined(LIBWOLFSSL_VERSION_HEX) && \
50-
LIBWOLFSSL_VERSION_HEX > 0x03015003
49+
#if defined(LIBWOLFSSL_VERSION_HEX) && LIBWOLFSSL_VERSION_HEX > 0x03015003
5150
#undef ENABLE_PKCS7EX_EXAMPLE
5251
#define ENABLE_PKCS7EX_EXAMPLE
5352
#endif
5453

54+
#if defined(LIBWOLFSSL_VERSION_HEX) && LIBWOLFSSL_VERSION_HEX < 0x05007004
55+
/* PKCS7 renamed to wc_PKCS7 */
56+
#define wc_PKCS7 PKCS7
57+
#endif
58+
5559
#ifndef MAX_PKCS7_SIZE
5660
#define MAX_PKCS7_SIZE MAX_CONTEXT_SIZE
5761
#endif

examples/run_examples.sh

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ fi
1313
if [ -z "$NO_FILESYSTEM" ]; then
1414
NO_FILESYSTEM=0
1515
fi
16+
if [ -z "$NO_PUBASPRIV" ]; then
17+
NO_PUBASPRIV=0
18+
fi
1619
if [ -z "$WOLFCRYPT_DEFAULT" ]; then
1720
WOLFCRYPT_DEFAULT=0
1821
fi
@@ -337,7 +340,7 @@ fi
337340

338341
# PKCS7 Tests
339342
echo -e "PKCS7 tests"
340-
if [ $WOLFCRYPT_ENABLE -eq 1 ] && [ $WOLFCRYPT_DEFAULT -eq 0 ] && [ $NO_FILESYSTEM -eq 0 ]; then
343+
if [ $WOLFCRYPT_ENABLE -eq 1 ] && [ $WOLFCRYPT_DEFAULT -eq 0 ] && [ $NO_FILESYSTEM -eq 0 ] && [ $NO_PUBASPRIV -eq 0 ]; then
341344
./examples/pkcs7/pkcs7 >> run.out 2>&1
342345
RESULT=$?
343346
[ $RESULT -ne 0 ] && echo -e "pkcs7 failed! $RESULT" && exit 1
@@ -400,22 +403,25 @@ if [ $WOLFCRYPT_ENABLE -eq 1 ] && [ $WOLFCRYPT_DEFAULT -eq 0 ] && [ $NO_FILESYST
400403
run_tpm_tls_client "rsa" "" "4"
401404
run_tpm_tls_client "rsa" "-aes" "4"
402405

403-
run_tpm_tls_server "rsa" "" "3"
404-
run_tpm_tls_server "rsa" "-aes" "3"
405-
run_tpm_tls_server "rsa" "" "4"
406-
run_tpm_tls_server "rsa" "-aes" "4"
406+
if [ $NO_PUBASPRIV -eq 0 ]; then
407+
run_tpm_tls_server "rsa" "" "3"
408+
run_tpm_tls_server "rsa" "-aes" "3"
409+
run_tpm_tls_server "rsa" "" "4"
410+
run_tpm_tls_server "rsa" "-aes" "4"
411+
fi
407412

408413
# TLS client/server ECC TLS v1.2 and v1.3 PK callbacks
409414
run_tpm_tls_client "rsa" "-pk" "3"
410415
run_tpm_tls_client "rsa" "-pk -aes" "3"
411416
run_tpm_tls_client "rsa" "-pk" "4"
412417
run_tpm_tls_client "rsa" "-pk -aes" "4"
413418

414-
run_tpm_tls_server "rsa" "-pk " "3"
415-
run_tpm_tls_server "rsa" "-pk -aes" "3"
416-
run_tpm_tls_server "rsa" "-pk " "4"
417-
run_tpm_tls_server "rsa" "-pk -aes" "4"
418-
419+
if [ $NO_PUBASPRIV -eq 0 ]; then
420+
run_tpm_tls_server "rsa" "-pk " "3"
421+
run_tpm_tls_server "rsa" "-pk -aes" "3"
422+
run_tpm_tls_server "rsa" "-pk " "4"
423+
run_tpm_tls_server "rsa" "-pk -aes" "4"
424+
fi
419425
fi
420426
if [ $WOLFCRYPT_ECC -eq 1 ]; then
421427
# TLS client/server ECC TLS v1.2 and v1.3 Crypto callbacks
@@ -424,21 +430,25 @@ if [ $WOLFCRYPT_ENABLE -eq 1 ] && [ $WOLFCRYPT_DEFAULT -eq 0 ] && [ $NO_FILESYST
424430
run_tpm_tls_client "ecc" "" "4"
425431
run_tpm_tls_client "ecc" "-aes" "4"
426432

427-
run_tpm_tls_server "ecc" "" "3"
428-
run_tpm_tls_server "ecc" "-aes" "3"
429-
run_tpm_tls_server "ecc" "" "4"
430-
run_tpm_tls_server "ecc" "-aes" "4"
433+
if [ $NO_PUBASPRIV -eq 0 ]; then
434+
run_tpm_tls_server "ecc" "" "3"
435+
run_tpm_tls_server "ecc" "-aes" "3"
436+
run_tpm_tls_server "ecc" "" "4"
437+
run_tpm_tls_server "ecc" "-aes" "4"
438+
fi
431439

432440
# TLS client/server ECC TLS v1.2 and v1.3 PK callbacks
433441
run_tpm_tls_client "ecc" "-pk" "3"
434442
run_tpm_tls_client "ecc" "-pk -aes" "3"
435443
run_tpm_tls_client "ecc" "-pk" "4"
436444
run_tpm_tls_client "ecc" "-pk -aes" "4"
437445

438-
run_tpm_tls_server "ecc" "-pk" "3"
439-
run_tpm_tls_server "ecc" "-pk -aes" "3"
440-
run_tpm_tls_server "ecc" "-pk" "4"
441-
run_tpm_tls_server "ecc" "-pk -aes" "4"
446+
if [ $NO_PUBASPRIV -eq 0 ]; then
447+
run_tpm_tls_server "ecc" "-pk" "3"
448+
run_tpm_tls_server "ecc" "-pk -aes" "3"
449+
run_tpm_tls_server "ecc" "-pk" "4"
450+
run_tpm_tls_server "ecc" "-pk -aes" "4"
451+
fi
442452
fi
443453
fi
444454

src/tpm2_wrap.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,8 @@ int wolfTPM2_DecodeRsaDer(const byte* der, word32 derSz,
28182818
rc = wc_InitRsaKey(key, NULL);
28192819
if (rc == 0) {
28202820
idx = 0;
2821+
/* skip PKCS8 header */
2822+
(void)wc_GetPkcs8TraditionalOffset((byte*)der, &idx, derSz);
28212823
rc = wc_RsaPrivateKeyDecode(der, &idx, key, derSz);
28222824
if (rc == 0) {
28232825
isPrivateKey = 1;
@@ -3033,7 +3035,7 @@ int wolfTPM2_ExportPublicKeyBuffer(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
30333035
/* load public portion of key into wolf RSA Key */
30343036
rc = wolfTPM2_RsaKey_TpmToWolf(dev, tpmKey, &key.rsa);
30353037
if (rc == 0) {
3036-
rc = wc_RsaKeyToPublicDer_ex(&key.rsa, out, *outSz, 1);
3038+
rc = wc_RsaKeyToPublicDer(&key.rsa, out, *outSz);
30373039
if (rc > 0) {
30383040
derSz = rc;
30393041
rc = 0;
@@ -6836,15 +6838,19 @@ static int CSR_Parse_DN(CertName* name, const char* subject)
68366838
{"/CN=", OFFSETOF(CertName, commonName)}, /* Common Name */
68376839
{"/C=", OFFSETOF(CertName, country)}, /* Country */
68386840
{"/ST=", OFFSETOF(CertName, state)}, /* State */
6839-
{"/street=", OFFSETOF(CertName, street)}, /* Street */
68406841
{"/L=", OFFSETOF(CertName, locality)}, /* Locality */
68416842
{"/SN=", OFFSETOF(CertName, sur)}, /* Surname */
68426843
{"/O=", OFFSETOF(CertName, org)}, /* Organization */
68436844
{"/OU=", OFFSETOF(CertName, unit)}, /* Organization Unit */
6844-
{"/postalCode=", OFFSETOF(CertName, postalCode)}, /* PostalCode */
6845-
{"/userid=", OFFSETOF(CertName, userId)}, /* UserID */
68466845
{"/serialNumber=", OFFSETOF(CertName, serialDev)}, /* Serial Number */
68476846
{"/emailAddress=", OFFSETOF(CertName, email)}, /* Email Address */
6847+
#if defined(LIBWOLFSSL_VERSION_HEX) && LIBWOLFSSL_VERSION_HEX > 0x05000000
6848+
{"/street=", OFFSETOF(CertName, street)}, /* Street */
6849+
{"/postalCode=", OFFSETOF(CertName, postalCode)}, /* PostalCode */
6850+
#endif
6851+
#if defined(LIBWOLFSSL_VERSION_HEX) && LIBWOLFSSL_VERSION_HEX > 0x05003000
6852+
{"/userid=", OFFSETOF(CertName, userId)}, /* UserID */
6853+
#endif
68486854
#ifdef WOLFSSL_CERT_EXT
68496855
{"/businessCategory=", OFFSETOF(CertName, busCat)}, /* Business Category */
68506856
#endif
@@ -6916,7 +6922,7 @@ static int CSR_MakeAndSign(WOLFTPM2_DEV* dev, WOLFTPM2_CSR* csr, CSRKey* key,
69166922
}
69176923

69186924
/* Optionally convert to PEM */
6919-
if (rc >= 0 && outFormat == CTC_FILETYPE_PEM) {
6925+
if (rc >= 0 && outFormat == ENCODING_TYPE_PEM) {
69206926
#ifdef WOLFSSL_DER_TO_PEM
69216927
byte tmp[MAX_CONTEXT_SIZE];
69226928
if (rc > (int)sizeof(tmp)) {

wolftpm/tpm2_types.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ typedef int64_t INT64;
125125
#define ENCODING_TYPE_PEM 1 /* CTC_FILETYPE_PEM */
126126
#define ENCODING_TYPE_ASN1 2 /* CTC_FILETYPE_ASN1 */
127127

128+
#ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
129+
#define wc_ecc_key_get_priv(key) (&((key)->k))
130+
#define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
131+
#endif
132+
133+
#ifndef PRIVATE_KEY_LOCK
134+
#define PRIVATE_KEY_LOCK() do {} while (0)
135+
#endif
136+
#ifndef PRIVATE_KEY_UNLOCK
137+
#define PRIVATE_KEY_UNLOCK() do {} while (0)
138+
#endif
128139
#else
129140

130141
#include <stdio.h>
@@ -172,14 +183,6 @@ typedef int64_t INT64;
172183
#define LITTLE_ENDIAN_ORDER
173184
#endif
174185

175-
#ifndef OFFSETOF
176-
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 4))
177-
#define OFFSETOF(type, field) __builtin_offsetof(type, field)
178-
#else
179-
#define OFFSETOF(type, field) ((size_t)&(((type *)0)->field))
180-
#endif
181-
#endif
182-
183186
/* GCC Version */
184187
#ifndef __GNUC_PREREQ
185188
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
@@ -223,6 +226,14 @@ typedef int64_t INT64;
223226

224227
#endif /* !WOLFTPM2_NO_WOLFCRYPT */
225228

229+
#ifndef OFFSETOF
230+
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 4))
231+
#define OFFSETOF(type, field) __builtin_offsetof(type, field)
232+
#else
233+
#define OFFSETOF(type, field) ((size_t)&(((type *)0)->field))
234+
#endif
235+
#endif
236+
226237
#ifndef WOLFTPM_CUSTOM_TYPES
227238
#include <stdlib.h>
228239

0 commit comments

Comments
 (0)