Skip to content

Commit 8455bea

Browse files
committed
Fixes for Public PEM functions. Cleanups.
1 parent bc9434a commit 8455bea

7 files changed

Lines changed: 121 additions & 75 deletions

File tree

.gitignore

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ src/.deps
2626
src/.libs
2727
RemoteSystemsTempFiles
2828
*.o
29+
*.dep
2930
*.deps
3031
*.libs
32+
IDE/IAR-EWARM/settings
3133
wolftpm/options.h
3234

3335
examples/wrap/wrap_test
@@ -75,12 +77,16 @@ certs/server-*.pem
7577
certs/client-*.der
7678
certs/client-*.pem
7779
certs/serial.old
78-
*.dep
79-
IDE/IAR-EWARM/settings
80+
81+
# Test files
8082
quote.blob
8183
keyblob.bin
8284
ecc_test_blob.raw
8385
rsa_test_blob.raw
86+
ak.name
87+
cred.blob
88+
ek.pub
89+
srk.pub
8490

8591
# Generated Documentation
8692
docs/html

examples/attestation/README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ Note:
4343
Using the `keygen` example we can create the necessary TPM 2.0 Attestation Key and TPM 2.0 Primary Storage Key that will be used as a Primary Attestation Key(PAK).
4444

4545
```
46-
4746
$ ./examples/keygen/keygen -rsa
4847
TPM2.0 Key generation example
4948
Key Blob: keyblob.bin
@@ -55,15 +54,13 @@ RSA AIK template
5554
Creating new RSA key...
5655
Created new key (pub 280, priv 222 bytes)
5756
Wrote 508 bytes to keyblob.bin
58-
5957
```
6058

6159
### Make Credential Example Usage
6260

6361
Using the `make_credential` example an attestation server can generate remote attestation challenge. The secret is 32 bytes of randomly generated seed that could be used for a symmetric key in some remote attestation schemes.
6462

6563
```
66-
6764
$ ./examples/attestation/make_credential
6865
Using default values
6966
Demo how to create a credential blob for remote attestation
@@ -74,7 +71,6 @@ Reading the private part of the key
7471
AK loaded at 0x80000001
7572
TPM2_MakeCredential success
7673
Wrote credential blob and secret to cred.blob, 514 bytes
77-
7874
```
7975

8076
The transfer of the PAK and AK public parts between the client and attestation server is not part of the `make_credential` example, because the exchange is implementation specific.
@@ -84,7 +80,6 @@ The transfer of the PAK and AK public parts between the client and attestation s
8480
Using the `activate_credential` example a client can decrypt the remote attestation challenge. The secret will be exposed in plain and can be exchanged with the attestation server.
8581

8682
```
87-
8883
$ ./examples/attestation/activate_credential
8984
Using default values
9085
Demo how to create a credential blob for remote attestation
@@ -99,7 +94,6 @@ TPM2_StartAuthSession: sessionHandle 0x3000000
9994
TPM2_policyCommandCode success
10095
Read credential blob and secret from cred.blob, 514 bytes
10196
TPM2_ActivateCredential success
102-
10397
```
10498

10599
The transfer of the challenge response containing the secret in plain (or used as a symmetric key seed) is not part of the `activate_credential` example, because the exchange is also implementation specific.

examples/attestation/activate_credential.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ int TPM2_ActivateCredential_Example(void* userCtx, int argc, char *argv[])
182182
sizeof(cmdIn.activCred.secret), fp);
183183
XFCLOSE(fp);
184184
}
185-
printf("Read credential blob and secret from %s, %d bytes\n", input, dataSize);
185+
printf("Read credential blob and secret from %s, %d bytes\n",
186+
input, dataSize);
186187
#else
187188
printf("Can not load credential. File support not enabled\n");
188189
goto exit;

