1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "device_security_info.h"
17
18 #include "ohos_types.h"
19 #include "utils_log.h"
20 #include "utils_mem.h"
21 #include "utils_mutex.h"
22
23 #include "device_security_level_defines.h"
24 #include "device_security_level_proxy.h"
25
26 #define DEFAULT_KEEP_LEN 45
27 #define MAX_KEEP_LEN 300
28
29 DslmClientProxy *GetClientProxy(void);
30 void ReleaseClientProxy(DslmClientProxy *clientProxy);
31
GetMutex(void)32 static inline Mutex *GetMutex(void)
33 {
34 static Mutex mutex = INITED_MUTEX;
35 return &mutex;
36 }
37
RequestDeviceSecurityInfoImpl(const DeviceIdentify * identify,const RequestOption * option,DeviceSecurityInfo ** info)38 static int32_t RequestDeviceSecurityInfoImpl(const DeviceIdentify *identify, const RequestOption *option,
39 DeviceSecurityInfo **info)
40 {
41 return ERR_IPC_ERR;
42 }
43
RequestDeviceSecurityInfoAsyncImpl(const DeviceIdentify * identify,const RequestOption * option,DeviceSecurityInfoCallback callback)44 static int32_t RequestDeviceSecurityInfoAsyncImpl(const DeviceIdentify *identify, const RequestOption *option,
45 DeviceSecurityInfoCallback callback)
46 {
47 static uint32_t generated = 0;
48 if (identify == NULL || callback == NULL) {
49 SECURITY_LOG_ERROR("GetDeviceSecurityInfo input error");
50 return ERR_INVALID_PARA;
51 }
52
53 static RequestOption defaultOption = {0, DEFAULT_KEEP_LEN, 0};
54 if (option == NULL) {
55 option = &defaultOption;
56 }
57 if (option->timeout > MAX_KEEP_LEN) {
58 SECURITY_LOG_ERROR("GetDeviceSecurityInfo input error, timeout too long");
59 return ERR_INVALID_PARA;
60 }
61
62 DslmClientProxy *proxy = GetClientProxy();
63 if (proxy == NULL) {
64 SECURITY_LOG_ERROR("[GetFeatureApi S:%s F:%s]: failed", DSLM_SAMGR_SERVICE, DSLM_SAMGR_FEATURE);
65 return ERR_IPC_PROXY_ERR;
66 }
67
68 if (proxy->DslmIpcAsyncCall == NULL) {
69 SECURITY_LOG_ERROR("proxy has NULL api");
70 return ERR_IPC_PROXY_ERR;
71 }
72
73 LockMutex(GetMutex());
74 uint32_t cookie = ++generated;
75 UnlockMutex(GetMutex());
76 BOOL result = proxy->DslmIpcAsyncCall((IUnknown *)proxy, *identify, *option, cookie, callback);
77 if (result != SUCCESS) {
78 SECURITY_LOG_ERROR("GetDeviceSecurityInfo RequestDeviceSecurityLevel error");
79 return result;
80 }
81 SECURITY_LOG_INFO("GetDeviceSecurityInfo RequestDeviceSecurityLevel success");
82 ReleaseClientProxy(proxy);
83
84 return SUCCESS;
85 }
86
FreeDeviceSecurityInfoImpl(DeviceSecurityInfo * info)87 static void FreeDeviceSecurityInfoImpl(DeviceSecurityInfo *info)
88 {
89 if (info != NULL && info->magicNum == SECURITY_MAGIC) {
90 info->magicNum = 0;
91 FREE(info);
92 }
93 }
94
GetDeviceSecurityLevelValueImpl(const DeviceSecurityInfo * info,int32_t * level)95 static int32_t GetDeviceSecurityLevelValueImpl(const DeviceSecurityInfo *info, int32_t *level)
96 {
97 if (info == NULL || level == NULL) {
98 return ERR_INVALID_PARA;
99 }
100 if (info->magicNum != SECURITY_MAGIC) {
101 return ERR_INVALID_PARA;
102 }
103
104 *level = (int32_t)(info->level);
105 return (int32_t)(info->result);
106 }
107
RequestDeviceSecurityInfo(const DeviceIdentify * identify,const RequestOption * option,DeviceSecurityInfo ** info)108 int32_t RequestDeviceSecurityInfo(const DeviceIdentify *identify, const RequestOption *option,
109 DeviceSecurityInfo **info)
110 {
111 return RequestDeviceSecurityInfoImpl(identify, option, info);
112 }
113
RequestDeviceSecurityInfoAsync(const DeviceIdentify * identify,const RequestOption * option,DeviceSecurityInfoCallback callback)114 int32_t RequestDeviceSecurityInfoAsync(const DeviceIdentify *identify, const RequestOption *option,
115 DeviceSecurityInfoCallback callback)
116 {
117 return RequestDeviceSecurityInfoAsyncImpl(identify, option, callback);
118 }
119
FreeDeviceSecurityInfo(DeviceSecurityInfo * info)120 void FreeDeviceSecurityInfo(DeviceSecurityInfo *info)
121 {
122 return FreeDeviceSecurityInfoImpl(info);
123 }
124
GetDeviceSecurityLevelValue(const DeviceSecurityInfo * info,int32_t * level)125 int32_t GetDeviceSecurityLevelValue(const DeviceSecurityInfo *info, int32_t *level)
126 {
127 return GetDeviceSecurityLevelValueImpl(info, level);
128 }
129