• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 & ownerID,const std::string & filePath,ByteBuffer & signature)46 int32_t LocalCodeSignProxy::SignLocalCode(const std::string &ownerID, const std::string &filePath,
47                                           ByteBuffer &signature)
48 {
49     MessageParcel data;
50     MessageParcel reply;
51     MessageOption option;
52     sptr<IRemoteObject> remote = Remote();
53     if (remote == nullptr) {
54         return CS_ERR_REMOTE_CONNECTION;
55     }
56     if (!data.WriteInterfaceToken(GetDescriptor())) {
57         LOG_ERROR(LABEL, "Write interface token failed.");
58         return CS_ERR_IPC_WRITE_DATA;
59     }
60     if (!data.WriteString(filePath)) {
61         LOG_ERROR(LABEL, "Write string failed.");
62         return CS_ERR_IPC_WRITE_DATA;
63     }
64 
65     if (!ownerID.empty()) {
66         if (!data.WriteString(ownerID)) {
67             LOG_ERROR(LABEL, "Write ownerID string failed.");
68             return CS_ERR_IPC_WRITE_DATA;
69         }
70     }
71     if (remote->SendRequest(static_cast<uint32_t>(LocalCodeSignInterfaceCode::SIGN_LOCAL_CODE),
72         data, reply, option) != NO_ERROR) {
73         return CS_ERR_IPC_MSG_INVALID;
74     }
75     return ReadResultFromReply(reply, signature);
76 }
77 
ReadResultFromReply(MessageParcel & reply,ByteBuffer & buffer)78 int32_t LocalCodeSignProxy::ReadResultFromReply(MessageParcel &reply, ByteBuffer &buffer)
79 {
80     int32_t result;
81     if (!reply.ReadInt32(result)) {
82         return CS_ERR_IPC_READ_DATA;
83     }
84     if (result != CS_SUCCESS) {
85         return result;
86     }
87     uint32_t size;
88     if (!reply.ReadUint32(size)) {
89         return CS_ERR_IPC_READ_DATA;
90     }
91     if (size > MAX_REPLY_BUFFER_SIZE) {
92         LOG_ERROR(LABEL, "Invalid reply data size.");
93         return CS_ERR_IPC_MSG_INVALID;
94     }
95     const uint8_t *outData = reply.ReadBuffer(size);
96     if (outData == nullptr) {
97         return CS_ERR_IPC_MSG_INVALID;
98     }
99     if (!buffer.CopyFrom(outData, size)) {
100         return CS_ERR_MEMORY;
101     }
102     return CS_SUCCESS;
103 }
104 }
105 }
106 }
107 
108