1 /*
2 * Copyright (C) 2022 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 "securec.h"
17 #include "log.h"
18
19 #include "napi_x509_certificate.h"
20 #include "napi_cert_chain_validator.h"
21 #include "napi_pub_key.h"
22 #include "napi_utils.h"
23 #include "napi_x509_crl_entry.h"
24 #include "napi_x509_crl.h"
25 #include "napi_crypto_framework_defines.h"
26
27 namespace OHOS {
28 namespace CryptoFramework {
CreateEncodingFormat(napi_env env)29 static napi_value CreateEncodingFormat(napi_env env)
30 {
31 napi_value encodingFormat = nullptr;
32 napi_create_object(env, &encodingFormat);
33
34 AddUint32Property(env, encodingFormat, "FORMAT_DER", HCF_FORMAT_DER);
35 AddUint32Property(env, encodingFormat, "FORMAT_PEM", HCF_FORMAT_PEM);
36
37 return encodingFormat;
38 }
39
DefineEncodingFormatProperties(napi_env env,napi_value exports)40 static void DefineEncodingFormatProperties(napi_env env, napi_value exports)
41 {
42 napi_property_descriptor desc[] = {
43 DECLARE_NAPI_PROPERTY("EncodingFormat", CreateEncodingFormat(env)),
44 };
45 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
46 }
47
CreateCertResultCode(napi_env env)48 static napi_value CreateCertResultCode(napi_env env)
49 {
50 napi_value resultCode = nullptr;
51 napi_create_object(env, &resultCode);
52
53 AddUint32Property(env, resultCode, "INVALID_PARAMS", JS_ERR_CERT_INVALID_PARAMS);
54 AddUint32Property(env, resultCode, "NOT_SUPPORT", JS_ERR_CERT_NOT_SUPPORT);
55 AddUint32Property(env, resultCode, "ERR_OUT_OF_MEMORY", JS_ERR_CERT_OUT_OF_MEMORY);
56 AddUint32Property(env, resultCode, "ERR_RUNTIME_ERROR", JS_ERR_CERT_RUNTIME_ERROR);
57 AddUint32Property(env, resultCode, "ERR_CRYPTO_OPERATION", JS_ERR_CERT_CRYPTO_OPERATION);
58 AddUint32Property(env, resultCode, "ERR_CERT_SIGNATURE_FAILURE", JS_ERR_CERT_SIGNATURE_FAILURE);
59 AddUint32Property(env, resultCode, "ERR_CERT_NOT_YET_VALID", JS_ERR_CERT_NOT_YET_VALID);
60 AddUint32Property(env, resultCode, "ERR_CERT_HAS_EXPIRED", JS_ERR_CERT_HAS_EXPIRED);
61 AddUint32Property(env, resultCode, "ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
62 JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY);
63 AddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_CERTSIGN", JS_ERR_KEYUSAGE_NO_CERTSIGN);
64 AddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE", JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE);
65
66 return resultCode;
67 }
68
DefineResultCodeProperties(napi_env env,napi_value exports)69 static void DefineResultCodeProperties(napi_env env, napi_value exports)
70 {
71 napi_property_descriptor desc[] = {
72 DECLARE_NAPI_PROPERTY("CertResult", CreateCertResultCode(env)),
73 };
74 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
75 }
76
CreateCryptoModule(napi_env env)77 static void CreateCryptoModule(napi_env env)
78 {
79 napi_value globalValue;
80 napi_get_global(env, &globalValue);
81 napi_value func;
82 napi_get_named_property(env, globalValue, "requireNapi", &func);
83
84 napi_value cryptoInfo;
85 napi_create_string_utf8(env, "security.cryptoFramework", NAPI_AUTO_LENGTH, &cryptoInfo);
86 napi_value funcArgv[1] = { cryptoInfo };
87 napi_value returnValue;
88 napi_call_function(env, globalValue, func, 1, funcArgv, &returnValue);
89 }
90
91 /***********************************************
92 * Module export and register
93 ***********************************************/
CertModuleExport(napi_env env,napi_value exports)94 static napi_value CertModuleExport(napi_env env, napi_value exports)
95 {
96 LOGI("module init start.");
97 CreateCryptoModule(env);
98 DefineEncodingFormatProperties(env, exports);
99 DefineResultCodeProperties(env, exports);
100
101 NapiCertChainValidator::DefineCertChainValidatorJSClass(env, exports);
102 NapiX509Certificate::DefineX509CertJSClass(env, exports);
103 NapiX509CrlEntry::DefineX509CrlEntryJSClass(env);
104 NapiX509Crl::DefineX509CrlJSClass(env, exports);
105 LOGI("module init end.");
106 return exports;
107 }
108
RegisterCertModule(void)109 extern "C" __attribute__((constructor)) void RegisterCertModule(void)
110 {
111 static napi_module cryptoFrameworkCertModule = {
112 .nm_version = 1,
113 .nm_flags = 0,
114 .nm_filename = nullptr,
115 .nm_register_func = CertModuleExport,
116 .nm_modname = "security.cert",
117 .nm_priv = nullptr,
118 .reserved = { nullptr },
119 };
120 napi_module_register(&cryptoFrameworkCertModule);
121 }
122 } // namespace CryptoFramework
123 } // namespace OHOS
124