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