目录

Code Signature Information Query (C/C++)

Starting from 6.1.1(24), the file code signature information query API is supported, allowing you to obtain the signature information of signed files on a device.

Use Cases

The signature information includes the app ID, issuing organization certificate chain, signature digest, signature timestamp, and the hash algorithm used for the signing. Through the HMS_SecurityAudit_AcquireCodeSign API, apps can obtain code signature information to help assess the integrity and security of the code being executed. This helps prevent the execution of malicious software and enhances the device's security capabilities.

Constraints

  1. Currently, this capability is supported only on PCs/2-in-1 devices.
  2. Apps that call the HMS_SecurityAudit_AcquireCodeSign API must have permission to read the target code signature file.

Service Process

Process description:

  1. Your app calls the HMS_SecurityAudit_AcquireCodeSign API to query code signature information of a file.
  2. The HMS_SecurityAudit_AcquireCodeSign API synchronously returns the code signature information of the file.

API Description

The following table lists the APIs. For details about the APIs, please refer to API Reference.

API Description
int32_t HMS_SecurityAudit_AcquireCodeSign(char* path, char** outOwnedResult) Obtains the code signature information of the file specified by the input file path.

Development Procedure

When preparing for the development, you need to apply for the ohos.permission.QUERY_AUDIT_EVENT permission. Only enterprise apps in the trustlist can apply for this permission. For details about how to apply for this permission, please refer to Permissions for Enterprise Applications.

  1. Import and link the security audit shared library in CMakeLists.txt.

    find_library(dsm-lib libsecurityaudit_ndk.z.so)
    target_link_libraries(entry PUBLIC libace_napi.z.so ${dsm-lib})
  2. Import the security audit header file.

    #include <cstdio>
    #include "DeviceSecurityKit/security_audit.h"
  3. Call the HMS_SecurityAudit_AcquireCodeSign API to obtain the code signature information of the input file. After the returned code signature information is processed, release the memory allocated for the input and output parameters.

    char *result = nullptr;
    const char *path = "test";
    // ...
    int32_t ret = HMS_SecurityAudit_AcquireCodeSign(const_cast<char*>(path), &result);
    if (ret == 0 && result != nullptr) {
        printf("HMS_SecurityAudit_AcquireCodeSign result: %s\n", result);
    } else {
        printf("HMS_SecurityAudit_AcquireCodeSign failed with error: %d\n", ret);
    }
    // ...
    if (result != nullptr) {
        delete[] result;
        result = nullptr;
    }