Skip to content

Commit

Permalink
Simplify authentication handlers in delegate proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
nabla-c0d3 committed Feb 9, 2016
1 parent dea41b2 commit 6574121
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 68 deletions.
33 changes: 2 additions & 31 deletions TrustKit/Swizzling/TSKNSURLConnectionDelegateProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -206,37 +206,7 @@ - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticatio
// Check the trust object against the pinning policy
trustDecision = [TSKPinningValidator evaluateTrust:serverTrust forHostname:serverHostname];
_lastTrustDecision = trustDecision;
if (trustDecision == TSKTrustDecisionShouldAllowConnection)
{
// Success - don't do anything and forward the challenge to the original delegate
wasChallengeHandled = NO;
}
else if (trustDecision == TSKTrustDecisionDomainNotPinned)
{
if ([self forwardToOriginalDelegateAuthenticationChallenge:challenge forConnection:connection])
{
// The original delegate handled the challenge and performed SSL validation itself
wasChallengeHandled = YES;
}
else
{
// The original delegate does not have authentication handlers for this challenge
// We need to do the default validation ourselves to avoid disabling SSL validation for all non pinned domains
TSKLog(@"Performing default certificate validation for %@", serverHostname);
SecTrustResultType trustResult = 0;
SecTrustEvaluate(serverTrust, &trustResult);
if ((trustResult != kSecTrustResultUnspecified) && (trustResult != kSecTrustResultProceed))
{
// Default SSL validation failed - block the connection
CFDictionaryRef evaluationDetails = SecTrustCopyResult(serverTrust);
TSKLog(@"Error: default SSL validation failed: %@", evaluationDetails);
CFRelease(evaluationDetails);
wasChallengeHandled = YES;
[challenge.sender cancelAuthenticationChallenge:challenge];
}
}
}
else
if (trustDecision == TSKTrustDecisionShouldBlockConnection)
{
// Pinning validation failed - block the connection
wasChallengeHandled = YES;
Expand All @@ -247,6 +217,7 @@ - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticatio
// Forward all challenges (including client auth challenges) to the original delegate
if (wasChallengeHandled == NO)
{
// We will also get here if the pinning validation succeeded or the domain was not pinned
if ([self forwardToOriginalDelegateAuthenticationChallenge:challenge forConnection:connection] == NO)
{
// The original delegate could not handle the challenge; use the default handler
Expand Down
41 changes: 4 additions & 37 deletions TrustKit/Swizzling/TSKNSURLSessionDelegateProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,37 +184,7 @@ - (void)URLSession:(NSURLSession * _Nonnull)session
// Check the trust object against the pinning policy
trustDecision = [TSKPinningValidator evaluateTrust:serverTrust forHostname:serverHostname];
_lastTrustDecision = trustDecision;
if (trustDecision == TSKTrustDecisionShouldAllowConnection)
{
// Success - don't do anything and forward the challenge to the original delegate
wasChallengeHandled = NO;
}
else if (trustDecision == TSKTrustDecisionDomainNotPinned)
{
if ([self forwardToOriginalDelegateAuthenticationChallenge:challenge completionHandler:completionHandler forSession:session])
{
// The original delegate handled the challenge and performed SSL validation itself
wasChallengeHandled = YES;
}
else
{
// The original delegate does not have authentication handlers for this challenge
// We need to do the default validation ourselves to avoid disabling SSL validation for all non pinned domains
TSKLog(@"Performing default certificate validation for %@", serverHostname);
SecTrustResultType trustResult = 0;
SecTrustEvaluate(serverTrust, &trustResult);
if ((trustResult != kSecTrustResultUnspecified) && (trustResult != kSecTrustResultProceed))
{
// Default SSL validation failed - block the connection
CFDictionaryRef evaluationDetails = SecTrustCopyResult(serverTrust);
TSKLog(@"Error: default SSL validation failed: %@", evaluationDetails);
CFRelease(evaluationDetails);
wasChallengeHandled = YES;
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, NULL);
}
}
}
else
if (trustDecision == TSKTrustDecisionShouldBlockConnection)
{
// Pinning validation failed - block the connection
wasChallengeHandled = YES;
Expand All @@ -225,6 +195,7 @@ - (void)URLSession:(NSURLSession * _Nonnull)session
// Forward all challenges (including client auth challenges) to the original delegate
if (wasChallengeHandled == NO)
{
// We will also get here if the pinning validation succeeded or the domain was not pinned
if ([self forwardToOriginalDelegateAuthenticationChallenge:challenge completionHandler:completionHandler forSession:session] == NO)
{
// The original delegate could not handle the challenge; use the default handler
Expand All @@ -250,12 +221,7 @@ - (void)URLSession:(NSURLSession * _Nonnull)session
trustDecision = [TSKPinningValidator evaluateTrust:challenge.protectionSpace.serverTrust
forHostname:challenge.protectionSpace.host];
_lastTrustDecision = trustDecision;
if ((trustDecision == TSKTrustDecisionShouldAllowConnection) || (trustDecision == TSKTrustDecisionDomainNotPinned))
{
// Don't do anything and forward the challenge to the original delegate
wasChallengeHandled = NO;
}
else
if (trustDecision == TSKTrustDecisionShouldBlockConnection)
{
// Pinning validation failed - block the connection
wasChallengeHandled = YES;
Expand All @@ -266,6 +232,7 @@ - (void)URLSession:(NSURLSession * _Nonnull)session
// Forward all challenges (including client auth challenges) to the original delegate
if (wasChallengeHandled == NO)
{
// We will also get here if the pinning validation succeeded or the domain was not pinned
// If we're in this delegate method (and not URLSession:didReceiveChallenge:completionHandler:)
// it means the delegate definitely implements the handler method so we can call it directly
[originalDelegate URLSession:session task:task didReceiveChallenge:challenge completionHandler:completionHandler];
Expand Down

0 comments on commit 6574121

Please sign in to comment.