Skip to content

Commit

Permalink
Update the uTPM and C-Shared Util libs (#1220)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrohera authored May 20, 2019
1 parent 49be5d4 commit a272069
Show file tree
Hide file tree
Showing 41 changed files with 142 additions and 123 deletions.
2 changes: 2 additions & 0 deletions edgelet/hsm-sys/azure-iot-hsm-c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ set (iothsm_VERSION_PATCH 8)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

include_directories(./deps/c-shared/inc)
include_directories(./deps/c-shared/deps/azure-macro-utils-c/inc)
include_directories(./deps/c-shared/deps/umock-c/inc)
include_directories(./deps/utpm/inc)
include_directories(. ./inc)

Expand Down
2 changes: 1 addition & 1 deletion edgelet/hsm-sys/azure-iot-hsm-c/deps/c-shared
Submodule c-shared updated 291 files
4 changes: 2 additions & 2 deletions edgelet/hsm-sys/azure-iot-hsm-c/src/certificate_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "certificate_info.h"

#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/base64.h"
#include "azure_c_shared_utility/azure_base64.h"
#include "azure_c_shared_utility/buffer_.h"
#include "azure_c_shared_utility/xlogging.h"

Expand Down Expand Up @@ -180,7 +180,7 @@ static BUFFER_HANDLE decode_certificate(CERT_DATA_INFO* cert_info)
}
iterator++;
}
result = Base64_Decoder(cert_base64);
result = Azure_Base64_Decode(cert_base64);
free(cert_base64);
}
return result;
Expand Down
1 change: 1 addition & 0 deletions edgelet/hsm-sys/azure-iot-hsm-c/src/edge_enc_openssl_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "azure_c_shared_utility/gballoc.h"
#include "hsm_client_store.h"
#include "hsm_err.h"
#include "hsm_log.h"
#include "edge_openssl_common.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "azure_c_shared_utility/gballoc.h"
#include "hsm_client_data.h"
#include "hsm_client_store.h"
#include "hsm_err.h"
#include "hsm_key.h"
#include "hsm_log.h"
#include "hsm_constants.h"
Expand Down
5 changes: 3 additions & 2 deletions edgelet/hsm-sys/azure-iot-hsm-c/src/edge_hsm_client_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdlib.h>

