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_import_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_IMPORT_KEY_MIN_ARGS = 2;
30 constexpr int HUKS_NAPI_IMPORT_KEY_MAX_ARGS = 3;
31 }  // namespace
32 
33 struct ImportKeyAsyncContextT {
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 ImportKeyAsyncContext = ImportKeyAsyncContextT *;
44 
CreateImportKeyAsyncContext()45 static ImportKeyAsyncContext CreateImportKeyAsyncContext()
46 {
47     ImportKeyAsyncContext context = static_cast<ImportKeyAsyncContext>(HksMalloc(sizeof(ImportKeyAsyncContextT)));
48     if (context != nullptr) {
49         (void)memset_s(context, sizeof(ImportKeyAsyncContextT), 0, sizeof(ImportKeyAsyncContextT));
50     }
51     return context;
52 }
53 
DeleteImportKeyAsyncContext(napi_env env,ImportKeyAsyncContext & context)54 static void DeleteImportKeyAsyncContext(napi_env env, ImportKeyAsyncContext &context)
55 {
56     if (context == nullptr) {
57         return;
58     }
59     DeleteCommonAsyncContext(env, context->asyncWork, context->callback, context->keyAlias, context->paramSet);
60     if (context->key != nullptr) {
61         if (context->key->data != nullptr && context->key->size != 0) {
62             (void)memset_s(context->key->data, context->key->size, 0, context->key->size);
63         }
64         FreeHksBlob(context->key);
65     }
66     HKS_FREE(context);
67     context = nullptr;
68 }
69 
ImportKeyParseParams(napi_env env,napi_callback_info info,ImportKeyAsyncContext context)70 static napi_value ImportKeyParseParams(napi_env env, napi_callback_info info, ImportKeyAsyncContext context)
71 {
72     size_t argc = HUKS_NAPI_IMPORT_KEY_MAX_ARGS;
73     napi_value argv[HUKS_NAPI_IMPORT_KEY_MAX_ARGS] = { 0 };
74     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
75 
76     if (argc < HUKS_NAPI_IMPORT_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("importKey parse keyAlias or paramSet failed");
86         return nullptr;
87     }
88 
89     result = ParseKeyData(env, argv[index], context->key);
90     if (result == nullptr) {
91         HKS_LOG_E("importKey parse keyData failed");
92         return nullptr;
93     }
94 
95     index++;
96     if (index < argc) {
97         context->callback = GetCallback(env, argv[index]);
98     }
99 
100     return GetInt32(env, 0);
101 }
102 
ImportKeyWriteResult(napi_env env,ImportKeyAsyncContext context)103 static napi_value ImportKeyWriteResult(napi_env env, ImportKeyAsyncContext context)
104 {
105     return GenerateHksResult(env, context->result, nullptr, 0);
106 }
107 
ImportKeyAsyncWork(napi_env env,ImportKeyAsyncContext & context)108 static napi_value ImportKeyAsyncWork(napi_env env, ImportKeyAsyncContext &context)
109 {
110     napi_value promise = nullptr;
111     if (context->callback == nullptr) {
112         NAPI_CALL(env, napi_create_promise(env, &context->deferred, &promise));
113     }
114 
115     napi_value resourceName = nullptr;
116     napi_create_string_latin1(env, "importKeyAsyncWork", NAPI_AUTO_LENGTH, &resourceName);
117 
118     napi_create_async_work(
119         env,
120         nullptr,
121         resourceName,
122         [](napi_env env, void *data) {
123             ImportKeyAsyncContext napiContext = static_cast<ImportKeyAsyncContext>(data);
124 
125             napiContext->result = HksImportKey(napiContext->keyAlias,
126                 napiContext->paramSet, napiContext->key);
127         },
128         [](napi_env env, napi_status status, void *data) {
129             ImportKeyAsyncContext napiContext = static_cast<ImportKeyAsyncContext>(data);
130             napi_value result = ImportKeyWriteResult(env, napiContext);
131             if (napiContext->callback == nullptr) {
132                 napi_resolve_deferred(env, napiContext->deferred, result);
133             } else if (result != nullptr) {
134                 CallAsyncCallback(env, napiContext->callback, napiContext->result, result);
135             }
136             DeleteImportKeyAsyncContext(env, napiContext);
137         },
138         static_cast<void *>(context),
139         &context->asyncWork);
140 
141     napi_status status = napi_queue_async_work(env, context->asyncWork);
142     if (status != napi_ok) {
143         GET_AND_THROW_LAST_ERROR((env));
144         DeleteImportKeyAsyncContext(env, context);
145         HKS_LOG_E("could not queue async work");
146         return nullptr;
147     }
148 
149     if (context->callback == nullptr) {
150         return promise;
151     } else {
152         return GetNull(env);
153     }
154 }
155 
HuksNapiImportKey(napi_env env,napi_callback_info info)156 napi_value HuksNapiImportKey(napi_env env, napi_callback_info info)
157 {
158     ImportKeyAsyncContext context = CreateImportKeyAsyncContext();
159     if (context == nullptr) {
160         HKS_LOG_E("could not create context");
161         return nullptr;
162     }
163 
164     napi_value result = ImportKeyParseParams(env, info, context);
165     if (result == nullptr) {
166         HKS_LOG_E("could not parse params");
167         DeleteImportKeyAsyncContext(env, context);
168         return nullptr;
169     }
170 
171     result = ImportKeyAsyncWork(env, context);
172     if (result == nullptr) {
173         HKS_LOG_E("could not start async work");
174         DeleteImportKeyAsyncContext(env, context);
175         return nullptr;
176     }
177     return result;
178 }
179 }  // namespace HuksNapi
180