TrustKit.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. TrustKit.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. #if __has_feature(modules)
  9. @import Foundation;
  10. #else
  11. #import <Foundation/Foundation.h>
  12. #endif
  13. #ifndef _TRUSTKIT_
  14. #define _TRUSTKIT_
  15. #import "TSKTrustKitConfig.h"
  16. #import "TSKPinningValidatorResult.h"
  17. #import "TSKPinningValidatorCallback.h"
  18. #import "TSKPinningValidator.h"
  19. #import "TSKTrustDecision.h"
  20. #endif /* _TRUSTKIT_ */
  21. NS_ASSUME_NONNULL_BEGIN
  22. /**
  23. `TrustKit` is the main class for configuring an SSL pinning policy within an App.
  24. For most Apps, TrustKit should be used as a singleton, where a global SSL pinning policy is
  25. configured for the App. In singleton mode, the policy can be set either:
  26. * By adding it to the App's _Info.plist_ under the `TSKConfiguration` key, or
  27. * By programmatically supplying it using the `+initSharedInstanceWithConfiguration:` method.
  28. In singleton mode, TrustKit can only be initialized once so only one of the two techniques
  29. should be used.
  30. For more complex Apps where multiple SSL pinning policies need to be used independently
  31. (for example within different frameworks), TrustKit can be used in "multi-instance" mode
  32. by leveraging the `-initWithConfiguration:` method described at the end of this page.
  33. A TrustKit pinning policy is a dictionary which contains some global, App-wide settings
  34. (of type `TSKGlobalConfigurationKey`) as well as domain-specific configuration keys
  35. (of type `TSKDomainConfigurationKey`) to be defined under the `kTSKPinnedDomains` entry.
  36. The following table shows the keys and the types of the corresponding values, and uses
  37. indentation to indicate structure:
  38. ```
  39. | Key | Type |
  40. |----------------------------------------------|------------|
  41. | TSKSwizzleNetworkDelegates | Boolean |
  42. | TSKIgnorePinningForUserDefinedTrustAnchors | Boolean |
  43. | TSKPinnedDomains | Dictionary |
  44. | __ <domain-name-to-pin-as-string> | Dictionary |
  45. | ____ TSKPublicKeyHashes | Array |
  46. | ____ TSKIncludeSubdomains | Boolean |
  47. | ____ TSKExcludeSubdomainFromParentPolicy | Boolean |
  48. | ____ TSKEnforcePinning | Boolean |
  49. | ____ TSKReportUris | Array |
  50. | ____ TSKDisableDefaultReportUri | Boolean |
  51. ```
  52. When setting the pinning policy programmatically, it has to be supplied to the
  53. `initSharedInstanceWithConfiguration:` method as a dictionary in order to initialize
  54. TrustKit. For example:
  55. ```
  56. NSDictionary *trustKitConfig =
  57. @{
  58. kTSKPinnedDomains : @{
  59. @"www.datatheorem.com" : @{
  60. kTSKExpirationDate: @"2017-12-01",
  61. kTSKPublicKeyHashes : @[
  62. @"HXXQgxueCIU5TTLHob/bPbwcKOKw6DkfsTWYHbxbqTY=",
  63. @"0SDf3cRToyZJaMsoS17oF72VMavLxj/N7WBNasNuiR8="
  64. ],
  65. kTSKEnforcePinning : @NO,
  66. kTSKReportUris : @[@"http://report.datatheorem.com/log_report"],
  67. },
  68. @"yahoo.com" : @{
  69. kTSKPublicKeyHashes : @[
  70. @"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
  71. @"rFjc3wG7lTZe43zeYTvPq8k4xdDEutCmIhI5dn4oCeE=",
  72. ],
  73. kTSKIncludeSubdomains : @YES
  74. }
  75. }};
  76. [TrustKit initSharedInstanceWithConfiguration:trustKitConfig];
  77. trustKit = [TrustKit sharedInstance];
  78. ```
  79. Similarly, the TrustKit singleton can be initialized in Swift:
  80. ```
  81. let trustKitConfig = [
  82. kTSKSwizzleNetworkDelegates: false,
  83. kTSKPinnedDomains: [
  84. "yahoo.com": [
  85. kTSKExpirationDate: "2017-12-01",
  86. kTSKPublicKeyHashes: [
  87. "JbQbUG5JMJUoI6brnx0x3vZF6jilxsapbXGVfjhN8Fg=",
  88. "WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="
  89. ],]]] as [String : Any]
  90. TrustKit.initSharedInstance(withConfiguration:trustKitConfig)
  91. ```
  92. After initialization, the `TrustKit` instance's `pinningValidator` should be used to implement
  93. pinning validation within the App's network authentication handlers.
  94. */
  95. @interface TrustKit : NSObject
  96. #pragma mark Usage in Singleton Mode
  97. /**
  98. See `+initSharedInstanceWithConfiguration:sharedContainerIdentifier:`
  99. @param trustKitConfig A dictionary containing various keys for configuring the SSL pinning policy.
  100. @exception NSException Thrown when the supplied configuration is invalid or TrustKit has
  101. already been initialized.
  102. */
  103. + (void)initSharedInstanceWithConfiguration:(NSDictionary<TSKGlobalConfigurationKey, id> *)trustKitConfig;
  104. /**
  105. Initialize the global TrustKit singleton with the supplied pinning policy.
  106. @param trustKitConfig A dictionary containing various keys for configuring the SSL pinning policy.
  107. @param sharedContainerIdentifier The container identifier for an app extension. This must be set in order
  108. for reports to be sent from an app extension. See
  109. https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1409450-sharedcontaineridentifier
  110. @exception NSException Thrown when the supplied configuration is invalid or TrustKit has
  111. already been initialized.
  112. */
  113. + (void)initSharedInstanceWithConfiguration:(NSDictionary<TSKGlobalConfigurationKey, id> *)trustKitConfig
  114. sharedContainerIdentifier:(nullable NSString *)sharedContainerIdentifier;
  115. /**
  116. Retrieve the global TrustKit singleton instance. Raises an exception if `+initSharedInstanceWithConfiguration:`
  117. has not yet been invoked.
  118. @return the shared TrustKit singleton
  119. */
  120. + (instancetype)sharedInstance;
  121. #pragma mark Implementing Pinning Validation
  122. /**
  123. Retrieve the validator instance conforming to the pinning policy of this TrustKit instance.
  124. The validator should be used to implement pinning validation within the App's network
  125. authentication handlers.
  126. */
  127. @property (nonatomic, nonnull) TSKPinningValidator *pinningValidator;
  128. #pragma mark Configuring a Validation Callback
  129. /**
  130. Register a block to be invoked for every request that is going through TrustKit's pinning
  131. validation mechanism. See `TSKPinningValidatorCallback` for more information.
  132. */
  133. @property (nonatomic, nullable) TSKPinningValidatorCallback pinningValidatorCallback;
  134. /**
  135. Queue on which to invoke the `pinningValidatorCallback`; default value is the main queue.
  136. */
  137. @property (nonatomic, null_resettable) dispatch_queue_t pinningValidatorCallbackQueue;
  138. #pragma mark Usage in Multi-Instance Mode
  139. /**
  140. See `-initWithConfiguration:sharedContainerIdentifier:`
  141. @param trustKitConfig A dictionary containing various keys for configuring the SSL pinning policy.
  142. */
  143. - (instancetype)initWithConfiguration:(NSDictionary<TSKGlobalConfigurationKey, id> *)trustKitConfig;
  144. /**
  145. Initialize a local TrustKit instance with the supplied SSL pinning policy configuration.
  146. This method is useful in scenarios where the TrustKit singleton cannot be used, for example within
  147. larger Apps that have split some of their functionality into multiple framework/SDK. Each
  148. framework can initialize its own instance of TrustKit and use it for pinning validation independently
  149. of the App's other components.
  150. @param trustKitConfig A dictionary containing various keys for configuring the SSL pinning policy.
  151. @param sharedContainerIdentifier The container identifier for an app extension. This must be set in order
  152. for reports to be sent from an app extension. See
  153. https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1409450-sharedcontaineridentifier
  154. */
  155. - (instancetype)initWithConfiguration:(NSDictionary<TSKGlobalConfigurationKey, id> *)trustKitConfig
  156. sharedContainerIdentifier:(nullable NSString *)sharedContainerIdentifier;
  157. #pragma mark Other Settings
  158. /**
  159. Set the global logger.
  160. This method sets the global logger, used when any `TrustKit` instance needs to display a message to
  161. the developer.
  162. If a global logger is not set, the default logger will be used, which will only print TrustKit log
  163. messages (using `NSLog()`) when the App is built in Debug mode. If the App was built for Release, the default
  164. logger will not print any messages at all.
  165. */
  166. + (void)setLoggerBlock:(void (^)(NSString *))block;
  167. @end
  168. NS_ASSUME_NONNULL_END