/
DomainPinningPolicy.java
270 lines (223 loc) · 9.46 KB
/
DomainPinningPolicy.java
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
package com.datatheorem.android.trustkit.config;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public final class DomainPinningPolicy {
// The default URL to submit pin failure report to
private static final URL DEFAULT_REPORTING_URL;
static {
java.net.URL defaultUrl;
try {
defaultUrl = new java.net.URL("https://overmind.datatheorem.com/trustkit/report");
} catch (java.net.MalformedURLException e) {
throw new IllegalStateException("Bad DEFAULT_REPORTING_URL");
}
DEFAULT_REPORTING_URL = defaultUrl;
}
@NonNull private final String hostname;
private final boolean shouldIncludeSubdomains;
@NonNull private final Set<PublicKeyPin> publicKeyPins;
@Nullable private final Date expirationDate;
private final boolean shouldEnforcePinning;
@NonNull private final Set<URL> reportUris;
DomainPinningPolicy(@NonNull String hostname,
Boolean shouldIncludeSubdomains,
@NonNull Set<String> publicKeyHashStrList,
Boolean shouldEnforcePinning,
@Nullable Date expirationDate,
@Nullable Set<String> reportUriStrList,
Boolean shouldDisableDefaultReportUri)
throws MalformedURLException {
// Run some sanity checks on the configuration
// Check if the hostname seems valid
// DomainValidator.getInstance(true) allows TrustKit to include localhost in the valid domains
// see https://github.com/datatheorem/TrustKit-Android/issues/25
DomainValidator domainValidator = DomainValidator.getInstance(true);
if (!domainValidator.isValid(hostname)) {
throw new ConfigurationException("Tried to pin an invalid domain: " + hostname);
}
this.hostname = hostname.trim();
// Check if the configuration has a empty pin-set and still would enforce pinning
// TrustKit should not work if the configuration contains both (opposite behaviors)
if (publicKeyHashStrList.isEmpty() && shouldEnforcePinning) {
throw new ConfigurationException("An empty pin-set was supplied "+
"for domain " + this.hostname + " with the enforcePinning set to true. " +
"An empty pin-set disables pinning and can't be use with enforcePinning set to true.");
}
// Check if the configuration has at least two pins (including a backup pin)
// TrustKit should not work if the configuration contains only one pin
// more info (https://tools.ietf.org/html/rfc7469#page-21)
if (publicKeyHashStrList.size() < 2 && shouldEnforcePinning) {
throw new ConfigurationException("Less than two pins were supplied "+
"for domain " + this.hostname + ". This might " +
"brick your App; please review the Getting Started guide in " +
"./docs/getting-started.md");
}
// Parse the supplied pins
publicKeyPins = new HashSet<>();
for (String pinStr : publicKeyHashStrList) {
publicKeyPins.add(new PublicKeyPin(pinStr));
}
// Parse the supplied report URLs
reportUris = new HashSet<>();
if (reportUriStrList != null) {
for (String UriStr : reportUriStrList) {
reportUris.add(new URL(UriStr));
}
}
// Add the default report URL
if ((shouldDisableDefaultReportUri == null) || (!shouldDisableDefaultReportUri) ) {
reportUris.add(DEFAULT_REPORTING_URL);
}
// Parse boolean settings and handle default values
if (shouldEnforcePinning == null) {
this.shouldEnforcePinning = false;
} else {
this.shouldEnforcePinning = shouldEnforcePinning;
}
if (shouldIncludeSubdomains == null) {
this.shouldIncludeSubdomains = false;
} else {
this.shouldIncludeSubdomains = shouldIncludeSubdomains;
}
this.expirationDate = expirationDate;
}
@NonNull
public String getHostname() {
return hostname;
}
@NonNull
public Set<PublicKeyPin> getPublicKeyPins() {
return publicKeyPins;
}
public boolean shouldEnforcePinning() {
return shouldEnforcePinning;
}
@NonNull
public Set<URL> getReportUris() {
return reportUris;
}
public boolean shouldIncludeSubdomains() {
return shouldIncludeSubdomains;
}
@Nullable
public Date getExpirationDate() {
return expirationDate;
}
@Override
public String toString() {
return "DomainPinningPolicy{" +
"hostname = " + hostname + "\n" +
"knownPins = " + Arrays.toString(publicKeyPins.toArray()) +
"\n" +
"shouldEnforcePinning = " + shouldEnforcePinning + "\n" +
"reportUris = " + reportUris + "\n" +
"shouldIncludeSubdomains = " + shouldIncludeSubdomains + "\n" +
"}";
}
public static final class Builder {
// The domain must always be specified in domain-config
private String hostname;
// The remaining settings can be inherited from a parent domain-config
private Boolean shouldIncludeSubdomains = null;
private Set<String> publicKeyHashes = new HashSet<>();
private Date expirationDate = null;
private Boolean shouldEnforcePinning = null;
private Set<String> reportUris = null;
private Boolean shouldDisableDefaultReportUri = null;
// The parent domain-config
private Builder parentBuilder = null;
public DomainPinningPolicy build() throws MalformedURLException {
if (parentBuilder != null) {
// Get missing values from the parent as some entries can be inherited
// build() should already have been called on it so it has its parent's values
// inherited already
if (shouldIncludeSubdomains == null) {
shouldIncludeSubdomains = parentBuilder.getShouldIncludeSubdomains();
}
if (publicKeyHashes == null) {
publicKeyHashes = parentBuilder.getPublicKeyHashes();
}
if (expirationDate == null) {
expirationDate = parentBuilder.getExpirationDate();
}
if (shouldEnforcePinning == null) {
shouldEnforcePinning = parentBuilder.getShouldEnforcePinning();
}
if (reportUris == null) {
reportUris = parentBuilder.getReportUris();
}
if (shouldDisableDefaultReportUri == null) {
shouldDisableDefaultReportUri = parentBuilder.getShouldDisableDefaultReportUri();
}
}
return new DomainPinningPolicy(hostname, shouldIncludeSubdomains, publicKeyHashes,
shouldEnforcePinning, expirationDate, reportUris,
shouldDisableDefaultReportUri);
}
public Builder setParent(Builder parent) {
// Sanity check to avoid adding loops.
Builder current = parent;
while (current != null) {
if (current == this) {
throw new IllegalArgumentException("Loops are not allowed in Builder parents");
}
current = current.parentBuilder;
}
parentBuilder = parent;
return this;
}
public Builder setHostname(String hostname) {
this.hostname = hostname;
return this;
}
Boolean getShouldIncludeSubdomains() {
return shouldIncludeSubdomains;
}
public Builder setShouldIncludeSubdomains(Boolean shouldIncludeSubdomains) {
this.shouldIncludeSubdomains = shouldIncludeSubdomains;
return this;
}
Set<String> getPublicKeyHashes() {
return publicKeyHashes;
}
public Builder setPublicKeyHashes(Set<String> publicKeyHashes) {
this.publicKeyHashes = publicKeyHashes;
return this;
}
Date getExpirationDate() {
return expirationDate;
}
public Builder setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
return this;
}
Boolean getShouldEnforcePinning() {
return shouldEnforcePinning;
}
public Builder setShouldEnforcePinning(Boolean shouldEnforcePinning) {
this.shouldEnforcePinning = shouldEnforcePinning;
return this;
}
Set<String> getReportUris() {
return reportUris;
}
public Builder setReportUris(Set<String> reportUris) {
this.reportUris = reportUris;
return this;
}
Boolean getShouldDisableDefaultReportUri() {
return shouldDisableDefaultReportUri;
}
public Builder setShouldDisableDefaultReportUri(Boolean shouldDisableDefaultReportUri) {
this.shouldDisableDefaultReportUri = shouldDisableDefaultReportUri;
return this;
}
}
}