1 /*
2 * Copyright (c) 2023 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_x509_cert_chain_validate_result.h"
17
18 #include "cf_log.h"
19 #include "cf_memory.h"
20 #include "cf_type.h"
21 #include "napi/native_api.h"
22 #include "napi/native_common.h"
23 #include "napi_cert_crl_collection.h"
24 #include "napi_cert_defines.h"
25 #include "napi_cert_utils.h"
26 #include "napi_cert_crl_common.h"
27 #include "napi_object.h"
28 #include "napi_x509_trust_anchor.h"
29 #include "napi_x509_certificate.h"
30 #include "utils.h"
31 #include "x509_cert_chain_validate_result.h"
32
33 namespace OHOS {
34 namespace CertFramework {
35
BuildX509CertChainValidateResultJS(napi_env env,const HcfX509CertChainValidateResult * result)36 napi_value BuildX509CertChainValidateResultJS(napi_env env, const HcfX509CertChainValidateResult *result)
37 {
38 napi_value trustAnchor = BuildX509TrustAnchorJS(env, result->trustAnchor);
39 if (trustAnchor == nullptr) {
40 LOGE("trustAnchor is nullptr");
41 return nullptr;
42 }
43
44 napi_value entityCert = ConvertCertToNapiValue(env, result->entityCert);
45 if (entityCert == nullptr) {
46 LOGE("entityCert is nullptr");
47 return nullptr;
48 }
49
50 napi_value returnValue = nullptr;
51 napi_create_object(env, &returnValue);
52 if (returnValue == nullptr) {
53 LOGE("create result obj failed");
54 return nullptr;
55 }
56 napi_set_named_property(env, returnValue, CERT_CHAIN_VALIDATE_RESULLT_TAG_X509CERT.c_str(), entityCert);
57 napi_set_named_property(env, returnValue, CERT_CHAIN_VALIDATE_RESULLT_TAG_TRUSTANCHOR.c_str(), trustAnchor);
58
59 return returnValue;
60 }
61
62 /* [freeCertFlag] : if building a obj for return failed, the cert object need to free manually. */
FreeX509CertChainValidateResult(HcfX509CertChainValidateResult & param,bool freeCertFlag)63 void FreeX509CertChainValidateResult(HcfX509CertChainValidateResult ¶m, bool freeCertFlag)
64 {
65 if (param.trustAnchor != nullptr) {
66 FreeX509TrustAnchorObj(param.trustAnchor, freeCertFlag);
67 }
68 if (freeCertFlag) {
69 CfObjDestroy(param.entityCert);
70 }
71 param.entityCert = nullptr;
72 }
73 } // namespace CertFramework
74 } // namespace OHOS