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, "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 } 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 CreateCMKeyDigest(napi_env env)72 static napi_value CreateCMKeyDigest(napi_env env) 73 { 74 napi_value keyDigest = nullptr; 75 NAPI_CALL(env, napi_create_object(env, &keyDigest)); 76 77 AddInt32Property(env, keyDigest, "CM_DIGEST_NONE", CM_JS_DIGEST_NONE); 78 AddInt32Property(env, keyDigest, "CM_DIGEST_MD5", CM_JS_DIGEST_MD5); 79 AddInt32Property(env, keyDigest, "CM_DIGEST_SHA1", CM_JS_DIGEST_SHA1); 80 AddInt32Property(env, keyDigest, "CM_DIGEST_SHA224", CM_JS_DIGEST_SHA224); 81 AddInt32Property(env, keyDigest, "CM_DIGEST_SHA256", CM_JS_DIGEST_SHA256); 82 AddInt32Property(env, keyDigest, "CM_DIGEST_SHA384", CM_JS_DIGEST_SHA384); 83 AddInt32Property(env, keyDigest, "CM_DIGEST_SHA512", CM_JS_DIGEST_SHA512); 84 return keyDigest; 85 } 86 CreateCMKeyPadding(napi_env env)87 static napi_value CreateCMKeyPadding(napi_env env) 88 { 89 napi_value keyPadding = nullptr; 90 NAPI_CALL(env, napi_create_object(env, &keyPadding)); 91 92 AddInt32Property(env, keyPadding, "CM_PADDING_NONE", CM_JS_PADDING_NONE); 93 AddInt32Property(env, keyPadding, "CM_PADDING_PSS", CM_JS_PADDING_PSS); 94 AddInt32Property(env, keyPadding, "CM_PADDING_PKCS1_V1_5", CM_JS_PADDING_PKCS1_V1_5); 95 return keyPadding; 96 } 97 } // namespace CertManagerNapi 98 99 using namespace CMNapi; 100 101 extern "C" { CMNapiRegister(napi_env env,napi_value exports)102 static napi_value CMNapiRegister(napi_env env, napi_value exports) 103 { 104 napi_property_descriptor desc[] = { 105 DECLARE_NAPI_PROPERTY("CMErrorCode", CreateCMErrorCode(env)), 106 DECLARE_NAPI_PROPERTY("CmKeyPurpose", CreateCMKeyPurpose(env)), 107 DECLARE_NAPI_PROPERTY("CmKeyDigest", CreateCMKeyDigest(env)), 108 DECLARE_NAPI_PROPERTY("CmKeyPadding", CreateCMKeyPadding(env)), 109 110 DECLARE_NAPI_FUNCTION("getSystemTrustedCertificateList", CMNapiGetSystemCertList), 111 DECLARE_NAPI_FUNCTION("getSystemTrustedCertificate", CMNapiGetSystemCertInfo), 112 DECLARE_NAPI_FUNCTION("setCertificateStatus", CMNapiSetCertStatus), 113 DECLARE_NAPI_FUNCTION("installAppCertificate", CMNapiInstallAppCert), 114 DECLARE_NAPI_FUNCTION("uninstallAllAppCertificate", CMNapiUninstallAllAppCert), 115 DECLARE_NAPI_FUNCTION("uninstallAppCertificate", CMNapiUninstallAppCert), 116 DECLARE_NAPI_FUNCTION("getAppCertificateList", CMNapiGetAppCertList), 117 DECLARE_NAPI_FUNCTION("getAppCertificate", CMNapiGetAppCertInfo), 118 119 DECLARE_NAPI_FUNCTION("installUserTrustedCertificate", CMNapiInstallUserTrustedCert), 120 DECLARE_NAPI_FUNCTION("uninstallAllUserTrustedCertificate", CMNapiUninstallAllUserTrustedCert), 121 DECLARE_NAPI_FUNCTION("uninstallUserTrustedCertificate", CMNapiUninstallUserTrustedCert), 122 DECLARE_NAPI_FUNCTION("getUserTrustedCertificateList", CMNapiGetUserTrustedCertList), 123 DECLARE_NAPI_FUNCTION("getUserTrustedCertificate", CMNapiGetUserTrustedCertInfo), 124 DECLARE_NAPI_FUNCTION("installPrivateCertificate", CMNapiInstallPrivateAppCert), 125 DECLARE_NAPI_FUNCTION("uninstallPrivateCertificate", CMNapiUninstallPrivateAppCert), 126 DECLARE_NAPI_FUNCTION("getPrivateCertificateList", CMNapiGetPrivateAppCertList), 127 DECLARE_NAPI_FUNCTION("getAllAppPrivateCertificates", CMNapiGetPrivateAppCertList), 128 DECLARE_NAPI_FUNCTION("getPrivateCertificate", CMNapiGetPrivateAppCertInfo), 129 DECLARE_NAPI_FUNCTION("grantAppCertificate", CMNapiGrantAppCertificate), 130 DECLARE_NAPI_FUNCTION("isAuthorizedApp", CMNapiIsAuthorizedApp), 131 DECLARE_NAPI_FUNCTION("getAuthorizedAppList", CMNapiGetAuthorizedAppList), 132 DECLARE_NAPI_FUNCTION("removeGrantedAppCertificate", CMNapiRemoveGrantedApp), 133 DECLARE_NAPI_FUNCTION("init", CMNapiInit), 134 DECLARE_NAPI_FUNCTION("update", CMNapiUpdate), 135 DECLARE_NAPI_FUNCTION("finish", CMNapiFinish), 136 DECLARE_NAPI_FUNCTION("abort", CMNapiAbort), 137 }; 138 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 139 return exports; 140 } 141 142 static napi_module g_module = { 143 .nm_version = 1, 144 .nm_flags = 0, 145 .nm_filename = nullptr, 146 .nm_register_func = CMNapiRegister, 147 .nm_modname = "security.certmanager", 148 .nm_priv = nullptr, 149 .reserved = { nullptr }, 150 }; 151 CertManagerRegister(void)152 __attribute__((constructor)) void CertManagerRegister(void) 153 { 154 napi_module_register(&g_module); 155 } 156 } 157