• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "hks_request.h"
17 
18 #include <securec.h>
19 #include "iservice_registry.h"
20 
21 #include "hks_log.h"
22 #include "hks_param.h"
23 #include "hks_template.h"
24 
25 using namespace OHOS;
26 
27 namespace {
28 constexpr int SA_ID_KEYSTORE_SERVICE = 3510;
29 const std::u16string SA_KEYSTORE_SERVICE_DESCRIPTOR = u"ohos.security.hks.service";
30 }
31 
GetHksProxy()32 static sptr<IRemoteObject> GetHksProxy()
33 {
34     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
35     HKS_IF_NULL_LOGE_RETURN(registry, nullptr, "GetHksProxy registry is null")
36 
37     sptr<IRemoteObject> hksProxy = registry->GetSystemAbility(SA_ID_KEYSTORE_SERVICE);
38     HKS_IF_NULL_LOGE_RETURN(hksProxy, nullptr,
39         "GetHksProxy GetSystemAbility %" LOG_PUBLIC "d is null", SA_ID_KEYSTORE_SERVICE)
40 
41     return hksProxy;
42 }
43 
HksReadRequestReply(MessageParcel & reply,struct HksBlob * outBlob)44 static int32_t HksReadRequestReply(MessageParcel &reply, struct HksBlob *outBlob)
45 {
46     int32_t ret = reply.ReadInt32();
47     HKS_IF_NOT_SUCC_RETURN(ret, ret)
48 
49     uint32_t outLen = reply.ReadUint32();
50     if (outLen == 0) {
51         if (outBlob != nullptr) {
52             outBlob->size = 0;
53         }
54         return ret;
55     }
56 
57     HKS_IF_NOT_SUCC_RETURN(CheckBlob(outBlob), HKS_ERROR_INVALID_ARGUMENT)
58 
59     const uint8_t *outData = reply.ReadBuffer(outLen);
60     HKS_IF_NULL_RETURN(outData, HKS_ERROR_IPC_MSG_FAIL)
61 
62     if (outBlob->size < outLen) {
63         HKS_LOG_E("outBlob size[%" LOG_PUBLIC "u] smaller than outLen[%" LOG_PUBLIC "u]", outBlob->size, outLen);
64         return HKS_ERROR_BUFFER_TOO_SMALL;
65     }
66 
67     (void)memcpy_s(outBlob->data, outBlob->size, outData, outLen);
68     outBlob->size = outLen;
69     return HKS_SUCCESS;
70 }
71 
HksSendRequest(enum HksMessage type,const struct HksBlob * inBlob,struct HksBlob * outBlob,const struct HksParamSet * paramSet)72 int32_t HksSendRequest(enum HksMessage type, const struct HksBlob *inBlob,
73     struct HksBlob *outBlob, const struct HksParamSet *paramSet)
74 {
75     enum HksSendType sendType = HKS_SEND_TYPE_SYNC;
76     struct HksParam *sendTypeParam = nullptr;
77     int32_t ret = HksGetParam(paramSet, HKS_TAG_IS_ASYNCHRONIZED, &sendTypeParam);
78     if (ret == HKS_SUCCESS) {
79         sendType = static_cast<enum HksSendType>(sendTypeParam->uint32Param);
80     }
81 
82     MessageParcel data;
83     MessageParcel reply;
84     MessageOption option;
85     if (sendType == HKS_SEND_TYPE_SYNC) {
86         option = MessageOption::TF_SYNC;
87     } else {
88         option = MessageOption::TF_ASYNC;
89     }
90 
91     data.WriteInterfaceToken(SA_KEYSTORE_SERVICE_DESCRIPTOR);
92 
93     if (outBlob == nullptr) {
94         data.WriteUint32(0);
95     } else {
96         data.WriteUint32(outBlob->size);
97     }
98 
99     data.WriteUint32(inBlob->size);
100     data.WriteBuffer(inBlob->data, static_cast<size_t>(inBlob->size));
101 
102     sptr<IRemoteObject> hksProxy = GetHksProxy();
103     HKS_IF_NULL_LOGE_RETURN(hksProxy, HKS_ERROR_BAD_STATE, "GetHksProxy registry is null")
104 
105     int error = hksProxy->SendRequest(type, data, reply, option);
106     if (error != 0) {
107         return error;
108     }
109 
110     return HksReadRequestReply(reply, outBlob);
111 }
112