examples/attestation/make_credential.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
129129
goto exit;
130130
}
131131
/* Prepare the key for use by the TPM */
132-
XMEMCPY(&cmdIn.loadExtIn.inPublic, &primary.pub, sizeof(cmdIn.loadExtIn.inPublic));
132+
XMEMCPY(&cmdIn.loadExtIn.inPublic, &primary.pub,
133+
sizeof(cmdIn.loadExtIn.inPublic));
133134
cmdIn.loadExtIn.hierarchy = TPM_RH_NULL;
134135
rc = TPM2_LoadExternal(&cmdIn.loadExtIn, &cmdOut.loadExtOut);
135136
if (rc != TPM_RC_SUCCESS) {
@@ -175,7 +176,8 @@ int TPM2_MakeCredential_Example(void* userCtx, int argc, char *argv[])
175176
sizeof(cmdOut.makeCred.secret), fp);
176177
XFCLOSE(fp);
177178
}
178-
printf("Wrote credential blob and secret to %s, %d bytes\n", output, dataSize);
179+
printf("Wrote credential blob and secret to %s, %d bytes\n",
180+
output, dataSize);
179181
#else
180182
printf("Can not store credential. File support not enabled\n");
181183
#endif

examples/keygen/keygen.c

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int symChoice(const char* arg, TPM_ALG_ID* algSym, int* keyBits,
101101
}
102102

103103
*keyBits = atoi(&arg[SYM_EXTRA_OPTS_KEY_BITS_POS]);
104-
if(*keyBits != 128 && *keyBits != 192 && *keyBits != 256) {
104+
if (*keyBits != 128 && *keyBits != 192 && *keyBits != 256) {
105105
return TPM_RC_FAILURE;
106106
}
107107

@@ -116,7 +116,6 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
116116
WOLFTPM2_KEY storage; /* SRK */
117117
WOLFTPM2_KEY *primary = NULL;
118118
WOLFTPM2_KEY aesKey; /* Symmetric key */
119-
WOLFTPM2_KEY newKey; /* child or attestation key */
120119
WOLFTPM2_KEYBLOB newKeyBlob; /* newKey as WOLFTPM2_KEYBLOB */
121120
WOLFTPM2_KEYBLOB primaryBlob; /* Primary key as WOLFTPM2_KEYBLOB */
122121
TPMT_PUBLIC publicTemplate;
@@ -194,7 +193,6 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
194193
XMEMSET(&endorse, 0, sizeof(endorse));
195194
XMEMSET(&storage, 0, sizeof(storage));
196195
XMEMSET(&aesKey, 0, sizeof(aesKey));
197-
XMEMSET(&newKey, 0, sizeof(newKey));
198196
XMEMSET(&newKeyBlob, 0, sizeof(newKeyBlob));
199197
XMEMSET(&primaryBlob, 0, sizeof(primaryBlob));
200198
XMEMSET(&tpmSession, 0, sizeof(tpmSession));
@@ -215,7 +213,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
215213
goto exit;
216214
}
217215