#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/base64.h"
#include "azure_c_shared_utility/azure_base64.h"
#include "azure_c_shared_utility/buffer_.h"
#include "azure_c_shared_utility/strings.h"
#include "azure_c_shared_utility/singlylinkedlist.h"
Expand All @@ -11,6 +11,7 @@
#include "hsm_client_data.h"
#include "hsm_client_store.h"
#include "hsm_constants.h"
#include "hsm_err.h"
#include "hsm_key.h"
#include "hsm_log.h"
#include "hsm_utils.h"
Expand Down Expand Up @@ -599,7 +600,7 @@ STRING_HANDLE compute_b64_sha_digest_string
else
{
size_t digest_size = USHAHashSize(SHA256);
if ((result = Base64_Encode_Bytes(digest, digest_size)) == NULL)
if ((result = Azure_Base64_Encode_Bytes(digest, digest_size)) == NULL)
{
LOG_ERROR("Base 64 encode failed after SHA compute");
}
Expand Down
1 change: 1 addition & 0 deletions edgelet/hsm-sys/azure-iot-hsm-c/src/edge_hsm_client_x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "azure_c_shared_utility/crt_abstractions.h"
#include "hsm_client_data.h"
#include "hsm_constants.h"
#include "hsm_err.h"
#include "hsm_log.h"
#include "hsm_utils.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "azure_c_shared_utility/gballoc.h"

#include "hsm_client_store.h"
#include "hsm_err.h"
#include "hsm_key.h"
#include "hsm_log.h"

Expand Down
2 changes: 1 addition & 1 deletion edgelet/hsm-sys/azure-iot-hsm-c/src/edge_openssl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {
#include <stddef.h>
#endif

#include "azure_c_shared_utility/umock_c_prod.h"
#include "umock_c/umock_c_prod.h"

MOCKABLE_FUNCTION(, void, initialize_openssl);

Expand Down
1 change: 1 addition & 0 deletions edgelet/hsm-sys/azure-iot-hsm-c/src/edge_pki_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "azure_c_shared_utility/hmacsha256.h"
#include "edge_openssl_common.h"

#include "hsm_err.h"
#include "hsm_key.h"
#include "hsm_log.h"
#include "hsm_utils.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include "azure_c_shared_utility/buffer_.h"
#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/hmacsha256.h"
#include "azure_c_shared_utility/macro_utils.h"
#include "azure_macro_utils/macro_utils.h"

#include "hsm_err.h"
#include "hsm_log.h"

int perform_sign_with_key
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include "azure_c_shared_utility/umock_c_prod.h"
#include "umock_c/umock_c_prod.h"

MOCKABLE_FUNCTION(,int, perform_sign_with_key, const unsigned char *, key, size_t, key_len,
const unsigned char *, data_to_be_signed, size_t, data_to_be_signed_size,
MOCKABLE_FUNCTION(,int, perform_sign_with_key, const unsigned char *, key, size_t, key_len,
const unsigned char *, data_to_be_signed, size_t, data_to_be_signed_size,
unsigned char **, digest, size_t *, digest_size);

9 changes: 5 additions & 4 deletions edgelet/hsm-sys/azure-iot-hsm-c/src/hsm_client_tpm_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/sastoken.h"
#include "azure_c_shared_utility/sha.h"
#include "hsm_log.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "azure_utpm_c/tpm_comm.h"
#include "azure_utpm_c/tpm_codec.h"
#include "azure_utpm_c/Marshal_fp.h" // for activation blob unmarshaling

#include "hsm_client_data.h"
#include "hsm_err.h"
#include "hsm_log.h"
#include "edge_sas_perform_sign_with_key.h"
#include "azure_utpm_c/tpm_comm.h"
#include "azure_utpm_c/tpm_codec.h"

#include "azure_utpm_c/Marshal_fp.h" // for activation blob unmarshaling

#define EPOCH_TIME_T_VALUE 0
#define HMAC_LENGTH 32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "azure_c_shared_utility/gballoc.h"
#include "hsm_client_data.h"
#include "hsm_client_store.h"
#include "hsm_err.h"
#include "hsm_log.h"
#include "hsm_constants.h"

Expand Down
6 changes: 6 additions & 0 deletions edgelet/hsm-sys/azure-iot-hsm-c/src/hsm_err.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef HSM_ERR_H
#define HSM_ERR_H

#define __FAILURE__ MU_FAILURE

#endif //HSM_ERR_H
2 changes: 1 addition & 1 deletion edgelet/hsm-sys/azure-iot-hsm-c/src/hsm_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {
#include <stddef.h>
#endif

#include "azure_c_shared_utility/umock_c_prod.h"
#include "umock_c/umock_c_prod.h"
#include "hsm_key_interface.h"

enum HSM_PKI_KEY_T_TAG
Expand Down
1 change: 1 addition & 0 deletions edgelet/hsm-sys/azure-iot-hsm-c/src/hsm_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "hsm_err.h"
#include "hsm_log.h"
#include "hsm_utils.h"

Expand Down
2 changes: 1 addition & 1 deletion edgelet/hsm-sys/azure-iot-hsm-c/src/hsm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define HSM_UTILS_H

#include <stddef.h>
#include "azure_c_shared_utility/umock_c_prod.h"
#include "umock_c/umock_c_prod.h"

//##############################################################################
// Helper macros
Expand Down
2 changes: 1 addition & 1 deletion edgelet/hsm-sys/azure-iot-hsm-c/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(save_ut ${run_unittests})
set(run_unittests OFF CACHE BOOL "unittests" FORCE)
add_subdirectory(../deps/c-shared/testtools/ctest c-shared/testtools/ctest)
add_subdirectory(../deps/c-shared/testtools/testrunner c-shared/testtools/testrunner)
add_subdirectory(../deps/c-shared/testtools/umock-c c-shared/testtools/umock-c)
add_subdirectory(../deps/c-shared/deps/umock-c c-shared/deps/umock-c)
if (save_ut)
set(run_unittests ${save_ut} CACHE BOOL "unittests" FORCE)
endif(save_ut)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ static void* my_gballoc_realloc(void* ptr, size_t size)
}

#include "testrunnerswitcher.h"
#include "umock_c.h"
#include "umocktypes_charptr.h"
#include "umocktypes_stdint.h"
#include "umock_c_negative_tests.h"
#include "azure_c_shared_utility/macro_utils.h"
#include "umock_c/umock_c.h"
#include "umock_c/umocktypes_charptr.h"
#include "umock_c/umocktypes_stdint.h"
#include "umock_c/umock_c_negative_tests.h"
#include "azure_macro_utils/macro_utils.h"

#define ENABLE_MOCKS
#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/umock_c_prod.h"
#include "umock_c/umock_c_prod.h"

#include "azure_c_shared_utility/buffer_.h"
#include "azure_c_shared_utility/base64.h"
#include "azure_c_shared_utility/azure_base64.h"
#undef ENABLE_MOCKS

#include "certificate_info.h"
Expand All @@ -51,9 +51,9 @@ extern "C" {
#endif

extern time_t get_utc_time_from_asn_string(const unsigned char *time_value, size_t length);
extern STRING_HANDLE real_Base64_Encoder(BUFFER_HANDLE input);
extern STRING_HANDLE real_Azure_Base64_Encode(BUFFER_HANDLE input);
extern STRING_HANDLE real_Base64_Encode_Bytes(const unsigned char* source, size_t size);
extern BUFFER_HANDLE real_Base64_Decoder(const char* source);
extern BUFFER_HANDLE real_Azure_Base64_Decode(const char* source);

extern BUFFER_HANDLE real_BUFFER_new(void);
extern void real_BUFFER_delete(BUFFER_HANDLE handle);
Expand Down Expand Up @@ -187,12 +187,12 @@ static size_t TEST_PRIVATE_KEY_LEN = sizeof(TEST_PRIVATE_KEY)/sizeof(TEST_PRIVAT
static TEST_MUTEX_HANDLE g_testByTest;
static TEST_MUTEX_HANDLE g_dllByDll;

DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)

static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
{
char temp_str[256];
(void)snprintf(temp_str, sizeof(temp_str), "umock_c reported error :%s", ENUM_TO_STRING(UMOCK_C_ERROR_CODE, error_code));
(void)snprintf(temp_str, sizeof(temp_str), "umock_c reported error :%s", MU_ENUM_TO_STRING(UMOCK_C_ERROR_CODE, error_code));
ASSERT_FAIL(temp_str);
}

Expand Down Expand Up @@ -237,7 +237,7 @@ BEGIN_TEST_SUITE(certificate_info_ut)
REGISTER_GLOBAL_MOCK_HOOK(BUFFER_pre_build, real_BUFFER_pre_build);
REGISTER_GLOBAL_MOCK_FAIL_RETURN(BUFFER_pre_build, __LINE__);

REGISTER_GLOBAL_MOCK_HOOK(Base64_Decoder, real_Base64_Decoder);
REGISTER_GLOBAL_MOCK_HOOK(Azure_Base64_Decode, real_Azure_Base64_Decode);
}

TEST_SUITE_CLEANUP(suite_cleanup)
Expand Down Expand Up @@ -281,7 +281,7 @@ BEGIN_TEST_SUITE(certificate_info_ut)
STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
STRICT_EXPECTED_CALL(gballoc_malloc(cert_len));
STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
STRICT_EXPECTED_CALL(Base64_Decoder(IGNORED_PTR_ARG));
STRICT_EXPECTED_CALL(Azure_Base64_Decode(IGNORED_PTR_ARG));
// *************** Happens in Decoder **************
STRICT_EXPECTED_CALL(BUFFER_new());
STRICT_EXPECTED_CALL(BUFFER_pre_build(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ static void test_hook_gballoc_free(void* ptr)
}

#include "testrunnerswitcher.h"
#include "umock_c.h"
#include "umock_c_negative_tests.h"
#include "umocktypes_charptr.h"
#include "umock_c/umock_c.h"
#include "umock_c/umock_c_negative_tests.h"
#include "umock_c/umocktypes_charptr.h"

//#############################################################################
// Declare and enable MOCK definitions
Expand Down Expand Up @@ -102,7 +102,7 @@ MOCKABLE_FUNCTION(, int, generate_rand_buffer, unsigned char*, buffer, size_t, n
#define TEST_TRUST_BUNDLE_CERT_INFO_HANDLE (CERT_INFO_HANDLE)0x1004
#define TEST_CERT_PROPS_HANDLE (CERT_PROPS_HANDLE)0x1005

DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)

static TEST_MUTEX_HANDLE g_testByTest;
static TEST_MUTEX_HANDLE g_dllByDll;
Expand Down Expand Up @@ -152,7 +152,7 @@ static void test_hook_on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
{
char temp_str[256];
(void)snprintf(temp_str, sizeof(temp_str), "umock_c reported error :%s",
ENUM_TO_STRING(UMOCK_C_ERROR_CODE, error_code));
MU_ENUM_TO_STRING(UMOCK_C_ERROR_CODE, error_code));
ASSERT_FAIL(temp_str);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ static void test_hook_gballoc_free(void* ptr)
}

#include "testrunnerswitcher.h"
#include "umock_c.h"
#include "umock_c_negative_tests.h"
#include "umocktypes_charptr.h"
#include "umock_c/umock_c.h"
#include "umock_c/umock_c_negative_tests.h"
#include "umock_c/umocktypes_charptr.h"

//#############################################################################
// Declare and enable MOCK definitions
Expand All @@ -58,7 +58,7 @@ static void test_hook_gballoc_free(void* ptr)
// Test defines and data
//#############################################################################

DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)

#define TEST_BUFFER_HANDLE (BUFFER_HANDLE)0x1000
#define TEST_DERIVED_BUFFER_HANDLE (BUFFER_HANDLE)0x1001
Expand All @@ -79,7 +79,7 @@ static void test_hook_on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
{
char temp_str[256];
(void)snprintf(temp_str, sizeof(temp_str), "umock_c reported error :%s",
ENUM_TO_STRING(UMOCK_C_ERROR_CODE, error_code));
MU_ENUM_TO_STRING(UMOCK_C_ERROR_CODE, error_code));
ASSERT_FAIL(temp_str);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "azure_c_shared_utility/sastoken.h"
#include "azure_c_shared_utility/urlencode.h"
#include "azure_c_shared_utility/hmacsha256.h"
#include "azure_c_shared_utility/base64.h"
#include "azure_c_shared_utility/azure_base64.h"
#include "azure_c_shared_utility/agenttime.h"
#include "azure_c_shared_utility/strings.h"
#include "azure_c_shared_utility/buffer_.h"
Expand Down Expand Up @@ -132,7 +132,7 @@ static void tpm_deprovision(HSM_CLIENT_HANDLE hsm_handle)

static BUFFER_HANDLE test_helper_base64_converter(const char* input)
{
BUFFER_HANDLE result = Base64_Decoder(input);
BUFFER_HANDLE result = Azure_Base64_Decode(input);
ASSERT_IS_NOT_NULL(result, "Line:" TOSTRING(__LINE__));
size_t out_len = BUFFER_length(result);
ASSERT_ARE_NOT_EQUAL(size_t, 0, out_len, "Line:" TOSTRING(__LINE__));
Expand Down Expand Up @@ -211,7 +211,7 @@ static STRING_HANDLE tpm_construct_sas_token
size_t inLen = STRING_length(toBeHashed);
const unsigned char* inBuf = (const unsigned char*)STRING_c_str(toBeHashed);
if ((tpm_sign(hsm_handle, derived_identity, derived_identity_size, inBuf, inLen, hash) != 0) ||
((base64Signature = Base64_Encoder(hash)) == NULL) ||
((base64Signature = Azure_Base64_Encode(hash)) == NULL) ||
((urlEncodedSignature = URL_Encode(base64Signature)) == NULL) ||
(STRING_copy(result, "SharedAccessSignature sr=") != 0) ||
(STRING_concat(result, scope) != 0) ||
Expand Down Expand Up @@ -295,8 +295,8 @@ BEGIN_TEST_SUITE(edge_hsm_sas_auth_int_tests)
test_data_to_be_signed_size, test_output_digest);

// assert
STRING_HANDLE expected = Base64_Encoder(test_expected_digest);
STRING_HANDLE result = Base64_Encoder(test_output_digest);
STRING_HANDLE expected = Azure_Base64_Encode(test_expected_digest);
STRING_HANDLE result = Azure_Base64_Encode(test_output_digest);
printf("Expected: %s\r\n", STRING_c_str(expected));
printf("Got Result: %s\r\n", STRING_c_str(result));
ASSERT_ARE_EQUAL(int, 0, STRING_compare(expected, result));
Expand Down Expand Up @@ -344,8 +344,8 @@ BEGIN_TEST_SUITE(edge_hsm_sas_auth_int_tests)
test_data_to_be_signed, test_data_to_be_signed_size, test_output_digest);

// assert
STRING_HANDLE expected = Base64_Encoder(test_expected_digest);
STRING_HANDLE result = Base64_Encoder(test_output_digest);
STRING_HANDLE expected = Azure_Base64_Encode(test_expected_digest);
STRING_HANDLE result = Azure_Base64_Encode(test_output_digest);
printf("Expected digest: %s, Result digest %s\r\n",
STRING_c_str(expected), STRING_c_str(result));
ASSERT_ARE_EQUAL(int, 0, STRING_compare(expected, result));
Expand Down Expand Up @@ -392,10 +392,10 @@ BEGIN_TEST_SUITE(edge_hsm_sas_auth_int_tests)
tpm_sign(hsm_handle, NULL, 0, (unsigned char*)secondary_fqmid, strlen(secondary_fqmid), test_output_secondary_key_buf);

// assert
STRING_HANDLE expected_primary_key_str = Base64_Encoder(test_expected_primary_key_buf);
STRING_HANDLE expected_secondary_key_str = Base64_Encoder(test_expected_secondary_key_buf);
STRING_HANDLE result_primary_key_str = Base64_Encoder(test_output_primary_key_buf);
STRING_HANDLE result_secondary_key_str = Base64_Encoder(test_output_secondary_key_buf);
STRING_HANDLE expected_primary_key_str = Azure_Base64_Encode(test_expected_primary_key_buf);
STRING_HANDLE expected_secondary_key_str = Azure_Base64_Encode(test_expected_secondary_key_buf);
STRING_HANDLE result_primary_key_str = Azure_Base64_Encode(test_output_primary_key_buf);
STRING_HANDLE result_secondary_key_str = Azure_Base64_Encode(test_output_secondary_key_buf);
printf("Expected Primary Key: %s, Result Primary Key %s\r\n",
STRING_c_str(expected_primary_key_str), STRING_c_str(result_primary_key_str));
printf("Expected Secondary Key: %s, Result Secondary Key %s\r\n",
Expand Down
Loading

0 comments on commit a272069

Please sign in to comment.