1 /*
2 * Copyright (c) 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 "local_code_sign_proxy.h"
17
18 #include "errcode.h"
19 #include "ipc_types.h"
20 #include "log.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace CodeSign {
25 constexpr uint32_t MAX_REPLY_BUFFER_SIZE = 65536;
26
InitLocalCertificate(ByteBuffer & cert)27 int32_t LocalCodeSignProxy::InitLocalCertificate(ByteBuffer &cert)
28 {
29 MessageParcel data;
30 MessageParcel reply;
31 MessageOption option;
32 sptr<IRemoteObject> remote = Remote();
33 if (remote == nullptr) {
34 return CS_ERR_REMOTE_CONNECTION;
35 }
36 if (!data.WriteInterfaceToken(GetDescriptor())) {
37 return CS_ERR_IPC_WRITE_DATA;
38 }
39 if (remote->SendRequest(static_cast<uint32_t>(LocalCodeSignInterfaceCode::INIT_LOCAL_CERTIFICATE),
40 data, reply, option) != NO_ERROR) {
41 return CS_ERR_IPC_MSG_INVALID;
42 }
43 return ReadResultFromReply(reply, cert);
44 }
45
SignLocalCode(const std::string & filePath,ByteBuffer & signature)46 int32_t LocalCodeSignProxy::SignLocalCode(const std::string &filePath, ByteBuffer &signature)
47 {
48 MessageParcel data;
49 MessageParcel reply;
50 MessageOption option;
51 sptr<IRemoteObject> remote = Remote();
52 if (remote == nullptr) {
53 return CS_ERR_REMOTE_CONNECTION;
54 }
55 if (!data.WriteInterfaceToken(GetDescriptor())) {
56 LOG_ERROR(LABEL, "Write interface token failed.");
57 return CS_ERR_IPC_WRITE_DATA;
58 }
59 if (!data.WriteString(filePath)) {
60 LOG_ERROR(LABEL, "Write string failed.");
61 return CS_ERR_IPC_WRITE_DATA;
62 }
63 if (remote->SendRequest(static_cast<uint32_t>(LocalCodeSignInterfaceCode::SIGN_LOCAL_CODE),
64 data, reply, option) != NO_ERROR) {
65 return CS_ERR_IPC_MSG_INVALID;
66 }
67 return ReadResultFromReply(reply, signature);
68 }
69
ReadResultFromReply(MessageParcel & reply,ByteBuffer & buffer)70 int32_t LocalCodeSignProxy::ReadResultFromReply(MessageParcel &reply, ByteBuffer &buffer)
71 {
72 int32_t result;
73 if (!reply.ReadInt32(result)) {
74 return CS_ERR_IPC_READ_DATA;
75 }
76 if (result != CS_SUCCESS) {
77 return result;
78 }
79 uint32_t size;
80 if (!reply.ReadUint32(size)) {
81 return CS_ERR_IPC_READ_DATA;
82 }
83 if (size > MAX_REPLY_BUFFER_SIZE) {
84 LOG_ERROR(LABEL, "Invalid reply data size.");
85 return CS_ERR_IPC_MSG_INVALID;
86 }
87 const uint8_t *outData = reply.ReadBuffer(size);
88 if (outData == nullptr) {
89 return CS_ERR_IPC_MSG_INVALID;
90 }
91 if (!buffer.CopyFrom(outData, size)) {
92 return CS_ERR_MEMORY;
93 }
94 return CS_SUCCESS;
95 }
96 }
97 }
98 }
99
100