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 empty array!");
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 certObj = nullptr;
71 return nullptr;
72 }
73 napi_value instance = NapiX509Certificate::CreateX509Cert(env);
74 napi_status status = napi_wrap(
75 env, instance, x509Cert,
76 [](napi_env env, void *data, void *hint) {
77 NapiX509Certificate *certClass = static_cast<NapiX509Certificate *>(data);
78 delete certClass;
79 return;
80 },
81 nullptr, nullptr);
82 if (status != napi_ok) {
83 LOGE("failed to wrap NapiX509Certificate obj!");
84 delete x509Cert;
85 return nullptr;
86 }
87 return instance;
88 }
89
GetArrayCertFromNapiValue(napi_env env,napi_value object,HcfX509CertificateArray * certs,bool allowEmptyFlag)90 bool GetArrayCertFromNapiValue(napi_env env, napi_value object, HcfX509CertificateArray *certs, bool allowEmptyFlag)
91 {
92 bool flag = false;
93 napi_status status = napi_is_array(env, object, &flag);
94 if (status != napi_ok || !flag) {
95 LOGE("not array!");
96 return false;
97 }
98 uint32_t length;
99 status = napi_get_array_length(env, object, &length);
100 if (status != napi_ok || length == 0) {
101 LOGI("array length is invalid!");
102 return allowEmptyFlag;
103 }
104 if (length > MAX_LEN_OF_ARRAY) {
105 LOGE("array length is invalid!");
106 return false;
107 }
108
109 certs->data = static_cast<HcfX509Certificate **>(CfMalloc(length * sizeof(HcfX509Certificate *), 0));
110 if (certs->data == nullptr) {
111 LOGE("malloc failed");
112 return false;
113 }
114 certs->count = length;
115 for (uint32_t i = 0; i < length; i++) {
116 napi_value element;
117 status = napi_get_element(env, object, i, &element);
118 if (status != napi_ok) {
119 LOGE("get element failed!");
120 CF_FREE_PTR(certs->data);
121 return false;
122 }
123 NapiX509Certificate *napiCertObj = nullptr;
124 napi_unwrap(env, element, reinterpret_cast<void **>(&napiCertObj));
125 if (napiCertObj == nullptr) {
126 LOGE("napi cert object is nullptr!");
127 CF_FREE_PTR(certs->data);
128 return false;
129 }
130 certs->data[i] = napiCertObj->GetX509Cert();
131 }
132 return true;
133 }
134
GetArrayCRLFromNapiValue(napi_env env,napi_value object,HcfX509CrlArray * crls,bool allowEmptyFlag)135 bool GetArrayCRLFromNapiValue(napi_env env, napi_value object, HcfX509CrlArray *crls, bool allowEmptyFlag)
136 {
137 napi_valuetype valueType;
138 napi_typeof(env, object, &valueType);
139 if (valueType == napi_undefined) {
140 LOGI("crl list is undefined.");
141 return true;
142 }
143 bool flag = false;
144 napi_status status = napi_is_array(env, object, &flag);
145 if (status != napi_ok || !flag) {
146 LOGE("not array!");
147 return false;
148 }
149 uint32_t length;
150 status = napi_get_array_length(env, object, &length);
151 if (status != napi_ok || length == 0) { /* empty arr is ok */
152 LOGI("array length = 0!");
153 return allowEmptyFlag;
154 }
155 if (length > MAX_LEN_OF_ARRAY) {
156 LOGE("array length is invalid!");
157 return false;
158 }
159 crls->data = static_cast<HcfX509Crl **>(CfMalloc(length * sizeof(HcfX509Crl *), 0));
160 if (crls->data == nullptr) {
161 LOGE("malloc failed");
162 return false;
163 }
164 crls->count = length;
165 for (uint32_t i = 0; i < length; i++) {
166 napi_value element;
167 status = napi_get_element(env, object, i, &element);
168 if (status != napi_ok) {
169 LOGE("get element failed!");
170 CF_FREE_PTR(crls->data);
171 return false;
172 }
173 NapiX509Crl *napiCrlObj = nullptr;
174 napi_unwrap(env, element, reinterpret_cast<void **>(&napiCrlObj));
175 if (napiCrlObj == nullptr) {
176 LOGE("napi cert object is nullptr!");
177 CF_FREE_PTR(crls->data);
178 return false;
179 }
180 crls->data[i] = napiCrlObj->GetX509Crl();
181 }
182 return true;
183 }
184
GetCertObject(HcfX509Certificate * x509Cert,CfObject ** out)185 CfResult GetCertObject(HcfX509Certificate *x509Cert, CfObject **out)
186 {
187 CfEncodingBlob encodingBlob = { 0 };
188 CfResult res = x509Cert->base.getEncoded(&(x509Cert->base), &encodingBlob);
189 if (res != CF_SUCCESS) {
190 LOGE("Failed to getEncoded!");
191 return res;
192 }
193 res = static_cast<CfResult>(CfCreate(CF_OBJ_TYPE_CERT, &encodingBlob, out));
194 if (res != CF_SUCCESS) {
195 LOGE("Failed to CfCreate!");
196 CF_FREE_PTR(encodingBlob.data);
197 return res;
198 }
199 CF_FREE_PTR(encodingBlob.data);
200 return CF_SUCCESS;
201 }
202
203 } // namespace CertFramework
204 } // namespace OHOS
205