TrustKit

Build Status Carthage compatible Version Status Platform License MIT Gitter chat

TrustKit 是一个开源框架,可以轻松地在任何 iOS 12+、macOS 10.13+、tvOS 12+ 或 watchOS 4+ 应用中部署 SSL 公钥绑定和报告;它同时支持 Swift 和 Objective-C 应用。

如果您的 Android 应用需要 SSL 绑定/报告,我们也发布了 TrustKit for Android,地址为 https://github.com/datatheorem/TrustKit-Android

概述

TrustKit 提供以下功能:

入门

示例用法

在应用程序中部署 SSL 绑定需要使用绑定策略(域名、Subject Public Key Info 哈希和附加设置)初始化 TrustKit

可以在应用程序的 Info.plist 中配置策略。

Info.plist policy

或者,可以通过编程方式设置绑定策略。

    NSDictionary *trustKitConfig =
  @{
    kTSKSwizzleNetworkDelegates: @NO,
    kTSKPinnedDomains : @{
            @"www.datatheorem.com" : @{
                    kTSKExpirationDate: @"2017-12-01",
                    kTSKPublicKeyHashes : @[
                            @"HXXQgxueCIU5TTLHob/bPbwcKOKw6DkfsTWYHbxbqTY=",
                            @"0SDf3cRToyZJaMsoS17oF72VMavLxj/N7WBNasNuiR8="
                            ],
                    kTSKEnforcePinning : @NO,
                    },
            @"yahoo.com" : @{
                    kTSKPublicKeyHashes : @[
                            @"TQEtdMbmwFgYUifM4LDF+xgEtd0z69mPGmkp014d6ZY=",
                            @"rFjc3wG7lTZe43zeYTvPq8k4xdDEutCmIhI5dn4oCeE=",
                            ],
                    kTSKIncludeSubdomains : @YES
                    }
            }};
    
    [TrustKit initSharedInstanceWithConfiguration:trustKitConfig];

也可以在 Swift 应用中通过编程方式设置策略。

        let trustKitConfig = [
            kTSKSwizzleNetworkDelegates: false,
            kTSKPinnedDomains: [
                "yahoo.com": [
                    kTSKExpirationDate: "2017-12-01",
                    kTSKPublicKeyHashes: [
                        "JbQbUG5JMJUoI6brnx0x3vZF6jilxsapbXGVfjhN8Fg=",
                        "WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="
                    ],]]] as [String : Any]
        
        TrustKit.initSharedInstance(withConfiguration:trustKitConfig)

初始化 TrustKit 后,可以从 TrustKit 单例中检索 TSKPinningValidator 实例,并可用于在应用程序的网络代理中执行 SSL 绑定验证。例如,在 NSURLSessionDelegate 中:

- (void)URLSession:(NSURLSession *)session 
              task:(NSURLSessionTask *)task 
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge 
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
{
    TSKPinningValidator *pinningValidator = [[TrustKit sharedInstance] pinningValidator];
    // Pass the authentication challenge to the validator; if the validation fails, the connection will be blocked
    if (![pinningValidator handleChallenge:challenge completionHandler:completionHandler])
    {
        // TrustKit did not handle this challenge: perhaps it was not for server trust
        // or the domain was not pinned. Fall back to the default behavior
        completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
    }
}

有关更多信息,请参阅入门指南

鸣谢

TrustKit 是 Data Theorem 和 Yahoo 移动团队的共同努力。有关详细信息,请参见 AUTHORS

许可证

TrustKit 在 MIT 许可证下发布。有关详细信息,请参见 LICENSE