• 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 #include "huks_napi_agree_key.h"
17 
18 #include "securec.h"
19 
20 #include "hks_api.h"
21 #include "hks_log.h"
22 #include "hks_mem.h"
23 #include "hks_param.h"
24 #include "hks_type.h"
25 #include "huks_napi_common.h"
26 
27 namespace HuksNapi {
28 namespace {
29 constexpr int HUKS_NAPI_AGREE_KEY_MIN_ARGS = 2;
30 constexpr int HUKS_NAPI_AGREE_KEY_MAX_ARGS = 3;
31 
32 constexpr int HKS_MAX_AGREED_KEY_SIZE = 512;
33 }  // namespace
34 
35 struct AgreeKeyAsyncContextT {
36     napi_async_work asyncWork = nullptr;
37     napi_deferred deferred = nullptr;
38     napi_ref callback = nullptr;
39 
40     int32_t result = 0;
41     struct HksParamSet *paramSet = nullptr;
42     struct HksBlob *keyAlias = nullptr;
43     struct HksBlob *peerPublicKey = nullptr;
44     struct HksBlob *agreedKey = nullptr;
45 };
46 using AgreeKeyAsyncContext = AgreeKeyAsyncContextT *;
47 
CreateAgreeKeyAsyncContext()48 static AgreeKeyAsyncContext CreateAgreeKeyAsyncContext()
49 {
50     AgreeKeyAsyncContext context = (AgreeKeyAsyncContext)HksMalloc(sizeof(AgreeKeyAsyncContextT));
51     if (context != nullptr) {
52         (void)memset_s(context, sizeof(AgreeKeyAsyncContextT), 0, sizeof(AgreeKeyAsyncContextT));
53     }
54     return context;
55 }
56 
DeleteAgreeKeyAsyncContext(napi_env env,AgreeKeyAsyncContext & context)57 static void DeleteAgreeKeyAsyncContext(napi_env env, AgreeKeyAsyncContext &context)
58 {
59     if (context == nullptr) {
60         return;
61     }
62 
63     if (context->asyncWork != nullptr) {
64         napi_delete_async_work(env, context->asyncWork);
65         context->asyncWork = nullptr;
66     }
67 
68     if (context->callback != nullptr) {
69         napi_delete_reference(env, context->callback);
70         context->callback = nullptr;
71     }
72 
73     if (context->keyAlias != nullptr) {
74         FreeHksBlob(context->keyAlias);
75     }
76 
77     if (context->paramSet != nullptr) {
78         HksFreeParamSet(&context->paramSet);
79     }
80 
81     if (context->peerPublicKey != nullptr) {
82         if (context->peerPublicKey->data != nullptr && context->peerPublicKey->size != 0) {
83             (void)memset_s(context->peerPublicKey->data, context->peerPublicKey->size, 0, context->peerPublicKey->size);
84         }
85         FreeHksBlob(context->peerPublicKey);
86     }
87 
88     if (context->agreedKey != nullptr) {
89         if (context->agreedKey->data != nullptr && context->agreedKey->size != 0) {
90             (void)memset_s(context->agreedKey->data, context->agreedKey->size, 0, context->agreedKey->size);
91         }
92         FreeHksBlob(context->agreedKey);
93     }
94 
95     HksFree(context);
96     context = nullptr;
97 }
98 
AgreeKeyParseParams(napi_env env,napi_callback_info info,AgreeKeyAsyncContext context)99 static napi_value AgreeKeyParseParams(napi_env env, napi_callback_info info, AgreeKeyAsyncContext context)
100 {
101     size_t argc = HUKS_NAPI_AGREE_KEY_MAX_ARGS;
102     napi_value argv[HUKS_NAPI_AGREE_KEY_MAX_ARGS] = {0};
103     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
104 
105     if (argc < HUKS_NAPI_AGREE_KEY_MIN_ARGS) {
106         napi_throw_error(env, NULL, "invalid arguments");
107         HKS_LOG_E("no enough params");
108         return nullptr;
109     }
110 
111     size_t index = 0;
112     napi_value result = ParseKeyAlias(env, argv[index], context->keyAlias);
113     if (result == nullptr) {
114         HKS_LOG_E("could not get alias");
115         return nullptr;
116     }
117 
118     index++;
119     napi_value properties = nullptr;
120     napi_status status =
121         napi_get_named_property(env, argv[index], HKS_OPTIONS_PROPERTY_PROPERTIES.c_str(), &properties);
122     if (status != napi_ok || properties == nullptr) {
123         GET_AND_THROW_LAST_ERROR((env));
124         HKS_LOG_E("could not get property %s", HKS_OPTIONS_PROPERTY_PROPERTIES.c_str());
125         return nullptr;
126     }
127     result = ParseHksParamSet(env, properties, context->paramSet);
128     if (result == nullptr) {
129         HKS_LOG_E("could not get paramset");
130         return nullptr;
131     }
132 
133     napi_value inData = nullptr;
134     status = napi_get_named_property(env, argv[index], HKS_OPTIONS_PROPERTY_INDATA.c_str(), &inData);
135     if (status != napi_ok || inData == nullptr) {
136         GET_AND_THROW_LAST_ERROR((env));
137         HKS_LOG_E("could not get property %s", HKS_OPTIONS_PROPERTY_INDATA.c_str());
138         return nullptr;
139     }
140     context->peerPublicKey = (HksBlob *)HksMalloc(sizeof(HksBlob));
141     if (context->peerPublicKey == nullptr) {
142         HKS_LOG_E("could not alloc memory");
143         return nullptr;
144     }
145     (void)memset_s(context->peerPublicKey, sizeof(HksBlob), 0, sizeof(HksBlob));
146 
147     if (GetUint8Array(env, inData, *context->peerPublicKey) == nullptr) {
148         HKS_LOG_E("could not get indata");
149         return nullptr;
150     }
151 
152     index++;
153     if (index < argc) {
154         context->callback = GetCallback(env, argv[index]);
155     }
156 
157     return GetInt32(env, 0);
158 }
159 
AgreeKeyWriteResult(napi_env env,AgreeKeyAsyncContext context)160 static napi_value AgreeKeyWriteResult(napi_env env, AgreeKeyAsyncContext context)
161 {
162     return GenerateHksResult(env,
163         context->result,
164         ((context->result == HKS_SUCCESS && context->agreedKey != nullptr) ? context->agreedKey->data : nullptr),
165         (context->result == HKS_SUCCESS && context->agreedKey != nullptr) ? context->agreedKey->size : 0);
166 }
167 
AgreeKeyAsyncWork(napi_env env,AgreeKeyAsyncContext context)168 static napi_value AgreeKeyAsyncWork(napi_env env, AgreeKeyAsyncContext context)
169 {
170     napi_value promise = nullptr;
171     if (context->callback == nullptr) {
172         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
173     }
174 
175     napi_value resourceName = nullptr;
176     napi_create_string_latin1(env, "agreeKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
177 
178     napi_create_async_work(
179         env,
180         nullptr,
181         resourceName,
182         [](napi_env env, void *data) {
183             AgreeKeyAsyncContext context = static_cast<AgreeKeyAsyncContext>(data);
184 
185             context->agreedKey = (HksBlob *)HksMalloc(sizeof(HksBlob));
186             if (context->agreedKey != nullptr) {
187                 context->agreedKey->data = (uint8_t *)HksMalloc(HKS_MAX_AGREED_KEY_SIZE);
188                 context->agreedKey->size = HKS_MAX_AGREED_KEY_SIZE;
189             }
190 
191             context->result =
192                 HksAgreeKey(context->paramSet, context->keyAlias, context->peerPublicKey, context->agreedKey);
193         },
194         [](napi_env env, napi_status status, void *data) {
195             AgreeKeyAsyncContext context = static_cast<AgreeKeyAsyncContext>(data);
196             napi_value result = AgreeKeyWriteResult(env, context);
197             if (result == nullptr) {
198                 return;
199             }
200             if (context->callback != nullptr) {
201                 CallAsyncCallback(env, context->callback, context->result, result);
202             } else {
203                 napi_resolve_deferred(env, context->deferred, result);
204             }
205             DeleteAgreeKeyAsyncContext(env, context);
206         },
207         (void *)context,
208         &context->asyncWork);
209 
210     napi_status status = napi_queue_async_work(env, context->asyncWork);
211     if (status != napi_ok) {
212         GET_AND_THROW_LAST_ERROR((env));
213         DeleteAgreeKeyAsyncContext(env, context);
214         HKS_LOG_E("could not queue async work");
215         return nullptr;
216     }
217 
218     if (context->callback == nullptr) {
219         return promise;
220     } else {
221         return GetNull(env);
222     }
223 }
224 
HuksNapiAgreeKey(napi_env env,napi_callback_info info)225 napi_value HuksNapiAgreeKey(napi_env env, napi_callback_info info)
226 {
227     AgreeKeyAsyncContext context = CreateAgreeKeyAsyncContext();
228     if (context == nullptr) {
229         HKS_LOG_E("could not create context");
230         return nullptr;
231     }
232 
233     napi_value result = AgreeKeyParseParams(env, info, context);
234     if (result == nullptr) {
235         HKS_LOG_E("could not parse params");
236         DeleteAgreeKeyAsyncContext(env, context);
237         return nullptr;
238     }
239 
240     result = AgreeKeyAsyncWork(env, context);
241     if (result == nullptr) {
242         HKS_LOG_E("could not start async work");
243         DeleteAgreeKeyAsyncContext(env, context);
244         return nullptr;
245     }
246     return result;
247 }
248 }  // namespace HuksNapi
249