• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (length == 0 || length > DEVICE_ID_MAX_LEN) {
52         HiLog::Error(LABEL, "RequestDeviceSecurityLevel invalid para len.");
53         return ERR_INVALID_LEN_PARA;
54     }
55 
56     if (!data.WriteInterfaceToken(GetDescriptor())) {
57         HiLog::Error(LABEL, "RequestDeviceSecurityLevel write descriptor failed");
58         return ERR_INVALID_PARA;
59     }
60 
61     /* DeviceIdentify */
62     data.WriteUint32(length);
63     data.WriteBuffer(identify.identity, DEVICE_ID_MAX_LEN);
64     /* option */
65     data.WriteUint64(option.challenge);
66     data.WriteUint32(option.timeout);
67     data.WriteUint32(option.extra);
68 
69     /* callback */
70     data.WriteRemoteObject(callback);
71     /* cookie */
72     data.WriteUint32(cookie);
73 
74     MessageOption ipcOption = {MessageOption::TF_SYNC};
75     auto result = Remote()->SendRequest(CMD_GET_DEVICE_SECURITY_LEVEL, data, reply, ipcOption);
76     if (result != ERR_NONE) {
77         HiLog::Error(LABEL, "RequestDeviceSecurityLevelSendRequest send failed, ret is %{public}d", result);
78         return result;
79     }
80 
81     if (reply.GetReadableBytes() < sizeof(uint32_t)) {
82         HiLog::Error(LABEL, "RequestDeviceSecurityLevelSendRequest result length error");
83         return ERR_IPC_RET_PARCEL_ERR;
84     }
85 
86     auto status = reply.ReadUint32();
87     if (status != cookie) {
88         HiLog::Error(LABEL, "RequestDeviceSecurityLevelSendRequest result value error, ret is %{public}u", status);
89         return ERR_IPC_REMOTE_OBJ_ERR;
90     }
91 
92     return SUCCESS;
93 }
94 } // namespace DeviceSecurityLevel
95 } // namespace Security
96 } // namespace OHOS
97