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 
24 #define LOG_TAG "USER_AUTH_SA"
25 
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29 using HiSysEvent = OHOS::HiviewDFX::HiSysEvent;
30 
31 constexpr char STR_USER_ID[] = "USER_ID";
32 constexpr char STR_AUTH_TYPE[] = "AUTH_TYPE";
33 constexpr char STR_OPERATION_TYPE[] = "OPERATION_TYPE";
34 constexpr char STR_OPERATION_RESULT[] = "OPERATION_RESULT";
35 constexpr char STR_AUTH_RESULT[] = "AUTH_RESULT";
36 constexpr char STR_TRIGGER_REASON[] = "TRIGGER_REASON";
37 constexpr char STR_CHANGE_TYPE[] = "CHANGE_TYPE";
38 constexpr char STR_EXECUTOR_TYPE[] = "EXECUTOR_TYPE";
39 constexpr char STR_MODULE_NAME[] = "MODULE_NAME";
40 constexpr char STR_HAPPEN_TIME[] = "HAPPEN_TIME";
41 constexpr char STR_AUTH_TRUST_LEVEL[] = "AUTH_TRUST_LEVEL";
42 constexpr char STR_SDK_VERSION[] = "SDK_VERSION";
43 constexpr char STR_AUTH_WIDGET_TYPE[] = "AUTH_WIDGET_TYPE";
44 constexpr char STR_CALLER_NAME[] = "CALLER_NAME";
45 constexpr char STR_REQUEST_CONTEXTID[] = "REQUEST_CONTEXTID";
46 constexpr char STR_TIME_SPAN[] = "TIME_SPAN";
47 constexpr char STR_AUTH_TIME_SPAN[] = "AUTH_TIME_SPAN";
48 constexpr char STR_AUTH_CONTEXTID[] = "AUTH_CONTEXTID";
49 constexpr char STR_SCHEDULE_ID[] = "SCHEDULE_ID";
50 constexpr char STR_REUSE_UNLOCK_RESULT_TYPE[] = "REUSE_UNLOCK_RESULT_TYPE";
51 constexpr char STR_REUSE_UNLOCK_RESULT_DURATION[] = "REUSE_UNLOCK_RESULT_DURATION";
52 constexpr char STR_IS_BACKGROUND_APPLICATION[] = "IS_BACKGROUND_APPLICATION";
53 
ReportSystemFault(const std::string & timeString,const std::string & moudleName)54 void ReportSystemFault(const std::string &timeString, const std::string &moudleName)
55 {
56     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_SYSTEM_FAULT",
57         HiSysEvent::EventType::FAULT,
58         STR_HAPPEN_TIME, timeString,
59         STR_MODULE_NAME, moudleName);
60     if (ret != 0) {
61         IAM_LOGE("hisysevent write failed! ret %{public}d, timeString %{public}s, moudleName %{public}s.",
62             ret, timeString.c_str(), moudleName.c_str());
63     }
64 }
65 
ReportSecurityTemplateChange(const TemplateChangeTrace & info)66 void ReportSecurityTemplateChange(const TemplateChangeTrace &info)
67 {
68     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_TEMPLATE_CHANGE",
69         HiSysEvent::EventType::SECURITY,
70         STR_SCHEDULE_ID, info.scheduleId,
71         STR_EXECUTOR_TYPE, info.executorType,
72         STR_CHANGE_TYPE, info.changeType,
73         STR_TRIGGER_REASON, info.reason);
74     if (ret != 0) {
75         IAM_LOGE("hisysevent write failed! ret %{public}d, executorType %{public}d, changeType %{public}u,"
76             "scheduleId %{public}s, trigger reason %{public}s.", ret, info.executorType, info.changeType,
77             GET_MASKED_STRING(info.scheduleId).c_str(), info.reason.c_str());
78     }
79 }
80 
ReportBehaviorCredManager(const UserCredManagerTrace & info)81 void ReportBehaviorCredManager(const UserCredManagerTrace &info)
82 {
83     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_USER_CREDENTIAL_MANAGER",
84         HiSysEvent::EventType::BEHAVIOR,
85         STR_CALLER_NAME, info.callerName,
86         STR_USER_ID, info.userId,
87         STR_AUTH_TYPE, info.authType,
88         STR_OPERATION_TYPE, info.operationType,
89         STR_OPERATION_RESULT, info.operationResult);
90     if (ret != 0) {
91         IAM_LOGE("hisysevent write failed! ret %{public}d, userId %{public}d, authType %{public}d,"
92             "operationType %{public}u, operationResult %{public}d, callerName %{public}s.", ret, info.userId,
93             info.authType, info.operationType, info.operationResult, info.callerName.c_str());
94     }
95 }
96 
ReportSecurityCredChange(const UserCredChangeTrace & info)97 void ReportSecurityCredChange(const UserCredChangeTrace &info)
98 {
99     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_CREDENTIAL_CHANGE",
100         HiSysEvent::EventType::SECURITY,
101         STR_CALLER_NAME, info.callerName,
102         STR_REQUEST_CONTEXTID, info.requestContextId,
103         STR_USER_ID, info.userId,
104         STR_AUTH_TYPE, info.authType,
105         STR_OPERATION_TYPE, info.operationType,
106         STR_OPERATION_RESULT, info.operationResult,
107         STR_TIME_SPAN, info.timeSpan);
108     if (ret != 0) {
109         IAM_LOGE("hisysevent write failed! ret %{public}d, userId %{public}d, authType %{public}d,"
110             "operationType %{public}u, timeSpan %{public}" PRIu64 ", operationResult %{public}d, callerName %{public}s"
111             ", requestContextId %{public}s.", ret, info.userId, info.authType, info.operationType, info.timeSpan,
112             info.operationResult, info.callerName.c_str(),  GET_MASKED_STRING(info.requestContextId).c_str());
113     }
114 }
115 
ReportUserAuth(const UserAuthTrace & info)116 void ReportUserAuth(const UserAuthTrace &info)
117 {
118     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_USER_AUTH",
119         HiSysEvent::EventType::BEHAVIOR,
120         STR_CALLER_NAME, info.callerName,
121         STR_SDK_VERSION, info.sdkVersion,
122         STR_AUTH_TRUST_LEVEL, info.atl,
123         STR_AUTH_TYPE, info.authType,
124         STR_AUTH_RESULT, info.authResult,
125         STR_AUTH_TIME_SPAN, info.authtimeSpan,
126         STR_AUTH_WIDGET_TYPE, info.authWidgetType,
127         STR_REUSE_UNLOCK_RESULT_TYPE, info.reuseUnlockResultMode,
128         STR_REUSE_UNLOCK_RESULT_DURATION, info.reuseUnlockResultDuration,
129         STR_IS_BACKGROUND_APPLICATION, info.isBackgroundApplication);
130     if (ret != 0) {
131         IAM_LOGE("hisysevent write failed! ret %{public}d, authType %{public}d, atl %{public}u,"
132             " authResult %{public}d, authtimeSpan %{public}" PRIu64 ","
133             " sdkVersion %{public}u, authwidgetType %{public}u, callerName %{public}s,"
134             " reuseUnlockResultMode %{public}u, reuseUnlockResultDuration %{public}" PRIu64 ".",
135             ret, info.authType, info.atl, info.authResult, info.authtimeSpan, info.sdkVersion, info.authWidgetType,
136             info.callerName.c_str(), info.reuseUnlockResultMode, info.reuseUnlockResultDuration);
137     }
138 }
139 
ReportSecurityUserAuthFwk(const UserAuthFwkTrace & info)140 void ReportSecurityUserAuthFwk(const UserAuthFwkTrace &info)
141 {
142     int32_t ret = HiSysEventWrite(HiSysEvent::Domain::USERIAM_FWK, "USERIAM_USER_AUTH_FWK",
143         HiSysEvent::EventType::SECURITY,
144         STR_CALLER_NAME, info.callerName,
145         STR_REQUEST_CONTEXTID, info.requestContextId,
146         STR_AUTH_CONTEXTID, info.authContextId,
147         STR_AUTH_TRUST_LEVEL, info.atl,
148         STR_AUTH_TYPE, info.authType,
149         STR_AUTH_RESULT, info.authResult,
150         STR_AUTH_TIME_SPAN, info.authtimeSpan);
151     if (ret != 0) {
152         IAM_LOGE("hisysevent write failed! ret %{public}d, authType %{public}d, atl %{public}u, authResult %{public}d,"
153             "authtimeSpan %{public}" PRIu64 ", callerName %{public}s, requestContextId %{public}s, "
154             "authContextId %{public}s.", ret, info.authType, info.atl, info.authResult, info.authtimeSpan,
155             info.callerName.c_str(), GET_MASKED_STRING(info.requestContextId).c_str(),
156             GET_MASKED_STRING(info.authContextId).c_str());
157     }
158 }
159 } // namespace UserAuth
160 } // namespace UserIam
161 } // namespace OHOS