218-
if(endorseKey) {
216+
if (endorseKey) {
219217
rc = wolfTPM2_CreateEK(&dev, &endorse, TPM_ALG_RSA);
220218
endorse.handle.policyAuth = 1; /* EK requires Policy auth, not Password */
221219
pubFilename = ekPubFile;
@@ -329,7 +327,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
329327
}
330328
}
331329
#else
332-
if(alg == TPM_ALG_SYMCIPHER) {
330+
if (alg == TPM_ALG_SYMCIPHER) {
333331
printf("The Public Part of a symmetric key contains only meta data\n");
334332
}
335333
printf("Key Public Blob %d\n", newKeyBlob.pub.size);
@@ -341,32 +339,31 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
341339
/* Save EK public key as PEM format file to the disk */
342340
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(NO_FILESYSTEM)
343341
if (pemFiles) {
344-
byte pem[MAX_RSA_KEY_BYTES], tempBuf[MAX_RSA_KEY_BYTES];
345-
int pemSz, tempSz = sizeof(tempBuf);
346-
347-
if (endorseKey) {
348-
pemFilename = pemFileEk;
349-
}
350-
else {
351-
pemFilename = pemFileSrk;
352-
}
342+
byte pem[MAX_RSA_KEY_BYTES];
343+
word32 pemSz;
353344

354-
rc = wolfTPM2_RsaKey_TpmToPem(&dev, primary, pem, &pemSz, tempBuf, tempSz);
345+
pemFilename = (endorseKey) ? pemFileEk : pemFileSrk;
346+
pemSz = (word32)sizeof(pem);
347+
rc = wolfTPM2_RsaKey_TpmToPemPub(&dev, primary, pem, &pemSz);
355348
if (rc == 0) {
356-
rc = writeKeyPubPem(pemFilename, pem, (word32)pemSz);
357-
}
358-
359-
if (bAIK) {
360-
pemFilename = pemFileAk;
361-
}
362-
else {
363-
pemFilename = pemFileKey;
349+
rc = writeKeyPubPem(pemFilename, pem, pemSz);
364350
}
365351

366-
rc = wolfTPM2_RsaKey_TpmToPem(&dev, &newKey, pem, &pemSz, tempBuf, tempSz);
352+
pemFilename = (bAIK) ? pemFileAk : pemFileKey;
353+
pemSz = (word32)sizeof(pem);
354+
rc = wolfTPM2_RsaKey_TpmToPemPub(&dev, (WOLFTPM2_KEY*)&newKeyBlob,
355+
pem, &pemSz);
367356
if (rc == 0) {
368-
rc = writeKeyPubPem(pemFilename, pem, (word32)pemSz);
357+
rc = writeKeyPubPem(pemFilename, pem, pemSz);
369358
}
359+
wolfTPM2_UnloadHandle(&dev, &newKeyBlob.handle);
360+
361+
#if 0
362+
/* example for loading public pem to TPM */
363+
rc = wolfTPM2_RsaKey_PubPemToTpm(&dev, (WOLFTPM2_KEY*)&newKeyBlob, pem, pemSz);
364+
printf("wolfTPM2_RsaKey_PubPemToTpm rc=%d\n", rc);
365+
rc = 0;
366+
#endif
370367
}
371368
#else
372369
printf("Unable to store EK pub as PEM file. Lack of file support\n");

src/tpm2_wrap.c

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,29 +1784,58 @@ int wolfTPM2_RsaKey_TpmToWolf(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
17841784
return rc;
17851785
}
17861786

1787-
int wolfTPM2_RsaKey_TpmToPem(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
1788-
byte* pem, int* pemSz, byte* tempBuf, int tempSz)
1787+
int wolfTPM2_RsaKey_TpmToPemPub(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
1788+
byte* pem, word32* pemSz)
17891789
{
17901790
int rc = TPM_RC_FAILURE;
17911791
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_DER_TO_PEM)
1792-
int derSz;
17931792
RsaKey rsaKey;
1793+
byte* derBuf = NULL;
1794+
int derSz = 0;
1795+
#endif
1796+
1797+
if (dev == NULL || tpmKey == NULL || pem == NULL || pemSz == NULL)
1798+
return BAD_FUNC_ARG;
17941799

1800+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_DER_TO_PEM)
17951801
/* Prepare wolfCrypt key structure */
17961802
rc = wc_InitRsaKey(&rsaKey, NULL);
1797-
if (rc != 0) return rc;
1798-
/* Convert the wolfTPM key to wolfCrypt format */
1799-
rc = wolfTPM2_RsaKey_TpmToWolf(dev, tpmKey, &rsaKey);
1800-
if (rc != 0) return rc;
1801-
/* Convert the wolfCrypt key to DER format */
1802-
rc = wc_RsaKeyToPublicDer(&rsaKey, tempBuf, tempSz);
1803-
if (rc <= 0) return rc;
1804-
derSz = rc;
1805-
/* Convert the DER key to PEM format */
1806-
rc = wc_DerToPem(tempBuf, derSz, pem, tempSz, PUBLICKEY_TYPE);
1807-
if (rc <= 0) return rc;
1808-
*pemSz = rc;
1809-
rc = TPM_RC_SUCCESS;
1803+
if (rc == 0) {
1804+
/* Convert the wolfTPM key to wolfCrypt format */
1805+
rc = wolfTPM2_RsaKey_TpmToWolf(dev, tpmKey, &rsaKey);
1806+
if (rc == 0) {
1807+
/* Get DER size - newer API can be called with NULL to get size */
1808+
rc = wc_RsaKeyToPublicDer(&rsaKey, NULL, 0);
1809+
if (rc > 0) {
1810+
derSz = rc;
1811+
}
1812+
else if (rc == BAD_FUNC_ARG) {
1813+
/* for older wolfSSL estimate based on key size */
1814+
derSz = wc_RsaEncryptSize(&rsaKey) * 2;
1815+
rc = 0;
1816+
}
1817+
}
1818+
if (derSz > 0) {
1819+
derBuf = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1820+
if (derBuf == NULL)
1821+
rc = MEMORY_E;
1822+
}
1823+
if (rc == 0) {
1824+
/* Convert the wolfCrypt key to DER format */
1825+
rc = wc_RsaKeyToPublicDer(&rsaKey, derBuf, derSz);
1826+
}
1827+
if (rc >= 0) {
1828+
/* Convert the DER key to PEM format */
1829+
derSz = rc;
1830+
rc = wc_DerToPem(derBuf, derSz, pem, *pemSz, PUBLICKEY_TYPE);
1831+
}
1832+
if (rc >= 0) {
1833+
*pemSz = rc;
1834+
rc = TPM_RC_SUCCESS;
1835+
}
1836+
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1837+
wc_FreeRsaKey(&rsaKey);
1838+
}
18101839
#endif
18111840
return rc;
18121841
}
@@ -1878,27 +1907,49 @@ int wolfTPM2_RsaKey_WolfToTpm(WOLFTPM2_DEV* dev, RsaKey* wolfKey,
18781907
}
18791908

