Skip to content

Commit 7821b11

Browse files
author
elms
authored
Merge pull request #132 from dgarske/keyimport
Added support for importing external private key to get a key blob
2 parents 7dbef68 + 3c697c7 commit 7821b11

12 files changed

Lines changed: 410 additions & 86 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ examples/tls/tls_client_notpm
5050
tests/unit.test
5151
examples/keygen/keyload
5252
examples/keygen/keygen
53+
examples/keygen/keyimport
5354

5455
# Generated Cert Files
5556
certs/ca-*.pem

examples/README.md

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,61 @@ Performance benchmarks.
186186
Examples for generating a TPM key blob and storing to disk, then loading from disk and loading into temporary TPM handle.
187187

188188
```
189-
$ ./examples/keygen/keygen
189+
$ ./examples/keygen/keygen keyblob.bin RSA
190190
TPM2.0 Key generation example
191-
wolfTPM2_Init: success
192-
193191
Loading SRK: Storage 0x81000200 (282 bytes)
194192
Creating new RSA key...
195193
Created new key (pub 280, priv 222 bytes)
196-
Wrote 2168 bytes to keyblob.bin
194+
Wrote 840 bytes to keyblob.bin
197195
198-
$ ./examples/keygen/keyload
196+
$ ./examples/keygen/keyload keyblob.bin
199197
TPM2.0 Key load example
200-
wolfTPM2_Init: success
198+
Loading SRK: Storage 0x81000200 (282 bytes)
199+
Reading 840 bytes from keyblob.bin
200+
Loaded key to 0x80000001
201+
201202
203+
$ ./examples/keygen/keygen keyblob.bin ECC
204+
TPM2.0 Key generation example
202205
Loading SRK: Storage 0x81000200 (282 bytes)
203-
Reading 2168 bytes from keyblob.bin
206+
Creating new ECC key...
207+
Created new key (pub 88, priv 126 bytes)
208+
Wrote 744 bytes to keyblob.bin
209+
210+
$ ./examples/keygen/keyload keyblob.bin
211+
TPM2.0 Key load example
212+
Loading SRK: Storage 0x81000200 (282 bytes)
213+
Reading 744 bytes from keyblob.bin
204214
Loaded key to 0x80000001
205215
```
216+
217+
Example for importing a private key as TPM key blob and storing to disk, then loading from disk and loading into temporary TPM handle.
218+
219+
```
220+
$ ./examples/keygen/keyimport keyblob.bin RSA
221+
TPM2.0 Key import example
222+
Loading SRK: Storage 0x81000200 (282 bytes)
223+
Imported key (pub 278, priv 222 bytes)
224+
Wrote 840 bytes to keyblob.bin
225+
226+
$ ./examples/keygen/keyload keyblob.bin
227+
TPM2.0 Key load example
228+
Loading SRK: Storage 0x81000200 (282 bytes)
229+
Reading 840 bytes from keyblob.bin
230+
Loaded key to 0x80000001
231+
232+
233+
$ ./examples/keygen/keyimport keyblob.bin ECC
234+
TPM2.0 Key Import example
235+
Loading SRK: Storage 0x81000200 (282 bytes)
236+
Imported key (pub 86, priv 126 bytes)
237+
Wrote 744 bytes to keyblob.bin
238+
239+
$ ./examples/keygen/keyload keyblob.bin
240+
TPM2.0 Key load example
241+
Loading SRK: Storage 0x81000200 (282 bytes)
242+
Reading 744 bytes from keyblob.bin
243+
Loaded key to 0x80000001
244+
```
245+
246+
The `keyload` tool takes only one argument, the filename of the stored key. Because the information what is key scheme (RSA or ECC) is contained within the key blob.

examples/keygen/include.am

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ examples_keygen_keygen_SOURCES = examples/keygen/keygen.c \
1515
examples/tpm_io.c
1616
examples_keygen_keygen_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
1717
examples_keygen_keygen_DEPENDENCIES = src/libwolftpm.la
18+
19+
bin_PROGRAMS += examples/keygen/keyimport
20+
examples_keygen_keyimport_SOURCES = examples/keygen/keyimport.c \
21+
examples/tpm_io.c
22+
examples_keygen_keyimport_LDADD = src/libwolftpm.la $(LIB_STATIC_ADD)
23+
examples_keygen_keyimport_DEPENDENCIES = src/libwolftpm.la
1824
endif
1925

