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_attest_key_item_as_user.h"
17 #include "huks_napi_attest_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_ATTEST_KEY_AS_USER_ARGS_COUNT = 3;
AttestKeyAsUserParseParams(napi_env env,napi_callback_info info,AttestKeyAsyncContext context)30 static napi_value AttestKeyAsUserParseParams(napi_env env, napi_callback_info info, AttestKeyAsyncContext context)
31 {
32     size_t argc = HUKS_NAPI_ATTEST_KEY_AS_USER_ARGS_COUNT;
33     napi_value argv[HUKS_NAPI_ATTEST_KEY_AS_USER_ARGS_COUNT] = { 0 };
34     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
35 
36     if (argc != HUKS_NAPI_ATTEST_KEY_AS_USER_ARGS_COUNT) {
37         HksNapiThrowInvalidParamCount(env);
38         HKS_LOG_E("no enough params");
39         return nullptr;
40     }
41 
42     int userId = 0;
43     size_t index = 0;
44     napi_value result = GetUserIdValue(env, argv[index], userId);
45     if (result == nullptr) {
46         HksNapiThrowGetUserIdFail(env);
47         HKS_LOG_E("AttestKeyAsUserParseParams could not get user id value");
48         return nullptr;
49     }
50     index++;
51     result = ParseKeyAlias(env, argv[index], context->keyAlias);
52     if (result == nullptr) {
53         HksNapiThrow(env, HUKS_ERR_CODE_ILLEGAL_ARGUMENT, "could not get key alias");
54         HKS_LOG_E("could not get alias");
55         return nullptr;
56     }
57 
58     index++;
59     napi_value property = GetPropertyFromOptions(env, argv[index], HKS_OPTIONS_PROPERTY_PROPERTIES);
60     if (property == nullptr) {
61         HKS_LOG_E("attestKey get property failed");
62         return nullptr;
63     }
64     result = ParseHksParamSetAndAddParam(env, property, context->paramSet,
65         {{ .tag = HKS_TAG_ATTESTATION_BASE64, .boolParam = true },
66         {.tag = HKS_TAG_SPECIFIC_USER_ID, .int32Param = userId}});
67     if (result == nullptr) {
68         HKS_LOG_E("could not get paramset");
69         return nullptr;
70     }
71 
72     return GetInt32(env, 0);
73 }
74 
HuksNapiAttestKeyItemAsUser(napi_env env,napi_callback_info info,bool isAnon)75 napi_value HuksNapiAttestKeyItemAsUser(napi_env env, napi_callback_info info, bool isAnon)
76 {
77     AttestKeyAsyncContext context = CreateAttestKeyAsyncContext(isAnon);
78     if (context == nullptr) {
79         HksNapiThrowInsufficientMemory(env);
80         HKS_LOG_E("could not create context");
81         return nullptr;
82     }
83 
84     napi_value result = AttestKeyAsUserParseParams(env, info, context);
85     if (result == nullptr) {
86         HKS_LOG_E("could not parse params");
87         DeleteAttestKeyAsyncContext(env, context);
88         return nullptr;
89     }
90     result = AttestKeyAsyncWork(env, context);
91     if (result == nullptr) {
92         HKS_LOG_E("could not start async work");
93         DeleteAttestKeyAsyncContext(env, context);
94         return nullptr;
95     }
96     return result;
97 }
98 
HuksNapiAttestKeyItemAsUser(napi_env env,napi_callback_info info)99 napi_value HuksNapiAttestKeyItemAsUser(napi_env env, napi_callback_info info)
100 {
101     return HuksNapiAttestKeyItemAsUser(env, info, false);
102 }
103 
HuksNapiAnonAttestKeyItemAsUser(napi_env env,napi_callback_info info)104 napi_value HuksNapiAnonAttestKeyItemAsUser(napi_env env, napi_callback_info info)
105 {
106     return HuksNapiAttestKeyItemAsUser(env, info, true);
107 }
108 }  // namespace HuksNapiItem