• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_distinguished_name.h"
17 
18 #include "cf_log.h"
19 #include "cf_memory.h"
20 #include "cf_object_base.h"
21 #include "cf_result.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 "utils.h"
27 
28 namespace OHOS {
29 namespace CertFramework {
30 thread_local napi_ref NapiX509DistinguishedName::classRef_ = nullptr;
31 
32 struct CfCtx {
33     napi_value promise = nullptr;
34     napi_deferred deferred = nullptr;
35     napi_async_work asyncWork = nullptr;
36     napi_ref cfRef = nullptr;
37 
38     CfBlob *inPara = nullptr;
39     bool paraIsString = true;
40     HcfX509DistinguishedName *x509Name = nullptr;
41     NapiX509DistinguishedName *nameClass = nullptr;
42     int32_t errCode = 0;
43     const char *errMsg = nullptr;
44 };
45 
NapiX509DistinguishedName(HcfX509DistinguishedName * x509Name_)46 NapiX509DistinguishedName::NapiX509DistinguishedName(HcfX509DistinguishedName *x509Name_)
47 {
48     this->x509Name_ = x509Name_;
49 }
50 
~NapiX509DistinguishedName()51 NapiX509DistinguishedName::~NapiX509DistinguishedName()
52 {
53     if (this->x509Name_ == this->x509NameUtf8_) {
54         this->x509NameUtf8_ = nullptr;
55     }
56     CfObjDestroy(this->x509Name_);
57     CfObjDestroy(this->x509NameUtf8_);
58 }
59 
FreeCryptoFwkCtx(napi_env env,CfCtx * context)60 static void FreeCryptoFwkCtx(napi_env env, CfCtx *context)
61 {
62     if (context == nullptr) {
63         return;
64     }
65 
66     if (context->asyncWork != nullptr) {
67         napi_delete_async_work(env, context->asyncWork);
68     }
69 
70     if (context->cfRef != nullptr) {
71         napi_delete_reference(env, context->cfRef);
72         context->cfRef = nullptr;
73     }
74 
75     if (context->inPara != nullptr) {
76         CfBlobFree(&(context->inPara));
77     }
78 
79     CfFree(context);
80 }
81 
ReturnPromiseResult(napi_env env,CfCtx * context,napi_value result)82 static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result)
83 {
84     if (context->errCode == CF_SUCCESS) {
85         napi_resolve_deferred(env, context->deferred, result);
86     } else {
87         napi_reject_deferred(env, context->deferred,
88             CertGenerateBusinessError(env, context->errCode, context->errMsg));
89     }
90 }
91 
CreateDistinguishedNameExecute(napi_env env,void * data)92 void NapiX509DistinguishedName::CreateDistinguishedNameExecute(napi_env env, void *data)
93 {
94     CfCtx *context = static_cast<CfCtx *>(data);
95     context->errCode = HcfX509DistinguishedNameCreate(context->inPara, context->paraIsString, &context->x509Name);
96     if (context->errCode != CF_SUCCESS) {
97         context->errMsg = "create x509DistinguishedName failed";
98     }
99 }
100 
CreateDistinguishedNameComplete(napi_env env,napi_status status,void * data)101 void NapiX509DistinguishedName::CreateDistinguishedNameComplete(napi_env env, napi_status status, void *data)
102 {
103     CfCtx *context = static_cast<CfCtx *>(data);
104     if (context->errCode != CF_SUCCESS) {
105         LOGE("call create x509DistinguisehdName failed!");
106         ReturnPromiseResult(env, context, nullptr);
107         FreeCryptoFwkCtx(env, context);
108         return;
109     }
110     napi_value instance = CreateX509DistinguishedName(env);
111     NapiX509DistinguishedName *x509NameClass = new (std::nothrow) NapiX509DistinguishedName(context->x509Name);
112     if (x509NameClass == nullptr) {
113         context->errCode = CF_ERR_MALLOC;
114         context->errMsg = "Failed to create x509DistinguisehdName class";
115         LOGE("Failed to create x509DistinguisehdName class");
116         ReturnPromiseResult(env, context, nullptr);
117         CfObjDestroy(context->x509Name);
118         FreeCryptoFwkCtx(env, context);
119         return;
120     }
121     x509NameClass->SetX509DistinguishedNameUtf8(context->x509Name);
122     status = napi_wrap(
123         env, instance, x509NameClass,
124         [](napi_env env, void *data, void *hint) {
125             NapiX509DistinguishedName *nameClass = static_cast<NapiX509DistinguishedName *>(data);
126             delete nameClass;
127             return;
128         },
129         nullptr, nullptr);
130     if (status != napi_ok) {
131         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "failed to wrap obj!"));
132         LOGE("failed to wrap obj!");
133         delete x509NameClass;
134         return;
135     }
136     ReturnPromiseResult(env, context, instance);
137     FreeCryptoFwkCtx(env, context);
138 }
139 
GetEncoded(napi_env env,napi_callback_info info)140 napi_value NapiX509DistinguishedName::GetEncoded(napi_env env, napi_callback_info info)
141 {
142     HcfX509DistinguishedName *x509Name = GetX509DistinguishedName();
143     CfEncodingBlob blob = {nullptr, 0, CF_FORMAT_DER};
144     CfResult ret = x509Name->getEncode(x509Name, &blob);
145     if (ret != CF_SUCCESS) {
146         LOGE("Distinguished Name get encoded failed");
147         napi_throw(env, CertGenerateBusinessError(env, ret, "Distinguished Name get encoded failed"));
148         return nullptr;
149     }
150     napi_value result = ConvertEncodingBlobToNapiValue(env, &blob);
151     CfEncodingBlobDataFree(&blob);
152     return result;
153 }
154 
GetName(napi_env env,napi_callback_info info)155 napi_value NapiX509DistinguishedName::GetName(napi_env env, napi_callback_info info)
156 {
157     size_t argc = ARGS_SIZE_ONE;
158     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
159     napi_value thisVar = nullptr;
160     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
161     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
162         LOGE("CertCheckArgsCount error");
163         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "CertCheckArgsCount failed"));
164         return nullptr;
165     }
166     HcfX509DistinguishedName *x509Name = GetX509DistinguishedName();
167     if (argc == PARAM0) {
168         CfBlob blob = { 0, nullptr };
169         CfResult ret = x509Name->getName(x509Name, NULL, &blob, NULL);
170         if (ret != CF_SUCCESS) {
171             LOGE("Distinguished Name get name failed");
172             napi_throw(env, CertGenerateBusinessError(env, ret, "Distinguished Name get name failed"));
173             return nullptr;
174         }
175 
176         napi_value result = nullptr;
177         napi_create_string_utf8(env, reinterpret_cast<char *>(blob.data), blob.size, &result);
178         CfBlobDataFree(&blob);
179         return result;
180     } else if (argc == ARGS_SIZE_ONE) {
181         CfBlob *inPara = CertGetBlobFromStringJSParams(env, argv[PARAM0]);
182         if (inPara != nullptr) {
183             CfArray outArr = { nullptr, CF_FORMAT_DER, 0 };
184             CfResult ret = x509Name->getName(x509Name, inPara, NULL, &outArr);
185             if (ret != CF_SUCCESS) {
186                 LOGE("Distinguished Name get name failed");
187                 CfBlobFree(&inPara);
188                 napi_throw(env, CertGenerateBusinessError(env, ret, "Distinguished Name get name failed"));
189                 return nullptr;
190             }
191 
192             napi_value result = ConvertArrayStringToNapiValue(env, &outArr);
193             CfBlobFree(&inPara);
194             CfArrayDataClearAndFree(&outArr);
195             return result;
196         }
197     }
198     return nullptr;
199 }
200 
GetNameEx(napi_env env,napi_callback_info info,CfEncodinigType encodingType)201 napi_value NapiX509DistinguishedName::GetNameEx(napi_env env, napi_callback_info info, CfEncodinigType encodingType)
202 {
203     HcfX509DistinguishedName *x509Name = GetX509DistinguishedNameUtf8();
204     CfBlob blob = { 0, nullptr };
205     CfResult ret = x509Name->getNameEx(x509Name, encodingType, &blob);
206     if (ret != CF_SUCCESS) {
207         LOGE("Distinguished Name get utf8 name failed");
208         napi_throw(env, CertGenerateBusinessError(env, ret, "Distinguished Name get utf8 name failed"));
209         return nullptr;
210     }
211 
212     napi_value result = nullptr;
213     napi_create_string_utf8(env, reinterpret_cast<char *>(blob.data), blob.size, &result);
214     CfBlobDataFree(&blob);
215     return result;
216 }
217 
NapiGetEncoded(napi_env env,napi_callback_info info)218 static napi_value NapiGetEncoded(napi_env env, napi_callback_info info)
219 {
220     napi_value thisVar = nullptr;
221     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
222     NapiX509DistinguishedName *x509Name = nullptr;
223     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Name));
224     if (x509Name == nullptr) {
225         LOGE("x509Name is nullptr!");
226         return nullptr;
227     }
228     return x509Name->GetEncoded(env, info);
229 }
230 
NapiGetName(napi_env env,napi_callback_info info)231 static napi_value NapiGetName(napi_env env, napi_callback_info info)
232 {
233     size_t argc = ARGS_SIZE_ONE;
234     napi_value thisVar = nullptr;
235     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
236     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
237     if (argc != 0 && argc != ARGS_SIZE_ONE) {
238         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "wrong argument num!"));
239         LOGE("wrong argument num!");
240         return nullptr;
241     }
242 
243     NapiX509DistinguishedName *x509Name = nullptr;
244     napi_unwrap(env, thisVar, reinterpret_cast<void **>(&x509Name));
245     if (x509Name == nullptr) {
246         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "x509Cert is nullptr!"));
247         LOGE("x509Name is nullptr!");
248         return nullptr;
249     }
250     if (argc == ARGS_SIZE_ONE) {
251         napi_valuetype valueType;
252         napi_typeof(env, argv[PARAM0], &valueType);
253         if (valueType == napi_number) {
254             CfEncodinigType encodingType;
255             if (napi_get_value_uint32(env, argv[PARAM0], reinterpret_cast<uint32_t *>(&encodingType)) != napi_ok) {
256                 napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "napi_get_value_uint32 failed!"));
257                 LOGE("napi_get_value_uint32 failed!");
258                 return nullptr;
259             }
260             return x509Name->GetNameEx(env, info, encodingType);
261         } else {
262             return x509Name->GetName(env, info);
263         }
264     }
265     return x509Name->GetName(env, info);
266 }
267 
X509DistinguishedNameConstructor(napi_env env,napi_callback_info info)268 static napi_value X509DistinguishedNameConstructor(napi_env env, napi_callback_info info)
269 {
270     napi_value thisVar = nullptr;
271     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
272     return thisVar;
273 }
274 
NapiCreateX509DistinguishedName(napi_env env,napi_callback_info info)275 napi_value NapiX509DistinguishedName::NapiCreateX509DistinguishedName(napi_env env, napi_callback_info info)
276 {
277     size_t argc = ARGS_SIZE_ONE;
278     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
279     napi_value thisVar = nullptr;
280     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
281     if (!CertCheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) {
282         LOGE("CertCheckArgsCount error");
283         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "CertCheckArgsCount failed"));
284         return nullptr;
285     }
286 
287     CfCtx *context = static_cast<CfCtx *>(CfMalloc(sizeof(CfCtx), 0));
288     if (context == nullptr) {
289         LOGE("malloc context failed!");
290         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "CfMalloc failed"));
291         return nullptr;
292     }
293 
294     napi_create_promise(env, &context->deferred, &context->promise);
295 
296     napi_valuetype valueType;
297     napi_typeof(env, argv[PARAM0], &valueType);
298     if (valueType != napi_string) {
299         context->inPara = CertGetBlobFromUint8ArrJSParams(env, argv[PARAM0]);
300         context->paraIsString = false;
301     } else {
302         context->inPara = CertGetBlobFromStringJSParams(env, argv[PARAM0]);
303         context->paraIsString = true;
304     }
305 
306     if (napi_create_reference(env, thisVar, 1, &context->cfRef) != napi_ok) {
307         LOGE("create reference failed!");
308         FreeCryptoFwkCtx(env, context);
309         napi_throw(env, CertGenerateBusinessError(env, CF_INVALID_PARAMS, "Create reference failed"));
310         return nullptr;
311     }
312 
313     napi_create_async_work(env, nullptr, CertGetResourceName(env, "createX500DistinguishedName"),
314         CreateDistinguishedNameExecute,
315         CreateDistinguishedNameComplete,
316         static_cast<void *>(context),
317         &context->asyncWork);
318 
319     napi_queue_async_work(env, context->asyncWork);
320     return context->promise;
321 }
322 
DefineX509DistinguishedNameJSClass(napi_env env,napi_value exports)323 void NapiX509DistinguishedName::DefineX509DistinguishedNameJSClass(napi_env env, napi_value exports)
324 {
325     napi_property_descriptor desc[] = {
326         DECLARE_NAPI_FUNCTION("createX500DistinguishedName", NapiCreateX509DistinguishedName),
327     };
328     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
329 
330     napi_property_descriptor x509NameDesc[] = {
331         DECLARE_NAPI_FUNCTION("getEncoded", NapiGetEncoded),
332         DECLARE_NAPI_FUNCTION("getName", NapiGetName),
333     };
334     napi_value constructor = nullptr;
335     napi_define_class(env, "X500DistinguishedName", NAPI_AUTO_LENGTH, X509DistinguishedNameConstructor, nullptr,
336         sizeof(x509NameDesc) / sizeof(x509NameDesc[0]), x509NameDesc, &constructor);
337     napi_create_reference(env, constructor, 1, &classRef_);
338 }
339 
CreateX509DistinguishedName(napi_env env)340 napi_value NapiX509DistinguishedName::CreateX509DistinguishedName(napi_env env)
341 {
342     napi_value constructor = nullptr;
343     napi_value instance = nullptr;
344     napi_get_reference_value(env, classRef_, &constructor);
345     napi_new_instance(env, constructor, 0, nullptr, &instance);
346     return instance;
347 }
348 
ConstructX509DistinguishedName(HcfX509DistinguishedName * x509Name,HcfX509DistinguishedName * x509NameUtf8,napi_env env)349 napi_value ConstructX509DistinguishedName(HcfX509DistinguishedName *x509Name,
350     HcfX509DistinguishedName *x509NameUtf8, napi_env env)
351 {
352     napi_value instance = NapiX509DistinguishedName::CreateX509DistinguishedName(env);
353     NapiX509DistinguishedName *x509NameClass = new (std::nothrow) NapiX509DistinguishedName(x509Name);
354     if (x509NameClass == nullptr) {
355         LOGE("Failed to create a NapiX509DistinguishedName class");
356         CfObjDestroy(x509Name);
357         CfObjDestroy(x509NameUtf8);
358         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_MALLOC, "NapiX509DistinguishedName new failed"));
359         return nullptr;
360     }
361     x509NameClass->SetX509DistinguishedNameUtf8(x509NameUtf8);
362     napi_status status = napi_wrap(
363         env, instance, x509NameClass,
364         [](napi_env env, void *data, void *hint) {
365             NapiX509DistinguishedName *nameClass = static_cast<NapiX509DistinguishedName *>(data);
366             delete nameClass;
367             return;
368         }, nullptr, nullptr);
369     if (status != napi_ok) {
370         napi_throw(env, CertGenerateBusinessError(env, CF_ERR_NAPI, "failed to wrap obj!"));
371         LOGE("failed to wrap obj!");
372         delete x509NameClass;
373         return nullptr;
374     }
375     return instance;
376 }
377 } // namespace CertFramework
378 } // namespace OHOS
379