1 /*
2 * Copyright (c) 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 #ifndef HUKS_NAPI_COMMON_ITEM_H
17 #define HUKS_NAPI_COMMON_ITEM_H
18
19 #include <string>
20 #include <vector>
21
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24
25 #include "hks_errcode_adapter.h"
26 #include "hks_mem.h"
27 #include "hks_type.h"
28
29 namespace HuksNapiItem {
30 struct HksSuccessReturnResult {
31 bool isOnlyReturnBoolResult;
32 bool boolReturned;
33 struct HksBlob *handle;
34 struct HksBlob *challenge;
35 struct HksBlob *outData;
36 HksParamSet *paramSet;
37 struct HksCertChain *certChain;
38 };
39
40 #define DATA_SIZE_64KB (1024 * 64)
41
42 const std::string HKS_OPTIONS_PROPERTY_PROPERTIES = "properties";
43 const std::string HKS_OPTIONS_PROPERTY_INDATA = "inData";
44
45 const std::string HKS_PARAM_PROPERTY_TAG = "tag";
46 const std::string HKS_PARAM_PROPERTY_VALUE = "value";
47
48 const std::string HKS_RESULT_PROPERTY_ERRORCODE = "errorCode";
49 const std::string HKS_RESULT_PROPERTY_OUTDATA = "outData";
50 const std::string HKS_RESULT_PRPPERTY_PROPERTIES = "properties";
51 const std::string HKS_RESULT_PRPPERTY_CERTCHAINS = "certChains";
52
53 const std::string BUSINESS_ERROR_PROPERTY_CODE = "code";
54 const std::string BUSINESS_ERROR_PROPERTY_MESSAGE = "message";
55 const std::string BUSINESS_ERROR_PROPERTY_DATA = "data";
56
57 const std::string HKS_HANDLE_PROPERTY_ERRORCODE = "errorCode";
58 const std::string HKS_HANDLE_PROPERTY_HANDLE = "handle";
59 const std::string HKS_HANDLE_PROPERTY_CHALLENGE = "challenge";
60
GetNull(napi_env env)61 inline napi_value GetNull(napi_env env)
62 {
63 napi_value result = nullptr;
64 NAPI_CALL(env, napi_get_null(env, &result));
65 return result;
66 }
67
GetInt32(napi_env env,int32_t value)68 inline napi_value GetInt32(napi_env env, int32_t value)
69 {
70 napi_value result = nullptr;
71 NAPI_CALL(env, napi_create_int32(env, value, &result));
72 return result;
73 }
74
FreeHksBlob(HksBlob * & blob)75 inline void FreeHksBlob(HksBlob *&blob)
76 {
77 if (blob == nullptr) {
78 return;
79 }
80
81 if (blob->data != nullptr) {
82 HKS_FREE(blob->data);
83 blob->data = nullptr;
84 }
85 blob->size = 0;
86
87 HKS_FREE(blob);
88 blob = nullptr;
89 }
90
91 napi_value GetUint8Array(napi_env env, napi_value object, HksBlob &arrayBlob);
92
93 napi_value ParseKeyAlias(napi_env env, napi_value object, HksBlob *&alias);
94
95 void FreeParsedParams(std::vector<HksParam> ¶ms);
96
97 napi_value ParseParams(napi_env env, napi_value object, std::vector<HksParam> ¶ms);
98
99 napi_value ParseHksParamSetAndAddParam(napi_env env, napi_value object, HksParamSet *¶mSet, HksParam *addParam);
100
101 napi_ref GetCallback(napi_env env, napi_value object);
102
103 napi_value GetHandleValue(napi_env env, napi_value object, struct HksBlob *&handleBlob);
104
105 void FreeHksCertChain(HksCertChain *&certChain, uint32_t certChainCapacity);
106
107 void DeleteCommonAsyncContext(napi_env env, napi_async_work &asyncWork, napi_ref &callback,
108 struct HksBlob *&blob, struct HksParamSet *¶mSet);
109
110 napi_value ParseHandleAndHksParamSet(napi_env env, napi_value *argv, size_t &index,
111 HksBlob *&handleBlob, HksParamSet *¶mSet);
112
113 napi_value ParseKeyAliasAndHksParamSet(napi_env env, napi_value *argv, size_t &index,
114 HksBlob *&keyAliasBlob, HksParamSet *¶mSet);
115
116 napi_value ParseKeyData(napi_env env, napi_value value, HksBlob *&keyDataBlob);
117
118 napi_value GetPropertyFromOptions(napi_env env, napi_value value, const std::string propertyStr);
119
120 void SuccessReturnResultInit(struct HksSuccessReturnResult &resultData);
121
122 void HksReturnNapiResult(napi_env env, napi_ref callback, napi_deferred deferred, int32_t errorCode,
123 const struct HksSuccessReturnResult resultData);
124
125 void HksReturnKeyExistResult(napi_env env, napi_ref callback, napi_deferred deferred, int32_t errorCode,
126 const struct HksSuccessReturnResult resultData);
127 } // namespace HuksNapiItem
128 #endif
129