Skip to content

Commit

Permalink
Added test and implementation for self signed cert against default se…
Browse files Browse the repository at this point in the history
…curity

Without this change the whole library can default to letting certs
through with various settings that would suggest otherwise. And there
was not a test to check this.

Also corrected the note an adjacent test, which was incongruous with
the test and the title of the test.
  • Loading branch information
duttski committed Mar 25, 2015
1 parent 50f90bc commit 8cfb272
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
13 changes: 10 additions & 3 deletions AFNetworking/AFSecurityPolicy.m
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,21 @@ - (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust

SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef)policies);

if (self.SSLPinningMode != AFSSLPinningModeNone && !AFServerTrustIsValid(serverTrust) && !self.allowInvalidCertificates) {
if(self.SSLPinningMode == AFSSLPinningModeNone) {
if(self.allowInvalidCertificates || AFServerTrustIsValid(serverTrust)){
return YES;
} else {
return NO;
}
} else if(!AFServerTrustIsValid(serverTrust) && !self.allowInvalidCertificates) {
return NO;
}

NSArray *serverCertificates = AFCertificateTrustChainForServerTrust(serverTrust);
switch (self.SSLPinningMode) {
case AFSSLPinningModeNone:
return YES;
default:
return NO;
case AFSSLPinningModeCertificate: {
NSMutableArray *pinnedCertificates = [NSMutableArray array];
for (NSData *certificateData in self.pinnedCertificates) {
Expand Down
11 changes: 9 additions & 2 deletions Tests/Tests/AFSecurityPolicyTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,17 @@ - (void)testDefaultPolicySetToPublicKeyChainFailsWithMissingChain {
CFRelease(trust);
}

- (void)testDefaultPolicyIsSetToAFSSLPinningModePublicKey {
- (void)testDefaultPolicyIsSetToAFSSLPinningModeNone {
AFSecurityPolicy *policy = [AFSecurityPolicy defaultPolicy];

XCTAssert(policy.SSLPinningMode==AFSSLPinningModeNone, @"Default policy is not set to AFSSLPinningModePublicKey.");
XCTAssert(policy.SSLPinningMode==AFSSLPinningModeNone, @"Default policy is not set to AFSSLPinningModeNone.");
}

- (void)testDefaultPolicyFailsToEvaluateServerTrustFromSelfSignedCertificate {
AFSecurityPolicy *policy = [AFSecurityPolicy defaultPolicy];
SecTrustRef trust = AFUTTrustWithCertificate(AFUTSelfSignedCertificateWithoutDomain());
XCTAssert([policy evaluateServerTrust:trust forDomain:nil] == NO, @"Unmatched certificate and pinning mode None should fail");
CFRelease(trust);
}

- (void)testDefaultPolicyIsSetToNotAllowInvalidSSLCertificates {
Expand Down

0 comments on commit 8cfb272

Please sign in to comment.