使用 TrustKit iOS 在 react-native 应用程序中实现 ssl pinning

Implementing ssl pinning in a react-native application using TrustKit iOS

我正在尝试在本机反应应用程序 (RN 0.60) 中实施 SSL 固定,并且我正在使用 Trustkit。

按照 https://github.com/datatheorem/TrustKit 中发布的指南,这些是我完成的步骤:

1) 使用 pod 'TrustKit'pod install

安装 TrustKit pod

2) 添加到我的AppDelegate.m这段代码:

#import <TrustKit/TrustKit.h>

//inside didFinishLaunchingWithOptions

NSDictionary *trustKitConfig =
  @{
    kTSKSwizzleNetworkDelegates: @YES,
    kTSKPinnedDomains: @{
        @"www.datatheorem.com" : @{
            kTSKEnforcePinning:@YES,
            kTSKIncludeSubdomains:@YES,
            //Using wrong hashes so it fails
            kTSKPublicKeyHashes : @[
                @"Ca5gV6n7OVx4AxtEaIk8NI9qyKBTtKJjwqullb/v9hh=",
                @"YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihh="
                ]
            }}};

  [TrustKit initSharedInstanceWithConfiguration:trustKitConfig];

当我尝试做

 RNFetchBlob.fetch('GET', "https://www.datatheorem.com", {})    //tried using standard fetch() but gives same results
    .then(async(res) => {
        console.log('RES => ' ,res)
    })
    // Something went wrong:
    .catch((err) => {
        console.log('ERROR =>', err);
    })

它进入 then 并且没有给出任何错误但以 200 状态代码(使用错误的哈希值)响应。

否则,使用 Android 它可以正常工作,进入捕获并说:

Error: Pin verification failed

所以,我又回到了这里并再次尝试并让它工作。我当前的代码与我之前发布的代码的唯一区别是我在特定的固定域中添加了 kTSKPublicKeyAlgorithms : @[kTSKAlgorithmRsa2048]

我已按照我在问题中发布的相同步骤进行操作。最后的 AppDelegate 看起来像:

return YES 之前的 didFinishLaunchingWithOptions 内,我添加了:

  [self initTrustKit];

然后在 didFinishLaunchingWithOptions 的括号后添加:

- (void)initTrustKit {
      NSDictionary *trustKitConfig =
  @{
    kTSKSwizzleNetworkDelegates: @YES,                    
    kTSKPinnedDomains : @{
            @"www.datatheorem.com" : @{
              kTSKEnforcePinning : @YES,
              kTSKIncludeSubdomains:@YES,
                    kTSKPublicKeyHashes : @[
                        @"Ca5gV6n7OVx4AxtEaIk8NI9qyKBTtKJjwqullb/v9hh=",
                        @"YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihh="
                            ],
              kTSKPublicKeyAlgorithms : @[kTSKAlgorithmRsa2048],
                    },
            }};
    [TrustKit initSharedInstanceWithConfiguration:trustKitConfig];
}

它在 iOS 中不起作用,返回捕获并打印:ERROR => cancelled

我已经在 Info.plist 中配置了 TrustKit。 我还注意到,即使你只有 1 个 PublicKeyHash,你也必须提供一个虚拟的 Trustkit 才能在 iOS 应用程序中工作。