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