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 "cf_log.h"
17 #include "cf_type.h"
18 #include "napi_cert_chain_validator.h"
19 #include "napi_cert_defines.h"
20 #include "napi_cert_extension.h"
21 #include "napi_cert_utils.h"
22 #include "napi_pub_key.h"
23 #include "napi_x509_cert_chain.h"
24 #include "napi_x509_certificate.h"
25 #include "napi_x509_crl.h"
26 #include "napi_x509_crl_entry.h"
27 #include "napi_cert_crl_collection.h"
28 #include "securec.h"
29
30 namespace OHOS {
31 namespace CertFramework {
CreateEncodingFormat(napi_env env)32 static napi_value CreateEncodingFormat(napi_env env)
33 {
34 napi_value encodingFormat = nullptr;
35 napi_create_object(env, &encodingFormat);
36
37 CertAddUint32Property(env, encodingFormat, "FORMAT_DER", CF_FORMAT_DER);
38 CertAddUint32Property(env, encodingFormat, "FORMAT_PEM", CF_FORMAT_PEM);
39 CertAddUint32Property(env, encodingFormat, "FORMAT_PKCS7", CF_FORMAT_PKCS7);
40
41 return encodingFormat;
42 }
43
DefineEncodingFormatProperties(napi_env env,napi_value exports)44 static void DefineEncodingFormatProperties(napi_env env, napi_value exports)
45 {
46 napi_property_descriptor desc[] = {
47 DECLARE_NAPI_PROPERTY("EncodingFormat", CreateEncodingFormat(env)),
48 };
49 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
50 }
51
CreateCertResultCode(napi_env env)52 static napi_value CreateCertResultCode(napi_env env)
53 {
54 napi_value resultCode = nullptr;
55 napi_create_object(env, &resultCode);
56
57 CertAddUint32Property(env, resultCode, "INVALID_PARAMS", JS_ERR_CERT_INVALID_PARAMS);
58 CertAddUint32Property(env, resultCode, "NOT_SUPPORT", JS_ERR_CERT_NOT_SUPPORT);
59 CertAddUint32Property(env, resultCode, "ERR_OUT_OF_MEMORY", JS_ERR_CERT_OUT_OF_MEMORY);
60 CertAddUint32Property(env, resultCode, "ERR_RUNTIME_ERROR", JS_ERR_CERT_RUNTIME_ERROR);
61 CertAddUint32Property(env, resultCode, "ERR_CRYPTO_OPERATION", JS_ERR_CERT_CRYPTO_OPERATION);
62 CertAddUint32Property(env, resultCode, "ERR_CERT_SIGNATURE_FAILURE", JS_ERR_CERT_SIGNATURE_FAILURE);
63 CertAddUint32Property(env, resultCode, "ERR_CERT_NOT_YET_VALID", JS_ERR_CERT_NOT_YET_VALID);
64 CertAddUint32Property(env, resultCode, "ERR_CERT_HAS_EXPIRED", JS_ERR_CERT_HAS_EXPIRED);
65 CertAddUint32Property(env, resultCode, "ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
66 JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY);
67 CertAddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_CERTSIGN", JS_ERR_KEYUSAGE_NO_CERTSIGN);
68 CertAddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE", JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE);
69
70 return resultCode;
71 }
72
DefineResultCodeProperties(napi_env env,napi_value exports)73 static void DefineResultCodeProperties(napi_env env, napi_value exports)
74 {
75 napi_property_descriptor desc[] = {
76 DECLARE_NAPI_PROPERTY("CertResult", CreateCertResultCode(env)),
77 };
78 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
79 }
80
CreateCertItemType(napi_env env)81 static napi_value CreateCertItemType(napi_env env)
82 {
83 napi_value certItemType = nullptr;
84 napi_create_object(env, &certItemType);
85
86 CertAddUint32Property(env, certItemType, "CERT_ITEM_TYPE_TBS", CF_ITEM_TBS);
87 CertAddUint32Property(env, certItemType, "CERT_ITEM_TYPE_PUBLIC_KEY", CF_ITEM_PUBLIC_KEY);
88 CertAddUint32Property(env, certItemType, "CERT_ITEM_TYPE_ISSUER_UNIQUE_ID", CF_ITEM_ISSUER_UNIQUE_ID);
89 CertAddUint32Property(env, certItemType, "CERT_ITEM_TYPE_SUBJECT_UNIQUE_ID", CF_ITEM_SUBJECT_UNIQUE_ID);
90 CertAddUint32Property(env, certItemType, "CERT_ITEM_TYPE_EXTENSIONS", CF_ITEM_EXTENSIONS);
91
92 return certItemType;
93 }
94
DefineCertItemTypeProperties(napi_env env,napi_value exports)95 static void DefineCertItemTypeProperties(napi_env env, napi_value exports)
96 {
97 napi_property_descriptor desc[] = {
98 DECLARE_NAPI_PROPERTY("CertItemType", CreateCertItemType(env)),
99 };
100 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
101 }
102
CreateExtensionOidType(napi_env env)103 static napi_value CreateExtensionOidType(napi_env env)
104 {
105 napi_value extensionOidType = nullptr;
106 napi_create_object(env, &extensionOidType);
107
108 CertAddUint32Property(env, extensionOidType, "EXTENSION_OID_TYPE_ALL", CF_EXT_TYPE_ALL_OIDS);
109 CertAddUint32Property(env, extensionOidType, "EXTENSION_OID_TYPE_CRITICAL", CF_EXT_TYPE_CRITICAL_OIDS);
110 CertAddUint32Property(env, extensionOidType, "EXTENSION_OID_TYPE_UNCRITICAL", CF_EXT_TYPE_UNCRITICAL_OIDS);
111
112 return extensionOidType;
113 }
114
DefineExtensionOidTypeProperties(napi_env env,napi_value exports)115 static void DefineExtensionOidTypeProperties(napi_env env, napi_value exports)
116 {
117 napi_property_descriptor desc[] = {
118 DECLARE_NAPI_PROPERTY("ExtensionOidType", CreateExtensionOidType(env)),
119 };
120 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
121 }
122
CreateExtensionEntryType(napi_env env)123 static napi_value CreateExtensionEntryType(napi_env env)
124 {
125 napi_value extensionEntryType = nullptr;
126 napi_create_object(env, &extensionEntryType);
127
128 CertAddUint32Property(env, extensionEntryType, "EXTENSION_ENTRY_TYPE_ENTRY", CF_EXT_ENTRY_TYPE_ENTRY);
129 CertAddUint32Property(env, extensionEntryType, "EXTENSION_ENTRY_TYPE_ENTRY_CRITICAL",
130 CF_EXT_ENTRY_TYPE_ENTRY_CRITICAL);
131 CertAddUint32Property(env, extensionEntryType, "EXTENSION_ENTRY_TYPE_ENTRY_VALUE", CF_EXT_ENTRY_TYPE_ENTRY_VALUE);
132
133 return extensionEntryType;
134 }
135
DefineExtensionEntryTypeProperties(napi_env env,napi_value exports)136 static void DefineExtensionEntryTypeProperties(napi_env env, napi_value exports)
137 {
138 napi_property_descriptor desc[] = {
139 DECLARE_NAPI_PROPERTY("ExtensionEntryType", CreateExtensionEntryType(env)),
140 };
141 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
142 }
143
144 /***********************************************
145 * Module export and register
146 ***********************************************/
CertModuleExport(napi_env env,napi_value exports)147 static napi_value CertModuleExport(napi_env env, napi_value exports)
148 {
149 LOGI("module init start.");
150 DefineEncodingFormatProperties(env, exports);
151 DefineResultCodeProperties(env, exports);
152 DefineCertItemTypeProperties(env, exports);
153 DefineExtensionOidTypeProperties(env, exports);
154 DefineExtensionEntryTypeProperties(env, exports);
155
156 NapiKey::DefineHcfKeyJSClass(env);
157 NapiPubKey::DefinePubKeyJSClass(env);
158 NapiCertChainValidator::DefineCertChainValidatorJSClass(env, exports);
159 NapiX509Certificate::DefineX509CertJSClass(env, exports);
160 NapiX509CrlEntry::DefineX509CrlEntryJSClass(env, std::string("X509CrlEntry"));
161 NapiX509CrlEntry::DefineX509CrlEntryJSClass(env, std::string("X509CRLEntry"));
162 NapiX509Crl::DefineX509CrlJSClass(env, exports, std::string("X509Crl"));
163 NapiX509Crl::DefineX509CrlJSClass(env, exports, std::string("X509CRL"));
164 NapiCertExtension::DefineCertExtensionJsClass(env, exports);
165 NapiX509CertChain::DefineX509CertChainJsClass(env, exports);
166 NapiCertCRLCollection::DefineCertCRLCollectionJSClass(env, exports);
167 LOGI("module init end.");
168 return exports;
169 }
170
RegisterCertModule(void)171 extern "C" __attribute__((constructor)) void RegisterCertModule(void)
172 {
173 static napi_module cryptoFrameworkCertModule = {
174 .nm_version = 1,
175 .nm_flags = 0,
176 .nm_filename = nullptr,
177 .nm_register_func = CertModuleExport,
178 .nm_modname = "security.cert",
179 .nm_priv = nullptr,
180 .reserved = { nullptr },
181 };
182 napi_module_register(&cryptoFrameworkCertModule);
183 }
184 } // namespace CertFramework
185 } // namespace OHOS
186