private void invokeSysIntegrity() {
SafetyDetectClient mClient = SafetyDetect.getClient(getActivity());
// TODO(developer): Change the nonce generation to include your own, used once value,
// ideally from your remote server.
byte[] nonce = ("Sample" + System.currentTimeMillis()).getBytes();
SysIntegrityRequest sysintegrityrequest = new SysIntegrityRequest();
sysintegrityrequest.setAppId("3*******");
sysintegrityrequest.setNonce(nonce);
sysintegrityrequest.setAlg(alg);
Task task = mClient.sysIntegrity(sysintegrityrequest);
task.addOnSuccessListener(new OnSuccessListener<SysIntegrityResp>() {
@Override
public void onSuccess(SysIntegrityResp response) {
// Indicates communication with the service was successful.
// Use response.getResult() to get the result data.
String jwsStr = response.getResult();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// An error occurred while communicating with the service.
if (e instanceof ApiException) {
// An error with the HMS API contains some
// additional details.
ApiException apiException = (ApiException) e;
// You can retrieve the status code using
// the apiException.getStatusCode() method.
Log.e(TAG, "Error: " + SafetyDetectStatusCodes.getStatusCodeString(apiException.getStatusCode()) + ": " + apiException.getMessage());
} else {
// A different, unknown type of error occurred.
Log.e(TAG, "ERROR:" + e.getMessage());
}
}
});
}
3. 在服务器中验证检测结果。
将验证结果发送到华为服务器校验证书的准确性并将完成校验的结果通过SysIntegrityResp返回,使用此对象的getResult方法获取响应,格式为JSON WEB签名(JWS)。
a. 解析JWS,获取header、payload、signature。
b. 从header中获取证书链,使用Huawei CBG Root CA证书对其进行验证。
c. 校验证书链中的叶证书域名,域名:sysintegrity.platform.hicloud.com。
d. 从signature中获取签名,校验其签名。
e. 从payload中获取完整性验证结果,格式和样例摘录如下: