github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/src/tpm2/RestoreCloudProxyKeyHierarchy.cc (about) 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <fcntl.h> 6 #include <unistd.h> 7 #include <string.h> 8 9 #include <tpm20.h> 10 #include <tpm2_lib.h> 11 #include <gflags/gflags.h> 12 13 // 14 // Copyright 2015 Google Corporation, All Rights Reserved. 15 // 16 // Licensed under the Apache License, Version 2.0 (the "License"); 17 // you may not use this file except in compliance with the License. 18 // You may obtain a copy of the License at 19 // http://www.apache.org/licenses/LICENSE-2.0 20 // or in the the file LICENSE-2.0.txt in the top level sourcedirectory 21 // Unless required by applicable law or agreed to in writing, software 22 // distributed under the License is distributed on an "AS IS" BASIS, 23 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 24 // See the License for the specific language governing permissions and 25 // limitations under the License 26 // 27 // Portions of this code were derived TPM2.0-TSS published 28 // by Intel under the license set forth in intel_license.txt 29 // and downloaded on or about August 6, 2015. 30 // Portions of this code were derived tboot published 31 // by Intel under the license set forth in intel_license.txt 32 // and downloaded on or about August 6, 2015. 33 // Portions of this code were derived from the crypto utility 34 // published by John Manferdelli under the Apache 2.0 license. 35 // See github.com/jlmucb/crypto. 36 // File: RestoreCloudProxyKeyHierarchy.cc 37 38 // This program reloads primary key, signing key (for quotes) and 39 // sealing key under the owner hierarchy from nv ram. It optionally 40 // unseals sealed file contents and verifies quoted file contents. 41 42 // Calling sequence 43 // RestoreCloudProxyKeyHierarchy.exe 44 // --slot_primary=int32 --slot_seal=int32 --slot_quote=int32 45 // --seal_value=value-string --quote_value=value-string 46 // --pcr_hash_alg_name=[sha1 | sha256] 47 // --pcr_list="int, int, ..." --seal_output_file=output-file-name 48 // --quote_output_file= output-file-name --pcr_file=output-file-name 49 50 using std::string; 51 52 #define CALLING_SEQUENCE "RestoreCloudProxyKeyHierarchy.exe " \ 53 "--slot_primary=int32 --slot_seal=int32 " \ 54 "--slot_quote=int32" \ 55 "--seal_value=value-string --quote_value=value-string " \ 56 "--pcr_hash_alg_name=[sha1 | sha256]" \ 57 "--pcr_list=\"int, int, ...\" --seal_output_file=output-file-name" \ 58 "--quote_output_file= output-file-name --pcr_file=output-file-name\n" 59 60 void PrintOptions() { 61 printf("Calling sequence: %s", CALLING_SEQUENCE); 62 } 63 64 DEFINE_int32(slot_primary, 0, ""); 65 DEFINE_int32(slot_seal, 1, ""); 66 DEFINE_int32(slot_quote, 2, ""); 67 DEFINE_string(seal_value, "", "test seal value"); 68 DEFINE_string(quote_value, "", "test quote value"); 69 DEFINE_string(pcr_hash_alg_name, "", "hash alg (sha1 or sha256"); 70 DEFINE_string(pcr_list, "", "comma separated pcr list"); 71 DEFINE_string(seal_output_file, "", "output-file-name"); 72 DEFINE_string(quote_output_file, "", "output-file-name"); 73 DEFINE_string(pcr_file, "", "output-file-name"); 74 DEFINE_string(hash_alg, "sha1", "hash alg"); 75 76 #ifndef GFLAGS_NS 77 #define GFLAGS_NS google 78 #endif 79 80 #define MAX_SIZE_PARAMS 4096 81 82 int main(int an, char** av) { 83 LocalTpm tpm; 84 int ret_val = 0; 85 86 printf("\nRestoreCloudProxyKeyHierarchy\n\n"); 87 88 GFLAGS_NS::ParseCommandLineFlags(&an, &av, true); 89 if (!tpm.OpenTpm("/dev/tpm0")) { 90 printf("Can't open tpm\n"); 91 return 1; 92 } 93 94 TPM_ALG_ID hash_alg_id; 95 if (FLAGS_hash_alg == "sha1") { 96 hash_alg_id = TPM_ALG_SHA1; 97 } else if (FLAGS_hash_alg == "sha256") { 98 hash_alg_id = TPM_ALG_SHA256; 99 } else { 100 printf("Unknown hash algorithm\n"); 101 return 1; 102 } 103 104 105 string authString("01020304"); 106 string parentAuth("01020304"); 107 string emptyAuth; 108 109 TPML_PCR_SELECTION pcrSelect; 110 111 TPM_HANDLE root_handle = 0; 112 TPM_HANDLE seal_handle = 0; 113 TPM_HANDLE quote_handle = 0; 114 TPM_HANDLE nv_handle = 0; 115 byte context_save_area[MAX_SIZE_PARAMS]; 116 uint16_t context_data_size = 924; 117 118 InitSinglePcrSelection(7, hash_alg_id, &pcrSelect); 119 120 // root handle 121 memset(context_save_area, 0, MAX_SIZE_PARAMS); 122 nv_handle = GetNvHandle(FLAGS_slot_primary); 123 if (!Tpm2_ReadNv(tpm, nv_handle, authString, &context_data_size, 124 context_save_area)) { 125 printf("Root ReadNv failed\n"); 126 ret_val = 1; 127 goto done; 128 } 129 130 #ifdef DEBUG_EXTRA 131 printf("\ncontext_save_area: "); 132 PrintBytes(context_data_size, context_save_area); 133 printf("\n\n"); 134 #endif 135 136 if (!Tpm2_LoadContext(tpm, context_data_size, context_save_area, 137 &root_handle)) { 138 printf("Root LoadContext failed\n"); 139 ret_val = 1; 140 goto done; 141 } 142 143 // seal handle 144 memset(context_save_area, 0, MAX_SIZE_PARAMS); 145 nv_handle = GetNvHandle(FLAGS_slot_seal); 146 if (!Tpm2_ReadNv(tpm, nv_handle, authString, &context_data_size, 147 context_save_area)) { 148 printf("Root ReadNv failed\n"); 149 ret_val = 1; 150 goto done; 151 } 152 printf("context_save_area: "); 153 PrintBytes(context_data_size, context_save_area); 154 printf("\n"); 155 if (!Tpm2_LoadContext(tpm, context_data_size, context_save_area, 156 &seal_handle)) { 157 printf("Root LoadContext failed\n"); 158 ret_val = 1; 159 goto done; 160 } 161 162 // quote handle 163 memset(context_save_area, 0, MAX_SIZE_PARAMS); 164 nv_handle = GetNvHandle(FLAGS_slot_quote); 165 if (!Tpm2_ReadNv(tpm, nv_handle, authString, &context_data_size, 166 context_save_area)) { 167 printf("Quote ReadNv failed\n"); 168 ret_val = 1; 169 goto done; 170 } 171 if (!Tpm2_LoadContext(tpm, context_data_size, context_save_area, 172 "e_handle)) { 173 printf("Quote LoadContext failed\n"); 174 ret_val = 1; 175 goto done; 176 } 177 178 done: 179 if (root_handle != 0) { 180 Tpm2_FlushContext(tpm, root_handle); 181 } 182 if (seal_handle != 0) { 183 Tpm2_FlushContext(tpm, seal_handle); 184 } 185 if (quote_handle != 0) { 186 Tpm2_FlushContext(tpm, quote_handle); 187 } 188 189 tpm.CloseTpm(); 190 return ret_val; 191 } 192