• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 struct HksSuccessListAliasesResult {
41     struct HksKeyAliasSet *aliasSet;
42 };
43 
44 #define DATA_SIZE_64KB  (1024 * 64)
45 
46 // The Maximum length of AES-CCM data is 100K bytes. The extra 32 bytes is reserved for Nonce and Aead
47 #define FINISH_DATA_SIZE_200KB  (1024 * 100 + 32)
48 
49 const std::string HKS_OPTIONS_PROPERTY_PROPERTIES = "properties";
50 const std::string HKS_OPTIONS_PROPERTY_INDATA = "inData";
51 
52 const std::string HKS_PARAM_PROPERTY_TAG = "tag";
53 const std::string HKS_PARAM_PROPERTY_VALUE = "value";
54 
55 const std::string HKS_RESULT_PROPERTY_ERRORCODE = "errorCode";
56 const std::string HKS_RESULT_PROPERTY_OUTDATA = "outData";
57 const std::string HKS_RESULT_PRPPERTY_PROPERTIES = "properties";
58 const std::string HKS_RESULT_PRPPERTY_CERTCHAINS = "certChains";
59 
60 const std::string HKS_RESULT_PRPPERTY_ALIASES = "keyAliases";
61 
62 const std::string BUSINESS_ERROR_PROPERTY_CODE = "code";
63 const std::string BUSINESS_ERROR_PROPERTY_MESSAGE = "message";
64 const std::string BUSINESS_ERROR_PROPERTY_DATA = "data";
65 
66 const std::string HKS_HANDLE_PROPERTY_ERRORCODE = "errorCode";
67 const std::string HKS_HANDLE_PROPERTY_HANDLE = "handle";
68 const std::string HKS_HANDLE_PROPERTY_CHALLENGE = "challenge";
69 
GetNull(napi_env env)70 inline napi_value GetNull(napi_env env)
71 {
72     napi_value result = nullptr;
73     NAPI_CALL(env, napi_get_null(env, &result));
74     return result;
75 }
76 
GetInt32(napi_env env,int32_t value)77 inline napi_value GetInt32(napi_env env, int32_t value)
78 {
79     napi_value result = nullptr;
80     NAPI_CALL(env, napi_create_int32(env, value, &result));
81     return result;
82 }
83 
FreeHksBlob(HksBlob * & blob)84 inline void FreeHksBlob(HksBlob *&blob)
85 {
86     if (blob == nullptr) {
87         return;
88     }
89 
90     if (blob->data != nullptr) {
91         HKS_FREE(blob->data);
92         blob->data = nullptr;
93     }
94     blob->size = 0;
95 
96     HKS_FREE(blob);
97     blob = nullptr;
98 }
99 
100 napi_value GetUint8Array(napi_env env, napi_value object, HksBlob &arrayBlob);
101 
102 napi_value ParseKeyAlias(napi_env env, napi_value object, HksBlob *&alias);
103 
104 void FreeParsedParams(std::vector<HksParam> &params);
105 
106 napi_value ParseParams(napi_env env, napi_value object, std::vector<HksParam> &params);
107 
108 napi_value ParseHksParamSetWithToken(napi_env env, struct HksBlob *&token, napi_value object, HksParamSet *&paramSet);
109 
110 napi_value ParseHksParamSetAndAddParam(
111     napi_env env, napi_value object, HksParamSet *&paramSet, const std::vector<HksParam> &addParam = {});
112 
113 napi_ref GetCallback(napi_env env, napi_value object);
114 
115 napi_value GetHandleValue(napi_env env, napi_value object, struct HksBlob *&handleBlob);
116 
117 napi_value GetUserIdValue(napi_env env, napi_value object, int &userId);
118 
119 void FreeHksCertChain(HksCertChain *&certChain, uint32_t certChainCapacity);
120 
121 void FreeHksKeyAliasSet(HksKeyAliasSet *&keyAliasSet, uint32_t cnt);
122 
123 void DeleteCommonAsyncContext(napi_env env, napi_async_work &asyncWork, napi_ref &callback,
124     struct HksBlob *&blob, struct HksParamSet *&paramSet);
125 
126 napi_value ParseKeyAliasAndHksParamSet(napi_env env, napi_value *argv, size_t &index,
127     HksBlob *&keyAliasBlob, HksParamSet *&paramSet);
128 
129 napi_value ParseKeyAliasAndHksParamSetAsUser(napi_env env, int userId, napi_value *argv, size_t &index,
130     std::pair<HksBlob *&, HksParamSet *&> out);
131 
132 napi_value ParseGetHksParamSet(napi_env env, napi_value value, HksParamSet *&paramSet);
133 
134 napi_value ParseKeyData(napi_env env, napi_value value, HksBlob *&keyDataBlob);
135 
136 napi_value GetPropertyFromOptions(napi_env env, napi_value value, const std::string propertyStr);
137 
138 void SuccessReturnResultInit(struct HksSuccessReturnResult &resultData);
139 
140 void SuccessListAliasesReturnResultInit(struct HksSuccessListAliasesResult &resultData);
141 
142 void HksReturnNapiResult(napi_env env, napi_ref callback, napi_deferred deferred, int32_t errorCode,
143     const struct HksSuccessReturnResult resultData);
144 
145 void HksReturnKeyExistResult(napi_env env, napi_ref callback, napi_deferred deferred, int32_t errorCode,
146     const struct HksSuccessReturnResult resultData);
147 
148 void HksReturnListAliasesResult(napi_env env, napi_ref callback, napi_deferred deferred, int32_t errorCode,
149     const struct HksSuccessListAliasesResult resultData);
150 
151 napi_value CreateJsError(napi_env env, int32_t errCode, const char *errorMsg);
152 
HksNapiThrow(napi_env env,int32_t errCode,const char * errorMsg)153 inline void HksNapiThrow(napi_env env, int32_t errCode, const char *errorMsg)
154 {
155     napi_throw(env, CreateJsError(env, errCode, errorMsg));
156 }
157 
HksNapiThrowInsufficientMemory(napi_env env)158 inline void HksNapiThrowInsufficientMemory(napi_env env)
159 {
160     HksNapiThrow(env, HUKS_ERR_CODE_INSUFFICIENT_MEMORY, "Insufficient memory.");
161 }
HksNapiThrowInvalidParamCount(napi_env env)162 inline void HksNapiThrowInvalidParamCount(napi_env env)
163 {
164     HksNapiThrow(env, HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
165         "The number of parameters does not meet the expectation.");
166 }
HksNapiThrowGetUserIdFail(napi_env env)167 inline void HksNapiThrowGetUserIdFail(napi_env env)
168 {
169     HksNapiThrow(env, HUKS_ERR_CODE_ILLEGAL_ARGUMENT, "GetUserIdValue failed");
170 }
171 }  // namespace HuksNapiItem
172 #endif
173