1# @ohos.deviceAttest (设备证明)(系统接口)
2
3为了证明设备是OpenHarmony生态中的合法设备,设备证明模块会把设备信息通过云端进行一致性合法校验。
4通过本模块接口,可查询设备在云端校验的结果。
5
6> **说明:**
7>
8> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9>
10> - 本模块接口为系统接口。
11
12## 导入模块
13
14```ts
15import deviceAttest from '@ohos.deviceAttest';
16```
17
18## deviceAttest.getAttestStatus
19
20getAttestStatus(callback: AsyncCallback<AttestResultInfo>) : void
21
22获取端云校验结果的详细信息。使用callback异步回调。
23
24**系统能力:** SystemCapability.XTS.DeviceAttest
25
26**参数:**
27
28| 参数名   | 类型                                                        | 必填 | 说明                                                         |
29| -------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ |
30| callback | AsyncCallback<[AttestResultInfo](#attestresultinfo)> | 是   | 回调函数。当获取端云校验结果的详细信息成功,error为undefined,result为获取到的[AttestResultInfo](#attestresultinfo);否则为错误对象。 |
31
32**错误码:**
33以下错误码的详细介绍请参见[设备证明错误码](./errorcode-deviceAttest.md)和[通用错误码说明文档](../errorcode-universal.md)。
34| 错误码ID  | 错误信息             |
35|----------|----------------------|
36| 202     | This api is system api, Please use the system application to call this api. |
37| 401     | Input parameters wrong, the number of parameters is incorrect, or the type of parameters is incorrect. |
38| 20000001 | System service exception, please try again or reboot your device. |
39
40**示例:**
41
42```ts
43import base from '@ohos.base';
44
45try {
46    deviceAttest.getAttestStatus((error: base.BusinessError, value: deviceAttest.AttestResultInfo) => {
47    if (typeof error != 'undefined') {
48        console.info("error code:" + error.code + " message:" + error.message);
49    } else {
50        console.info("auth:" + value.authResult + " software:" + value.softwareResult + " ticket:" + value.ticket);
51        console.info("versionIdResult:" + value.softwareResultDetail[0],
52        " patchLevelResult:" + value.softwareResultDetail[1],
53        " rootHashResult:" + value.softwareResultDetail[2],
54        " PCIDResult:" + value.softwareResultDetail[3],
55        " reserver:" + value.softwareResultDetail[4]);
56    }
57    })
58} catch (error) {
59    let code: number = (error as base.BusinessError).code;
60    let message: string = (error as base.BusinessError).message;
61    console.info("error code:" + code + " message:" + message);
62}
63```
64
65## deviceAttest.getAttestStatus
66
67getAttestStatus() : Promise<AttestResultInfo>
68
69获取端云校验结果的详细信息。使用Promise异步回调。
70
71**系统能力:** SystemCapability.XTS.DeviceAttest
72
73**返回值:**
74
75| 类型                                                  | 说明                            |
76| ----------------------------------------------------- | ------------------------------- |
77| Promise<[AttestResultInfo](#attestresultinfo)> | Promise对象,返回端云校验结果的详细信息。 |
78
79**错误码:**
80以下错误码的详细介绍请参见[设备证明错误码](./errorcode-deviceAttest.md)和[通用错误码说明文档](../errorcode-universal.md)。
81| 错误码ID  | 错误信息             |
82|----------|----------------------|
83| 202     | This api is system api, Please use the system application to call this api. |
84| 401     | Input parameters wrong, the number of parameters is incorrect, or the type of parameters is incorrect. |
85| 20000001 | System service exception, please try again or reboot your device. |
86
87**示例:**
88
89```ts
90import base from '@ohos.base';
91
92try {
93    deviceAttest.getAttestStatus().then((value: deviceAttest.AttestResultInfo) => {
94    console.info("auth:" + value.authResult + " software:" + value.softwareResult + " ticket:" + value.ticket);
95    console.info("versionIdResult:" + value.softwareResultDetail[0],
96        " patchLevelResult:" + value.softwareResultDetail[1],
97        " rootHashResult:" + value.softwareResultDetail[2],
98        " PCIDResult:" + value.softwareResultDetail[3],
99        " reserver:" + value.softwareResultDetail[4]);
100    }).catch((error: base.BusinessError) => {
101        console.info("error code:" + error.code + " message:" + error.message);
102    });
103} catch (error) {
104    let code: number = (error as base.BusinessError).code;
105    let message: string = (error as base.BusinessError).message;
106    console.info("error code:" + code + " message:" + message);
107}
108```
109
110## deviceAttest.getAttestStatusSync
111
112getAttestStatusSync() : AttestResultInfo
113
114以同步方式获取端云校验结果的详细信息。
115
116**系统能力:** SystemCapability.XTS.DeviceAttest
117
118**返回值:**
119
120| 类型                                                  | 说明                            |
121| ----------------------------------------------------- | ------------------------------- |
122| [AttestResultInfo](#attestresultinfo) | 返回端云校验结果的详细信息。 |
123
124**错误码:**
125以下错误码的详细介绍请参见[设备证明错误码](./errorcode-deviceAttest.md)和[通用错误码说明文档](../errorcode-universal.md)。
126| 错误码ID  | 错误信息             |
127|----------|----------------------|
128| 202     | This api is system api, Please use the system application to call this api. |
129| 401     | Input parameters wrong, the number of parameters is incorrect, or the type of parameters is incorrect. |
130| 20000001 | System service exception, please try again or reboot your device. |
131
132**示例:**
133
134```ts
135import base from '@ohos.base';
136
137try {
138    let value: deviceAttest.AttestResultInfo = deviceAttest.getAttestStatusSync();
139    console.info("auth:" + value.authResult + " software:" + value.softwareResult + " ticket:" + value.ticket);
140    console.info("versionIdResult:" + value.softwareResultDetail[0],
141    " patchLevelResult:" + value.softwareResultDetail[1],
142    " rootHashResult:" + value.softwareResultDetail[2],
143    " PCIDResult:" + value.softwareResultDetail[3],
144    " reserver:" + value.softwareResultDetail[4]);
145} catch (error) {
146    let code: number = (error as base.BusinessError).code;
147    let message: string = (error as base.BusinessError).message;
148    console.info("error code:" + code + " message:" + message);
149}
150```
151
152## AttestResultInfo
153
154端云校验结果的详细信息。
155
156**系统能力:** SystemCapability.XTS.DeviceAttest
157
158| 名称                  | 类型                  | 可读 | 可写 | 说明                   |
159| --------------------- | --------------------- | ---- | ---- | ---------------------- |
160| authResult            | number               | 是   | 否   | 设备硬件信息校验结果。    |
161| softwareResult        | number               | 是   | 否   | 设备软件信息校验结果。    |
162| softwareResultDetail  | Array&lt;number&gt;  | 是   | 否   | 设备软件信息校验结果详细说明<br/> - softwareResultDetail[0]:版本Id的校验结果;<br/>- softwareResultDetail[1]:安全补丁标签的校验结果;<br/>- softwareResultDetail[2]:版本Hash的校验结果;<br/>- softwareResultDetail[3]:系统能力集合的校验结果;<br/>- softwareResultDetail[4]:保留位。  |
163| ticket                | string               | 是   | 否   | 云侧下发的软证书。<br/>设备硬件信息校验结果通过后有值;校验结果失败,该值为空        |
164
165> **说明:**
166>
167> - 设备硬件信息和设备软件信息的校验结果返回值有-2、-1、0。-2表示未认证,-1表示认证失败,0表示认证通过。
168