2026
dist_example_DATA+= examples/keygen/keyload.c
2127
dist_example_DATA+= examples/keygen/keygen.c
28+
dist_example_DATA+= examples/keygen/keyimport.c
2229
DISTCLEANFILES+= examples/keygen/.libs/keyload
2330
DISTCLEANFILES+= examples/keygen/.libs/keygen
31+
DISTCLEANFILES+= examples/keygen/.libs/keyimport

examples/keygen/keygen.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
/******************************************************************************/
3434
/* --- BEGIN TPM Keygen Example -- */
3535
/******************************************************************************/
36+
static void usage(void)
37+
{
38+
printf("Expected usage:\n");
39+
printf("keygen [keyblob.bin] [ECC/RSA]\n");
40+
}
3641

3742
int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
3843
{
@@ -43,13 +48,19 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
4348
TPMT_PUBLIC publicTemplate;
4449
TPMS_AUTH_COMMAND session[MAX_SESSION_NUM];
4550
TPMI_ALG_PUBLIC alg = TPM_ALG_RSA; /* TPM_ALG_ECC */
46-
4751
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(NO_FILESYSTEM)
4852
XFILE f;
49-
const char* outputFile = "keyblob.bin";
5053
size_t fileSz = 0;
54+
#endif
55+
const char* outputFile = "keyblob.bin";
5156

5257
if (argc >= 2) {
58+
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
59+
XSTRNCMP(argv[1], "-h", 2) == 0 ||
60+
XSTRNCMP(argv[1], "--help", 6) == 0) {
61+
usage();
62+
return 0;
63+
}
5364
outputFile = argv[1];
5465
if (argc >= 3) {
5566
/* ECC vs RSA */
@@ -58,22 +69,20 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
5869
}
5970
}
6071
}
61-
#else
62-
(void)argc;
63-
(void)argv;
64-
#endif
72+
6573
XMEMSET(session, 0, sizeof(session));
6674
XMEMSET(&storage, 0, sizeof(storage));
6775
XMEMSET(&newKey, 0, sizeof(newKey));
6876

6977
printf("TPM2.0 Key generation example\n");
78+
printf("\tKey Blob: %s\n", outputFile);
79+
printf("\tAlgorithm: %s\n", TPM2_GetAlgName(alg));
80+
7081
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
7182
if (rc != TPM_RC_SUCCESS) {
7283
printf("\nwolfTPM2_Init failed\n");
7384
goto exit;
7485
}
75-
printf("wolfTPM2_Init: success\n\n");
76-
7786

7887
/* Define the default session auth that has NULL password */
7988
session[0].sessionHandle = TPM_RS_PW;

examples/keygen/keygen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[]);
3030
int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[]);
31+
int TPM2_Keyimport_Example(void* userCtx, int argc, char *argv[]);
3132

3233
#ifdef __cplusplus
3334
} /* extern "C" */

