1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
|
page.title=Network Security Configuration
page.keywords=androidn,security,network
page.image=images/cards/card-nyc_2x.jpg
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>In this document</h2>
<ol>
<li><a href="#manifest">Adding a Security Configuration File</a></li>
<li><a href="#CustomTrust">Customizing Trusted CAs</a>
<ol>
<li><a href="#ConfigCustom">Configuring a Trusted Custom CA</a></li>
<li><a href="#LimitingCas">Limiting the Set of Trusted CAs</a></li>
<li><a href="#TrustingAdditionalCas">Trusting Additional CAs</a></li>
</ol>
</li>
<li><a href="#TrustingDebugCa">Debugging-only CAs</a></li>
<li><a href="#CleartextTrafficPermitted">Opting Out of Cleartext Traffic</a></li>
<li><a href="#CertificatePinning">Pinning Certificates</a></li>
<li><a href="#ConfigInheritance">Configuration Inheritance Behavior</a></li>
<li><a href="#FileFormat">Configuration File Format</a></li>
</ol>
</div>
</div>
<p>
Android N includes a Network Security Configuration
feature that lets apps customize their network security settings in a safe,
declarative configuration file without modifying app code. These settings can
be configured for specific domains and for a specific app. The key
capabilities of this feature are as follows:
</p>
<ul>
<li>
<b>Custom trust anchors:</b> Customize which Certificate Authorities (CA)
are trusted for an app's secure connections. For
example, trusting particular self-signed certificates or restricting the
set of public CAs that the app trusts.
</li>
<li>
<b>Debug-only overrides:</b> Safely debug secure connections in an app
without added risk to the installed base.
</li>
<li>
<b>Cleartext traffic opt-out:</b> Protect apps from from
accidental usage of cleartext traffic.
</li>
<li>
<b>Certificate pinning:</b> Restrict an app's secure connection to
particular certificates.
</li>
</ul>
<h2 id="manifest">Adding a Security Configuration File</h2>
<p>
The Network Security Configuration feature uses an XML file where you specify
the settings for your app. You must include an entry in the manifest of your
app to point to this file. The following code excerpt from a manifest
demonstrates how to create this entry:
</p>
<pre>
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config"
... >
...
</application>
</manifest>
</pre>
<h2 id="CustomTrust">Customizing Trusted CAs</h2>
<p>
An app may want to trust a custom set of CAs instead of the platform
default. The most common reasons of this are:
</p>
<ul>
<li>Connecting to a host with a custom certificate authority(self-signed,
issued by an internal corporate CA, etc).
</li>
<li>Limiting the set of CAs to only the CAs you trust instead of every
preinstalled CA.
</li>
<li>Trusting additional CAs not included in the system.
</li>
</ul>
<p>
By default secure (e.g. TLS, HTTPS) connections from all apps trust
the pre-installed system CAs, and apps targeting API level 23
(Android M) and below also trust the user-added CA store by default. An
app can customize its own connections using {@code base-config} (for
app-wide customization) or {@code domain-config} (for per-domain
customization).
</p>
<h3 id="ConfigCustom">Configuring a Custom CA</h3>
<p>
Assume you want to connect to your host which uses a self-signed SSL
certificate or to a host whose SSL certificate is issued by a non-public CA
which you trust, such as your company's internal CA.
</p>
<p>
<code>res/xml/network_security_config.xml</code>:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">example.com</domain>
<trust-anchors>
<certificates src="@raw/my_ca"/>
</trust-anchors>
</domain-config>
</network-security-config>
</pre>
</p>
<p>
Add the self-signed or non-public CA certificate, in PEM or DER format, to
{@code res/raw/my_ca}.
</p>
<h3 id="LimitingCas">Limiting the Set of Trusted CAs</h3>
<p>
An app that does not want to trust all CAs trusted by system can
instead specify its own reduced set of CAs to trust. This protects the
app from fradulent certificates issued by any of the other CAs.
</p>
<p>
The config to limit the set of trusted CAs is similar to <a href=
"#TrustingACustomCa">trusting a custom CA</a> for a specific domain except
that multiple CAs are provided in the resource.
</p>
<p>
<code>res/xml/network_security_config.xml</code>:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">secure.example.com</domain>
<domain includeSubdomains="true">cdn.example.com</domain>
<trust-anchors>
<certificates src="@raw/trusted_roots"/>
</trust-anchors>
</domain-config>
</network-security-config>
</pre>
</p>
<p>
Add the trusted CAs, in PEM or DER format, to {@code res/raw/trusted_roots}.
Note that if using PEM format the file must contain <em>only</em> PEM data
and no extra text. You can also provide multiple
<a href="#certificates"><code><certificates></code></a>
elements instead of one.
</p>
<h3 id="TrustingAdditionalCas">
Trusting Additional CAs
</h3>
<p>
An app may want to trust additional CAs not trusted by the system,
this could be due to the system not yet including the CA or a CA that does
not meet the requirements for inclusion into the Android system. An
app can do this by specifying multiple certificate sources for a
configuration.
</p>
<p>
<code>res/xml/network_security_config.xml</code>:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config>
<trust-anchors>
<certificates src="@raw/extracas"/>
<certificates src="system"/>
</trust-anchors>
</base-config>
</network-security-config>
</pre>
</p>
<h2 id="TrustingDebugCa">Configuring CAs for Debugging</h2>
<p>
When debugging an app that connects over HTTPS you may want to
connect to a local development server, which does not have the SSL
certificate for your production server. In order to support this without any
modification to your app's code you can specify debug-only CAs that
are <i>only</i> trusted when <a href=
"{@docRoot}guide/topics/manifest/application-element.html#debug">
android:debuggable</a>
is {@code true} by using {@code debug-overrides}. Normally IDEs and build
tools set this flag automatically for non-release builds.
</p>
<p>
This is safer than the usual conditional code because, as a security
precaution, app stores do not accept apps which are marked
debuggable.
</p>
<p>
<code>res/xml/network_security_config.xml</code>:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<debug-overrides>
<trust-anchors>
<certificates src="@raw/debug_cas"/>
</trust-anchors>
</debug-overrides>
</network-security-config>
</pre>
</p>
<h2 id="CleartextTrafficPermitted">Opting Out of Cleartext Traffic</h2>
<p>
Applications intending to connect to destinations using only secure
connections can opt-out of supporting cleartext (using unencrypted HTTP
protocol instead of HTTPS) to those destinations. This option helps prevent
accidental regressions in apps due to changes in URLs provided by external
sources such as backend servers.
See {@link android.security.NetworkSecurityPolicy#isCleartextTrafficPermitted
NetworkSecurityPolicy.isCleartextTrafficPermitted()} for more details.
</p>
<p>
For example, an app may want to ensure that all connections to {@code
secure.example.com} are always done over HTTPS to protect sensitive traffic
from hostile networks.
</p>
<p>
<code>res/xml/network_security_config.xml</code>:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">secure.example.com</domain>
</domain-config>
</network-security-config>
</pre>
</p>
<h2 id="CertificatePinning">Pinning Certificates</h2>
<p>
Normally an app trusts all preinstalled CAs. If any of these CAs were
to issue a fradulent certificate the app would be at risk from a MiTM
attack. Some apps choose to limit the set of certificates they accept
by either limiting the set of CAs they trust or by certificate pinning.
</p>
<p>
Certificate pinning is done by providing a set of certificates by hash of the
public key (SubjectPublicKeyInfo of the X.509 certificate). A certificate
chain is then only valid if the certificate chain contains at least one of
the pinned public keys.
</p>
<p>
Note that when using certificate pinning you should always include a backup
key so that if you are forced to switch to new keys, or change CAs (when
pinning to a CA certificate or an intermediate of that CA), your
app's connectivity is unaffected. Otherwise you must to push out
an update to the app to restore connectivity.
</p>
<p>
Additionally it is possible to set an expiration time for pins after which
pinning is not performed. This helps prevent connectivity issues in
apps which have not been updated. However, setting an expiration time
on pins may enable pinning bypass.
</p>
<p>
<code>res/xml/network_security_config.xml</code>:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">example.com</domain>
<pin-set expiration="2018-01-01">
<pin digest="SHA-256">7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y=</pin>
<!-- backup pin -->
<pin digest="SHA-256">fwza0LRMXouZHRC8Ei+4PyuldPDcf3UKgO/04cDM1oE=</pin>
</pin-set>
</domain-config>
</network-security-config>
</pre>
</p>
<h2 id="ConfigInheritance">Configuration Inheritance Behavior</h2>
<p>
Values not set in a specific config are inherited. This behavior allows more
complex configurations while keeping the configuration file readable.
</p>
<p>
If a value is not set in a specific entry then value from the next more
general entry is used. Values not set in a {@code domain-config} is
taken from the parent {@code domain-config}, if nested, or from the {@code
base-config} if not. Values not set in the {@code base-config} uses the
platform default values.
</p>
<p>
For example consider, where all connections to subdomains of {@code
example.com} must use a custom set of CAs. Additonally cleartext traffic to
these domains is permitted <em>except</em> when connecting to {@code
secure.example.com}. By nesting the configuration for {@code
secure.example.com} inside the configuration for {@code example.com} the
{@code trust-anchors} does not need to be duplicated.
</p>
<p>
<code>res/xml/network_security_config.xml</code>:
<pre>
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">example.com</domain>
<trust-anchors>
<certificates src="@raw/my_ca"/>
</trust-anchors>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">secure.example.com</domain>
</domain-config>
</domain-config>
</network-security-config>
</pre>
</p>
<h2 id="FileFormat">Configuration File Format</h2>
<p>
The Network Security Configuration feature uses an XML file format.
The overall structure of the file is shown in the following code sample:
</p>
<pre>
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config>
<trust-anchors>
<certificates src="..."/>
...
</trust-anchors>
</base-config>
<domain-config>
<domain>android.com</domain>
...
<trust-anchors>
<certificates src="..."/>
...
</trust-anchors>
<pin-set>
<pin digest="...">...</pin>
...
</pin-set>
</domain-config>
...
<debug-overrides>
<trust-anchors>
<certificates src="..."/>
...
</trust-anchors>
</debug-overrides>
</network-security-config>
</pre>
<p>
The following sections describe the syntax and other details of the file
format.
</p>
<h3 id="network-security-config">
<network-security-config>
</h3>
<dl class="xml">
<dt>
can contain:
</dt>
<dd>
0 or 1 of <code><a href="#base-config"><base-config></a></code><br>
Any number of <code><a href=
"#domain-config"><domain-config></a></code><br>
0 or 1 of <code><a href="#debug-overrides"><debug-overrides></a></code>
</dd>
</dl>
<h3 id="base-config">
<base-config>
</h3>
<dl class="xml">
<dt>
syntax:
</dt>
</dl>
<pre class="stx">
<base-config <a href=
"#CleartextTrafficPermitted">cleartextTrafficPermitted</a>=["true" | "false"]>
...
</base-config>
</pre>
<dl class="xml">
<dt>
can contain:
</dt>
<dd>
<code><a href="#trust-anchors"><trust-anchors></a></code>
</dd>
<dt>
description:
</dt>
<dd>
The default configuration used by all connections whose destination is not
covered by a <a href="#domain-config"><code>domain-config</code></a>.
<p>
Any values that are not set use the platform default values. The default
configuration for apps targeting above API level 24 and above:
</p>
<pre>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</pre>
The default configuration for apps targeting API level 23 and below is:
<pre>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>
</base-config>
</pre>
</dd>
</dl>
<h3 id="domain-config"><domain-config></h3>
<dl class="xml">
<dt>syntax:</dt>
<dd>
<pre class="stx"><domain-config <a href="#CleartextTrafficPermitted">cleartextTrafficPermitted</a>=["true" | "false"]>
...
</domain-config></pre>
</dd>
<dt>Can Contain:</dt>
<dd>
1 or more <code><a href="#domain"><domain></a></code>
<br/>0 or 1 <code><a href="#trust-anchors"><trust-anchors></a></code>
<br/>0 or 1 <code><a href="#pin-set"><pin-set></code></a>
<br/>Any number of nested <code><domain-config></code></dd>
<dt>Description</dt>
<dd>Configuration used for connections to specific destinations as the defined by {@code domain} elements.
<p>Note that if multiple {@code domain-config} elements cover a destination the config with the most specific (longest)
matching domain rule is used.</p></dd>
</dl>
<h3 id="domain"><domain></h3>
<dl class="xml">
<dt>
syntax:
</dt>
<dd>
<pre class="stx">
<domain includeSubdomains=["true" | "false"]>example.com</domain>
</pre>
</dd>
<dt>
Attributes:
</dt>
<dd>
<dl class="attr">
<dt>
{@code includeSubdomains}
</dt>
<dd>
If {@code "true"} then this domain rule matches the domain and all
subdomains, including subdomains of subdomains, otherwise the rule only
applies to exact matches.
</dd>
</dl>
</dd>
<dt>
Description:
</dt>
</dl>
<h3 id="debug-overrides"><debug-overrides></h3>
<dl class="xml">
<dt>
syntax:
</dt>
<dd>
<pre class="stx">
<debug-overrides>
...
</debug-overrides>
</pre>
</dd>
<dt>
Can Contain:
</dt>
<dd>
0 or 1 <code><a href="#trust-anchors"><trust-anchors></a></code>
</dd>
<dt>
Description:
</dt>
<dd>
Overrides to be applied when <a href=
"{@docRoot}guide/topics/manifest/application-element.html#debug">android:debuggable</a>
is {@code "true"} which is normally the case for non-release builds
generated by IDEs and build tools. Trust anchors specified in {@code
debug-overrides} are added to all other configurations and certificate
pinning is not performed when the server's certificate chain uses one of
these debug-only trust anchors. If <a href=
"{@docRoot}guide/topics/manifest/application-element.html#debug">android:debuggable</a>
is {@code "false"} then this section is completely ignored.
</dd>
</dl>
<h3 id="trust-anchors"><trust-anchors></h3>
<dl class="xml">
<dt>
syntax:
</dt>
<dd>
<pre class="stx">
<trust-anchors>
...
</trust-anchors>
</pre>
</dd>
<dt>
Can Contain:
</dt>
<dd>
Any number of <code><a href="#certificates"><certificates></a></code>
</dd>
<dt>
Description:
</dt>
<dd>
Set of trust anchors for secure connections.
</dd>
</dl>
<h3 id="certificates"><certificates></h3>
<dl class="xml">
<dt>syntax:</dt>
<dd><pre class="stx"><certificates src=["system" | "user" | "<i>raw resource</i>"]
overridePins=["true" | "false"] />
</pre></dd>
<dt>description:</dt>
<dd>Set of X.509 certificates for {@code trust-anchors} elements.</dd>
<dt>attributes:</dt>
<dd><dl class="attr">
<dt>{@code src}</dt>
<dd>
The source of CA certificates, can be one of
<ul>
<li>a raw resource id pointing to a file containing X.509 certificates.
Certificates must be encoded in DER or PEM format. In the case of PEM
certificates the file <em>must not</em> contain extra non-PEM data such as
comments.
</li>
<li>{@code "system"} for the pre-installed system CA certificates
</li>
<li>{@code "user"} for user-added CA certificates
</li>
</ul>
</dd>
<dt>{@code overridePins}</dt>
<dd>
<p>
Specifies if the CAs from this source bypass certificate pinning. If {@code
"true"} then certificate chains which chain through one of the CAs from this
source then pinning is not be performed. This can be useful for debug CAs
or to support letting the user MiTM your app's secure traffic.
</p>
<p>
Default is {@code "false"} unless specified in a {@code debug-overrides}
element, in which case the default is {@code "true"}.
</p>
</dd>
</dl>
</dd>
<h3 id="pin-set"><pin-set></h3>
<dl class="xml">
<dt>
syntax:
</dt>
<dd>
<pre class="stx">
<pin-set expiration="date">
...
</pin-set>
</pre>
</dd>
<dt>
Can Contain:
</dt>
<dd>
Any number of <code><a href="#pin"><pin></a></code>
</dd>
<dt>
Description:
</dt>
<dd>
A set of public key pins. For a secure connection to be trusted, one of the
public keys in the chain of trust must be in the set of pins. See
<code><a href="#pin"><pin></a></code> for the format of pins.
</dd>
<dt>
Attributes:
</dt>
<dd>
<dl class="attr">
<dt>
{@code expiration}
</dt>
<dd>
The date, in {@code yyyy-MM-dd} format, at and after which the pins
expire, thus disabling pinning. If the attribute is not set then the
pins do not expire.
<p>
Expiration helps prevent connectivity issues in apps which do
not get updates to their pin set, for example because the user
disabled app updates.
</p>
</dd>
</dl>
</dd>
</dl>
<h3 id="pin"><pin></h3>
<dl class="xml">
<dt>
syntax:
</dt>
<dd>
<pre class="stx">
<pin digest=["SHA-256"]>base64 encoded digest of X.509
SubjectPublicKeyInfo (SPKI)</pin>
</pre>
</dd>
<dt>
Attributes:
</dt>
<dd>
<dl class="attr">
<dt>
{@code digest}
</dt>
<dd>
The digest algorithm used to generate the pin. Currently only
{@code "SHA-256"} is supported.
</dd>
</dl>
</dd>
</dl>
|