• 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 #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 *&paramSet);
53 
54 void FreeParsedParams(std::vector<HksParam> &params);
55 
56 napi_value ParseParams(napi_env env, napi_value object, std::vector<HksParam> &params);
57 
58 napi_value ParseHksParamSetAndAddParam(napi_env env, napi_value object, HksParamSet *&paramSet, HksParam *addParam);
59 
60 napi_ref GetCallback(napi_env env, napi_value object);
61 
62 napi_value GenerateHksResult(napi_env env, int32_t error, uint8_t *data, uint32_t size);
63 napi_value GenerateHksResult(napi_env env, int32_t error, uint8_t *data, uint32_t size, const HksParamSet &paramSet);
64 
65 napi_value GenerateStringArray(napi_env env, const struct HksBlob *blob, const uint32_t blobCount);
66 
67 napi_value GenerateHksHandle(napi_env env, int32_t error, const struct HksBlob *handle,
68     const struct HksBlob *token);
69 
70 napi_value GetHandleValue(napi_env env, napi_value object, struct HksBlob *&handleBlob);
71 
72 void CallAsyncCallback(napi_env env, napi_ref callback, int32_t error, napi_value data);
73 
GetNull(napi_env env)74 inline napi_value GetNull(napi_env env)
75 {
76     napi_value result = nullptr;
77     NAPI_CALL(env, napi_get_null(env, &result));
78     return result;
79 }
80 
GetInt32(napi_env env,int32_t value)81 inline napi_value GetInt32(napi_env env, int32_t value)
82 {
83     napi_value result = nullptr;
84     NAPI_CALL(env, napi_create_int32(env, value, &result));
85     return result;
86 }
87 
FreeHksBlob(HksBlob * & blob)88 inline void FreeHksBlob(HksBlob *&blob)
89 {
90     if (blob == nullptr) {
91         return;
92     }
93 
94     if (blob->data != nullptr) {
95         HksFree(blob->data);
96         blob->data = nullptr;
97     }
98     blob->size = 0;
99 
100     HksFree(blob);
101     blob = nullptr;
102 }
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 *&paramSet);
108 
109 napi_value ParseHandleAndHksParamSet(napi_env env, napi_value *argv, size_t &index,
110     HksBlob *&handleBlob, HksParamSet *&paramSet);
111 
112 napi_value ParseKeyAliasAndHksParamSet(napi_env env, napi_value *argv, size_t &index,
113     HksBlob *&keyAliasBlob, HksParamSet *&paramSet);
114 
115 napi_value ParseKeyData(napi_env env, napi_value value, HksBlob *&keyDataBlob);
116 }  // namespace HuksNapi
117 #endif
118