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 );
0 commit comments