• 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 #ifndef HUKS_NAPI_COMMON_H
17 #define HUKS_NAPI_COMMON_H
18 
19 #include <string>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 
24 #include "hks_mem.h"
25 #include "hks_type.h"
26 
27 #define DATA_SIZE_64KB  (1024 * 64)
28 
29 namespace HuksNapi {
30 static const size_t HKS_HANDLE_OFFSET32 = 32;
31 static const std::string HKS_OPTIONS_PROPERTY_PROPERTIES = "properties";
32 static const std::string HKS_OPTIONS_PROPERTY_INDATA = "inData";
33 
34 static const std::string HKS_PARAM_PROPERTY_TAG = "tag";
35 static const std::string HKS_PARAM_PROPERTY_VALUE = "value";
36 
37 static const std::string HKS_RESULT_PROPERTY_ERRORCODE = "errorCode";
38 static const std::string HKS_RESULT_PROPERTY_OUTDATA = "outData";
39 static const std::string HKS_RESULT_PRPPERTY_PROPERTIES = "properties";
40 static const std::string HKS_RESULT_PRPPERTY_CERTCHAINS = "certChains";
41 
42 static const std::string BUSINESS_ERROR_PROPERTY_CODE = "code";
43 
44 static const std::string HKS_HANDLE_PROPERTY_ERRORCODE = "errorCode";
45 static const std::string HKS_HANDLE_PROPERTY_HANDLE = "handle";
46 
47 napi_value GetUint8Array(napi_env env, napi_value object, HksBlob &arrayBlob);
48 
49 napi_value ParseKeyAlias(napi_env env, napi_value object, HksBlob *&alias);
50 
51 napi_value ParseHksParamSet(napi_env env, napi_value object, HksParamSet *&paramSet);
52 
53 napi_ref GetCallback(napi_env env, napi_value object);
54 
55 napi_value GenerateHksResult(napi_env env, int32_t error);
56 napi_value GenerateHksResult(napi_env env, int32_t error, uint8_t *data, uint32_t size);
57 napi_value GenerateHksResult(napi_env env, int32_t error, uint8_t *data, uint32_t size, const HksParamSet &paramSet);
58 
59 napi_value GenerateStringArray(napi_env env, const struct HksBlob *blob, const uint32_t blobCount);
60 
61 napi_value GenerateHksHandle(napi_env env, int32_t error, uint8_t *data, uint32_t size);
62 
63 void CallAsyncCallback(napi_env env, napi_ref callback, int32_t error, napi_value data);
64 
GetNull(napi_env env)65 inline napi_value GetNull(napi_env env)
66 {
67     napi_value result = nullptr;
68     NAPI_CALL(env, napi_get_null(env, &result));
69     return result;
70 }
71 
GetInt32(napi_env env,int32_t value)72 inline napi_value GetInt32(napi_env env, int32_t value)
73 {
74     napi_value result = nullptr;
75     NAPI_CALL(env, napi_create_int32(env, value, &result));
76     return result;
77 }
78 
FreeHksBlob(HksBlob * & blob)79 inline void FreeHksBlob(HksBlob *&blob)
80 {
81     if (blob == nullptr) {
82         return;
83     }
84 
85     if (blob->data != nullptr) {
86         HksFree(blob->data);
87         blob->data = nullptr;
88     }
89     blob->size = 0;
90 
91     HksFree(blob);
92     blob = nullptr;
93 }
94 
95 void FreeHksCertChain(HksCertChain *&certChain);
96 }  // namespace HuksNapi
97 
98 #endif