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