ae_error_t CertificateProvisioningProtocol::aesCMAC(const upse::Buffer& key, const upse::Buffer& message, upse::Buffer& cmac) { ae_error_t status = AE_FAILURE; do { if (key.getSize() != sizeof(sgx_aes_gcm_128bit_key_t)) break; status = cmac.Alloc(sizeof(sgx_cmac_128bit_tag_t)); if (AE_FAILED(status)) break; uint8_t* pCMAC; status = upse::BufferWriter(cmac).reserve(cmac.getSize(), &pCMAC); if (AE_FAILED(status)) break; sgx_status_t sgx_status; sgx_status = sgx_rijndael128_cmac_msg(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(key.getData()), message.getData(), message.getSize(), reinterpret_cast<sgx_cmac_128bit_tag_t *>(pCMAC)); if (SGX_SUCCESS != sgx_status) { status = AE_FAILURE; break; } status = AE_SUCCESS; } while (0); return status; }
ae_error_t CertificateProvisioningProtocol::msg3_create_header(const upse::Buffer& transactionID, uint32_t nonceSize, uint32_t quoteSize, uint32_t epidSigSize, uint32_t csrSize, provision_request_header_t& header) { ae_error_t status = AESM_PSE_PR_INTERNAL_ERROR; do { uint32_t seq2_0_tlv_block_cipher_text_size = BLOCK_CIPHER_TEXT_TLV_SIZE(quoteSize + epidSigSize + csrSize); uint32_t seq3_0_tlv_nonce_size = nonceSize; uint32_t seq4_0_tlv_mac_size = MAC_TLV_SIZE(MAC_SIZE); header.protocol = PSE_PROVISIONING; header.version = TLV_VERSION_1; header.type = static_cast<uint8_t>(TYPE_PSE_MSG3); if (XID_SIZE != transactionID.getSize()) break; if (memcpy_s(header.xid, sizeof(header.xid), transactionID.getData(), transactionID.getSize()) != 0) break; uint32_t totalSize = seq2_0_tlv_block_cipher_text_size + seq3_0_tlv_nonce_size + seq4_0_tlv_mac_size; uint32_t serializedSize = _htonl(totalSize); se_static_assert(sizeof(serializedSize) == sizeof(header.size)); if (memcpy_s(header.size, sizeof(header.size), &serializedSize, sizeof(serializedSize)) != 0) break; status = AE_SUCCESS; } while (0); return status; }
ae_error_t CertificateProvisioningProtocol::msg3_seq3_2_create_quote_signature_tlv(const upse::Buffer& quote, TLVsMsg& seq3_2_tlv_quote_signature) { ae_error_t status = AESM_PSE_PR_INTERNAL_ERROR; tlv_status_t tlv_status; do { if (sizeof(sgx_quote_t) > quote.getSize()) break; const sgx_quote_t* pQuote = (const sgx_quote_t*)quote.getData(); /* the QUOTE SIGNATURE TLV doesn't include Quote.signature_len */ uint32_t SigLen = pQuote->signature_len; if (sizeof(sgx_quote_t) + SigLen > quote.getSize()) break; const uint8_t *pSig = pQuote->signature; tlv_status = seq3_2_tlv_quote_signature.add_quote_signature(pSig, SigLen); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; status = AE_SUCCESS; } while (0); return status; }
ae_error_t CertificateProvisioningProtocol::aesGCMDecrypt(const upse::Buffer& iv, const upse::Buffer& key, const upse::Buffer& cipherText, const upse::Buffer& aad, const upse::Buffer& mac, upse::Buffer& plainText) { ae_error_t status = AE_FAILURE; do { if (key.getSize() != sizeof(sgx_aes_gcm_128bit_key_t)) break; status = plainText.Alloc(cipherText.getSize()); if (AE_FAILED(status)) break; uint8_t* pPlainText = NULL; status = upse::BufferWriter(plainText).reserve(plainText.getSize(), &pPlainText); if (AE_FAILED(status)) break; sgx_status_t sgx_status; sgx_status = sgx_rijndael128GCM_decrypt(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(key.getData()), cipherText.getData(), cipherText.getSize(), pPlainText, iv.getData(), IV_SIZE, aad.getData(), aad.getSize(), reinterpret_cast<const sgx_aes_gcm_128bit_tag_t *>(mac.getData())); if (SGX_SUCCESS != sgx_status) { AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_PSE_CERT_PROV_INTEGRITY_ERROR]); status = AE_FAILURE; break; } status = AE_SUCCESS; } while (0); return status; }
ae_error_t CertificateProvisioningProtocol::sendReceive(const upse::Buffer& sendSerialized, upse::Buffer& recvSerialized) { ae_error_t status = AE_FAILURE; uint8_t* recv = NULL; uint32_t recv_size = 0; do { AESM_DBG_INFO("start send msg"); status = AESMNetworkEncoding::aesm_send_recv_msg_encoding(m_url.c_str(), const_cast<uint8_t *>(sendSerialized.getData()), sendSerialized.getSize(), recv, recv_size); if (AE_FAILED(status)) break; AESM_DBG_INFO("msg received with size %u", recv_size); status = recvSerialized.Alloc(recv_size); if (AE_FAILED(status)) break; AESM_DBG_INFO("buffer alloced"); status = upse::BufferWriter(recvSerialized).writeRaw(recv, recv_size); if (AE_FAILED(status)) break; AESM_DBG_INFO("buffer written"); status = AE_SUCCESS; } while (0); if (NULL != recv) AESMNetworkEncoding::aesm_free_response_msg(recv); return status; }
ae_error_t CertificateProvisioningProtocol::encryptRSA_OAEP_SHA256(const public_key_t& publicKey, upse::BufferReader& plainTextReader, upse::Buffer& cipherText) { ae_error_t status = AE_FAILURE; void* rsa_pub_key = NULL; size_t dst_len = 0; do { if(SGX_SUCCESS != get_intel_rsa_pub_key(publicKey, &rsa_pub_key)) break; int plainTextSize = plainTextReader.getRemainingSize(); const uint8_t* pPlainText = NULL; if (AE_FAILED(plainTextReader.readRaw(&pPlainText))) break; int cipherTextSize = get_intel_pek_cipher_text_size(); if (AE_FAILED(cipherText.Alloc(cipherTextSize))) break; upse::BufferWriter cipherTextWriter(cipherText); uint8_t* pCipherText; if (AE_FAILED(cipherTextWriter.reserve(cipherText.getSize(), &pCipherText))) break; // Check the encrypt output length sgx_status_t res = sgx_rsa_pub_encrypt_sha256(rsa_pub_key, NULL, &dst_len, pPlainText, plainTextSize); if(res != SGX_SUCCESS || dst_len != cipherText.getSize()) break; res = sgx_rsa_pub_encrypt_sha256(rsa_pub_key, pCipherText, &dst_len, pPlainText, plainTextSize); if(res != SGX_SUCCESS) break; status = AE_SUCCESS; } while (0); free_intel_rsa_pub_key(rsa_pub_key); return status; }
ae_error_t CertificateProvisioningProtocol::msg3_seq3_1_create_quote_tlv(const upse::Buffer& quoteBuffer, TLVsMsg& quoteTLV) { ae_error_t status = AESM_PSE_PR_INTERNAL_ERROR; tlv_status_t tlv_status; do { if (sizeof(sgx_quote_t) > quoteBuffer.getSize()) break; const sgx_quote_t* pQuote = (const sgx_quote_t*)quoteBuffer.getData(); tlv_status = quoteTLV.add_quote((uint8_t*)pQuote, static_cast<uint32_t>(sizeof(sgx_quote_t)-sizeof(pQuote->signature_len))); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; } while (0); return status; }
ae_error_t CertificateProvisioningProtocol::aesGCMEncrypt(const upse::Buffer& iv, const upse::Buffer& key, const upse::Buffer& plainText, const upse::Buffer& aad, upse::Buffer& encryptedText, upse::Buffer& mac) { ae_error_t status = AE_FAILURE; do { if (key.getSize() != sizeof(sgx_aes_gcm_128bit_key_t)) break; status = encryptedText.Alloc(plainText.getSize()); if (AE_FAILED(status)) break; uint8_t* pEncryptedText; status = upse::BufferWriter(encryptedText).reserve(encryptedText.getSize(), &pEncryptedText); if (AE_FAILED(status)) break; status = mac.Alloc(sizeof(sgx_aes_gcm_128bit_tag_t)); if (AE_FAILED(status)) break; uint8_t* pMAC; status = upse::BufferWriter(mac).reserve(mac.getSize(), &pMAC); if (AE_FAILED(status)) break; sgx_status_t sgx_status; sgx_status = sgx_rijndael128GCM_encrypt(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(key.getData()), plainText.getData(), plainText.getSize(), pEncryptedText, iv.getData(), IV_SIZE, aad.getData(), aad.getSize(), reinterpret_cast<sgx_aes_gcm_128bit_tag_t *>(pMAC)); if (SGX_SUCCESS != sgx_status) { status = AE_FAILURE; break; } status = AE_SUCCESS; } while (0); return status; }
ae_error_t upsePersistentStorage::Write(aesm_data_id_t data_id, upse::Buffer& data) { ae_error_t status = AESM_PSE_PR_PERSISTENT_STORAGE_WRITE_ERROR; do { if (AE_FAILED(aesm_write_data(FT_PERSISTENT_STORAGE, data_id, data.getData(), data.getSize()))) break; status = AE_SUCCESS; } while (0); return status; }
ae_error_t CertificateProvisioningProtocol::get_random_value(uint32_t size, upse::Buffer& randomValue) { ae_error_t status = AE_FAILURE; do { status = randomValue.Alloc(size); if (AE_FAILED(status)) break; uint8_t* p; upse::BufferWriter bw(randomValue); status = bw.reserve(size, &p); if (AE_FAILED(status)) break; status = aesm_read_rand(p, size); } while (0); return status; }
ae_error_t upsePersistentStorage::Read(aesm_data_id_t data_id, upse::Buffer& data) { ae_error_t status = AESM_PSE_PR_PERSISTENT_STORAGE_READ_ERROR; uint8_t* tempData = NULL; do { ae_error_t readError; uint32_t sizeInout; readError = aesm_query_data_size(FT_PERSISTENT_STORAGE, data_id, &sizeInout); if ((readError != AE_SUCCESS) || (sizeInout == 0)) { break; } tempData = (uint8_t*) malloc(sizeInout); if (tempData == NULL) { break; } readError = aesm_read_data(FT_PERSISTENT_STORAGE, data_id, tempData, &sizeInout); if (AE_SUCCESS != readError) { break; } data.Alloc(sizeInout); upse::BufferWriter bw(data); bw.writeRaw(tempData, sizeInout); status = AE_SUCCESS; } while (0); if (NULL != tempData) free(tempData); return status; }
//********************************************************************* // Call quoting enclave to convert report to name-based quote //********************************************************************* static ae_error_t do_get_quote ( /*in */ upse::Buffer& reportBuffer, /*in */ upse::Buffer& sigRLBuffer, /*out*/ upse::Buffer& quoteBuffer ) { // Call QE to convert REPORT to a name-based QUOTE ae_error_t status = AE_FAILURE; ae_error_t tmp_status = AE_SUCCESS; do { #ifndef FAKE_QUOTE uint32_t nQuote; // in - Quote buffer size sgx_report_t enclaveReport; // in sgx_quote_sign_type_t quote_type; // in sgx_spid_t spid = {{0}}; // in uint8_t* pSigRL = NULL; // in uint32_t nSigRL = 0; // in - Sig RL buffer size memset(&enclaveReport, 0, sizeof(enclaveReport)); nSigRL = sigRLBuffer.getSize(); if (0 != nSigRL) pSigRL = const_cast<uint8_t*>(sigRLBuffer.getData()); if (SGX_SUCCESS != sgx_calc_quote_size(pSigRL, nSigRL, &nQuote)) break; tmp_status = quoteBuffer.Alloc(nQuote); if (AE_FAILED(tmp_status)) break; upse::BufferWriter bwQuote(quoteBuffer); uint8_t* pQuote; tmp_status = bwQuote.reserve(nQuote, &pQuote); // out if (AE_FAILED(tmp_status)) break; quote_type = SGX_UNLINKABLE_SIGNATURE; // or SGX_LINKABLE_SIGNATURE // LSB16(SHA256("SGX PSE PROVISIONING SERVER")) // const char* SPID_VALUE = "SGX PSE PROVISIONING SERVER"; // sgx_sha256_hash_t spid_hash; // sgx_sha256_msg((const uint8_t*)SPID_VALUE, strlen(SPID_VALUE), &spid_hash); // memcpy_s(spid.id, sizeof(spid.id), &spid_hash[0], 16); static uint8_t spid_hash[] = { 0x32, 0x81, 0xE5, 0x9E, 0xB1, 0x23, 0xFA, 0xCD, 0x56, 0xDB, 0x62, 0x1E, 0x3B, 0x37, 0xFB, 0xE2 }; memcpy_s(spid.id, sizeof(spid.id), spid_hash, sizeof(spid_hash)); if (reportBuffer.getSize() != sizeof(enclaveReport)) break; memcpy_s(&enclaveReport, reportBuffer.getSize(), reportBuffer.getData(), reportBuffer.getSize()); aesm_error_t result; result = AESMLogic::get_quote( (uint8_t*)&enclaveReport, sizeof(enclaveReport), quote_type, (uint8_t*)&spid, sizeof(spid), NULL, 0, pSigRL, nSigRL, NULL, 0, (uint8_t*)pQuote, nQuote); if (result == AESM_BUSY) { //EPID_PROVISION triggered, make sure previous EPID provision has finished ae_error_t temp_ret = wait_pve_thread(); BREAK_IF_TRUE(AE_SUCCESS != temp_ret , status, PSE_PR_PCH_EPID_UNKNOWN_ERROR); //redo get_quote result = AESMLogic::get_quote( (uint8_t*)&enclaveReport, sizeof(enclaveReport), quote_type, (uint8_t*)&spid, sizeof(spid), NULL, 0, pSigRL, nSigRL, NULL, 0, (uint8_t*)pQuote, nQuote); } BREAK_IF_TRUE(AESM_OUT_OF_EPC == result, status, AESM_AE_OUT_OF_EPC); BREAK_IF_TRUE(AESM_SUCCESS != result, status, AESM_PSE_PR_GET_QUOTE_ERROR); #else const uint16_t SIGNATURE_LENGTH = 32; tmp_status = quoteBuffer.Alloc(sizeof(sgx_quote_t) + SIGNATURE_LENGTH); if (AE_FAILED(tmp_status)) break; sgx_quote_t* pQuote; tmp_status = upse::BufferWriter(quoteBuffer).reserve(quoteBuffer.getSize(), (uint8_t**)&pQuote); if (AE_FAILED(tmp_status)) break; uint16_t CPUSVN = 1; pQuote->version = 1; memcpy_s(pQuote->epid_group_id, sizeof(pQuote->epid_group_id), &GID_TO_USE, sizeof(GID_TO_USE)); pQuote->report_body.isv_prod_id = 0x0002; //0x8086; pQuote->report_body.isv_svn = 1; memcpy_s(pQuote->report_body.cpu_svn, sizeof(pQuote->report_body.cpu_svn), &CPUSVN, sizeof(CPUSVN)); const sgx_report_t* pReport = (sgx_report_t*)reportBuffer.getData(); memcpy_s(pQuote->report_body.report_data, sizeof(pQuote->report_body.report_data), pReport->body.report_data, sizeof(pQuote->report_body.report_data)); pQuote->signature_len = SIGNATURE_LENGTH; //NOTE: The signature is not valid when doing a FAKE_QUOTE #endif status = AE_SUCCESS; } while (0); if ((AE_FAILURE == status) && AE_FAILED(tmp_status)) status = tmp_status; SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status); return status; }
//********************************************************************* // Call quoting enclave to get target info //********************************************************************* static ae_error_t do_quote_initialization ( /*out */ upse::Buffer& targetInfo, /*out */ GroupId* pGID ) { ae_error_t status = AE_FAILURE; do { BREAK_IF_TRUE( (NULL == pGID), status, PSE_PR_BAD_POINTER_ERROR); #ifndef FAKE_QUOTE if (AE_FAILED(targetInfo.Alloc(sizeof(sgx_target_info_t)))) break; upse::BufferWriter bwTargetInfo(targetInfo); uint8_t* p; status = bwTargetInfo.reserve(sizeof(sgx_target_info_t), &p); if (AE_FAILED(status)) break; sgx_target_info_t* pTargetInfo = (sgx_target_info_t*)p; aesm_error_t result; SGX_DBGPRINT_PRINT_ANSI_STRING("aesmLogic.init_quote?"); result = AESMLogic::init_quote( (uint8_t*)pTargetInfo, sizeof(sgx_target_info_t), (uint8_t*)pGID, sizeof(*pGID)); if (result == AESM_BUSY) { //EPID_PROVISION triggered, make sure previous EPID provision has finished ae_error_t temp_ret = wait_pve_thread(); BREAK_IF_TRUE(AE_SUCCESS != temp_ret , status, PSE_PR_PCH_EPID_UNKNOWN_ERROR); //redo init_quote result = AESMLogic::init_quote( (uint8_t*)pTargetInfo, sizeof(sgx_target_info_t), (uint8_t*)pGID, sizeof(*pGID)); } BREAK_IF_TRUE(AESM_UPDATE_AVAILABLE == result, status, PSW_UPDATE_REQUIRED); BREAK_IF_TRUE(AESM_OUT_OF_EPC == result, status, AESM_AE_OUT_OF_EPC); BREAK_IF_TRUE(AESM_SUCCESS != result, status, AESM_PSE_PR_INIT_QUOTE_ERROR); #else //NRG: m_tmpGID = 0; upse::Buffer m_tmpGID; if (AE_FAILED(m_tmpGID.Alloc(GID_TO_USE, sizeof(GID_TO_USE)))) break; // m_tmpGID = 1244; // upse::BufferWriter(m_tmpGID).writeRaw(GID_TO_USE, sizeof(GID_TO_USE)); SigmaData::SetGID(m_tmpGID); memcpy_s(pGID, sizeof(GroupId), m_tmpGID.getData(), sizeof(GroupId)); if (AE_FAILED(targetInfo.Alloc(sizeof(sgx_target_info_t)))) break; #endif SGX_DBGPRINT_PRINT_ANSI_STRING("aesmLogic.init_quote success"); status = AE_SUCCESS; } while (0); SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status); return status; }
ae_error_t Helper::PrepareCertificateChainVLR( /*in*/ std::list<upse::Buffer>& certChain, /*out*/ upse::Buffer& certChainVLR) { ae_error_t status = AESM_PSE_PR_LOAD_VERIFIER_CERT_ERROR; try { do { int nPaddedBytes = 0; int nCertChain = 0; #if !defined(LEAFTOROOT) #error LEAFTOROOT not #defined #endif // // spec'd behavior is to receive certs in leaft to root order // then, it only makes sense to store them leaf to root // but sigma wants them root to leaf // we'll leave the #if here since, cumulatively, it shows how to traverse // in both directions // #if !LEAFTOROOT SGX_DBGPRINT_PRINT_STRING_LTP("leaf cert to root cert direction, padding"); std::list<upse::Buffer>::reverse_iterator it; for (it = certChain.rbegin(); it != certChain.rend(); ++it) { int nSize = (*it).getSize(); nPaddedBytes += REQUIRED_PADDING_DWORD_ALIGNMENT(nSize); nCertChain += nSize; } #else SGX_DBGPRINT_PRINT_STRING_LTP("root cert to leaf cert direction, padding"); std::list<upse::Buffer>::iterator it; for (it = certChain.begin(); it != certChain.end(); ++it) { int nSize = (*it).getSize(); nPaddedBytes += REQUIRED_PADDING_DWORD_ALIGNMENT(nSize); nCertChain += nSize; } #endif SGX_DBGPRINT_PRINT_STRING_LTP("less cert padding"); //NRG: This doesn't work, but should. It should replace the previous nPaddedBytes = REQUIRED_PADDING_DWORD_ALIGNMENT(nCertChain); if(UINT16_MAX - ((int)sizeof(SIGMA_VLR_HEADER) + nPaddedBytes) < nCertChain){ break; } int nLength = static_cast<int>(sizeof(SIGMA_VLR_HEADER)) + nPaddedBytes + nCertChain; certChainVLR.Alloc(nLength); upse::BufferWriter bw(certChainVLR); VERIFIER_CERT_CHAIN_VLR* pVLR; uint8_t* p; if (AE_FAILED(bw.reserve(nLength, &p))) break; pVLR = (VERIFIER_CERT_CHAIN_VLR*)p; pVLR->VlrHeader.ID = VERIFIER_CERTIFICATE_CHAIN_VLR_ID; pVLR->VlrHeader.PaddedBytes = (UINT8)nPaddedBytes; pVLR->VlrHeader.Length = (UINT16)nLength; memset(pVLR->VerifierCertificateChain, 0, nPaddedBytes + nCertChain); int ndx = 0; // // see above // #if (!LEAFTOROOT) SGX_DBGPRINT_PRINT_STRING_LTP("leaf cert to root cert direction"); for (it = certChain.rbegin(); it != certChain.rend(); ++it) { memcpy_s(pVLR->VerifierCertificateChain + ndx, (*it).getSize(), (*it).getData(), (*it).getSize()); ndx += (*it).getSize(); } #else SGX_DBGPRINT_PRINT_STRING_LTP("root cert to leaf cert direction"); for (it = certChain.begin(); it != certChain.end(); ++it) { memcpy_s(pVLR->VerifierCertificateChain + ndx, (*it).getSize(), (*it).getData(), (*it).getSize()); ndx += (*it).getSize(); } #endif status = AE_SUCCESS; } while (0); } catch(...) { } SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status); return status; }
ae_error_t pse_pr_interface_psda::ExchangeS2AndS3(/*in*/ const uint8_t* pse_instance_id, /*in */ const upse::Buffer& s2, /*out*/ upse::Buffer& s3) { ae_error_t status = AE_FAILURE; lt_session_m7_t* pLt_session_m7 = NULL; lt_session_m8_t* pLt_session_m8 = NULL; uint32_t lt_session_m7_size = static_cast<uint32_t>(sizeof(lt_session_m7_t) + s2.getSize()); uint32_t lt_session_m8_size = 10000; do { pLt_session_m7 = (lt_session_m7_t*)malloc(lt_session_m7_size); BREAK_IF_TRUE( (NULL == pLt_session_m7), status, AESM_PSE_PR_INSUFFICIENT_MEMORY_ERROR); pLt_session_m8 = (lt_session_m8_t*)malloc(lt_session_m8_size); BREAK_IF_TRUE( (NULL == pLt_session_m8), status, AESM_PSE_PR_INSUFFICIENT_MEMORY_ERROR); memset(pLt_session_m8, 0, lt_session_m8_size); memcpy_s(pLt_session_m7->msg_hdr.pse_instance_id, SW_INSTANCE_ID_SIZE, pse_instance_id, SW_INSTANCE_ID_SIZE); pLt_session_m7->msg_hdr.msg_type = _htonl(PSDA_MSG_TYPE_LT_M7); pLt_session_m7->msg_hdr.msg_len = _htonl(s2.getSize()); memcpy_s(&pLt_session_m7->msg7, s2.getSize(), s2.getData(), s2.getSize()); commBuf_s3.TxBuf->buffer = pLt_session_m7; commBuf_s3.TxBuf->length = lt_session_m7_size; commBuf_s3.RxBuf->buffer = pLt_session_m8; commBuf_s3.RxBuf->length = lt_session_m8_size; status = PSDAService::instance().send_and_recv( PSDA_COMMAND_LT, &commBuf_s3, &responseCode, NO_RETRY_ON_SESSION_LOSS); AESM_DBG_INFO("JHI_SendAndRecv2 response_code is %d", responseCode); if (status != AE_SUCCESS) { AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_DAL_COMM_FAILURE]); break; } if (PSDA_SUCCESS != responseCode) { AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_DAL_SIGMA_ERROR]); BREAK_IF_TRUE( (responseCode == PSDA_INTEGRITY_ERROR), status, AESM_PSDA_LT_SESSION_INTEGRITY_ERROR); BREAK_IF_TRUE( (responseCode == PSDA_INTERNAL_ERROR) , status, AESM_PSDA_INTERNAL_ERROR); BREAK_IF_TRUE( (responseCode == PSDA_PLATFORM_KEYS_REVOKED) , status, AESM_PSDA_PLATFORM_KEYS_REVOKED); BREAK_IF_TRUE( (responseCode == PSDA_PERSISTENT_DATA_WRITE_THROTTLED) , status, AESM_PSDA_WRITE_THROTTLED); BREAK_IF_TRUE( (responseCode != PSDA_SUCCESS) , status, AESM_PSDA_INTERNAL_ERROR); } uint32_t msg_len = _ntohl(pLt_session_m8->msg_hdr.msg_len); uint32_t msg_type = _ntohl(pLt_session_m8->msg_hdr.msg_type); if (msg_type != PSDA_MSG_TYPE_LT_M8) { status = AE_FAILURE; break; } if (commBuf_s3.RxBuf->length <= sizeof(pLt_session_m8->msg_hdr) || msg_len != commBuf_s3.RxBuf->length - sizeof(pLt_session_m8->msg_hdr)) { AESM_DBG_INFO("Received invalid S3 message from PSDA!"); status = AE_FAILURE; break; } status = s3.Alloc(msg_len); BREAK_IF_FAILED(status); upse::BufferWriter bw(s3); status = bw.writeRaw((UINT8*)&pLt_session_m8->msg8, msg_len); BREAK_IF_FAILED(status); status = AE_SUCCESS; } while (0); if (NULL != pLt_session_m7) free(pLt_session_m7); if (NULL != pLt_session_m8) free(pLt_session_m8); return status; }
ae_error_t pse_pr_interface_psda::GetS1(/*in*/const uint8_t* pse_instance_id, /*out*/ upse::Buffer& s1) { ae_error_t status = AE_FAILURE; lt_session_m1_t lt_session_m1; lt_session_m2_t lt_session_m2; do { // endian-ness doesn't matter for these two; they're 0 memcpy_s(lt_session_m1.msg_hdr.pse_instance_id, SW_INSTANCE_ID_SIZE, pse_instance_id, SW_INSTANCE_ID_SIZE); lt_session_m1.msg_hdr.msg_type = PSDA_MSG_TYPE_LT_M1; lt_session_m1.msg_hdr.msg_len = 0; memset(<_session_m2, 0, sizeof(lt_session_m2)); commBuf_s1.TxBuf->buffer = <_session_m1; commBuf_s1.TxBuf->length = sizeof(lt_session_m1_t); commBuf_s1.RxBuf->buffer = <_session_m2; commBuf_s1.RxBuf->length = sizeof(lt_session_m2_t); status = PSDAService::instance().send_and_recv( PSDA_COMMAND_LT, &commBuf_s1, &responseCode, AUTO_RETRY_ON_SESSION_LOSS); AESM_DBG_INFO("JHI_SendAndRecv2 response_code is %d", responseCode); if (status != AE_SUCCESS) { AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_DAL_COMM_FAILURE]); break; } if (PSDA_SUCCESS != responseCode) { AESM_LOG_ERROR("%s", g_event_string_table[SGX_EVENT_DAL_SIGMA_ERROR]); BREAK_IF_TRUE( (responseCode == PSDA_NOT_PROVISIONED), status, AESM_PSDA_NOT_PROVISONED_ERROR); BREAK_IF_TRUE( (responseCode == PSDA_PROTOCOL_NOT_SUPPORTED), status, AESM_PSDA_PROTOCOL_NOT_SUPPORTED); BREAK_IF_TRUE( (responseCode == PSDA_INTERNAL_ERROR) , status, AESM_PSDA_INTERNAL_ERROR); BREAK_IF_TRUE( (responseCode == PSDA_PERSISTENT_DATA_WRITE_THROTTLED) , status, AESM_PSDA_WRITE_THROTTLED); } uint32_t msg_len = _ntohl(lt_session_m2.msg_hdr.msg_len); uint32_t msg_type = _ntohl(lt_session_m2.msg_hdr.msg_type); if (responseCode != PSDA_SUCCESS || msg_type != PSDA_MSG_TYPE_LT_M2 || msg_len != sizeof(pse_cse_lt_msg2_t)) { status = AE_FAILURE; break; } if (commBuf_s1.RxBuf->length <= sizeof(lt_session_m2.msg_hdr) || msg_len != commBuf_s1.RxBuf->length - sizeof(lt_session_m2.msg_hdr)) { AESM_DBG_INFO("Received invalid S1 message from PSDA!"); status = AE_FAILURE; break; } status = s1.Alloc(msg_len); BREAK_IF_FAILED(status); upse::BufferWriter bw(s1); status = bw.writeRaw((UINT8*)<_session_m2.msg2, msg_len); BREAK_IF_FAILED(status); } while (0); return status; }
ae_error_t CertificateProvisioningProtocol::msg3_generate(const upse::Buffer& csrBuffer, const upse::Buffer& quoteBuffer, upse::Buffer& serializedMsg3) { ae_error_t status = AE_FAILURE; tlv_status_t tlv_status = TLV_UNKNOWN_ERROR; provision_request_header_t serializedHeader; memset(&serializedHeader, 0, sizeof(serializedHeader)); TLVsMsg seq2_0_tlv_nonce; TLVsMsg seq3_0_tlv_block_cipher_text; TLVsMsg seq3_1_tlv_quote; TLVsMsg seq3_2_tlv_quote_signature; TLVsMsg seq3_3_tlv_x509_csr; TLVsMsg seq4_0_tlv_mac; do { tlv_status = seq2_0_tlv_nonce.add_nonce(Nonce.getData(), Nonce.getSize()); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; status = msg3_seq3_1_create_quote_tlv(quoteBuffer, seq3_1_tlv_quote); if (AE_FAILED(status)) break; status = msg3_seq3_2_create_quote_signature_tlv(quoteBuffer, seq3_2_tlv_quote_signature); if (AE_FAILED(status)) break; tlv_status = seq3_3_tlv_x509_csr.add_x509_csr(csrBuffer.getData(), csrBuffer.getSize()); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; status = msg3_create_header(TransactionID, seq2_0_tlv_nonce.get_tlv_msg_size(), seq3_1_tlv_quote.get_tlv_msg_size(), seq3_2_tlv_quote_signature.get_tlv_msg_size(), seq3_3_tlv_x509_csr.get_tlv_msg_size(), serializedHeader); if (AE_FAILED(status)) break; upse::Buffer mac; status = msg3_seq3_0_create_block_cipher_text_tlv(seq3_1_tlv_quote, seq3_2_tlv_quote_signature, seq3_3_tlv_x509_csr, seq2_0_tlv_nonce, serializedHeader, EK2, seq3_0_tlv_block_cipher_text, mac); if (AE_FAILED(status)) break; tlv_status = seq4_0_tlv_mac.add_mac(mac.getData()); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; //********************************************************************* // Prepare serialized message buffer //********************************************************************* uint32_t size_msg3 = static_cast<uint32_t>(PROVISION_REQUEST_HEADER_SIZE + seq2_0_tlv_nonce.get_tlv_msg_size() + seq3_0_tlv_block_cipher_text.get_tlv_msg_size() + seq4_0_tlv_mac.get_tlv_msg_size()); status = serializedMsg3.Alloc(size_msg3); if (AE_FAILED(status)) break; serializedMsg3.zeroMemory(); upse::BufferWriter bwMsg3(serializedMsg3); // Write serialized request header to serialized message status = bwMsg3.writeRaw((uint8_t*)&serializedHeader, sizeof(serializedHeader)); if (AE_FAILED(status)) break; // Write sequence 2.0 - Nonce TLV status = bwMsg3.writeRaw(const_cast<uint8_t*>(seq2_0_tlv_nonce.get_tlv_msg()), seq2_0_tlv_nonce.get_tlv_msg_size()); if (AE_FAILED(status)) break; // Write sequence 3.0 - Block Cipher Text TLV (contains 3.1, 3.2, and 3.3 as encrypted payload) status = bwMsg3.writeRaw(const_cast<uint8_t*>(seq3_0_tlv_block_cipher_text.get_tlv_msg()), seq3_0_tlv_block_cipher_text.get_tlv_msg_size()); if (AE_FAILED(status)) break; // Write sequence 4.0 - MAC TLV status = bwMsg3.writeRaw(const_cast<uint8_t*>(seq4_0_tlv_mac.get_tlv_msg()), seq4_0_tlv_mac.get_tlv_msg_size()); if (AE_FAILED(status)) break; status = AE_SUCCESS; } while (0); return status; }
ae_error_t CertificateProvisioningProtocol::msg1_generate(const GroupId gid, upse::Buffer& serializedMsg1) { ae_error_t status = AE_FAILURE; tlv_status_t tlv_status = TLV_UNKNOWN_ERROR; GroupId be_gid; //gid from init_quote is little endian, change to bigendian for backend server here be_gid.data[0]=gid.data[3]; be_gid.data[1]=gid.data[2]; be_gid.data[2]=gid.data[1]; be_gid.data[3]=gid.data[0]; provision_request_header_t header; memset(&header, 0, sizeof(header)); TLVsMsg seq2_0_tlv_cipher_text; TLVsMsg seq2_1_tlv_block_cipher_info; TLVsMsg seq3_0_tlv_block_cipher_text; TLVsMsg seq3_1_tlv_epid_gid; TLVsMsg seq4_0_tlv_mac; do { status = get_random_value(XID_SIZE, TransactionID); if (AE_FAILED(status)) break; // Prepare sequence 2.1 -- Block Cipher Text TLV with SK status = msg1_create_seq2_1(seq2_1_tlv_block_cipher_info); if (AE_FAILED(status)) break; // Prepare sequence 2.0 -- Cipher Text TLV with KeyID and encrypted 2.1 status = msg1_create_seq2_0(seq2_1_tlv_block_cipher_info, seq2_0_tlv_cipher_text); if (AE_FAILED(status)) break; // Prepare sequence 3.1 -- EPID GID TLV tlv_status = seq3_1_tlv_epid_gid.add_epid_gid(be_gid); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; // Derive EK1 upse::Buffer EK1; status = aesCMAC(M1SK, TransactionID, EK1); if (AE_FAILED(status)) break; // Create Request Header (we need to calculate size before AES-GCM CMAC) status = msg1_create_header(seq2_0_tlv_cipher_text.get_tlv_msg_size(), seq3_1_tlv_epid_gid.get_tlv_msg_size(), TransactionID, header); if (AE_FAILED(status)) break; // Prepare sequence 3.0 -- Block Cipher Text TLV with IV and encrypted 3.1 upse::Buffer mac; status = msg1_create_seq3_0(seq3_1_tlv_epid_gid, header, EK1, seq3_0_tlv_block_cipher_text, mac); if (AE_FAILED(status)) break; // Prepare sequence 4.0 -- MAC TLV tlv_status = seq4_0_tlv_mac.add_mac(mac.getData()); status = tlv_error_2_pve_error(tlv_status); if (AE_FAILED(status)) break; //********************************************************************* // Prepare serialized message buffer //********************************************************************* uint32_t size_msg1 = static_cast<uint32_t>(PROVISION_REQUEST_HEADER_SIZE) + seq2_0_tlv_cipher_text.get_tlv_msg_size() + seq3_0_tlv_block_cipher_text.get_tlv_msg_size() + seq4_0_tlv_mac.get_tlv_msg_size(); status = serializedMsg1.Alloc(size_msg1); if (AE_FAILED(status)) break; serializedMsg1.zeroMemory(); upse::BufferWriter bwMsg1(serializedMsg1); // Write serialized request header to serialized message status = bwMsg1.writeRaw((uint8_t*)&header, sizeof(header)); if (AE_FAILED(status)) break; // Write sequence 2.0 - Cipher Text TLV (contains 2.1 as encrypted payload) status = bwMsg1.writeRaw(const_cast<uint8_t*>(seq2_0_tlv_cipher_text.get_tlv_msg()), seq2_0_tlv_cipher_text.get_tlv_msg_size()); if (AE_FAILED(status)) break; // Write sequence 3.0 - Block Cipher Text TLV (contains 3.1 as encrypted payload) status = bwMsg1.writeRaw(const_cast<uint8_t*>(seq3_0_tlv_block_cipher_text.get_tlv_msg()), seq3_0_tlv_block_cipher_text.get_tlv_msg_size()); if (AE_FAILED(status)) break; // Write sequence 4.0 - MAC TLV status = bwMsg1.writeRaw(const_cast<uint8_t*>(seq4_0_tlv_mac.get_tlv_msg()), seq4_0_tlv_mac.get_tlv_msg_size()); if (AE_FAILED(status)) break; status = AE_SUCCESS; } while (0); return status; }
ae_error_t SigmaHelper::GetRLsFromServer ( /*out*/ upse::Buffer& sigRlOut, /*out*/ upse::Buffer& privRlOut ) { // // iKGF serves up binary (legacy) versions of EPID 1.1 RLs // all we need to do is convey the GID in the URL itself // for example, https://trustedservices.intel.com/content/crl/Signature_<GID>.crl // so, we get url out of config file and concatenate with filename that's // specific to the type of RL ae_error_t sigRetValue = AE_FAILURE; ae_error_t privRetValue = AE_FAILURE; if(!g_epid_service){ AESM_DBG_ERROR("failed to load IEpidQuoteService service"); return AE_FAILURE; } if(!g_network_service){ AESM_DBG_ERROR("failed to load network service"); return AE_FAILURE; } const char *url = g_epid_service->get_server_url(REVOCATION_LIST_RETRIEVAL); if (url == NULL) { return OAL_CONFIG_FILE_ERROR; } uint8_t* p1 = const_cast<uint8_t*>(m_gid.getData()); do { if ((m_gid.getSize() < 1) || (m_gid.getSize() > 4)) break; char msg[9]; for (unsigned i = 0; i < m_gid.getSize(); i++) { sprintf_s((char*) msg+2*i, 3, "%02X", *(p1+m_gid.getSize()-1-i)); } std::string gidString(msg); unsigned numLeading0s = 8 - static_cast<unsigned>(gidString.length()); for (unsigned i = 0; i < numLeading0s; i++) { gidString = '0' + gidString; } //if config file entry doesn't have trailing "/" , add it. std::string s_url = url; if(s_url.size()>0&&s_url[s_url.size()-1]!='/') s_url+='/'; std::string stringUrl = s_url + "Signature_" + gidString + ".crl"; uint8_t *recv=NULL; uint32_t recv_size = 0; sigRetValue = g_network_service->aesm_send_recv_msg(stringUrl.c_str(), NULL, 0, recv, recv_size, GET, false); if (AE_SUCCESS != sigRetValue) { sigRlOut.Alloc(0); } else { sigRlOut.Alloc(recv_size); upse::BufferWriter bw(sigRlOut); bw.writeRaw(recv, recv_size); g_network_service->aesm_free_response_msg(recv); } stringUrl = s_url + "Product_" + gidString + ".crl"; recv=NULL; recv_size = 0; privRetValue = g_network_service->aesm_send_recv_msg(stringUrl.c_str(), NULL, 0, recv, recv_size, GET, false); if (AE_SUCCESS != privRetValue) { privRlOut.Alloc(0); } else { privRlOut.Alloc(recv_size); upse::BufferWriter bw(privRlOut); bw.writeRaw(recv, recv_size); g_network_service->aesm_free_response_msg(recv); } } while (0); if (AE_FAILED(privRetValue)) { SGX_DBGPRINT_PRINT_STRING_LTP("PrivRL not retrieved: continuing without PrivRL"); } if (AE_FAILED(sigRetValue)) { SGX_DBGPRINT_PRINT_STRING_LTP("SigRL not retrieved: continuing without SigRL"); } if ((privRetValue == AE_SUCCESS) && (sigRetValue == AE_SUCCESS)) { return AE_SUCCESS; } else if (sigRetValue != AE_SUCCESS) { return AESM_PSE_PR_GET_SIGRL_ERROR; } else { return AESM_PSE_PR_GET_PRIVRL_ERROR; } }
ae_error_t SigmaHelper::GetOcspResponseFromServer ( /*in */ const std::list<upse::Buffer>& certChain, /*in */ const OCSP_REQ& ocspReq, /*out*/ upse::Buffer& ocspResp ) { ae_error_t status = AE_FAILURE; int nPaddedBytes = 0; int nTotalOcspBytes = 0; do { if (ocspReq.ReqType == NO_OCSP) { status = AE_SUCCESS; break; } if(!g_epid_service){ AESM_DBG_ERROR("failed to load IEpidQuoteService service"); return AE_FAILURE; } const char *url = g_epid_service->get_server_url( PSE_OCSP); if (url == NULL){ return OAL_CONFIG_FILE_ERROR; } // Load the root certificate into a local buffer upse::Buffer rootCert; SigmaHelper::GetRootCA(rootCert); std::list<upse::Buffer> ocspResponseList; // loop through chain and get an OCSP Response for each certificate/issuer pair bool fDone = false; // // certs were added leaf to root direction (assuming server functions according to spec) // std::list<upse::Buffer>::const_iterator itCertificate = certChain.begin(); do { if (itCertificate == certChain.end()) { status = AE_FAILURE; break; } upse::Buffer ocspResponse; const upse::Buffer& verifierCertificate = *itCertificate; ++itCertificate; int busy_loop = 0; do { if (itCertificate != certChain.end()) { const upse::Buffer& issuerCertificate = *itCertificate; status = Get_OCSPResponse(url, &ocspReq.OcspNonce, verifierCertificate, issuerCertificate, ocspResponse); } else { fDone = true; const upse::Buffer& issuerCertificate = rootCert; status = Get_OCSPResponse(url, &ocspReq.OcspNonce, verifierCertificate, issuerCertificate, ocspResponse); } if (AESM_PSE_PR_OCSP_RESPONSE_STATUS_TRYLATER != status) break; se_sleep(OCSP_BUSY_RETRY_SLEEP_MILLISECONDS); } while (busy_loop++ < MAX_OCSP_BUSY_RETRIES); if (AE_FAILED(status)) break; nPaddedBytes += REQUIRED_PADDING_DWORD_ALIGNMENT(ocspResponse.getSize()); nTotalOcspBytes += ocspResponse.getSize(); ocspResponseList.push_back(ocspResponse); } while (!fDone); if (AE_FAILED(status)) break; if (0 == ocspResponseList.size()) { status = AE_FAILURE; break; } nPaddedBytes = REQUIRED_PADDING_DWORD_ALIGNMENT(nTotalOcspBytes); if(UINT16_MAX-((int)sizeof(SIGMA_VLR_HEADER) + nPaddedBytes) < nTotalOcspBytes){ status = AE_FAILURE; break; } int nLength = static_cast<int>(sizeof(SIGMA_VLR_HEADER)) + nPaddedBytes + nTotalOcspBytes; ocspResp.Alloc(nLength); upse::BufferWriter bw(ocspResp); uint8_t* p; status = bw.reserve(nLength, &p); if (AE_FAILED(status)) break; OCSP_RESPONSE_VLR* pVLR = (OCSP_RESPONSE_VLR*)p; pVLR->VlrHeader.ID = OCSP_RESPONSE_VLR_ID; pVLR->VlrHeader.PaddedBytes = (UINT8)nPaddedBytes; pVLR->VlrHeader.Length = (UINT16)nLength; memset(pVLR->OcspResponse, 0, nPaddedBytes + nTotalOcspBytes); int nNext = 0; // // order above doesn't really matter since it's between verifier/host and ocsp responder // and each request/response is independent // spec basically says what's correct here // but we'll leave condition to show how to traverse in either order // #if !defined(LEAFTOROOT) #error LEAFTOROOT not defined #endif #if !LEAFTOROOT // // this clause adds responses from root to leaf // SGX_DBGPRINT_PRINT_STRING_LTP("root ocsp to leaf ocsp direction"); std::list<upse::Buffer>::reverse_iterator itRespList = ocspResponseList.rbegin(); for ( ; itRespList != ocspResponseList.rend(); ++itRespList) { const upse::Buffer& item = *itRespList; memcpy_s(pVLR->OcspResponse + nNext, item.getSize(), item.getData(), item.getSize()); nNext += item.getSize(); } #else SGX_DBGPRINT_PRINT_STRING_LTP("leaf ocsp to root ocsp direction"); // // this clause adds responses from leaf to root // std::list<upse::Buffer>::iterator itRespList = ocspResponseList.begin(); for ( ; itRespList != ocspResponseList.end(); ++itRespList) { const upse::Buffer& item = *itRespList; memcpy_s(pVLR->OcspResponse + nNext, item.getSize(), item.getData(), item.getSize()); nNext += item.getSize(); } #endif Helper::write_ocsp_response_vlr(ocspResp); status = AE_SUCCESS; } while (0); if (status == OAL_NETWORK_UNAVAILABLE_ERROR) { if (ocspReq.ReqType == CACHED && AE_SUCCEEDED(Helper::read_ocsp_response_vlr(ocspResp))) { status = AE_SUCCESS; } } SGX_DBGPRINT_PRINT_FUNCTION_AND_RETURNVAL(__FUNCTION__, status); return status; }
void SigmaHelper::GetRootCA(upse::Buffer& b) { b.Alloc(sizeof(caRootDER)); upse::BufferWriter bw(b); bw.writeRaw(caRootDER, sizeof(caRootDER)); }