|
1 | 1 | // Copyright (c) Microsoft. All rights reserved.
|
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
3 | 3 | //
|
4 |
| -#include <stdlib.h> |
5 |
| -#include <ctype.h> |
6 |
| -#include <stdbool.h> |
7 |
| -#include "hsm_utils.h" |
8 |
| -#include "hsm_log.h" |
| 4 | + |
9 | 5 | #include "hsm_client_tpm_device.h"
|
10 | 6 | #include "hsm_client_tpm_in_mem.h"
|
11 | 7 |
|
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 |
| - |
73 | 8 | int hsm_client_tpm_init(void)
|
74 | 9 | {
|
75 | 10 | 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 |
97 | 16 |
|
98 | 17 | return result;
|
99 | 18 | }
|
100 | 19 |
|
101 | 20 | void hsm_client_tpm_deinit(void)
|
102 | 21 | {
|
103 |
| - if (g_use_tpm_device) |
104 |
| - { |
105 |
| - hsm_client_tpm_device_deinit(); |
106 |
| - } |
107 |
| - else |
108 |
| - { |
| 22 | + #ifdef TEST_TPM_INTERFACE_IN_MEM |
109 | 23 | hsm_client_tpm_store_deinit();
|
110 |
| - } |
| 24 | + #else |
| 25 | + hsm_client_tpm_device_deinit(); |
| 26 | + #endif |
111 | 27 | }
|
112 | 28 |
|
113 | 29 | const HSM_CLIENT_TPM_INTERFACE* hsm_client_tpm_interface(void)
|
114 | 30 | {
|
115 | 31 | 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 |
122 | 33 | result = hsm_client_tpm_store_interface();
|
123 |
| - } |
| 34 | + #else |
| 35 | + result = hsm_client_tpm_device_interface(); |
| 36 | + #endif |
| 37 | + |
124 | 38 | return result;
|
125 | 39 | }
|
0 commit comments