📄 securestorage.c
字号:
vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Load_NVMing[%d],\n", buffer_len(&sealed_NVM)); Crypto_SHA1Full(sealed_NVM.bytes, buffer_len(&sealed_NVM), (BYTE *) &sealedNVMHash); // Verify measurement of sealed blob. if (memcmp(&sealedNVMHash, &myDMI->NVM_measurement, sizeof(TPM_DIGEST)) ) { vtpmlogerror(VTPM_LOG_VTPM, "VTPM LoadNVM NVM measurement check failed.\n"); vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Correct hash: "); for (i=0; i< sizeof(TPM_DIGEST); i++) vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", ((BYTE*)&myDMI->NVM_measurement)[i]); vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n"); vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Measured hash: "); for (i=0; i< sizeof(TPM_DIGEST); i++) vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", ((BYTE*)&sealedNVMHash)[i]); vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n"); status = TPM_AUTHFAIL; goto abort_egress; } TPMTRYRETURN( envelope_decrypt(&sealed_NVM, myDMI->TCSContext, vtpm_globals->storageKeyHandle, (const TPM_AUTHDATA*)&vtpm_globals->storage_key_usage_auth, outbuf) ); goto egress; abort_egress: vtpmlogerror(VTPM_LOG_VTPM, "Failed to load NVM\n."); egress: buffer_free( &sealed_NVM ); return status;}TPM_RESULT VTPM_SaveManagerData(void) { TPM_RESULT status=TPM_SUCCESS; int fh, dmis=-1; BYTE *flat_boot_key=NULL, *flat_dmis=NULL, *flat_enc=NULL; buffer_t clear_flat_global=NULL_BUF, enc_flat_global=NULL_BUF; UINT32 storageKeySize = buffer_len(&vtpm_globals->storageKeyWrap); UINT32 bootKeySize = buffer_len(&vtpm_globals->bootKeyWrap); struct pack_buf_t storage_key_pack = {storageKeySize, vtpm_globals->storageKeyWrap.bytes}; struct pack_buf_t boot_key_pack = {bootKeySize, vtpm_globals->bootKeyWrap.bytes}; BYTE vtpm_manager_gen = VTPM_MANAGER_GEN; struct hashtable_itr *dmi_itr; VTPM_DMI_RESOURCE *dmi_res; UINT32 boot_key_size = 0, flat_dmis_size = 0; // Initially fill these with buffer sizes for each data type. Later fill // in actual size, once flattened. boot_key_size = sizeof(UINT32) + // bootkeysize bootKeySize; // boot key TPMTRYRETURN(buffer_init(&clear_flat_global,sizeof(BYTE) + // manager version 3*sizeof(TPM_DIGEST) + // Auths sizeof(UINT32) +// storagekeysize storageKeySize, NULL) ); // storage key flat_boot_key = (BYTE *) malloc( boot_key_size ); flat_enc = (BYTE *) malloc( sizeof(UINT32) ); boot_key_size = BSG_PackList(flat_boot_key, 1, BSG_TPM_SIZE32_DATA, &boot_key_pack); BSG_PackList(clear_flat_global.bytes, 4, BSG_TYPE_BYTE, &vtpm_manager_gen, BSG_TPM_AUTHDATA, &vtpm_globals->owner_usage_auth, BSG_TPM_SECRET, &vtpm_globals->storage_key_usage_auth, BSG_TPM_SIZE32_DATA, &storage_key_pack); TPMTRYRETURN(envelope_encrypt(&clear_flat_global, &vtpm_globals->bootKey, &enc_flat_global) ); BSG_PackConst(buffer_len(&enc_flat_global), 4, flat_enc); // Per DMI values to be saved (if any exit) if (hashtable_count(vtpm_globals->dmi_map) > 1) { flat_dmis = (BYTE *) malloc( (hashtable_count(vtpm_globals->dmi_map) - 1) * // num DMIS (-1 for Dom0) (sizeof(UINT32) +sizeof(BYTE) + 2*sizeof(TPM_DIGEST)) ); // Per DMI info dmi_itr = hashtable_iterator(vtpm_globals->dmi_map); do { dmi_res = (VTPM_DMI_RESOURCE *) hashtable_iterator_value(dmi_itr); dmis++; // No need to save dmi0. if (dmi_res->dmi_id == 0) continue; flat_dmis_size += BSG_PackList( flat_dmis + flat_dmis_size, 4, BSG_TYPE_UINT32, &dmi_res->dmi_id, BSG_TYPE_BYTE, &dmi_res->dmi_type, BSG_TPM_DIGEST, &dmi_res->NVM_measurement, BSG_TPM_DIGEST, &dmi_res->DMI_measurement); } while (hashtable_iterator_advance(dmi_itr)); } fh = open(STATE_FILE, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE); if (fh == -1) { vtpmlogerror(VTPM_LOG_VTPM, "Unable to open %s file for write.\n", STATE_FILE); status = TPM_IOERROR; goto abort_egress; } if ( ( write(fh, flat_boot_key, boot_key_size) != boot_key_size ) || ( write(fh, flat_enc, sizeof(UINT32)) != sizeof(UINT32) ) || ( write(fh, enc_flat_global.bytes, buffer_len(&enc_flat_global)) != buffer_len(&enc_flat_global) ) || ( write(fh, flat_dmis, flat_dmis_size) != flat_dmis_size ) ) { vtpmlogerror(VTPM_LOG_VTPM, "Failed to completely write service data.\n"); status = TPM_IOERROR; goto abort_egress; } goto egress; abort_egress: egress: free(flat_boot_key); free(flat_enc); buffer_free(&enc_flat_global); free(flat_dmis); close(fh); vtpmloginfo(VTPM_LOG_VTPM, "Saved VTPM Manager state (status = %d, dmis = %d)\n", (int) status, dmis); return status;}TPM_RESULT VTPM_LoadManagerData(void) { TPM_RESULT status=TPM_SUCCESS; int fh, stat_ret, dmis=0; long fh_size = 0, step_size; BYTE *flat_table=NULL; buffer_t unsealed_data, enc_table_abuf; struct pack_buf_t storage_key_pack, boot_key_pack; UINT32 *dmi_id_key, enc_size; BYTE vtpm_manager_gen; VTPM_DMI_RESOURCE *dmi_res; UINT32 dmi_id; BYTE dmi_type; struct stat file_stat; TPM_HANDLE boot_key_handle; TPM_AUTHDATA boot_usage_auth; memset(&boot_usage_auth, 0, sizeof(TPM_AUTHDATA)); fh = open(STATE_FILE, O_RDONLY ); stat_ret = fstat(fh, &file_stat); if (stat_ret == 0) fh_size = file_stat.st_size; else { status = TPM_IOERROR; goto abort_egress; } flat_table = (BYTE *) malloc(fh_size); if ((long) read(fh, flat_table, fh_size) != fh_size ) { status = TPM_IOERROR; goto abort_egress; } // Read Boot Key step_size = BSG_UnpackList( flat_table, 2, BSG_TPM_SIZE32_DATA, &boot_key_pack, BSG_TYPE_UINT32, &enc_size); TPMTRYRETURN(buffer_init(&vtpm_globals->bootKeyWrap, 0, 0) ); TPMTRYRETURN(buffer_init_alias_convert(&enc_table_abuf, enc_size, flat_table + step_size) ); TPMTRYRETURN(buffer_append_raw(&vtpm_globals->bootKeyWrap, boot_key_pack.size, boot_key_pack.data) ); //Load Boot Key TPMTRYRETURN( VTSP_LoadKey( vtpm_globals->manager_tcs_handle, TPM_SRK_KEYHANDLE, &vtpm_globals->bootKeyWrap, &SRK_AUTH, &boot_key_handle, &vtpm_globals->keyAuth, &vtpm_globals->bootKey, FALSE) ); TPMTRYRETURN( envelope_decrypt(&enc_table_abuf, vtpm_globals->manager_tcs_handle, boot_key_handle, (const TPM_AUTHDATA*) &boot_usage_auth, &unsealed_data) ); step_size += enc_size; if (*unsealed_data.bytes != VTPM_MANAGER_GEN) { // Once there is more than one gen, this will include some compatability stuff vtpmlogerror(VTPM_LOG_VTPM, "Warning: Manager Data file is gen %d, which this manager is gen %d.\n", vtpm_manager_gen, VTPM_MANAGER_GEN); } // Global Values needing to be saved BSG_UnpackList( unsealed_data.bytes, 4, BSG_TYPE_BYTE, &vtpm_manager_gen, BSG_TPM_AUTHDATA, &vtpm_globals->owner_usage_auth, BSG_TPM_SECRET, &vtpm_globals->storage_key_usage_auth, BSG_TPM_SIZE32_DATA, &storage_key_pack); TPMTRYRETURN(buffer_init(&vtpm_globals->storageKeyWrap, 0, 0) ); TPMTRYRETURN(buffer_append_raw(&vtpm_globals->storageKeyWrap, storage_key_pack.size, storage_key_pack.data) ); // Per DMI values to be saved while ( step_size < fh_size ){ if (fh_size - step_size < (long) (sizeof(UINT32) + sizeof(BYTE) + 2*sizeof(TPM_DIGEST))) { vtpmlogerror(VTPM_LOG_VTPM, "Encountered %ld extra bytes at end of manager state.\n", fh_size-step_size); step_size = fh_size; } else { step_size += BSG_UnpackList(flat_table + step_size, 2, BSG_TYPE_UINT32, &dmi_id, BSG_TYPE_BYTE, &dmi_type); //TODO: Try and gracefully recover from problems. TPMTRYRETURN(init_dmi(dmi_id, dmi_type, &dmi_res) ); dmis++; step_size += BSG_UnpackList(flat_table + step_size, 2, BSG_TPM_DIGEST, &dmi_res->NVM_measurement, BSG_TPM_DIGEST, &dmi_res->DMI_measurement); } } vtpmloginfo(VTPM_LOG_VTPM, "Loaded saved state (dmis = %d).\n", dmis); goto egress; abort_egress: vtpmlogerror(VTPM_LOG_VTPM, "Failed to load service data with error = %s\n", tpm_get_error_name(status)); egress: free(flat_table); close(fh); // TODO: Could be nice and evict BootKey. (Need to add EvictKey to VTSP. return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -