Skip to content

Commit

Permalink
integrate use of attestation api in eservice enclave
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Vavala <bruno.vavala@intel.com>
  • Loading branch information
bvavala committed Oct 19, 2024
1 parent 4d68cb7 commit 77a17c7
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build/cmake/SGX.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ ENDIF()
SET(SGX_TRUSTED_LIBS sgx_tstdc sgx_tcxx sgx_tcrypto ${SERVICE_LIBRARY_NAME})
SET(SGX_UNTRUSTED_LIBS ${URTS_LIBRARY_NAME} pthread)

SET(SGX_SEARCH_PATH "${SGX_SDK}/include:${SGX_SSL}/include")
SET(SGX_SEARCH_PATH "${SGX_SDK}/include:${SGX_SSL}/include:$ENV{PDO_SOURCE_ROOT}/common")
SET(SGX_TRUSTED_INCLUDE_DIRS
"${SGX_SDK}/include"
"${SGX_SDK}/include/tlibc"
Expand Down
5 changes: 5 additions & 0 deletions common/cmake/CommonVariables.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ SET(C_COMMON_LIB_NAME cpdo-common)
SET(U_COMMON_LIB_NAME updo-common)
SET(T_COMMON_LIB_NAME tpdo-common)

# import attestation lib variables U_ONE_ATTESTATION_LIB_NAME, T_ONE_ATTESTATION_LIB_NAME
INCLUDE("${COMMON_SOURCE_DIR}/crypto/attestation-api/CMakeVariables.txt")

SET(INTERPRETER_LIB_NAME pdo-contract)

# Block store library does not depend on sgx at all
Expand Down Expand Up @@ -78,6 +81,7 @@ LIST(APPEND COMMON_CLIENT_LIBS pthread lmdb)
LIST(APPEND COMMON_UNTRUSTED_LIBS ${U_COMMON_LIB_NAME})
LIST(APPEND COMMON_UNTRUSTED_LIBS ${U_CRYPTO_LIB_NAME})
LIST(APPEND COMMON_UNTRUSTED_LIBS ${BLOCK_STORE_LIB_NAME})
LIST(APPEND COMMON_UNTRUSTED_LIBS ${U_ONE_ATTESTATION_LIB_NAME})
LIST(APPEND COMMON_UNTRUSTED_LIBS pthread lmdb)

# -----------------------------------------------------------------
Expand All @@ -88,4 +92,5 @@ LIST(APPEND COMMON_TRUSTED_LIBS ${T_COMMON_LIB_NAME})
LIST(APPEND COMMON_TRUSTED_LIBS ${T_CRYPTO_LIB_NAME})
LIST(APPEND COMMON_TRUSTED_LIBS ${BLOCK_STORE_LIB_NAME})
LIST(APPEND COMMON_TRUSTED_LIBS ${COMMON_INTERPRETER_LIBRARIES})
LIST(APPEND COMMON_TRUSTED_LIBS ${T_ONE_ATTESTATION_LIB_NAME})
LIST(APPEND COMMON_TRUSTED_LIBS lmdb)
1 change: 1 addition & 0 deletions eservice/lib/libpdo_enclave/enclave.edl
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ enclave {
from "signup.edl" import *;
from "contract.edl" import *;
from "block_store.edl" import *;
from "crypto/attestation-api/ocalls/attestation-ocalls.edl" import *;
};
5 changes: 5 additions & 0 deletions eservice/lib/libpdo_enclave/signup.edl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ enclave {
public pdo_err_t ecall_CreateEnclaveData(
[in] const sgx_target_info_t* inTargetInfo,
[in, string] const char* inOriginatorPublicKeyHash,
[in, size=inAttestationParamsSize] uint8_t* inAttestationParams,
size_t inAttestationParamsSize,
[out, size=inAllocatedAttestationSize] uint8_t* outAttestation,
size_t inAllocatedAttestationSize,
[out] size_t* outAttestationSize,
[out, size=inAllocatedPublicEnclaveDataSize] char* outPublicEnclaveData,
size_t inAllocatedPublicEnclaveDataSize,
[out] size_t* outPublicEnclaveDataSize,
Expand Down
26 changes: 26 additions & 0 deletions eservice/lib/libpdo_enclave/signup_enclave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include "enclave_utils.h"
#include "signup_enclave.h"

#include "attestation-api/include/attestation.h"

// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XX Declaration of static helper functions XX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Expand Down Expand Up @@ -109,6 +111,11 @@ pdo_err_t ecall_CalculatePublicEnclaveDataSize(size_t* pPublicEnclaveDataSize)
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
pdo_err_t ecall_CreateEnclaveData(const sgx_target_info_t* inTargetInfo,
const char* inOriginatorPublicKeyHash,
uint8_t* inAttestationParams,
size_t inAttestationParamsSize,
uint8_t* outAttestation,
size_t inAllocatedAttestationSize,
size_t* outAttestationSize,
char* outPublicEnclaveData,
size_t inAllocatedPublicEnclaveDataSize,
size_t* outPublicEnclaveDataSize,
Expand Down Expand Up @@ -158,6 +165,16 @@ pdo_err_t ecall_CreateEnclaveData(const sgx_target_info_t* inTargetInfo,
sgx_report_data_t reportData = {0};
CreateSignupReportData(inOriginatorPublicKeyHash, enclaveData, &reportData);

// get serialized statement (which will be later hashed to create report data)
std::string hashString;
hashString.append(enclaveData.get_serialized_signing_key());
hashString.append(enclaveData.get_serialized_encryption_key());
std::transform(inOriginatorPublicKeyHash,
inOriginatorPublicKeyHash + strlen(inOriginatorPublicKeyHash), std::back_inserter(hashString),
[](char c) {
return c; // do nothing
});

sgx_status_t ret = sgx_create_report(inTargetInfo, &reportData, outEnclaveReport);
pdo::error::ThrowSgxError(ret, "Failed to create enclave report");

Expand Down Expand Up @@ -187,6 +204,15 @@ pdo_err_t ecall_CreateEnclaveData(const sgx_target_info_t* inTargetInfo,
strncpy_s(outPublicEnclaveData, inAllocatedPublicEnclaveDataSize,
enclaveData.get_public_data().c_str(),
enclaveData.get_public_data_size());

bool b = init_attestation(inAttestationParams, inAttestationParamsSize);
pdo::error::ThrowIf<pdo::error::ValueError>(b == false, "Error in init attestation");

uint32_t as;
b = get_attestation((uint8_t*)hashString.c_str(), hashString.length(), outAttestation, inAllocatedAttestationSize, &as);
*outAttestationSize = (size_t)as;
pdo::error::ThrowIf<pdo::error::ValueError>(b == false, "Error in attestation");

}
catch (pdo::error::Error& e)
{
Expand Down
5 changes: 5 additions & 0 deletions eservice/lib/libpdo_enclave/signup_enclave.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ extern pdo_err_t ecall_CalculatePublicEnclaveDataSize(size_t* pPublicEnclaveData
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
extern pdo_err_t ecall_CreateEnclaveData(const sgx_target_info_t* inTargetInfo,
const char* inOriginatorPublicKeyHash,
uint8_t* inAttestationParams,
size_t inAttestationParamsSize,
uint8_t* outAttestation,
size_t inAllocatedAttestationSize,
size_t* outAttestationSize,
char* outPublicEnclaveData,
size_t inAllocatedPublicEnclaveDataSize,
size_t* outPublicEnclaveDataSize,
Expand Down
2 changes: 1 addition & 1 deletion eservice/pdo/eservice/enclave/enclave/enclave.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ namespace pdo {
{
return this->threadId;
}
sgx_spid_t spid;

protected:
void LoadEnclave();
Expand All @@ -116,7 +117,6 @@ namespace pdo {
size_t sealedSignupDataSize;

std::string signatureRevocationList;
sgx_spid_t spid;

sgx_target_info_t reportTargetInfo;
sgx_epid_group_id_t epidGroupId;
Expand Down
37 changes: 37 additions & 0 deletions eservice/pdo/eservice/enclave/enclave/signup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "pdo_error.h"
#include "types.h"
#include "zero.h"
#include "jsonvalue.h"
#include "hex_string.h"

#include "enclave/enclave.h"
#include "enclave/base.h"
Expand Down Expand Up @@ -134,11 +136,24 @@ pdo_err_t pdo::enclave_api::enclave_data::CreateEnclaveData(
size_t computed_public_enclave_data_size;
size_t computed_sealed_enclave_data_size;

std::string hex_spid = BinaryToHexString(g_Enclave[0].spid.id, 16);
std::string attestation_params =
std::string("{\"attestation_type\": \"epid-linkable\", \"hex_spid\": \"") +
hex_spid +
std::string("\", \"sig_rl\": \"\"}")
;
ByteArray attestation;
attestation.resize(1 << 12);
size_t attestation_size;

sresult = g_Enclave[0].CallSgx(
[enclaveid,
&presult,
target_info,
inOriginatorPublicKeyHash,
&attestation_params,
&attestation,
&attestation_size,
&outPublicEnclaveData,
&computed_public_enclave_data_size,
&sealed_enclave_data_buffer,
Expand All @@ -150,6 +165,11 @@ pdo_err_t pdo::enclave_api::enclave_data::CreateEnclaveData(
&presult,
&target_info,
inOriginatorPublicKeyHash.c_str(),
(uint8_t*)(attestation_params.c_str()),
attestation_params.length(),
attestation.data(),
attestation.size(),
&attestation_size,
outPublicEnclaveData.data(),
outPublicEnclaveData.size(),
&computed_public_enclave_data_size,
Expand All @@ -175,6 +195,23 @@ pdo_err_t pdo::enclave_api::enclave_data::CreateEnclaveData(
g_Enclave[0].CreateQuoteFromReport(&enclave_report, enclave_quote_buffer);
outEnclaveQuote = ByteArrayToBase64EncodedString(enclave_quote_buffer);


{
const char* pvalue = nullptr;
std::string a(attestation.begin(), attestation.end());

JsonValue parsed(json_parse_string(a.c_str()));
pdo::error::ThrowIfNull(parsed.value, "failed to parse serialized attestation; badly formed JSON");

JSON_Object* data_object = json_value_get_object(parsed);
pdo::error::ThrowIfNull(data_object, "invalid serialized attestation; missing root object");

pvalue = json_object_dotget_string(data_object, "attestation");
pdo::error::ThrowIfNull(pvalue, "invalid serialized attestation; missing attestation");

outEnclaveQuote.assign(pvalue);
}

} catch (pdo::error::Error& e) {
pdo::enclave_api::base::SetLastError(e.what());
result = e.error_code();
Expand Down
3 changes: 2 additions & 1 deletion eservice/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@

libraries = [
'updo-common',
'u-one-attestation',
'pdo-lmdb-block-store',
'lmdb'
]
Expand All @@ -109,7 +110,7 @@
else :
libraries += ['sgx_urts', 'sgx_uae_service']

libraries += ['sgx_usgxssl']
libraries += ['sgx_usgxssl', 'sgx_dcap_ql']

module_files = [
os.path.join(module_src_path, 'pdo_enclave_internal.i'),
Expand Down

0 comments on commit 77a17c7

Please sign in to comment.