examples/keygen/keyimport.c

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/* keyimport.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfTPM.
6+
*
7+
* wolfTPM is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfTPM is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
/* Tool and example for creating, storing and loading keys using TPM2.0 */
23+
24+
#include <wolftpm/tpm2_wrap.h>
25+
26+
#include <examples/keygen/keygen.h>
27+
#include <examples/tpm_io.h>
28+
#include <examples/tpm_test.h>
29+
30+
#include <stdio.h>
31+
32+
33+
/******************************************************************************/
34+
/* --- BEGIN TPM Key Import / Blob Example -- */
35+
/******************************************************************************/
36+
37+
static void usage(void)
38+
{
39+
printf("Expected usage:\n");
40+
printf("keyimport [keyblob.bin] [ECC/RSA]\n");
41+
}
42+
43+
44+
int TPM2_Keyimport_Example(void* userCtx, int argc, char *argv[])
45+
{
46+
int rc;
47+
WOLFTPM2_DEV dev;
48+
WOLFTPM2_KEY storage; /* SRK */
49+
WOLFTPM2_KEYBLOB impKey;
50+
TPMS_AUTH_COMMAND session[MAX_SESSION_NUM];
51+
TPMI_ALG_PUBLIC alg = TPM_ALG_RSA; /* TPM_ALG_ECC */
52+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(NO_FILESYSTEM)
53+
XFILE f;
54+
size_t fileSz = 0;
55+
#endif
56+
const char* outputFile = "keyblob.bin";
57+
58+
if (argc >= 2) {
59+
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
60+
XSTRNCMP(argv[1], "-h", 2) == 0 ||
61+
XSTRNCMP(argv[1], "--help", 6) == 0) {
62+
usage();
63+
return 0;
64+
}
65+
66+
outputFile = argv[1];
67+
if (argc >= 3) {
68+
/* ECC vs RSA */
69+
if (XSTRNCMP(argv[2], "ECC", 3) == 0) {
70+
alg = TPM_ALG_ECC;
71+
}
72+
}
73+
}
74+
75+
XMEMSET(session, 0, sizeof(session));
76+
XMEMSET(&storage, 0, sizeof(storage));
77+
78+
printf("TPM2.0 Key Import example\n");
79+
printf("\tKey Blob: %s\n", outputFile);
80+
printf("\tAlgorithm: %s\n", TPM2_GetAlgName(alg));
81+
82+
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
83+
if (rc != TPM_RC_SUCCESS) {
84+
printf("\nwolfTPM2_Init failed\n");
85+
goto exit;
86+
}
87+
88+
/* Define the default session auth that has NULL password */
89+
session[0].sessionHandle = TPM_RS_PW;
90+
session[0].auth.size = 0;
91+
TPM2_SetSessionAuth(session);
92+
93+
/* See if SRK already exists */
94+
rc = wolfTPM2_ReadPublicKey(&dev, &storage, TPM2_DEMO_STORAGE_KEY_HANDLE);
95+
if (rc != 0) {
96+
printf("Loading SRK: Storage failed 0x%x: %s\n", rc,
97+
TPM2_GetRCString(rc));
98+
goto exit;
99+
}
100+
printf("Loading SRK: Storage 0x%x (%d bytes)\n",
101+
(word32)storage.handle.hndl, storage.pub.size);
102+
103+
/* set session for authorization of the storage key */
104+
session[0].auth.size = sizeof(gStorageKeyAuth)-1;
105+
XMEMCPY(session[0].auth.buffer, gStorageKeyAuth, session[0].auth.size);
106+
107+
XMEMSET(&impKey, 0, sizeof(impKey));
108+
if (alg == TPM_ALG_RSA) {
109+
/* Import raw RSA private key into TPM */
110+
rc = wolfTPM2_ImportRsaPrivateKey(&dev, &storage, &impKey,
111+
kRsaKeyPubModulus, (word32)sizeof(kRsaKeyPubModulus),
112+
kRsaKeyPubExponent,
113+
kRsaKeyPrivQ, (word32)sizeof(kRsaKeyPrivQ),
114+
TPM_ALG_NULL, TPM_ALG_NULL);
115+
}
116+
else if (alg == TPM_ALG_ECC) {
117+
/* Import raw ECC private key into TPM */
118+
rc = wolfTPM2_ImportEccPrivateKey(&dev, &storage, &impKey,
119+
TPM_ECC_NIST_P256,
120+
kEccKeyPubXRaw, (word32)sizeof(kEccKeyPubXRaw),
121+
kEccKeyPubYRaw, (word32)sizeof(kEccKeyPubYRaw),
122+
kEccKeyPrivD, (word32)sizeof(kEccKeyPrivD));
123+
}
124+
if (rc != 0) goto exit;
125+
126+
printf("Imported %s key (pub %d, priv %d bytes)\n",
127+
TPM2_GetAlgName(alg), impKey.pub.size, impKey.priv.size);
128+
129+
/* Save key as encrypted blob to the disk */
130+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(NO_FILESYSTEM)
131+
f = XFOPEN(outputFile, "wb");
132+
if (f != XBADFILE) {
133+
impKey.pub.size = sizeof(impKey.pub);
134+
fileSz += XFWRITE(&impKey.pub, 1, sizeof(impKey.pub), f);
135+
fileSz += XFWRITE(&impKey.priv, 1, sizeof(UINT16) + impKey.priv.size, f);
136+
XFCLOSE(f);
137+
}
138+
printf("Wrote %d bytes to %s\n", (int)fileSz, outputFile);
139+
#else
140+
printf("Key Public Blob %d\n", impKey.pub.size);
141+
TPM2_PrintBin((const byte*)&impKey.pub.publicArea, impKey.pub.size);
142+
printf("Key Private Blob %d\n", impKey.priv.size);
143+
TPM2_PrintBin(impKey.priv.buffer, impKey.priv.size);
144+
#endif
145+
146+
exit:
147+
148+
if (rc != 0) {
149+
printf("\nFailure 0x%x: %s\n\n", rc, wolfTPM2_GetRCString(rc));
150+
}
151+
152+
/* Close key handles */
153+
wolfTPM2_UnloadHandle(&dev, &impKey.handle);
154+
155+
wolfTPM2_Cleanup(&dev);
156+
return rc;
157+
}
158+
159+
/******************************************************************************/
160+
/* --- END TPM Timestamp Test -- */
161+
/******************************************************************************/
162+
163+
164+
#ifndef NO_MAIN_DRIVER
165+
int main(int argc, char *argv[])
166+
{
167+
int rc;
168+
169+
rc = TPM2_Keyimport_Example(NULL, argc, argv);
170+
171+
return rc;
172+
}
173+
#endif

