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 #ifndef HUKS_NAPI_COMMON_H
17 #define HUKS_NAPI_COMMON_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_mem.h"
26 #include "hks_type.h"
27
28 #define DATA_SIZE_64KB (1024 * 64)
29
30 namespace HuksNapi {
31 const std::string HKS_OPTIONS_PROPERTY_PROPERTIES = "properties";
32 const std::string HKS_OPTIONS_PROPERTY_INDATA = "inData";
33
34 const std::string HKS_PARAM_PROPERTY_TAG = "tag";
35 const std::string HKS_PARAM_PROPERTY_VALUE = "value";
36
37 const std::string HKS_RESULT_PROPERTY_ERRORCODE = "errorCode";
38 const std::string HKS_RESULT_PROPERTY_OUTDATA = "outData";
39 const std::string HKS_RESULT_PRPPERTY_PROPERTIES = "properties";
40 const std::string HKS_RESULT_PRPPERTY_CERTCHAINS = "certChains";
41
42 const std::string BUSINESS_ERROR_PROPERTY_CODE = "code";
43
44 const std::string HKS_HANDLE_PROPERTY_ERRORCODE = "errorCode";
45 const std::string HKS_HANDLE_PROPERTY_HANDLE = "handle";
46 const std::string HKS_HANDLE_PROPERTY_TOKEN = "token";
47
48 napi_value GetUint8Array(napi_env env, napi_value object, HksBlob &arrayBlob);
49
50 napi_value ParseKeyAlias(napi_env env, napi_value object, HksBlob *&alias);
51
52 napi_value ParseHksParamSet(napi_env env, napi_value object, HksParamSet *¶mSet);
53
54 void FreeParsedParams(std::vector<HksParam> ¶ms);
55
56 napi_value ParseParams(napi_env env, napi_value object, std::vector<HksParam> ¶ms);
57
58 napi_ref GetCallback(napi_env env, napi_value object);
59
60 napi_value GenerateHksResult(napi_env env, int32_t error, uint8_t *data, uint32_t size);
61 napi_value GenerateHksResult(napi_env env, int32_t error, uint8_t *data, uint32_t size, const HksParamSet ¶mSet);
62
63 napi_value GenerateStringArray(napi_env env, const struct HksBlob *blob, const uint32_t blobCount);
64
65 napi_value GenerateHksHandle(napi_env env, int32_t error, const struct HksBlob *handle,
66 const struct HksBlob *token);
67
68 napi_value GetHandleValue(napi_env env, napi_value object, struct HksBlob *&handleBlob);
69
70 void CallAsyncCallback(napi_env env, napi_ref callback, int32_t error, napi_value data);
71
GetNull(napi_env env)72 inline napi_value GetNull(napi_env env)
73 {
74 napi_value result = nullptr;
75 NAPI_CALL(env, napi_get_null(env, &result));
76 return result;
77 }
78
GetInt32(napi_env env,int32_t value)79 inline napi_value GetInt32(napi_env env, int32_t value)
80 {
81 napi_value result = nullptr;
82 NAPI_CALL(env, napi_create_int32(env, value, &result));
83 return result;
84 }
85
FreeHksBlob(HksBlob * & blob)86 inline void FreeHksBlob(HksBlob *&blob)
87 {
88 if (blob == nullptr) {
89 return;
90 }
91
92 if (blob->data != nullptr) {
93 HKS_FREE(blob->data);
94 blob->data = nullptr;
95 }
96 blob->size = 0;
97
98 HKS_FREE(blob);
99 blob = nullptr;
100 }
101
102 void FreeHksCertChain(HksCertChain *&certChain);
103
104 void DeleteCommonAsyncContext(napi_env env, napi_async_work &asyncWork, napi_ref &callback,
105 struct HksBlob *&blob, struct HksParamSet *¶mSet);
106
107 napi_value ParseHandleAndHksParamSet(napi_env env, napi_value *argv, size_t &index,
108 HksBlob *&handleBlob, HksParamSet *¶mSet);
109
110 napi_value ParseKeyAliasAndHksParamSet(napi_env env, napi_value *argv, size_t &index,
111 HksBlob *&keyAliasBlob, HksParamSet *¶mSet);
112
113 napi_value ParseKeyData(napi_env env, napi_value value, HksBlob *&keyDataBlob);
114 } // namespace HuksNapi
115 #endif
116