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_stub.h"
17 
18 #include "cert_utils.h"
19 #include "cs_hisysevent.h"
20 #include "cs_hitrace.h"
21 #include "errcode.h"
22 #include "ipc_skeleton.h"
23 #include "log.h"
24 #include "message_parcel.h"
25 #include "permission_utils.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace CodeSign {
30 using namespace std;
31 
LocalCodeSignStub()32 LocalCodeSignStub::LocalCodeSignStub()
33 {
34 }
35 
~LocalCodeSignStub()36 LocalCodeSignStub::~LocalCodeSignStub()
37 {
38 }
39 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int32_t LocalCodeSignStub::OnRemoteRequest(uint32_t code,
41     MessageParcel &data, MessageParcel &reply, MessageOption &option)
42 {
43     DelayUnloadTask();
44     std::u16string descriptor = LocalCodeSignStub::GetDescriptor();
45     std::u16string token = data.ReadInterfaceToken();
46     if (token != descriptor) {
47         return CS_ERR_IPC_MSG_INVALID;
48     }
49     switch (code) {
50         case static_cast<uint32_t>(LocalCodeSignInterfaceCode::INIT_LOCAL_CERTIFICATE):
51             return InitLocalCertificateInner(data, reply);
52         case static_cast<uint32_t>(LocalCodeSignInterfaceCode::SIGN_LOCAL_CODE):
53             return SignLocalCodeInner(data, reply);
54         default:
55             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
56     }
57 }
58 
InitLocalCertificateInner(MessageParcel & data,MessageParcel & reply)59 int32_t LocalCodeSignStub::InitLocalCertificateInner(MessageParcel &data, MessageParcel &reply)
60 {
61     if (!PermissionUtils::IsValidCallerOfCert()) {
62         reply.WriteInt32(CS_ERR_NO_PERMISSION);
63         return CS_ERR_NO_PERMISSION;
64     }
65 
66     uint32_t challengeLen;
67     if (!data.ReadUint32(challengeLen) || !CheckChallengeSize(challengeLen)) {
68         LOG_ERROR("Get challenge size failed.");
69         return CS_ERR_IPC_READ_DATA;
70     }
71     ByteBuffer challenge;
72     const uint8_t *challengeBuffer = data.ReadBuffer(challengeLen);
73     if (!challenge.CopyFrom(challengeBuffer, challengeLen)) {
74         return CS_ERR_MEMORY;
75     }
76 
77     ByteBuffer cert;
78     int32_t result = InitLocalCertificate(challenge, cert);
79     if (!reply.WriteInt32(result)) {
80         return CS_ERR_IPC_WRITE_DATA;
81     }
82     if (result != CS_SUCCESS) {
83         return result;
84     }
85     if (!reply.WriteUint32(cert.GetSize())) {
86         return CS_ERR_IPC_WRITE_DATA;
87     }
88     if (!reply.WriteBuffer(cert.GetBuffer(), cert.GetSize())) {
89         return CS_ERR_IPC_WRITE_DATA;
90     }
91     return CS_SUCCESS;
92 }
93 
SignLocalCodeInner(MessageParcel & data,MessageParcel & reply)94 int32_t LocalCodeSignStub::SignLocalCodeInner(MessageParcel &data, MessageParcel &reply)
95 {
96     if (!PermissionUtils::IsValidCallerOfLocalCodeSign()) {
97         (void)reply.WriteInt32(CS_ERR_NO_PERMISSION);
98         return CS_ERR_NO_PERMISSION;
99     }
100     std::string filePath = data.ReadString();
101     std::string ownerID;
102     if (data.GetReadableBytes() > 0) {
103         ownerID = data.ReadString();
104     }
105     StartTrace(HITRACE_TAG_ACCESS_CONTROL, CODE_SIGN_ENABLE_START);
106     ByteBuffer signature;
107     int32_t result = SignLocalCode(ownerID, filePath, signature);
108     FinishTrace(HITRACE_TAG_ACCESS_CONTROL);
109     if (!reply.WriteInt32(result)) {
110         return CS_ERR_IPC_WRITE_DATA;
111     }
112     if (result != CS_SUCCESS) {
113         return result;
114     }
115     if (!reply.WriteUint32(signature.GetSize())) {
116         return CS_ERR_IPC_WRITE_DATA;
117     }
118     if (!reply.WriteBuffer(signature.GetBuffer(), signature.GetSize())) {
119         return CS_ERR_IPC_WRITE_DATA;
120     }
121     return CS_SUCCESS;
122 }
123 }
124 }
125 }