examples/keygen/keyload.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
/* --- BEGIN TPM Key Load Example -- */
3535
/******************************************************************************/
3636

37+
static void usage(void)
38+
{
39+
printf("Expected usage:\n");
40+
printf("keyload [keyblob.bin]\n");
41+
}
42+
3743
int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
3844
{
3945
int rc;
@@ -43,28 +49,32 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
4349
TPMS_AUTH_COMMAND session[MAX_SESSION_NUM];
4450
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(NO_FILESYSTEM)
4551
XFILE f;
52+
#endif
4653
const char* inputFile = "keyblob.bin";
4754

4855
if (argc >= 2) {
56+
if (XSTRNCMP(argv[1], "-?", 2) == 0 ||
57+
XSTRNCMP(argv[1], "-h", 2) == 0 ||
58+
XSTRNCMP(argv[1], "--help", 6) == 0) {
59+
usage();
60+
return 0;
61+
}
62+
4963
inputFile = argv[1];
5064
}
51-
#else
52-
(void)argc;
53-
(void)argv;
54-
#endif
5565

5666
XMEMSET(session, 0, sizeof(session));
5767
XMEMSET(&storage, 0, sizeof(storage));
5868
XMEMSET(&newKey, 0, sizeof(newKey));
5969

6070
printf("TPM2.0 Key load example\n");
71+
printf("\tKey Blob: %s\n", inputFile);
72+
6173
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
6274
if (rc != TPM_RC_SUCCESS) {
6375
printf("\nwolfTPM2_Init failed\n");
6476
goto exit;
6577
}
66-
printf("wolfTPM2_Init: success\n\n");
67-
6878

6979
/* Define the default session auth that has NULL password */
7080
session[0].sessionHandle = TPM_RS_PW;
@@ -98,7 +108,7 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
98108
rc = BUFFER_E; goto exit;
99109
}
100110
printf("Reading %d bytes from %s\n", (int)fileSz, inputFile);
101-
111+
102112
XFREAD(&newKey.pub, 1, sizeof(newKey.pub), f);
103113
if (fileSz > sizeof(newKey.pub)) {
104114
fileSz -= sizeof(newKey.pub);

0 commit comments

Comments
 (0)