Skip to content

Commit 633f627

Browse files
aidangarskedgarske
authored andcommitted
Use shared wolfTPM2_SetKeyBlobFromBuffer parser and guard TPM signing args
1 parent a2c28c9 commit 633f627

2 files changed

Lines changed: 39 additions & 92 deletions

File tree

examples/echoserver/echoserver.c

Lines changed: 37 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,113 +2237,51 @@ static int LoadPubKeyList(StrList* strList, int format, PwMapList* mapList)
22372237
#endif
22382238

22392239
#ifdef WOLFSSH_TPM
2240-
/* Default key auth produced by 'keygen ... -ecc -t -eh' (override with -auth). */
2241-
#define ECHOSERVER_TPM_KEY_AUTH "ThisIsMyKeyAuth"
2240+
/* Default key auth produced by 'keygen ... -t -eh'; pass a different value to
2241+
* EchoserverInitTpmHostKey() to override. */
2242+
#define ECHOSERVER_TPM_KEY_AUTH_DEFAULT "ThisIsMyKeyAuth"
22422243
static WOLFTPM2_DEV tpmHostDev;
22432244
static WOLFTPM2_KEY tpmHostKey;
22442245
static int tpmHostKeyValid = 0;
22452246

2246-
static int EchoserverReadKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
2247-
{
2248-
int rc = 0;
2249-
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
2250-
WFILE* fp = NULL;
2251-
size_t fileSz = 0;
2252-
size_t bytesRead = 0;
2253-
byte pubAreaBuffer[sizeof(TPM2B_PUBLIC)];
2254-
int pubAreaSize;
2255-
2256-
WMEMSET(key, 0, sizeof(WOLFTPM2_KEYBLOB));
2257-
2258-
if (WFOPEN(NULL, &fp, filename, "rb") != 0 || fp == WBADFILE) {
2259-
fprintf(stderr, "Failed to open TPM key blob %s\n", filename);
2260-
return BUFFER_E;
2261-
}
2262-
2263-
WFSEEK(NULL, fp, 0, WSEEK_END);
2264-
fileSz = WFTELL(NULL, fp);
2265-
WREWIND(NULL, fp);
2266-
2267-
if (fileSz > sizeof(key->priv) + sizeof(key->pub)) {
2268-
rc = BUFFER_E;
2269-
}
2270-
2271-
if (rc == 0) {
2272-
bytesRead = WFREAD(NULL, &key->pub.size, 1, sizeof(key->pub.size), fp);
2273-
if (bytesRead != sizeof(key->pub.size))
2274-
rc = BUFFER_E;
2275-
else
2276-
fileSz -= bytesRead;
2277-
}
2278-
2279-
if (rc == 0 &&
2280-
(sizeof(UINT16) + (size_t)key->pub.size) > sizeof(pubAreaBuffer)) {
2281-
rc = BUFFER_E;
2282-
}
2283-
2284-
if (rc == 0) {
2285-
bytesRead = WFREAD(NULL, pubAreaBuffer, 1,
2286-
sizeof(UINT16) + key->pub.size, fp);
2287-
if (bytesRead != sizeof(UINT16) + key->pub.size)
2288-
rc = BUFFER_E;
2289-
else
2290-
fileSz -= bytesRead;
2291-
}
2292-
2293-
if (rc == 0) {
2294-
/* Bound the parse to the bytes actually read so a malformed size
2295-
* field cannot consume the uninitialized tail of the buffer. */
2296-
rc = TPM2_ParsePublic(&key->pub, pubAreaBuffer,
2297-
(word32)(sizeof(UINT16) + key->pub.size), &pubAreaSize);
2298-
}
2299-
2300-
if (rc == 0 &&
2301-
pubAreaSize != (int)(sizeof(UINT16) + key->pub.size)) {
2302-
rc = BUFFER_E;
2303-
}
2304-
2305-
if (rc == 0 && fileSz > sizeof(key->priv)) {
2306-
rc = BUFFER_E;
2307-
}
2308-
2309-
if (rc == 0 && fileSz > 0) {
2310-
bytesRead = WFREAD(NULL, &key->priv, 1, fileSz, fp);
2311-
if (bytesRead != fileSz)
2312-
rc = BUFFER_E;
2313-
}
2314-
2315-
if (rc == 0 && key->priv.size > sizeof(key->priv.buffer)) {
2316-
rc = BUFFER_E;
2317-
}
2318-
2319-
WFCLOSE(NULL, fp);
2320-
#else
2321-
(void)filename;
2322-
(void)key;
2323-
rc = WS_NOT_COMPILED;
2324-
#endif
2325-
return rc;
2326-
}
2327-
2328-
/* Loads an ECC host key blob into the TPM and registers it as the server host
2329-
* key so the private key never enters RAM. */
2330-
static int EchoserverInitTpmHostKey(WOLFSSH_CTX* ctx, const char* keyFile)
2247+
/* Loads a TPM host key blob (ECC or RSA) into the TPM and registers it as the
2248+
* server host key so the private key never enters RAM. */
2249+
static int EchoserverInitTpmHostKey(WOLFSSH_CTX* ctx, const char* keyFile,
2250+
const char* keyAuth)
23312251
{
23322252
int rc;
23332253
TPMI_ALG_PUBLIC alg = TPM_ALG_ECC;
23342254
WOLFTPM2_KEY endorse;
23352255
WOLFTPM2_KEYBLOB keyBlob;
23362256
WOLFTPM2_SESSION tpmSession;
2257+
#ifndef NO_FILESYSTEM
2258+
byte fileBuf[sizeof(WOLFTPM2_KEYBLOB)];
2259+
word32 fileSz = (word32)sizeof(fileBuf);
2260+
int readSz = 0;
2261+
#endif
23372262

23382263
WMEMSET(&endorse, 0, sizeof(endorse));
23392264
WMEMSET(&tpmSession, 0, sizeof(tpmSession));
2265+
WMEMSET(&keyBlob, 0, sizeof(keyBlob));
23402266
WMEMSET(&tpmHostKey, 0, sizeof(tpmHostKey));
23412267

23422268
rc = wolfTPM2_Init(&tpmHostDev, TPM2_IoCb, NULL);
23432269

2270+
/* Read the key blob and parse it with the shared wolfTPM helper. */
2271+
#ifndef NO_FILESYSTEM
23442272
if (rc == 0) {
2345-
rc = EchoserverReadKeyBlob(keyFile, &keyBlob);
2273+
readSz = load_file(keyFile, fileBuf, &fileSz);
2274+
if (readSz <= 0)
2275+
rc = WS_BAD_FILE_E;
23462276
}
2277+
if (rc == 0) {
2278+
rc = wolfTPM2_SetKeyBlobFromBuffer(&keyBlob, fileBuf, (word32)readSz);
2279+
}
2280+
#else
2281+
(void)keyFile;
2282+
if (rc == 0)
2283+
rc = WS_NOT_COMPILED;
2284+
#endif
23472285

23482286
/* Match the endorsement key type to the host key (RSA or ECC). */
23492287
if (rc == 0) {
@@ -2360,9 +2298,13 @@ static int EchoserverInitTpmHostKey(WOLFSSH_CTX* ctx, const char* keyFile)
23602298
rc = wolfTPM2_SetAuthSession(&tpmHostDev, 0, &tpmSession, 0);
23612299
}
23622300

2301+
if (rc == 0 && XSTRLEN(keyAuth) > sizeof(keyBlob.handle.auth.buffer)) {
2302+
rc = WS_BAD_ARGUMENT;
2303+
}
2304+
23632305
if (rc == 0) {
2364-
keyBlob.handle.auth.size = (word32)XSTRLEN(ECHOSERVER_TPM_KEY_AUTH);
2365-
XMEMCPY(keyBlob.handle.auth.buffer, ECHOSERVER_TPM_KEY_AUTH,
2306+
keyBlob.handle.auth.size = (word32)XSTRLEN(keyAuth);
2307+
XMEMCPY(keyBlob.handle.auth.buffer, keyAuth,
23662308
keyBlob.handle.auth.size);
23672309
rc = wolfTPM2_LoadKey(&tpmHostDev, &keyBlob, &endorse.handle);
23682310
}
@@ -2390,6 +2332,9 @@ static int EchoserverInitTpmHostKey(WOLFSSH_CTX* ctx, const char* keyFile)
23902332
/* keyBlob holds the private blob and key auth; the session may hold auth. */
23912333
wc_ForceZero(&keyBlob, sizeof(keyBlob));
23922334
wc_ForceZero(&tpmSession, sizeof(tpmSession));
2335+
#ifndef NO_FILESYSTEM
2336+
wc_ForceZero(fileBuf, sizeof(fileBuf));
2337+
#endif
23932338

23942339
return rc;
23952340
}
@@ -3161,7 +3106,8 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
31613106

31623107
#ifdef WOLFSSH_TPM
31633108
if (tpmHostKeyPath != NULL) {
3164-
if (EchoserverInitTpmHostKey(ctx, tpmHostKeyPath) != 0) {
3109+
if (EchoserverInitTpmHostKey(ctx, tpmHostKeyPath,
3110+
ECHOSERVER_TPM_KEY_AUTH_DEFAULT) != 0) {
31653111
ES_ERROR("Couldn't load TPM host key from %s.\n", tpmHostKeyPath);
31663112
}
31673113
loadDefaultHostKeys = 0;

src/internal.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12869,7 +12869,8 @@ static int SignHRsa(WOLFSSH* ssh, byte* sig, word32* sigSz,
1286912869
WLOG(WS_LOG_INFO, "Signing hash with %s.",
1287012870
IdToName(ssh->handshake->pubKeyId));
1287112871
#ifdef WOLFSSH_TPM
12872-
if (ssh->handshake->useTpm) {
12872+
if (ssh->handshake->useTpm && ssh->ctx->tpmDev != NULL
12873+
&& ssh->ctx->tpmKey != NULL) {
1287312874
/* Pass the raw digest; the TPM builds the PKCS#1 DigestInfo. */
1287412875
ret = wolfTPM2_SignHashScheme(ssh->ctx->tpmDev,
1287512876
ssh->ctx->tpmKey, digest, (int)digestSz, sig, (int*)sigSz,

0 commit comments

Comments
 (0)