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_export_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_EXPORT_KEY_MIN_ARGS = 2;
30 constexpr int HUKS_NAPI_EXPORT_KEY_MAX_ARGS = 3;
31 }  // namespace
32 
33 struct ExportKeyAsyncContextT {
34     napi_async_work asyncWork = nullptr;
35     napi_deferred deferred = nullptr;
36     napi_ref callback = nullptr;
37 
38     int32_t result = 0;
39     struct HksBlob *keyAlias = nullptr;
40     struct HksParamSet *paramSet = nullptr;
41     struct HksBlob *key = nullptr;
42 };
43 using ExportKeyAsyncContext = ExportKeyAsyncContextT *;
44 
CreateExportKeyAsyncContext()45 static ExportKeyAsyncContext CreateExportKeyAsyncContext()
46 {
47     ExportKeyAsyncContext context = static_cast<ExportKeyAsyncContext>(HksMalloc(sizeof(ExportKeyAsyncContextT)));
48     if (context != nullptr) {
49         (void)memset_s(context, sizeof(ExportKeyAsyncContextT), 0, sizeof(ExportKeyAsyncContextT));
50     }
51     return context;
52 }
53 
DeleteExportKeyAsyncContext(napi_env env,ExportKeyAsyncContext & context)54 static void DeleteExportKeyAsyncContext(napi_env env, ExportKeyAsyncContext &context)
55 {
56     if (context == nullptr) {
57         return;
58     }
59     if (context->key != nullptr) {
60         if (context->key->data != nullptr && context->key->size != 0) {
61             (void)memset_s(context->key->data, context->key->size, 0, context->key->size);
62         }
63         FreeHksBlob(context->key);
64     }
65     DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
66     HKS_FREE(context);
67     context = nullptr;
68 }
69 
ExportKeyParseParams(napi_env env,napi_callback_info info,ExportKeyAsyncContext context)70 static napi_value ExportKeyParseParams(napi_env env, napi_callback_info info, ExportKeyAsyncContext context)
71 {
72     size_t argc = HUKS_NAPI_EXPORT_KEY_MAX_ARGS;
73     napi_value argv[HUKS_NAPI_EXPORT_KEY_MAX_ARGS] = { 0 };
74     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
75 
76     if (argc < HUKS_NAPI_EXPORT_KEY_MIN_ARGS) {
77         napi_throw_error(env, nullptr, "invalid arguments");
78         HKS_LOG_E("no enough params");
79         return nullptr;
80     }
81 
82     size_t index = 0;
83     napi_value result = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSet);
84     if (result == nullptr) {
85         HKS_LOG_E("exportKey parse params failed");
86         return nullptr;
87     }
88 
89     index++;
90     if (index < argc) {
91         context->callback = GetCallback(env, argv[index]);
92     }
93 
94     return GetInt32(env, 0);
95 }
96 
ExportKeyWriteResult(napi_env env,ExportKeyAsyncContext context)97 static napi_value ExportKeyWriteResult(napi_env env, ExportKeyAsyncContext context)
98 {
99     return GenerateHksResult(env,
100         context->result,
101         ((context->result == HKS_SUCCESS && context->key != nullptr) ? context->key->data : nullptr),
102         (context->result == HKS_SUCCESS && context->key != nullptr) ? context->key->size : 0);
103 }
104 
PrePareExportKeyContextBuffer(ExportKeyAsyncContext context)105 static int32_t PrePareExportKeyContextBuffer(ExportKeyAsyncContext context)
106 {
107     context->key = static_cast<HksBlob *>(HksMalloc(sizeof(HksBlob)));
108     if (context->key == nullptr) {
109         return HKS_ERROR_MALLOC_FAIL;
110     }
111 
112     context->key->data = static_cast<uint8_t *>(HksMalloc(MAX_KEY_SIZE));
113     if (context->key->data == nullptr) {
114         return HKS_ERROR_MALLOC_FAIL;
115     }
116     context->key->size = MAX_KEY_SIZE;
117     return HKS_SUCCESS;
118 }
119 
ExportKeyAsyncWork(napi_env env,ExportKeyAsyncContext & context)120 static napi_value ExportKeyAsyncWork(napi_env env, ExportKeyAsyncContext &context)
121 {
122     napi_value promise = nullptr;
123     if (context->callback == nullptr) {
124         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
125     }
126 
127     napi_value resourceName = nullptr;
128     napi_create_string_latin1(env, "exportKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
129 
130     napi_create_async_work(
131         env,
132         nullptr,
133         resourceName,
134         [](napi_env env, void *data) {
135             (void)env;
136             ExportKeyAsyncContext napiContext = static_cast<ExportKeyAsyncContext>(data);
137             int32_t ret = PrePareExportKeyContextBuffer(napiContext);
138             if (ret == HKS_SUCCESS) {
139                 napiContext->result = HksExportPublicKey(napiContext->keyAlias,
140                     napiContext->paramSet, napiContext->key);
141             } else {
142                 napiContext->result = ret;
143             }
144         },
145         [](napi_env env, napi_status status, void *data) {
146             ExportKeyAsyncContext napiContext = static_cast<ExportKeyAsyncContext>(data);
147             napi_value result = ExportKeyWriteResult(env, napiContext);
148             if (napiContext->callback == nullptr) {
149                 napi_resolve_deferred(env, napiContext->deferred, result);
150             } else if (result != nullptr) {
151                 CallAsyncCallback(env, napiContext->callback, napiContext->result, result);
152             }
153             DeleteExportKeyAsyncContext(env, napiContext);
154         },
155         static_cast<void *>(context),
156         &context->asyncWork);
157 
158     napi_status status = napi_queue_async_work(env, context->asyncWork);
159     if (status != napi_ok) {
160         GET_AND_THROW_LAST_ERROR((env));
161         DeleteExportKeyAsyncContext(env, context);
162         HKS_LOG_E("could not queue async work");
163         return nullptr;
164     }
165 
166     if (context->callback == nullptr) {
167         return promise;
168     } else {
169         return GetNull(env);
170     }
171 }
172 
HuksNapiExportKey(napi_env env,napi_callback_info info)173 napi_value HuksNapiExportKey(napi_env env, napi_callback_info info)
174 {
175     ExportKeyAsyncContext context = CreateExportKeyAsyncContext();
176     if (context == nullptr) {
177         HKS_LOG_E("could not create context");
178         return nullptr;
179     }
180 
181     napi_value result = ExportKeyParseParams(env, info, context);
182     if (result == nullptr) {
183         HKS_LOG_E("could not parse params");
184         DeleteExportKeyAsyncContext(env, context);
185         return nullptr;
186     }
187 
188     result = ExportKeyAsyncWork(env, context);
189     if (result == nullptr) {
190         HKS_LOG_E("could not start async work");
191         DeleteExportKeyAsyncContext(env, context);
192         return nullptr;
193     }
194     return result;
195 }
196 }  // namespace HuksNapi
197