1 /*
2  * Copyright (c) 2024 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_as_user.h"
17 #include "huks_napi_import_wrapped_key_item.h"
18 #include "huks_napi_common_item.h"
19 
20 #include "securec.h"
21 
22 #include "hks_api.h"
23 #include "hks_log.h"
24 #include "hks_mem.h"
25 #include "hks_param.h"
26 #include "hks_type.h"
27 
28 namespace HuksNapiItem {
29 constexpr int HUKS_NAPI_IMPORT_WRAPPED_KEY_AS_USER_ARGS_COUNT = 4;
ImportWrappedKeyAsUserParseParams(napi_env env,napi_callback_info info,ImportWrappedKeyAsyncContext context)30 static napi_value ImportWrappedKeyAsUserParseParams(napi_env env, napi_callback_info info,
31     ImportWrappedKeyAsyncContext context)
32 {
33     size_t argc = HUKS_NAPI_IMPORT_WRAPPED_KEY_AS_USER_ARGS_COUNT;
34     napi_value argv[HUKS_NAPI_IMPORT_WRAPPED_KEY_AS_USER_ARGS_COUNT] = { 0 };
35     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
36 
37     if (argc != HUKS_NAPI_IMPORT_WRAPPED_KEY_AS_USER_ARGS_COUNT) {
38         HksNapiThrowInvalidParamCount(env);
39         HKS_LOG_E("invalid params count %" LOG_PUBLIC "zu", argc);
40         return nullptr;
41     }
42 
43     int userId = 0;
44     size_t index = 0;
45     napi_value result = GetUserIdValue(env, argv[index], userId);
46     if (result == nullptr) {
47         HksNapiThrowGetUserIdFail(env);
48         HKS_LOG_E("ImportWrappedKeyAsUserParseParams could not get user id value");
49         return nullptr;
50     }
51     index++;
52     result = ParseKeyAlias(env, argv[index], context->keyAlias);
53     if (result == nullptr) {
54         HksNapiThrow(env, HUKS_ERR_CODE_ILLEGAL_ARGUMENT, "could not get key alias");
55         HKS_LOG_E("importWrappedKey parse keyAlias failed");
56         return nullptr;
57     }
58 
59     index++;
60     result = ParseKeyAliasAndHksParamSetAsUser(env, userId, argv, index,
61         {context->wrappingKeyAlias, context->paramSet});
62     if (result == nullptr) {
63         HKS_LOG_E("importWrappedKey parse params failed");
64         return nullptr;
65     }
66 
67     result = ParseKeyData(env, argv[index], context->wrappedData);
68     if (result == nullptr) {
69         HKS_LOG_E("importWrappedKey parse keyData failed");
70         return nullptr;
71     }
72 
73     return GetInt32(env, 0);
74 }
75 
HuksNapiImportWrappedKeyItemAsUser(napi_env env,napi_callback_info info)76 napi_value HuksNapiImportWrappedKeyItemAsUser(napi_env env, napi_callback_info info)
77 {
78     ImportWrappedKeyAsyncContext context = CreateImportWrappedKeyAsyncContext();
79     if (context == nullptr) {
80         HksNapiThrowInsufficientMemory(env);
81         HKS_LOG_E("could not create context");
82         return nullptr;
83     }
84 
85     napi_value result = ImportWrappedKeyAsUserParseParams(env, info, context);
86     if (result == nullptr) {
87         HKS_LOG_E("could not parse params");
88         DeleteImportWrappedKeyAsyncContext(env, context);
89         return nullptr;
90     }
91 
92     result = ImportWrappedKeyAsyncWork(env, context);
93     if (result == nullptr) {
94         HKS_LOG_E("could not start async work");
95         DeleteImportWrappedKeyAsyncContext(env, context);
96         return nullptr;
97     }
98     return result;
99 }
100 }  // namespace HuksNapiItem
101