1 /*
2  * Copyright (c) 2023-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 "napi_cert_chain_validator.h"
17 
18 #include "napi/native_common.h"
19 #include "napi/native_api.h"
20 #include "cf_log.h"
21 #include "cf_memory.h"
22 #include "utils.h"
23 #include "cf_result.h"
24 #include "cf_object_base.h"
25 #include "napi_cert_defines.h"
26 #include "napi_cert_utils.h"
27 
28 namespace OHOS {
29 namespace CertFramework {
30 thread_local napi_ref NapiCertChainValidator::classRef_ = nullptr;
31 
32 struct CfCtx {
33     AsyncType asyncType = ASYNC_TYPE_CALLBACK;
34     napi_ref callback = nullptr;
35     napi_deferred deferred = nullptr;
36     napi_async_work asyncWork = nullptr;
37     napi_ref cfRef = nullptr;
38 
39     NapiCertChainValidator *ccvClass = nullptr;
40     HcfCertChainData *certChainData = nullptr;
41 
42     int32_t errCode = 0;
43     const char *errMsg = nullptr;
44 };
45 
NapiCertChainValidator(HcfCertChainValidator * certChainValidator)46 NapiCertChainValidator::NapiCertChainValidator(HcfCertChainValidator *certChainValidator)
47 {
48     this->certChainValidator_ = certChainValidator;
49 }
50 
~NapiCertChainValidator()51 NapiCertChainValidator::~NapiCertChainValidator()
52 {
53     CfObjDestroy(this->certChainValidator_);
54 }
55 
FreeCryptoFwkCtx(napi_env env,CfCtx * context)56 static void FreeCryptoFwkCtx(napi_env env, CfCtx *context)
57 {
58     if (context == nullptr) {
59         return;
60     }
61 
62     if (context->asyncWork != nullptr) {
63         napi_delete_async_work(env, context->asyncWork);
64     }
65 
66     if (context->callback != nullptr) {
67         napi_delete_reference(env, context->callback);
68     }
69 
70     if (context->cfRef != nullptr) {
71         napi_delete_reference(env, context->cfRef);
72         context->cfRef = nullptr;
73     }
74 
75     if (context->certChainData != nullptr) {
76         CfFree(context->certChainData->data);
77         context->certChainData->data = nullptr;
78         CfFree(context->certChainData);
79         context->certChainData = nullptr;
80     }
81 
82     CfFree(context);
83 }
84 
ReturnCallbackResult(napi_env env,CfCtx * context,napi_value result)85 static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result)
86 {
87     napi_value businessError = nullptr;
88     if (context->errCode != CF_SUCCESS) {
89         businessError = CertGenerateBusinessError(env, context->errCode, context->errMsg);
90     }
91     napi_value params[ARGS_SIZE_TWO] = { businessError, result };
92 
93     napi_value func = nullptr;
94     napi_get_reference_value(env, context->callback, &func);
95 
96     napi_value recv = nullptr;
97     napi_value callFuncRet = nullptr;
98     napi_get_undefined(env, &recv);
99     napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
100 }
101 
ReturnPromiseResult(napi_env env,CfCtx * context,napi_value result)102 static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result)
103 {
104     if (context->errCode == CF_SUCCESS) {
105         napi_resolve_deferred(env, context->deferred, result);
106     } else {
107         napi_reject_deferred(env, context->deferred,
108             CertGenerateBusinessError(env, context->errCode, context->errMsg));
109     }
110 }
111 
ReturnResult(napi_env env,CfCtx * context,napi_value result)112 static void ReturnResult(napi_env env, CfCtx *context, napi_value result)
113 {
114     if (context->asyncType == ASYNC_TYPE_CALLBACK) {
115         ReturnCallbackResult(env, context, result);
116     } else {
117         ReturnPromiseResult(env, context, result);
118     }
119 }
120 
ValidateExecute(napi_env env,void * data)121 static void ValidateExecute(napi_env env, void *data)
122 {
123     CfCtx *context = static_cast<CfCtx *>(data);
124     HcfCertChainValidator *validator = context->ccvClass->GetCertChainValidator();
125     context->errCode = validator->validate(validator, context->certChainData);
126     if (context->errCode != CF_SUCCESS) {
127         LOGE("validate cert chain failed!");
128         context->errMsg = "validate cert chain failed";
129     }
130 }
131 
ValidateComplete(napi_env env,napi_status status,void * data)132 static void ValidateComplete(napi_env env, napi_status status, void *data)
133 {
134     CfCtx *context = static_cast<CfCtx *>(data);
135     ReturnResult(env, context, CertNapiGetNull(env));
136     FreeCryptoFwkCtx(env, context);
137 }
138 
Validate(napi_env env,napi_callback_info info)139 napi_value NapiCertChainValidator::Validate(napi_env env, napi_callback_info info)
140 {
141     size_t argc = ARGS_SIZE_TWO;
142     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
143     napi_value thisVar = nullptr;
144     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
145     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) {
146         return nullptr;
147     }
148     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
149     if (context == nullptr) {
150         LOGE("malloc context failed!");
151         return nullptr;
152     }
153     context->ccvClass = this;
154 
155     context->asyncType = GetAsyncType(env, argc, ARGS_SIZE_TWO, argv[PARAM1]);
156     if (!GetCertChainFromValue(env, argv[PARAM0], &context->certChainData)) {
157         LOGE("get cert chain data from napi value failed!");
158         FreeCryptoFwkCtx(env, context);
159         return nullptr;
160     }
161 
162     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
163         LOGE("create reference failed!");
164         FreeCryptoFwkCtx(env, context);
165         return nullptr;
166     }
167 
168     napi_value promise = nullptr;
169     if (context->asyncType == ASYNC_TYPE_CALLBACK) {
170         if (!CertGetCallbackFromJSParams(env, argv[PARAM1], &context->callback)) {
171             LOGE("get callback failed!");
172             FreeCryptoFwkCtx(env, context);
173             return nullptr;
174         }
175     } else {
176         napi_create_promise(env, &context->deferred, &promise);
177     }
178 
179     napi_create_async_work(
180         env, nullptr, CertGetResourceName(env, "Validate"),
181         ValidateExecute,
182         ValidateComplete,
183         static_cast<void *>(context),
184         &context->asyncWork);
185 
186     napi_queue_async_work(env, context->asyncWork);
187     if (context->asyncType == ASYNC_TYPE_PROMISE) {
188         return promise;
189     } else {
190         return CertNapiGetNull(env);
191     }
192 }
193 
NapiValidate(napi_env env,napi_callback_info info)194 static napi_value NapiValidate(napi_env env, napi_callback_info info)
195 {
196     napi_value thisVar = nullptr;
197     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
198     NapiCertChainValidator *certChainValidator = nullptr;
199     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&certChainValidator));
200     if (certChainValidator == nullptr) {
201         LOGE("certChainValidator is nullptr!");
202         return nullptr;
203     }
204     return certChainValidator->Validate(env, info);
205 }
206 
CertChainValidatorConstructor(napi_env env,napi_callback_info info)207 static napi_value CertChainValidatorConstructor(napi_env env, napi_callback_info info)
208 {
209     napi_value thisVar = nullptr;
210     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
211     return thisVar;
212 }
213 
CreateCertChainValidator(napi_env env,napi_callback_info info)214 napi_value NapiCertChainValidator::CreateCertChainValidator(napi_env env, napi_callback_info info)
215 {
216     napi_value thisVar = nullptr;
217     size_t argc = ARGS_SIZE_ONE;
218     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
219     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
220 
221     if (argc != ARGS_SIZE_ONE) {
222         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "invalid params count"));
223         LOGE("invalid params count!");
224         return nullptr;
225     }
226 
227     std::string algorithm;
228     if (!CertGetStringFromJSParams(env, argv[PARAM0], algorithm)) {
229         LOGE("Failed to get algorithm.");
230         return nullptr;
231     }
232     HcfCertChainValidator *certChainValidator = nullptr;
233     CfResult res = HcfCertChainValidatorCreate(algorithm.c_str(), &certChainValidator);
234     if (res != CF_SUCCESS) {
235         napi_throw(env, CertGenerateBusinessError(env, res, "create cert chain validator failed"));
236         LOGE("Failed to create c cert chain validator.");
237         return nullptr;
238     }
239     const char *returnAlgorithm = certChainValidator->getAlgorithm(certChainValidator);
240     napi_value algValue = nullptr;
241     napi_create_string_utf8(env, returnAlgorithm, NAPI_AUTO_LENGTH, &algValue);
242     napi_value constructor = nullptr;
243     napi_value validatorInstance = nullptr;
244     napi_get_reference_value(env, classRef_, &constructor);
245     napi_new_instance(env, constructor, 0, nullptr, &validatorInstance);
246     napi_set_named_property(env, validatorInstance, CERT_TAG_ALGORITHM.c_str(), algValue);
247     NapiCertChainValidator *ccvClass = new (std::nothrow) NapiCertChainValidator(certChainValidator);
248     if (ccvClass == nullptr) {
249         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "Failed to create a ccv class"));
250         LOGE("Failed to create a ccv class");
251         CfObjDestroy(certChainValidator);
252         return nullptr;
253     }
254     napi_wrap(
255         env, validatorInstance, ccvClass,
256         [](napi_env env, void* data, void *hint) {
257             NapiCertChainValidator *ccv = static_cast<NapiCertChainValidator *>(data);
258             delete ccv;
259         },
260         nullptr,
261         nullptr);
262 
263     return validatorInstance;
264 }
265 
DefineCertChainValidatorJSClass(napi_env env,napi_value exports)266 void NapiCertChainValidator::DefineCertChainValidatorJSClass(napi_env env, napi_value exports)
267 {
268     napi_property_descriptor desc[] = {
269         DECLARE_NAPI_FUNCTION("createCertChainValidator", CreateCertChainValidator),
270     };
271     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
272 
273     napi_property_descriptor validatorDesc[] = {
274         DECLARE_NAPI_FUNCTION("validate", NapiValidate),
275     };
276     napi_value constructor = nullptr;
277     napi_define_class(env, "CertChainValidator", NAPI_AUTO_LENGTH, CertChainValidatorConstructor, nullptr,
278         sizeof(validatorDesc) / sizeof(validatorDesc[0]), validatorDesc, &constructor);
279     napi_create_reference(env, constructor, 1, &classRef_);
280 }
281 } // namespace CertFramework
282 } // namespace OHOS