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_cert_crl_common.h"
17 
18 #include "cf_blob.h"
19 #include "cf_log.h"
20 #include "cf_memory.h"
21 #include "config.h"
22 #include "securec.h"
23 #include "cipher.h"
24 #include "napi_cert_defines.h"
25 #include "napi_x509_certificate.h"
26 #include "napi_x509_crl.h"
27 
28 namespace OHOS {
29 namespace CertFramework {
30 
ConvertCertArrToNapiValue(napi_env env,HcfX509CertificateArray * certs)31 napi_value ConvertCertArrToNapiValue(napi_env env, HcfX509CertificateArray *certs)
32 {
33     napi_value instance;
34     napi_create_array(env, &instance);
35     if (instance == nullptr) {
36         LOGE("create return array failed!");
37         return nullptr;
38     }
39     if (certs == nullptr) {
40         LOGI("return emtpy erray!");
41         return instance;
42     }
43     int j = 0;
44     for (uint32_t i = 0; i < certs->count; ++i) {
45         napi_value element = ConvertCertToNapiValue(env, certs->data[i]);
46         if (element != nullptr) {
47             napi_set_element(env, instance, j++, element);
48             certs->data[i] = nullptr;
49         }
50     }
51     return instance;
52 }
53 
ConvertCertToNapiValue(napi_env env,HcfX509Certificate * cert)54 napi_value ConvertCertToNapiValue(napi_env env, HcfX509Certificate *cert)
55 {
56     if (cert == nullptr) {
57         LOGE("ConvertCertToNapiValue:cert is nullptr.");
58         return nullptr;
59     }
60     CfObject *certObj = nullptr;
61     CfResult res = GetCertObject(cert, &certObj);
62     if (res != CF_SUCCESS) {
63         LOGE("GetCertObject failed.");
64         return nullptr;
65     }
66     NapiX509Certificate *x509Cert = new (std::nothrow) NapiX509Certificate(cert, certObj);
67     if (x509Cert == nullptr) {
68         LOGE("new x509Cert failed!");
69         certObj->destroy(&certObj);
70         return nullptr;
71     }
72     napi_value instance = NapiX509Certificate::CreateX509Cert(env);
73     napi_status status = napi_wrap(
74         env, instance, x509Cert,
75         [](napi_env env, void *data, void *hint) {
76             NapiX509Certificate *certClass = static_cast<NapiX509Certificate *>(data);
77             delete certClass;
78             return;
79         },
80         nullptr, nullptr);
81     if (status != napi_ok) {
82         LOGE("failed to wrap NapiX509Certificate obj!");
83         delete x509Cert;
84         return nullptr;
85     }
86     return instance;
87 }
88 
GetArrayCertFromNapiValue(napi_env env,napi_value object,HcfX509CertificateArray * certs,bool allowEmptyFlag)89 bool GetArrayCertFromNapiValue(napi_env env, napi_value object, HcfX509CertificateArray *certs, bool allowEmptyFlag)
90 {
91     bool flag = false;
92     napi_status status = napi_is_array(env, object, &flag);
93     if (status != napi_ok || !flag) {
94         LOGE("not array!");
95         return false;
96     }
97     uint32_t length;
98     status = napi_get_array_length(env, object, &length);
99     if (status != napi_ok || length == 0) {
100         LOGI("array length is invalid!");
101         return allowEmptyFlag;
102     }
103     if (length > MAX_LEN_OF_ARRAY) {
104         LOGE("array length is invalid!");
105         return false;
106     }
107 
108     certs->data = static_cast<HcfX509Certificate **>(CfMalloc(length * sizeof(HcfX509Certificate *), 0));
109     if (certs->data == nullptr) {
110         LOGE("malloc failed");
111         return false;
112     }
113     certs->count = length;
114     for (uint32_t i = 0; i < length; i++) {
115         napi_value element;
116         status = napi_get_element(env, object, i, &element);
117         if (status != napi_ok) {
118             LOGE("get element failed!");
119             CF_FREE_PTR(certs->data);
120             return false;
121         }
122         NapiX509Certificate *napiCertObj = nullptr;
123         napi_unwrap(env, element, reinterpret_cast<void **>(&napiCertObj));
124         if (napiCertObj == nullptr) {
125             LOGE("napi cert object is nullptr!");
126             CF_FREE_PTR(certs->data);
127             return false;
128         }
129         certs->data[i] = napiCertObj->GetX509Cert();
130     }
131     return true;
132 }
133 
GetArrayCRLFromNapiValue(napi_env env,napi_value object,HcfX509CrlArray * crls,bool allowEmptyFlag)134 bool GetArrayCRLFromNapiValue(napi_env env, napi_value object, HcfX509CrlArray *crls, bool allowEmptyFlag)
135 {
136     napi_valuetype valueType;
137     napi_typeof(env, object, &valueType);
138     if (valueType == napi_undefined) {
139         LOGI("crl list is undefined.");
140         return true;
141     }
142     bool flag = false;
143     napi_status status = napi_is_array(env, object, &flag);
144     if (status != napi_ok || !flag) {
145         LOGE("not array!");
146         return false;
147     }
148     uint32_t length;
149     status = napi_get_array_length(env, object, &length);
150     if (status != napi_ok || length == 0) { /* empty arr is ok */
151         LOGI("array length = 0!");
152         return allowEmptyFlag;
153     }
154     if (length > MAX_LEN_OF_ARRAY) {
155         LOGE("array length is invalid!");
156         return false;
157     }
158     crls->data = static_cast<HcfX509Crl **>(CfMalloc(length * sizeof(HcfX509Crl *), 0));
159     if (crls->data == nullptr) {
160         LOGE("malloc failed");
161         return false;
162     }
163     crls->count = length;
164     for (uint32_t i = 0; i < length; i++) {
165         napi_value element;
166         status = napi_get_element(env, object, i, &element);
167         if (status != napi_ok) {
168             LOGE("get element failed!");
169             CF_FREE_PTR(crls->data);
170             return false;
171         }
172         NapiX509Crl *napiCrlObj = nullptr;
173         napi_unwrap(env, element, reinterpret_cast<void **>(&napiCrlObj));
174         if (napiCrlObj == nullptr) {
175             LOGE("napi cert object is nullptr!");
176             CF_FREE_PTR(crls->data);
177             return false;
178         }
179         crls->data[i] = napiCrlObj->GetX509Crl();
180     }
181     return true;
182 }
183 
GetCertObject(HcfX509Certificate * x509Cert,CfObject ** out)184 CfResult GetCertObject(HcfX509Certificate *x509Cert, CfObject **out)
185 {
186     CfEncodingBlob encodingBlob = { 0 };
187     CfResult res = x509Cert->base.getEncoded(&(x509Cert->base), &encodingBlob);
188     if (res != CF_SUCCESS) {
189         LOGE("Failed to getEncoded!");
190         return res;
191     }
192     res = static_cast<CfResult>(CfCreate(CF_OBJ_TYPE_CERT, &encodingBlob, out));
193     if (res != CF_SUCCESS) {
194         LOGE("Failed to CfCreate!");
195         CF_FREE_PTR(encodingBlob.data);
196         return res;
197     }
198     CF_FREE_PTR(encodingBlob.data);
199     return CF_SUCCESS;
200 }
201 
202 } // namespace CertFramework
203 } // namespace OHOS
204