[Huawei Developers] John Wo:
Easy-to-Integrate Huawei System Integrity Check – Helping You Protect Your Apps from Unsafe Devices

Running apps on an unsafe device, for example, a rooted or unlocked device, can pose serious security risks. On an unsafe device, viruses, Trojan horses, and other malicious software may exploit the root permission to implant viruses, tamper with device information, and damage the system. Therefore, there is a real need to protect your apps from being run on unsafe devices. To meet such a need, Huawei offers Safety Detect, which provides the SysIntegrity API for system integrity check. This API can check whether a device running your app is unsafe, for example, due to the device being rooted or unlocked.

I. Service Introduction

The SysIntegrity API has the following features:

1. TEE-based system integrity check results: The API evaluates the integrity of a system during its secure boot in the Trusted Execution Environment (TEE) and will dynamically evaluate the system integrity. The check results are highly reliable.

2. Secure and reliable system integrity check results: The system integrity check results are signed using the signing certificate and cannot be tampered with.

(1) The Safety Detect SDK integrated with your app calls the SysIntegrity API.

(2) Safety Detect requests the signed check results from the Trusted Security Management Service (TSMS) server.

(3) Your app requests the check results.

II. Scenarios

Currently, developers of apps in the finance, entertainment, lifestyle, news, and other categories have integrated the SysIntegrity API into their apps.

A financial app can integrate SysIntegrity to improve transaction security. For example, the app can check whether the device is secure when a user enters the Card Verification Code (CVC) of their credit card. If the device is unsafe, the app will be stopped on the device to ensure no unsafe transactions can occur.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112658.71317469538729582233401498662127:50511117083200:2800:5566C5D38F07C4DFDE3C79C113BE5A24AB9730CA84193266D61949AFCFE06374.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112658.71317469538729582233401498662127:50511117083200:2800:5566C5D38F07C4DFDE3C79C113BE5A24AB9730CA84193266D61949AFCFE06374.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112658.24539066693882871947945719370609:50511117083200:2800:8B818249DB3A888FD5B191CAD7DA1EFCC12FDDA4CC6A27D0E095AD9C004F2DBC.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112658.24539066693882871947945719370609:50511117083200:2800:8B818249DB3A888FD5B191CAD7DA1EFCC12FDDA4CC6A27D0E095AD9C004F2DBC.jpg[/IMG]

A lifestyle or news app can integrate SysIntegrity to prevent malicious attacks, ensuring security of in-app operations such as payment.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112722.26648751219563357668548769711358:50511117083200:2800:36447352DDA8B3D02FC885C049F408E252F25ED4D174002C4A3F9E35E99DA2A8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112722.26648751219563357668548769711358:50511117083200:2800:36447352DDA8B3D02FC885C049F408E252F25ED4D174002C4A3F9E35E99DA2A8.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112722.54589857218987595037126128247489:50511117083200:2800:6D4BF7BAE227BB086F6E076BD7F3A6E55551072A9D1DD60DFAA798778CD5297E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112722.54589857218987595037126128247489:50511117083200:2800:6D4BF7BAE227BB086F6E076BD7F3A6E55551072A9D1DD60DFAA798778CD5297E.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112722.07924838278658882037042892383511:50511117083200:2800:979C459FC753E7D19B16CB69475E2AF246173CCCE68131EE680C4C257D89E030.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112722.07924838278658882037042892383511:50511117083200:2800:979C459FC753E7D19B16CB69475E2AF246173CCCE68131EE680C4C257D89E030.png[/IMG]

A video entertainment app can integrate SysIntegrity to help protect copyrighted content. When a user registers an account on the app and watches or downloads video content, SysIntegrity can help ensure that this being done on a device that is trusted by the content provider.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112832.29451949116355283909365961631412:50511117083200:2800:C1D82F8A50AB3C73D153DB916D103F15E8558E62CCE2DAB78566C018FB531773.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110112832.29451949116355283909365961631412:50511117083200:2800:C1D82F8A50AB3C73D153DB916D103F15E8558E62CCE2DAB78566C018FB531773.jpg[/IMG]

III. Code Development

1 Configuring App Information in AppGallery Connect

Before you start developing an app, configure app information in AppGallery Connect.

For details, please visit https://developer.huawei.com/consumer/en/doc/HMSCore-Guides-V5/config-agc-0000001050416303-V5?ha_source=hms1

2 Configuring the Maven Repository Address for the HMS Core SDK

2.1 Open the build.gradle file in the root directory of your Android Studio project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110113136.67948066647981953543137759213354:50511117083200:2800:1DAE4285587F8D3D9D08B70C1B519C3426256D2CA814E8DB441CE3B09F988556.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201110113136.67948066647981953543137759213354:50511117083200:2800:1DAE4285587F8D3D9D08B70C1B519C3426256D2CA814E8DB441CE3B09F988556.png[/IMG]

2.2 Add the AppGallery connect plug-in and the Maven repository.

Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.

allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.

buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

Go to buildscript > dependencies and add dependency configurations.

buildscript{
dependencies {
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
}
}

3 Creating a SafetyDetectClient Instance and Generating a Nonce Value

// Create a SafetyDetectClient instance.
SafetyDetectClient mClient = SafetyDetect.getClient(MainActivity.this);
// Generate a nonce value.
byte[] nonce = new byte[24];
try {
SecureRandom random;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
random = SecureRandom.getInstanceStrong();
} else {
random = SecureRandom.getInstance("SHA1PRNG");
}
random.nextBytes(nonce);
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, e.getMessage());
}

4 Creating a Check Result Listener

// Implement the OnSuccessListener API and obtain the check result from onSuccess
protected class SysIntegrityOnSuccessListener implements OnSuccessListener<SysIntegrityResp> {

// Obtain the system integrity check result.
@Override
public void onSuccess(SysIntegrityResp sysIntegrityResp) {

}

}
// Implement the OnFailureListener API and obtain the failure details from onFailure.
protected class SysIntegrityOnFailureListener implements OnFailureListener {
// Obtain the result code and description.
@Override
public void onFailure(Exception e) {

}
}

5 Calling the SysIntegrity API

// Call the SysIntegrity API. Replace ******** in the following code with your app ID.
Task task = mClient.sysIntegrity(nonce,"********");
task.addOnSuccessListener(new SysIntegrityOnSuccessListener()).addOnFailureListener(new SysIntegrityOnFailureListener());

6 Verifying the Check Results

For details about how to verify the check results on the server, please refer to relevant documents on the HUAWEI Developers website.

To find out more, visit the following links:

SysIntegrity API:

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/dysintegritydevelopment-0000001050156331?ha_source=hms1

Safety Detect development guide:

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050156325?ha_source=hms1

Demo and sample code on GitHub:

https://github.com/HMS-Core/hms-safetydetect-demo-android

Stack Overflow (for technical discussions):

https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest