1 /*
2 * Copyright (c) 2023-2024 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
InitLocalCertificate(const ByteBuffer & challenge,ByteBuffer & cert)26 int32_t LocalCodeSignProxy::InitLocalCertificate(const ByteBuffer &challenge, ByteBuffer &cert)
27 {
28 MessageParcel data;
29 MessageParcel reply;
30 MessageOption option;
31 sptr<IRemoteObject> remote = Remote();
32 if (remote == nullptr) {
33 return CS_ERR_REMOTE_CONNECTION;
34 }
35 if (!data.WriteInterfaceToken(GetDescriptor())) {
36 return CS_ERR_IPC_WRITE_DATA;
37 }
38 if (!data.WriteUint32(challenge.GetSize())) {
39 return CS_ERR_IPC_WRITE_DATA;
40 }
41 if (!data.WriteBuffer(challenge.GetBuffer(), challenge.GetSize())) {
42 return CS_ERR_IPC_WRITE_DATA;
43 }
44 if (remote->SendRequest(static_cast<uint32_t>(LocalCodeSignInterfaceCode::INIT_LOCAL_CERTIFICATE),
45 data, reply, option) != NO_ERROR) {
46 return CS_ERR_IPC_MSG_INVALID;
47 }
48 return ReadResultFromReply(reply, cert);
49 }
50
SignLocalCode(const std::string & ownerID,const std::string & filePath,ByteBuffer & signature)51 int32_t LocalCodeSignProxy::SignLocalCode(const std::string &ownerID, const std::string &filePath,
52 ByteBuffer &signature)
53 {
54 MessageParcel data;
55 MessageParcel reply;
56 MessageOption option;
57 sptr<IRemoteObject> remote = Remote();
58 if (remote == nullptr) {
59 return CS_ERR_REMOTE_CONNECTION;
60 }
61 if (!data.WriteInterfaceToken(GetDescriptor())) {
62 LOG_ERROR("Write interface token failed.");
63 return CS_ERR_IPC_WRITE_DATA;
64 }
65 if (!data.WriteString(filePath)) {
66 LOG_ERROR("Write string failed.");
67 return CS_ERR_IPC_WRITE_DATA;
68 }
69
70 if (!ownerID.empty()) {
71 if (!data.WriteString(ownerID)) {
72 LOG_ERROR("Write ownerID string failed.");
73 return CS_ERR_IPC_WRITE_DATA;
74 }
75 }
76 if (remote->SendRequest(static_cast<uint32_t>(LocalCodeSignInterfaceCode::SIGN_LOCAL_CODE),
77 data, reply, option) != NO_ERROR) {
78 return CS_ERR_IPC_MSG_INVALID;
79 }
80 return ReadResultFromReply(reply, signature);
81 }
82
ReadResultFromReply(MessageParcel & reply,ByteBuffer & buffer)83 int32_t LocalCodeSignProxy::ReadResultFromReply(MessageParcel &reply, ByteBuffer &buffer)
84 {
85 int32_t result;
86 if (!reply.ReadInt32(result)) {
87 return CS_ERR_IPC_READ_DATA;
88 }
89 if (result != CS_SUCCESS) {
90 return result;
91 }
92 uint32_t size;
93 if (!reply.ReadUint32(size)) {
94 return CS_ERR_IPC_READ_DATA;
95 }
96 if (size > reply.GetReadableBytes()) {
97 LOG_ERROR("Invalid reply data size.");
98 return CS_ERR_IPC_MSG_INVALID;
99 }
100 const uint8_t *outData = reply.ReadBuffer(size);
101 if (outData == nullptr) {
102 return CS_ERR_IPC_MSG_INVALID;
103 }
104 if (!buffer.CopyFrom(outData, size)) {
105 return CS_ERR_MEMORY;
106 }
107 return CS_SUCCESS;
108 }
109 }
110 }
111 }
112
113