diff --git a/src/UnitTest/test_quote_prov.cpp b/src/UnitTest/test_quote_prov.cpp index d833535a..551a503c 100644 --- a/src/UnitTest/test_quote_prov.cpp +++ b/src/UnitTest/test_quote_prov.cpp @@ -5,7 +5,6 @@ #include "../local_cache.h" #include "dcap_provider.h" -#include "sgx_ql_lib_common.h" #include #include @@ -104,7 +103,8 @@ static sgx_ql_get_revocation_info_t sgx_ql_get_revocation_info; static sgx_ql_free_quote_config_t sgx_ql_free_quote_config; static sgx_ql_get_quote_config_t sgx_ql_get_quote_config; static sgx_ql_set_logging_function_t sgx_ql_set_logging_function; -static sgx_ql_free_quote_verification_collateral_t +static sgx_ql_set_logging_callback_t sgx_ql_set_logging_callback; +static sgx_ql_free_quote_verification_collateral_t sgx_ql_free_quote_verification_collateral; static tdx_ql_free_quote_verification_collateral_t tdx_ql_free_quote_verification_collateral; @@ -128,46 +128,45 @@ static constexpr uint8_t TDX_TEST_FMSPC[] = const uint16_t custom_param_length = 45; const char* custom_param = "tcbEvaluationDataNumber=11;region=us central"; std::string tcbEvaluationDataNumber = "11"; +static const sgx_ql_log_level_t LEVEL_ERROR = SGX_QL_LOG_ERROR; const uint16_t incorrect_custom_param_length = 24; const char* incorrect_custom_param = "tcbEvaluationDataNum=11"; // Test input (choose an arbitrary Azure server) -static uint8_t qe_id[16] = { - 0x00, - 0xfb, - 0xe6, - 0x73, - 0x33, - 0x36, - 0xea, - 0xf7, - 0xa4, - 0xe3, - 0xd8, - 0xb9, - 0x66, - 0xa8, - 0x2e, - 0x64}; - -static sgx_cpu_svn_t cpusvn = { - 0x04, - 0x04, - 0x02, - 0x04, - 0xff, - 0x80, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00}; +static uint8_t qe_id[16] = {0x00, + 0xfb, + 0xe6, + 0x73, + 0x33, + 0x36, + 0xea, + 0xf7, + 0xa4, + 0xe3, + 0xd8, + 0xb9, + 0x66, + 0xa8, + 0x2e, + 0x64}; + +static sgx_cpu_svn_t cpusvn = {0x04, + 0x04, + 0x02, + 0x04, + 0xff, + 0x80, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00}; static sgx_isv_svn_t pcesvn = 6; @@ -250,8 +249,7 @@ static void* LoadFunctions() abort(); } - sgx_ql_free_revocation_info = - reinterpret_cast( + sgx_ql_free_revocation_info = reinterpret_cast( dlsym(library, "sgx_ql_free_revocation_info")); EXPECT_NE(sgx_ql_free_revocation_info, nullptr); @@ -267,13 +265,15 @@ static void* LoadFunctions() dlsym(library, "sgx_ql_get_quote_config")); EXPECT_NE(sgx_ql_get_quote_config, nullptr); - sgx_ql_set_logging_function = - reinterpret_cast( + sgx_ql_set_logging_function = reinterpret_cast( dlsym(library, "sgx_ql_set_logging_function")); EXPECT_NE(sgx_ql_set_logging_function, nullptr); - sgx_ql_free_quote_verification_collateral = - reinterpret_cast( + sgx_ql_set_logging_callback = reinterpret_cast( + dlsym(library, "sgx_ql_set_logging_callback")); + EXPECT_NE(sgx_ql_set_logging_callback, nullptr); + + sgx_ql_free_quote_verification_collateral = reinterpret_cast( dlsym(library, "sgx_ql_free_quote_verification_collateral")); EXPECT_NE(sgx_ql_free_quote_verification_collateral, nullptr); @@ -297,6 +297,7 @@ static void* LoadFunctions() sgx_ql_get_quote_verification_collateral = reinterpret_cast( + dlsym(library, "sgx_ql_get_quote_verification_collateral")); EXPECT_NE(sgx_ql_get_quote_verification_collateral, nullptr); @@ -326,8 +327,7 @@ static HINSTANCE LoadFunctions() abort(); } - sgx_ql_free_revocation_info = - reinterpret_cast( + sgx_ql_free_revocation_info = reinterpret_cast( GetProcAddress(hLibCapdll, "sgx_ql_free_revocation_info")); EXPECT_NE(sgx_ql_free_revocation_info, nullptr); @@ -343,15 +343,16 @@ static HINSTANCE LoadFunctions() GetProcAddress(hLibCapdll, "sgx_ql_get_quote_config")); EXPECT_NE(sgx_ql_get_quote_config, nullptr); - sgx_ql_set_logging_function = - reinterpret_cast( + sgx_ql_set_logging_function = reinterpret_cast( GetProcAddress(hLibCapdll, "sgx_ql_set_logging_function")); EXPECT_NE(sgx_ql_set_logging_function, nullptr); - sgx_ql_free_quote_verification_collateral = - reinterpret_cast( - GetProcAddress( - hLibCapdll, "sgx_ql_free_quote_verification_collateral")); + sgx_ql_set_logging_callback = reinterpret_cast( + GetProcAddress(hLibCapdll, "sgx_ql_set_logging_callback")); + EXPECT_NE(sgx_ql_set_logging_callback, nullptr); + + sgx_ql_free_quote_verification_collateral = reinterpret_cast( + GetProcAddress(hLibCapdll, "sgx_ql_free_quote_verification_collateral")); EXPECT_NE(sgx_ql_free_quote_verification_collateral, nullptr); tdx_ql_free_quote_verification_collateral = @@ -368,10 +369,8 @@ static HINSTANCE LoadFunctions() GetProcAddress(hLibCapdll, "sgx_ql_free_root_ca_crl")); EXPECT_NE(sgx_ql_free_root_ca_crl, nullptr); - sgx_ql_get_quote_verification_collateral = - reinterpret_cast( - GetProcAddress( - hLibCapdll, "sgx_ql_get_quote_verification_collateral")); + sgx_ql_get_quote_verification_collateral = reinterpret_cast( + GetProcAddress(hLibCapdll, "sgx_ql_get_quote_verification_collateral")); EXPECT_NE(sgx_ql_get_quote_verification_collateral, nullptr); sgx_ql_get_quote_verification_collateral_with_params = reinterpret_cast< @@ -958,7 +957,11 @@ void ReloadLibrary(libary_type_t* library, bool set_logging_callback = true) #endif if (set_logging_callback) { - ASSERT_TRUE(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log)); + ASSERT_TRUE( + SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log)); + ASSERT_TRUE( + SGX_QL_SUCCESS == + sgx_ql_set_logging_callback(Log, SGX_QL_LOG_INFO)); } } @@ -1290,7 +1293,6 @@ TEST(testQuoteProv, quoteProviderTestsData) { libary_type_t library = LoadFunctions(); ASSERT_TRUE(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log)); - // // Get the data from the service // @@ -1307,7 +1309,8 @@ TEST(testQuoteProv, quoteProviderTestsData) TEST(testQuoteProv, quoteProviderTestsV2DataFromService) { libary_type_t library = LoadFunctions(); - ASSERT_TRUE(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log)); + ASSERT_TRUE( + SGX_QL_SUCCESS == sgx_ql_set_logging_callback(Log, SGX_QL_LOG_INFO)); // // Get the data from the service @@ -1329,7 +1332,6 @@ TEST(testQuoteProv, quoteProviderTestsV2Data) { libary_type_t library = LoadFunctions(); ASSERT_TRUE(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log)); - // // Get the data from the service // @@ -1348,7 +1350,8 @@ TEST(testQuoteProv, quoteProviderTestsV2Data) TEST(testQuoteProv, quoteProviderTestsV3DataFromService) { libary_type_t library = LoadFunctions(); - ASSERT_TRUE(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log)); + ASSERT_TRUE( + SGX_QL_SUCCESS == sgx_ql_set_logging_callback(Log, SGX_QL_LOG_INFO)); // // Get the data from the service @@ -1371,7 +1374,6 @@ TEST(testQuoteProv, quoteProviderTestsV3Data) { libary_type_t library = LoadFunctions(); ASSERT_TRUE(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log)); - // // Get the data from the service // @@ -1429,7 +1431,6 @@ TEST(testQuoteProv, testWithoutLogging) { libary_type_t library = LoadFunctions(); ASSERT_TRUE(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log)); - // // Get the data from the service // @@ -1449,7 +1450,6 @@ TEST(testQuoteProv, testRestrictAccessToFilesystem) { libary_type_t library = LoadFunctions(); ASSERT_TRUE(SGX_PLAT_ERROR_OK == sgx_ql_set_logging_function(Log)); - // // Get the data from the service // diff --git a/src/Windows/dcap_provider_tests/easy_curl_tests.cpp b/src/Windows/dcap_provider_tests/easy_curl_tests.cpp index cde9c629..766b252e 100644 --- a/src/Windows/dcap_provider_tests/easy_curl_tests.cpp +++ b/src/Windows/dcap_provider_tests/easy_curl_tests.cpp @@ -23,6 +23,7 @@ static void DefaultLogCallback(sgx_ql_log_level_t level, const char* message) TEST_MODULE_INITIALIZE(InitializeModule) { + logger_function = DefaultLogCallback; logger_callback = DefaultLogCallback; } diff --git a/src/Windows/dll/dcap_provider.def b/src/Windows/dll/dcap_provider.def index 428e9748..8905e710 100644 --- a/src/Windows/dll/dcap_provider.def +++ b/src/Windows/dll/dcap_provider.def @@ -16,4 +16,4 @@ EXPORTS sgx_ql_get_root_ca_crl; sgx_get_qe_identity_info sgx_free_qe_identity_info - + sgx_ql_set_logging_callback diff --git a/src/dcap_provider.cpp b/src/dcap_provider.cpp index d5419536..36b75f8d 100644 --- a/src/dcap_provider.cpp +++ b/src/dcap_provider.cpp @@ -20,7 +20,6 @@ #include #include -#include "sgx_ql_lib_common.h" #include "environment.h" #ifdef __LINUX__ @@ -2304,10 +2303,19 @@ extern "C" void sgx_ql_free_revocation_info( extern "C" sgx_plat_error_t sgx_ql_set_logging_function( sgx_ql_logging_function_t logger) { - logger_callback = logger; + logger_function = logger; return SGX_PLAT_ERROR_OK; } +extern "C" quote3_error_t sgx_ql_set_logging_callback( + sgx_ql_logging_callback_t logger, + sgx_ql_log_level_t loglevel) +{ + logger_callback = logger; + g_loglevel = loglevel; + return SGX_QL_SUCCESS; +} + quote3_error_t ql_free_quote_verification_collateral( sgx_ql_qve_collateral_t* p_quote_collateral) { diff --git a/src/dcap_provider.h b/src/dcap_provider.h index 6848dbf7..f45f6d80 100644 --- a/src/dcap_provider.h +++ b/src/dcap_provider.h @@ -1,6 +1,12 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +#ifdef __LINUX__ +#include "sgx_ql_lib_common.h" +#else +#include "sgx_ql_lib_common.h" +#endif + #pragma once #ifndef PLATFORM_QUOTE_PROVIDER_H #define PLATFORM_QUOTE_PROVIDER_H @@ -112,10 +118,17 @@ typedef enum _sgx_ql_log_level_t { typedef void ( *sgx_ql_logging_function_t)(sgx_ql_log_level_t level, const char* message); +typedef void ( + *sgx_ql_logging_callback_t)(sgx_ql_log_level_t level, const char* message); + /// Set the callback used for recording log information. -typedef sgx_plat_error_t (*sgx_ql_set_logging_function_t)( - sgx_ql_logging_function_t logger); +typedef sgx_plat_error_t (*sgx_ql_set_logging_function_t)(sgx_ql_logging_function_t logger); const std::string customParam = "customParameter"; +/// Set the callback used for recording log information. +typedef quote3_error_t (*sgx_ql_set_logging_callback_t)( + sgx_ql_logging_callback_t logger, + sgx_ql_log_level_t loglevel); + #endif // #ifndef PLATFORM_QUOTE_PROVIDER_H diff --git a/src/logging.cpp b/src/logging.cpp index 36d8c0c7..d7558ba6 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -23,8 +23,8 @@ #include "environment.h" using namespace std; - -sgx_ql_logging_function_t logger_callback = nullptr; +sgx_ql_logging_callback_t logger_callback = nullptr; +sgx_ql_logging_function_t logger_function = nullptr; static sgx_ql_log_level_t debug_log_level = SGX_QL_LOG_NONE; static bool debug_log_initialized = false; static mutex log_init_mutex; @@ -96,17 +96,21 @@ static inline void enable_debug_logging(string level) debug_log_level = sgx_level; auto logging_enabled_message = "Debug Logging Enabled"; - if (logger_callback != nullptr) - { - logger_callback(SGX_QL_LOG_INFO, logging_enabled_message); - } - else + if ((logger_callback == nullptr) && (logger_function == nullptr)) { printf( "Azure Quote Provider: libdcap_quoteprov.so [%s]: %s\n", log_level_string(SGX_QL_LOG_INFO).c_str(), logging_enabled_message); } + if (logger_callback != nullptr) + { + logger_callback(SGX_QL_LOG_INFO, logging_enabled_message); + } + if (logger_function != nullptr) + { + logger_function(SGX_QL_LOG_INFO, logging_enabled_message); + } } } @@ -137,7 +141,6 @@ void init_debug_log() // void log_message(sgx_ql_log_level_t level, const char* message) { - auto now = chrono::system_clock::now(); time_t nowTimeT = chrono::system_clock::to_time_t(now); char date[100]; @@ -181,22 +184,30 @@ void log_message(sgx_ql_log_level_t level, const char* message) } #endif - if (logger_callback != nullptr) - { - logger_callback(level, message); - } - else + + if ((logger_function == nullptr) && (logger_callback == nullptr)) { init_debug_log(); if (debug_log_level != SGX_QL_LOG_NONE) { if (level <= debug_log_level) { - printf("%s", logMessage.c_str()); + printf( + "Azure Quote Provider: libdcap_quoteprov.so [%s]: %s\n", + log_level_string(level).c_str(), + message); } } } - fflush(stdout); + if (logger_callback != nullptr) + { + logger_callback(level, message); + } + if (logger_function != nullptr) + { + logger_function(level, message); + } + fflush(stdout); #ifndef __LINUX__ // Emitting Events only in Windows @@ -234,7 +245,8 @@ void log(sgx_ql_log_level_t level, const char* fmt, ...) va_list args; va_start(args, fmt); #pragma warning(suppress : 25141) // all fmt buffers come from static strings - vsnprintf(message, sizeof(message), fmt, args); + vsnprintf(message, sizeof(message), + fmt, args); va_end(args); // ensure buf is always null-terminated diff --git a/src/private.h b/src/private.h index cc361357..8d2dea8c 100644 --- a/src/private.h +++ b/src/private.h @@ -8,7 +8,9 @@ #include "dcap_provider.h" #include -extern sgx_ql_logging_function_t logger_callback; +extern sgx_ql_logging_function_t logger_function; +extern sgx_ql_logging_callback_t logger_callback; +extern sgx_ql_log_level_t g_loglevel; /////////////////////////////////////////////////////////////////////////////// // Various internal functions which are not exported. diff --git a/src/sgx_ql_lib_common.h b/src/sgx_ql_lib_common.h index a5431d16..d5b34a1b 100644 --- a/src/sgx_ql_lib_common.h +++ b/src/sgx_ql_lib_common.h @@ -39,7 +39,7 @@ #ifndef _SGX_QL_LIB_COMMON_H_ #define _SGX_QL_LIB_COMMON_H_ -#include "sgx_key.h" +#include ".\Windows\ext\intel\sgx_key.h" #define SGX_QL_MK_ERROR(x) (0x0000E000|(x))