← All articlesMobile applications

How to Limit Trust in an Internal CA to One Android App Domain

An internal HTTPS API may require its own certificate authority, but trust in that authority does not have to extend across the entire Android app. The developer’s task is to define the domain scope of trust explicitly and verify it through the network client in use. We will use an entirely fictional test environment in which all hosts, certificates, and synthetic data belong to the team: the internal CA must be accepted for api.team.example and rejected for control.team.example.

The trust boundary: which host accepts the internal CA

Network Security Configuration lets you define general settings through base-config and domain-specific settings through domain-config. For this example, we choose a narrow scope: the internal CA is allowed only for the exact hostname api.team.example. The control host, control.team.example, falls outside that scope and uses the general settings.

The acceptance criteria have two parts: the intended host accepts a valid certificate from the internal CA, and the control host rejects a certificate from the same CA. In addition, the control host must work successfully with its permitted certificate chain. Without this last condition, an ordinary connection failure could easily be mistaken for an effective trust restriction.

Connect the configuration and define explicit trust-anchors

First, reference the Network Security Configuration XML file in the app manifest. Place your CA certificate in the res/raw/internal_ca resource in PEM or DER format. The documentation provides declarative configuration for new or custom CAs without requiring changes to application code to add that trust.

In base-config, define trust-anchors with certificates src=system as the only source: this represents the preinstalled system CAs. In a separate domain-config, specify api.team.example with includeSubdomains=false, and set its trust-anchors to certificates src=@raw/internal_ca only. This explicitly defines two different trust sets. In this configuration, system CAs are not included in the API’s domain rule.

The internal CA must not be present among the test environment’s trusted system CAs. Otherwise, the control host could accept it through the system source, and the environment would not let you verify the intended boundary.

Choose subdomain coverage in advance

With includeSubdomains=false, the rule applies only to the exact hostname api.team.example. It does not cover child.api.team.example. With includeSubdomains=true, the scope includes the domain itself and all levels of its subdomains, so this attribute changes the trust boundary.

For the single-hostname API in our example, we keep false. If the product requires trust for subdomains, test a separate configuration with true. In both configurations, control.team.example must remain outside the internal CA’s scope. Record the chosen coverage as a testable requirement so that results can be compared unambiguously with expectations.

Inheritance and more specific rules

If several domain-config rules match a destination, the rule with the most specific, longest matching domain is selected. Unspecified values in a nested domain-config are inherited from its parent domain-config; a non-nested rule inherits them from base-config. Connections without a matching domain-config use base-config.

A nested rule that omits trust-anchors does not imply a return to system CAs. To verify this, create a configuration with includeSubdomains=true for api.team.example and a nested rule for child.api.team.example without its own trust-anchors: the internal CA should be accepted through inheritance. Then explicitly set certificates src=system as the only source in the nested rule. The expectation changes: the internal CA is rejected, and a permitted system certificate chain is accepted.

Three checks for the main domain restriction

The following are original proposed defensive tests, not an official Android procedure. Run them only in the team-owned fictional test environment through the app’s actual network client. For each host, present a certificate valid for that specific hostname.

  • API positive control: api.team.example establishes an HTTPS connection using a certificate from the internal CA.
  • Negative scope control: control.team.example, using a certificate from the same internal CA, fails specifically during certificate chain trust validation. DNS errors, server unavailability, and hostname mismatches do not count.
  • Control-host positive control: separately present a certificate on control.team.example with a chain to a system CA trusted in the test environment. The same client must successfully establish an HTTPS connection.

Subdomain checks and observable results

For child.api.team.example, prepare a separate certificate from the internal CA that is valid for this hostname. Without additional domain rules, expect a trust failure with includeSubdomains=false and acceptance in a separate configuration with true. In each configuration, repeat both checks for control.team.example: rejection of the internal CA and success with the permitted system certificate chain.

Record the client, build, environment, configuration variant, presented certificate chain, and reason for failure. To verify inheritance, separately compare the results before and after explicitly setting system trust-anchors in the nested rule. These observations test the scope of trust; a successful HTTP response alone does not confirm acceptance or rejection of any change to application state.

What to fix first and where the conclusions end

The first priority is to eliminate unconditional acceptance of all certificates by the TLS validation component. Then check the XML reference in the manifest, placement of the internal CA only in the intended domain-config, the chosen subdomain coverage, and trust-anchors inheritance. After making corrections, repeat the relevant positive and negative controls.

The result applies to the client, build, and environment tested. It does not prove how all other connections in the app behave and provides no statistical guarantee of security. This article is limited to the scope of CA trust and the behavior of the client in use; other network security mechanisms and server-side checks are outside its scope. If you need an assessment of a website or the web component of your product, you can start with a free CheckMyApp audit.

Sources and documentation

  1. Network security configuration
  2. Improve your app's security
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free