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_crl_match_parameters.h"
17 #include "napi_x509_certificate.h"
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_node_api.h"
23 #include "napi_cert_defines.h"
24 #include "napi_cert_utils.h"
25 #include "napi_object.h"
26 #include "utils.h"
27
28 namespace OHOS {
29 namespace CertFramework {
30
GetIssuer(napi_env env,napi_value arg,CfBlobArray * & out)31 static bool GetIssuer(napi_env env, napi_value arg, CfBlobArray *&out)
32 {
33 napi_value obj = GetProp(env, arg, CRL_MATCH_TAG_PRIVATE_KEY_VALID.c_str());
34 if (obj == nullptr) {
35 return true;
36 }
37 out = CertGetBlobArrFromArrUarrJSParams(env, obj);
38 if (out == nullptr) {
39 LOGE("out is nullptr");
40 return false;
41 }
42 return true;
43 }
44
GetX509Cert(napi_env env,napi_value arg,HcfCertificate * & out)45 static bool GetX509Cert(napi_env env, napi_value arg, HcfCertificate *&out)
46 {
47 napi_value obj = GetProp(env, arg, CRL_MATCH_TAG_X509CERT.c_str());
48 if (obj == nullptr) {
49 return true;
50 }
51 NapiX509Certificate *napiX509Cert = nullptr;
52 napi_unwrap(env, obj, reinterpret_cast<void **>(&napiX509Cert));
53 if (napiX509Cert == nullptr) {
54 LOGE("napiX509Cert is null!");
55 return false;
56 }
57
58 HcfX509Certificate *cert = napiX509Cert->GetX509Cert();
59 if (cert == nullptr) {
60 LOGE("cert is null!");
61 return false;
62 }
63 LOGI("x509Cert is not null!");
64 out = &(cert->base);
65
66 return true;
67 }
68
BuildX509CrlMatchParams(napi_env env,napi_value arg,HcfX509CrlMatchParams * & matchParams)69 bool BuildX509CrlMatchParams(napi_env env, napi_value arg, HcfX509CrlMatchParams *&matchParams)
70 {
71 napi_valuetype type;
72 napi_typeof(env, arg, &type);
73 if (type != napi_object) {
74 LOGE("wrong argument type. expect object type. [Type]: %d", type);
75 return false;
76 }
77 if (!GetX509Cert(env, arg, matchParams->x509Cert)) {
78 return false;
79 }
80 if (!GetIssuer(env, arg, matchParams->issuer)) {
81 return false;
82 }
83 return true;
84 }
85
FreeX509CrlMatchParams(HcfX509CrlMatchParams * & matchParams)86 void FreeX509CrlMatchParams(HcfX509CrlMatchParams *&matchParams)
87 {
88 if (matchParams == nullptr) {
89 return;
90 }
91
92 if (matchParams->issuer != nullptr) {
93 FreeCfBlobArray(matchParams->issuer->data, matchParams->issuer->count);
94 CF_FREE_PTR(matchParams->issuer);
95 }
96 matchParams->x509Cert = nullptr;
97
98 CF_FREE_PTR(matchParams);
99 }
100
101 } // namespace CertFramework
102 } // namespace OHOS