Skip to content

Commit 8a0f5c0

Browse files
authored
Disable access TPM in memory interface (#1245)
* Disable access TPM in memory interface (#1059) Remove ability to configure an in memory TPM vs a HW TPM via an environment variable and added tests. This has caused failures when users have tried to use the in memory implementation which is useful only for testing. Essentially environment variable IOTEDGE_USE_TPM_DEVICE will be ignored by libiothsm and by default is built for use with a TPM device. To use the in memory implementation the library must be built using cmake flag USE_TEST_TPM_INTERFACE_IN_MEM. * Revert edge_hsm_sas_auth_int integration test to use public TPM API (#1087) b5f281b changed this test to use the `*tpm_store*` functions. But it fails to link on Windows since the functions are not available to be linked to. Since it's an integration test, it should be using the public libiothsm API anyway. This change reverts the test to use the public libiothsm API again.
1 parent f455ae2 commit 8a0f5c0

File tree

14 files changed

+319
-290
lines changed

14 files changed

+319
-290
lines changed

builds/checkin/libiothsm.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
displayName: Setup
2323
inputs:
2424
cwd: edgelet/hsm-sys/azure-iot-hsm-c/build
25-
cmakeArgs: -Drun_valgrind=ON -DBUILD_SHARED=ON -Drun_unittests=ON -Duse_emulator=OFF -Duse_http=OFF -DCMAKE_BUILD_TYPE=Release -DCPACK_DEBIAN_PACKAGE_RELEASE=$(Build.BuildNumber) ..
25+
cmakeArgs: -Drun_valgrind=ON -DBUILD_SHARED=ON -Drun_unittests=ON -Duse_emulator=OFF -Duse_http=OFF -DUSE_TEST_TPM_INTERFACE_IN_MEM=ON -DCMAKE_BUILD_TYPE=Release -DCPACK_DEBIAN_PACKAGE_RELEASE=$(Build.BuildNumber) ..
2626
- script: make package
2727
displayName: Build
2828
workingDirectory: edgelet/hsm-sys/azure-iot-hsm-c/build

builds/ci/libiothsm.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
displayName: Setup
2727
inputs:
2828
cwd: edgelet/hsm-sys/azure-iot-hsm-c/build
29-
cmakeArgs: -Drun_valgrind=ON -DBUILD_SHARED=ON -Drun_unittests=ON -Duse_emulator=OFF -Duse_http=OFF -DCMAKE_BUILD_TYPE=Release -DCPACK_DEBIAN_PACKAGE_RELEASE=$(Build.BuildNumber) ..
29+
cmakeArgs: -Drun_valgrind=ON -DBUILD_SHARED=ON -Drun_unittests=ON -Duse_emulator=OFF -Duse_http=OFF -DUSE_TEST_TPM_INTERFACE_IN_MEM=ON -DCMAKE_BUILD_TYPE=Release -DCPACK_DEBIAN_PACKAGE_RELEASE=$(Build.BuildNumber) ..
3030
- script: make package
3131
displayName: Build
3232
workingDirectory: edgelet/hsm-sys/azure-iot-hsm-c/build

edgelet/edgelet-hsm/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ hsm = { path = "../hsm-rs"}
1515
base64 = "0.9"
1616
hmac = "0.5.0"
1717
sha2 = "0.7.0"
18+
19+
hsm = { path = "../hsm-rs", features = ["in_memory"] }

edgelet/hsm-rs/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ authors = ["Azure IoT Edge Devs"]
77
chrono = "0.4"
88
hsm-sys = { path = "../hsm-sys"}
99
failure = "0.1"
10+
11+
[features]
12+
in_memory = ["hsm-sys/in_memory"]

edgelet/hsm-sys/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ cmake = "0.1"
1212

1313
[dev-dependencies]
1414
num_cpus = "1.0"
15+
16+
[features]
17+
in_memory = []

edgelet/hsm-sys/azure-iot-hsm-c/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*.a
77

88
cmake*/
9-
build/
9+
build*/
10+
1011

1112
# Doxygen output
1213
html/

edgelet/hsm-sys/azure-iot-hsm-c/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ include_directories(. ./inc)
1717
find_package(OpenSSL REQUIRED)
1818
include_directories(${OPENSSL_INCLUDE_DIR})
1919

20+
if(USE_TEST_TPM_INTERFACE_IN_MEM)
21+
add_definitions(-DTEST_TPM_INTERFACE_IN_MEM)
22+
endif(USE_TEST_TPM_INTERFACE_IN_MEM)
23+
2024
set(source_c_files
2125
./src/certificate_info.c
2226
./src/constants.c
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,39 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
//
4-
#include <stdlib.h>
5-
#include <ctype.h>
6-
#include <stdbool.h>
7-
#include "hsm_utils.h"
8-
#include "hsm_log.h"
4+
95
#include "hsm_client_tpm_device.h"
106
#include "hsm_client_tpm_in_mem.h"
117

12-
extern const char* const ENV_TPM_SELECT;
13-
14-
static int strcmp_i(const char* lhs, const char* rhs)
15-
{
16-
char lc, rc;
17-
int cmp = 0;
18-
do
19-
{
20-
lc = *lhs++;
21-
rc = *rhs++;
22-
if ((tolower(lc) - tolower(rc)) != 0)
23-
{
24-
cmp = 1;
25-
}
26-
} while (lc != 0 && rc != 0);
27-
28-
return cmp;
29-
}
30-
31-
// IF ENV_TPM_SELECT is set and not empty, "NO", "OFF" or "FALSE", then user wants to use the
32-
// TPM device for TPM functionality.
33-
static int use_tpm_device(bool *use_tpm)
34-
{
35-
static const char * user_says_no[] = { "", "off", "no", "false" };
36-
int array_size = sizeof(user_says_no)/sizeof(user_says_no[0]);
37-
int result;
38-
char * env_use_tpm;
39-
40-
*use_tpm = false;
41-
if (hsm_get_env(ENV_TPM_SELECT, &env_use_tpm) != 0)
42-
{
43-
LOG_ERROR("Could not lookup env variable %s", ENV_TPM_SELECT);
44-
result = __FAILURE__;
45-
}
46-
else
47-
{
48-
if (env_use_tpm != NULL)
49-
{
50-
*use_tpm = true;
51-
for(int no = 0; no < array_size; no++)
52-
{
53-
if (strcmp_i(env_use_tpm, user_says_no[no]) == 0)
54-
{
55-
*use_tpm = false;
56-
break;
57-
}
58-
}
59-
free(env_use_tpm);
60-
}
61-
else
62-
{
63-
*use_tpm = false;
64-
}
65-
result = 0;
66-
}
67-
68-
return result;
69-
}
70-
71-
static bool g_use_tpm_device = false;
72-
738
int hsm_client_tpm_init(void)
749
{
7510
int result;
76-
bool use_tpm_flag = false;
77-
78-
if (use_tpm_device(&use_tpm_flag) != 0)
79-
{
80-
result = __FAILURE__;
81-
}
82-
else
83-
{
84-
if (use_tpm_flag)
85-
{
86-
result = hsm_client_tpm_device_init();
87-
if (result == 0)
88-
{
89-
g_use_tpm_device = true;
90-
}
91-
}
92-
else
93-
{
94-
result = hsm_client_tpm_store_init();
95-
}
96-
}
11+
#ifdef TEST_TPM_INTERFACE_IN_MEM
12+
result = hsm_client_tpm_store_init();
13+
#else
14+
result = hsm_client_tpm_device_init();
15+
#endif
9716

9817
return result;
9918
}
10019

10120
void hsm_client_tpm_deinit(void)
10221
{
103-
if (g_use_tpm_device)
104-
{
105-
hsm_client_tpm_device_deinit();
106-
}
107-
else
108-
{
22+
#ifdef TEST_TPM_INTERFACE_IN_MEM
10923
hsm_client_tpm_store_deinit();
110-
}
24+
#else
25+
hsm_client_tpm_device_deinit();
26+
#endif
11127
}
11228

11329
const HSM_CLIENT_TPM_INTERFACE* hsm_client_tpm_interface(void)
11430
{
11531
const HSM_CLIENT_TPM_INTERFACE* result;
116-
if (g_use_tpm_device)
117-
{
118-
result = hsm_client_tpm_device_interface();
119-
}
120-
else
121-
{
32+
#ifdef TEST_TPM_INTERFACE_IN_MEM
12233
result = hsm_client_tpm_store_interface();
123-
}
34+
#else
35+
result = hsm_client_tpm_device_interface();
36+
#endif
37+
12438
return result;
12539
}

edgelet/hsm-sys/azure-iot-hsm-c/tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ endif(save_ut)
1616
set(SHARED_UTIL_REAL_TEST_FOLDER ${SHARED_UTIL_SRC_FOLDER}/../tests/real_test_files CACHE INTERNAL "this is what needs to be included when doing test sources" FORCE)
1717

1818
add_subdirectory(hsm_certificate_props_ut)
19+
add_subdirectory(hsm_tpm_select_ut)
1920
add_subdirectory(certificate_info_ut)
2021
add_subdirectory(edge_hsm_tpm_ut)
2122
add_subdirectory(edge_hsm_key_intf_sas_ut)
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
#Copyright (c) Microsoft. All rights reserved.
22
#Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
#this is CMakeLists.txt for edge_hsm_tpm_ut
4+
#this is CMakeLists.txt for hsm_tpm_select_ut
55
cmake_minimum_required(VERSION 2.8.11)
66

7-
compileAsC11()
8-
9-
set(theseTestsName hspm_tpm_select_ut)
10-
11-
include_directories(../../src ../test_utils)
7+
include_directories(../../src)
128

13-
add_definitions(-DGB_DEBUG_ALLOC)
9+
compileAsC11()
10+
set(theseTestsName hsm_tpm_select_ut)
1411

1512
set(${theseTestsName}_test_files
13+
${theseTestsName}.c
14+
)
15+
16+
set(${theseTestsName}_c_files
17+
../../src/hsm_client_tpm_select.c
1618
../../src/hsm_log.c
17-
../../src/hsm_utils.c
1819
../../src/constants.c
19-
../test_utils/test_utils.c
20-
${theseTestsName}.c
2120
)
2221

2322
set(${theseTestsName}_h_files
23+
../../src/hsm_client_tpm_device.h
24+
../../src/hsm_client_tpm_in_mem.h
2425
)
2526

26-
build_c_test_artifacts(${theseTestsName} ON "tests/azure_c_shared_utility_tests")
27-
28-
if(WIN32)
29-
target_link_libraries(${theseTestsName}_exe iothsm aziotsharedutil $ENV{OPENSSL_ROOT_DIR}/lib/ssleay32.lib $ENV{OPENSSL_ROOT_DIR}/lib/libeay32.lib)
30-
else()
31-
target_link_libraries(${theseTestsName}_exe iothsm aziotsharedutil ${OPENSSL_LIBRARIES})
32-
endif(WIN32)
33-
34-
copy_iothsm_dll(${theseTestsName}_exe ${CMAKE_CURRENT_BINARY_DIR}/$(Configuration))
27+
build_c_test_artifacts(${theseTestsName} ON "tests")

0 commit comments

Comments
 (0)