We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
Best PracticesApplication SecurityNetwork Connection Security Configuration

Network Connection Security Configuration

Overview

Data transmitted between apps and servers must be secure to protect data confidentiality and integrity and prevent sensitive data from being stolen or tampered with. The Transport Layer Security (TLS) protocol is recommended for security purposes.

When an app accesses a cloud server via HTTPS, if the app trusts a user-installed CA certificate, the user can use network proxy tools (such as Fiddler or Charles) to perform man-in-the-middle attacks (such as viewing, tampering with requests and responses) on HTTPS messages, which may pose security risks to the app or the cloud server. Therefore, a CA certificate should be configured for legitimacy verification for the access to a cloud server via HTTPS.

This topic describes how to configure a CA certificate for an app to prevent security risks to the app or cloud server.

Configuring the CA Certificate to Verify the Server Validity

When an app attempts to connect to a server using TLS, the server provides a certificate chain to prove its identity. The app needs to use a trusted CA certificate to verify the validity of the server's certificate chain.

The certificate chain used by a server varies, depending on the scenarios.

  1. Authoritative CA certificates, such as root CA certificates from CFCA and GlobalSign, comply with industry management standards and have passed audit certification. These CA certificates are highly credible.
  2. CA certificates created by enterprises are used for certificate chains of internal servers. Internal apps of enterprises trust these CA certificates.

The CA certificates in the system can be classified into the following types:

  1. Prebuilt CA certificates: The system is prebuilt with industry's mainstream authoritative CA certificates.
  2. App-managed CA certificates: Trusted CA certificates can be prebuilt in an app's HAP, for example, CA certificates created by an enterprise.
  3. User-installed CA certificates: These certificates are private CA certificates installed on the system settings page. This type of certificate has low trustworthiness and can be exploited for man-in-the-middle attacks.

You should properly configure the trusted CA certificates based on service requirements.

Network Connection Security Configuration

Configuring the System Prebuilt CA Certificates as Trusted Certificates

The apps that provide services for Internet users only need to trust the system's prebuilt CA certificates. The HTTPS connections set up by using Network Kit and Remote Communication Kit trust the system's prebuilt CA certificates by default.
NOTE

By default, the HTTPS connections set up by using Network Kit and Remote Communication Kit trust the system's prebuilt CA certificates and user-installed CA certificates. You can configure untrusted user-installed CA certificates to further improve security.

If the app uses a third-party library for network connection, you need to set the system's prebuilt CA certificate path /etc/security/certificates.

Example

If the third-party library curl is used to set up HTTPS connections, set the path of trusted CA certificates as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. curl_easy_setopt(curl, CURLOPT_CATH, "/etc/security/certificates");

Configuring Untrusted User-Installed CA Certificates

Configure untrusted user-installed certificates in the src/main/resources/base/profile/network_config.json file. The settings take effect for Network Kit and Remote Communication Kit.

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. "network-security-config": {
  3. ... ...
  4. },
  5. "trust-global-user-ca": false, //Configure whether to trust the CA certificate manually installed by the enterprise MDM system or device administrator. The default value is true
  6. "trust-current-user-ca": false // Configure whether to trust the CA certificate installed by the current user. The default value is true
  7. }

Configuring the Application-Managed CA Certificates as Trusted Certificates

If the app server uses the CA certificates created by an enterprise, you can prebuild the CA certificates in the HAP and configure the app to trust these CA certificates.

  • You can configure the Network Kit and Remote Communication Kit using the src/main/resources/base/profile/network_config.json file. For example, you can prebuild the CA certificates trusted by the app to the /data/storage/el1/bundle/entry/resources/resfile/appCaCert directory and the CA certificates trusted by the specific domain to the /data/storage/el1/bundle/entry/resources/resfile/domainCaCert directory.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. {
    2. "network-security-config": {
    3. "base-config": {
    4. "trust-anchors": [
    5. {
    6. "certificates": "/data/storage/el1/bundle/entry/resources/resfile/appCaCert"
    7. }
    8. ]
    9. },
    10. "domain-config": [
    11. {
    12. "domains": [
    13. {
    14. "include-subdomains": true,
    15. "name": "example.com"
    16. }
    17. ],
    18. "trust-anchors": [
    19. {
    20. "certificates": "/data/storage/el1/bundle/entry/resources/resfile/domainCaCert"
    21. }
    22. ]
    23. }
    24. ]
    25. }
    26. }

    You can also use Network Kit to specify the path of the trusted CA certificates in the code that initiates an HTTPS request.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. httpRequest.request( 'EXAMPLE_URL', {
    2. method: http.RequestMethod.POST,
    3. header: {
    4. 'Content-Type': 'application/json'
    5. },
    6. extraData: 'data to send',
    7. expectDataType: http.HttpDataType.STRING,
    8. connectTimeout: 60000,
    9. caPath:'/data/storage/el1/bundle/entry/resources/resfile/domainCaCert', // Specifies the trusted CA certificate path
    10. }, (err: BusinessError, data: http.HttpResponse) => {
    11. // ...
    12. })

    You can also use Remote Communication Kit to specify the path of the trusted CA certificates.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. const caPath: rcp.CertificateAuthority = {
    2. folderPath: '/data/storage/el1/bundle/entry/resources/resfile/appCaCert', // Specify trusted CA certificate path
    3. }
    4. const securityConfig: rcp.SecurityConfiguration = {
    5. remoteValidation: caPath
    6. };
    7. // Use the security configuration in the session creation
    8. const sessionWithSecurityConfig = rcp.createSession({ requestConfiguration: { security: securityConfig } });
