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