openharmony 鸿蒙 js-apis-networkSecurity

2025-06-12 浏览 (1)

@ohos.net.networkSecurity (Network Security)

The networkSecurity module provides the network security verification capability. Specifically, it provides APIs for applications to verify the certificates in use.

NOTE

The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { networkSecurity } from '@kit.NetworkKit';

Sample Code

import { networkSecurity } from '@kit.NetworkKit';

// Define certificate blobs
const cert: networkSecurity.CertBlob = {
  type: networkSecurity.CertType.CERT_TYPE_PEM,
  data: '-----BEGIN CERTIFICATE-----\n... (certificate data) ...\n-----END CERTIFICATE-----',
};

const caCert: networkSecurity.CertBlob = {
  type: networkSecurity.CertType.CERT_TYPE_PEM,
  data: '-----BEGIN CERTIFICATE-----\n... (CA certificate data) ...\n-----END CERTIFICATE-----',
};

// Perform asynchronous certificate verification
networkSecurity.certVerification(cert, caCert)
  .then((result) => {
    console.info('Certificate verification result:', result);
  })
  .catch((error: BusinessError) => {
    console.error('Certificate verification failed:', error);
  });

NOTE

Be sure to replace the certificate data in the example with the actual certificate data.

CertType

Enumerates certificate types.

System capability: SystemCapability.Communication.NetStack

NameValueDescription
CERT_TYPE_PEM0PEM certificate
CERT_TYPE_DER1DER certificate.

CertBlob

Defines the certificate data.

System capability: SystemCapability.Communication.NetStack

NameTypeMandatoryDescription
typeCertTypeYesCertificate type.
datastring |ArrayBufferYesCertificate data.

networkSecurity.certVerification

certVerification(cert: CertBlob, caCert?: CertBlob): Promise<number>

Obtains the preset CA certificate and custom CA certificate from the certificate management module, and verifies the certificate passed by the application.

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
certCertBlobYesCertificate to be verified.
caCertCertBlobNoCustom CA certificate.

Return values:

TypeDescription
Promise<number>Promise used to return the result. The value 0 indicates that the certificate verification is successful, and a non-0 value indicates that the verification has failed.

Error codes

IDError Message
401Parameter error.
2305001Unspecified error.
2305002Unable to get issuer certificate.
2305003Unable to get certificate revocation list (CRL).
2305004Unable to decrypt certificate signature.
2305005Unable to decrypt CRL signature.
2305006Unable to decode issuer public key.
2305007Certificate signature failure.
2305008CRL signature failure.
2305009Certificate is not yet valid.
2305010Certificate has expired.
2305011CRL is not yet valid.
2305012CRL has expired.
2305018Self-signed certificate.
2305023Certificate has been revoked.
2305024Invalid certificate authority (CA).
2305027Certificate is untrusted.
2305069Invalid certificate verification context.

NOTE

If any of the preceding error codes is reported during certificate verification, rectify the error based on the detailed information about the error description.

Example

import { networkSecurity } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

// Define certificate blobs
const cert:networkSecurity.CertBlob = {
  type: networkSecurity.CertType.CERT_TYPE_PEM,
  data: '-----BEGIN CERTIFICATE-----\n... (certificate data) ...\n-----END CERTIFICATE-----',
};

const caCert:networkSecurity.CertBlob = {
  type: networkSecurity.CertType.CERT_TYPE_PEM,
  data: '-----BEGIN CERTIFICATE-----\n... (CA certificate data) ...\n-----END CERTIFICATE-----',
};

// Perform asynchronous certificate verification
networkSecurity.certVerification(cert, caCert)
  .then((result) => {
    console.info('Certificate verification result:', result);
  })
  .catch((error: BusinessError) => {
    console.error('Certificate verification failed:', error);
  });

NOTE

Be sure to replace the certificate data in the example with the actual certificate data.

networkSecurity.certVerificationSync

certVerificationSync(cert: CertBlob, caCert?: CertBlob): number

Obtains the preset CA certificate and custom CA certificate from the certificate management module, and verifies the certificate passed by the application.

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
certCertBlobYesCertificate to be verified.
caCertCertBlobNoCustom CA certificate.

