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_level_proxy.h"
17 
18 #include <cstdint>
19 #include <functional>
20 #include <new>
21 #include <type_traits>
22 
23 #include "hilog/log_cpp.h"
24 #include "ipc_types.h"
25 #include "iremote_broker.h"
26 #include "iremote_object.h"
27 #include "message_option.h"
28 #include "message_parcel.h"
29 #include "refbase.h"
30 #include "singleton.h"
31 
32 #include "device_security_defines.h"
33 #include "device_security_level_defines.h"
34 #include "idevice_security_level.h"
35 namespace OHOS {
36 namespace Security {
37 namespace DeviceSecurityLevel {
38 using namespace OHOS::HiviewDFX;
DeviceSecurityLevelProxy(const sptr<IRemoteObject> & impl)39 DeviceSecurityLevelProxy::DeviceSecurityLevelProxy(const sptr<IRemoteObject> &impl)
40     : IRemoteProxy<IDeviceSecurityLevel>(impl)
41 {
42 }
43 
RequestDeviceSecurityLevel(const DeviceIdentify & identify,const RequestOption & option,const sptr<IRemoteObject> & callback,uint64_t cookie)44 int32_t DeviceSecurityLevelProxy::RequestDeviceSecurityLevel(const DeviceIdentify &identify,
45     const RequestOption &option, const sptr<IRemoteObject> &callback, uint64_t cookie)
46 {
47     MessageParcel data;
48     MessageParcel reply;
49 
50     auto length = identify.length;
51     sptr<IRemoteObject> remote = Remote();
52     if (remote == nullptr) {
53         HILOG_ERROR(LOG_CORE, "Remote is nullptr");
54         return ERR_INVALID_PARA;
55     }
56 
57     if (length == 0 || length > DEVICE_ID_MAX_LEN) {
58         HILOG_ERROR(LOG_CORE, "RequestDeviceSecurityLevel invalid para len.");
59         return ERR_INVALID_LEN_PARA;
60     }
61 
62     if (!data.WriteInterfaceToken(GetDescriptor())) {
63         HILOG_ERROR(LOG_CORE, "RequestDeviceSecurityLevel write descriptor failed");
64         return ERR_INVALID_PARA;
65     }
66 
67     /* DeviceIdentify */
68     data.WriteUint32(length);
69     data.WriteBuffer(identify.identity, DEVICE_ID_MAX_LEN);
70     /* option */
71     data.WriteUint64(option.challenge);
72     data.WriteUint32(option.timeout);
73     data.WriteUint32(option.extra);
74 
75     /* callback */
76     data.WriteRemoteObject(callback);
77     /* cookie */
78     data.WriteUint32(cookie);
79 
80     MessageOption ipcOption = {MessageOption::TF_SYNC};
81     auto result = remote->SendRequest(CMD_GET_DEVICE_SECURITY_LEVEL, data, reply, ipcOption);
82     if (result != ERR_NONE) {
83         HILOG_ERROR(LOG_CORE, "RequestDeviceSecurityLevelSendRequest send failed, ret is %{public}d", result);
84         return result;
85     }
86 
87     if (reply.GetReadableBytes() < sizeof(uint32_t)) {
88         HILOG_ERROR(LOG_CORE, "RequestDeviceSecurityLevelSendRequest result length error");
89         return ERR_IPC_RET_PARCEL_ERR;
90     }
91 
92     auto status = reply.ReadUint32();
93     if (status != cookie) {
94         HILOG_ERROR(LOG_CORE, "RequestDeviceSecurityLevelSendRequest result value error, ret is %{public}u", status);
95         return ERR_IPC_REMOTE_OBJ_ERR;
96     }
97 
98     return SUCCESS;
99 }
100 } // namespace DeviceSecurityLevel
101 } // namespace Security
102 } // namespace OHOS
103