18801909
int wolfTPM2_RsaKey_PubPemToTpm(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
1881-
byte* pem, int pemSz, byte* tempBuf, int tempSz)
1910+
const byte* pem, word32 pemSz)
18821911
{
18831912
int rc = TPM_RC_FAILURE;
1884-
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_CERT_EXT) && \
1885-
defined(WOLFSSL_PUB_PEM_TO_DER) && defined(WOLFSSL_PEM_TO_DER)
1886-
word32 idx;
1913+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_PEM_TO_DER) && \
1914+
(defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
18871915
RsaKey rsaKey;
1916+
#endif
1917+
1918+
if (dev == NULL || tpmKey == NULL || pem == NULL)
1919+
return BAD_FUNC_ARG;
18881920

1921+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_PEM_TO_DER) && \
1922+
(defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
18891923
/* Prepare wolfCrypt key structure */
18901924
rc = wc_InitRsaKey(&rsaKey, NULL);
1891-
if (rc != 0) return rc;
1892-
/* Convert PEM format key from file to DER */
1893-
rc = wc_PubKeyPemToDer(pem, pemSz, tempBuf, tempSz);
1894-
if (rc != 0) return rc;
1895-
/* Convert DER to wolfCrypt file */
1896-
idx = 0;
1897-
rc = wc_RsaPublicKeyDecode(tempBuf, &idx, &rsaKey, tempSz);
1898-
if (rc != 0) return rc;
1899-
/* Load into the TPM */
1900-
rc = wolfTPM2_RsaKey_WolfToTpm(dev, &rsaKey, tpmKey);
1925+
if (rc == 0) {
1926+
/* allocate buffer for DER */
1927+
word32 derSz = pemSz; /* DER is always smaller */
1928+
byte* derBuf = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1929+
if (derBuf == NULL) {
1930+
rc = MEMORY_E;
1931+
}
1932+
if (rc == 0) {
1933+
/* Convert PEM format key from file to DER - inline okay */
1934+
rc = wc_PubKeyPemToDer(pem, pemSz, derBuf, derSz);
1935+
}
1936+
if (rc >= 0) {
1937+
/* Convert DER to wolfCrypt file */
1938+
word32 idx = 0;
1939+
derSz = (word32)rc;
1940+
rc = wc_RsaPublicKeyDecode(derBuf, &idx, &rsaKey, derSz);
1941+
}
1942+
if (rc == 0) {
1943+
/* Load into the TPM */
1944+
rc = wolfTPM2_RsaKey_WolfToTpm(dev, &rsaKey, tpmKey);
1945+
}
1946+
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1947+
wc_FreeRsaKey(&rsaKey);
1948+
}
1949+
#else
1950+
(void)pemSz;
19011951
#endif
1952+
19021953
return rc;
19031954
}
19041955
#endif /* !NO_RSA */

