Skip to content

Commit

Permalink
Enable HSM to #define out TPM and/or x.509 (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
jebrando authored Feb 13, 2019
1 parent e5a9504 commit 1239a17
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
27 changes: 19 additions & 8 deletions provisioning_client/src/prov_auth_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ PROV_AUTH_HANDLE prov_auth_create()
{
memset(result, 0, sizeof(PROV_AUTH_INFO) );
SECURE_DEVICE_TYPE sec_type = prov_dev_security_get_type();
#if defined(HSM_TYPE_SAS_TOKEN) || defined(HSM_AUTH_TYPE_CUSTOM)
if (sec_type == SECURE_DEVICE_TYPE_TPM)
{
/* Codes_SRS_PROV_AUTH_CLIENT_07_003: [ prov_auth_create shall validate the specified secure enclave interface to ensure. ] */
Expand All @@ -244,7 +245,9 @@ PROV_AUTH_HANDLE prov_auth_create()
result = NULL;
}
}
else if (sec_type == SECURE_DEVICE_TYPE_X509)
#endif
#if defined(HSM_TYPE_X509) || defined(HSM_AUTH_TYPE_CUSTOM)
if (sec_type == SECURE_DEVICE_TYPE_X509)
{
/* Codes_SRS_PROV_AUTH_CLIENT_07_003: [ prov_auth_create shall validate the specified secure enclave interface to ensure. ] */
result->sec_type = PROV_AUTH_TYPE_X509;
Expand All @@ -262,9 +265,10 @@ PROV_AUTH_HANDLE prov_auth_create()
result = NULL;
}
}
else
{
#endif
#if defined(HSM_TYPE_SYMM_KEY) || defined(HSM_AUTH_TYPE_CUSTOM)
if (sec_type == SECURE_DEVICE_TYPE_SYMMETRIC_KEY)
{
result->sec_type = PROV_AUTH_TYPE_KEY;
const HSM_CLIENT_KEY_INTERFACE* key_interface = hsm_client_key_interface();
if ((key_interface == NULL) ||
Expand All @@ -279,13 +283,20 @@ PROV_AUTH_HANDLE prov_auth_create()
free(result);
result = NULL;
}
#else
LogError("Invalid secure device type was specified");
result = NULL;
#endif
}
#endif

if (result != NULL)
if (result == NULL)
{
LogError("Error allocating result or else unsupported security type %d", sec_type);
}
else if (result->hsm_client_create == NULL)
{
LogError("hsm_client_create is not a valid address");
free(result);
result = NULL;
}
else
{
/* Codes_SRS_PROV_AUTH_CLIENT_07_004: [ prov_auth_create shall call hsm_client_create on the secure enclave interface. ] */
if ((result->hsm_client_handle = result->hsm_client_create() ) == NULL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ static const char* TEST_TOKEN_SCOPE_VALUE = "token_scope";
static const char* TEST_KEY_NAME_VALUE = "key_value";
static const char* TEST_BASE32_VALUE = "aebagbaf";

static const char* TEST_REGISTRATION_ID = "Registration Id";

TEST_DEFINE_ENUM_TYPE(PROV_AUTH_RESULT, PROV_AUTH_RESULT_VALUES);
IMPLEMENT_UMOCK_C_ENUM_TYPE(PROV_AUTH_RESULT, PROV_AUTH_RESULT_VALUES);

Expand Down Expand Up @@ -1291,6 +1293,78 @@ BEGIN_TEST_SUITE(prov_auth_client_ut)
prov_auth_destroy(sec_handle);
}

TEST_FUNCTION(prov_auth_set_registration_id_handle_NULL_fail)
{
//arrange

//act
int result = prov_auth_set_registration_id(NULL, TEST_REGISTRATION_ID);

//assert
ASSERT_ARE_NOT_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());

//cleanup
}

TEST_FUNCTION(prov_auth_set_registration_id_registration_id_NULL_fail)
{
PROV_AUTH_HANDLE sec_handle = prov_auth_create();
umock_c_reset_all_calls();

//arrange

//act
int result = prov_auth_set_registration_id(sec_handle, NULL);

//assert
ASSERT_ARE_NOT_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());

//cleanup
prov_auth_destroy(sec_handle);
}

TEST_FUNCTION(prov_auth_set_registration_id_success)
{
PROV_AUTH_HANDLE sec_handle = prov_auth_create();
umock_c_reset_all_calls();

//arrange
STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG));

//act
int result = prov_auth_set_registration_id(sec_handle, TEST_REGISTRATION_ID);

//assert
ASSERT_ARE_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());

//cleanup
prov_auth_destroy(sec_handle);
}

TEST_FUNCTION(prov_auth_set_registration_id_2_calls_success)
{
PROV_AUTH_HANDLE sec_handle = prov_auth_create();
umock_c_reset_all_calls();

//arrange
STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG));

//act
int result = prov_auth_set_registration_id(sec_handle, TEST_REGISTRATION_ID);
ASSERT_ARE_EQUAL(int, 0, result);
result = prov_auth_set_registration_id(sec_handle, TEST_REGISTRATION_ID);

//assert
ASSERT_ARE_NOT_EQUAL(int, 0, result);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());

//cleanup
prov_auth_destroy(sec_handle);
}

TEST_FUNCTION(prov_auth_get_certificate_succeed)
{
STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
Expand Down

0 comments on commit 1239a17

Please sign in to comment.