source: azure_iot_hub_riscv/trunk/azure_iot_sdk/provisioning_client/src/iothub_security_factory.c@ 453

Last change on this file since 453 was 453, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 5.0 KB
Line 
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#include <stdlib.h>
5#include "azure_prov_client/iothub_security_factory.h"
6#include "azure_prov_client/prov_security_factory.h"
7#include "azure_c_shared_utility/xlogging.h"
8#include "azure_c_shared_utility/crt_abstractions.h"
9
10#include "hsm_client_data.h"
11
12static IOTHUB_SECURITY_TYPE g_security_type = IOTHUB_SECURITY_TYPE_UNKNOWN;
13static char* g_symm_key = NULL;
14static char* g_symm_key_reg_name = NULL;
15
16static SECURE_DEVICE_TYPE get_secure_device_type(IOTHUB_SECURITY_TYPE sec_type)
17{
18 SECURE_DEVICE_TYPE ret;
19
20 switch (sec_type)
21 {
22#if defined(HSM_TYPE_SAS_TOKEN) || defined(HSM_AUTH_TYPE_CUSTOM)
23 case IOTHUB_SECURITY_TYPE_SAS:
24 ret = SECURE_DEVICE_TYPE_TPM;
25 break;
26#endif
27
28#if defined(HSM_TYPE_X509) || defined(HSM_AUTH_TYPE_CUSTOM)
29 case IOTHUB_SECURITY_TYPE_X509:
30 ret = SECURE_DEVICE_TYPE_X509;
31 break;
32#endif
33
34#if defined(HSM_TYPE_SYMM_KEY) || defined(HSM_AUTH_TYPE_CUSTOM)
35 case IOTHUB_SECURITY_TYPE_SYMMETRIC_KEY:
36 ret = SECURE_DEVICE_TYPE_SYMMETRIC_KEY;
37 break;
38#endif
39
40#ifdef HSM_TYPE_HTTP_EDGE
41 case IOTHUB_SECURITY_TYPE_HTTP_EDGE:
42 ret = SECURE_DEVICE_TYPE_HTTP_EDGE;
43 break;
44#endif
45
46 default:
47 ret = SECURE_DEVICE_TYPE_UNKNOWN;
48 break;
49 }
50
51 return ret;
52}
53
54int iothub_security_init(IOTHUB_SECURITY_TYPE sec_type)
55{
56 int result;
57
58 SECURE_DEVICE_TYPE secure_device_type_from_caller = get_secure_device_type(sec_type);
59
60 if (secure_device_type_from_caller == SECURE_DEVICE_TYPE_UNKNOWN)
61 {
62 LogError("Security type %d is not supported in this SDK build", sec_type);
63 result = MU_FAILURE;
64 }
65 else
66 {
67 g_security_type = sec_type;
68 SECURE_DEVICE_TYPE security_device_type_from_prov = prov_dev_security_get_type();
69 if (security_device_type_from_prov == SECURE_DEVICE_TYPE_UNKNOWN)
70 {
71 result = prov_dev_security_init(secure_device_type_from_caller);
72 }
73 else if (secure_device_type_from_caller != security_device_type_from_prov)
74 {
75 LogError("Security type from caller %d (which maps to security device type %d) does not match already specified security device type %d", sec_type, secure_device_type_from_caller, security_device_type_from_prov);
76 result = MU_FAILURE;
77 }
78 else
79 {
80 result = 0;
81 }
82
83 if (result == 0)
84 {
85 result = initialize_hsm_system();
86 }
87 }
88 return result;
89}
90
91void iothub_security_deinit()
92{
93 if (g_symm_key != NULL)
94 {
95 free(g_symm_key);
96 g_symm_key = NULL;
97 }
98 if (g_symm_key_reg_name != NULL)
99 {
100 free(g_symm_key_reg_name);
101 g_symm_key_reg_name = NULL;
102 }
103 deinitialize_hsm_system();
104 if (prov_dev_get_symmetric_key() != NULL || prov_dev_get_symm_registration_name() != NULL)
105 {
106 prov_dev_security_deinit();
107 }
108}
109
110IOTHUB_SECURITY_TYPE iothub_security_type()
111{
112 return g_security_type;
113}
114
115int iothub_security_set_symmetric_key_info(const char* registration_name, const char* symmetric_key)
116{
117 int result;
118 if (registration_name == NULL || symmetric_key == NULL)
119 {
120 LogError("Invalid parameter specified reg_name: %p, symm_key: %p", registration_name, symmetric_key);
121 result = MU_FAILURE;
122 }
123 else
124 {
125 char* temp_key;
126 char* temp_name;
127 if (mallocAndStrcpy_s(&temp_name, registration_name) != 0)
128 {
129 LogError("Failure allocating registration name");
130 result = MU_FAILURE;
131 }
132 else if (mallocAndStrcpy_s(&temp_key, symmetric_key) != 0)
133 {
134 LogError("Failure allocating symmetric key");
135 free(temp_name);
136 result = MU_FAILURE;
137 }
138 else
139 {
140 if (g_symm_key != NULL)
141 {
142 free(g_symm_key);
143 }
144 if (g_symm_key_reg_name != NULL)
145 {
146 free(g_symm_key_reg_name);
147 }
148 g_symm_key_reg_name = temp_name;
149 g_symm_key = temp_key;
150
151 // Sync iothub with dps
152 if (prov_dev_get_symmetric_key() == NULL || prov_dev_get_symm_registration_name() == NULL)
153 {
154 if (prov_dev_set_symmetric_key_info(g_symm_key_reg_name, g_symm_key) != 0)
155 {
156 LogError("Failure syncing dps & IoThub key information");
157 result = MU_FAILURE;
158 }
159 else
160 {
161 result = 0;
162 }
163 }
164 else
165 {
166 result = 0;
167 }
168 }
169 }
170 return result;
171}
172
173const char* iothub_security_get_symmetric_key()
174{
175 return g_symm_key;
176}
177
178const char* iothub_security_get_symm_registration_name()
179{
180 return g_symm_key_reg_name;
181}
Note: See TracBrowser for help on using the repository browser.