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 "cm_napi_set_cert_status.h"
17 
18 #include "securec.h"
19 
20 #include "cert_manager_api.h"
21 #include "cm_log.h"
22 #include "cm_mem.h"
23 #include "cm_type.h"
24 #include "cm_napi_common.h"
25 
26 namespace CMNapi {
27 namespace {
28 constexpr int CM_NAPI_SET_CERT_STATUS_MIN_ARGS = 3;
29 constexpr int CM_NAPI_SET_CERT_STATUS_MAX_ARGS = 4;
30 }  // namespace
31 
32 struct SetCertStatusAsyncContextT {
33     napi_async_work asyncWork = nullptr;
34     napi_deferred deferred = nullptr;
35     napi_ref callback = nullptr;
36 
37     int32_t result = 0;
38     struct CmBlob *certUri = nullptr;
39     uint32_t store = 0;
40     bool status = false;
41 };
42 using SetCertStatusAsyncContext = SetCertStatusAsyncContextT *;
43 
CreateSetCertStatusAsyncContext()44 static SetCertStatusAsyncContext CreateSetCertStatusAsyncContext()
45 {
46     SetCertStatusAsyncContext context =
47         static_cast<SetCertStatusAsyncContext>(CmMalloc(sizeof(SetCertStatusAsyncContextT)));
48     if (context != nullptr) {
49         (void)memset_s(
50             context, sizeof(SetCertStatusAsyncContextT), 0, sizeof(SetCertStatusAsyncContextT));
51     }
52     return context;
53 }
54 
DeleteSetCertStatusAsyncContext(napi_env env,SetCertStatusAsyncContext & context)55 static void DeleteSetCertStatusAsyncContext(napi_env env, SetCertStatusAsyncContext &context)
56 {
57     if (context == nullptr) {
58         return;
59     }
60 
61     DeleteNapiContext(env, context->asyncWork, context->callback);
62 
63     if (context->certUri != nullptr) {
64         FreeCmBlob(context->certUri);
65     }
66 
67     CmFree(context);
68     context = nullptr;
69 }
70 
SetCertStatusParseParams(napi_env env,napi_callback_info info,SetCertStatusAsyncContext context)71 static napi_value SetCertStatusParseParams(
72     napi_env env, napi_callback_info info, SetCertStatusAsyncContext context)
73 {
74     size_t argc = CM_NAPI_SET_CERT_STATUS_MAX_ARGS;
75     napi_value argv[CM_NAPI_SET_CERT_STATUS_MAX_ARGS] = { nullptr };
76     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
77 
78     if ((argc != CM_NAPI_SET_CERT_STATUS_MIN_ARGS) && (argc != CM_NAPI_SET_CERT_STATUS_MAX_ARGS)) {
79         ThrowError(env, PARAM_ERROR, "arguments count invalid.");
80         CM_LOG_E("arguments count invalid. argc = %d", argc);
81         return nullptr;
82     }
83 
84     size_t index = 0;
85     napi_value result = ParseString(env, argv[index], context->certUri);
86     if (result == nullptr) {
87         ThrowError(env, PARAM_ERROR, "get certUri type error");
88         CM_LOG_E("could not get cert uri when set cert status");
89         return nullptr;
90     }
91 
92     index++;
93     result = ParseUint32(env, argv[index], context->store);
94     if (result == nullptr) {
95         ThrowError(env, PARAM_ERROR, "get store type error");
96         CM_LOG_E("could not get store");
97         return nullptr;
98     }
99 
100     index++;
101     result = ParseBoolean(env, argv[index], context->status);
102     if (result == nullptr) {
103         ThrowError(env, PARAM_ERROR, "get status type error");
104         CM_LOG_E("could not get status");
105         return nullptr;
106     }
107 
108     index++;
109     if (index < argc) {
110         int32_t ret = GetCallback(env, argv[index], context->callback);
111         if (ret != CM_SUCCESS) {
112             ThrowError(env, PARAM_ERROR, "Get callback type failed.");
113             CM_LOG_E("get callback function failed when set cert status function");
114             return nullptr;
115         }
116     }
117     return GetInt32(env, 0);
118 }
119 
SetCertStatusExecute(napi_env env,void * data)120 static void SetCertStatusExecute(napi_env env, void *data)
121 {
122     SetCertStatusAsyncContext context = static_cast<SetCertStatusAsyncContext>(data);
123     if (context->store == CM_SYSTEM_TRUSTED_STORE) {
124         context->result = CmSetCertStatus(context->certUri, context->store,
125             context->status);
126     } else if (context->store == CM_USER_TRUSTED_STORE) {
127         context->result = CmSetUserCertStatus(context->certUri, context->store, context->status);
128     } else {
129         context->result = CMR_ERROR_INVALID_ARGUMENT;
130     }
131 }
132 
SetCertStatusComplete(napi_env env,napi_status status,void * data)133 static void SetCertStatusComplete(napi_env env, napi_status status, void *data)
134 {
135     SetCertStatusAsyncContext context = static_cast<SetCertStatusAsyncContext>(data);
136     napi_value result[RESULT_NUMBER] = { nullptr };
137     if (context->result == CM_SUCCESS) {
138         NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
139         NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1]));
140     } else {
141         result[0] = GenerateBusinessError(env, context->result);
142         NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
143     }
144     if (context->deferred != nullptr) {
145         GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
146     } else {
147         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
148     }
149     DeleteSetCertStatusAsyncContext(env, context);
150 }
151 
SetCertStatusAsyncWork(napi_env env,SetCertStatusAsyncContext context)152 static napi_value SetCertStatusAsyncWork(napi_env env, SetCertStatusAsyncContext context)
153 {
154     napi_value promise = nullptr;
155     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
156 
157     napi_value resourceName = nullptr;
158     NAPI_CALL(env, napi_create_string_latin1(env, "SetCertStatusAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
159 
160     NAPI_CALL(env, napi_create_async_work(
161         env,
162         nullptr,
163         resourceName,
164         SetCertStatusExecute,
165         SetCertStatusComplete,
166         static_cast<void *>(context),
167         &context->asyncWork));
168 
169     napi_status status = napi_queue_async_work(env, context->asyncWork);
170     if (status != napi_ok) {
171         GET_AND_THROW_LAST_ERROR((env));
172         DeleteSetCertStatusAsyncContext(env, context);
173         CM_LOG_E("could not queue async work");
174         return nullptr;
175     }
176     return promise;
177 }
178 
CMNapiSetCertStatus(napi_env env,napi_callback_info info)179 napi_value CMNapiSetCertStatus(napi_env env, napi_callback_info info)
180 {
181     SetCertStatusAsyncContext context = CreateSetCertStatusAsyncContext();
182     if (context == nullptr) {
183         CM_LOG_E("could not create context");
184         return nullptr;
185     }
186 
187     napi_value result = SetCertStatusParseParams(env, info, context);
188     if (result == nullptr) {
189         CM_LOG_E("could not parse params");
190         DeleteSetCertStatusAsyncContext(env, context);
191         return nullptr;
192     }
193     result = SetCertStatusAsyncWork(env, context);
194     if (result == nullptr) {
195         CM_LOG_E("could not start async work");
196         DeleteSetCertStatusAsyncContext(env, context);
197         return nullptr;
198     }
199     return result;
200 }
201 }  // namespace CertManagerNapi
202