int hash(TPMI_RH_HIERARCHY hierarchyValue, TPM2B_MAX_BUFFER *data, TPMI_ALG_HASH halg, const char *outHashFilePath, const char *outTicketFilePath) { UINT32 rval; TPM2B_DIGEST outHash = { { sizeof(TPM2B_DIGEST)-2, } }; TPMT_TK_HASHCHECK validation; rval = Tss2_Sys_Hash(sysContext, 0, data, halg, hierarchyValue, &outHash, &validation, 0); if(rval != TPM_RC_SUCCESS) { printf("\n......TPM2_Hash Error. TPM Error:0x%x......\n", rval); return -1; } printf("\ntpm2_hash succ.\n\n"); printf("\nhash value(hex type): "); for(UINT16 i = 0; i < outHash.t.size; i++) printf("%02x ", outHash.t.buffer[i]); printf("\n"); printf("\nvalidation value(hex type): "); for(INT16 j = 0; j < validation.digest.t.size; j++) printf("%02x ", validation.digest.t.buffer[j]); printf("\n"); if(saveDataToFile(outHashFilePath, (UINT8 *)&outHash, sizeof(outHash))) return -2; if(saveDataToFile(outTicketFilePath, (UINT8 *)&validation, sizeof(validation))) return -3; return 0; }
int sign(TPMI_DH_OBJECT keyHandle, TPMI_ALG_HASH halg, BYTE *msg, UINT16 length, TPMT_TK_HASHCHECK *validation, const char *outFilePath) { UINT32 rval; TPM2B_DIGEST digest; TPMT_SIG_SCHEME inScheme; TPMT_SIGNATURE signature; TSS2_SYS_CMD_AUTHS sessionsData; TPMS_AUTH_RESPONSE sessionDataOut; TSS2_SYS_RSP_AUTHS sessionsDataOut; TPMS_AUTH_COMMAND *sessionDataArray[1]; TPMS_AUTH_RESPONSE *sessionDataOutArray[1]; sessionDataArray[0] = &sessionData; sessionsData.cmdAuths = &sessionDataArray[0]; sessionDataOutArray[0] = &sessionDataOut; sessionsDataOut.rspAuths = &sessionDataOutArray[0]; sessionsDataOut.rspAuthsCount = 1; sessionsData.cmdAuthsCount = 1; sessionData.sessionHandle = TPM_RS_PW; sessionData.nonce.t.size = 0; *((UINT8 *)((void *)&sessionData.sessionAttributes)) = 0; if(computeDataHash(msg, length, halg, &digest)) { printf("Compute message hash failed !\n"); return -1; } printf("\ndigest(hex type):\n "); for(UINT16 i = 0; i < digest.t.size; i++) printf("%02x ", digest.t.buffer[i]); printf("\n"); if(setScheme(keyHandle, halg, &inScheme)) { printf("No suitable signing scheme!\n"); return -2; } rval = Tss2_Sys_Sign(sysContext, keyHandle, &sessionsData, &digest, &inScheme, validation, &signature, &sessionsDataOut); if(rval != TPM_RC_SUCCESS) { printf("tpm2_sign failed, error code: 0x%x\n", rval); return -3; } printf("\ntpm2_sign succ.\n"); if(saveDataToFile(outFilePath, (UINT8 *)&signature, sizeof(signature))) { printf("failed to save signature into %s\n", outFilePath); return -4; } return 0; }
int encryptDecrypt(TPMI_DH_OBJECT keyHandle, TPMI_YES_NO decryptVal, TPM2B_MAX_BUFFER *inData, const char *outFilePath) { UINT32 rval; // Inputs TPMI_ALG_SYM_MODE mode; TPM2B_IV ivIn; // Outputs TPM2B_MAX_BUFFER outData = { { sizeof(TPM2B_MAX_BUFFER)-2, } }; TPM2B_IV ivOut = { { sizeof(TPM2B_IV)-2, } }; TSS2_SYS_CMD_AUTHS sessionsData; TPMS_AUTH_RESPONSE sessionDataOut; TSS2_SYS_RSP_AUTHS sessionsDataOut; TPMS_AUTH_COMMAND *sessionDataArray[1]; TPMS_AUTH_RESPONSE *sessionDataOutArray[1]; sessionDataArray[0] = &sessionData; sessionsData.cmdAuths = &sessionDataArray[0]; sessionDataOutArray[0] = &sessionDataOut; sessionsDataOut.rspAuths = &sessionDataOutArray[0]; sessionsDataOut.rspAuthsCount = 1; sessionData.sessionHandle = TPM_RS_PW; sessionData.nonce.t.size = 0; *((UINT8 *)((void *)&sessionData.sessionAttributes)) = 0; sessionsData.cmdAuthsCount = 1; sessionsData.cmdAuths[0] = &sessionData; mode = TPM_ALG_NULL; ivIn.t.size = MAX_SYM_BLOCK_SIZE; memset(ivIn.t.buffer, 0, MAX_SYM_BLOCK_SIZE); if(decryptVal == NO) printf("\nENCRYPTDECRYPT: ENCRYPT\n"); if(decryptVal == YES) printf("\nENCRYPTDECRYPT: DECRYPT\n"); rval = Tss2_Sys_EncryptDecrypt(sysContext, keyHandle, &sessionsData, decryptVal, mode, &ivIn, inData, &outData, &ivOut, &sessionsDataOut); if(rval != TPM_RC_SUCCESS) { printf("EncryptDecrypt failed, error code: 0x%x\n", rval); return -1; } printf("\nEncryptDecrypt succ.\n"); if(saveDataToFile(outFilePath, (UINT8 *)outData.t.buffer, outData.t.size)) return -2; printf("OutFile %s completed!\n", outFilePath); return 0; }
void DataLayer::setDataModel(Day* currentDay) { if (_currentDay != NULL) { _currentDay->deleteLater(); } _currentDay = currentDay; _dataModel = _currentDay->getAppointments(); emit dataModelChanged(); saveDataToFile(); setPending(false); }
UINT32 unseal(TPMI_DH_OBJECT itemHandle, const char *outFileName, int P_flag) { UINT32 rval; TPMS_AUTH_RESPONSE sessionDataOut; TSS2_SYS_CMD_AUTHS sessionsData; TSS2_SYS_RSP_AUTHS sessionsDataOut; TPMS_AUTH_COMMAND *sessionDataArray[1]; TPMS_AUTH_RESPONSE *sessionDataOutArray[1]; TPM2B_SENSITIVE_DATA outData = {{sizeof(TPM2B_SENSITIVE_DATA)-2, }}; sessionDataArray[0] = &sessionData; sessionDataOutArray[0] = &sessionDataOut; sessionsDataOut.rspAuths = &sessionDataOutArray[0]; sessionsData.cmdAuths = &sessionDataArray[0]; sessionsDataOut.rspAuthsCount = 1; sessionsData.cmdAuthsCount = 1; sessionData.sessionHandle = TPM_RS_PW; sessionData.nonce.t.size = 0; *((UINT8 *)((void *)&sessionData.sessionAttributes)) = 0; if(P_flag == 0) sessionData.hmac.t.size = 0; rval = Tss2_Sys_Unseal(sysContext, itemHandle, &sessionsData, &outData, &sessionsDataOut); if(rval != TPM_RC_SUCCESS) { printf("Unseal failed. Error Code: 0x%x\n", rval); return -1; } printf("\nUnseal succ.\nUnsealed data: "); for(UINT16 i = 0; i < outData.t.size; i++) printf(" 0x%02x", outData.t.buffer[i]); printf("\n"); if(saveDataToFile(outFileName, (UINT8 *)&outData, sizeof(outData))) { printf("Failed to save unsealed data into %s\n", outFileName); return -2; } return 0; }
int rsaDecrypt(TPMI_DH_OBJECT keyHandle, TPM2B_PUBLIC_KEY_RSA *cipherText, const char *outFilePath) { UINT32 rval; TPMT_RSA_DECRYPT inScheme; TPM2B_DATA label; TPM2B_PUBLIC_KEY_RSA message; TSS2_SYS_CMD_AUTHS sessionsData; TPMS_AUTH_RESPONSE sessionDataOut; TSS2_SYS_RSP_AUTHS sessionsDataOut; TPMS_AUTH_COMMAND *sessionDataArray[1]; TPMS_AUTH_RESPONSE *sessionDataOutArray[1]; sessionDataArray[0] = &sessionData; sessionsData.cmdAuths = &sessionDataArray[0]; sessionDataOutArray[0] = &sessionDataOut; sessionsDataOut.rspAuths = &sessionDataOutArray[0]; sessionsDataOut.rspAuthsCount = 1; sessionsData.cmdAuthsCount = 1; sessionData.sessionHandle = TPM_RS_PW; sessionData.nonce.t.size = 0; *((UINT8 *)((void *)&sessionData.sessionAttributes)) = 0; inScheme.scheme = TPM_ALG_RSAES; label.t.size = 0; rval = Tss2_Sys_RSA_Decrypt(sysContext, keyHandle, &sessionsData, cipherText, &inScheme, &label, &message, &sessionsDataOut); if(rval != TPM_RC_SUCCESS) { printf("rsaDecrypt failed, error code: 0x%x\n", rval); return -1; } printf("\nRSA Decrypt succ.\n"); if(saveDataToFile(outFilePath, message.t.buffer, message.t.size)) { printf("OutFile %s failed!\n", outFilePath); return -1; } printf("OutFile %s completed!\n", outFilePath); return 0; }
void CQBarChart::contextMenuEvent(QContextMenuEvent *) { #ifdef DEBUG_UI qDebug() << "-- in qwt3dPlot.cpp Plot3d::contextMenuEvent --"; #endif QMenu* mpContextMenu = new QMenu(this); Q_CHECK_PTR(mpContextMenu); mpContextMenu->addAction("handling information", this, SLOT(hotKeysMessage())); if (mColorLegend) mpContextMenu->addAction("hide legend", this, SLOT(showLegend())); else mpContextMenu->addAction("show legend", this, SLOT(showLegend())); mpContextMenu->addAction("Print to &file", this, SLOT(saveDataToFile())); mpContextMenu->exec(QCursor::pos()); delete mpContextMenu; }
int getRandom(const char *outFileName, UINT16 bytesRequested) { TPM_RC rval; TPM2B_DIGEST randomBytes; rval = Tss2_Sys_GetRandom(sysContext, NULL, bytesRequested, &randomBytes, NULL); if (rval != TSS2_RC_SUCCESS) { printf("\n......TPM2_GetRandom Error. TPM Error:0x%x......\n", rval); return -1; } printf("\nGetRandom succ...\n"); printf("byte size: %d\n",randomBytes.t.size); for(UINT16 i = 0; i < randomBytes.t.size; i++) printf(" 0x%2.2X",randomBytes.t.buffer[i]); printf("\n"); if(saveDataToFile(outFileName, (UINT8 *)randomBytes.t.buffer, randomBytes.t.size)) return -2; return 0; }
void CQTSSAResultSubWidget::init() { assert(CCopasiRootContainer::getDatamodelList()->size() > 0); pTSSATask = dynamic_cast<CTSSATask *>((*(*CCopasiRootContainer::getDatamodelList())[0]->getTaskList())["Time Scale Separation Analysis"]); if (!pTSSATask) return; pProblem = dynamic_cast<CTSSAProblem*>(pTSSATask->getProblem()); pModel = pProblem->getModel(); pMethod = dynamic_cast<CTSSAMethod*>(pTSSATask->getMethod()); mpLabelTime->setNum(0); mpLabelStep->setNum(0); connect(mpBox1, SIGNAL(currentIndexChanged(int)), this, SLOT(slotTableChanged(/* int */))); //set colorsettings for ArrayAnnotationWidgets CColorScaleAdvanced * tcs = new CColorScaleAdvanced() ; tcs->setColorMin(QColor(240, 240, 240)); tcs->setColorMax(QColor(0, 255, 0)); mpArrayWidget->setColorCoding(tcs); mpArrayWidget->setColorScalingAutomatic(true); connect(mpSlider, SIGNAL(valueChanged(int)), this, SLOT(changeInterval())); connect(mpButton, SIGNAL(clicked()), this, SLOT(changeContents())); connect(ButtonSaveData, SIGNAL(clicked()), this, SLOT(saveDataToFile())); mpBox1->setEditable(false); mpIndex = 0; mpTimeScaleWidget->hide(); mpBox1->show(); mpArrayWidget->show(); }
int rsaEncrypt(TPMI_DH_OBJECT keyHandle, TPM2B_PUBLIC_KEY_RSA *message, const char *outFilePath) { UINT32 rval; // Inputs TPMT_RSA_DECRYPT inScheme; TPM2B_DATA label; // Outputs TPM2B_PUBLIC_KEY_RSA outData = { { sizeof(TPM2B_PUBLIC_KEY_RSA)-2, } }; TPMS_AUTH_RESPONSE sessionDataOut; TSS2_SYS_RSP_AUTHS sessionsDataOut; TPMS_AUTH_RESPONSE *sessionDataOutArray[1]; sessionDataOutArray[0] = &sessionDataOut; sessionsDataOut.rspAuths = &sessionDataOutArray[0]; sessionsDataOut.rspAuthsCount = 1; inScheme.scheme = TPM_ALG_RSAES; label.t.size = 0; rval = Tss2_Sys_RSA_Encrypt(sysContext, keyHandle, NULL, message, &inScheme, &label, &outData, &sessionsDataOut); if(rval != TPM_RC_SUCCESS) { printf("rsaencrypt failed, error code: 0x%x\n", rval); return -1; } printf("\nRSA Encrypt succ.\n"); if(saveDataToFile(outFilePath, outData.t.buffer, outData.t.size)) { printf("OutFile %s failed!\n", outFilePath); return -2; } printf("OutFile %s completed!\n",outFilePath); return 0; }
int readPublic(TPMI_DH_OBJECT objectHandle, const char *outFilePath) { UINT32 rval; TPMS_AUTH_RESPONSE sessionDataOut; TSS2_SYS_RSP_AUTHS sessionsDataOut; TPMS_AUTH_RESPONSE *sessionDataOutArray[1]; TPM2B_PUBLIC outPublic = { { 0, } }; TPM2B_NAME name = { { sizeof(TPM2B_NAME)-2, } }; TPM2B_NAME qualifiedName = { { sizeof(TPM2B_NAME)-2, } }; sessionDataOutArray[0] = &sessionDataOut; sessionsDataOut.rspAuths = &sessionDataOutArray[0]; sessionsDataOut.rspAuthsCount = 1; rval = Tss2_Sys_ReadPublic(sysContext, objectHandle, 0, &outPublic, &name, &qualifiedName, &sessionsDataOut); if(rval != TPM_RC_SUCCESS) { printf("\nTPM2_ReadPublic error: rval = 0x%0x\n\n",rval); return -1; } printf("\nTPM2_ReadPublic OutPut: \n"); printf("name: \n"); for(UINT16 i = 0; i < name.t.size; i++) printf("%02x ",name.t.name[i]); printf("\n"); printf("qualifiedName: \n"); for(UINT16 j = 0; j < qualifiedName.t.size; j++) printf("%02x ",qualifiedName.t.name[j]); printf("\n"); if(saveDataToFile(outFilePath, (UINT8 *)&outPublic, sizeof(outPublic))) return -2; return 0; }
int activateCredential() { UINT32 rval; TPM2B_DIGEST certInfoData; printf("\nACTIVATE CREDENTIAL TESTS:\n"); cmdAuth.sessionHandle = TPM_RS_PW; cmdAuth2.sessionHandle = TPM_RS_PW; *((UINT8 *)((void *)&cmdAuth.sessionAttributes)) = 0; *((UINT8 *)((void *)&cmdAuth2.sessionAttributes)) = 0; TPMS_AUTH_COMMAND *cmdSessionArray[2] = { &cmdAuth, &cmdAuth2 }; TSS2_SYS_CMD_AUTHS cmdAuthArray = { 2, &cmdSessionArray[0] }; rval = Tss2_Sys_ActivateCredential(sysContext,activateHandle ,keyHandle , &cmdAuthArray, &credentialBlob, &secret, &certInfoData, 0); if(rval != TPM_RC_SUCCESS) { printf("\n......ActivateCredential failed. TPM Error:0x%x......\n", rval); return -1; } printf("\nActivate Credential succ.\n"); printf("\nCertInfoData :\n"); for (int k = 0; k<certInfoData.t.size; k++) { printf("0x%.2x ", certInfoData.t.buffer[k]); } printf("\n\n"); if(saveDataToFile(outFilePath, certInfoData.t.buffer, certInfoData.t.size) == 0) printf("OutFile %s completed!\n",outFilePath); else return -2; return 0; }
int createEKHandle() { UINT32 rval; TPMS_AUTH_COMMAND sessionData; TPMS_AUTH_RESPONSE sessionDataOut; TSS2_SYS_CMD_AUTHS sessionsData; TSS2_SYS_RSP_AUTHS sessionsDataOut; TPMS_AUTH_COMMAND *sessionDataArray[1]; TPMS_AUTH_RESPONSE *sessionDataOutArray[1]; TPM2B_SENSITIVE_CREATE inSensitive = { { sizeof(TPM2B_SENSITIVE_CREATE) - 2, } }; TPM2B_PUBLIC inPublic = { { sizeof(TPM2B_PUBLIC) - 2, } }; TPM2B_DATA outsideInfo = { { 0, } }; TPML_PCR_SELECTION creationPCR; TPM2B_NAME name = { { sizeof(TPM2B_NAME) - 2, } }; TPM2B_PUBLIC outPublic = { { 0, } }; TPM2B_CREATION_DATA creationData = { { 0, } }; TPM2B_DIGEST creationHash = { { sizeof(TPM2B_DIGEST) - 2, } }; TPMT_TK_CREATION creationTicket = { 0, }; TPM_HANDLE handle2048ek; sessionDataArray[0] = &sessionData; sessionDataOutArray[0] = &sessionDataOut; sessionsDataOut.rspAuths = &sessionDataOutArray[0]; sessionsData.cmdAuths = &sessionDataArray[0]; sessionsDataOut.rspAuthsCount = 1; sessionsData.cmdAuthsCount = 1; sessionData.sessionHandle = TPM_RS_PW; sessionData.nonce.t.size = 0; sessionData.hmac.t.size = 0; *((UINT8 *)((void *)&sessionData.sessionAttributes)) = 0; // use enAuth in Tss2_Sys_CreatePrimary if (strlen(endorsePasswd) > 0 && !hexPasswd) { sessionData.hmac.t.size = strlen(endorsePasswd); memcpy( &sessionData.hmac.t.buffer[0], endorsePasswd, sessionData.hmac.t.size ); } else if (strlen(endorsePasswd) > 0 && hexPasswd) { sessionData.hmac.t.size = sizeof(sessionData.hmac) - 2; if (hex2ByteStructure(endorsePasswd, &sessionData.hmac.t.size, sessionData.hmac.t.buffer) != 0) { printf( "Failed to convert Hex format password for endorsePasswd.\n"); return -1; } } if (strlen(ekPasswd) > 0 && !hexPasswd) { sessionData.hmac.t.size = strlen(ekPasswd); memcpy( &sessionData.hmac.t.buffer[0], ekPasswd, sessionData.hmac.t.size ); } else if (strlen(ekPasswd) > 0 && hexPasswd) { inSensitive.t.sensitive.userAuth.t.size = sizeof(inSensitive.t.sensitive.userAuth) - 2; if (hex2ByteStructure(ekPasswd, &inSensitive.t.sensitive.userAuth.t.size, inSensitive.t.sensitive.userAuth.t.buffer) != 0) { printf( "Failed to convert Hex format password for ekPasswd.\n"); return -1; } } inSensitive.t.sensitive.data.t.size = 0; inSensitive.t.size = inSensitive.t.sensitive.userAuth.b.size + 2; if ( setKeyAlgorithm(algorithmType, inSensitive, inPublic) ) return -1; creationPCR.count = 0; /*To Create EK*/ rval = Tss2_Sys_CreatePrimary(sysContext, TPM_RH_ENDORSEMENT, &sessionsData, &inSensitive, &inPublic, &outsideInfo, &creationPCR, &handle2048ek, &outPublic, &creationData, &creationHash, &creationTicket, &name, &sessionsDataOut); if ( rval != TPM_RC_SUCCESS ) { printf("\n......TPM2_CreatePrimary Error. TPM Error:0x%x......\n", rval); return -2; } printf("\nEK create succ.. Handle: 0x%8.8x\n", handle2048ek); if (!nonPersistentRead) { // To make EK persistent, use own auth sessionData.hmac.t.size = 0; if (strlen(ownerPasswd) > 0 && !hexPasswd) { sessionData.hmac.t.size = strlen(ownerPasswd); memcpy( &sessionData.hmac.t.buffer[0], ownerPasswd, sessionData.hmac.t.size ); } else if (strlen(ownerPasswd) > 0 && hexPasswd) { sessionData.hmac.t.size = sizeof(sessionData.hmac) - 2; if (hex2ByteStructure(ownerPasswd, &sessionData.hmac.t.size, sessionData.hmac.t.buffer) != 0) { printf( "Failed to convert Hex format password for ownerPasswd.\n"); return -1; } } rval = Tss2_Sys_EvictControl(sysContext, TPM_RH_OWNER, handle2048ek, &sessionsData, persistentHandle, &sessionsDataOut); if ( rval != TPM_RC_SUCCESS ) { printf("\n......EvictControl:Make EK persistent Error. TPM Error:0x%x......\n", rval); return -3; } printf("EvictControl EK persistent succ.\n"); } rval = Tss2_Sys_FlushContext(sysContext, handle2048ek); if ( rval != TPM_RC_SUCCESS ) { printf("\n......Flush transient EK failed. TPM Error:0x%x......\n", rval); return -4; } printf("Flush transient EK succ.\n"); // save ek public if ( saveDataToFile(outputFile, (UINT8 *)&outPublic, sizeof(outPublic)) ) { printf("\n......Failed to save EK pub key into file(%s)......\n", outputFile); return -5; } return 0; }
int create(TPMI_DH_OBJECT parentHandle, TPM2B_PUBLIC *inPublic, TPM2B_SENSITIVE_CREATE *inSensitive, TPMI_ALG_PUBLIC type, TPMI_ALG_HASH nameAlg, const char *opuFilePath, const char *oprFilePath, int o_flag, int O_flag, int I_flag, int A_flag, UINT32 objectAttributes) { TPM_RC rval; TPMS_AUTH_RESPONSE sessionDataOut; TSS2_SYS_CMD_AUTHS sessionsData; TSS2_SYS_RSP_AUTHS sessionsDataOut; TPMS_AUTH_COMMAND *sessionDataArray[1]; TPMS_AUTH_RESPONSE *sessionDataOutArray[1]; TPM2B_DATA outsideInfo = { { 0, } }; TPML_PCR_SELECTION creationPCR; TPM2B_PUBLIC outPublic = { { 0, } }; TPM2B_PRIVATE outPrivate = { { sizeof(TPM2B_PRIVATE)-2, } }; TPM2B_CREATION_DATA creationData = { { 0, } }; TPM2B_DIGEST creationHash = { { sizeof(TPM2B_DIGEST)-2, } }; TPMT_TK_CREATION creationTicket = { 0, }; sessionDataArray[0] = &sessionData; sessionDataOutArray[0] = &sessionDataOut; sessionsDataOut.rspAuths = &sessionDataOutArray[0]; sessionsData.cmdAuths = &sessionDataArray[0]; sessionsDataOut.rspAuthsCount = 1; sessionData.sessionHandle = TPM_RS_PW; sessionData.nonce.t.size = 0; sessionsData.cmdAuthsCount = 1; sessionsData.cmdAuths[0] = &sessionData; inSensitive->t.size = inSensitive->t.sensitive.userAuth.b.size + 2; if(setAlg(type, nameAlg, inPublic, I_flag)) return -1; if(A_flag == 1) inPublic->t.publicArea.objectAttributes.val = objectAttributes; printf("ObjectAttribute: 0x%08X\n",inPublic->t.publicArea.objectAttributes.val); creationPCR.count = 0; rval = Tss2_Sys_Create(sysContext, parentHandle, &sessionsData, inSensitive, inPublic, &outsideInfo, &creationPCR, &outPrivate,&outPublic,&creationData, &creationHash, &creationTicket, &sessionsDataOut); if(rval != TPM_RC_SUCCESS) { printf("\nCreate Object Failed ! ErrorCode: 0x%0x\n\n",rval); return -2; } printf("\nCreate Object Succeed !\n"); if(o_flag == 1) { if(saveDataToFile(opuFilePath, (UINT8 *)&outPublic, sizeof(outPublic))) return -3; } if(O_flag == 1) { if(saveDataToFile(oprFilePath, (UINT8 *)&outPrivate, sizeof(outPrivate))) return -4; } return 0; }
void MainWindow::on_actionSave_trackingdata_triggered() { saveDataToFile(); }
int activateCredential() { UINT32 rval; TPM2B_DIGEST certInfoData = { { sizeof(certInfoData)-2, } }; printf("\nACTIVATE CREDENTIAL TESTS:\n"); cmdAuth.sessionHandle = TPM_RS_PW; cmdAuth2.sessionHandle = TPM_RS_PW; *((UINT8 *)((void *)&cmdAuth.sessionAttributes)) = 0; *((UINT8 *)((void *)&cmdAuth2.sessionAttributes)) = 0; *((UINT8 *)((void *)&cmdAuth3.sessionAttributes)) = 0; TPMS_AUTH_COMMAND *cmdSessionArray[2] = { &cmdAuth, &cmdAuth3 }; TSS2_SYS_CMD_AUTHS cmdAuthArray = { 2, &cmdSessionArray[0] }; TPMS_AUTH_COMMAND *cmdSessionArray1[1] = { &cmdAuth2 }; TSS2_SYS_CMD_AUTHS cmdAuthArray1 = { 1, &cmdSessionArray1[0] }; SESSION *session; TPM2B_ENCRYPTED_SECRET encryptedSalt = { { 0, } }; TPM2B_NONCE nonceCaller = { { 0, } }; TPMT_SYM_DEF symmetric; symmetric.algorithm = TPM_ALG_NULL; if (cmdAuth.hmac.t.size > 0 && hexPasswd) { cmdAuth.hmac.t.size = sizeof(cmdAuth.hmac) - 2; if (hex2ByteStructure((char *)cmdAuth.hmac.t.buffer, &cmdAuth.hmac.t.size, cmdAuth.hmac.t.buffer) != 0) { printf( "Failed to convert Hex format password for handlePasswd.\n"); return -1; } } if (cmdAuth2.hmac.t.size > 0 && hexPasswd) { cmdAuth2.hmac.t.size = sizeof(cmdAuth2.hmac) - 2; if (hex2ByteStructure((char *)cmdAuth2.hmac.t.buffer, &cmdAuth2.hmac.t.size, cmdAuth2.hmac.t.buffer) != 0) { printf( "Failed to convert Hex format password for endorsePasswd.\n"); return -1; } } rval = StartAuthSessionWithParams( &session, TPM_RH_NULL, 0, TPM_RH_NULL, 0, &nonceCaller, &encryptedSalt, TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256 ); if( rval != TPM_RC_SUCCESS ) { printf("\n......StartAuthSessionWithParams Error. TPM Error:0x%x......\n", rval); return -1; } printf("\nStartAuthSessionWithParams succ.......\n"); rval = Tss2_Sys_PolicySecret(sysContext, TPM_RH_ENDORSEMENT, session->sessionHandle, &cmdAuthArray1, 0, 0, 0, 0, 0, 0, 0); if( rval != TPM_RC_SUCCESS ) { printf("\n......Tss2_Sys_PolicySecret Error. TPM Error:0x%x......\n", rval); return -2; } printf("\nTss2_Sys_PolicySecret succ.......\n"); cmdAuth3.sessionHandle = session->sessionHandle; cmdAuth3.sessionAttributes.continueSession = 1; cmdAuth3.hmac.t.size = 0; rval = Tss2_Sys_ActivateCredential(sysContext, activateHandle, keyHandle, &cmdAuthArray, &credentialBlob, &secret, &certInfoData, 0); if(rval != TPM_RC_SUCCESS) { printf("\n......ActivateCredential failed. TPM Error:0x%x......\n", rval); return -3; } printf("\nActivate Credential succ.\n"); // Need to flush the session here. rval = Tss2_Sys_FlushContext( sysContext, session->sessionHandle ); if( rval != TPM_RC_SUCCESS ) { printf("\n......TPM2_Sys_FlushContext Error. TPM Error:0x%x......\n", rval); return -4; } // And remove the session from sessions table. rval = EndAuthSession( session ); if( rval != TPM_RC_SUCCESS ) { printf("\n......EndAuthSession Error. TPM Error:0x%x......\n", rval); return -5; } printf("\nCertInfoData :\n"); for (int k = 0; k<certInfoData.t.size; k++) { printf("0x%.2x ", certInfoData.t.buffer[k]); } printf("\n\n"); if(saveDataToFile(outFilePath, certInfoData.t.buffer, certInfoData.t.size) == 0) printf("OutFile %s completed!\n",outFilePath); else return -6; return 0; }
bool AnalysisDao::saveAnalysis(AnalysisDao::AnalysisInfo* info) { if (!m_db.isOpen() || info == NULL) { return false; } if (!info->trackId.isValid()) { qDebug() << "Can't save analysis since trackId is invalid."; return false; } PerformanceTimer time; time.start(); QByteArray compressedData = qCompress(info->data, kCompressionLevel); int checksum = qChecksum(compressedData.constData(), compressedData.length()); QSqlQuery query(m_db); if (info->analysisId == -1) { query.prepare(QString( "INSERT INTO %1 (track_id, type, description, version, data_checksum) " "VALUES (:trackId,:type,:description,:version,:data_checksum)") .arg(s_analysisTableName)); QByteArray waveformBytes; query.bindValue(":trackId", info->trackId.toVariant()); query.bindValue(":type", info->type); query.bindValue(":description", info->description); query.bindValue(":version", info->version); query.bindValue(":data_checksum", checksum); if (!query.exec()) { LOG_FAILED_QUERY(query) << "couldn't save new analysis"; return false; } info->analysisId = query.lastInsertId().toInt(); } else { query.prepare(QString( "UPDATE %1 SET " "track_id = :trackId," "type = :type," "description = :description," "version = :version," "data_checksum = :data_checksum " "WHERE id = :analysisId").arg(s_analysisTableName)); query.bindValue(":analysisId", info->analysisId); query.bindValue(":trackId", info->trackId.toVariant()); query.bindValue(":type", info->type); query.bindValue(":description", info->description); query.bindValue(":version", info->version); query.bindValue(":data_checksum", checksum); if (!query.exec()) { LOG_FAILED_QUERY(query) << "couldn't update existing analysis"; return false; } } QString dataPath = getAnalysisStoragePath().absoluteFilePath( QString::number(info->analysisId)); if (!saveDataToFile(dataPath, compressedData)) { qDebug() << "WARNING: Couldn't save analysis data to file" << dataPath; return false; } qDebug() << "AnalysisDAO saved analysis" << info->analysisId << QString("%1 (%2 compressed)").arg(QString::number(info->data.length()), QString::number(compressedData.length())) << "bytes for track" << info->trackId << "in" << time.elapsed().debugMillisWithUnit(); return true; }
KLUPD::CoreError KLUPD::AdministrationKitProtocol::getFile( const Path &fileName, const Path &localPath, const Path &relativeUrlPathIn, const bool useMasterAdministrationServer) { #ifdef DISABLE_AK_FILETRANSFER TRACE_MESSAGE("Administration Kit Transport is not implemented"); return CORE_DOWNLOAD_ERROR; #else // skip initial slash in relative URL path Path relativeUrlPath = relativeUrlPathIn; if(!relativeUrlPath.empty() && (relativeUrlPath[0] == L'\\' || relativeUrlPath[0] == L'/')) { relativeUrlPath = relativeUrlPath.toWideChar() + 1; } const Path path = relativeUrlPath + fileName; /////////////////////////////////////////////// /// checking current state and parameters const CoreError connectionResult = setupLowLevelConnectionIfNeed(useMasterAdministrationServer); if(connectionResult != CORE_NO_ERROR) { TRACE_MESSAGE2("Failed to setup connection to Administration Server, result '%S'", toString(connectionResult).toWideChar()); return connectionResult; } /////////////////////////////////////////////// /// receiving data by portions size_t regettingPosition = LocalFile(localPath + fileName).size(); AutoStream destinationFile(pLog); while(true) { // check if request should be cancelled if(m_downloadProgress.checkCancel()) { TRACE_MESSAGE2("File transfer cancelled '%S'", path.toWideChar()); return CORE_CANCELLED; } const long localBufferSize = 65536; std::vector<unsigned char> localBuffer(localBufferSize, 0); int bytesRead = 0; m_downloadProgress.updateSpeedStartTimer(); const KLFT::FileOpeartionResult getChunkResult = m_adminKitTransprot->GetFileChunk( path.toWideChar(), regettingPosition, &localBuffer[0], localBuffer.size(), bytesRead); m_downloadProgress.updateSpeedStopTimer(); // a portion of file got successfully if(getChunkResult == KLFT::FR_Ok || getChunkResult == KLFT::FR_OkDownloaded) { const CoreError saveDataToFileResult = saveDataToFile( localPath + fileName, &localBuffer[0], bytesRead, regettingPosition != 0, destinationFile, pLog); if(!isSuccess(saveDataToFileResult)) { TRACE_MESSAGE3("Failed to write data obtained from Administration Server to file '%S', result '%S'", path.toWideChar(), toString(saveDataToFileResult).toWideChar()); return saveDataToFileResult; } regettingPosition += bytesRead; m_downloadProgress.bytesTransferred(bytesRead); // file has been completely downloaded if(getChunkResult == KLFT::FR_OkDownloaded) return CORE_NO_ERROR; continue; } TRACE_MESSAGE5("Failed to obtain file chunk from Administration Server, file '%S', result '%s', bytes read %d, current position %d", path.toWideChar(), toString(getChunkResult).c_str(), bytesRead, regettingPosition); if(getChunkResult == KLFT::FR_Timeout) { // TODO check if time out is over and error should be returned continue; } // error while download happened switch(getChunkResult) { // invalid AdminKit Transport identifier: either receiver was deleted or connect was already called for this receiver case KLFT::FR_WrongReceiverId: return CORE_AK_WrongReceiverId; // invalid arguments for AdminKit Transport // wrong offset is requested, consider file is downloaded and signature should be checked case KLFT::FR_WrongArguments: { // TODO: this is *work around* about the problem that AdminKit returns return CORE_NO_ERROR; // FR_WrongArguments code in case source contains // valid file in target download folder // this code can be deleted when problem is fixed in AdminKit } // file not found on AdminKit server case KLFT::FR_FileNotFound: return CORE_NO_SOURCE_FILE; // AdminKit transport file receive error case KLFT::FR_ErrorInOperation: return CORE_AK_ErrorInOperation; // unknown AdminKit transport receive operation error default: return CORE_AK_UnknownError; } } #endif // DISABLE_AK_FILETRANSFER }