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_get_key_item_properties_as_user.h"
17 #include "huks_napi_get_key_item_properties.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_GET_KEY_PROPERTIES_AS_USER_ARGS_COUNT = 3;
30 
GetKeyPropertiesAsUserParseParams(napi_env env,napi_callback_info info,GetKeyPropertiesAsyncContext context)31 static napi_value GetKeyPropertiesAsUserParseParams(
32     napi_env env, napi_callback_info info, GetKeyPropertiesAsyncContext context)
33 {
34     size_t argc = HUKS_NAPI_GET_KEY_PROPERTIES_AS_USER_ARGS_COUNT;
35     napi_value argv[HUKS_NAPI_GET_KEY_PROPERTIES_AS_USER_ARGS_COUNT] = { 0 };
36     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
37 
38     if (argc != HUKS_NAPI_GET_KEY_PROPERTIES_AS_USER_ARGS_COUNT) {
39         HksNapiThrowInvalidParamCount(env);
40         HKS_LOG_E("invalid params count %" LOG_PUBLIC "zu", argc);
41         return nullptr;
42     }
43 
44     int userId = 0;
45     size_t index = 0;
46     napi_value result = GetUserIdValue(env, argv[index], userId);
47     if (result == nullptr) {
48         HksNapiThrowGetUserIdFail(env);
49         HKS_LOG_E("GetKeyPropertiesAsUserParseParams could not get user id value");
50         return nullptr;
51     }
52     index++;
53     result = ParseKeyAliasAndHksParamSetAsUser(env, userId, argv, index, {context->keyAlias, context->paramSetIn});
54     if (result == nullptr) {
55         HKS_LOG_E("getKeyProperties parse params failed");
56         return nullptr;
57     }
58 
59     return GetInt32(env, 0);
60 }
61 
HuksNapiGetKeyItemPropertiesAsUser(napi_env env,napi_callback_info info)62 napi_value HuksNapiGetKeyItemPropertiesAsUser(napi_env env, napi_callback_info info)
63 {
64     GetKeyPropertiesAsyncContext context = CreateGetKeyPropertiesAsyncContext();
65     if (context == nullptr) {
66         HksNapiThrowInsufficientMemory(env);
67         HKS_LOG_E("could not create context");
68         return nullptr;
69     }
70 
71     napi_value result = GetKeyPropertiesAsUserParseParams(env, info, context);
72     if (result == nullptr) {
73         HKS_LOG_E("could not parse params");
74         DeleteGetKeyPropertiesAsyncContext(env, context);
75         return nullptr;
76     }
77 
78     result = GetKeyPropertiesAsyncWork(env, context);
79     if (result == nullptr) {
80         HKS_LOG_E("could not start async work");
81         DeleteGetKeyPropertiesAsyncContext(env, context);
82         return nullptr;
83     }
84     return result;
85 }
86 }  // namespace HuksNapiItem
87