Skip to content

Commit 0cc3416

Browse files
authored
Merge pull request #130 from elms/mingw/tls
minGW support for TLS example
2 parents 7821b11 + b48f53f commit 0cc3416

27 files changed

Lines changed: 876 additions & 437 deletions

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ certs/*.par
6060
certs/crlnumber*
6161
certs/serial
6262
certs/index*
63-
63+
certs/tpm-*.csr
64+
certs/server-*.der
65+
certs/server-*.pem
66+
certs/client-*.der
67+
certs/client-*.pem
68+
certs/serial.old
6469
*.dep
6570
IDE/IAR-EWARM/settings
6671
quote.blob
6772
keyblob.bin
73+
ecc_test_blob.raw
74+
rsa_test_blob.raw

certs/certreq.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
echo Run from wolftpm root
4-
echo Run ./examples/crl/crl first to generate the CSR
4+
echo Run ./examples/csr/csr first to generate the CSR
55

66

77
# Make sure required CA files exist and are populated
@@ -23,7 +23,7 @@ if [ "$1" == "clean" ]; then
2323
rm -f ./certs/server-*.der
2424
rm -f ./certs/*.old
2525

26-
# cleanup the ./examples/crl/crl generated
26+
# cleanup the ./examples/csr/csr generated
2727
rm -f ./certs/tpm-*-cert.csr
2828

2929
exit 0

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ then
210210
AC_MSG_ERROR([Cannot enable swtpm or devtpm with windows API])
211211
fi
212212

213-
AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_WINAPI -DNO_WOLFSSL_SERVER -DNO_WOLFSSL_CLIENT"
213+
AM_CFLAGS="$AM_CFLAGS -DWOLFTPM_WINAPI"
214214
fi
215215

216216

docs/WindowTBS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export PREFIX=$PWD/tmp_install
1111
1212
cd wolfssl
1313
./autogen.sh
14-
./configure --prefix="$PREFIX"
14+
./configure --prefix="$PREFIX" --enable-certgen --enable-certreq --enable-certext --enable-pkcs7 --enable-cryptocb
1515
make
1616
make install
1717
@@ -42,7 +42,7 @@ export PREFIX=$PWD/tmp_install
4242
4343
cd wolfssl
4444
./autogen.sh
45-
./configure --host=i686 CC=i686-w64-mingw32-gcc CFLAGS="-DWIN32 -DMINGW -D_WIN32_WINNT=0x0600" LIBS="-lws2_32" --prefix="$PREFIX"
45+
./configure --host=i686 CC=i686-w64-mingw32-gcc CFLAGS="-DWIN32 -DMINGW -D_WIN32_WINNT=0x0600" LIBS="-lws2_32" --prefix="$PREFIX" --enable-certgen --enable-certreq --enable-certext --enable-pkcs7 --enable-cryptocb
4646
make
4747
make install
4848

examples/README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,13 @@ To force ECC use with wolfSSL when RSA is enabled define `TLS_USE_ECC`.
108108
To use symmetric AES/Hashing/HMAC with the TPM define `WOLFTPM_USE_SYMMETRIC`.
109109

110110
Generation of the Client and Server Certificates requires running:
111-
1. `./examples/csr/csr`
112-
2. `./certs/certreq.sh`
113-
3. Copy the CA files from wolfTPM to wolfSSL certs directory.
111+
112+
113+
1. `./examples/keygen/keygen rsa_test_blob.raw RSA T`
114+
2. `./examples/keygen/keygen ecc_test_blob.raw ECC T`
115+
3. `./examples/csr/csr`
116+
4. `./certs/certreq.sh`
117+
5. Copy the CA files from wolfTPM to wolfSSL certs directory.
114118
a. `cp ./certs/ca-ecc-cert.pem ../wolfssl/certs/tpm-ca-ecc-cert.pem`
115119
b. `cp ./certs/ca-rsa-cert.pem ../wolfssl/certs/tpm-ca-rsa-cert.pem`
116120

@@ -122,15 +126,17 @@ Examples show using a TPM key and certificate for TLS mutual authentication (cli
122126
This example client connects to localhost on on port 11111 by default. These can be overridden using `TLS_HOST` and `TLS_PORT`.
123127

124128
You can validate using the wolfSSL example server this like:
125-
`./examples/server/server -b -p 11111 -g -d`
129+
`./examples/server/server -b -p 11111 -g -d -i -V`
126130

127131
To validate client certificate use the following wolfSSL example server command:
128-
`./examples/server/server -b -p 11111 -g -A ./certs/tpm-ca-rsa-cert.pem`
132+
`./examples/server/server -b -p 11111 -g -A ./certs/tpm-ca-rsa-cert.pem -i -V`
129133
or
130-
`./examples/server/server -b -p 11111 -g -A ./certs/tpm-ca-ecc-cert.pem`
134+
`./examples/server/server -b -p 11111 -g -A ./certs/tpm-ca-ecc-cert.pem -i -V`
131135

132136
Then run the wolfTPM TLS client example:
133-
`./examples/tls/tls_client`
137+
`./examples/tls/tls_client RSA`
138+
or
139+
`./examples/tls/tls_client ECC`
134140

135141

136142
### TLS Server
@@ -140,7 +146,9 @@ This example shows using a TPM key and certificate for a TLS server.
140146
By default it listens on port 11111 and can be overridden at build-time using the `TLS_PORT` macro.
141147

142148
Run the wolfTPM TLS server example:
143-
`./examples/tls/tls_server`.
149+
`./examples/tls/tls_server RSA`
150+
or
151+
`./examples/tls/tls_server ECC`
144152

145153
Then run the wolfSSL example client this like:
146154
`./examples/client/client -h localhost -p 11111 -g -d`

examples/bench/bench.c

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <examples/tpm_io.h>
3131
#include <examples/tpm_test.h>
32+
#include <examples/tpm_test_keys.h>
3233
#include <examples/bench/bench.h>
3334

3435
#include <stdio.h>
@@ -207,30 +208,10 @@ int TPM2_Wrapper_Bench(void* userCtx)
207208
if (rc != 0) return rc;
208209

209210
/* See if primary storage key already exists */
210-
rc = wolfTPM2_ReadPublicKey(&dev, &storageKey,
211-
TPM2_DEMO_STORAGE_KEY_HANDLE);
212-
if (rc != 0) {
213-
/* Create primary storage key */
214-
rc = wolfTPM2_GetKeyTemplate_RSA(&publicTemplate,
215-
TPMA_OBJECT_fixedTPM | TPMA_OBJECT_fixedParent |
216-
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
217-
TPMA_OBJECT_restricted | TPMA_OBJECT_decrypt | TPMA_OBJECT_noDA);
218-
if (rc != 0) goto exit;
219-
rc = wolfTPM2_CreatePrimaryKey(&dev, &storageKey, TPM_RH_OWNER,
220-
&publicTemplate, (byte*)gStorageKeyAuth, sizeof(gStorageKeyAuth)-1);
221-
if (rc != 0) goto exit;
222-
223-
/* Move this key into persistent storage */
224-
rc = wolfTPM2_NVStoreKey(&dev, TPM_RH_OWNER, &storageKey,
225-
TPM2_DEMO_STORAGE_KEY_HANDLE);
226-
if (rc != 0) goto exit;
227-
}
228-
else {
229-
/* specify auth password for storage key */
230-
storageKey.handle.auth.size = sizeof(gStorageKeyAuth)-1;
231-
XMEMCPY(storageKey.handle.auth.buffer, gStorageKeyAuth,
232-
storageKey.handle.auth.size);
233-
}
211+
rc = getPrimaryStoragekey(&dev,
212+
&storageKey,
213+
&publicTemplate);
214+
if (rc != 0) goto exit;
234215

