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 
33 namespace CMNapi {
AddInt32Property(napi_env env,napi_value object,const char * name,int32_t value)34     inline void AddInt32Property(napi_env env, napi_value object, const char *name, int32_t value)
35     {
36         napi_value property = nullptr;
37         NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, value, &property));
38         NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, name, property));
39     }
40 
AddCMErrorCodePart(napi_env env,napi_value errorCode)41     static void AddCMErrorCodePart(napi_env env, napi_value errorCode)
42     {
43         AddInt32Property(env, errorCode, "CM_ERROR_NO_PERMISSION", HAS_NO_PERMISSION);
44         AddInt32Property(env, errorCode, "CM_ERROR_NOT_SYSTEM_APP", NOT_SYSTEM_APP);
45         AddInt32Property(env, errorCode, "CM_ERROR_INVALID_PARAMS", PARAM_ERROR);
46         AddInt32Property(env, errorCode, "CM_ERROR_GENERIC", INNER_FAILURE);
47         AddInt32Property(env, errorCode, "CM_ERROR_NO_FOUND", NOT_FOUND);
48         AddInt32Property(env, errorCode, "CM_ERROR_INCORRECT_FORMAT", INVALID_CERT_FORMAT);
49         AddInt32Property(env, errorCode, "CM_ERROR_MAX_CERT_COUNT_REACHED", MAX_CERT_COUNT_REACHED);
50         AddInt32Property(env, errorCode, "CM_ERROR_NO_AUTHORIZATION", NO_AUTHORIZATION);
51         AddInt32Property(env, errorCode, "CM_ERROR_ALIAS_LENGTH_REACHED_LIMIT", ALIAS_LENGTH_REACHED_LIMIT);
52         AddInt32Property(env, errorCode, "CM_ERROR_DEVICE_ENTER_ADVSECMODE", DEVICE_ENTER_ADVSECMODE);
53         AddInt32Property(env, errorCode, "CM_ERROR_PASSWORD_IS_ERR", PASSWORD_IS_ERROR);
54     }
55 
CreateCMErrorCode(napi_env env)56     static napi_value CreateCMErrorCode(napi_env env)
57     {
58         napi_value errorCode = nullptr;
59         NAPI_CALL(env, napi_create_object(env, &errorCode));
60 
61         AddCMErrorCodePart(env, errorCode);
62 
63         return errorCode;
64     }
65 
CreateCMKeyPurpose(napi_env env)66     static napi_value CreateCMKeyPurpose(napi_env env)
67     {
68         napi_value keyPurpose = nullptr;
69         NAPI_CALL(env, napi_create_object(env, &keyPurpose));
70 
71         AddInt32Property(env, keyPurpose, "CM_KEY_PURPOSE_SIGN", CM_KEY_PURPOSE_SIGN);
72         AddInt32Property(env, keyPurpose, "CM_KEY_PURPOSE_VERIFY", CM_KEY_PURPOSE_VERIFY);
73 
74         return keyPurpose;
75     }
76 
CreateCMKeyDigest(napi_env env)77     static napi_value CreateCMKeyDigest(napi_env env)
78     {
79         napi_value keyDigest = nullptr;
80         NAPI_CALL(env, napi_create_object(env, &keyDigest));
81 
82         AddInt32Property(env, keyDigest, "CM_DIGEST_NONE", CM_JS_DIGEST_NONE);
83         AddInt32Property(env, keyDigest, "CM_DIGEST_MD5", CM_JS_DIGEST_MD5);
84         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA1", CM_JS_DIGEST_SHA1);
85         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA224", CM_JS_DIGEST_SHA224);
86         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA256", CM_JS_DIGEST_SHA256);
87         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA384", CM_JS_DIGEST_SHA384);
88         AddInt32Property(env, keyDigest, "CM_DIGEST_SHA512", CM_JS_DIGEST_SHA512);
89         return keyDigest;
90     }
91 
CreateCMKeyPadding(napi_env env)92     static napi_value CreateCMKeyPadding(napi_env env)
93     {
94         napi_value keyPadding = nullptr;
95         NAPI_CALL(env, napi_create_object(env, &keyPadding));
96 
97         AddInt32Property(env, keyPadding, "CM_PADDING_NONE", CM_JS_PADDING_NONE);
98         AddInt32Property(env, keyPadding, "CM_PADDING_PSS", CM_JS_PADDING_PSS);
99         AddInt32Property(env, keyPadding, "CM_PADDING_PKCS1_V1_5", CM_JS_PADDING_PKCS1_V1_5);
100         return keyPadding;
101     }
102 }  // namespace CertManagerNapi
103 
104 using namespace CMNapi;
105 
106 extern "C" {
CMNapiRegister(napi_env env,napi_value exports)107     static napi_value CMNapiRegister(napi_env env, napi_value exports)
108     {
109         napi_property_descriptor desc[] = {
110             DECLARE_NAPI_PROPERTY("CMErrorCode", CreateCMErrorCode(env)),
111             DECLARE_NAPI_PROPERTY("CmKeyPurpose", CreateCMKeyPurpose(env)),
112             DECLARE_NAPI_PROPERTY("CmKeyDigest", CreateCMKeyDigest(env)),
113             DECLARE_NAPI_PROPERTY("CmKeyPadding", CreateCMKeyPadding(env)),
114 
115             /* system ca */
116             DECLARE_NAPI_FUNCTION("getSystemTrustedCertificateList", CMNapiGetSystemCertList),
117             DECLARE_NAPI_FUNCTION("getSystemTrustedCertificate", CMNapiGetSystemCertInfo),
118             DECLARE_NAPI_FUNCTION("setCertificateStatus", CMNapiSetCertStatus),
119 
120             /* user public cred */
121             DECLARE_NAPI_FUNCTION("installPublicCertificate", CMNapiInstallPublicCert),
122             DECLARE_NAPI_FUNCTION("uninstallAllAppCertificate", CMNapiUninstallAllAppCert),
123             DECLARE_NAPI_FUNCTION("uninstallPublicCertificate", CMNapiUninstallPublicCert),
124             DECLARE_NAPI_FUNCTION("getAllPublicCertificates", CMNapiGetAllPublicCertList),
125             DECLARE_NAPI_FUNCTION("getPublicCertificate", CMNapiGetPublicCertInfo),
126 
127             /* user ca */
128             DECLARE_NAPI_FUNCTION("installUserTrustedCertificate", CMNapiInstallUserTrustedCert),
129             DECLARE_NAPI_FUNCTION("uninstallAllUserTrustedCertificate", CMNapiUninstallAllUserTrustedCert),
130             DECLARE_NAPI_FUNCTION("uninstallUserTrustedCertificate", CMNapiUninstallUserTrustedCert),
131             DECLARE_NAPI_FUNCTION("getAllUserTrustedCertificates", CMNapiGetAllUserTrustedCertList),
132             DECLARE_NAPI_FUNCTION("getUserTrustedCertificate", CMNapiGetUserTrustedCertInfo),
133 
134             /* private cred */
135             DECLARE_NAPI_FUNCTION("installPrivateCertificate", CMNapiInstallPrivateAppCert),
136             DECLARE_NAPI_FUNCTION("uninstallPrivateCertificate", CMNapiUninstallPrivateAppCert),
137             DECLARE_NAPI_FUNCTION("getAllAppPrivateCertificates", CMNapiGetPrivateAppCertList),
138             DECLARE_NAPI_FUNCTION("getPrivateCertificate", CMNapiGetPrivateAppCertInfo),
139             DECLARE_NAPI_FUNCTION("getPrivateCertificates", CMNapiGetCallingPrivateAppCertList),
140 
141             /* grant, sign and verify */
142             DECLARE_NAPI_FUNCTION("grantPublicCertificate", CMNapiGrantPublicCertificate),
143             DECLARE_NAPI_FUNCTION("isAuthorizedApp", CMNapiIsAuthorizedApp),
144             DECLARE_NAPI_FUNCTION("getAuthorizedAppList", CMNapiGetAuthorizedAppList),
145             DECLARE_NAPI_FUNCTION("removeGrantedPublicCertificate", CMNapiRemoveGrantedPublic),
146             DECLARE_NAPI_FUNCTION("init", CMNapiInit),
147             DECLARE_NAPI_FUNCTION("update", CMNapiUpdate),
148             DECLARE_NAPI_FUNCTION("finish", CMNapiFinish),
149             DECLARE_NAPI_FUNCTION("abort", CMNapiAbort),
150 
151             /* system cred */
152             DECLARE_NAPI_FUNCTION("installSystemAppCertificate", CMNapiInstallSystemAppCert),
153             DECLARE_NAPI_FUNCTION("uninstallSystemAppCertificate", CMNapiUninstallSystemAppCert),
154             DECLARE_NAPI_FUNCTION("getAllSystemAppCertificates", CMNapiGetSystemAppCertList),
155             DECLARE_NAPI_FUNCTION("getSystemAppCertificate", CMNapiGetSystemAppCertInfo),
156         };
157         NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
158         return exports;
159     }
160 
161     static napi_module g_module = {
162         .nm_version = 1,
163         .nm_flags = 0,
164         .nm_filename = nullptr,
165         .nm_register_func = CMNapiRegister,
166         .nm_modname = "security.certmanager",
167         .nm_priv =  nullptr,
168         .reserved = { nullptr },
169     };
170 
CertManagerRegister(void)171     __attribute__((constructor)) void CertManagerRegister(void)
172     {
173         napi_module_register(&g_module);
174     }
175 }
176