From fb4f4c00cbd6a507ea5055c5da822dc01a1133b1 Mon Sep 17 00:00:00 2001 From: huweishuang Date: Sat, 8 Oct 2022 17:01:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=AF=81=E4=B9=A6=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=A4=A7=E6=95=B0=E6=8D=AE=E6=89=93=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huweishuang --- BUILD.gn | 5 +- bundle.json | 9 +- .../main/common/include/cm_x509.h | 2 - .../main/common/src/cm_x509.c | 8 - .../os_dependency/cm_ipc/src/cm_ipc_client.c | 2 +- hisysevent.yaml | 23 ++ .../main/core/include/cert_manager_status.h | 1 - .../main/core/src/cert_manager.c | 122 +------ .../main/core/src/cert_manager_file.c | 7 +- .../main/core/src/cert_manager_status.c | 19 -- .../main/core/src/cert_manager_uri.c | 2 +- .../main/hisysevent_wrapper/BUILD.gn | 42 +++ .../include/cm_report_wrapper.h | 34 ++ .../include/hisysevent_wrapper.h | 38 +++ .../src/cm_report_wrapper.c | 42 +++ .../src/hisysevent_wrapper.cpp | 48 +++ .../main/os_dependency/idl/BUILD.gn | 6 +- .../os_dependency/idl/cm_ipc/cm_ipc_service.c | 28 +- .../os_dependency/idl/cm_ipc/cm_ipc_service.h | 1 + .../os_dependency/idl/cm_ipc/cm_response.cpp | 22 -- .../os_dependency/idl/cm_ipc/cm_response.h | 2 - test/hisysevent_test/BUILD.gn | 46 +++ .../include/cm_hisysevent_test_common.h | 46 +++ .../src/cm_hisysevent_test.cpp | 306 ++++++++++++++++++ .../src/cm_hisysevent_test_common.cpp | 182 +++++++++++ test/unittest/src/cm_test_common.cpp | 2 + 26 files changed, 858 insertions(+), 187 deletions(-) create mode 100644 hisysevent.yaml create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/BUILD.gn create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/cm_report_wrapper.h create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/hisysevent_wrapper.h create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/cm_report_wrapper.c create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/hisysevent_wrapper.cpp create mode 100644 test/hisysevent_test/BUILD.gn create mode 100644 test/hisysevent_test/include/cm_hisysevent_test_common.h create mode 100644 test/hisysevent_test/src/cm_hisysevent_test.cpp create mode 100644 test/hisysevent_test/src/cm_hisysevent_test_common.cpp diff --git a/BUILD.gn b/BUILD.gn index bf16666..7ebe8b6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -14,7 +14,10 @@ group("cert_manager_sdk_test") { testonly = true if (os_level == "standard") { - deps = [ "//base/security/certificate_manager/test:unittest" ] + deps = [ + "//base/security/certificate_manager/test:unittest", + "//base/security/certificate_manager/test/hisysevent_test:cert_hisysevent_test" + ] } else { } } diff --git a/bundle.json b/bundle.json index 2f4702d..0ed5484 100644 --- a/bundle.json +++ b/bundle.json @@ -23,6 +23,9 @@ "small", "mini" ], + "hisysevent_config": [ + "//base/security/huks/hisysevent.yaml" + ], "rom": "5000KB", "ram": "500kB", "deps": { @@ -31,7 +34,8 @@ "ipc_core", "system_ability_fwk", "samgr_proxy", - "c_utils" + "c_utils", + "hisysevent_native" ], "third_party": [ "openssl", @@ -62,7 +66,8 @@ } ], "test": [ - "//base/security/certificate_manager/test:unittest" + "//base/security/certificate_manager/test:unittest", + "//base/security/certificate_manager/test/hisysevent_test:cert_hisysevent_test" ] } } diff --git a/frameworks/cert_manager_standard/main/common/include/cm_x509.h b/frameworks/cert_manager_standard/main/common/include/cm_x509.h index 7c34ec3..6d1fb73 100644 --- a/frameworks/cert_manager_standard/main/common/include/cm_x509.h +++ b/frameworks/cert_manager_standard/main/common/include/cm_x509.h @@ -58,8 +58,6 @@ struct DataTime { X509 *InitCertContext(const uint8_t *certBuf, uint32_t size); -int32_t GetX509Version(X509 *x509cert); - int32_t GetX509SerialNumber(X509 *x509cert, char *outBuf, uint32_t outBufMaxSize); int32_t GetX509SubjectName(const X509 *x509cert, const char *subjectObjName, char* outBuf, uint32_t outBufMaxSize); diff --git a/frameworks/cert_manager_standard/main/common/src/cm_x509.c b/frameworks/cert_manager_standard/main/common/src/cm_x509.c index 9d9774c..eef0813 100644 --- a/frameworks/cert_manager_standard/main/common/src/cm_x509.c +++ b/frameworks/cert_manager_standard/main/common/src/cm_x509.c @@ -50,14 +50,6 @@ X509 *InitCertContext(const uint8_t *certBuf, uint32_t size) return x509; } -int32_t GetX509Version(X509 *x509cert) -{ - if (x509cert == NULL) { - return CMR_ERROR_INVALID_ARGUMENT; - } - return (int32_t)X509_get_version(x509cert) + 1; -} - int32_t GetX509SerialNumber(X509 *x509cert, char *outBuf, uint32_t outBufMaxSize) { if (outBuf == NULL || x509cert == NULL) { diff --git a/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/src/cm_ipc_client.c b/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/src/cm_ipc_client.c index 2c3bae2..52e2060 100644 --- a/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/src/cm_ipc_client.c +++ b/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/src/cm_ipc_client.c @@ -600,7 +600,7 @@ static int32_t CmAppCertInfoUnpackFromService(const struct CmBlob *outData, stru int32_t ret = GetUint32FromBuffer(&certificateInfo->isExist, outData, &offset); if (ret != CM_SUCCESS || certificateInfo->isExist == 0) { - CM_LOG_E("Get certificateInfo->isExist failed ret:%s, is exist:%d", ret, certificateInfo->isExist); + CM_LOG_E("Get certificateInfo->isExist failed ret:%d, is exist:%d", ret, certificateInfo->isExist); return ret; } diff --git a/hisysevent.yaml b/hisysevent.yaml new file mode 100644 index 0000000..257b698 --- /dev/null +++ b/hisysevent.yaml @@ -0,0 +1,23 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +domain: CERT_MANAGER + +CERT_FAULT: + __BASE: {type: FAULT, level: CRITICAL, tag: cert, desc: fault event for certmanager} + FUNCTION: {type: STRING, desc: function name} + USER_ID: {type: INT32, desc: user id} + UID: {type: INT32, desc: uid} + CERT_NAME: {type: STRING, desc: cert name} + ERROR_CODE: {type: INT32, desc: error code} \ No newline at end of file diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/include/cert_manager_status.h b/services/cert_manager_standard/cert_manager_engine/main/core/include/cert_manager_status.h index 4ad7135..3483359 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/include/cert_manager_status.h +++ b/services/cert_manager_standard/cert_manager_engine/main/core/include/cert_manager_status.h @@ -91,7 +91,6 @@ int32_t CertManagerGetCertificatesStatus( uint32_t *status); int32_t CertManagerStatusInit(void); -int32_t CertManagerStatusDestroy(void); int32_t SetcertStatus(const struct CmContext *context, const struct CmBlob *certUri, uint32_t store, uint32_t status, uint32_t *stp); diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager.c index 4aaedb7..fcd2815 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager.c @@ -238,7 +238,6 @@ void CmCertificateListFree(struct CmMutableBlob *certListData, uint32_t certList } CMFree(certListData); - certListData = NULL; } static int32_t CmCreateCertificateList(struct CmBlob *certList, @@ -249,10 +248,13 @@ static int32_t CmCreateCertificateList(struct CmBlob *certList, uint32_t certBuffSize = 0; int32_t ret; struct CmMutableBlob *certDataList = NULL; + if ((certList == NULL) || (fileNames == NULL) || (path == NULL)) { + CM_LOG_E("Bad parameters: param is null"); + return CMR_ERROR_INVALID_ARGUMENT; + } - if ((certList == NULL) || (fileNames == NULL) || (path == NULL) || - (fileNames->data == NULL) || (fileNames->size > MAX_FILES_IN_DIR)) { - CM_LOG_E("Bad parameters: path = %s, ileNames->size = %u", path, fileNames->size); + if ((fileNames->data == NULL) || (fileNames->size > MAX_FILES_IN_DIR)) { + CM_LOG_E("Bad parameters: path = %s, fileNames->size = %u", path, fileNames->size); return CMR_ERROR_INVALID_ARGUMENT; } @@ -698,117 +700,6 @@ cleanup: return retVal; } -static int32_t CmGetMatchedFileSubjectNameIndex(const struct CmMutableBlob *nameDigest, - const struct CmMutableBlob *fileNames, const struct CmAsn1Obj *subjectName, struct CmMathedIndexPara indexPara) -{ - uint32_t i; - struct CmAsn1Obj subjectFromList; - uint8_t certBuff[CERT_MANAGER_MAX_CERT_SIZE]; - struct CmBlob certificate = {sizeof(certBuff), certBuff}; - char *path = indexPara.path; - uint8_t *indexes = indexPara.indexes; - uint32_t *count = indexPara.count; - struct CmMutableBlob *fname = (struct CmMutableBlob *)fileNames->data; - - (void)memset_s(&subjectFromList, sizeof(struct CmAsn1Obj), 0, sizeof(struct CmAsn1Obj)); - for (i = 0; i < fileNames->size; i++) { - if (memcmp(fname[i].data, nameDigest->data, nameDigest->size) == 0) { - if (CmFileRead(path, (char *)fname[i].data, 0, certBuff, sizeof(certBuff)) == 0) { - CM_LOG_E("Failed to read file: %s/%s", path, fname[i].data); - return CMR_ERROR_READ_FILE_ERROR; - } - - if (CmGetSubjectNameAsn1(&certificate, &subjectFromList) != CMR_OK) { - CM_LOG_E("Failed to obtain subjectName"); - return CMR_ERROR_NOT_FOUND; - } - if ((subjectName->value.size == subjectFromList.value.size) || - (memcmp(subjectName->value.data, subjectFromList.value.data, subjectName->value.size))) { - indexes[*count] = i; - count++; - } - } - } - return CMR_OK; -} - -static int32_t CmListCertificatesBySubjectNameAsn1(const struct CmContext *context, - struct CmBlob *certificateList, uint32_t store, const struct CmAsn1Obj *subjectName) -{ - int32_t retVal = 0; - uint32_t count = 0; - uint8_t *indexes = NULL; - uint32_t *status = NULL; - struct CmMutableBlob fileNames = {0, NULL}, matchingFiles = {0, NULL}; - char path[CERT_MAX_PATH_LEN]; - uint8_t buff[MAX_NAME_DIGEST_LEN]; - struct CmMutableBlob nameDigest = {sizeof(buff), buff}, pathBlob = {sizeof(path), (uint8_t *)path}; - - retVal = NameHashFromAsn1(subjectName, &nameDigest); - if (retVal != CMR_OK) { - return retVal; - } - - if (CmGetFilenames(context, &pathBlob, store, &fileNames, path) != CMR_OK) { - return CMR_ERROR_STORAGE; - } - - if (CmInitFileNameIndexArray(&indexes, fileNames) != CMR_OK) { - retVal = CMR_ERROR_MALLOC_FAIL; - goto cleanup; - } - - struct CmMathedIndexPara indexPara = {path, store, status, &count, indexes}; - if (CmGetMatchedFileSubjectNameIndex(&nameDigest, &fileNames, subjectName, indexPara) != CMR_OK) { - CM_LOG_E("Failed to get matched file name indexes"); - retVal = CMR_ERROR; - goto cleanup; - } - - matchingFiles.size = count; - matchingFiles.data = malloc(sizeof(struct CmMutableBlob) * count); - if (matchingFiles.data == NULL) { - CM_LOG_E("Failed to allocate memory for files"); - retVal = CMR_ERROR_MALLOC_FAIL; - goto cleanup; - } - - struct CmMutableBlob *fname = (struct CmMutableBlob *)fileNames.data; - struct CmMutableBlob *matchName = (struct CmMutableBlob *)matchingFiles.data; - - if (CmGetMatchedFileNames(matchName, fname, count, indexes) != CMR_OK) { - CM_LOG_E("Failed to get matched file Name indexes"); - retVal = CMR_ERROR; - goto cleanup; - } - - if (CmCreateCertificateList(certificateList, &matchingFiles, path) < 0) { - CM_LOG_E("Failed to create certificates: %s", path); - retVal = CMR_ERROR_STORAGE; - goto cleanup; - } - -cleanup: - CmFreeCertificatesInfo(&fileNames, &matchingFiles, indexes, certificateList, retVal); - return retVal; -} - -int32_t CertManagerListCertificatesBySubjectName(const struct CmContext *context, - struct CmBlob *certificateList, uint32_t store, const struct CmBlob *subjectName) -{ - struct CmAsn1Obj subjectAsn1; - struct CmBlob skip = {0, NULL}; - errno_t ret; - - (void)memset_s(&subjectAsn1, sizeof(struct CmAsn1Obj), 0, sizeof(struct CmAsn1Obj)); - ret = CmAsn1ExtractTag(&skip, &subjectAsn1, &CM_BLOB(subjectName), ASN_1_TAG_TYPE_SEQ); - if (ret != CMR_OK) { - CM_LOG_E("Subject name in bad format"); - return CMR_ERROR_NOT_FOUND; - } - return CmListCertificatesBySubjectNameAsn1(context, certificateList, store, &subjectAsn1); -} - /* This function constructes md5 hash part of filename for storing certificate. * All cetificates are stored in files namePrefix.count. where namePrefix = md5(subjectName) * and count is = 0, 1.... needed fpr potential hash collisions. @@ -1048,6 +939,7 @@ void CmFreeFileNames(struct CmBlob *fileNames, const uint32_t fileSize) { if (fileNames == NULL) { CM_LOG_E("CmFreeFileNames fileNames is null"); + return; } for (uint32_t i = 0; i < fileSize; i++) { diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file.c index 182947d..321d93e 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file.c @@ -48,7 +48,7 @@ inline int32_t CertManagerFileRemove(const char *path, const char *fileName) return CM_ERROR(CmFileRemove(path, fileName)); } -static uint32_t GetNumberOfFiles(const char *path) +static int32_t GetNumberOfFiles(const char *path) { void *dir = CmOpenDir(path); if (dir == NULL) { @@ -56,7 +56,7 @@ static uint32_t GetNumberOfFiles(const char *path) return -1; } - uint32_t count = 0; + int32_t count = 0; struct CmFileDirentInfo dire = {{0}}; while (CmGetDirFile(dir, &dire) == CMR_OK) { count++; @@ -69,7 +69,7 @@ static int32_t MallocFileNames(struct CmMutableBlob *fileNames, const char *path uint32_t *fileCount) { struct CmMutableBlob *tmp = NULL; - uint32_t fileNums = GetNumberOfFiles(path); + int32_t fileNums = GetNumberOfFiles(path); if (fileNums < 0) { CM_LOG_E("Failed to obtain number of files from: path = %s", path); return -1; @@ -108,7 +108,6 @@ static void FreeFileNames(struct CmMutableBlob *fNames, uint32_t endIndex) } } CMFree(fNames); - fNames = NULL; } int32_t CertManagerGetFilenames(struct CmMutableBlob *fileNames, const char *path, struct CmBlob *uri) diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_status.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_status.c index b2d7e71..7066ad4 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_status.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_status.c @@ -93,15 +93,6 @@ static void FreeStatus(struct CertStatus *cs) } } -static void FreeTreeNodeValue(RbTreeKey key, RbTreeValue v, const void *context) -{ - (void) context; - (void) key; - if (v != NULL) { - FreeStatus((struct CertStatus *) v); - } -} - static int GetStoreIndex(uint32_t store) { switch (store) { @@ -440,16 +431,6 @@ finally: return rc; } -int32_t CertManagerStatusDestroy(void) -{ - pthread_rwlock_wrlock(&g_statusLock); - for (uint32_t i = 0; i < g_treeCount; i++) { - (void) RbTreeDestroyEx(&g_trees[i], FreeTreeNodeValue, NULL); - } - pthread_rwlock_unlock(&g_statusLock); - return CMR_OK; -} - inline static RbTreeKey GetRbTreeKeyFromName(char *name) { /* use the first 4 bytes of file name (exluding the first bit) as the key */ diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_uri.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_uri.c index 152e08b..43f1a00 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_uri.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_uri.c @@ -275,7 +275,7 @@ static inline uint32_t IndexOf(char sep, const char *data, uint32_t start, uint3 static char *DecodeValue(const char *s, uint32_t off, uint32_t len) { - if (s == NULL || len <= 0) { + if (s == NULL || len == 0) { return NULL; } char *buf = MALLOC(len + 1); diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/BUILD.gn b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/BUILD.gn new file mode 100644 index 0000000..a99940d --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/BUILD.gn @@ -0,0 +1,42 @@ +# Copyright (C) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +config("cert_manager_config") { + include_dirs = [ "include" ] +} + +ohos_static_library("libcert_manager_hisysevent_wrapper_static") { + subsystem_name = "security" + part_name = "certificate_manager" + public_configs = [ ":cert_manager_config" ] + include_dirs = [ + "//base/security/certificate_manager/frameworks/cert_manager_standard/main/common/include", + "//base/security/certificate_manager/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/include", + ] + sources = [ + "src/hisysevent_wrapper.cpp", + "src/cm_report_wrapper.c", + ] + cflags = [ + "-Wall", + "-Werror", + ] + defines = [ + "L2_STANDARD", + "_CM_LOG_ENABLE_", +] + external_deps = [ "hisysevent_native:libhisysevent" ] + complete_static_lib = true +} diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/cm_report_wrapper.h b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/cm_report_wrapper.h new file mode 100644 index 0000000..9502352 --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/cm_report_wrapper.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CM_REPORT_WRAPPER_H +#define CM_REPORT_WRAPPER_H + +#include +#include "hisysevent_wrapper.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t ReportFaultEvent(const char *funcName, const struct CmContext *cmContext, const char *certName, int32_t errorCode); + +void CmReport(const char *funcName, const struct CmContext *cmContext, const char *certName, int32_t errorCode); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/hisysevent_wrapper.h b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/hisysevent_wrapper.h new file mode 100644 index 0000000..605189c --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/hisysevent_wrapper.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CM_CLIENT_SERVICE_ADAPTER_H +#define CM_CLIENT_SERVICE_ADAPTER_H + +#include "cm_type_inner.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct EventValues { + uint32_t userId; + uint32_t uid; + const char *certName; + int32_t errorCode; +}; + +int WriteEvent(const char *functionName, const struct EventValues *eventValues); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/cm_report_wrapper.c b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/cm_report_wrapper.c new file mode 100644 index 0000000..803082d --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/cm_report_wrapper.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type_inner.h" +#include "cm_report_wrapper.h" + +int32_t ReportFaultEvent(const char *funcName, const struct CmContext *cmContext, const char *certName, int32_t errorCode) +{ + if (errorCode == CM_SUCCESS) { + return CM_SUCCESS; + } + int32_t ret; + + struct EventValues eventValues = { cmContext->userId, cmContext->uid, certName, errorCode }; + ret = WriteEvent(funcName, &eventValues); + if (ret != CM_SUCCESS) { + CM_LOG_E("ReportFaultEvent failed, ret = %d", ret); + } + return ret; +} + +void CmReport(const char *funcName, const struct CmContext *cmContext, const char *certName, int32_t errorCode) +{ + int32_t ret = ReportFaultEvent(funcName, cmContext, certName, errorCode); + if (ret != CM_SUCCESS) { + CM_LOG_E("report fault event failed, ret = %d", ret); + } +} \ No newline at end of file diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/hisysevent_wrapper.cpp b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/hisysevent_wrapper.cpp new file mode 100644 index 0000000..5cf6fca --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/hisysevent_wrapper.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "hisysevent_wrapper.h" +#include "hisysevent.h" + +#include "cm_log.h" + +using namespace OHOS::HiviewDFX; + +static constexpr const char domain[] = "CERT_MANAGER"; +static constexpr const char g_eventName[] = "CERT_FAULT"; +static constexpr const char g_tagFunction[] = "FUNCTION"; +static constexpr const char g_tagUserId[] = "USER_ID"; +static constexpr const char g_tagUID[] = "UID"; +static constexpr const char g_tagCertName[] = "CERT_NAME"; +static constexpr const char g_tagErrorCode[] = "ERROR_CODE"; + +int WriteEvent(const char *functionName, const struct EventValues *eventValues) +{ + int32_t ret = HiSysEventWrite(domain, g_eventName, HiSysEvent::EventType::FAULT, + g_tagFunction, functionName, + g_tagUserId, eventValues->userId, + g_tagUID, eventValues->uid, + g_tagCertName, eventValues->certName, + g_tagErrorCode, eventValues->errorCode); + CM_LOG_I("g_tagFunction:%s, g_tagUserId:%u, g_tagUID:%u, g_tagCertName:%s, g_tagErrorCode:%d", + functionName, eventValues->userId, eventValues->uid, eventValues->certName, eventValues->errorCode); + + if (ret != CM_SUCCESS) { + CM_LOG_E("WriteEvent failed!"); + return ret; + } + return ret; +} \ No newline at end of file diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/BUILD.gn b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/BUILD.gn index 2a1f4ca..ad9c54f 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/BUILD.gn +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/BUILD.gn @@ -29,7 +29,8 @@ ohos_static_library("libcm_service_idl_standard_static") { "//commonlibrary/c_utils/base/include", "//base/security/huks/interfaces/innerkits/huks_standard/main/include", "//base/security/certificate_manager/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/include", - "//third_party/openssl/include" + "//third_party/openssl/include", + "//base/security/certificate_manager/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include" ] defines = [ "L2_STANDARD", @@ -44,7 +45,8 @@ ohos_static_library("libcm_service_idl_standard_static") { "//base/security/certificate_manager/frameworks/cert_manager_standard/main/os_dependency:libcert_manager_os_dependency_standard_static", "//base/security/certificate_manager/frameworks/cert_manager_standard/main/common:libcert_manager_common_standard_static", "//base/security/certificate_manager/services/cert_manager_standard/cert_manager_engine/main/core:cert_manager_engine_core_standard", - "//third_party/openssl:libcrypto_shared" + "//third_party/openssl:libcrypto_shared", + "//base/security/certificate_manager/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper:libcert_manager_hisysevent_wrapper_static" ] external_deps = [ "ipc:ipc_core", diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c index 48e5538..d6912a0 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c @@ -45,6 +45,7 @@ #include "cert_manager_query.h" #include "cert_manager_permission_check.h" +#include "cm_report_wrapper.h" #define MAX_PACKAGENAME_LEN 32 #define MAX_LEN_CERTIFICATE 8196 @@ -522,6 +523,8 @@ static int32_t CmInstallAppCert(const struct CmContext *context, const struct Cm } } while (0); + CmReport(__func__, context, (char *)certAlias->data, ret); + EVP_PKEY_free(priKey); return ret; } @@ -1216,10 +1219,9 @@ void CmIpcServiceGrantAppCertificate(const struct CmBlob *paramSetBlob, struct C { struct CmContext cmContext = { 0, 0, {0} }; struct CmParamSet *paramSet = NULL; - + struct CmBlob keyUri = { 0, NULL }; int32_t ret; do { - struct CmBlob keyUri = { 0, NULL }; uint32_t appUid = 0; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &keyUri }, @@ -1238,6 +1240,8 @@ void CmIpcServiceGrantAppCertificate(const struct CmBlob *paramSetBlob, struct C } } while (0); + CmReport(__func__, context, (char *)keyUri.data, ret); + CM_LOG_I("CmIpcServiceGrantAppCertificate end:%d", ret); CmSendResponse(context, ret, outData); CmFreeParamSet(¶mSet); @@ -1248,10 +1252,10 @@ void CmIpcServiceGetAuthorizedAppList(const struct CmBlob *paramSetBlob, struct { struct CmContext cmContext = { 0, 0, {0} }; struct CmParamSet *paramSet = NULL; + struct CmBlob keyUri = { 0, NULL }; int32_t ret; do { - struct CmBlob keyUri = { 0, NULL }; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &keyUri }, }; @@ -1267,6 +1271,7 @@ void CmIpcServiceGetAuthorizedAppList(const struct CmBlob *paramSetBlob, struct break; } } while (0); + CmReport(__func__, context, (char *)keyUri.data, ret); CM_LOG_I("CmIpcServiceGetAuthorizedAppList end:%d", ret); CmSendResponse(context, ret, outData); @@ -1279,10 +1284,10 @@ void CmIpcServiceIsAuthorizedApp(const struct CmBlob *paramSetBlob, struct CmBlo (void)outData; struct CmContext cmContext = { 0, 0, {0} }; struct CmParamSet *paramSet = NULL; + struct CmBlob authUri = { 0, NULL }; int32_t ret; do { - struct CmBlob authUri = { 0, NULL }; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &authUri }, }; @@ -1299,6 +1304,7 @@ void CmIpcServiceIsAuthorizedApp(const struct CmBlob *paramSetBlob, struct CmBlo } } while (0); + CmReport(__func__, context, (char *)authUri.data, ret); CM_LOG_I("CmIpcServiceIsAuthorizedApp end:%d", ret); CmSendResponse(context, ret, NULL); CmFreeParamSet(¶mSet); @@ -1310,11 +1316,11 @@ void CmIpcServiceRemoveGrantedApp(const struct CmBlob *paramSetBlob, struct CmBl struct CmContext cmContext = { 0, 0, {0} }; struct CmParamSet *paramSet = NULL; (void)outData; + struct CmBlob keyUri = { 0, NULL }; int32_t ret; do { uint32_t appUid = 0; - struct CmBlob keyUri = { 0, NULL }; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &keyUri }, { .tag = CM_TAG_PARAM1_UINT32, .uint32Param = &appUid }, @@ -1331,6 +1337,7 @@ void CmIpcServiceRemoveGrantedApp(const struct CmBlob *paramSetBlob, struct CmBl break; } } while (0); + CmReport(__func__, context, (char *)keyUri.data, ret); CM_LOG_I("CmIpcServiceRemoveGrantedApp end:%d", ret); CmSendResponse(context, ret, NULL); @@ -1483,7 +1490,6 @@ static int32_t CmCheckCallerPermission(const struct CmContext *ipcInfo) void CmIpcServiceGetUserCertList(const struct CmBlob *paramSetBlob, struct CmBlob *outData, const struct CmContext *context) { - CM_LOG_I("enter CmIpcServiceGetUserCertList"); int32_t ret = CM_SUCCESS; uint32_t store; struct CmContext cmContext = {0}; @@ -1520,6 +1526,9 @@ void CmIpcServiceGetUserCertList(const struct CmBlob *paramSetBlob, struct CmBlo CmSendResponse(context, ret, outData); } while (0); + + CmReport(__func__, &cmContext, "certName", ret); + if (ret != CM_SUCCESS) { CmSendResponse(context, ret, NULL); } @@ -1570,6 +1579,7 @@ void CmIpcServiceGetUserCertInfo(const struct CmBlob *paramSetBlob, struct CmBlo } CmSendResponse(context, ret, outData); } while (0); + CmReport(__func__, &cmContext, (char *)certUri.data, ret); if (ret != CM_SUCCESS) { CmSendResponse(context, ret, NULL); } @@ -1622,7 +1632,7 @@ void CmIpcServiceInstallUserCert(const struct CmBlob *paramSetBlob, struct CmBlo struct CmBlob userCert = { 0, NULL }; struct CmBlob certAlias = { 0, NULL }; struct CmContext cmContext = {0}; - struct CmParamSet *paramSet = NULL; + struct CmParamSet *paramSet = NULL; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &userCert }, { .tag = CM_TAG_PARAM1_BUFFER, .blob = &certAlias }, @@ -1648,6 +1658,9 @@ void CmIpcServiceInstallUserCert(const struct CmBlob *paramSetBlob, struct CmBlo CmSendResponse(context, ret, outData); } while (0); + + CmReport(__func__, &cmContext, "NULL", ret); + if (ret != CM_SUCCESS) { CmSendResponse(context, ret, NULL); } @@ -1685,6 +1698,7 @@ void CmIpcServiceUninstallUserCert(const struct CmBlob *paramSetBlob, struct CmB break; } } while (0); + CmReport(__func__, &cmContext, (char *)certUri.data, ret); CmSendResponse(context, ret, NULL); CmFreeParamSet(¶mSet); } diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.h b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.h index 3242696..60c061a 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.h +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.h @@ -25,6 +25,7 @@ extern "C" { struct CertParam { uint8_t *aliasBuff; uint8_t *passWdBuff; + struct CmContext *cmContext; }; void CmIpcServiceGetCertificateList(const struct CmBlob *srcData, const struct CmContext *context); diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.cpp b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.cpp index e4df11e..e8a56eb 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.cpp +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.cpp @@ -46,28 +46,6 @@ void CmSendResponse(const struct CmContext *context, int32_t result, const struc } } -int32_t CmGetProcessNameForIPC(const struct CmContext *context, struct CmBlob *processName) -{ - if ((context == nullptr) || (processName == nullptr)) { - CM_LOG_D("CmGetProcessNameForIPC don't need get process name in hosp."); - return CM_SUCCESS; - } - - auto callingUid = IPCSkeleton::GetCallingUid(); - uint8_t *name = (uint8_t *)CmMalloc(sizeof(callingUid)); - if (name == nullptr) { - CM_LOG_E("CmGetProcessNameForIPC malloc failed."); - return CMR_ERROR_MALLOC_FAIL; - } - - if (memcpy_s(name, sizeof(callingUid), &callingUid, sizeof(callingUid)) != EOK) { - return CM_FAILURE; - } - processName->size = sizeof(callingUid); - processName->data = name; - return CM_SUCCESS; -} - int32_t CmGetProcessInfoForIPC(struct CmContext *cmContext) { if (cmContext == nullptr) { diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.h b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.h index 1686178..864cf1a 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.h +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.h @@ -24,8 +24,6 @@ extern "C" { void CmSendResponse(const struct CmContext *context, int32_t result, const struct CmBlob *response); -int32_t CmGetProcessNameForIPC(const struct CmContext *context, struct CmBlob *processName); - int32_t CmGetProcessInfoForIPC(struct CmContext *cmContext); #ifdef __cplusplus diff --git a/test/hisysevent_test/BUILD.gn b/test/hisysevent_test/BUILD.gn new file mode 100644 index 0000000..afa87b2 --- /dev/null +++ b/test/hisysevent_test/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (C) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +module_output_path = "cert_manager_standard/cert_manager_standard_test" + +ohos_unittest("cert_hisysevent_test") { + module_out_path = module_output_path + + sources = [ + "src/cm_hisysevent_test.cpp", + "src/cm_hisysevent_test_common.cpp", + ] + + defines = [ + "L2_STANDARD", + "_CM_LOG_ENABLE_", + ] + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "include", + "//base/security/certificate_manager/frameworks/cert_manager_standard/main/common/include", + ] + + deps = [ + "//base/security/certificate_manager/frameworks/cert_manager_standard/main:cert_manager_standard_frameworks", + "//base/security/certificate_manager/interfaces/innerkits/cert_manager_standard/main:cert_manager_sdk", + ] + + external_deps = [ + "hisysevent_native:libhisysevent", + "hisysevent_native:libhisyseventmanager", + ] +} diff --git a/test/hisysevent_test/include/cm_hisysevent_test_common.h b/test/hisysevent_test/include/cm_hisysevent_test_common.h new file mode 100644 index 0000000..77029fb --- /dev/null +++ b/test/hisysevent_test/include/cm_hisysevent_test_common.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CM_HISYSEVENT_TEST_COMMON_H +#define CM_HISYSEVENT_TEST_COMMON_H + +#include +#include +#include "securec.h" +#include "cm_type.h" +#ifdef __cplusplus +extern "C" { +#endif + +#define CM_HISYSEVENT_QUERY_SUCCESS 0 +#define CM_HISYSEVENT_QUERY_FAILED (-1) + +void CmHiSysEventQueryStart(void); + +int32_t CmHiSysEventQueryResult(const std::string funStr); + +void FreeCMBlobData(struct CmBlob *blob); + +uint32_t InitUserCertList(struct CertList **cList); + +uint32_t InitUserCertInfo(struct CertInfo **cInfo); + +void FreeCertList(struct CertList *certList); + +#ifdef __cplusplus +} +#endif + +#endif // CM_HISYSEVENT_TEST_COMMON_H \ No newline at end of file diff --git a/test/hisysevent_test/src/cm_hisysevent_test.cpp b/test/hisysevent_test/src/cm_hisysevent_test.cpp new file mode 100644 index 0000000..0b86a95 --- /dev/null +++ b/test/hisysevent_test/src/cm_hisysevent_test.cpp @@ -0,0 +1,306 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "cm_hisysevent_test_common.h" +#include "cert_manager_api.h" + +using namespace testing::ext; +namespace { +#define MAX_URI_LEN 256 +static constexpr uint32_t DEFAULT_AUTH_URI_LEN = 256; +static constexpr uint32_t DEFAULT_APP_ID = 1000; + +static const uint8_t g_p12AbnormalCertinfo[] = { + 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 0x03, 0x30, 0x82, 0x0b, 0x87, 0x06, 0x09, 0x2a, 0x86, 0x48, + 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82, 0x0b, 0x78, 0x04, 0x82, 0x0b, 0x74, 0x30, 0x82, + 0x0b, 0x70, 0x30, 0x82, 0x06, 0x27, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, + 0x06, 0xa0, 0x82, 0x06, 0x18, 0x30, 0x82, 0x06, 0x14, 0x02, 0x01, 0x00, 0x30, 0x82, 0x06, 0x0d, + 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x1c, 0x06, 0x0a, 0x2a, + 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0e, 0x04, 0x08, 0x1a, 0x8f, 0xc1, + 0xd1, 0xda, 0x6c, 0xd1, 0xa9, 0x02, 0x02, 0x08, 0x00, 0x80, 0x82, 0x05, 0xe0, 0xd0, 0x2f, 0x2d, + 0x52, 0x09, 0x86, 0x55, 0x53, 0xf0, 0x49, 0x8f, 0x00, 0xa1, 0x4d, 0x21, 0xc8, 0xb4, 0xad, 0x27, + 0x12, 0x44, 0xab, 0x4d, 0x10, 0x14, 0xe3, 0x3c, 0x9a, 0x05, 0x77, 0x51, 0x90, 0x4a, 0x3a, 0x8a, + 0x09, 0xa9, 0x4b, 0x36, 0x50, 0x60, 0x22, 0x4b, 0x77, 0x12, 0x5c, 0x2f, 0x60, 0xd3, 0xd9, 0x30, + 0x94, 0x4d, 0x9e, 0x81, 0xc3, 0xe9, 0x9d, 0xd9, 0x47, 0xb3, 0x54, 0xa2, 0x9a, 0x8f, 0xe7, 0x58, + 0x95, 0xd7, 0x48, 0x87, 0xc4, 0x40, 0xad, 0x9a, 0x42, 0x1d, 0x36, 0xb7, 0x48, 0xbc, 0x70, 0x8c, + 0x84, 0xcb, 0x3c, 0x02, 0x25, 0x9f, 0xfe, 0x2c, 0x4a, 0x76, 0xb1, 0x27, 0x94, 0x8f, 0xb0, 0x07, + 0xf0, 0xc0, 0x00, 0x3a, 0x69, 0x16, 0xe1, 0x63, 0x0c, 0xe5, 0x92, 0xc2, 0x7d, 0x99, 0xd9, 0x11, + 0x40, 0xd8, 0x64, 0xab, 0x13, 0xda, 0x73, 0x7b, 0x12, 0x53, 0xb1, 0x0b, 0x0c, 0x67, 0x81, 0xe1, + 0xf5, 0x59, 0x3a, 0xc7, 0xe0, 0xe9, 0xda, 0x12, 0xc7, 0x2b, 0xab, 0x3d, 0xbc, 0x10, 0x3d, 0x1a, + 0x88, 0xc7, 0x1d, 0x31, 0x5f, 0x39, 0x63, 0x51, 0x8b, 0x11, 0x99, 0x05, 0xf9, 0x40, 0x42, 0x27, + 0xad, 0x75, 0x6f, 0xe2, 0x2d, 0x66, 0x28, 0x97, 0x7c, 0x6f, 0xf4, 0xfc, 0x95, 0xaa, 0x67, 0x81, + 0xd8, 0x15, 0x3c, 0xf4, 0x7b, 0x97, 0x08, 0x7b, 0x1b, 0x8c, 0xd3, 0x45, 0x8b, 0x96, 0x54, 0x2c, + 0xb1, 0x00, 0x87, 0x59, 0x5c, 0x94, 0x78, 0x29, 0xaa, 0x7b, 0x9c, 0x5c, 0x61, 0xff, 0xcc, 0x32, + 0x14, 0x4e, 0xc3, 0x1b, 0x96 +}; + +static const uint8_t g_certData04[] = { /* invalid data */ + 0xa0, 0x41, 0xa0, 0x3f, 0x86, 0x3d, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, + 0x2e, 0x64, 0x2d, 0x74, 0x72, 0x75, 0x73, 0x74, 0x2e, 0x6e, 0x65, 0x74, 0x2f, 0x63, 0x72, 0x6c, + 0x2f, 0x64, 0x2d, 0x74, 0x72, 0x75, 0x73, 0x74, 0x5f, 0x72, 0x6f, 0x6f, 0x74, 0x5f, 0x63, 0x6c, + 0x61, 0x73, 0x73, 0x5f, 0x33, 0x5f, 0x63, 0x61, 0x5f, 0x32, 0x5f, 0x32, 0x30, 0x30, 0x39, 0x2e, + 0x63, 0x72, 0x6c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, + 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x7f, 0x97, 0xdb, 0x30, 0xc8, 0xdf, 0xa4, 0x9c, 0x7d, + 0x21, 0x7a, 0x80, 0x70, 0xce, 0x14, 0x12, 0x69, 0x88, 0x14, 0x95, 0x60, 0x44, 0x01, 0xac, 0xb2, + 0xe9, 0x30, 0x4f, 0x9b, 0x50, 0xc2, 0x66, 0xd8, 0x7e, 0x8d, 0x30, 0xb5, 0x70, 0x31, 0xe9, 0xe2, + 0x69, 0xc7, 0xf3, 0x70, 0xdb, 0x20, 0x15, 0x86, 0xd0, 0x0d, 0xf0, 0xbe, 0xac, 0x01, 0x75, 0x84, + 0xce, 0x7e, 0x9f, 0x4d, 0xbf, 0xb7, 0x60, 0x3b, 0x9c, 0xf3, 0xca, 0x1d, 0xe2, 0x5e, 0x68, 0xd8, + 0xa3, 0x9d, 0x97, 0xe5, 0x40, 0x60, 0xd2, 0x36, 0x21, 0xfe, 0xd0, 0xb4, 0xb8, 0x17, 0xda, 0x74, + 0xa3, 0x7f, 0xd4, 0xdf, 0xb0, 0x98, 0x02, 0xac, 0x6f, 0x6b, 0x6b, 0x2c, 0x25, 0x24, 0x72, 0xa1, + 0x65, 0xee, 0x25, 0x5a, 0xe5, 0xe6, 0x32, 0xe7, 0xf2, 0xdf, 0xab, 0x49, 0xfa, 0xf3, 0x90, 0x69, + 0x23, 0xdb, 0x04, 0xd9, 0xe7, 0x5c, 0x58, 0xfc, 0x65, 0xd4, 0x97, 0xbe, 0xcc, 0xfc, 0x2e, 0x0a, + 0xcc, 0x25, 0x2a, 0x35, 0x04, 0xf8, 0x60, 0x91, 0x15, 0x75, 0x3d, 0x41, 0xff, 0x23, 0x1f, 0x19, + 0xc8, 0x6c, 0xeb, 0x82, 0x53, 0x04, 0xa6, 0xe4, 0x4c, 0x22, 0x4d, 0x8d, 0x8c, 0xba, 0xce, 0x5b, + 0x73, 0xec, 0x64, 0x54, 0x50, 0x6d, 0xd1, 0x9c, 0x55, 0xfb, 0x69, 0xc3, 0x36, 0xc3, 0x8c, 0xbc, + 0x3c, 0x85, 0xa6, 0x6b, 0x0a, 0x26, 0x0d, 0xe0, 0x93, 0x98, 0x60, 0xae, 0x7e, 0xc6, 0x24, 0x97, + 0x8a, 0x61, 0x5f, 0x91, 0x8e, 0x66, 0x92, 0x09, 0x87, 0x36, 0xcd, 0x8b, 0x9b, 0x2d, 0x3e, 0xf6, + 0x51, 0xd4, 0x50, 0xd4, 0x59, 0x28, 0xbd, 0x83, 0xf2, 0xcc, 0x28, 0x7b, 0x53, 0x86, 0x6d, 0xd8, + 0x26, 0x88, 0x70, 0xd7, 0xea, 0x91, 0xcd, 0x3e, 0xb9, 0xca, 0xc0, 0x90, 0x6e, 0x5a, 0xc6, 0x5e, + 0x74, 0x65, 0xd7, 0x5c, 0xfe, 0xa3, 0xe2 +}; + +struct UserCertInfoResult { + struct CertInfo certInfo; + bool bExpectResult; +}; + +struct UserCertInfoResult g_certInfoExpectResult[] = { + { + { + "oh:t=c;o=40dc992e;u=0;a=0", + "Hellenic Academic and Research Institutions Cert. Authority", + true, + "CN=Hellenic Academic and Research Institutions RootCA 2011,OU=,O=Hellenic Academic and Research Institutions Cert. Authority", + "CN=Hellenic Academic and Research Institutions RootCA 2011,OU=,O=Hellenic Academic and Research Institutions Cert. Authority", + "0", + "2011-12-6", + "2031-12-1", + "BC:10:4F:15:A4:8B:E7:09:DC:A5:42:A7:E1:D4:B9:DF:6F:05:45:27:E8:02:EA:A9:2D:59:54:44:25:8A:FE:71" + }, + true + }, +}; + + +class CmHiSysEventTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void CmHiSysEventTest::SetUpTestCase(void) +{ +} + +void CmHiSysEventTest::TearDownTestCase(void) +{ +} + +void CmHiSysEventTest::SetUp() +{ +} + +void CmHiSysEventTest::TearDown() +{ +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest001 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmInstallAppCert'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest001, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + uint32_t store = CM_CREDENTIAL_STORE; + uint8_t appCertPwdBuf[] = "123456"; + uint8_t certAliasBuf[] = "keyA"; + uint8_t keyUriBuf[MAX_LEN_URI] = {0}; + struct CmBlob keyUri = { MAX_LEN_URI, keyUriBuf }; + + struct CmBlob appCert = { sizeof(g_p12AbnormalCertinfo), (uint8_t*)g_p12AbnormalCertinfo }; + struct CmBlob appCertPwd = { sizeof(appCertPwdBuf), appCertPwdBuf }; + struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf }; + + (void)CmInstallAppCert(&appCert, &appCertPwd, &certAlias, store, &keyUri); + ret = CmHiSysEventQueryResult("CmInstallAppCert"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; + +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest002 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceInstallUserCert'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest002, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + uint8_t certAliasBuf[] = "abnormal-invalid-certdata"; + uint8_t certUriBuf[MAX_URI_LEN] = {0}; + + struct CmBlob userCertTemp = { sizeof(g_certData04), (uint8_t *)g_certData04 }; /* invalid certData */ + struct CmBlob certAliasTemp = { sizeof(certAliasBuf), certAliasBuf }; + struct CmBlob certUriTemp = { sizeof(certUriBuf), certUriBuf }; + + (void)CmInstallUserTrustedCert(&userCertTemp, &certAliasTemp, &certUriTemp); + ret = CmHiSysEventQueryResult("CmIpcServiceInstallUserCert"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; + +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest003 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceGetUserCertList'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest003, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + + struct CertList *cList = nullptr; + InitUserCertList(&cList); + (void)CmGetUserCertList(100, cList); /* invalid store 100 */ + FreeCertList(cList); + + ret = CmHiSysEventQueryResult("CmIpcServiceGetUserCertList"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest004 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceGetUserCertInfo'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest004, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + char *uri = g_certInfoExpectResult[0].certInfo.uri; + struct CmBlob certUri = { sizeof(uri), (uint8_t *)uri }; + + struct CertInfo *cInfo = nullptr; + InitUserCertInfo(&cInfo); + (void)CmGetUserCertInfo(&certUri, 100, cInfo); /* invalid store 100 */ + + ret = CmHiSysEventQueryResult("CmIpcServiceGetUserCertInfo"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; + + FreeCMBlobData(&(cInfo->certInfo)); +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest005 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceUninstallUserCert'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest005, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + uint8_t certUriBuf[MAX_URI_LEN] = {0}; + struct CmBlob certUriTemp = { sizeof(certUriBuf), certUriBuf }; + + (void)CmUninstallUserTrustedCert(&certUriTemp); + ret = CmHiSysEventQueryResult("CmIpcServiceUninstallUserCert"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest006 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceGrantAppCertificate'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest006, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + uint8_t uriData[] = "oh:t=ak;o=keyA;u=0"; + struct CmBlob keyUri = { sizeof(uriData), uriData }; + + uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0}; + struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData }; + + uint32_t appId = DEFAULT_APP_ID; + + (void)CmGrantAppCertificate(&keyUri, appId, &authUri); + + ret = CmHiSysEventQueryResult("CmIpcServiceGrantAppCertificate"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest007 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceIsAuthorizedApp'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest007, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + + /* authUri macData size 31 */ + uint8_t uriDataFail[] = + "oh:t=ak;o=TestNormalGrant;u=0;a=0?ca=0&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FA"; + struct CmBlob authUriFail = { sizeof(uriDataFail), uriDataFail }; + (void)CmIsAuthorizedApp(&authUriFail); + + ret = CmHiSysEventQueryResult("CmIpcServiceIsAuthorizedApp"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest008 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceRemoveGrantedApp'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest008, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0"; + struct CmBlob keyUri = { strlen((char *)uriData), uriData }; + uint32_t appId = 0; + int32_t ret = CmRemoveGrantedApp(&keyUri, appId); + + ret = CmHiSysEventQueryResult("CmIpcServiceRemoveGrantedApp"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret;} + +} \ No newline at end of file diff --git a/test/hisysevent_test/src/cm_hisysevent_test_common.cpp b/test/hisysevent_test/src/cm_hisysevent_test_common.cpp new file mode 100644 index 0000000..3295474 --- /dev/null +++ b/test/hisysevent_test/src/cm_hisysevent_test_common.cpp @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cm_hisysevent_test_common.h" + +#include +#include +#include + +#include "hisysevent_manager.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" + +using namespace std; + +static const int MAX_QUERY_EVENT_COUNT = 1000; +static const int TIME_S_TO_MS = 1000; +static const int TIME_MS_TO_US = 1000; + +static long long int g_beginTime = 0; +static long long int g_endTime = 0; +static volatile bool g_queryResult = false; +static string g_queryStr; + +namespace OHOS { +namespace HiviewDFX { +class CmHiSysEventCallBack : public OHOS::HiviewDFX::HiSysEventQueryCallback { +public: + CmHiSysEventCallBack() {} + virtual ~CmHiSysEventCallBack() {} + void OnQuery(std::shared_ptr> sysEvents); + void OnComplete(int32_t reason, int32_t total); +}; + +void CmHiSysEventCallBack::OnQuery(std::shared_ptr> sysEvents) +{ + if (g_queryStr.size() == 0 || sysEvents == nullptr) { + return; + } + for_each((*sysEvents).cbegin(), (*sysEvents).cend(), [](const HiSysEventRecord& tmp) { + string::size_type idx = tmp.AsJson().find(g_queryStr); + if (idx != string::npos) { + g_queryResult = true; + } + }); + return; +} + +void CmHiSysEventCallBack::OnComplete(int32_t reason, int32_t total) +{ + return; +} +} // namespace HiviewDFX +} // namespace OHOS + +using namespace OHOS::HiviewDFX; + +static long long int GetCurrentTime(void) +{ + struct timeval tv; + (void)gettimeofday(&tv, nullptr); + long long int timeStamp = tv.tv_sec * TIME_S_TO_MS + tv.tv_usec / TIME_MS_TO_US; + return timeStamp; +} + +void CmHiSysEventQueryStart(void) +{ + g_beginTime = GetCurrentTime(); + g_endTime = 0; +} + +int32_t CmHiSysEventQueryResult(const string funStr) +{ + if (g_beginTime == 0) { + return CM_HISYSEVENT_QUERY_FAILED; + } + + g_queryResult = false; + g_queryStr = funStr; + + sleep(2); // Waiting for hisysevent to upload + + // queryArg + g_endTime = GetCurrentTime(); + struct QueryArg args(g_beginTime, g_endTime, MAX_QUERY_EVENT_COUNT); + + // queryRules + string domain = "CERT_MANAGER"; + vector eventList; + eventList.push_back("CERT_FAULT"); + QueryRule rule(domain, eventList); + vector queryRules; + queryRules.push_back(rule); + + // queryCallback + auto queryCallBack = std::make_shared(); + if (HiSysEventManager::Query(args, queryRules, queryCallBack) == 0) { + CM_LOG_I("CmHiSysEventQueryResult01"); + if (g_queryResult) { + CM_LOG_I("CmHiSysEventQueryResult02"); + return CM_HISYSEVENT_QUERY_SUCCESS; + } + return CM_HISYSEVENT_QUERY_FAILED; + } + CM_LOG_I("CmHiSysEventQueryResult03"); + + return CM_HISYSEVENT_QUERY_FAILED; +} + +void FreeCMBlobData(struct CmBlob *blob) +{ + if (blob == nullptr) { + return; + } + + if (blob->data != nullptr) { + CmFree(blob->data); + blob->data = nullptr; + } + blob->size = 0; +} + +uint32_t InitUserCertInfo(struct CertInfo **cInfo) +{ + *cInfo = (struct CertInfo *)CmMalloc(sizeof(struct CertInfo)); + if (*cInfo == nullptr) { + return CMR_ERROR_MALLOC_FAIL; + } + (void)memset_s(*cInfo, sizeof(struct CertInfo), 0, sizeof(struct CertInfo)); + + (*cInfo)->certInfo.data = (uint8_t *)CmMalloc(MAX_LEN_CERTIFICATE); + if ((*cInfo)->certInfo.data == NULL) { + return CMR_ERROR_MALLOC_FAIL; + } + (*cInfo)->certInfo.size = MAX_LEN_CERTIFICATE; + + return CM_SUCCESS; +} + +uint32_t InitUserCertList(struct CertList **cList) +{ + *cList = (struct CertList *)CmMalloc(sizeof(struct CertList)); + if (*cList == nullptr) { + return CMR_ERROR_MALLOC_FAIL; + } + + uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CertAbstract); + (*cList)->certAbstract = (struct CertAbstract *)CmMalloc(buffSize); + if ((*cList)->certAbstract == NULL) { + return CMR_ERROR_MALLOC_FAIL; + } + (void)memset_s((*cList)->certAbstract, buffSize, 0, buffSize); + (*cList)->certsCount = MAX_COUNT_CERTIFICATE; + + return CM_SUCCESS; +} + +void FreeCertList(struct CertList *certList) +{ + if (certList == nullptr || certList->certAbstract == nullptr) { + return; + } + + CmFree(certList->certAbstract); + certList->certAbstract = nullptr; + + CmFree(certList); + certList = nullptr; +} \ No newline at end of file diff --git a/test/unittest/src/cm_test_common.cpp b/test/unittest/src/cm_test_common.cpp index 47a19fe..df50edb 100644 --- a/test/unittest/src/cm_test_common.cpp +++ b/test/unittest/src/cm_test_common.cpp @@ -129,6 +129,7 @@ bool CompareCert(const struct CertAbstract *firstCert, const struct CertAbstract { if (firstCert == nullptr || secondCert == nullptr) { CM_TEST_LOG_E("cert invalid parameter"); + return false; } return ((strcmp(firstCert->uri, secondCert->uri) == 0) && (strcmp(firstCert->certAlias, secondCert->certAlias) == 0) && @@ -140,6 +141,7 @@ bool CompareCredentialList(const struct CredentialAbstract *firstCert, const str { if (firstCert == nullptr || secondCert == nullptr) { CM_TEST_LOG_E("cert invalid parameter"); + return false; } return ((strcmp(firstCert->type, secondCert->type) == 0) && (strcmp(firstCert->alias, secondCert->alias) == 0) && -- Gitee From 7b4b3084b85668e7d795ba1f9f015eaf9eabffd0 Mon Sep 17 00:00:00 2001 From: huweishuang Date: Sat, 8 Oct 2022 17:01:10 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=AF=81=E4=B9=A6=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=A4=A7=E6=95=B0=E6=8D=AE=E6=89=93=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huweishuang --- BUILD.gn | 5 +- bundle.json | 9 +- .../main/common/include/cm_x509.h | 2 - .../main/common/src/cm_x509.c | 8 - .../os_dependency/cm_ipc/src/cm_ipc_client.c | 2 +- hisysevent.yaml | 23 ++ .../main/core/include/cert_manager_status.h | 1 - .../main/core/src/cert_manager.c | 122 +------ .../main/core/src/cert_manager_file.c | 7 +- .../main/core/src/cert_manager_status.c | 19 -- .../main/core/src/cert_manager_uri.c | 2 +- .../main/hisysevent_wrapper/BUILD.gn | 42 +++ .../include/cm_report_wrapper.h | 34 ++ .../include/hisysevent_wrapper.h | 38 +++ .../src/cm_report_wrapper.c | 42 +++ .../src/hisysevent_wrapper.cpp | 48 +++ .../main/os_dependency/idl/BUILD.gn | 6 +- .../os_dependency/idl/cm_ipc/cm_ipc_service.c | 28 +- .../os_dependency/idl/cm_ipc/cm_response.cpp | 22 -- .../os_dependency/idl/cm_ipc/cm_response.h | 2 - test/hisysevent_test/BUILD.gn | 46 +++ .../include/cm_hisysevent_test_common.h | 46 +++ .../src/cm_hisysevent_test.cpp | 306 ++++++++++++++++++ .../src/cm_hisysevent_test_common.cpp | 182 +++++++++++ test/unittest/src/cm_test_common.cpp | 2 + 25 files changed, 857 insertions(+), 187 deletions(-) create mode 100644 hisysevent.yaml create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/BUILD.gn create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/cm_report_wrapper.h create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/hisysevent_wrapper.h create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/cm_report_wrapper.c create mode 100644 services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/hisysevent_wrapper.cpp create mode 100644 test/hisysevent_test/BUILD.gn create mode 100644 test/hisysevent_test/include/cm_hisysevent_test_common.h create mode 100644 test/hisysevent_test/src/cm_hisysevent_test.cpp create mode 100644 test/hisysevent_test/src/cm_hisysevent_test_common.cpp diff --git a/BUILD.gn b/BUILD.gn index bf16666..7ebe8b6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -14,7 +14,10 @@ group("cert_manager_sdk_test") { testonly = true if (os_level == "standard") { - deps = [ "//base/security/certificate_manager/test:unittest" ] + deps = [ + "//base/security/certificate_manager/test:unittest", + "//base/security/certificate_manager/test/hisysevent_test:cert_hisysevent_test" + ] } else { } } diff --git a/bundle.json b/bundle.json index 2f4702d..0ed5484 100644 --- a/bundle.json +++ b/bundle.json @@ -23,6 +23,9 @@ "small", "mini" ], + "hisysevent_config": [ + "//base/security/huks/hisysevent.yaml" + ], "rom": "5000KB", "ram": "500kB", "deps": { @@ -31,7 +34,8 @@ "ipc_core", "system_ability_fwk", "samgr_proxy", - "c_utils" + "c_utils", + "hisysevent_native" ], "third_party": [ "openssl", @@ -62,7 +66,8 @@ } ], "test": [ - "//base/security/certificate_manager/test:unittest" + "//base/security/certificate_manager/test:unittest", + "//base/security/certificate_manager/test/hisysevent_test:cert_hisysevent_test" ] } } diff --git a/frameworks/cert_manager_standard/main/common/include/cm_x509.h b/frameworks/cert_manager_standard/main/common/include/cm_x509.h index 7c34ec3..6d1fb73 100644 --- a/frameworks/cert_manager_standard/main/common/include/cm_x509.h +++ b/frameworks/cert_manager_standard/main/common/include/cm_x509.h @@ -58,8 +58,6 @@ struct DataTime { X509 *InitCertContext(const uint8_t *certBuf, uint32_t size); -int32_t GetX509Version(X509 *x509cert); - int32_t GetX509SerialNumber(X509 *x509cert, char *outBuf, uint32_t outBufMaxSize); int32_t GetX509SubjectName(const X509 *x509cert, const char *subjectObjName, char* outBuf, uint32_t outBufMaxSize); diff --git a/frameworks/cert_manager_standard/main/common/src/cm_x509.c b/frameworks/cert_manager_standard/main/common/src/cm_x509.c index 9d9774c..eef0813 100644 --- a/frameworks/cert_manager_standard/main/common/src/cm_x509.c +++ b/frameworks/cert_manager_standard/main/common/src/cm_x509.c @@ -50,14 +50,6 @@ X509 *InitCertContext(const uint8_t *certBuf, uint32_t size) return x509; } -int32_t GetX509Version(X509 *x509cert) -{ - if (x509cert == NULL) { - return CMR_ERROR_INVALID_ARGUMENT; - } - return (int32_t)X509_get_version(x509cert) + 1; -} - int32_t GetX509SerialNumber(X509 *x509cert, char *outBuf, uint32_t outBufMaxSize) { if (outBuf == NULL || x509cert == NULL) { diff --git a/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/src/cm_ipc_client.c b/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/src/cm_ipc_client.c index 2c3bae2..52e2060 100644 --- a/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/src/cm_ipc_client.c +++ b/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/src/cm_ipc_client.c @@ -600,7 +600,7 @@ static int32_t CmAppCertInfoUnpackFromService(const struct CmBlob *outData, stru int32_t ret = GetUint32FromBuffer(&certificateInfo->isExist, outData, &offset); if (ret != CM_SUCCESS || certificateInfo->isExist == 0) { - CM_LOG_E("Get certificateInfo->isExist failed ret:%s, is exist:%d", ret, certificateInfo->isExist); + CM_LOG_E("Get certificateInfo->isExist failed ret:%d, is exist:%d", ret, certificateInfo->isExist); return ret; } diff --git a/hisysevent.yaml b/hisysevent.yaml new file mode 100644 index 0000000..257b698 --- /dev/null +++ b/hisysevent.yaml @@ -0,0 +1,23 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +domain: CERT_MANAGER + +CERT_FAULT: + __BASE: {type: FAULT, level: CRITICAL, tag: cert, desc: fault event for certmanager} + FUNCTION: {type: STRING, desc: function name} + USER_ID: {type: INT32, desc: user id} + UID: {type: INT32, desc: uid} + CERT_NAME: {type: STRING, desc: cert name} + ERROR_CODE: {type: INT32, desc: error code} \ No newline at end of file diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/include/cert_manager_status.h b/services/cert_manager_standard/cert_manager_engine/main/core/include/cert_manager_status.h index 4ad7135..3483359 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/include/cert_manager_status.h +++ b/services/cert_manager_standard/cert_manager_engine/main/core/include/cert_manager_status.h @@ -91,7 +91,6 @@ int32_t CertManagerGetCertificatesStatus( uint32_t *status); int32_t CertManagerStatusInit(void); -int32_t CertManagerStatusDestroy(void); int32_t SetcertStatus(const struct CmContext *context, const struct CmBlob *certUri, uint32_t store, uint32_t status, uint32_t *stp); diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager.c index 4aaedb7..fcd2815 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager.c @@ -238,7 +238,6 @@ void CmCertificateListFree(struct CmMutableBlob *certListData, uint32_t certList } CMFree(certListData); - certListData = NULL; } static int32_t CmCreateCertificateList(struct CmBlob *certList, @@ -249,10 +248,13 @@ static int32_t CmCreateCertificateList(struct CmBlob *certList, uint32_t certBuffSize = 0; int32_t ret; struct CmMutableBlob *certDataList = NULL; + if ((certList == NULL) || (fileNames == NULL) || (path == NULL)) { + CM_LOG_E("Bad parameters: param is null"); + return CMR_ERROR_INVALID_ARGUMENT; + } - if ((certList == NULL) || (fileNames == NULL) || (path == NULL) || - (fileNames->data == NULL) || (fileNames->size > MAX_FILES_IN_DIR)) { - CM_LOG_E("Bad parameters: path = %s, ileNames->size = %u", path, fileNames->size); + if ((fileNames->data == NULL) || (fileNames->size > MAX_FILES_IN_DIR)) { + CM_LOG_E("Bad parameters: path = %s, fileNames->size = %u", path, fileNames->size); return CMR_ERROR_INVALID_ARGUMENT; } @@ -698,117 +700,6 @@ cleanup: return retVal; } -static int32_t CmGetMatchedFileSubjectNameIndex(const struct CmMutableBlob *nameDigest, - const struct CmMutableBlob *fileNames, const struct CmAsn1Obj *subjectName, struct CmMathedIndexPara indexPara) -{ - uint32_t i; - struct CmAsn1Obj subjectFromList; - uint8_t certBuff[CERT_MANAGER_MAX_CERT_SIZE]; - struct CmBlob certificate = {sizeof(certBuff), certBuff}; - char *path = indexPara.path; - uint8_t *indexes = indexPara.indexes; - uint32_t *count = indexPara.count; - struct CmMutableBlob *fname = (struct CmMutableBlob *)fileNames->data; - - (void)memset_s(&subjectFromList, sizeof(struct CmAsn1Obj), 0, sizeof(struct CmAsn1Obj)); - for (i = 0; i < fileNames->size; i++) { - if (memcmp(fname[i].data, nameDigest->data, nameDigest->size) == 0) { - if (CmFileRead(path, (char *)fname[i].data, 0, certBuff, sizeof(certBuff)) == 0) { - CM_LOG_E("Failed to read file: %s/%s", path, fname[i].data); - return CMR_ERROR_READ_FILE_ERROR; - } - - if (CmGetSubjectNameAsn1(&certificate, &subjectFromList) != CMR_OK) { - CM_LOG_E("Failed to obtain subjectName"); - return CMR_ERROR_NOT_FOUND; - } - if ((subjectName->value.size == subjectFromList.value.size) || - (memcmp(subjectName->value.data, subjectFromList.value.data, subjectName->value.size))) { - indexes[*count] = i; - count++; - } - } - } - return CMR_OK; -} - -static int32_t CmListCertificatesBySubjectNameAsn1(const struct CmContext *context, - struct CmBlob *certificateList, uint32_t store, const struct CmAsn1Obj *subjectName) -{ - int32_t retVal = 0; - uint32_t count = 0; - uint8_t *indexes = NULL; - uint32_t *status = NULL; - struct CmMutableBlob fileNames = {0, NULL}, matchingFiles = {0, NULL}; - char path[CERT_MAX_PATH_LEN]; - uint8_t buff[MAX_NAME_DIGEST_LEN]; - struct CmMutableBlob nameDigest = {sizeof(buff), buff}, pathBlob = {sizeof(path), (uint8_t *)path}; - - retVal = NameHashFromAsn1(subjectName, &nameDigest); - if (retVal != CMR_OK) { - return retVal; - } - - if (CmGetFilenames(context, &pathBlob, store, &fileNames, path) != CMR_OK) { - return CMR_ERROR_STORAGE; - } - - if (CmInitFileNameIndexArray(&indexes, fileNames) != CMR_OK) { - retVal = CMR_ERROR_MALLOC_FAIL; - goto cleanup; - } - - struct CmMathedIndexPara indexPara = {path, store, status, &count, indexes}; - if (CmGetMatchedFileSubjectNameIndex(&nameDigest, &fileNames, subjectName, indexPara) != CMR_OK) { - CM_LOG_E("Failed to get matched file name indexes"); - retVal = CMR_ERROR; - goto cleanup; - } - - matchingFiles.size = count; - matchingFiles.data = malloc(sizeof(struct CmMutableBlob) * count); - if (matchingFiles.data == NULL) { - CM_LOG_E("Failed to allocate memory for files"); - retVal = CMR_ERROR_MALLOC_FAIL; - goto cleanup; - } - - struct CmMutableBlob *fname = (struct CmMutableBlob *)fileNames.data; - struct CmMutableBlob *matchName = (struct CmMutableBlob *)matchingFiles.data; - - if (CmGetMatchedFileNames(matchName, fname, count, indexes) != CMR_OK) { - CM_LOG_E("Failed to get matched file Name indexes"); - retVal = CMR_ERROR; - goto cleanup; - } - - if (CmCreateCertificateList(certificateList, &matchingFiles, path) < 0) { - CM_LOG_E("Failed to create certificates: %s", path); - retVal = CMR_ERROR_STORAGE; - goto cleanup; - } - -cleanup: - CmFreeCertificatesInfo(&fileNames, &matchingFiles, indexes, certificateList, retVal); - return retVal; -} - -int32_t CertManagerListCertificatesBySubjectName(const struct CmContext *context, - struct CmBlob *certificateList, uint32_t store, const struct CmBlob *subjectName) -{ - struct CmAsn1Obj subjectAsn1; - struct CmBlob skip = {0, NULL}; - errno_t ret; - - (void)memset_s(&subjectAsn1, sizeof(struct CmAsn1Obj), 0, sizeof(struct CmAsn1Obj)); - ret = CmAsn1ExtractTag(&skip, &subjectAsn1, &CM_BLOB(subjectName), ASN_1_TAG_TYPE_SEQ); - if (ret != CMR_OK) { - CM_LOG_E("Subject name in bad format"); - return CMR_ERROR_NOT_FOUND; - } - return CmListCertificatesBySubjectNameAsn1(context, certificateList, store, &subjectAsn1); -} - /* This function constructes md5 hash part of filename for storing certificate. * All cetificates are stored in files namePrefix.count. where namePrefix = md5(subjectName) * and count is = 0, 1.... needed fpr potential hash collisions. @@ -1048,6 +939,7 @@ void CmFreeFileNames(struct CmBlob *fileNames, const uint32_t fileSize) { if (fileNames == NULL) { CM_LOG_E("CmFreeFileNames fileNames is null"); + return; } for (uint32_t i = 0; i < fileSize; i++) { diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file.c index 182947d..321d93e 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file.c @@ -48,7 +48,7 @@ inline int32_t CertManagerFileRemove(const char *path, const char *fileName) return CM_ERROR(CmFileRemove(path, fileName)); } -static uint32_t GetNumberOfFiles(const char *path) +static int32_t GetNumberOfFiles(const char *path) { void *dir = CmOpenDir(path); if (dir == NULL) { @@ -56,7 +56,7 @@ static uint32_t GetNumberOfFiles(const char *path) return -1; } - uint32_t count = 0; + int32_t count = 0; struct CmFileDirentInfo dire = {{0}}; while (CmGetDirFile(dir, &dire) == CMR_OK) { count++; @@ -69,7 +69,7 @@ static int32_t MallocFileNames(struct CmMutableBlob *fileNames, const char *path uint32_t *fileCount) { struct CmMutableBlob *tmp = NULL; - uint32_t fileNums = GetNumberOfFiles(path); + int32_t fileNums = GetNumberOfFiles(path); if (fileNums < 0) { CM_LOG_E("Failed to obtain number of files from: path = %s", path); return -1; @@ -108,7 +108,6 @@ static void FreeFileNames(struct CmMutableBlob *fNames, uint32_t endIndex) } } CMFree(fNames); - fNames = NULL; } int32_t CertManagerGetFilenames(struct CmMutableBlob *fileNames, const char *path, struct CmBlob *uri) diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_status.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_status.c index b2d7e71..7066ad4 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_status.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_status.c @@ -93,15 +93,6 @@ static void FreeStatus(struct CertStatus *cs) } } -static void FreeTreeNodeValue(RbTreeKey key, RbTreeValue v, const void *context) -{ - (void) context; - (void) key; - if (v != NULL) { - FreeStatus((struct CertStatus *) v); - } -} - static int GetStoreIndex(uint32_t store) { switch (store) { @@ -440,16 +431,6 @@ finally: return rc; } -int32_t CertManagerStatusDestroy(void) -{ - pthread_rwlock_wrlock(&g_statusLock); - for (uint32_t i = 0; i < g_treeCount; i++) { - (void) RbTreeDestroyEx(&g_trees[i], FreeTreeNodeValue, NULL); - } - pthread_rwlock_unlock(&g_statusLock); - return CMR_OK; -} - inline static RbTreeKey GetRbTreeKeyFromName(char *name) { /* use the first 4 bytes of file name (exluding the first bit) as the key */ diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_uri.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_uri.c index 152e08b..43f1a00 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_uri.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_uri.c @@ -275,7 +275,7 @@ static inline uint32_t IndexOf(char sep, const char *data, uint32_t start, uint3 static char *DecodeValue(const char *s, uint32_t off, uint32_t len) { - if (s == NULL || len <= 0) { + if (s == NULL || len == 0) { return NULL; } char *buf = MALLOC(len + 1); diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/BUILD.gn b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/BUILD.gn new file mode 100644 index 0000000..a99940d --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/BUILD.gn @@ -0,0 +1,42 @@ +# Copyright (C) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +config("cert_manager_config") { + include_dirs = [ "include" ] +} + +ohos_static_library("libcert_manager_hisysevent_wrapper_static") { + subsystem_name = "security" + part_name = "certificate_manager" + public_configs = [ ":cert_manager_config" ] + include_dirs = [ + "//base/security/certificate_manager/frameworks/cert_manager_standard/main/common/include", + "//base/security/certificate_manager/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/include", + ] + sources = [ + "src/hisysevent_wrapper.cpp", + "src/cm_report_wrapper.c", + ] + cflags = [ + "-Wall", + "-Werror", + ] + defines = [ + "L2_STANDARD", + "_CM_LOG_ENABLE_", +] + external_deps = [ "hisysevent_native:libhisysevent" ] + complete_static_lib = true +} diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/cm_report_wrapper.h b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/cm_report_wrapper.h new file mode 100644 index 0000000..9502352 --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/cm_report_wrapper.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CM_REPORT_WRAPPER_H +#define CM_REPORT_WRAPPER_H + +#include +#include "hisysevent_wrapper.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t ReportFaultEvent(const char *funcName, const struct CmContext *cmContext, const char *certName, int32_t errorCode); + +void CmReport(const char *funcName, const struct CmContext *cmContext, const char *certName, int32_t errorCode); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/hisysevent_wrapper.h b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/hisysevent_wrapper.h new file mode 100644 index 0000000..605189c --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include/hisysevent_wrapper.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CM_CLIENT_SERVICE_ADAPTER_H +#define CM_CLIENT_SERVICE_ADAPTER_H + +#include "cm_type_inner.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct EventValues { + uint32_t userId; + uint32_t uid; + const char *certName; + int32_t errorCode; +}; + +int WriteEvent(const char *functionName, const struct EventValues *eventValues); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/cm_report_wrapper.c b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/cm_report_wrapper.c new file mode 100644 index 0000000..803082d --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/cm_report_wrapper.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type_inner.h" +#include "cm_report_wrapper.h" + +int32_t ReportFaultEvent(const char *funcName, const struct CmContext *cmContext, const char *certName, int32_t errorCode) +{ + if (errorCode == CM_SUCCESS) { + return CM_SUCCESS; + } + int32_t ret; + + struct EventValues eventValues = { cmContext->userId, cmContext->uid, certName, errorCode }; + ret = WriteEvent(funcName, &eventValues); + if (ret != CM_SUCCESS) { + CM_LOG_E("ReportFaultEvent failed, ret = %d", ret); + } + return ret; +} + +void CmReport(const char *funcName, const struct CmContext *cmContext, const char *certName, int32_t errorCode) +{ + int32_t ret = ReportFaultEvent(funcName, cmContext, certName, errorCode); + if (ret != CM_SUCCESS) { + CM_LOG_E("report fault event failed, ret = %d", ret); + } +} \ No newline at end of file diff --git a/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/hisysevent_wrapper.cpp b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/hisysevent_wrapper.cpp new file mode 100644 index 0000000..5cf6fca --- /dev/null +++ b/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/src/hisysevent_wrapper.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "hisysevent_wrapper.h" +#include "hisysevent.h" + +#include "cm_log.h" + +using namespace OHOS::HiviewDFX; + +static constexpr const char domain[] = "CERT_MANAGER"; +static constexpr const char g_eventName[] = "CERT_FAULT"; +static constexpr const char g_tagFunction[] = "FUNCTION"; +static constexpr const char g_tagUserId[] = "USER_ID"; +static constexpr const char g_tagUID[] = "UID"; +static constexpr const char g_tagCertName[] = "CERT_NAME"; +static constexpr const char g_tagErrorCode[] = "ERROR_CODE"; + +int WriteEvent(const char *functionName, const struct EventValues *eventValues) +{ + int32_t ret = HiSysEventWrite(domain, g_eventName, HiSysEvent::EventType::FAULT, + g_tagFunction, functionName, + g_tagUserId, eventValues->userId, + g_tagUID, eventValues->uid, + g_tagCertName, eventValues->certName, + g_tagErrorCode, eventValues->errorCode); + CM_LOG_I("g_tagFunction:%s, g_tagUserId:%u, g_tagUID:%u, g_tagCertName:%s, g_tagErrorCode:%d", + functionName, eventValues->userId, eventValues->uid, eventValues->certName, eventValues->errorCode); + + if (ret != CM_SUCCESS) { + CM_LOG_E("WriteEvent failed!"); + return ret; + } + return ret; +} \ No newline at end of file diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/BUILD.gn b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/BUILD.gn index 2a1f4ca..ad9c54f 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/BUILD.gn +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/BUILD.gn @@ -29,7 +29,8 @@ ohos_static_library("libcm_service_idl_standard_static") { "//commonlibrary/c_utils/base/include", "//base/security/huks/interfaces/innerkits/huks_standard/main/include", "//base/security/certificate_manager/frameworks/cert_manager_standard/main/os_dependency/cm_ipc/include", - "//third_party/openssl/include" + "//third_party/openssl/include", + "//base/security/certificate_manager/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper/include" ] defines = [ "L2_STANDARD", @@ -44,7 +45,8 @@ ohos_static_library("libcm_service_idl_standard_static") { "//base/security/certificate_manager/frameworks/cert_manager_standard/main/os_dependency:libcert_manager_os_dependency_standard_static", "//base/security/certificate_manager/frameworks/cert_manager_standard/main/common:libcert_manager_common_standard_static", "//base/security/certificate_manager/services/cert_manager_standard/cert_manager_engine/main/core:cert_manager_engine_core_standard", - "//third_party/openssl:libcrypto_shared" + "//third_party/openssl:libcrypto_shared", + "//base/security/certificate_manager/services/cert_manager_standard/cert_manager_service/main/hisysevent_wrapper:libcert_manager_hisysevent_wrapper_static" ] external_deps = [ "ipc:ipc_core", diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c index 48e5538..d6912a0 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c @@ -45,6 +45,7 @@ #include "cert_manager_query.h" #include "cert_manager_permission_check.h" +#include "cm_report_wrapper.h" #define MAX_PACKAGENAME_LEN 32 #define MAX_LEN_CERTIFICATE 8196 @@ -522,6 +523,8 @@ static int32_t CmInstallAppCert(const struct CmContext *context, const struct Cm } } while (0); + CmReport(__func__, context, (char *)certAlias->data, ret); + EVP_PKEY_free(priKey); return ret; } @@ -1216,10 +1219,9 @@ void CmIpcServiceGrantAppCertificate(const struct CmBlob *paramSetBlob, struct C { struct CmContext cmContext = { 0, 0, {0} }; struct CmParamSet *paramSet = NULL; - + struct CmBlob keyUri = { 0, NULL }; int32_t ret; do { - struct CmBlob keyUri = { 0, NULL }; uint32_t appUid = 0; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &keyUri }, @@ -1238,6 +1240,8 @@ void CmIpcServiceGrantAppCertificate(const struct CmBlob *paramSetBlob, struct C } } while (0); + CmReport(__func__, context, (char *)keyUri.data, ret); + CM_LOG_I("CmIpcServiceGrantAppCertificate end:%d", ret); CmSendResponse(context, ret, outData); CmFreeParamSet(¶mSet); @@ -1248,10 +1252,10 @@ void CmIpcServiceGetAuthorizedAppList(const struct CmBlob *paramSetBlob, struct { struct CmContext cmContext = { 0, 0, {0} }; struct CmParamSet *paramSet = NULL; + struct CmBlob keyUri = { 0, NULL }; int32_t ret; do { - struct CmBlob keyUri = { 0, NULL }; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &keyUri }, }; @@ -1267,6 +1271,7 @@ void CmIpcServiceGetAuthorizedAppList(const struct CmBlob *paramSetBlob, struct break; } } while (0); + CmReport(__func__, context, (char *)keyUri.data, ret); CM_LOG_I("CmIpcServiceGetAuthorizedAppList end:%d", ret); CmSendResponse(context, ret, outData); @@ -1279,10 +1284,10 @@ void CmIpcServiceIsAuthorizedApp(const struct CmBlob *paramSetBlob, struct CmBlo (void)outData; struct CmContext cmContext = { 0, 0, {0} }; struct CmParamSet *paramSet = NULL; + struct CmBlob authUri = { 0, NULL }; int32_t ret; do { - struct CmBlob authUri = { 0, NULL }; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &authUri }, }; @@ -1299,6 +1304,7 @@ void CmIpcServiceIsAuthorizedApp(const struct CmBlob *paramSetBlob, struct CmBlo } } while (0); + CmReport(__func__, context, (char *)authUri.data, ret); CM_LOG_I("CmIpcServiceIsAuthorizedApp end:%d", ret); CmSendResponse(context, ret, NULL); CmFreeParamSet(¶mSet); @@ -1310,11 +1316,11 @@ void CmIpcServiceRemoveGrantedApp(const struct CmBlob *paramSetBlob, struct CmBl struct CmContext cmContext = { 0, 0, {0} }; struct CmParamSet *paramSet = NULL; (void)outData; + struct CmBlob keyUri = { 0, NULL }; int32_t ret; do { uint32_t appUid = 0; - struct CmBlob keyUri = { 0, NULL }; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &keyUri }, { .tag = CM_TAG_PARAM1_UINT32, .uint32Param = &appUid }, @@ -1331,6 +1337,7 @@ void CmIpcServiceRemoveGrantedApp(const struct CmBlob *paramSetBlob, struct CmBl break; } } while (0); + CmReport(__func__, context, (char *)keyUri.data, ret); CM_LOG_I("CmIpcServiceRemoveGrantedApp end:%d", ret); CmSendResponse(context, ret, NULL); @@ -1483,7 +1490,6 @@ static int32_t CmCheckCallerPermission(const struct CmContext *ipcInfo) void CmIpcServiceGetUserCertList(const struct CmBlob *paramSetBlob, struct CmBlob *outData, const struct CmContext *context) { - CM_LOG_I("enter CmIpcServiceGetUserCertList"); int32_t ret = CM_SUCCESS; uint32_t store; struct CmContext cmContext = {0}; @@ -1520,6 +1526,9 @@ void CmIpcServiceGetUserCertList(const struct CmBlob *paramSetBlob, struct CmBlo CmSendResponse(context, ret, outData); } while (0); + + CmReport(__func__, &cmContext, "certName", ret); + if (ret != CM_SUCCESS) { CmSendResponse(context, ret, NULL); } @@ -1570,6 +1579,7 @@ void CmIpcServiceGetUserCertInfo(const struct CmBlob *paramSetBlob, struct CmBlo } CmSendResponse(context, ret, outData); } while (0); + CmReport(__func__, &cmContext, (char *)certUri.data, ret); if (ret != CM_SUCCESS) { CmSendResponse(context, ret, NULL); } @@ -1622,7 +1632,7 @@ void CmIpcServiceInstallUserCert(const struct CmBlob *paramSetBlob, struct CmBlo struct CmBlob userCert = { 0, NULL }; struct CmBlob certAlias = { 0, NULL }; struct CmContext cmContext = {0}; - struct CmParamSet *paramSet = NULL; + struct CmParamSet *paramSet = NULL; struct CmParamOut params[] = { { .tag = CM_TAG_PARAM0_BUFFER, .blob = &userCert }, { .tag = CM_TAG_PARAM1_BUFFER, .blob = &certAlias }, @@ -1648,6 +1658,9 @@ void CmIpcServiceInstallUserCert(const struct CmBlob *paramSetBlob, struct CmBlo CmSendResponse(context, ret, outData); } while (0); + + CmReport(__func__, &cmContext, "NULL", ret); + if (ret != CM_SUCCESS) { CmSendResponse(context, ret, NULL); } @@ -1685,6 +1698,7 @@ void CmIpcServiceUninstallUserCert(const struct CmBlob *paramSetBlob, struct CmB break; } } while (0); + CmReport(__func__, &cmContext, (char *)certUri.data, ret); CmSendResponse(context, ret, NULL); CmFreeParamSet(¶mSet); } diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.cpp b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.cpp index e4df11e..e8a56eb 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.cpp +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.cpp @@ -46,28 +46,6 @@ void CmSendResponse(const struct CmContext *context, int32_t result, const struc } } -int32_t CmGetProcessNameForIPC(const struct CmContext *context, struct CmBlob *processName) -{ - if ((context == nullptr) || (processName == nullptr)) { - CM_LOG_D("CmGetProcessNameForIPC don't need get process name in hosp."); - return CM_SUCCESS; - } - - auto callingUid = IPCSkeleton::GetCallingUid(); - uint8_t *name = (uint8_t *)CmMalloc(sizeof(callingUid)); - if (name == nullptr) { - CM_LOG_E("CmGetProcessNameForIPC malloc failed."); - return CMR_ERROR_MALLOC_FAIL; - } - - if (memcpy_s(name, sizeof(callingUid), &callingUid, sizeof(callingUid)) != EOK) { - return CM_FAILURE; - } - processName->size = sizeof(callingUid); - processName->data = name; - return CM_SUCCESS; -} - int32_t CmGetProcessInfoForIPC(struct CmContext *cmContext) { if (cmContext == nullptr) { diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.h b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.h index 1686178..864cf1a 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.h +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_response.h @@ -24,8 +24,6 @@ extern "C" { void CmSendResponse(const struct CmContext *context, int32_t result, const struct CmBlob *response); -int32_t CmGetProcessNameForIPC(const struct CmContext *context, struct CmBlob *processName); - int32_t CmGetProcessInfoForIPC(struct CmContext *cmContext); #ifdef __cplusplus diff --git a/test/hisysevent_test/BUILD.gn b/test/hisysevent_test/BUILD.gn new file mode 100644 index 0000000..afa87b2 --- /dev/null +++ b/test/hisysevent_test/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (C) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +module_output_path = "cert_manager_standard/cert_manager_standard_test" + +ohos_unittest("cert_hisysevent_test") { + module_out_path = module_output_path + + sources = [ + "src/cm_hisysevent_test.cpp", + "src/cm_hisysevent_test_common.cpp", + ] + + defines = [ + "L2_STANDARD", + "_CM_LOG_ENABLE_", + ] + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "include", + "//base/security/certificate_manager/frameworks/cert_manager_standard/main/common/include", + ] + + deps = [ + "//base/security/certificate_manager/frameworks/cert_manager_standard/main:cert_manager_standard_frameworks", + "//base/security/certificate_manager/interfaces/innerkits/cert_manager_standard/main:cert_manager_sdk", + ] + + external_deps = [ + "hisysevent_native:libhisysevent", + "hisysevent_native:libhisyseventmanager", + ] +} diff --git a/test/hisysevent_test/include/cm_hisysevent_test_common.h b/test/hisysevent_test/include/cm_hisysevent_test_common.h new file mode 100644 index 0000000..77029fb --- /dev/null +++ b/test/hisysevent_test/include/cm_hisysevent_test_common.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CM_HISYSEVENT_TEST_COMMON_H +#define CM_HISYSEVENT_TEST_COMMON_H + +#include +#include +#include "securec.h" +#include "cm_type.h" +#ifdef __cplusplus +extern "C" { +#endif + +#define CM_HISYSEVENT_QUERY_SUCCESS 0 +#define CM_HISYSEVENT_QUERY_FAILED (-1) + +void CmHiSysEventQueryStart(void); + +int32_t CmHiSysEventQueryResult(const std::string funStr); + +void FreeCMBlobData(struct CmBlob *blob); + +uint32_t InitUserCertList(struct CertList **cList); + +uint32_t InitUserCertInfo(struct CertInfo **cInfo); + +void FreeCertList(struct CertList *certList); + +#ifdef __cplusplus +} +#endif + +#endif // CM_HISYSEVENT_TEST_COMMON_H \ No newline at end of file diff --git a/test/hisysevent_test/src/cm_hisysevent_test.cpp b/test/hisysevent_test/src/cm_hisysevent_test.cpp new file mode 100644 index 0000000..0b86a95 --- /dev/null +++ b/test/hisysevent_test/src/cm_hisysevent_test.cpp @@ -0,0 +1,306 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "cm_hisysevent_test_common.h" +#include "cert_manager_api.h" + +using namespace testing::ext; +namespace { +#define MAX_URI_LEN 256 +static constexpr uint32_t DEFAULT_AUTH_URI_LEN = 256; +static constexpr uint32_t DEFAULT_APP_ID = 1000; + +static const uint8_t g_p12AbnormalCertinfo[] = { + 0x30, 0x82, 0x0b, 0xc1, 0x02, 0x01, 0x03, 0x30, 0x82, 0x0b, 0x87, 0x06, 0x09, 0x2a, 0x86, 0x48, + 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82, 0x0b, 0x78, 0x04, 0x82, 0x0b, 0x74, 0x30, 0x82, + 0x0b, 0x70, 0x30, 0x82, 0x06, 0x27, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, + 0x06, 0xa0, 0x82, 0x06, 0x18, 0x30, 0x82, 0x06, 0x14, 0x02, 0x01, 0x00, 0x30, 0x82, 0x06, 0x0d, + 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x1c, 0x06, 0x0a, 0x2a, + 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0e, 0x04, 0x08, 0x1a, 0x8f, 0xc1, + 0xd1, 0xda, 0x6c, 0xd1, 0xa9, 0x02, 0x02, 0x08, 0x00, 0x80, 0x82, 0x05, 0xe0, 0xd0, 0x2f, 0x2d, + 0x52, 0x09, 0x86, 0x55, 0x53, 0xf0, 0x49, 0x8f, 0x00, 0xa1, 0x4d, 0x21, 0xc8, 0xb4, 0xad, 0x27, + 0x12, 0x44, 0xab, 0x4d, 0x10, 0x14, 0xe3, 0x3c, 0x9a, 0x05, 0x77, 0x51, 0x90, 0x4a, 0x3a, 0x8a, + 0x09, 0xa9, 0x4b, 0x36, 0x50, 0x60, 0x22, 0x4b, 0x77, 0x12, 0x5c, 0x2f, 0x60, 0xd3, 0xd9, 0x30, + 0x94, 0x4d, 0x9e, 0x81, 0xc3, 0xe9, 0x9d, 0xd9, 0x47, 0xb3, 0x54, 0xa2, 0x9a, 0x8f, 0xe7, 0x58, + 0x95, 0xd7, 0x48, 0x87, 0xc4, 0x40, 0xad, 0x9a, 0x42, 0x1d, 0x36, 0xb7, 0x48, 0xbc, 0x70, 0x8c, + 0x84, 0xcb, 0x3c, 0x02, 0x25, 0x9f, 0xfe, 0x2c, 0x4a, 0x76, 0xb1, 0x27, 0x94, 0x8f, 0xb0, 0x07, + 0xf0, 0xc0, 0x00, 0x3a, 0x69, 0x16, 0xe1, 0x63, 0x0c, 0xe5, 0x92, 0xc2, 0x7d, 0x99, 0xd9, 0x11, + 0x40, 0xd8, 0x64, 0xab, 0x13, 0xda, 0x73, 0x7b, 0x12, 0x53, 0xb1, 0x0b, 0x0c, 0x67, 0x81, 0xe1, + 0xf5, 0x59, 0x3a, 0xc7, 0xe0, 0xe9, 0xda, 0x12, 0xc7, 0x2b, 0xab, 0x3d, 0xbc, 0x10, 0x3d, 0x1a, + 0x88, 0xc7, 0x1d, 0x31, 0x5f, 0x39, 0x63, 0x51, 0x8b, 0x11, 0x99, 0x05, 0xf9, 0x40, 0x42, 0x27, + 0xad, 0x75, 0x6f, 0xe2, 0x2d, 0x66, 0x28, 0x97, 0x7c, 0x6f, 0xf4, 0xfc, 0x95, 0xaa, 0x67, 0x81, + 0xd8, 0x15, 0x3c, 0xf4, 0x7b, 0x97, 0x08, 0x7b, 0x1b, 0x8c, 0xd3, 0x45, 0x8b, 0x96, 0x54, 0x2c, + 0xb1, 0x00, 0x87, 0x59, 0x5c, 0x94, 0x78, 0x29, 0xaa, 0x7b, 0x9c, 0x5c, 0x61, 0xff, 0xcc, 0x32, + 0x14, 0x4e, 0xc3, 0x1b, 0x96 +}; + +static const uint8_t g_certData04[] = { /* invalid data */ + 0xa0, 0x41, 0xa0, 0x3f, 0x86, 0x3d, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, + 0x2e, 0x64, 0x2d, 0x74, 0x72, 0x75, 0x73, 0x74, 0x2e, 0x6e, 0x65, 0x74, 0x2f, 0x63, 0x72, 0x6c, + 0x2f, 0x64, 0x2d, 0x74, 0x72, 0x75, 0x73, 0x74, 0x5f, 0x72, 0x6f, 0x6f, 0x74, 0x5f, 0x63, 0x6c, + 0x61, 0x73, 0x73, 0x5f, 0x33, 0x5f, 0x63, 0x61, 0x5f, 0x32, 0x5f, 0x32, 0x30, 0x30, 0x39, 0x2e, + 0x63, 0x72, 0x6c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, + 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x7f, 0x97, 0xdb, 0x30, 0xc8, 0xdf, 0xa4, 0x9c, 0x7d, + 0x21, 0x7a, 0x80, 0x70, 0xce, 0x14, 0x12, 0x69, 0x88, 0x14, 0x95, 0x60, 0x44, 0x01, 0xac, 0xb2, + 0xe9, 0x30, 0x4f, 0x9b, 0x50, 0xc2, 0x66, 0xd8, 0x7e, 0x8d, 0x30, 0xb5, 0x70, 0x31, 0xe9, 0xe2, + 0x69, 0xc7, 0xf3, 0x70, 0xdb, 0x20, 0x15, 0x86, 0xd0, 0x0d, 0xf0, 0xbe, 0xac, 0x01, 0x75, 0x84, + 0xce, 0x7e, 0x9f, 0x4d, 0xbf, 0xb7, 0x60, 0x3b, 0x9c, 0xf3, 0xca, 0x1d, 0xe2, 0x5e, 0x68, 0xd8, + 0xa3, 0x9d, 0x97, 0xe5, 0x40, 0x60, 0xd2, 0x36, 0x21, 0xfe, 0xd0, 0xb4, 0xb8, 0x17, 0xda, 0x74, + 0xa3, 0x7f, 0xd4, 0xdf, 0xb0, 0x98, 0x02, 0xac, 0x6f, 0x6b, 0x6b, 0x2c, 0x25, 0x24, 0x72, 0xa1, + 0x65, 0xee, 0x25, 0x5a, 0xe5, 0xe6, 0x32, 0xe7, 0xf2, 0xdf, 0xab, 0x49, 0xfa, 0xf3, 0x90, 0x69, + 0x23, 0xdb, 0x04, 0xd9, 0xe7, 0x5c, 0x58, 0xfc, 0x65, 0xd4, 0x97, 0xbe, 0xcc, 0xfc, 0x2e, 0x0a, + 0xcc, 0x25, 0x2a, 0x35, 0x04, 0xf8, 0x60, 0x91, 0x15, 0x75, 0x3d, 0x41, 0xff, 0x23, 0x1f, 0x19, + 0xc8, 0x6c, 0xeb, 0x82, 0x53, 0x04, 0xa6, 0xe4, 0x4c, 0x22, 0x4d, 0x8d, 0x8c, 0xba, 0xce, 0x5b, + 0x73, 0xec, 0x64, 0x54, 0x50, 0x6d, 0xd1, 0x9c, 0x55, 0xfb, 0x69, 0xc3, 0x36, 0xc3, 0x8c, 0xbc, + 0x3c, 0x85, 0xa6, 0x6b, 0x0a, 0x26, 0x0d, 0xe0, 0x93, 0x98, 0x60, 0xae, 0x7e, 0xc6, 0x24, 0x97, + 0x8a, 0x61, 0x5f, 0x91, 0x8e, 0x66, 0x92, 0x09, 0x87, 0x36, 0xcd, 0x8b, 0x9b, 0x2d, 0x3e, 0xf6, + 0x51, 0xd4, 0x50, 0xd4, 0x59, 0x28, 0xbd, 0x83, 0xf2, 0xcc, 0x28, 0x7b, 0x53, 0x86, 0x6d, 0xd8, + 0x26, 0x88, 0x70, 0xd7, 0xea, 0x91, 0xcd, 0x3e, 0xb9, 0xca, 0xc0, 0x90, 0x6e, 0x5a, 0xc6, 0x5e, + 0x74, 0x65, 0xd7, 0x5c, 0xfe, 0xa3, 0xe2 +}; + +struct UserCertInfoResult { + struct CertInfo certInfo; + bool bExpectResult; +}; + +struct UserCertInfoResult g_certInfoExpectResult[] = { + { + { + "oh:t=c;o=40dc992e;u=0;a=0", + "Hellenic Academic and Research Institutions Cert. Authority", + true, + "CN=Hellenic Academic and Research Institutions RootCA 2011,OU=,O=Hellenic Academic and Research Institutions Cert. Authority", + "CN=Hellenic Academic and Research Institutions RootCA 2011,OU=,O=Hellenic Academic and Research Institutions Cert. Authority", + "0", + "2011-12-6", + "2031-12-1", + "BC:10:4F:15:A4:8B:E7:09:DC:A5:42:A7:E1:D4:B9:DF:6F:05:45:27:E8:02:EA:A9:2D:59:54:44:25:8A:FE:71" + }, + true + }, +}; + + +class CmHiSysEventTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void CmHiSysEventTest::SetUpTestCase(void) +{ +} + +void CmHiSysEventTest::TearDownTestCase(void) +{ +} + +void CmHiSysEventTest::SetUp() +{ +} + +void CmHiSysEventTest::TearDown() +{ +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest001 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmInstallAppCert'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest001, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + uint32_t store = CM_CREDENTIAL_STORE; + uint8_t appCertPwdBuf[] = "123456"; + uint8_t certAliasBuf[] = "keyA"; + uint8_t keyUriBuf[MAX_LEN_URI] = {0}; + struct CmBlob keyUri = { MAX_LEN_URI, keyUriBuf }; + + struct CmBlob appCert = { sizeof(g_p12AbnormalCertinfo), (uint8_t*)g_p12AbnormalCertinfo }; + struct CmBlob appCertPwd = { sizeof(appCertPwdBuf), appCertPwdBuf }; + struct CmBlob certAlias = { sizeof(certAliasBuf), certAliasBuf }; + + (void)CmInstallAppCert(&appCert, &appCertPwd, &certAlias, store, &keyUri); + ret = CmHiSysEventQueryResult("CmInstallAppCert"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; + +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest002 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceInstallUserCert'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest002, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + uint8_t certAliasBuf[] = "abnormal-invalid-certdata"; + uint8_t certUriBuf[MAX_URI_LEN] = {0}; + + struct CmBlob userCertTemp = { sizeof(g_certData04), (uint8_t *)g_certData04 }; /* invalid certData */ + struct CmBlob certAliasTemp = { sizeof(certAliasBuf), certAliasBuf }; + struct CmBlob certUriTemp = { sizeof(certUriBuf), certUriBuf }; + + (void)CmInstallUserTrustedCert(&userCertTemp, &certAliasTemp, &certUriTemp); + ret = CmHiSysEventQueryResult("CmIpcServiceInstallUserCert"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; + +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest003 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceGetUserCertList'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest003, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + + struct CertList *cList = nullptr; + InitUserCertList(&cList); + (void)CmGetUserCertList(100, cList); /* invalid store 100 */ + FreeCertList(cList); + + ret = CmHiSysEventQueryResult("CmIpcServiceGetUserCertList"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest004 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceGetUserCertInfo'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest004, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + char *uri = g_certInfoExpectResult[0].certInfo.uri; + struct CmBlob certUri = { sizeof(uri), (uint8_t *)uri }; + + struct CertInfo *cInfo = nullptr; + InitUserCertInfo(&cInfo); + (void)CmGetUserCertInfo(&certUri, 100, cInfo); /* invalid store 100 */ + + ret = CmHiSysEventQueryResult("CmIpcServiceGetUserCertInfo"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; + + FreeCMBlobData(&(cInfo->certInfo)); +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest005 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceUninstallUserCert'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest005, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + uint8_t certUriBuf[MAX_URI_LEN] = {0}; + struct CmBlob certUriTemp = { sizeof(certUriBuf), certUriBuf }; + + (void)CmUninstallUserTrustedCert(&certUriTemp); + ret = CmHiSysEventQueryResult("CmIpcServiceUninstallUserCert"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest006 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceGrantAppCertificate'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest006, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + uint8_t uriData[] = "oh:t=ak;o=keyA;u=0"; + struct CmBlob keyUri = { sizeof(uriData), uriData }; + + uint8_t authUriData[DEFAULT_AUTH_URI_LEN] = {0}; + struct CmBlob authUri = { DEFAULT_AUTH_URI_LEN, authUriData }; + + uint32_t appId = DEFAULT_APP_ID; + + (void)CmGrantAppCertificate(&keyUri, appId, &authUri); + + ret = CmHiSysEventQueryResult("CmIpcServiceGrantAppCertificate"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest007 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceIsAuthorizedApp'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest007, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + int32_t ret; + + /* authUri macData size 31 */ + uint8_t uriDataFail[] = + "oh:t=ak;o=TestNormalGrant;u=0;a=0?ca=0&m=BA632421B76F1059BC28184FB9E50D5795232B6D5C535E0DCAC0114A7AD8FA"; + struct CmBlob authUriFail = { sizeof(uriDataFail), uriDataFail }; + (void)CmIsAuthorizedApp(&authUriFail); + + ret = CmHiSysEventQueryResult("CmIpcServiceIsAuthorizedApp"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret; +} + +/** + * @tc.name: CmHiSysEventTest.CmHiSysEventTest008 + * @tc.desc: the abnormal test is for hisysevent; + the test interface is 'CmIpcServiceRemoveGrantedApp'. + * @tc.type: FUNC + * @tc.require: AR000HE22G /SR000HDQVV + */ +HWTEST_F(CmHiSysEventTest, CmHiSysEventTest008, TestSize.Level0) +{ + CmHiSysEventQueryStart(); + uint8_t uriData[] = "oh:t=ak;o=keyA;u=0;a=0"; + struct CmBlob keyUri = { strlen((char *)uriData), uriData }; + uint32_t appId = 0; + int32_t ret = CmRemoveGrantedApp(&keyUri, appId); + + ret = CmHiSysEventQueryResult("CmIpcServiceRemoveGrantedApp"); + EXPECT_EQ(ret, CM_HISYSEVENT_QUERY_SUCCESS) << "query failed, ret = " << ret;} + +} \ No newline at end of file diff --git a/test/hisysevent_test/src/cm_hisysevent_test_common.cpp b/test/hisysevent_test/src/cm_hisysevent_test_common.cpp new file mode 100644 index 0000000..3295474 --- /dev/null +++ b/test/hisysevent_test/src/cm_hisysevent_test_common.cpp @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cm_hisysevent_test_common.h" + +#include +#include +#include + +#include "hisysevent_manager.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" + +using namespace std; + +static const int MAX_QUERY_EVENT_COUNT = 1000; +static const int TIME_S_TO_MS = 1000; +static const int TIME_MS_TO_US = 1000; + +static long long int g_beginTime = 0; +static long long int g_endTime = 0; +static volatile bool g_queryResult = false; +static string g_queryStr; + +namespace OHOS { +namespace HiviewDFX { +class CmHiSysEventCallBack : public OHOS::HiviewDFX::HiSysEventQueryCallback { +public: + CmHiSysEventCallBack() {} + virtual ~CmHiSysEventCallBack() {} + void OnQuery(std::shared_ptr> sysEvents); + void OnComplete(int32_t reason, int32_t total); +}; + +void CmHiSysEventCallBack::OnQuery(std::shared_ptr> sysEvents) +{ + if (g_queryStr.size() == 0 || sysEvents == nullptr) { + return; + } + for_each((*sysEvents).cbegin(), (*sysEvents).cend(), [](const HiSysEventRecord& tmp) { + string::size_type idx = tmp.AsJson().find(g_queryStr); + if (idx != string::npos) { + g_queryResult = true; + } + }); + return; +} + +void CmHiSysEventCallBack::OnComplete(int32_t reason, int32_t total) +{ + return; +} +} // namespace HiviewDFX +} // namespace OHOS + +using namespace OHOS::HiviewDFX; + +static long long int GetCurrentTime(void) +{ + struct timeval tv; + (void)gettimeofday(&tv, nullptr); + long long int timeStamp = tv.tv_sec * TIME_S_TO_MS + tv.tv_usec / TIME_MS_TO_US; + return timeStamp; +} + +void CmHiSysEventQueryStart(void) +{ + g_beginTime = GetCurrentTime(); + g_endTime = 0; +} + +int32_t CmHiSysEventQueryResult(const string funStr) +{ + if (g_beginTime == 0) { + return CM_HISYSEVENT_QUERY_FAILED; + } + + g_queryResult = false; + g_queryStr = funStr; + + sleep(2); // Waiting for hisysevent to upload + + // queryArg + g_endTime = GetCurrentTime(); + struct QueryArg args(g_beginTime, g_endTime, MAX_QUERY_EVENT_COUNT); + + // queryRules + string domain = "CERT_MANAGER"; + vector eventList; + eventList.push_back("CERT_FAULT"); + QueryRule rule(domain, eventList); + vector queryRules; + queryRules.push_back(rule); + + // queryCallback + auto queryCallBack = std::make_shared(); + if (HiSysEventManager::Query(args, queryRules, queryCallBack) == 0) { + CM_LOG_I("CmHiSysEventQueryResult01"); + if (g_queryResult) { + CM_LOG_I("CmHiSysEventQueryResult02"); + return CM_HISYSEVENT_QUERY_SUCCESS; + } + return CM_HISYSEVENT_QUERY_FAILED; + } + CM_LOG_I("CmHiSysEventQueryResult03"); + + return CM_HISYSEVENT_QUERY_FAILED; +} + +void FreeCMBlobData(struct CmBlob *blob) +{ + if (blob == nullptr) { + return; + } + + if (blob->data != nullptr) { + CmFree(blob->data); + blob->data = nullptr; + } + blob->size = 0; +} + +uint32_t InitUserCertInfo(struct CertInfo **cInfo) +{ + *cInfo = (struct CertInfo *)CmMalloc(sizeof(struct CertInfo)); + if (*cInfo == nullptr) { + return CMR_ERROR_MALLOC_FAIL; + } + (void)memset_s(*cInfo, sizeof(struct CertInfo), 0, sizeof(struct CertInfo)); + + (*cInfo)->certInfo.data = (uint8_t *)CmMalloc(MAX_LEN_CERTIFICATE); + if ((*cInfo)->certInfo.data == NULL) { + return CMR_ERROR_MALLOC_FAIL; + } + (*cInfo)->certInfo.size = MAX_LEN_CERTIFICATE; + + return CM_SUCCESS; +} + +uint32_t InitUserCertList(struct CertList **cList) +{ + *cList = (struct CertList *)CmMalloc(sizeof(struct CertList)); + if (*cList == nullptr) { + return CMR_ERROR_MALLOC_FAIL; + } + + uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CertAbstract); + (*cList)->certAbstract = (struct CertAbstract *)CmMalloc(buffSize); + if ((*cList)->certAbstract == NULL) { + return CMR_ERROR_MALLOC_FAIL; + } + (void)memset_s((*cList)->certAbstract, buffSize, 0, buffSize); + (*cList)->certsCount = MAX_COUNT_CERTIFICATE; + + return CM_SUCCESS; +} + +void FreeCertList(struct CertList *certList) +{ + if (certList == nullptr || certList->certAbstract == nullptr) { + return; + } + + CmFree(certList->certAbstract); + certList->certAbstract = nullptr; + + CmFree(certList); + certList = nullptr; +} \ No newline at end of file diff --git a/test/unittest/src/cm_test_common.cpp b/test/unittest/src/cm_test_common.cpp index 47a19fe..df50edb 100644 --- a/test/unittest/src/cm_test_common.cpp +++ b/test/unittest/src/cm_test_common.cpp @@ -129,6 +129,7 @@ bool CompareCert(const struct CertAbstract *firstCert, const struct CertAbstract { if (firstCert == nullptr || secondCert == nullptr) { CM_TEST_LOG_E("cert invalid parameter"); + return false; } return ((strcmp(firstCert->uri, secondCert->uri) == 0) && (strcmp(firstCert->certAlias, secondCert->certAlias) == 0) && @@ -140,6 +141,7 @@ bool CompareCredentialList(const struct CredentialAbstract *firstCert, const str { if (firstCert == nullptr || secondCert == nullptr) { CM_TEST_LOG_E("cert invalid parameter"); + return false; } return ((strcmp(firstCert->type, secondCert->type) == 0) && (strcmp(firstCert->alias, secondCert->alias) == 0) && -- Gitee From 9f67e440bd7bf9a79b74cf7b4a80c3480940b6cf Mon Sep 17 00:00:00 2001 From: huweishuang Date: Sun, 9 Oct 2022 16:15:47 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=AF=81=E4=B9=A6=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=A4=A7=E6=95=B0=E6=8D=AE=E6=89=93=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huweishuang --- .../main/os_dependency/idl/cm_ipc/cm_ipc_service.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c index d6912a0..f22bff8 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c @@ -1527,7 +1527,7 @@ void CmIpcServiceGetUserCertList(const struct CmBlob *paramSetBlob, struct CmBlo CmSendResponse(context, ret, outData); } while (0); - CmReport(__func__, &cmContext, "certName", ret); + CmReport(__func__, &cmContext, "NULL", ret); if (ret != CM_SUCCESS) { CmSendResponse(context, ret, NULL); -- Gitee