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