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_x509_crl_match_parameters.h"
17 #include <string>
18 #include "napi_x509_certificate.h"
19 #include "cf_log.h"
20 #include "cf_memory.h"
21 #include "cf_type.h"
22 #include "napi/native_api.h"
23 #include "napi/native_common.h"
24 #include "napi_cert_defines.h"
25 #include "napi_cert_utils.h"
26 #include "napi_object.h"
27 #include "utils.h"
28 
29 namespace OHOS {
30 namespace CertFramework {
31 
GetIssuer(napi_env env,napi_value arg,CfBlobArray * & out)32 static bool GetIssuer(napi_env env, napi_value arg, CfBlobArray *&out)
33 {
34     napi_value obj = GetProp(env, arg, CRL_MATCH_TAG_PRIVATE_KEY_VALID.c_str());
35     if (obj == nullptr) {
36         return true;
37     }
38     out = CertGetBlobArrFromArrUarrJSParams(env, obj);
39     if (out == nullptr) {
40         LOGE("out is nullptr");
41         return false;
42     }
43     return true;
44 }
45 
GetX509Cert(napi_env env,napi_value arg,HcfCertificate * & out)46 static bool GetX509Cert(napi_env env, napi_value arg, HcfCertificate *&out)
47 {
48     napi_value obj = GetProp(env, arg, CRL_MATCH_TAG_X509CERT.c_str());
49     if (obj == nullptr) {
50         return true;
51     }
52     NapiX509Certificate *napiX509Cert = nullptr;
53     napi_unwrap(env, obj, reinterpret_cast<void **>(&napiX509Cert));
54     if (napiX509Cert == nullptr) {
55         LOGE("napiX509Cert is null!");
56         return false;
57     }
58 
59     HcfX509Certificate *cert = napiX509Cert->GetX509Cert();
60     if (cert == nullptr) {
61         LOGE("cert is null!");
62         return false;
63     }
64     out = &(cert->base);
65 
66     return true;
67 }
68 
GetUpdateDateTime(napi_env env,napi_value arg,CfBlob * & out)69 static bool GetUpdateDateTime(napi_env env, napi_value arg, CfBlob *&out)
70 {
71     napi_value obj = GetProp(env, arg, CRL_MATCH_TAG_UPDATE_DATE_TIME.c_str());
72     if (obj == nullptr) {
73         return true;
74     }
75     out = CertGetBlobFromStringJSParams(env, obj);
76     if (out == nullptr) {
77         LOGE("out is nullptr");
78         return false;
79     }
80     return true;
81 }
82 
GetCRLNum(napi_env env,napi_value arg,const std::string nameTag,CfBlob * & out)83 static bool GetCRLNum(napi_env env, napi_value arg, const std::string nameTag, CfBlob *&out)
84 {
85     napi_value obj = GetProp(env, arg, nameTag.c_str());
86     if (obj == nullptr) {
87         return true;
88     }
89     CfBlob outBlob = { 0, nullptr };
90     bool flag = CertGetBlobFromBigIntJSParams(env, obj, outBlob);
91     if (!flag) {
92         LOGE("out is nullptr");
93         return false;
94     }
95     out = static_cast<CfBlob *>(CfMalloc(sizeof(CfBlob), 0));
96     if (out == nullptr) {
97         LOGE("Failed to allocate newBlob memory!");
98         CfBlobDataFree(&outBlob);
99         return false;
100     }
101     out->data = outBlob.data;
102     out->size = outBlob.size;
103     return true;
104 }
105 
BuildX509CrlMatchParams(napi_env env,napi_value arg,HcfX509CrlMatchParams * & matchParams)106 bool BuildX509CrlMatchParams(napi_env env, napi_value arg, HcfX509CrlMatchParams *&matchParams)
107 {
108     napi_valuetype type;
109     napi_typeof(env, arg, &type);
110     if (type != napi_object) {
111         LOGE("wrong argument type. expect object type. [Type]: %d", type);
112         return false;
113     }
114     if (!GetX509Cert(env, arg, matchParams->x509Cert)) {
115         return false;
116     }
117     if (!GetIssuer(env, arg, matchParams->issuer)) {
118         return false;
119     }
120     if (!GetUpdateDateTime(env, arg, matchParams->updateDateTime)) {
121         return false;
122     }
123     if (!GetCRLNum(env, arg, CRL_MATCH_TAG_MAXCRL, matchParams->maxCRL)) {
124         return false;
125     }
126     if (!GetCRLNum(env, arg, CRL_MATCH_TAG_MINCRL, matchParams->minCRL)) {
127         return false;
128     }
129     return true;
130 }
131 
FreeX509CrlMatchParams(HcfX509CrlMatchParams * & matchParams)132 void FreeX509CrlMatchParams(HcfX509CrlMatchParams *&matchParams)
133 {
134     if (matchParams == nullptr) {
135         return;
136     }
137 
138     if (matchParams->issuer != nullptr) {
139         FreeCfBlobArray(matchParams->issuer->data, matchParams->issuer->count);
140         CF_FREE_PTR(matchParams->issuer);
141     }
142     matchParams->x509Cert = nullptr;
143     CfBlobFree(&matchParams->updateDateTime);
144     CfBlobFree(&matchParams->maxCRL);
145     CfBlobFree(&matchParams->minCRL);
146 
147     CF_FREE_PTR(matchParams);
148 }
149 
150 } // namespace CertFramework
151 } // namespace OHOS
152