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