wolftpm/tpm2_wrap.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,16 +1042,13 @@ WOLFTPM_API int wolfTPM2_RsaKey_TpmToWolf(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKe
10421042
\param keyBlob pointer to a struct of WOLFTPM2_KEY type, holding a TPM key
10431043
\param pem pointer to an array of byte type, used as temporary storage for PEM conversation
10441044
\param pemSz pointer to integer variable, to store the used buffer size
1045-
\param tempBuf pointer to an array of byte type, used as temporary storage for conversation
1046-
\param tempSz integer, specifying the size of the tempSz
10471045
10481046
\sa wolfTPM2_RsaKey_TpmToWolf
10491047
\sa wolfTPM2_RsaKey_WolfToTpm
10501048
*/
1051-
WOLFTPM_API int wolfTPM2_RsaKey_TpmToPem(WOLFTPM2_DEV* dev,
1049+
WOLFTPM_API int wolfTPM2_RsaKey_TpmToPemPub(WOLFTPM2_DEV* dev,
10521050
WOLFTPM2_KEY* keyBlob,
1053-
byte* pem, int* pemSz,
1054-
byte* tempBuf, int tempSz);
1051+
byte* pem, word32* pemSz);
10551052

10561053
/*!
10571054
\ingroup wolfTPM2_Wrappers
@@ -1102,15 +1099,13 @@ WOLFTPM_API int wolfTPM2_RsaKey_WolfToTpm_ex(WOLFTPM2_DEV* dev,
11021099
\param tpmKey pointer to an empty struct of WOLFTPM2_KEY type, to hold the imported TPM key
11031100
\param pem pointer to an array of byte type, containing a PEM formated public key material
11041101
\param pemSz pointer to integer variable, specifying the size of PEM key data
1105-
\param tempBuf pointer to an array of byte type, to be used as temporary storage
1106-
\param tempSz integer variable, specifying the size of the buffer
11071102
11081103
\sa wolfTPM2_RsaKey_WolfToTpm
11091104
\sa wolfTPM2_RsaKey_TpmToPem
11101105
\sa wolfTPM2_RsaKey_TpmToWolf
11111106
*/
1112-
int wolfTPM2_RsaKey_PubPemToTpm(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* tpmKey,
1113-
byte* pem, int pemSz, byte* tempBuf, int tempSz);
1107+
WOLFTPM_API int wolfTPM2_RsaKey_PubPemToTpm(WOLFTPM2_DEV* dev,
1108+
WOLFTPM2_KEY* tpmKey, const byte* pem, word32 pemSz);
11141109
#endif
11151110
#ifdef HAVE_ECC
11161111
/*!

0 commit comments

Comments
 (0)