forked from datatheorem/TrustKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSKPinConfigurationTests.m
348 lines (282 loc) · 19.9 KB
/
TSKPinConfigurationTests.m
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
/*
TSKPinConfigurationTests.m
TrustKit
Copyright 2015 The TrustKit Project Authors
Licensed under the MIT license, see associated LICENSE file for terms.
See AUTHORS file for the list of project authors.
*/
#import <XCTest/XCTest.h>
#import "../TrustKit/TrustKit.h"
#import "../TrustKit/TSKTrustKitConfig.h"
#import "../TrustKit/Pinning/ssl_pin_verifier.h"
#import "../TrustKit/parse_configuration.h"
#import "../TrustKit/configuration_utils.h"
#import "TSKCertificateUtils.h"
@interface TSKPinConfigurationTests : XCTestCase
@end
@implementation TSKPinConfigurationTests
- (void)setUp
{
[super setUp];
}
- (void)tearDown
{
[super tearDown];
}
// Pin to only one key and ensure it fails; TrustKit requires at least two pins (which should include a backup pin)
- (void)testPinOnePublicKey
{
XCTAssertThrows(parseTrustKitConfiguration(@{kTSKPinnedDomains : @{
@"www.good.com" : @{
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
]}}}),
@"Configuration with one pin only must be rejected");
}
- (void)testDisablePinningForSubdomainAndNoPublicKey
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains : @{
@"good.com" : @{
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
],
kTSKIncludeSubdomains: @YES},
@"unsecured.good.com": @{
// When using this option, TrustKit should accept an empty policy for the domain
kTSKExcludeSubdomainFromParentPolicy: @YES
}
}
});
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"unsecured.good.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertEqualObjects(serverConfigKey, @"unsecured.good.com", @"Did not receive a configuration for pinned subdomain");
}
- (void)testDisablePinningForSubdomainWithoutParentAndNoPublicKey
{
XCTAssertThrows(parseTrustKitConfiguration(@{kTSKPinnedDomains : @{
@"good.com" : @{
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
],
// IncludeSubdomains set to NO so the configuration here is invalid
kTSKIncludeSubdomains: @NO},
@"unsecured.good.com": @{
kTSKExcludeSubdomainFromParentPolicy: @YES
}
}
}),
@"Configuration with kTSKExcludeSubdomainFromParentPolicy must have a parent");
}
- (void)testDisablePinningForSubdomainAdditionalDomainKeys
{
XCTAssertThrows(parseTrustKitConfiguration(@{kTSKPinnedDomains : @{
@"good.com" : @{
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
],
kTSKIncludeSubdomains: @YES},
@"unsecured.good.com": @{
// When using this option, TrustKit should reject additional keys for the domain
kTSKExcludeSubdomainFromParentPolicy: @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
],
}
}
}),
@"Configuration with kTSKExcludeSubdomainFromParentPolicy must reject additional domain keys");
}
- (void)testNokTSKSwizzleNetworkDelegates
{
XCTAssertThrows(parseTrustKitConfiguration(@{kTSKPinnedDomains : @{
@"www.good.com" : @{
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
]}}}),
@"Configuration that does not specify kTSKSwizzleNetworkDelegates must be rejected");
}
- (void)testGetConfigurationPinningEnabled
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains : @{
@"www.good.com" : @{
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"www.good.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertEqualObjects(serverConfigKey, @"www.good.com", @"Did not receive a configuration for a pinned domain");
// Validate the content of the config
NSDictionary *serverConfig = trustKitConfig[kTSKPinnedDomains][serverConfigKey];
XCTAssertNil(serverConfig[kTSKExpirationDate]);
XCTAssertEqual([serverConfig[kTSKPublicKeyHashes] count], (unsigned long) 2);
}
- (void)testGetConfigurationPinningEnabledWithExpirationDate
{
NSString *expirationDateStr = @"2015-01-01";
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains : @{
@"www.good.com" : @{
kTSKExpirationDate: expirationDateStr,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"www.good.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertEqualObjects(serverConfigKey, @"www.good.com", @"Did not receive a configuration for a pinned domain");
// Validate the content of the config
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = @"yyyy-MM-dd";
dateFormat.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *expirationDate = [dateFormat dateFromString:expirationDateStr];
NSDictionary *serverConfig = trustKitConfig[kTSKPinnedDomains][serverConfigKey];
XCTAssertEqualObjects(expirationDate, serverConfig[kTSKExpirationDate]);
XCTAssertEqual([serverConfig[kTSKPublicKeyHashes] count], (unsigned long)2);
}
- (void)testGetConfigurationPinningDisabled
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains : @{
@"good.com" : @{
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
// Ensure www.datatheorem.com gets no configuration
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"www.datatheorem.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertNil(serverConfigKey, @"Received a configuration a non-pinned domain");
}
- (void)testIncludeSubdomainsEnabled
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains : @{
@"good.com" : @{
kTSKIncludeSubdomains : @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
// Ensure www.good.com gets the configuration set for good.com as includeSubdomains is enabled
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"www.good.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertEqualObjects(serverConfigKey, @"good.com", @"IncludeSubdomains did not work");
}
- (void)testIncludeSubdomainsEnabledSameDomain
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains : @{@"good.com" : @{
kTSKIncludeSubdomains : @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
// Ensure good.com gets the configuration set for good.com as includeSubdomains is enabled
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"good.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertEqualObjects(serverConfigKey, @"good.com", @"IncludeSubdomains did not work");
}
- (void)testIncludeSubdomainsEnabledSubSubDomain
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains :
@{@"www.good.com" : @{
kTSKIncludeSubdomains : @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"sub.www.good.com.www.good.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertEqualObjects(serverConfigKey, @"www.good.com", @"IncludeSubdomains did not work");
}
- (void)testIncludeSubdomainsEnabledNotSubdomain
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains :
@{@"good.com" : @{
kTSKIncludeSubdomains : @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
// Corner case to ensure two different domains with similar strings don't get returned as subdomains
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"good.com.otherdomaingood.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertNil(serverConfigKey);
}
- (void)testIncludeSubdomainsEnabledNotSubdomainDifferentTld
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains :
@{@"good.net" : @{
kTSKIncludeSubdomains : @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
// Corner case to ensure two different domains (because different TLD) with similar strings don't get returned as subdomains
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"test.good.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertNil(serverConfigKey);
}
- (void)testIncludeSubdomainsEnabledForSuffix
{
XCTAssertThrows(parseTrustKitConfiguration(@{kTSKSwizzleNetworkDelegates: @NO,
kTSKPinnedDomains :
@{@"com" : @{
kTSKIncludeSubdomains : @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}}),
@"Configuration that pins *.com must be rejected");
}
- (void)testIncludeSubdomainsDisabled
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains :
@{@"good.com" : @{
kTSKIncludeSubdomains : @NO,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
// Ensure www.good.com does not get the configuration set for good.com
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"www.good.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertNil(serverConfigKey, @"IncludeSubdomains did not work");
}
- (void)testIncludeSubdomainsEnabledAndSpecificConfiguration
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains :
@{@"good.com" : @{
kTSKIncludeSubdomains : @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]},
@"www.good.com": @{
kTSKPublicKeyHashes : @[@"iQMk4onrJJz/nwW1wCUR0Ycsh3omhbM+PqMEwNof/K0=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
// Ensure the configuration specific to www.good.com takes precedence over the more general config for good.com
NSString *serverConfigKey = getPinningConfigurationKeyForDomain(@"www.good.com", trustKitConfig[kTSKPinnedDomains]);
XCTAssertEqualObjects(serverConfigKey, @"www.good.com",
@"IncludeSubdomains took precedence over a more specialized configuration");
}
- (void)testNoPinnedDomains
{
XCTAssertThrows(parseTrustKitConfiguration(@{kTSKSwizzleNetworkDelegates : @YES}),
@"Configuration with no pinned domains must be rejected");
}
- (void)testGlobalSettings
{
NSDictionary *trustKitConfig;
trustKitConfig = parseTrustKitConfiguration(@{kTSKPinnedDomains :
@{@"good.com" : @{
kTSKIncludeSubdomains : @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}});
// Ensure the kTSKSwizzleNetworkDelegates setting was saved
XCTAssertFalse([trustKitConfig[kTSKSwizzleNetworkDelegates] boolValue],
@"kTSKSwizzleNetworkDelegates was not saved in the configuration");
}
- (void)testSwizzleNetworkDelegatesInLocalInstance
{
NSDictionary *trustKitConfig;
// Swizzling can only be enabled in the shared instance
trustKitConfig = @{kTSKSwizzleNetworkDelegates: @YES,
kTSKPinnedDomains :
@{@"good.com" : @{
kTSKIncludeSubdomains : @YES,
kTSKPublicKeyHashes : @[@"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" // Fake key
]}}};
XCTAssertThrows([[TrustKit alloc] initWithConfiguration:trustKitConfig]);
}
@end