• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
MaskForStringId(const std::string & id)63 static std::string MaskForStringId(const std::string &id)
64 {
65     const int32_t maskWidth = 2;
66     const int32_t stringIdLength = 64;
67     if (id.length() != stringIdLength) {
68         return "****";
69     }
70     return id.substr(0, maskWidth) + "**" + id.substr(id.length() - maskWidth, id.length());
71 }
72 
ReportSystemFault(const std::string & timeString,const std::string & moduleName)73 void ReportSystemFault(const std::string &timeString, const std::string &moduleName)
74 {
75     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_SYSTEM_FAULT",
76         HiSysEvent::EventType::FAULT,
77         STR_HAPPEN_TIME, timeString,
78         STR_MODULE_NAME, moduleName);
79     if (ret != 0) {
80         IAM_LOGE("hisysevent write failed! ret %{public}d, timeString %{public}s, moduleName %{public}s.",
81             ret, timeString.c_str(), moduleName.c_str());
82     }
83 }
84 
ReportSecurityTemplateChange(const TemplateChangeTrace & info)85 void ReportSecurityTemplateChange(const TemplateChangeTrace &info)
86 {
87     std::string operationTime = Common::GetNowTimeString();
88     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_TEMPLATE_CHANGE",
89         HiSysEvent::EventType::SECURITY,
90         STR_OPERATION_TIME, operationTime,
91         STR_SCHEDULE_ID, static_cast<uint16_t>(info.scheduleId),
92         STR_EXECUTOR_TYPE, info.executorType,
93         STR_CHANGE_TYPE, info.changeType,
94         STR_TRIGGER_REASON, info.reason);
95     if (ret != 0) {
96         IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
97     }
98 }
99 
ReportBehaviorCredManager(const UserCredManagerTrace & info)100 void ReportBehaviorCredManager(const UserCredManagerTrace &info)
101 {
102     std::string operationTime = Common::GetNowTimeString();
103     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_USER_CREDENTIAL_MANAGER",
104         HiSysEvent::EventType::BEHAVIOR,
105         STR_OPERATION_TIME, operationTime,
106         STR_CALLER_NAME, info.callerName,
107         STR_USER_ID, info.userId,
108         STR_AUTH_TYPE, info.authType,
109         STR_OPERATION_TYPE, info.operationType,
110         STR_OPERATION_RESULT, info.operationResult);
111     if (ret != 0) {
112             IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
113     }
114 }
115 
ReportSecurityCredChange(const UserCredChangeTrace & info)116 void ReportSecurityCredChange(const UserCredChangeTrace &info)
117 {
118     std::string operationTime = Common::GetNowTimeString();
119     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_CREDENTIAL_CHANGE",
120         HiSysEvent::EventType::SECURITY,
121         STR_OPERATION_TIME, operationTime,
122         STR_CALLER_NAME, info.callerName,
123         STR_REQUEST_CONTEXTID, static_cast<uint16_t>(info.requestContextId),
124         STR_USER_ID, info.userId,
125         STR_AUTH_TYPE, info.authType,
126         STR_OPERATION_TYPE, info.operationType,
127         STR_OPERATION_RESULT, info.operationResult,
128         STR_TIME_SPAN, info.timeSpan);
129     if (ret != 0) {
130         IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
131     }
132 }
133 
ReportUserAuth(const UserAuthTrace & info)134 void ReportUserAuth(const UserAuthTrace &info)
135 {
136     std::string operationTime = Common::GetNowTimeString();
137     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_USER_AUTH",
138         HiSysEvent::EventType::BEHAVIOR,
139         STR_OPERATION_TIME, operationTime,
140         STR_CALLER_NAME, info.callerName,
141         STR_SDK_VERSION, info.sdkVersion,
142         STR_AUTH_TRUST_LEVEL, info.atl,
143         STR_AUTH_TYPE, info.authType,
144         STR_AUTH_RESULT, info.authResult,
145         STR_AUTH_TIME_SPAN, info.authtimeSpan,
146         STR_AUTH_WIDGET_TYPE, info.authWidgetType,
147         STR_REUSE_UNLOCK_RESULT_TYPE, info.reuseUnlockResultMode,
148         STR_REUSE_UNLOCK_RESULT_DURATION, info.reuseUnlockResultDuration,
149         STR_IS_REMOTE_AUTH, info.isRemoteAuth,
150         STR_LOCAL_UDID, MaskForStringId(info.localUdid),
151         STR_REMOTE_UDID, MaskForStringId(info.remoteUdid),
152         STR_CONNECTION_NAME, info.connectionName,
153         STR_AUTH_FINISH_REASON, info.authFinishReason,
154         STR_IS_BACKGROUND_APPLICATION, info.isBackgroundApplication);
155     if (ret != 0) {
156         IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
157     }
158 }
159 
ReportSecurityUserAuthFwk(const UserAuthFwkTrace & info)160 void ReportSecurityUserAuthFwk(const UserAuthFwkTrace &info)
161 {
162     std::string operationTime = Common::GetNowTimeString();
163     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_USER_AUTH_FWK",
164         HiSysEvent::EventType::SECURITY,
165         STR_OPERATION_TIME, operationTime,
166         STR_CALLER_NAME, info.callerName,
167         STR_REQUEST_CONTEXTID, static_cast<uint16_t>(info.requestContextId),
168         STR_AUTH_CONTEXTID, static_cast<uint16_t>(info.authContextId),
169         STR_AUTH_TRUST_LEVEL, info.atl,
170         STR_AUTH_TYPE, info.authType,
171         STR_AUTH_RESULT, info.authResult,
172         STR_AUTH_TIME_SPAN, info.authtimeSpan,
173         STR_IS_REMOTE_AUTH, info.isRemoteAuth,
174         STR_LOCAL_UDID, MaskForStringId(info.localUdid),
175         STR_REMOTE_UDID, MaskForStringId(info.remoteUdid),
176         STR_CONNECTION_NAME, info.connectionName,
177         STR_AUTH_FINISH_REASON, info.authFinishReason);
178     if (ret != 0) {
179         IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
180     }
181 }
182 
ReportRemoteExecuteProc(const RemoteExecuteTrace & info)183 void ReportRemoteExecuteProc(const RemoteExecuteTrace &info)
184 {
185     std::string operationTime = Common::GetNowTimeString();
186     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_REMOTE_EXECUTE",
187         HiSysEvent::EventType::SECURITY,
188         STR_OPERATION_TIME, operationTime,
189         STR_SCHEDULE_ID, static_cast<uint16_t>(info.scheduleId),
190         STR_CONNECTION_NAME, info.connectionName,
191         STR_OPERATION_RESULT, info.operationResult);
192     if (ret != 0) {
193         IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
194     }
195 }
196 
ReportRemoteConnectOpen(const RemoteConnectOpenTrace & info)197 void ReportRemoteConnectOpen(const RemoteConnectOpenTrace &info)
198 {
199     std::string operationTime = Common::GetNowTimeString();
200     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_REMOTE_CONNECT",
201         HiSysEvent::EventType::SECURITY,
202         STR_OPERATION_TIME, operationTime,
203         STR_CONNECTION_NAME, info.connectionName,
204         STR_OPERATION_RESULT, info.operationResult,
205         STR_TIME_SPAN, info.timeSpan,
206         STR_NETWORK_ID, MaskForStringId(info.networkId),
207         STR_SOCKET_ID, static_cast<uint16_t>(info.socketId));
208     if (ret != 0) {
209         IAM_LOGE("hisysevent write failed! ret %{public}d", ret);
210     }
211 }
212 
ReportConnectFaultTrace(const RemoteConnectFaultTrace & info)213 void ReportConnectFaultTrace(const RemoteConnectFaultTrace &info)
214 {
215     std::ostringstream ss;
216     ss << "reason: " << info.reason << ", socketId: " << static_cast<uint16_t>(info.socketId)
217         << ", connectionName: " << info.connectionName << ", msgType:" << info.msgType
218         << ", messageSeq" << "ack:" << info.ack;
219     ReportSystemFault(Common::GetNowTimeString(), ss.str());
220 }
221 } // namespace UserAuth
222 } // namespace UserIam
223 } // namespace OHOS