1 /*
2 * Copyright (c) 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_item.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_item.h"
26
27 namespace HuksNapiItem {
28 constexpr int HUKS_NAPI_EXPORT_KEY_MIN_ARGS = 2;
29 constexpr int HUKS_NAPI_EXPORT_KEY_MAX_ARGS = 3;
30
CreateExportKeyAsyncContext()31 ExportKeyAsyncContext CreateExportKeyAsyncContext()
32 {
33 ExportKeyAsyncContext context = static_cast<ExportKeyAsyncContext>(HksMalloc(sizeof(ExportKeyAsyncContextT)));
34 if (context != nullptr) {
35 (void)memset_s(context, sizeof(ExportKeyAsyncContextT), 0, sizeof(ExportKeyAsyncContextT));
36 }
37 return context;
38 }
39
DeleteExportKeyAsyncContext(napi_env env,ExportKeyAsyncContext & context)40 void DeleteExportKeyAsyncContext(napi_env env, ExportKeyAsyncContext &context)
41 {
42 if (context == nullptr) {
43 return;
44 }
45 if (context->key != nullptr) {
46 if (context->key->data != nullptr && context->key->size != 0) {
47 (void)memset_s(context->key->data, context->key->size, 0, context->key->size);
48 }
49 FreeHksBlob(context->key);
50 }
51 DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
52 HKS_FREE(context);
53 context = nullptr;
54 }
55
ExportKeyParseParams(napi_env env,napi_callback_info info,ExportKeyAsyncContext context)56 static napi_value ExportKeyParseParams(napi_env env, napi_callback_info info, ExportKeyAsyncContext context)
57 {
58 size_t argc = HUKS_NAPI_EXPORT_KEY_MAX_ARGS;
59 napi_value argv[HUKS_NAPI_EXPORT_KEY_MAX_ARGS] = { 0 };
60 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
61
62 if (argc < HUKS_NAPI_EXPORT_KEY_MIN_ARGS) {
63 HksNapiThrow(env, HUKS_ERR_CODE_ILLEGAL_ARGUMENT, "no enough params input");
64 HKS_LOG_E("no enough params");
65 return nullptr;
66 }
67
68 size_t index = 0;
69 napi_value result = ParseKeyAliasAndHksParamSet(env, argv, index, context->keyAlias, context->paramSet);
70 if (result == nullptr) {
71 HKS_LOG_E("exportKey parse params failed");
72 return nullptr;
73 }
74
75 index++;
76 if (index < argc) {
77 context->callback = GetCallback(env, argv[index]);
78 }
79
80 return GetInt32(env, 0);
81 }
82
PrePareExportKeyContextBuffer(ExportKeyAsyncContext context)83 static int32_t PrePareExportKeyContextBuffer(ExportKeyAsyncContext context)
84 {
85 context->key = static_cast<struct HksBlob *>(HksMalloc(sizeof(HksBlob)));
86 if (context->key == nullptr) {
87 return HKS_ERROR_MALLOC_FAIL;
88 }
89
90 context->key->data = static_cast<uint8_t *>(HksMalloc(MAX_KEY_SIZE));
91 if (context->key->data == nullptr) {
92 return HKS_ERROR_MALLOC_FAIL;
93 }
94 context->key->size = MAX_KEY_SIZE;
95 return HKS_SUCCESS;
96 }
97
ExportKeyAsyncWork(napi_env env,ExportKeyAsyncContext & context)98 napi_value ExportKeyAsyncWork(napi_env env, ExportKeyAsyncContext &context)
99 {
100 napi_value promise = nullptr;
101 if (context->callback == nullptr) {
102 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
103 }
104
105 napi_value resourceName = nullptr;
106 napi_create_string_latin1(env, "exportKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
107
108 napi_create_async_work(
109 env,
110 nullptr,
111 resourceName,
112 [](napi_env env, void *data) {
113 (void)env;
114 ExportKeyAsyncContext napiContext = static_cast<ExportKeyAsyncContext>(data);
115 int32_t ret = PrePareExportKeyContextBuffer(napiContext);
116 if (ret == HKS_SUCCESS) {
117 napiContext->result = HksExportPublicKey(napiContext->keyAlias,
118 napiContext->paramSet, napiContext->key);
119 } else {
120 napiContext->result = ret;
121 }
122 },
123 [](napi_env env, napi_status status, void *data) {
124 ExportKeyAsyncContext napiContext = static_cast<ExportKeyAsyncContext>(data);
125 HksSuccessReturnResult resultData;
126 SuccessReturnResultInit(resultData);
127 resultData.outData = napiContext->key;
128 HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result, resultData);
129 DeleteExportKeyAsyncContext(env, napiContext);
130 },
131 static_cast<void *>(context),
132 &context->asyncWork);
133
134 napi_status status = napi_queue_async_work(env, context->asyncWork);
135 if (status != napi_ok) {
136 DeleteExportKeyAsyncContext(env, context);
137 HKS_LOG_E("could not queue async work");
138 return nullptr;
139 }
140
141 if (context->callback == nullptr) {
142 return promise;
143 } else {
144 return GetNull(env);
145 }
146 }
147
HuksNapiExportKeyItem(napi_env env,napi_callback_info info)148 napi_value HuksNapiExportKeyItem(napi_env env, napi_callback_info info)
149 {
150 ExportKeyAsyncContext context = CreateExportKeyAsyncContext();
151 if (context == nullptr) {
152 HKS_LOG_E("could not create context");
153 return nullptr;
154 }
155
156 napi_value result = ExportKeyParseParams(env, info, context);
157 if (result == nullptr) {
158 HKS_LOG_E("could not parse params");
159 DeleteExportKeyAsyncContext(env, context);
160 return nullptr;
161 }
162
163 result = ExportKeyAsyncWork(env, context);
164 if (result == nullptr) {
165 HKS_LOG_E("could not start async work");
166 DeleteExportKeyAsyncContext(env, context);
167 return nullptr;
168 }
169 return result;
170 }
171 } // namespace HuksNapiItem
172