1 /*
2 * Copyright (c) 2022-2023 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 "hisysevent_adapter.h"
17
18 #include <cinttypes>
19
20 #include "hisysevent.h"
21 #include "iam_logger.h"
22 #include "iam_para2str.h"
23 #include "iam_time.h"
24
25 #define LOG_TAG "USER_AUTH_SA"
26
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 using HiSysEvent = OHOS::HiviewDFX::HiSysEvent;
31
32 constexpr char STR_USER_ID[] = "USER_ID";
33 constexpr char STR_AUTH_TYPE[] = "AUTH_TYPE";
34 constexpr char STR_OPERATION_TYPE[] = "OPERATION_TYPE";
35 constexpr char STR_OPERATION_RESULT[] = "OPERATION_RESULT";
36 constexpr char STR_AUTH_RESULT[] = "AUTH_RESULT";
37 constexpr char STR_TRIGGER_REASON[] = "TRIGGER_REASON";
38 constexpr char STR_CHANGE_TYPE[] = "CHANGE_TYPE";
39 constexpr char STR_EXECUTOR_TYPE[] = "EXECUTOR_TYPE";
40 constexpr char STR_MODULE_NAME[] = "MODULE_NAME";
41 constexpr char STR_HAPPEN_TIME[] = "HAPPEN_TIME";
42 constexpr char STR_AUTH_TRUST_LEVEL[] = "AUTH_TRUST_LEVEL";
43 constexpr char STR_SDK_VERSION[] = "SDK_VERSION";
44 constexpr char STR_AUTH_WIDGET_TYPE[] = "AUTH_WIDGET_TYPE";
45 constexpr char STR_CALLER_NAME[] = "CALLER_NAME";
46 constexpr char STR_REQUEST_CONTEXTID[] = "REQUEST_CONTEXTID";
47 constexpr char STR_TIME_SPAN[] = "TIME_SPAN";
48 constexpr char STR_AUTH_TIME_SPAN[] = "AUTH_TIME_SPAN";
49 constexpr char STR_AUTH_CONTEXTID[] = "AUTH_CONTEXTID";
50 constexpr char STR_SCHEDULE_ID[] = "SCHEDULE_ID";
51 constexpr char STR_REUSE_UNLOCK_RESULT_TYPE[] = "REUSE_UNLOCK_RESULT_TYPE";
52 constexpr char STR_REUSE_UNLOCK_RESULT_DURATION[] = "REUSE_UNLOCK_RESULT_DURATION";
53 constexpr char STR_IS_REMOTE_AUTH[] = "IS_REMOTE_AUTH";
54 constexpr char STR_LOCAL_UDID[] = "LOCAL_UDID";
55 constexpr char STR_REMOTE_UDID[] = "REMOTE_UDID";
56 constexpr char STR_CONNECTION_NAME[] = "CONNECTION_NAME";
57 constexpr char STR_NETWORK_ID[] = "NETWORK_ID";
58 constexpr char STR_SOCKET_ID[] = "SOCKET_ID";
59 constexpr char STR_AUTH_FINISH_REASON[] = "AUTH_FINISH_REASON";
60 constexpr char STR_OPERATION_TIME[] = "OPERATION_TIME";
61 constexpr char STR_IS_BACKGROUND_APPLICATION[] = "IS_BACKGROUND_APPLICATION";
62 constexpr char STR_ERROR_CODE[] = "ERROR_CODE";
63 constexpr char STR_PREVIOUS_STATUS[] = "PREVIOUS_STATUS";
64 constexpr char STR_UPDATED_STATUS[] = "UPDATED_STATUS";
65 constexpr char STRONG_AUTH_REASON[] = "STRONG_AUTH_REASON";
66
MaskForStringId(const std::string & id)67 static std::string MaskForStringId(const std::string &id)
68 {
69 const int32_t maskWidth = 2;
70 const int32_t stringIdLength = 64;
71 if (id.length() != stringIdLength) {
72 return "****";
73 }
74 return id.substr(0, maskWidth) + "**" + id.substr(id.length() - maskWidth, id.length());
75 }
76
ReportSystemFault(const std::string & timeString,const std::string & moduleName)77 void ReportSystemFault(const std::string &timeString, const std::string &moduleName)
78 {
79 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_SYSTEM_FAULT",
80 HiSysEvent::EventType::FAULT,
81 STR_HAPPEN_TIME, timeString,
82 STR_MODULE_NAME, moduleName);
83 if (ret != 0) {
84 IAM_LOGE("hisysevent write failed! ret %{public}d, timeString %{public}s, moduleName %{public}s.",
85 ret, timeString.c_str(), moduleName.c_str());
86 }
87 }
88
ReportSecurityTemplateChange(const TemplateChangeTrace & info)89 void ReportSecurityTemplateChange(const TemplateChangeTrace &info)
90 {
91 std::string operationTime = Common::GetNowTimeString();
92 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_TEMPLATE_CHANGE",
93 HiSysEvent::EventType::SECURITY,
94 STR_OPERATION_TIME, operationTime,
95 STR_SCHEDULE_ID, static_cast<uint16_t>(info.scheduleId),
96 STR_EXECUTOR_TYPE, info.executorType,
97 STR_CHANGE_TYPE, info.changeType,
98 STR_TRIGGER_REASON, info.reason);
99 if (ret != 0) {
100 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
101 }
102 }
103
ReportBehaviorCredManager(const UserCredManagerTrace & info)104 void ReportBehaviorCredManager(const UserCredManagerTrace &info)
105 {
106 std::string operationTime = Common::GetNowTimeString();
107 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_USER_CREDENTIAL_MANAGER",
108 HiSysEvent::EventType::BEHAVIOR,
109 STR_OPERATION_TIME, operationTime,
110 STR_CALLER_NAME, info.callerName,
111 STR_USER_ID, info.userId,
112 STR_AUTH_TYPE, info.authType,
113 STR_OPERATION_TYPE, info.operationType,
114 STR_OPERATION_RESULT, info.operationResult);
115 if (ret != 0) {
116 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
117 }
118 }
119
ReportSecurityCredChange(const UserCredChangeTrace & info)120 void ReportSecurityCredChange(const UserCredChangeTrace &info)
121 {
122 std::string operationTime = Common::GetNowTimeString();
123 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_CREDENTIAL_CHANGE",
124 HiSysEvent::EventType::SECURITY,
125 STR_OPERATION_TIME, operationTime,
126 STR_CALLER_NAME, info.callerName,
127 STR_REQUEST_CONTEXTID, static_cast<uint16_t>(info.requestContextId),
128 STR_USER_ID, info.userId,
129 STR_AUTH_TYPE, info.authType,
130 STR_OPERATION_TYPE, info.operationType,
131 STR_OPERATION_RESULT, info.operationResult,
132 STR_TIME_SPAN, info.timeSpan);
133 if (ret != 0) {
134 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
135 }
136 }
137
ReportUserAuth(const UserAuthTrace & info)138 void ReportUserAuth(const UserAuthTrace &info)
139 {
140 std::string operationTime = Common::GetNowTimeString();
141 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_USER_AUTH",
142 HiSysEvent::EventType::BEHAVIOR,
143 STR_OPERATION_TIME, operationTime,
144 STR_CALLER_NAME, info.callerName,
145 STR_SDK_VERSION, info.sdkVersion,
146 STR_AUTH_TRUST_LEVEL, info.atl,
147 STR_AUTH_TYPE, info.authType,
148 STR_AUTH_RESULT, info.authResult,
149 STR_AUTH_TIME_SPAN, info.authtimeSpan,
150 STR_AUTH_WIDGET_TYPE, info.authWidgetType,
151 STR_REUSE_UNLOCK_RESULT_TYPE, info.reuseUnlockResultMode,
152 STR_REUSE_UNLOCK_RESULT_DURATION, info.reuseUnlockResultDuration,
153 STR_IS_REMOTE_AUTH, info.isRemoteAuth,
154 STR_LOCAL_UDID, MaskForStringId(info.localUdid),
155 STR_REMOTE_UDID, MaskForStringId(info.remoteUdid),
156 STR_CONNECTION_NAME, info.connectionName,
157 STR_AUTH_FINISH_REASON, info.authFinishReason,
158 STR_IS_BACKGROUND_APPLICATION, info.isBackgroundApplication);
159 if (ret != 0) {
160 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
161 }
162 }
163
ReportSecurityUserAuthFwk(const UserAuthFwkTrace & info)164 void ReportSecurityUserAuthFwk(const UserAuthFwkTrace &info)
165 {
166 std::string operationTime = Common::GetNowTimeString();
167 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_USER_AUTH_FWK",
168 HiSysEvent::EventType::SECURITY,
169 STR_OPERATION_TIME, operationTime,
170 STR_CALLER_NAME, info.callerName,
171 STR_REQUEST_CONTEXTID, static_cast<uint16_t>(info.requestContextId),
172 STR_AUTH_CONTEXTID, static_cast<uint16_t>(info.authContextId),
173 STR_AUTH_TRUST_LEVEL, info.atl,
174 STR_AUTH_TYPE, info.authType,
175 STR_AUTH_RESULT, info.authResult,
176 STR_AUTH_TIME_SPAN, info.authtimeSpan,
177 STR_IS_REMOTE_AUTH, info.isRemoteAuth,
178 STR_LOCAL_UDID, MaskForStringId(info.localUdid),
179 STR_REMOTE_UDID, MaskForStringId(info.remoteUdid),
180 STR_CONNECTION_NAME, info.connectionName,
181 STR_AUTH_FINISH_REASON, info.authFinishReason);
182 if (ret != 0) {
183 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
184 }
185 }
186
ReportRemoteExecuteProc(const RemoteExecuteTrace & info)187 void ReportRemoteExecuteProc(const RemoteExecuteTrace &info)
188 {
189 std::string operationTime = Common::GetNowTimeString();
190 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_REMOTE_EXECUTE",
191 HiSysEvent::EventType::SECURITY,
192 STR_OPERATION_TIME, operationTime,
193 STR_SCHEDULE_ID, static_cast<uint16_t>(info.scheduleId),
194 STR_CONNECTION_NAME, info.connectionName,
195 STR_OPERATION_RESULT, info.operationResult);
196 if (ret != 0) {
197 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
198 }
199 }
200
ReportRemoteConnectOpen(const RemoteConnectOpenTrace & info)201 void ReportRemoteConnectOpen(const RemoteConnectOpenTrace &info)
202 {
203 std::string operationTime = Common::GetNowTimeString();
204 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_REMOTE_CONNECT",
205 HiSysEvent::EventType::SECURITY,
206 STR_OPERATION_TIME, operationTime,
207 STR_CONNECTION_NAME, info.connectionName,
208 STR_OPERATION_RESULT, info.operationResult,
209 STR_TIME_SPAN, info.timeSpan,
210 STR_NETWORK_ID, MaskForStringId(info.networkId),
211 STR_SOCKET_ID, static_cast<uint16_t>(info.socketId));
212 if (ret != 0) {
213 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
214 }
215 }
216
ReportConnectFaultTrace(const RemoteConnectFaultTrace & info)217 void ReportConnectFaultTrace(const RemoteConnectFaultTrace &info)
218 {
219 std::ostringstream ss;
220 ss << "reason: " << info.reason << ", socketId: " << static_cast<uint16_t>(info.socketId)
221 << ", connectionName: " << info.connectionName << ", msgType:" << info.msgType
222 << ", messageSeq" << "ack:" << info.ack;
223 ReportSystemFault(Common::GetNowTimeString(), ss.str());
224 }
225
ReportSaLoadDriverFailure(const SaLoadDriverFailureTrace & info)226 void ReportSaLoadDriverFailure(const SaLoadDriverFailureTrace &info)
227 {
228 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "SA_LOAD_DRIVER_FAILURE",
229 HiSysEvent::EventType::FAULT,
230 STR_ERROR_CODE, info.errCode);
231 if (ret != 0) {
232 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
233 }
234 }
235
ReportIsCredentialEnrolledMismatch(const IsCredentialEnrolledMismatchTrace & info)236 void ReportIsCredentialEnrolledMismatch(const IsCredentialEnrolledMismatchTrace &info)
237 {
238 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "IS_CREDENTIAL_ENROLLED_MISMATCH",
239 HiSysEvent::EventType::STATISTIC,
240 STR_AUTH_TYPE, info.authType,
241 STR_PREVIOUS_STATUS, info.preStatus,
242 STR_UPDATED_STATUS, info.updatedStatus);
243 if (ret != 0) {
244 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
245 }
246 }
247
ReportScreenLockStrongAuth(const ScreenLockStrongAuthTrace & info)248 void ReportScreenLockStrongAuth(const ScreenLockStrongAuthTrace &info)
249 {
250 int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "SCREENLOCK_STRONG_AUTH",
251 HiSysEvent::EventType::SECURITY,
252 STR_USER_ID, info.userId,
253 STRONG_AUTH_REASON, info.strongAuthReason);
254 if (ret != 0) {
255 IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
256 }
257 }
258 } // namespace UserAuth
259 } // namespace UserIam
260 } // namespace OHOS