• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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/native_api.h"
17 #include "napi/native_node_api.h"
18 
19 #include "cm_napi_common.h"
20 
21 #include "cm_napi_get_system_cert_list.h"
22 #include "cm_napi_get_system_cert_info.h"
23 #include "cm_napi_set_cert_status.h"
24 #include "cm_napi_install_app_cert.h"
25 #include "cm_napi_uninstall_app_cert.h"
26 #include "cm_napi_uninstall_all_app_cert.h"
27 #include "cm_napi_get_app_cert_list.h"
28 #include "cm_napi_get_app_cert_info.h"
29 #include "cm_napi_grant.h"
30 #include "cm_napi_sign_verify.h"
31 #include "cm_napi_user_trusted_cert.h"
32 #include "cm_napi_get_cert_store_path.h"
33 
34 namespace CMNapi {
AddInt32Property(napi_env env,napi_value object,const char * name,int32_t value)35     inline void AddInt32Property(napi_env env, napi_value object, const char *name, int32_t value)
36     {
37         napi_value property = nullptr;
38         NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, value, &property));
39         NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, name, property));
40     }
41 
AddCMErrorCodePart(napi_env env,napi_value errorCode)42     static void AddCMErrorCodePart(napi_env env, napi_value errorCode)
43     {
44         AddInt32Property(env, errorCode, "CM_ERROR_NO_PERMISSION", HAS_NO_PERMISSION);
45         AddInt32Property(env, errorCode, "CM_ERROR_NOT_SYSTEM_APP", NOT_SYSTEM_APP);
46         AddInt32Property(env, errorCode, "CM_ERROR_INVALID_PARAMS", PARAM_ERROR);
47         AddInt32Property(env, errorCode, "CM_ERROR_GENERIC", INNER_FAILURE);
48         AddInt32Property(env, errorCode, "CM_ERROR_NO_FOUND", NOT_FOUND);
49         AddInt32Property(env, errorCode, "CM_ERROR_INCORRECT_FORMAT", INVALID_CERT_FORMAT);
50         AddInt32Property(env, errorCode, "CM_ERROR_MAX_CERT_COUNT_REACHED", MAX_CERT_COUNT_REACHED);
51         AddInt32Property(env, errorCode, "CM_ERROR_NO_AUTHORIZATION", NO_AUTHORIZATION);
52         AddInt32Property(env, errorCode, "CM_ERROR_ALIAS_LENGTH_REACHED_LIMIT", ALIAS_LENGTH_REACHED_LIMIT);
53         AddInt32Property(env, errorCode, "CM_ERROR_DEVICE_ENTER_ADVSECMODE", DEVICE_ENTER_ADVSECMODE);
54         AddInt32Property(env, errorCode, "CM_ERROR_PASSWORD_IS_ERR", PASSWORD_IS_ERROR);
55     }
56 
CreateCMErrorCode(napi_env env)57     static napi_value CreateCMErrorCode(napi_env env)
58     {
59         napi_value errorCode = nullptr;
60         NAPI_CALL(env, napi_create_object(env, &errorCode));
61 
62         AddCMErrorCodePart(env, errorCode);
63 
64         return errorCode;
65     }
66 
CreateCMKeyPurpose(napi_env env)67     static napi_value CreateCMKeyPurpose(napi_env env)
68     {
69         napi_value keyPurpose = nullptr;
70         NAPI_CALL(env, napi_create_object(env, &keyPurpose));
71 
72         AddInt32Property(env, keyPurpose, "CM_KEY_PURPOSE_SIGN", CM_KEY_PURPOSE_SIGN);
73         AddInt32Property(env, keyPurpose, "CM_KEY_PURPOSE_VERIFY", CM_KEY_PURPOSE_VERIFY);
74 
75         return keyPurpose;
76     }
77 
CreateCMKeyDigest(napi_env env)78     static napi_value CreateCMKeyDigest(napi_env env)
79     {
80         napi_value keyDigest = nullptr;
81         NAPI_CALL(env, napi_create_object(env, &keyDigest));
82 
83         AddInt32Property(env, keyDigest, "CM_DIGEST_NONE", CM_JS_DIGEST_NONE);
84         AddInt32Property(env, keyDigest, "CM_DIGEST_MD5", CM_JS_DIGEST_MD5);
85         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA1", CM_JS_DIGEST_SHA1);
86         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA224", CM_JS_DIGEST_SHA224);
87         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA256", CM_JS_DIGEST_SHA256);
88         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA384", CM_JS_DIGEST_SHA384);
89         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA512", CM_JS_DIGEST_SHA512);
90         AddInt32Property(env, keyDigest, "CM_DIGEST_SM3", CM_JS_DIGEST_SM3);
91         return keyDigest;
92     }
93 
CreateCMKeyPadding(napi_env env)94     static napi_value CreateCMKeyPadding(napi_env env)
95     {
96         napi_value keyPadding = nullptr;
97         NAPI_CALL(env, napi_create_object(env, &keyPadding));
98 
99         AddInt32Property(env, keyPadding, "CM_PADDING_NONE", CM_JS_PADDING_NONE);
100         AddInt32Property(env, keyPadding, "CM_PADDING_PSS", CM_JS_PADDING_PSS);
101         AddInt32Property(env, keyPadding, "CM_PADDING_PKCS1_V1_5", CM_JS_PADDING_PKCS1_V1_5);
102         return keyPadding;
103     }
104 
CreateCertType(napi_env env)105     static napi_value CreateCertType(napi_env env)
106     {
107         napi_value type = nullptr;
108         NAPI_CALL(env, napi_create_object(env, &type));
109 
110         AddInt32Property(env, type, "CA_CERT_SYSTEM", CM_CA_CERT_SYSTEM);
111         AddInt32Property(env, type, "CA_CERT_USER", CM_CA_CERT_USER);
112         return type;
113     }
114 
CreateCertScope(napi_env env)115     static napi_value CreateCertScope(napi_env env)
116     {
117         napi_value scope = nullptr;
118         NAPI_CALL(env, napi_create_object(env, &scope));
119 
120         AddInt32Property(env, scope, "CURRENT_USER", CM_CURRENT_USER);
121         AddInt32Property(env, scope, "GLOBAL_USER", CM_GLOBAL_USER);
122         return scope;
123     }
124 
CreateCertFileFormat(napi_env env)125     static napi_value CreateCertFileFormat(napi_env env)
126     {
127         napi_value format = nullptr;
128         NAPI_CALL(env, napi_create_object(env, &format));
129 
130         AddInt32Property(env, format, "PEM_DER", PEM_DER);
131         AddInt32Property(env, format, "P7B", P7B);
132         return format;
133     }
134 
CreateAuthStorageLevel(napi_env env)135     static napi_value CreateAuthStorageLevel(napi_env env)
136     {
137         napi_value level = nullptr;
138         NAPI_CALL(env, napi_create_object(env, &level));
139 
140         AddInt32Property(env, level, "EL1", CM_AUTH_STORAGE_LEVEL_EL1);
141         AddInt32Property(env, level, "EL2", CM_AUTH_STORAGE_LEVEL_EL2);
142         AddInt32Property(env, level, "EL4", CM_AUTH_STORAGE_LEVEL_EL4);
143         return level;
144     }
145 
CreateCertAlgorithm(napi_env env)146     static napi_value CreateCertAlgorithm(napi_env env)
147     {
148         napi_value algorithm = nullptr;
149         NAPI_CALL(env, napi_create_object(env, &algorithm));
150 
151         AddInt32Property(env, algorithm, "INTERNATIONAL", CM_ALG_INTERNATIONAL);
152         AddInt32Property(env, algorithm, "SM", CM_ALG_SM);
153         return algorithm;
154     }
155 }  // namespace CertManagerNapi
156 
157 using namespace CMNapi;
158 
159 extern "C" {
CMNapiRegister(napi_env env,napi_value exports)160     static napi_value CMNapiRegister(napi_env env, napi_value exports)
161     {
162         napi_property_descriptor desc[] = {
163             DECLARE_NAPI_PROPERTY("CMErrorCode", CreateCMErrorCode(env)),
164             DECLARE_NAPI_PROPERTY("CmKeyPurpose", CreateCMKeyPurpose(env)),
165             DECLARE_NAPI_PROPERTY("CmKeyDigest", CreateCMKeyDigest(env)),
166             DECLARE_NAPI_PROPERTY("CmKeyPadding", CreateCMKeyPadding(env)),
167             DECLARE_NAPI_PROPERTY("CertType", CreateCertType(env)),
168             DECLARE_NAPI_PROPERTY("CertScope", CreateCertScope(env)),
169             DECLARE_NAPI_PROPERTY("CertFileFormat", CreateCertFileFormat(env)),
170             DECLARE_NAPI_PROPERTY("AuthStorageLevel", CreateAuthStorageLevel(env)),
171             DECLARE_NAPI_PROPERTY("CertAlgorithm", CreateCertAlgorithm(env)),
172 
173             /* system ca */
174             DECLARE_NAPI_FUNCTION("getSystemTrustedCertificateList", CMNapiGetSystemCertList),
175             DECLARE_NAPI_FUNCTION("getSystemTrustedCertificate", CMNapiGetSystemCertInfo),
176             DECLARE_NAPI_FUNCTION("setCertificateStatus", CMNapiSetCertStatus),
177 
178             /* user public cred */
179             DECLARE_NAPI_FUNCTION("installPublicCertificate", CMNapiInstallPublicCert),
180             DECLARE_NAPI_FUNCTION("uninstallAllAppCertificate", CMNapiUninstallAllAppCert),
181             DECLARE_NAPI_FUNCTION("uninstallPublicCertificate", CMNapiUninstallPublicCert),
182             DECLARE_NAPI_FUNCTION("getAllPublicCertificates", CMNapiGetAllPublicCertList),
183             DECLARE_NAPI_FUNCTION("getPublicCertificate", CMNapiGetPublicCertInfo),
184 
185             /* user ca */
186             DECLARE_NAPI_FUNCTION("installUserTrustedCertificate", CMNapiInstallUserTrustedCert),
187             DECLARE_NAPI_FUNCTION("installUserTrustedCertificateSync", CMNapiInstallUserTrustedCertSync),
188             DECLARE_NAPI_FUNCTION("uninstallAllUserTrustedCertificate", CMNapiUninstallAllUserTrustedCert),
189             DECLARE_NAPI_FUNCTION("uninstallUserTrustedCertificate", CMNapiUninstallUserTrustedCert),
190             DECLARE_NAPI_FUNCTION("getAllUserTrustedCertificates", CMNapiGetAllUserTrustedCertList),
191             DECLARE_NAPI_FUNCTION("getUserTrustedCertificate", CMNapiGetUserTrustedCertInfo),
192             DECLARE_NAPI_FUNCTION("uninstallUserTrustedCertificateSync", CMNapiUninstallUserCertSync),
193 
194             /* private cred */
195             DECLARE_NAPI_FUNCTION("installPrivateCertificate", CMNapiInstallPrivateAppCert),
196             DECLARE_NAPI_FUNCTION("uninstallPrivateCertificate", CMNapiUninstallPrivateAppCert),
197             DECLARE_NAPI_FUNCTION("getAllAppPrivateCertificates", CMNapiGetPrivateAppCertList),
198             DECLARE_NAPI_FUNCTION("getPrivateCertificate", CMNapiGetPrivateAppCertInfo),
199             DECLARE_NAPI_FUNCTION("getPrivateCertificates", CMNapiGetCallingPrivateAppCertList),
200 
201             /* grant, sign and verify */
202             DECLARE_NAPI_FUNCTION("grantPublicCertificate", CMNapiGrantPublicCertificate),
203             DECLARE_NAPI_FUNCTION("isAuthorizedApp", CMNapiIsAuthorizedApp),
204             DECLARE_NAPI_FUNCTION("getAuthorizedAppList", CMNapiGetAuthorizedAppList),
205             DECLARE_NAPI_FUNCTION("removeGrantedPublicCertificate", CMNapiRemoveGrantedPublic),
206             DECLARE_NAPI_FUNCTION("init", CMNapiInit),
207             DECLARE_NAPI_FUNCTION("update", CMNapiUpdate),
208             DECLARE_NAPI_FUNCTION("finish", CMNapiFinish),
209             DECLARE_NAPI_FUNCTION("abort", CMNapiAbort),
210 
211             /* system cred */
212             DECLARE_NAPI_FUNCTION("installSystemAppCertificate", CMNapiInstallSystemAppCert),
213             DECLARE_NAPI_FUNCTION("uninstallSystemAppCertificate", CMNapiUninstallSystemAppCert),
214             DECLARE_NAPI_FUNCTION("getAllSystemAppCertificates", CMNapiGetSystemAppCertList),
215             DECLARE_NAPI_FUNCTION("getSystemAppCertificate", CMNapiGetSystemAppCertInfo),
216 
217             /* get store path */
218             DECLARE_NAPI_FUNCTION("getCertificateStorePath", CMNapiGetCertStorePath),
219         };
220         NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
221         return exports;
222     }
223 
224     static napi_module g_module = {
225         .nm_version = 1,
226         .nm_flags = 0,
227         .nm_filename = nullptr,
228         .nm_register_func = CMNapiRegister,
229         .nm_modname = "security.certmanager",
230         .nm_priv =  nullptr,
231         .reserved = { nullptr },
232     };
233 
CertManagerRegister(void)234     __attribute__((constructor)) void CertManagerRegister(void)
235     {
236         napi_module_register(&g_module);
237     }
238 }
239