• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.net.networkSecurity (Network Security)
2
3本模块提供网络安全校验能力。应用可以通过证书链校验API完成证书链校验功能。
4
5> **说明:**
6>
7> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import networkSecurity from '@ohos.net.networkSecurity';
13```
14
15## 完整实例
16
17```ts
18import networkSecurity from '@ohos.net.networkSecurity';
19
20// Define certificate blobs
21const cert = {
22  type: networkSecurity.CertType.CERT_TYPE_PEM,
23  data: '-----BEGIN CERTIFICATE-----\n... (certificate data) ...\n-----END CERTIFICATE-----',
24};
25
26const caCert = {
27  type: networkSecurity.CertType.CERT_TYPE_PEM,
28  data: '-----BEGIN CERTIFICATE-----\n... (CA certificate data) ...\n-----END CERTIFICATE-----',
29};
30
31// Perform asynchronous certificate verification
32networkSecurity.certVerification(cert, caCert)
33  .then((result) => {
34    console.info('Certificate verification result:', result);
35  })
36  .catch((error) => {
37    console.error('Certificate verification failed:', error);
38  });
39```
40
41> **注意**:请务必将示例中的证书数据替换为实际的证书内容。
42
43## CertType
44
45证书编码类型。
46
47**系统能力**: SystemCapability.Communication.NetStack
48
49| 名称          | 值    |      说明     |
50| ------------- | ----- | ------------- |
51| CERT_TYPE_PEM | 0     | PEM格式证书。 |
52| CERT_TYPE_DER | 1     | DER格式证书。 |
53
54
55## CertBlob
56
57证书数据。
58
59**系统能力**: SystemCapability.Communication.NetStack
60
61| 名称  | 类型                   | 必填      | 说明           |
62| ----- | --------------------- | --------- | -------------- |
63| type  | CertType              | Yes       | 证书编码类型。  |
64| data  | string \| ArrayBuffer | Yes       | 证书内容。      |
65
66
67## networkSecurity.certVerification
68
69certVerification(cert: CertBlob, caCert?: CertBlob): Promise\<number\>
70
71从证书管理获取系统预置的CA证书和用户安装的CA证书,对应用传入的证书链进行校验。
72
73**系统能力**: SystemCapability.Communication.NetStack
74
75**参数**
76
77| 参数名 | 类型     | 必填 | 说明                   |
78| ------ | -------- | ---- | ---------------------- |
79| cert   | CertBlob | 是   | 被校验的证书链。       |
80| caCert | CertBlob | 否   | 传入自定义的CA证书链。 |
81
82**返回值:**
83
84| 类型            | 说明                                                         |
85| --------------- | ------------------------------------------------------------ |
86| Promise\<number\> | 以promise形式返回一个数字,表示证书验证的结果。如果证书验证成功,则返回0; 否则验证失败。 |
87
88**错误码:**
89
90| 错误码ID | 错误信息                                             |
91| -------- | ---------------------------------------------------- |
92| 401      | Parameter error.                                     |
93| 2305001  | Unspecified error.                                   |
94| 2305002  | Unable to get issuer certificate.                    |
95| 2305003  | Unable to get certificate revocation list (CRL).     |
96| 2305004  | Unable to decrypt certificate signature.             |
97| 2305005  | Unable to decrypt CRL signature.                     |
98| 2305006  | Unable to decode issuer public key.                  |
99| 2305007  | Certificate signature failure.                       |
100| 2305008  | CRL signature failure.                               |
101| 2305009  | Certificate is not yet valid.                        |
102| 2305010  | Certificate has expired.                             |
103| 2305011  | CRL is not yet valid.                                |
104| 2305012  | CRL has expired.                                     |
105| 2305023  | Certificate has been revoked.                        |
106| 2305024  | Invalid certificate authority (CA).                  |
107| 2305027  | Certificate is untrusted.                            |
108
109> **说明:**这些错误代码对应于证书验证过程中的各种失败,提供有关所遇到问题的详细信息。
110
111**示例:**
112
113```ts
114import networkSecurity from '@ohos.net.networkSecurity';
115
116// Define certificate blobs
117const cert = {
118  type: networkSecurity.CertType.CERT_TYPE_PEM,
119  data: '-----BEGIN CERTIFICATE-----\n... (certificate data) ...\n-----END CERTIFICATE-----',
120};
121
122const caCert = {
123  type: networkSecurity.CertType.CERT_TYPE_PEM,
124  data: '-----BEGIN CERTIFICATE-----\n... (CA certificate data) ...\n-----END CERTIFICATE-----',
125};
126
127// Perform asynchronous certificate verification
128networkSecurity.certVerification(cert, caCert)
129  .then((result) => {
130    console.info('Certificate verification result:', result);
131  })
132  .catch((error) => {
133    console.error('Certificate verification failed:', error);
134  });
135```
136> **注意**:请务必将示例中的证书数据替换为实际的证书内容。
137
138
139
140## networkSecurity.certVerificationSync
141
142certVerificationSync(cert: CertBlob, caCert?: CertBlob): number
143
144从证书管理获取系统预置的CA证书和用户安装的CA证书,对应用传入的证书链进行校验。
145
146**系统能力**:SystemCapability.Communication.NetStack
147
148**参数**:
149
150| 参数名 | 类型     | 必填 | 说明                   |
151| ------ | -------- | ---- | ---------------------- |
152| cert   | CertBlob | 是  | 被校验的证书链。       |
153| caCert | CertBlob | 否   | 传入自定义的CA证书链。 |
154
155**返回值:**
156
157| 类型   | 说明                                                         |
158| ------ | ------------------------------------------------------------ |
159| number | 表示证书链验证的结果。如果证书链验证成功,则返回0; 否则验证失败。 |
160
161**错误码:**
162
163| 错误码ID | 错误信息                                             |
164| -------- | ---------------------------------------------------- |
165| 401      | Parameter error.                                     |
166| 2305001  | Unspecified error.                                   |
167| 2305002  | Unable to get issuer certificate.                    |
168| 2305003  | Unable to get certificate revocation list (CRL).     |
169| 2305004  | Unable to decrypt certificate signature.             |
170| 2305005  | Unable to decrypt CRL signature.                     |
171| 2305006  | Unable to decode issuer public key.                  |
172| 2305007  | Certificate signature failure.                       |
173| 2305008  | CRL signature failure.                               |
174| 2305009  | Certificate is not yet valid.                        |
175| 2305010  | Certificate has expired.                             |
176| 2305011  | CRL is not yet valid.                                |
177| 2305012  | CRL has expired.                                     |
178| 2305023  | Certificate has been revoked.                        |
179| 2305024  | Invalid certificate authority (CA).                  |
180| 2305027  | Certificate is untrusted.                            |
181
182> **说明:**这些错误代码对应于证书验证过程中的各种失败,提供有关所遇到问题的详细信息。
183
184**示例:**
185
186```ts
187import networkSecurity from '@ohos.net.networkSecurity';
188
189// Create certificate blobs
190const cert: networkSecurity.CertBlob = {
191  type: networkSecurity.CertType.CERT_TYPE_PEM,
192  data: '-----BEGIN CERTIFICATE-----\n...'
193};
194
195const caCert: networkSecurity.CertBlob = {
196  type: networkSecurity.CertType.CERT_TYPE_PEM,
197  data: '-----BEGIN CERTIFICATE-----\n...'
198};
199
200// Asynchronous verification
201networkSecurity.certVerification(cert, caCert)
202  .then((result) => {
203    console.info('Verification Result:', result);
204  })
205  .catch((error) => {
206    console.error('Verification Error:', error);
207  });
208
209// Synchronous verification
210let resultSync :number = networkSecurity.certVerificationSync(cert, caCert);
211console.info('Synchronous Verification Result:', resultSync);
212```
213
214> **注意**:请务必将示例中的证书数据替换为实际的证书内容。
215