235216
/* RNG Benchmark */
236217
bench_stats_start(&count, &start);

examples/bench/include.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ if BUILD_EXAMPLES
55
noinst_PROGRAMS += examples/bench/bench
66
noinst_HEADERS += examples/bench/bench.h
77
examples_bench_bench_SOURCES = examples/bench/bench.c \
8+
examples/tpm_test_keys.c \
89
examples/tpm_io.c
910
examples_bench_bench_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1011
examples_bench_bench_DEPENDENCIES = src/libwolftpm.la

examples/csr/csr.c

Lines changed: 21 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <examples/tpm_io.h>
3131
#include <examples/tpm_test.h>
32+
#include <examples/tpm_test_keys.h>
3233
#include <examples/csr/csr.h>
3334
#include <wolfssl/wolfcrypt/asn_public.h>
3435

@@ -173,59 +174,21 @@ int TPM2_CSR_Example(void* userCtx)
173174
if (rc != 0) goto exit;
174175

175176
/* See if primary storage key already exists */
176-
rc = wolfTPM2_ReadPublicKey(&dev, &storageKey,
177-
TPM2_DEMO_STORAGE_KEY_HANDLE);
178-
if (rc != 0) {
179-
/* Create primary storage key */
180-
rc = wolfTPM2_GetKeyTemplate_RSA(&publicTemplate,
181-
TPMA_OBJECT_fixedTPM | TPMA_OBJECT_fixedParent |
182-
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
183-
TPMA_OBJECT_restricted | TPMA_OBJECT_decrypt | TPMA_OBJECT_noDA);
184-
if (rc != 0) goto exit;
185-
rc = wolfTPM2_CreatePrimaryKey(&dev, &storageKey, TPM_RH_OWNER,
186-
&publicTemplate, (byte*)gStorageKeyAuth, sizeof(gStorageKeyAuth)-1);
187-
if (rc != 0) goto exit;
188-
189-
/* Move this key into persistent storage */
190-
rc = wolfTPM2_NVStoreKey(&dev, TPM_RH_OWNER, &storageKey,
191-
TPM2_DEMO_STORAGE_KEY_HANDLE);
192-
if (rc != 0) goto exit;
193-
}
194-
else {
195-
/* specify auth password for storage key */
196-
storageKey.handle.auth.size = sizeof(gStorageKeyAuth)-1;
197-
XMEMCPY(storageKey.handle.auth.buffer, gStorageKeyAuth,
177+
rc = getPrimaryStoragekey(&dev,
178+
&storageKey,
179+
&publicTemplate);
180+
if (rc != 0) goto exit;
181+
182+
storageKey.handle.auth.size = sizeof(gStorageKeyAuth)-1;
183+
XMEMCPY(storageKey.handle.auth.buffer, gStorageKeyAuth,
198184
storageKey.handle.auth.size);
199-
}
200185

201186
#ifndef NO_RSA
202-
/* Create/Load RSA key for CSR */
203-
rc = wolfTPM2_ReadPublicKey(&dev, &rsaKey, TPM2_DEMO_RSA_KEY_HANDLE);
204-
if (rc != 0) {
205-
rc = wolfTPM2_GetKeyTemplate_RSA(&publicTemplate,
206-
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
207-
TPMA_OBJECT_decrypt | TPMA_OBJECT_sign | TPMA_OBJECT_noDA);
208-
if (rc != 0) goto exit;
209-
rc = wolfTPM2_CreateAndLoadKey(&dev, &rsaKey, &storageKey.handle,
210-
&publicTemplate, (byte*)gKeyAuth, sizeof(gKeyAuth)-1);
211-
if (rc != 0) goto exit;
212-
213-
/* Move this key into persistent storage */
214-
rc = wolfTPM2_NVStoreKey(&dev, TPM_RH_OWNER, &rsaKey,
215-
TPM2_DEMO_RSA_KEY_HANDLE);
216-
if (rc != 0) goto exit;
217-
}
218-
else {
219-
/* specify auth password for RSA key */
220-
rsaKey.handle.auth.size = sizeof(gKeyAuth)-1;
221-
XMEMCPY(rsaKey.handle.auth.buffer, gKeyAuth, rsaKey.handle.auth.size);
222-
}
223-
224-
/* setup wolf RSA key with TPM deviceID, so crypto callbacks are used */
225-
rc = wc_InitRsaKey_ex(&wolfRsaKey, NULL, tpmDevId);
226-
if (rc != 0) goto exit;
227-
/* load public portion of key into wolf RSA Key */
228-
rc = wolfTPM2_RsaKey_TpmToWolf(&dev, &rsaKey, &wolfRsaKey);
187+
rc = getRSAkey(&dev,
188+
&storageKey,
189+
&rsaKey,
190+
&wolfRsaKey,
191+
tpmDevId);
229192
if (rc != 0) goto exit;
230193

231194
rc = TPM2_CSR_Generate(&dev, RSA_TYPE, &wolfRsaKey, gClientCertRsaFile);
@@ -234,34 +197,11 @@ int TPM2_CSR_Example(void* userCtx)
234197

235198

236199
#ifdef HAVE_ECC
237-
/* Create/Load ECC key for CSR */
238-
rc = wolfTPM2_ReadPublicKey(&dev, &eccKey, TPM2_DEMO_ECC_KEY_HANDLE);
239-
if (rc != 0) {
240-
rc = wolfTPM2_GetKeyTemplate_ECC(&publicTemplate,
241-
TPMA_OBJECT_sensitiveDataOrigin | TPMA_OBJECT_userWithAuth |
242-
TPMA_OBJECT_sign | TPMA_OBJECT_noDA,
243-
TPM_ECC_NIST_P256, TPM_ALG_ECDSA);
244-
if (rc != 0) goto exit;
245-
rc = wolfTPM2_CreateAndLoadKey(&dev, &eccKey, &storageKey.handle,
246-
&publicTemplate, (byte*)gKeyAuth, sizeof(gKeyAuth)-1);
247-
if (rc != 0) goto exit;
248-
249-
/* Move this key into persistent storage */
250-
rc = wolfTPM2_NVStoreKey(&dev, TPM_RH_OWNER, &eccKey,
251-
TPM2_DEMO_ECC_KEY_HANDLE);
252-
if (rc != 0) goto exit;
253-
}
254-
else {
255-
/* specify auth password for ECC key */
256-
eccKey.handle.auth.size = sizeof(gKeyAuth)-1;
257-
XMEMCPY(eccKey.handle.auth.buffer, gKeyAuth, eccKey.handle.auth.size);
258-
}
259-
260-
/* setup wolf ECC key with TPM deviceID, so crypto callbacks are used */
261-
rc = wc_ecc_init_ex(&wolfEccKey, NULL, tpmDevId);
262-
if (rc != 0) goto exit;
263-
/* load public portion of key into wolf ECC Key */
264-
rc = wolfTPM2_EccKey_TpmToWolf(&dev, &eccKey, &wolfEccKey);
200+
rc = getECCkey(&dev,
201+
&storageKey,
202+
&eccKey,
203+
&wolfEccKey,
204+
tpmDevId);
265205
if (rc != 0) goto exit;
266206

267207
rc = TPM2_CSR_Generate(&dev, ECC_TYPE, &wolfEccKey, gClientCertEccFile);
@@ -274,6 +214,9 @@ int TPM2_CSR_Example(void* userCtx)
274214
printf("Failure 0x%x: %s\n", rc, wolfTPM2_GetRCString(rc));
275215
}
276216

217+
218+
wolfTPM2_UnloadHandle(&dev, &storageKey.handle);
219+
277220
#ifndef NO_RSA
278221
wc_FreeRsaKey(&wolfRsaKey);
279222
wolfTPM2_UnloadHandle(&dev, &rsaKey.handle);

examples/csr/include.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ if BUILD_EXAMPLES
55
noinst_PROGRAMS += examples/csr/csr
66
noinst_HEADERS += examples/csr/csr.h
77
examples_csr_csr_SOURCES = examples/csr/csr.c \
8+
examples/tpm_test_keys.c \
89
examples/tpm_io.c
910
examples_csr_csr_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1011
examples_csr_csr_DEPENDENCIES = src/libwolftpm.la

examples/include.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ include examples/keygen/include.am
1515
dist_example_DATA+= examples/README.md \
1616
examples/tpm_io.c \
1717
examples/tpm_io.h \
18+
examples/tpm_test_keys.c \
19+
examples/tpm_test_keys.h \
1820
examples/tpm_test.h

0 commit comments

Comments
 (0)