14e56987cSopenharmony_ci/*
24e56987cSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
34e56987cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44e56987cSopenharmony_ci * you may not use this file except in compliance with the License.
54e56987cSopenharmony_ci * You may obtain a copy of the License at
64e56987cSopenharmony_ci *
74e56987cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84e56987cSopenharmony_ci *
94e56987cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104e56987cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114e56987cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124e56987cSopenharmony_ci * See the License for the specific language governing permissions and
134e56987cSopenharmony_ci * limitations under the License.
144e56987cSopenharmony_ci */
154e56987cSopenharmony_ci
164e56987cSopenharmony_ci#include "napi/native_api.h"
174e56987cSopenharmony_ci#include "napi/native_node_api.h"
184e56987cSopenharmony_ci
194e56987cSopenharmony_ci#include "cm_napi_common.h"
204e56987cSopenharmony_ci
214e56987cSopenharmony_ci#include "cm_napi_get_system_cert_list.h"
224e56987cSopenharmony_ci#include "cm_napi_get_system_cert_info.h"
234e56987cSopenharmony_ci#include "cm_napi_set_cert_status.h"
244e56987cSopenharmony_ci#include "cm_napi_install_app_cert.h"
254e56987cSopenharmony_ci#include "cm_napi_uninstall_app_cert.h"
264e56987cSopenharmony_ci#include "cm_napi_uninstall_all_app_cert.h"
274e56987cSopenharmony_ci#include "cm_napi_get_app_cert_list.h"
284e56987cSopenharmony_ci#include "cm_napi_get_app_cert_info.h"
294e56987cSopenharmony_ci#include "cm_napi_grant.h"
304e56987cSopenharmony_ci#include "cm_napi_sign_verify.h"
314e56987cSopenharmony_ci#include "cm_napi_user_trusted_cert.h"
324e56987cSopenharmony_ci
334e56987cSopenharmony_cinamespace CMNapi {
344e56987cSopenharmony_ci    inline void AddInt32Property(napi_env env, napi_value object, const char *name, int32_t value)
354e56987cSopenharmony_ci    {
364e56987cSopenharmony_ci        napi_value property = nullptr;
374e56987cSopenharmony_ci        NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, value, &property));
384e56987cSopenharmony_ci        NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, name, property));
394e56987cSopenharmony_ci    }
404e56987cSopenharmony_ci
414e56987cSopenharmony_ci    static void AddCMErrorCodePart(napi_env env, napi_value errorCode)
424e56987cSopenharmony_ci    {
434e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_NO_PERMISSION", HAS_NO_PERMISSION);
444e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_NOT_SYSTEM_APP", NOT_SYSTEM_APP);
454e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_INVALID_PARAMS", PARAM_ERROR);
464e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_GENERIC", INNER_FAILURE);
474e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_NO_FOUND", NOT_FOUND);
484e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_INCORRECT_FORMAT", INVALID_CERT_FORMAT);
494e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_MAX_CERT_COUNT_REACHED", MAX_CERT_COUNT_REACHED);
504e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_NO_AUTHORIZATION", NO_AUTHORIZATION);
514e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_ALIAS_LENGTH_REACHED_LIMIT", ALIAS_LENGTH_REACHED_LIMIT);
524e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_DEVICE_ENTER_ADVSECMODE", DEVICE_ENTER_ADVSECMODE);
534e56987cSopenharmony_ci        AddInt32Property(env, errorCode, "CM_ERROR_PASSWORD_IS_ERR", PASSWORD_IS_ERROR);
544e56987cSopenharmony_ci    }
554e56987cSopenharmony_ci
564e56987cSopenharmony_ci    static napi_value CreateCMErrorCode(napi_env env)
574e56987cSopenharmony_ci    {
584e56987cSopenharmony_ci        napi_value errorCode = nullptr;
594e56987cSopenharmony_ci        NAPI_CALL(env, napi_create_object(env, &errorCode));
604e56987cSopenharmony_ci
614e56987cSopenharmony_ci        AddCMErrorCodePart(env, errorCode);
624e56987cSopenharmony_ci
634e56987cSopenharmony_ci        return errorCode;
644e56987cSopenharmony_ci    }
654e56987cSopenharmony_ci
664e56987cSopenharmony_ci    static napi_value CreateCMKeyPurpose(napi_env env)
674e56987cSopenharmony_ci    {
684e56987cSopenharmony_ci        napi_value keyPurpose = nullptr;
694e56987cSopenharmony_ci        NAPI_CALL(env, napi_create_object(env, &keyPurpose));
704e56987cSopenharmony_ci
714e56987cSopenharmony_ci        AddInt32Property(env, keyPurpose, "CM_KEY_PURPOSE_SIGN", CM_KEY_PURPOSE_SIGN);
724e56987cSopenharmony_ci        AddInt32Property(env, keyPurpose, "CM_KEY_PURPOSE_VERIFY", CM_KEY_PURPOSE_VERIFY);
734e56987cSopenharmony_ci
744e56987cSopenharmony_ci        return keyPurpose;
754e56987cSopenharmony_ci    }
764e56987cSopenharmony_ci
774e56987cSopenharmony_ci    static napi_value CreateCMKeyDigest(napi_env env)
784e56987cSopenharmony_ci    {
794e56987cSopenharmony_ci        napi_value keyDigest = nullptr;
804e56987cSopenharmony_ci        NAPI_CALL(env, napi_create_object(env, &keyDigest));
814e56987cSopenharmony_ci
824e56987cSopenharmony_ci        AddInt32Property(env, keyDigest, "CM_DIGEST_NONE", CM_JS_DIGEST_NONE);
834e56987cSopenharmony_ci        AddInt32Property(env, keyDigest, "CM_DIGEST_MD5", CM_JS_DIGEST_MD5);
844e56987cSopenharmony_ci        AddInt32Property(env, keyDigest, "CM_DIGEST_SHA1", CM_JS_DIGEST_SHA1);
854e56987cSopenharmony_ci        AddInt32Property(env, keyDigest, "CM_DIGEST_SHA224", CM_JS_DIGEST_SHA224);
864e56987cSopenharmony_ci        AddInt32Property(env, keyDigest, "CM_DIGEST_SHA256", CM_JS_DIGEST_SHA256);
874e56987cSopenharmony_ci        AddInt32Property(env, keyDigest, "CM_DIGEST_SHA384", CM_JS_DIGEST_SHA384);
884e56987cSopenharmony_ci        AddInt32Property(env, keyDigest, "CM_DIGEST_SHA512", CM_JS_DIGEST_SHA512);
894e56987cSopenharmony_ci        return keyDigest;
904e56987cSopenharmony_ci    }
914e56987cSopenharmony_ci
924e56987cSopenharmony_ci    static napi_value CreateCMKeyPadding(napi_env env)
934e56987cSopenharmony_ci    {
944e56987cSopenharmony_ci        napi_value keyPadding = nullptr;
954e56987cSopenharmony_ci        NAPI_CALL(env, napi_create_object(env, &keyPadding));
964e56987cSopenharmony_ci
974e56987cSopenharmony_ci        AddInt32Property(env, keyPadding, "CM_PADDING_NONE", CM_JS_PADDING_NONE);
984e56987cSopenharmony_ci        AddInt32Property(env, keyPadding, "CM_PADDING_PSS", CM_JS_PADDING_PSS);
994e56987cSopenharmony_ci        AddInt32Property(env, keyPadding, "CM_PADDING_PKCS1_V1_5", CM_JS_PADDING_PKCS1_V1_5);
1004e56987cSopenharmony_ci        return keyPadding;
1014e56987cSopenharmony_ci    }
1024e56987cSopenharmony_ci}  // namespace CertManagerNapi
1034e56987cSopenharmony_ci
1044e56987cSopenharmony_ciusing namespace CMNapi;
1054e56987cSopenharmony_ci
1064e56987cSopenharmony_ciextern "C" {
1074e56987cSopenharmony_ci    static napi_value CMNapiRegister(napi_env env, napi_value exports)
1084e56987cSopenharmony_ci    {
1094e56987cSopenharmony_ci        napi_property_descriptor desc[] = {
1104e56987cSopenharmony_ci            DECLARE_NAPI_PROPERTY("CMErrorCode", CreateCMErrorCode(env)),
1114e56987cSopenharmony_ci            DECLARE_NAPI_PROPERTY("CmKeyPurpose", CreateCMKeyPurpose(env)),
1124e56987cSopenharmony_ci            DECLARE_NAPI_PROPERTY("CmKeyDigest", CreateCMKeyDigest(env)),
1134e56987cSopenharmony_ci            DECLARE_NAPI_PROPERTY("CmKeyPadding", CreateCMKeyPadding(env)),
1144e56987cSopenharmony_ci
1154e56987cSopenharmony_ci            /* system ca */
1164e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getSystemTrustedCertificateList", CMNapiGetSystemCertList),
1174e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getSystemTrustedCertificate", CMNapiGetSystemCertInfo),
1184e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("setCertificateStatus", CMNapiSetCertStatus),
1194e56987cSopenharmony_ci
1204e56987cSopenharmony_ci            /* user public cred */
1214e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("installPublicCertificate", CMNapiInstallPublicCert),
1224e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("uninstallAllAppCertificate", CMNapiUninstallAllAppCert),
1234e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("uninstallPublicCertificate", CMNapiUninstallPublicCert),
1244e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getAllPublicCertificates", CMNapiGetAllPublicCertList),
1254e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getPublicCertificate", CMNapiGetPublicCertInfo),
1264e56987cSopenharmony_ci
1274e56987cSopenharmony_ci            /* user ca */
1284e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("installUserTrustedCertificate", CMNapiInstallUserTrustedCert),
1294e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("uninstallAllUserTrustedCertificate", CMNapiUninstallAllUserTrustedCert),
1304e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("uninstallUserTrustedCertificate", CMNapiUninstallUserTrustedCert),
1314e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getAllUserTrustedCertificates", CMNapiGetAllUserTrustedCertList),
1324e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getUserTrustedCertificate", CMNapiGetUserTrustedCertInfo),
1334e56987cSopenharmony_ci
1344e56987cSopenharmony_ci            /* private cred */
1354e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("installPrivateCertificate", CMNapiInstallPrivateAppCert),
1364e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("uninstallPrivateCertificate", CMNapiUninstallPrivateAppCert),
1374e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getAllAppPrivateCertificates", CMNapiGetPrivateAppCertList),
1384e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getPrivateCertificate", CMNapiGetPrivateAppCertInfo),
1394e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getPrivateCertificates", CMNapiGetCallingPrivateAppCertList),
1404e56987cSopenharmony_ci
1414e56987cSopenharmony_ci            /* grant, sign and verify */
1424e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("grantPublicCertificate", CMNapiGrantPublicCertificate),
1434e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("isAuthorizedApp", CMNapiIsAuthorizedApp),
1444e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getAuthorizedAppList", CMNapiGetAuthorizedAppList),
1454e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("removeGrantedPublicCertificate", CMNapiRemoveGrantedPublic),
1464e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("init", CMNapiInit),
1474e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("update", CMNapiUpdate),
1484e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("finish", CMNapiFinish),
1494e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("abort", CMNapiAbort),
1504e56987cSopenharmony_ci
1514e56987cSopenharmony_ci            /* system cred */
1524e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("installSystemAppCertificate", CMNapiInstallSystemAppCert),
1534e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("uninstallSystemAppCertificate", CMNapiUninstallSystemAppCert),
1544e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getAllSystemAppCertificates", CMNapiGetSystemAppCertList),
1554e56987cSopenharmony_ci            DECLARE_NAPI_FUNCTION("getSystemAppCertificate", CMNapiGetSystemAppCertInfo),
1564e56987cSopenharmony_ci        };
1574e56987cSopenharmony_ci        NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1584e56987cSopenharmony_ci        return exports;
1594e56987cSopenharmony_ci    }
1604e56987cSopenharmony_ci
1614e56987cSopenharmony_ci    static napi_module g_module = {
1624e56987cSopenharmony_ci        .nm_version = 1,
1634e56987cSopenharmony_ci        .nm_flags = 0,
1644e56987cSopenharmony_ci        .nm_filename = nullptr,
1654e56987cSopenharmony_ci        .nm_register_func = CMNapiRegister,
1664e56987cSopenharmony_ci        .nm_modname = "security.certmanager",
1674e56987cSopenharmony_ci        .nm_priv =  nullptr,
1684e56987cSopenharmony_ci        .reserved = { nullptr },
1694e56987cSopenharmony_ci    };
1704e56987cSopenharmony_ci
1714e56987cSopenharmony_ci    __attribute__((constructor)) void CertManagerRegister(void)
1724e56987cSopenharmony_ci    {
1734e56987cSopenharmony_ci        napi_module_register(&g_module);
1744e56987cSopenharmony_ci    }
1754e56987cSopenharmony_ci}
176