TSKPinningValidator.h 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. TSKPinningValidator.h
  3. TrustKit
  4. Copyright 2015 The TrustKit Project Authors
  5. Licensed under the MIT license, see associated LICENSE file for terms.
  6. See AUTHORS file for the list of project authors.
  7. */
  8. #import "TSKTrustDecision.h"
  9. #if __has_feature(modules)
  10. @import Foundation;
  11. #else
  12. #import <Foundation/Foundation.h>
  13. #endif
  14. @class TSKPinningValidatorResult;
  15. @class TSKSPKIHashCache;
  16. /**
  17. A `TSKPinningValidator` instance can be used to verify a server's identity against an SSL pinning policy.
  18. In specific scenarios, TrustKit cannot intercept outgoing SSL connections and automatically validate the server's identity against the pinning policy:
  19. * All connections within an App that disables TrustKit's network delegate swizzling by setting the `kTSKSwizzleNetworkDelegates` configuration key to `NO`.
  20. * Connections that do not rely on the `NSURLConnection` or `NSURLSession` APIs:
  21. * `WKWebView` connections.
  22. * Connections leveraging low-level network APIs (such as `NSStream`).
  23. * Connections initiated using a third-party SSL library such as OpenSSL.
  24. For these connections, pin validation must be manually triggered using one of the two available methods within `TSKPinningValidator`.
  25. */
  26. @interface TSKPinningValidator : NSObject
  27. #pragma mark High-level Validation Method
  28. /**
  29. Helper method for handling authentication challenges received within a `NSURLSessionDelegate`, `NSURLSessionTaskDelegate` or `WKNavigationDelegate`.
  30. This method will evaluate the server trust within the authentication challenge against the global SSL pinning policy previously configured, and then call the `completionHandler` with the corresponding `disposition` and `credential`. For example, this method can be leveraged in a NSURLSessionDelegate challenge handler method:
  31. - (void)URLSession:(NSURLSession *)session
  32. task:(NSURLSessionTask *)task
  33. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  34. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
  35. {
  36. TSKPinningValidator *pinningValidator = [[TrustKit sharedInstance] pinningValidator];
  37. // Pass the authentication challenge to the validator; if the validation fails, the connection will be blocked
  38. if (![pinningValidator handleChallenge:challenge completionHandler:completionHandler])
  39. {
  40. // TrustKit did not handle this challenge: perhaps it was not for server trust
  41. // or the domain was not pinned. Fall back to the default behavior
  42. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  43. }
  44. }
  45. @param challenge The authentication challenge, supplied by the URL loading system to the delegate's challenge handler method.
  46. @param completionHandler A block to invoke to respond to the challenge, supplied by the URL loading system to the delegate's challenge handler method.
  47. @return `YES` if the challenge was handled and the `completionHandler` was successfuly invoked. `NO` if the challenge could not be handled because it was not for server certificate validation (ie. the challenge's `authenticationMethod` was not `NSURLAuthenticationMethodServerTrust`).
  48. @exception NSException Thrown when TrustKit has not been initialized with a pinning policy.
  49. */
  50. - (BOOL)handleChallenge:(NSURLAuthenticationChallenge * _Nonnull)challenge
  51. completionHandler:(void (^ _Nonnull)(NSURLSessionAuthChallengeDisposition disposition,
  52. NSURLCredential * _Nullable credential))completionHandler;
  53. #pragma mark Low-level Validation Method
  54. /**
  55. Evaluate the supplied server trust against the SSL pinning policy previously configured. If the validation fails, a pin failure report will be sent.
  56. When using the `NSURLSession` or `WKWebView` network APIs, the `handleChallenge:completionHandler:` method should be called instead, as it is simpler to use.
  57. When using low-level network APIs (such as `NSStream`), instructions on how to retrieve the connection's `serverTrust` are available at https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/OverridingSSLChainValidationCorrectly.html .
  58. @param serverTrust The trust object representing the server's certificate chain. The trust's evaluation policy is always overridden using `SecTrustSetPolicies()` to ensure all the proper SSL checks (expiration, hostname validation, etc.) are enabled.
  59. @param serverHostname The hostname of the server whose identity is being validated.
  60. @return A `TSKTrustDecision` which describes whether the SSL connection should be allowed or blocked, based on the configured pinning policy.
  61. @warning If no SSL pinning policy was configured for the supplied _serverHostname_, this method has no effect and will return `TSKTrustDecisionDomainNotPinned` without validating the supplied _serverTrust_ at all. This means that the server's _serverTrust_ object __must__ be verified against the device's trust store using `SecTrustEvaluate()`. Failing to do so will __disable SSL certificate validation__.
  62. @exception NSException Thrown when TrustKit has not been initialized with a pinning policy.
  63. */
  64. - (TSKTrustDecision)evaluateTrust:(SecTrustRef _Nonnull)serverTrust forHostname:(NSString * _Nonnull)serverHostname;
  65. @end