Arun kumar Sekar | 2ac5633 | 2018-03-30 11:20:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Intel Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | // |
| 17 | // main.c : Tool to import Openssl RSA key into TPM |
| 18 | // Author: Arun Kumar Sekar |
| 19 | // |
| 20 | |
| 21 | #include <stdio.h> |
Kiran Kamineni | a0eb11e | 2018-08-30 15:41:16 -0700 | [diff] [blame^] | 22 | #include <stdlib.h> |
Arun kumar Sekar | 2ac5633 | 2018-03-30 11:20:30 -0700 | [diff] [blame] | 23 | #include <string.h> |
Kiran Kamineni | a0eb11e | 2018-08-30 15:41:16 -0700 | [diff] [blame^] | 24 | #include <unistd.h> |
Arun kumar Sekar | 2ac5633 | 2018-03-30 11:20:30 -0700 | [diff] [blame] | 25 | |
| 26 | #include <sapi/tpm20.h> |
| 27 | |
Kiran Kamineni | a0eb11e | 2018-08-30 15:41:16 -0700 | [diff] [blame^] | 28 | #include "tpm_wrapper.h" |
| 29 | #include "util.h" |
Arun kumar Sekar | 2ac5633 | 2018-03-30 11:20:30 -0700 | [diff] [blame] | 30 | |
| 31 | void PrintHelp(); |
| 32 | char version[] = "0.1"; |
| 33 | |
| 34 | void PrintHelp() |
| 35 | { |
| 36 | printf( |
Kiran Kamineni | a0eb11e | 2018-08-30 15:41:16 -0700 | [diff] [blame^] | 37 | "OSSL key to tpm import tool, Version %s\nUsage:" |
| 38 | "./ossl_tpm_import " |
| 39 | "[-dupPub out_dupPubFile] [-dupPriv out_dupPrivFile] [-dupSymSeed out_dupSymSeedFile] " |
| 40 | "[-dupEncKey out_dupEncKeyFile] [-password keyPassword] " |
| 41 | "[-pub out_keyPub] [-priv out_KeyPriv] [-H primaryKeyHandle]\n" |
Arun kumar Sekar | 2ac5633 | 2018-03-30 11:20:30 -0700 | [diff] [blame] | 42 | "\n" |
| 43 | , version); |
| 44 | } |
| 45 | |
| 46 | int main(int argc, char* argv[]) |
| 47 | { |
| 48 | TPM_RC rval = 0; |
| 49 | int count=0; |
| 50 | TSS2_TCTI_CONTEXT *tcti_ctx = 0; |
| 51 | TSS2_SYS_CONTEXT *sysContext = 0; |
| 52 | |
| 53 | // SW Key Duplicate O/P variables |
| 54 | char dupPub_Filename[256]; |
| 55 | int dupPub_flag = 0; |
| 56 | char dupPriv_Filename[256]; |
| 57 | int dupPriv_flag = 0; |
| 58 | char dupSymSeed_Filename[256]; |
| 59 | int dupSymSeed_flag = 0; |
| 60 | char dupEncKey_Filename[256]; |
| 61 | int dupEncKey_flag = 0; |
Kiran Kamineni | a0eb11e | 2018-08-30 15:41:16 -0700 | [diff] [blame^] | 62 | char keyPassword[256] = {0}; |
| 63 | TPM2B_DATA encryptionKey; |
| 64 | TPM2B_PUBLIC swKeyPublic; |
| 65 | TPM2B_PRIVATE swKeyPrivate; |
| 66 | TPM2B_ENCRYPTED_SECRET encSymSeed; |
Arun kumar Sekar | 2ac5633 | 2018-03-30 11:20:30 -0700 | [diff] [blame] | 67 | |
| 68 | // SW Key Import O/P variables |
| 69 | char pub_Filename[256]; |
| 70 | int pub_flag = 0; |
| 71 | char priv_Filename[256]; |
| 72 | int priv_flag = 0; |
| 73 | unsigned short file_size = 0; |
| 74 | |
| 75 | TPM_HANDLE primaryKeyHandle = 0; |
| 76 | int H_flag = 0; |
| 77 | |
| 78 | TPM2B_PUBLIC parentKeyPublicPortion; |
| 79 | int pubKeysize = 0; |
| 80 | |
| 81 | setbuf(stdout, NULL); |
| 82 | setvbuf (stdout, NULL, _IONBF, BUFSIZ); |
| 83 | if( (argc < 2) ) |
| 84 | { |
| 85 | printf("Arguments count does not match \n"); |
| 86 | PrintHelp(); |
| 87 | return 1; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | /* Get the argument values and evaluate it */ |
| 92 | for( count = 1; count < argc; count++ ) |
| 93 | { |
| 94 | if( 0 == strcmp( argv[count], "-dupPub" ) ) { |
| 95 | count++; |
| 96 | if( (1 != sscanf( argv[count], "%s", dupPub_Filename )) ) |
| 97 | { |
| 98 | PrintHelp(); |
| 99 | return 1; |
| 100 | } |
| 101 | dupPub_flag = 1; |
| 102 | } |
| 103 | else if( 0 == strcmp( argv[count], "-dupPriv" ) ) { |
| 104 | count++; |
| 105 | if( (1 != sscanf( argv[count], "%s", dupPriv_Filename )) ) |
| 106 | { |
| 107 | PrintHelp(); |
| 108 | return 1; |
| 109 | } |
| 110 | dupPriv_flag = 1; |
| 111 | } |
| 112 | else if( 0 == strcmp( argv[count], "-dupSymSeed" ) ) { |
| 113 | count++; |
| 114 | if( (1 != sscanf( argv[count], "%s", dupSymSeed_Filename )) ) |
| 115 | { |
| 116 | PrintHelp(); |
| 117 | return 1; |
| 118 | } |
| 119 | dupSymSeed_flag = 1; |
| 120 | } |
| 121 | else if( 0 == strcmp( argv[count], "-dupEncKey" ) ) { |
| 122 | count++; |
| 123 | if( (1 != sscanf( argv[count], "%s", dupEncKey_Filename )) ) |
| 124 | { |
| 125 | PrintHelp(); |
| 126 | return 1; |
| 127 | } |
| 128 | dupEncKey_flag = 1; |
| 129 | } |
Kiran Kamineni | a0eb11e | 2018-08-30 15:41:16 -0700 | [diff] [blame^] | 130 | else if( 0 == strcmp( argv[count], "-password" ) ) { |
| 131 | count++; |
| 132 | // Read no more than a fixed length of characters |
| 133 | if ( (1 != sscanf(argv[count], "%255s", keyPassword )) ) |
| 134 | { |
| 135 | PrintHelp(); |
| 136 | return 1; |
| 137 | } |
| 138 | } |
Arun kumar Sekar | 2ac5633 | 2018-03-30 11:20:30 -0700 | [diff] [blame] | 139 | else if( 0 == strcmp( argv[count], "-pub" ) ) { |
| 140 | count++; |
| 141 | if( (1 != sscanf( argv[count], "%s", pub_Filename )) ) |
| 142 | { |
| 143 | PrintHelp(); |
| 144 | return 1; |
| 145 | } |
| 146 | pub_flag = 1; |
| 147 | } |
| 148 | else if( 0 == strcmp( argv[count], "-priv" ) ) { |
| 149 | count++; |
| 150 | if( (1 != sscanf( argv[count], "%s", priv_Filename )) ) |
| 151 | { |
| 152 | PrintHelp(); |
| 153 | return 1; |
| 154 | } |
| 155 | priv_flag = 1; |
| 156 | } |
| 157 | else if( 0 == strcmp( argv[count], "-H" ) ) { |
| 158 | count++; |
| 159 | primaryKeyHandle = strtoul(argv[count], NULL, 16); |
| 160 | printf("Primary Key handle Given: 0x%x \n", primaryKeyHandle); |
| 161 | H_flag = 1; |
| 162 | } |
| 163 | else if( 0 == strcmp( argv[count], "--help" ) ) { |
| 164 | PrintHelp(); |
| 165 | exit(1); |
| 166 | } |
| 167 | else { |
| 168 | PrintHelp(); |
| 169 | exit(1); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if((!H_flag)) { |
| 175 | printf("Parent handle should be passed for TPM import operation \n"); |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | // For TPM Import functionality, check all input params are present |
| 180 | if( (!dupPub_flag) || |
Kiran Kamineni | a0eb11e | 2018-08-30 15:41:16 -0700 | [diff] [blame^] | 181 | (!dupPriv_flag) || |
| 182 | (!dupSymSeed_flag) || |
| 183 | (!dupEncKey_flag) || |
| 184 | (!pub_flag) || |
| 185 | (!priv_flag) |
Arun kumar Sekar | 2ac5633 | 2018-03-30 11:20:30 -0700 | [diff] [blame] | 186 | ) { |
| 187 | printf("Error: One or more Inputs for TPM import functionality is missing ! \n"); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | /* SW Key TPM Import operation started */ |
| 192 | if(rval == 0) { |
| 193 | file_size = sizeof(TPM2B_PUBLIC); |
| 194 | rval = loadDataFromFile(dupPub_Filename, (UINT8 *) &swKeyPublic, &file_size); |
| 195 | if ( rval == 0 ) { |
| 196 | file_size = sizeof(TPM2B_PRIVATE); |
| 197 | rval = loadDataFromFile(dupPriv_Filename, (UINT8 *) &swKeyPrivate, &file_size); |
| 198 | } |
| 199 | if ( rval == 0 ) { |
| 200 | file_size = sizeof(TPM2B_ENCRYPTED_SECRET); |
| 201 | rval = loadDataFromFile(dupSymSeed_Filename, (UINT8 *) &encSymSeed, &file_size); |
| 202 | } |
| 203 | if ( rval == 0 ) { |
| 204 | file_size = sizeof(TPM2B_DATA); |
| 205 | rval = loadDataFromFile(dupEncKey_Filename, (UINT8 *) &encryptionKey, &file_size); |
| 206 | } |
| 207 | |
| 208 | if ( rval == 0 ) { |
| 209 | /* Initialize TCTI and sapi context */ |
| 210 | tcti_ctx = tpm_tcti_tabrmd_init(); |
| 211 | if(tcti_ctx == NULL) { |
| 212 | printf("Creation of TCTI context with TABRMD failed ! \n"); |
| 213 | goto end; |
| 214 | } |
| 215 | |
| 216 | sysContext = sys_ctx_init(tcti_ctx); |
| 217 | if(sysContext == NULL) { |
| 218 | printf("Creation of SAPI context with TABRMD failed ! \n"); |
| 219 | goto end; |
| 220 | } |
| 221 | printf("\nInitializing TPM context success: 0x%x ! \n", rval); |
| 222 | } |
| 223 | |
| 224 | TPM2B_PRIVATE importPrivate; |
| 225 | INIT_SIMPLE_TPM2B_SIZE(importPrivate); |
Kiran Kamineni | a0eb11e | 2018-08-30 15:41:16 -0700 | [diff] [blame^] | 226 | rval = swKeyTpmImport(sysContext, primaryKeyHandle, |
| 227 | &encryptionKey, &swKeyPublic, &swKeyPrivate, &encSymSeed, |
| 228 | keyPassword, strlen(keyPassword), |
Arun kumar Sekar | 2ac5633 | 2018-03-30 11:20:30 -0700 | [diff] [blame] | 229 | &importPrivate); |
| 230 | if(rval != 0) { |
| 231 | printf("\nswKeyTpmImport failed: 0x%x ! \n", rval); |
| 232 | goto end; |
| 233 | } |
| 234 | else { |
| 235 | printf("\nswKeyImport success: 0x%x ! \n", rval); |
| 236 | saveDataToFile(pub_Filename, (UINT8 *) &swKeyPublic, sizeof(TPM2B_PUBLIC)); |
| 237 | saveDataToFile(priv_Filename, (UINT8 *) &importPrivate, sizeof(TPM2B_PRIVATE)); |
| 238 | printf("\nOutput files are written successfully ! \n"); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | end: |
| 243 | if(sysContext) { |
| 244 | TeardownSysContext(&sysContext); |
| 245 | } |
| 246 | if(tcti_ctx) { |
| 247 | TeardownTctiContext(tcti_ctx); |
| 248 | } |
| 249 | |
| 250 | return rval; |
| 251 | } |