• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         ThrowParamsError(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         ThrowParamsError(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         ThrowParamsError(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         ThrowParamsError(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             ThrowParamsError(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         const char *errorMsg = "set cert status error";
142         result[0] = GenerateBusinessError(env, context->result, errorMsg);
143         NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
144     }
145     if (context->deferred != nullptr) {
146         GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
147     } else {
148         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
149     }
150     DeleteSetCertStatusAsyncContext(env, context);
151 }
152 
SetCertStatusAsyncWork(napi_env env,SetCertStatusAsyncContext context)153 static napi_value SetCertStatusAsyncWork(napi_env env, SetCertStatusAsyncContext context)
154 {
155     napi_value promise = nullptr;
156     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
157 
158     napi_value resourceName = nullptr;
159     NAPI_CALL(env, napi_create_string_latin1(env, "SetCertStatusAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
160 
161     NAPI_CALL(env, napi_create_async_work(
162         env,
163         nullptr,
164         resourceName,
165         SetCertStatusExecute,
166         SetCertStatusComplete,
167         static_cast<void *>(context),
168         &context->asyncWork));
169 
170     napi_status status = napi_queue_async_work(env, context->asyncWork);
171     if (status != napi_ok) {
172         GET_AND_THROW_LAST_ERROR((env));
173         DeleteSetCertStatusAsyncContext(env, context);
174         CM_LOG_E("could not queue async work");
175         return nullptr;
176     }
177     return promise;
178 }
179 
CMNapiSetCertStatus(napi_env env,napi_callback_info info)180 napi_value CMNapiSetCertStatus(napi_env env, napi_callback_info info)
181 {
182     SetCertStatusAsyncContext context = CreateSetCertStatusAsyncContext();
183     if (context == nullptr) {
184         CM_LOG_E("could not create context");
185         return nullptr;
186     }
187 
188     napi_value result = SetCertStatusParseParams(env, info, context);
189     if (result == nullptr) {
190         CM_LOG_E("could not parse params");
191         DeleteSetCertStatusAsyncContext(env, context);
192         return nullptr;
193     }
194     result = SetCertStatusAsyncWork(env, context);
195     if (result == nullptr) {
196         CM_LOG_E("could not start async work");
197         DeleteSetCertStatusAsyncContext(env, context);
198         return nullptr;
199     }
200     return result;
201 }
202 }  // namespace CertManagerNapi
203