NOTE

After the preceding configurations are complete, the system Network Kit and Remote Communication Kit still trust the prebuilt CA certificates and CA certificates installed by the user. To improve security, you can configure the CA certificates that are not trusted.

  • If the app uses a third-party library for network connection, you need to set the path of the app-managed CA certificate in the code. For example, if the third-party library curl is used to set up HTTPS connections, set the path of trusted CA certificates as follows:
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. curl_easy_setopt(curl, CURLOPT_CATH, "/data/storage/el1/bundle/entry/resources/resfile/domainCaCert");

Configuring the User-Installed CA Certificates as Trusted Certificates

The CA certificates installed by users pose security risks. Avoid setting this type of certificates as trusted CA certificates except in the following scenarios:

  1. You need to capture network packets to locate problems and perform tests in app development and debugging process. The user-installed CA certificates are saved in the /data/certificates/user_cacerts/{userid} directory. The value of userid starts from 100.

    NOTE: The commercially released apps should not trust the user-installed CA certificates.

  2. The enterprise apps need to access to app servers through the enterprise's proxy server. The CA certificates manually installed by the MDM system or device administrator are saved in the /data/certificates/user_cacerts/0 directory.

Configuring SSL Pinning

By default, an app trusts the CA certificates prebuilt in the system. If the prebuilt CA issues an untrusted certificate, apps are exposed to attacks. For security-critical apps, such as payment and banking apps, you can use SSL pinning to trust only the public key of the specified server certificate.

SSL pinning can be configured in either of the following ways: If the server domain name is fixed, static SSL pinning is recommended. Otherwise, dynamic SSL pinning is recommended.

NOTE

SSL pinning requires that the public key of the app cloud server certificate does not change. If the public key changes, you need to modify the certificate public key configured in the app. Otherwise, the network connection of the app will fail. Therefore, it is recommended that the SSL pinning configuration always contain at least one backup public key.

You can also set the expiration time of SSL pinning. After the certificate expires, the certificate is no longer locked. This helps prevent connectivity issues for apps that have not updated their public keys after the public key of the server certificate changes. However, setting the expiration time of SSL pinning may allow attackers to bypass certificate pinning.

You need to evaluate and determine whether to configure SSL pinning. If sensitive information is encrypted or signed at the app layer, the security risk is low.

  1. Configure static SSL pinning using the network_config.json file.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. {
    2. "network-security-config": {
    3. "domain-config": [
    4. {
    5. "domains": [
    6. {
    7. "include-subdomains": true,
    8. "name": "server.com"
    9. }
    10. ],
    11. "pin-set": {
    12. "expiration": "2024-11-08",
    13. "pin": [
    14. {
    15. "digest-algorithm": "sha256",
    16. "digest": "g8CsdcpyAKxmLoWFvMd2hC7ZDUy7L4E2NYOi1i8qEtE=" // Hash of the server certificate public key
    17. }
    18. ]
    19. }
    20. }
    21. ]
    22. }
    23. }

    For details, see Certificate Pinning.

  2. Set the following parameters in the code to configure dynamic SSL pinning:
    • Configure dynamic SSL pinning using Network Kit:
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. certificatePinning: [ //Optional, supports dynamic setting of certificate lock configuration information. This property is supported since API 12
      2. {
      3. publicKeyHash: 'g8CsdcpyAKxmLoWFvMd2hC7ZDUy7L4E2NYOi1i8qEtE=', // Hash of the server certificate public key
      4. hashAlgorithm: 'SHA-256'
      5. }, {
      6. publicKeyHash: 'MGFiY2UyMDk5ZjEyMzI3MWQ4MDMyY2E4ODEzMmY3EtE=', // Hash of the secondary public key of the server certificate
      7. hashAlgorithm: 'SHA-256'
      8. }
      9. ]

      For details, see certificatePinning.

    • Configure dynamic SSL pinning using Remote Communication Kit:
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. const keyHash: string = 'g8CsdcpyAKxmLoWFvMd2hC7ZDUy7L4E2NYOi1i8qEtE='; // Public key of the server certificate
      2. const session = rcp.createSession();
      3. const request = new rcp.Request(HTTP_SERVER);
      4. const pin: rcp.CertificatePinning = {
      5. kind: 'public-key',
      6. publicKeyHash: keyHash,
      7. hashAlgorithm: 'SHA-256'
      8. };
      9. request.configuration = {
      10. security: {
      11. certificatePinning: pin,
      12. }
      13. };
      14. const resp = await session.fetch(request);

      For details, see certificatePinning.

Search in Best Practices
Enter a keyword.