1 /*
2 * Copyright (c) 2023-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 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21
22 #include "hks_sa_interface.h"
23
24 #include <errors.h>
25 #include <ipc_types.h>
26 #include <memory>
27 #include <mutex>
28 #include <securec.h>
29
30 #include "hks_dcm_callback_handler.h"
31 #include "hks_log.h"
32 #include "hks_template.h"
33 #include "huks_service_ipc_interface_code.h"
34
35 namespace OHOS {
36 namespace Security {
37 namespace Hks {
38
SendAsyncReply(uint32_t errCode,std::unique_ptr<uint8_t[]> & certChain,uint32_t sz)39 void HksStub::SendAsyncReply(uint32_t errCode, std::unique_ptr<uint8_t[]> &certChain, uint32_t sz)
40 {
41 std::unique_lock<std::mutex> lck(mMutex);
42 received = true;
43 mErrCode = errCode;
44 mAsyncReply = std::move(certChain);
45 mSize = sz;
46 mCv.notify_all();
47 }
48
ProcessAttestKeyAsyncReply(MessageParcel & data)49 int HksStub::ProcessAttestKeyAsyncReply(MessageParcel& data)
50 {
51 std::unique_ptr<uint8_t[]> certChain{};
52 uint32_t errCode = 1;
53 if (!data.ReadUint32(errCode) || errCode != DCM_SUCCESS) {
54 HKS_LOG_E("ipc client read errCode %" LOG_PUBLIC "u", errCode);
55 SendAsyncReply(errCode, certChain, 0);
56 return ERR_INVALID_DATA;
57 }
58 uint32_t certChainLen = 0;
59 int err = ERR_INVALID_DATA;
60 do {
61 uint32_t sz = 0;
62 HKS_IF_TRUE_LOGE_BREAK(!data.ReadUint32(sz) || sz == 0 || sz > MAX_OUT_BLOB_SIZE,
63 "invalid sz %" LOG_PUBLIC "u", sz)
64 const uint8_t *ptr = data.ReadBuffer(sz);
65 HKS_IF_NULL_LOGE_BREAK(ptr, "ReadBuffer %" LOG_PUBLIC "u size ptr is nullptr", sz)
66 std::unique_ptr<uint8_t[]> receivedPtr(new (std::nothrow) uint8_t[sz]());
67 if (receivedPtr == nullptr) {
68 HKS_LOG_E("new receivedPtr failed");
69 err = ERR_NO_MEMORY;
70 break;
71 }
72 HKS_IF_NOT_EOK_LOGE_BREAK(memcpy_s(receivedPtr.get(), sz, ptr, sz), "memcpy_s receivedPtr failed");
73 err = ERR_OK;
74 certChain = std::move(receivedPtr);
75 certChainLen = sz;
76 } while (false);
77 SendAsyncReply(errCode, certChain, certChainLen);
78 return err;
79 }
80
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)81 int HksStub::OnRemoteRequest(uint32_t code,
82 MessageParcel& data, MessageParcel& reply, MessageOption& option)
83 {
84 HKS_IF_TRUE_LOGE_RETURN(data.ReadInterfaceToken() != GetDescriptor(), ERR_INVALID_DATA,
85 "failed to check interface token! code %" LOG_PUBLIC "d", code)
86 HKS_IF_TRUE_LOGE_RETURN(code != HKS_MSG_ATTEST_KEY_ASYNC_REPLY, ERR_TRANSACTION_FAILED,
87 "unknown remote request code %" LOG_PUBLIC "u", code)
88 return ProcessAttestKeyAsyncReply(data);
89 }
90
WaitForAsyncReply(int timeout)91 std::tuple<uint32_t, std::unique_ptr<uint8_t[]>, uint32_t> HksStub::WaitForAsyncReply(int timeout)
92 {
93 std::unique_lock<std::mutex> lck(mMutex);
94 if (received) {
95 return {mErrCode, std::move(mAsyncReply), mSize};
96 }
97 mAsyncReply = nullptr;
98 mSize = 0;
99 mErrCode = DCM_SUCCESS;
100 HKS_LOG_I("begin wait for async reply");
101 if (mCv.wait_for(lck, std::chrono::seconds(timeout)) == std::cv_status::timeout) {
102 HKS_LOG_E("WaitForAsyncReply timeout! %d seconds", timeout);
103 return {HKS_ERROR_BAD_STATE, nullptr, 0};
104 }
105 return {mErrCode, std::move(mAsyncReply), mSize};
106 }
107
108 BrokerDelegator<HksProxy> HksProxy::delegator_;
HksProxy(const sptr<IRemoteObject> & impl)109 HksProxy::HksProxy(const sptr<IRemoteObject> &impl)
110 : IRemoteProxy<IHksService>(impl)
111 {
112 }
113
SendAsyncReply(uint32_t errCode,std::unique_ptr<uint8_t[]> & certChain,uint32_t sz)114 void HksProxy::SendAsyncReply(uint32_t errCode, std::unique_ptr<uint8_t[]> &certChain, uint32_t sz)
115 {
116 HKS_IF_NULL_LOGE_RETURN_VOID(Remote(), "Remote() is nullptr! Would not SendRequest!")
117 MessageParcel data;
118 MessageParcel reply;
119 MessageOption option = MessageOption::TF_ASYNC;
120 bool writeResult = data.WriteInterfaceToken(GetDescriptor());
121 HKS_IF_NOT_TRUE_LOGE_RETURN_VOID(writeResult,
122 "WriteInterfaceToken errCode %" LOG_PUBLIC "u failed %" LOG_PUBLIC "d", errCode, writeResult)
123 writeResult = data.WriteUint32(errCode);
124 HKS_IF_NOT_TRUE_LOGE_RETURN_VOID(writeResult, "WriteUint32 errCode %" LOG_PUBLIC "u failed %" LOG_PUBLIC "d",
125 errCode, writeResult)
126 if (errCode != DCM_SUCCESS) {
127 HKS_LOG_E("dcm callback fail errCode %" LOG_PUBLIC "u", errCode);
128 int res = Remote()->SendRequest(HKS_MSG_ATTEST_KEY_ASYNC_REPLY, data, reply, option);
129 HKS_IF_TRUE_LOGE(res != ERR_OK, "send fail reply errCode failed %" LOG_PUBLIC "d", res)
130 return;
131 }
132 writeResult = data.WriteUint32(sz);
133 HKS_IF_NOT_TRUE_LOGE_RETURN_VOID(writeResult, "WriteUint32 sz %" LOG_PUBLIC "u failed %" LOG_PUBLIC "d",
134 sz, writeResult)
135 if (sz == 0 || certChain == nullptr) {
136 HKS_LOG_E("dcm reply success but empty certChain %" LOG_PUBLIC "u", sz);
137 int res = Remote()->SendRequest(HKS_MSG_ATTEST_KEY_ASYNC_REPLY, data, reply, option);
138 HKS_IF_TRUE_LOGE(res != ERR_OK,
139 "Remote()->SendRequest HKS_MSG_ATTEST_KEY_ASYNC_REPLY failed %" LOG_PUBLIC "d", res)
140 return;
141 }
142 writeResult = data.WriteBuffer(certChain.get(), sz);
143 HKS_IF_NOT_TRUE_LOGE_RETURN_VOID(writeResult, "WriteBuffer size %" LOG_PUBLIC "u failed %" LOG_PUBLIC "d",
144 sz, writeResult)
145 int res = Remote()->SendRequest(HKS_MSG_ATTEST_KEY_ASYNC_REPLY, data, reply, option);
146 HKS_IF_TRUE_LOGE(res != ERR_OK,
147 "Remote()->SendRequest HKS_MSG_ATTEST_KEY_ASYNC_REPLY failed %" LOG_PUBLIC "d", res)
148 }
149
150 } // namespace Hks
151 } // namespace Security
152 } // namespace OHOS
153