• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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, "SUCCESS", SUCCESS);
44         AddInt32Property(env, errorCode, "PARAM_ERROR", PARAM_ERROR);
45         AddInt32Property(env, errorCode, "FAILURE", INNER_FAILURE);
46         AddInt32Property(env, errorCode, "NO_PERMISSION", NO_PERMISSION);
47         AddInt32Property(env, errorCode, "NOT_FOUND", NOT_FOUND);
48         AddInt32Property(env, errorCode, "INVALID_CERT_FORMAT", INVALID_CERT_FORMAT);
49     }
50 
CreateCMErrorCode(napi_env env)51     static napi_value CreateCMErrorCode(napi_env env)
52     {
53         napi_value errorCode = nullptr;
54         NAPI_CALL(env, napi_create_object(env, &errorCode));
55 
56         AddCMErrorCodePart(env, errorCode);
57 
58         return errorCode;
59     }
60 
CreateCMKeyPurpose(napi_env env)61     static napi_value CreateCMKeyPurpose(napi_env env)
62     {
63         napi_value keyPurpose = nullptr;
64         NAPI_CALL(env, napi_create_object(env, &keyPurpose));
65 
66         AddInt32Property(env, keyPurpose, "CM_KEY_PURPOSE_SIGN", CM_KEY_PURPOSE_SIGN);
67         AddInt32Property(env, keyPurpose, "CM_KEY_PURPOSE_VERIFY", CM_KEY_PURPOSE_VERIFY);
68 
69         return keyPurpose;
70     }
71 }  // namespace CertManagerNapi
72 
73 using namespace CMNapi;
74 
75 extern "C" {
CMNapiRegister(napi_env env,napi_value exports)76     static napi_value CMNapiRegister(napi_env env, napi_value exports)
77     {
78         napi_property_descriptor desc[] = {
79             DECLARE_NAPI_PROPERTY("CMErrorCode", CreateCMErrorCode(env)),
80             DECLARE_NAPI_PROPERTY("CmKeyPurpose", CreateCMKeyPurpose(env)),
81 
82             DECLARE_NAPI_FUNCTION("getSystemTrustedCertificateList", CMNapiGetSystemCertList),
83             DECLARE_NAPI_FUNCTION("getSystemTrustedCertificate", CMNapiGetSystemCertInfo),
84             DECLARE_NAPI_FUNCTION("setCertificateStatus", CMNapiSetCertStatus),
85             DECLARE_NAPI_FUNCTION("installAppCertificate", CMNapiInstallAppCert),
86             DECLARE_NAPI_FUNCTION("uninstallAllAppCertificate", CMNapiUninstallAllAppCert),
87             DECLARE_NAPI_FUNCTION("uninstallAppCertificate", CMNapiUninstallAppCert),
88             DECLARE_NAPI_FUNCTION("getAppCertificateList", CMNapiGetAppCertList),
89             DECLARE_NAPI_FUNCTION("getAppCertificate", CMNapiGetAppCertInfo),
90 
91             DECLARE_NAPI_FUNCTION("installUserTrustedCertificate", CMNapiInstallUserTrustedCert),
92             DECLARE_NAPI_FUNCTION("uninstallAllUserTrustedCertificate", CMNapiUninstallAllUserTrustedCert),
93             DECLARE_NAPI_FUNCTION("uninstallUserTrustedCertificate", CMNapiUninstallUserTrustedCert),
94             DECLARE_NAPI_FUNCTION("getUserTrustedCertificateList", CMNapiGetUserTrustedCertList),
95             DECLARE_NAPI_FUNCTION("getUserTrustedCertificate", CMNapiGetUserTrustedCertInfo),
96             DECLARE_NAPI_FUNCTION("installPrivateCertificate", CMNapiInstallPrivateAppCert),
97             DECLARE_NAPI_FUNCTION("uninstallPrivateCertificate", CMNapiUninstallPrivateAppCert),
98             DECLARE_NAPI_FUNCTION("getPrivateCertificateList", CMNapiGetPrivateAppCertList),
99             DECLARE_NAPI_FUNCTION("getPrivateCertificate", CMNapiGetPrivateAppCertInfo),
100             DECLARE_NAPI_FUNCTION("grantAppCertificate", CMNapiGrantAppCertificate),
101             DECLARE_NAPI_FUNCTION("isAuthorizedApp", CMNapiIsAuthorizedApp),
102             DECLARE_NAPI_FUNCTION("getAuthorizedAppList", CMNapiGetAuthorizedAppList),
103             DECLARE_NAPI_FUNCTION("removeGrantedAppCertificate", CMNapiRemoveGrantedApp),
104             DECLARE_NAPI_FUNCTION("init", CMNapiInit),
105             DECLARE_NAPI_FUNCTION("update", CMNapiUpdate),
106             DECLARE_NAPI_FUNCTION("finish", CMNapiFinish),
107             DECLARE_NAPI_FUNCTION("abort", CMNapiAbort),
108         };
109         NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
110         return exports;
111     }
112 
113     static napi_module g_module = {
114         .nm_version = 1,
115         .nm_flags = 0,
116         .nm_filename = nullptr,
117         .nm_register_func = CMNapiRegister,
118         .nm_modname = "security.certmanager",
119         .nm_priv =  nullptr,
120         .reserved = { nullptr },
121     };
122 
CertManagerRegister(void)123     __attribute__((constructor)) void CertManagerRegister(void)
124     {
125         napi_module_register(&g_module);
126     }
127 }
128