Return values:

TypeDescription
numberCertificate verification result. The value 0 indicates that the certificate verification is successful, and a non-0 value indicates that the verification has failed.

Error codes

IDError Message
401Parameter error.
2305001Unspecified error.
2305002Unable to get issuer certificate.
2305003Unable to get certificate revocation list (CRL).
2305004Unable to decrypt certificate signature.
2305005Unable to decrypt CRL signature.
2305006Unable to decode issuer public key.
2305007Certificate signature failure.
2305008CRL signature failure.
2305009Certificate is not yet valid.
2305010Certificate has expired.
2305011CRL is not yet valid.
2305012CRL has expired.
2305018Self-signed certificate.
2305023Certificate has been revoked.
2305024Invalid certificate authority (CA).
2305027Certificate is untrusted.
2305069Invalid certificate verification context.

NOTE

If any of the preceding error codes is reported during certificate verification, rectify the error based on the detailed information about the error description.

Example

import { networkSecurity } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

// Create certificate blobs
const cert: networkSecurity.CertBlob = {
  type: networkSecurity.CertType.CERT_TYPE_PEM,
  data: '-----BEGIN CERTIFICATE-----\n...'
};

const caCert: networkSecurity.CertBlob = {
  type: networkSecurity.CertType.CERT_TYPE_PEM,
  data: '-----BEGIN CERTIFICATE-----\n...'
};

// Asynchronous verification
networkSecurity.certVerification(cert, caCert)
  .then((result) => {
    console.info('Verification Result:', result);
  })
  .catch((error: BusinessError) => {
    console.error('Verification Error:', error);
  });

// Synchronous verification
let resultSync: number = networkSecurity.certVerificationSync(cert, caCert);
console.info('Synchronous Verification Result:', resultSync);

NOTE

Be sure to replace the certificate data in the example with the actual certificate data.

networkSecurity.isCleartextPermitted18+

isCleartextPermitted(): boolean

Checks whether plaintext HTTP access is allowed from the preset network_config.json file of the application. By default, plaintext HTTP access is allowed.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Return values:

TypeDescription
booleanBoolean value indicating whether plaintext HTTP is allowed. The value true indicates that plaintext HTTP is allowed, and the value false indicates the opposite. The default value is true.

Error codes

IDError Message
201Permission denied.

Example

import { networkSecurity } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  let result: boolean = networkSecurity.isCleartextPermitted();
  console.info(`isCleartextPermitted Result: ${JSON.stringify(result)}`);
} catch (error) {
  console.error(`isCleartextPermitted Error: ${JSON.stringify(error)}`);
}

networkSecurity.isCleartextPermittedByHostName18+

isCleartextPermittedByHostName(hostName: string): boolean

Checks whether host name–based plaintext HTTP access is allowed from the preset network_config.json file of the application. By default, plaintext HTTP access is allowed.

Required permissions: ohos.permission.INTERNET

System capability: SystemCapability.Communication.NetStack

Parameters

NameTypeMandatoryDescription
hostNamestringYesHost name.

Return values:

TypeDescription
booleanBoolean value indicating whether host name–based plaintext HTTP is allowed. The value true indicates that plaintext HTTP is allowed, and the value false indicates the opposite. The default value is true.

Error codes

IDError Message
201Permission denied.

Example

import { networkSecurity } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  let result: boolean = networkSecurity.isCleartextPermittedByHostName("xxx");
  console.info(`isCleartextPermitted Result: ${JSON.stringify(result)}`);
} catch (error) {
  console.error(`isCleartextPermitted Error: ${JSON.stringify(error)}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙NetConn_ConnectionProperties

harmony 鸿蒙NetConn_HttpProxy

harmony 鸿蒙NetConn_NetAddr

harmony 鸿蒙NetConn_NetCapabilities

harmony 鸿蒙NetConn_NetConnCallback

harmony 鸿蒙NetConn_NetHandle

harmony 鸿蒙NetConn_NetHandleList

harmony 鸿蒙NetConn_NetSpecifier

harmony 鸿蒙NetConn_Route

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/KHv5mG