Since version 26.0.0, the RiskControlEngine is supported.
The RiskControlEngine is a risk control solution running in the on-device confidential computing space. Based on the system open risk factors and app-specific risk factors, the engine helps customize risk identification policies and rules in a securely isolated high-security trusted environment, efficiently completing risk analysis.
The confidential computing environment ensures that data is calculable but not retrievable, knowable but not visible, and that risk control policies and rules are available but not disclosed. The RiskControlEngine provides flexible, autonomous, and rich policy rule configuration capabilities, as well as an inter-app collaborative defense and control mechanism for apps connected to the RiskControlEngine.
For example, in telecom anti-fraud scenarios, an app can use the RiskControlEngine to identify fraud risks in payment/bank transfer scenarios. The process is as follows:

Process description:
The atomic service app obtains the nonce value.
When calling the importRiskFactors and getRiskControlResult APIs, the atomic service app needs to pass a randomly generated nonce value. The returned detection result contains the nonce value. You can verify this value to confirm the mapping between the response and the request, and prevent replay attacks.
The atomic service app imports the app risk factor data.
After receiving the request, Device Security Kit writes the app risk factor data into the RiskControlEngine. The data can be used together with system risk factors for risk control calculation.
The atomic service app can also skip the step of importing risk factors and directly call the getRiskControlResult API to initiate a risk control scoring request.
The atomic service app initiates a risk control scoring request.
After receiving the request, Device Security Kit calls the RiskControlEngine. The risk control engine collects risk control data, calculates the risk control score, and returns the comprehensive risk control score to the atomic service app through the getRiskControlResult API.
The atomic service app verifies the result on the app server.
After receiving the risk control scoring result, the atomic service app parses the return value in JSON Web Signature (JWS) format to verify the signature, and then performs risk control based on the result of each risk control factor in the payload.
The following table lists the APIs related to the RiskControlEngine. For more information about the APIs, please refer to API Reference.
| API | Description |
|---|---|
| importRiskFactors(data: ImportData): Promise<void> | Imports app risk factor data. |
| getRiskControlResult(params: RiskControlDetectionRequest): Promise<RiskControlDetectionResponse> | Risk control scoring result. |
Import the Device Security Kit module and related public modules.
import { riskControlEngine } from '@kit.DeviceSecurityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { util } from '@kit.ArkTS'; Call the importRiskFactors API to import app risk factor data.
const TAG = 'riskControlEngineJsTest';
let rand = cryptoFramework.createRandom();
let len = 32;
let randData = rand.generateRandomSync(len);
let base64 = new util.Base64Helper();
// Import app risk factor data.
let data: riskControlEngine.ImportData = {
appFactorData: [
{ factorName: 'factor_1', factorValue: 3600 },
{ factorName: 'factor_2', factorValue: false }
],
nonce: base64.encodeToStringSync(randData.data) // Random number of 16 to 66 bytes
};
try {
hilog.info(0x0000, TAG, 'ImportRiskFactors begin.');
await riskControlEngine.importRiskFactors(data);
hilog.info(0x0000, TAG, 'Succeeded in importRiskFactors.');
} catch (err) {
let e: BusinessError = err as BusinessError;
hilog.error(0x0000, TAG, 'ImportRiskFactors failed: %{public}d %{public}s', e.code, e.message);
} Call the getRiskControlResult API to obtain the risk control score.
const TAG = 'riskControlEngineJsTest';
let rand = cryptoFramework.createRandom();
let len = 32;
let randData = rand.generateRandomSync(len);
let base64 = new util.Base64Helper();
// Prepare a risk control detection request.
const request: riskControlEngine.RiskControlDetectionRequest = {
policyName: 'Policy_1001', // Risk policy
nonce: base64.encodeToStringSync(randData.data) // Random number of 16 to 66 bytes
};
try {
hilog.info(0x0000, TAG, 'Getting risk control score begin.');
const response: riskControlEngine.RiskControlDetectionResponse =
await riskControlEngine.getRiskControlResult(request);
// The result is in JWS format and needs to be parsed and verified according to the specifications.
hilog.info(0x0000, TAG, 'Risk control score result: %{public}s', response.result);
} catch (err) {
const e: BusinessError = err as BusinessError;
hilog.error(0x0000, TAG, 'GetRiskControlScore failed: %{public}d %{public}s', e.code, e.message);
} Verify the result on the atomic service app server.
The result returned by the risk control API is a JWS string. The procedure is as follows:
Parse the JWS string to obtain the header, payload, and signature of the risk control scoring result.
Fields in JWS-format Header are as follows:
{
"alg": "ES256", // Digital signature algorithm. ES256 indicates SHA256withECDSA.
"typ": "JWS", // The value is fixed to JWS.
"x5c": ["","",""] // Certificate chain signed by the Device Security Kit server for the JWS, which contains three levels of certificates. x5c[0] is the certificate signed for the JWS, x5c[1] is the level-2 CA certificate for Huawei devices, and x5c[2] is the root CA certificate for Huawei devices.
} Fields in JWS-format Payload are as follows:
{
"nonce": "xxxxxxxxx", // Nonce value encoded in Base64 format and transferred when the getRiskControlResult API is called
"RiskDetectionOutput": {
"status": 0, // Status code
"result": "1" // Risk control scoring result
}
} Obtain the certificate chain from Header and verify it using the Huawei CBG Device Attestation Root CA certificate.
Verify that the certificate chain contains three levels of certificates and Common Name of the x5c[0] certificate is Harmony OS Device Attestation Service.