• 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_get_system_cert_info.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_GET_CERT_INFO_MIN_ARGS = 1;
29 constexpr int CM_NAPI_GET_CERT_INFO_MAX_ARGS = 2;
30 }  // namespace
31 
32 struct GetCertInfoAsyncContextT {
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     struct CertInfo *certificate = nullptr;
41 };
42 using GetCertInfoAsyncContext = GetCertInfoAsyncContextT *;
43 
CreateGetCertInfoAsyncContext()44 static GetCertInfoAsyncContext CreateGetCertInfoAsyncContext()
45 {
46     GetCertInfoAsyncContext context =
47         static_cast<GetCertInfoAsyncContext>(CmMalloc(sizeof(GetCertInfoAsyncContextT)));
48     if (context != nullptr) {
49         (void)memset_s(
50             context, sizeof(GetCertInfoAsyncContextT), 0, sizeof(GetCertInfoAsyncContextT));
51     }
52     return context;
53 }
54 
DeleteGetCertInfoAsyncContext(napi_env env,GetCertInfoAsyncContext & context)55 static void DeleteGetCertInfoAsyncContext(napi_env env, GetCertInfoAsyncContext &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     if (context->certificate != nullptr) {
68         FreeCertInfo(context->certificate);
69     }
70 
71     CmFree(context);
72     context = nullptr;
73 }
74 
GetCertInfoParseParams(napi_env env,napi_callback_info info,GetCertInfoAsyncContext context,uint32_t store)75 static napi_value GetCertInfoParseParams(napi_env env, napi_callback_info info,
76     GetCertInfoAsyncContext context, uint32_t store)
77 {
78     size_t argc = CM_NAPI_GET_CERT_INFO_MAX_ARGS;
79     napi_value argv[CM_NAPI_GET_CERT_INFO_MAX_ARGS] = { nullptr };
80     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
81 
82     if ((argc != CM_NAPI_GET_CERT_INFO_MAX_ARGS) && (argc != CM_NAPI_GET_CERT_INFO_MIN_ARGS)) {
83         ThrowParamsError(env, PARAM_ERROR, "arguments count invalid when getting trusted certificate info");
84         CM_LOG_E("arguments count invalid when getting trusted certificate info");
85         return nullptr;
86     }
87 
88     size_t index = 0;
89     napi_value result = ParseString(env, argv[index], context->certUri);
90     if (result == nullptr) {
91         ThrowParamsError(env, PARAM_ERROR, "certUri type error");
92         CM_LOG_E("get cert uri failed when getting trusted certificate info");
93         return nullptr;
94     }
95 
96     index++;
97     if (index < argc) {
98         int32_t ret = GetCallback(env, argv[index], context->callback);
99         if (ret != CM_SUCCESS) {
100             ThrowParamsError(env, PARAM_ERROR, "Get callback type failed.");
101             CM_LOG_E("get callback function failed when get cert info function");
102             return nullptr;
103         }
104     }
105 
106     context->store = store;
107     return GetInt32(env, 0);
108 }
109 
GetCertInfoWriteResult(napi_env env,GetCertInfoAsyncContext context)110 static napi_value GetCertInfoWriteResult(napi_env env, GetCertInfoAsyncContext context)
111 {
112     napi_value result = nullptr;
113     NAPI_CALL(env, napi_create_object(env, &result));
114     napi_value certInfo = GenerateCertInfo(env, context->certificate);
115     if (certInfo != nullptr) {
116         napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CERTINFO.c_str(), certInfo);
117     } else {
118         NAPI_CALL(env, napi_get_undefined(env, &result));
119     }
120     return result;
121 }
122 
GetCertInfoExecute(napi_env env,void * data)123 static void GetCertInfoExecute(napi_env env, void *data)
124 {
125     GetCertInfoAsyncContext context = static_cast<GetCertInfoAsyncContext>(data);
126 
127     context->certificate = static_cast<struct CertInfo *>(CmMalloc(sizeof(struct CertInfo)));
128     if (context->certificate == nullptr) {
129         CM_LOG_E("malloc certificate fail");
130         context->result = CMR_ERROR_MALLOC_FAIL;
131         return;
132     }
133     (void)memset_s(context->certificate, sizeof(struct CertInfo), 0, sizeof(struct CertInfo));
134 
135     context->certificate->certInfo.data = static_cast<uint8_t *>(CmMalloc(MAX_LEN_CERTIFICATE));
136     if (context->certificate->certInfo.data == nullptr) {
137         CM_LOG_E("malloc certificate certInfo data fail");
138         context->result = CMR_ERROR_MALLOC_FAIL;
139         return;
140     }
141     context->certificate->certInfo.size = MAX_LEN_CERTIFICATE;
142 
143     if (context->store == CM_SYSTEM_TRUSTED_STORE) {
144         context->result = CmGetCertInfo(context->certUri, context->store,
145             context->certificate);
146     } else {
147         context->result = CmGetUserCertInfo(context->certUri, context->store, context->certificate);
148     }
149 }
150 
GetCertInfoComplete(napi_env env,napi_status status,void * data)151 static void GetCertInfoComplete(napi_env env, napi_status status, void *data)
152 {
153     GetCertInfoAsyncContext context = static_cast<GetCertInfoAsyncContext>(data);
154     napi_value result[RESULT_NUMBER] = { nullptr };
155     if (context->result == CM_SUCCESS) {
156         NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
157         result[1] = GetCertInfoWriteResult(env, context);
158     } else {
159         const char *errorMsg = "get system cert info error";
160         result[0] = GenerateBusinessError(env, context->result, errorMsg);
161         NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
162     }
163     if (context->deferred != nullptr) {
164         GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
165     } else {
166         GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
167     }
168     DeleteGetCertInfoAsyncContext(env, context);
169     CM_LOG_D("get system cert info end");
170 }
171 
GetCertInfoAsyncWork(napi_env env,GetCertInfoAsyncContext context)172 static napi_value GetCertInfoAsyncWork(napi_env env, GetCertInfoAsyncContext context)
173 {
174     napi_value promise = nullptr;
175     GenerateNapiPromise(env, context->callback, &context->deferred, &promise);
176 
177     napi_value resourceName = nullptr;
178     NAPI_CALL(env, napi_create_string_latin1(env, "GetCertInfoAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
179 
180     NAPI_CALL(env, napi_create_async_work(
181         env,
182         nullptr,
183         resourceName,
184         GetCertInfoExecute,
185         GetCertInfoComplete,
186         static_cast<void *>(context),
187         &context->asyncWork));
188     napi_status status = napi_queue_async_work(env, context->asyncWork);
189     if (status != napi_ok) {
190         GET_AND_THROW_LAST_ERROR((env));
191         DeleteGetCertInfoAsyncContext(env, context);
192         CM_LOG_E("get system cert info could not queue async work");
193         return nullptr;
194     }
195     return promise;
196 }
197 
CMNapiGetSystemCertInfo(napi_env env,napi_callback_info info)198 napi_value CMNapiGetSystemCertInfo(napi_env env, napi_callback_info info)
199 {
200     GetCertInfoAsyncContext context = CreateGetCertInfoAsyncContext();
201     if (context == nullptr) {
202         CM_LOG_E("could not create context");
203         return nullptr;
204     }
205 
206     napi_value result = GetCertInfoParseParams(env, info, context, CM_SYSTEM_TRUSTED_STORE);
207     if (result == nullptr) {
208         CM_LOG_E("could not parse params");
209         DeleteGetCertInfoAsyncContext(env, context);
210         return nullptr;
211     }
212     result = GetCertInfoAsyncWork(env, context);
213     if (result == nullptr) {
214         CM_LOG_E("could not start async work");
215         DeleteGetCertInfoAsyncContext(env, context);
216         return nullptr;
217     }
218     return result;
219 }
220 
CMNapiGetUserTrustedCertInfo(napi_env env,napi_callback_info info)221 napi_value CMNapiGetUserTrustedCertInfo(napi_env env, napi_callback_info info)
222 {
223     GetCertInfoAsyncContext context = CreateGetCertInfoAsyncContext();
224     if (context == nullptr) {
225         CM_LOG_E("create cert info context failed");
226         return nullptr;
227     }
228 
229     napi_value result = GetCertInfoParseParams(env, info, context, CM_USER_TRUSTED_STORE);
230     if (result == nullptr) {
231         CM_LOG_E("parse get cert info params failed");
232         DeleteGetCertInfoAsyncContext(env, context);
233         return nullptr;
234     }
235     result = GetCertInfoAsyncWork(env, context);
236     if (result == nullptr) {
237         CM_LOG_E("get cert info params async work failed");
238         DeleteGetCertInfoAsyncContext(env, context);
239         return nullptr;
240     }
241     return result;
242 }
243 }  // namespace CertManagerNapi
244