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