• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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     HksFree(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, std::to_string(HUKS_ERR_CODE_ILLEGAL_ARGUMENT).c_str(), "no enough params input");
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 
PrePareExportKeyContextBuffer(ExportKeyAsyncContext context)97 static int32_t PrePareExportKeyContextBuffer(ExportKeyAsyncContext context)
98 {
99     context->key = static_cast<struct HksBlob *>(HksMalloc(sizeof(HksBlob)));
100     if (context->key == nullptr) {
101         return HKS_ERROR_MALLOC_FAIL;
102     }
103 
104     context->key->data = static_cast<uint8_t *>(HksMalloc(MAX_KEY_SIZE));
105     if (context->key->data == nullptr) {
106         return HKS_ERROR_MALLOC_FAIL;
107     }
108     context->key->size = MAX_KEY_SIZE;
109     return HKS_SUCCESS;
110 }
111 
ExportKeyAsyncWork(napi_env env,ExportKeyAsyncContext context)112 static napi_value ExportKeyAsyncWork(napi_env env, ExportKeyAsyncContext context)
113 {
114     napi_value promise = nullptr;
115     if (context->callback == nullptr) {
116         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
117     }
118 
119     napi_value resourceName = nullptr;
120     napi_create_string_latin1(env, "exportKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
121 
122     napi_create_async_work(
123         env,
124         nullptr,
125         resourceName,
126         [](napi_env env, void *data) {
127             (void)env;
128             ExportKeyAsyncContext napiContext = static_cast<ExportKeyAsyncContext>(data);
129             int32_t ret = PrePareExportKeyContextBuffer(napiContext);
130             if (ret == HKS_SUCCESS) {
131                 napiContext->result = HksExportPublicKey(napiContext->keyAlias,
132                     napiContext->paramSet, napiContext->key);
133             } else {
134                 napiContext->result = ret;
135             }
136         },
137         [](napi_env env, napi_status status, void *data) {
138             ExportKeyAsyncContext napiContext = static_cast<ExportKeyAsyncContext>(data);
139             HksSuccessReturnResult resultData;
140             SuccessReturnResultInit(resultData);
141             resultData.outData = napiContext->key;
142             HksReturnNapiResult(env, napiContext->callback, napiContext->deferred, napiContext->result, resultData);
143             DeleteExportKeyAsyncContext(env, napiContext);
144         },
145         static_cast<void *>(context),
146         &context->asyncWork);
147 
148     napi_status status = napi_queue_async_work(env, context->asyncWork);
149     if (status != napi_ok) {
150         DeleteExportKeyAsyncContext(env, context);
151         HKS_LOG_E("could not queue async work");
152         return nullptr;
153     }
154 
155     if (context->callback == nullptr) {
156         return promise;
157     } else {
158         return GetNull(env);
159     }
160 }
161 
HuksNapiExportKeyItem(napi_env env,napi_callback_info info)162 napi_value HuksNapiExportKeyItem(napi_env env, napi_callback_info info)
163 {
164     ExportKeyAsyncContext context = CreateExportKeyAsyncContext();
165     if (context == nullptr) {
166         HKS_LOG_E("could not create context");
167         return nullptr;
168     }
169 
170     napi_value result = ExportKeyParseParams(env, info, context);
171     if (result == nullptr) {
172         HKS_LOG_E("could not parse params");
173         DeleteExportKeyAsyncContext(env, context);
174         return nullptr;
175     }
176 
177     result = ExportKeyAsyncWork(env, context);
178     if (result == nullptr) {
179         HKS_LOG_E("could not start async work");
180         DeleteExportKeyAsyncContext(env, context);
181         return nullptr;
182     }
183     return result;
184 }
185 }  // namespace HuksNapiItem
186