【问题标题】:Implementing ssl pinning in a react-native application using TrustKit iOS使用 TrustKit iOS 在 react-native 应用程序中实现 ssl pinning
【发布时间】:2019-09-02 10:34:31
【问题描述】:

我正在尝试在 react-native 应用程序 (RN 0.60) 中实现 SSL pinning,并且我正在使用 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它可以正常工作,进入catch并说:

Error: Pin verification failed

【问题讨论】:

    标签: ios react-native react-native-ios trustkit


    【解决方案1】:

    所以,我又回到了这个问题上并再次尝试并让它工作。我当前的代码与我前段时间发布的代码的唯一区别是我在特定的固定域中添加了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 =&gt; cancelled

    【讨论】:

    • 您是否尝试在实现后运行 Charles 日志?有效吗?
    【解决方案2】:

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

    【讨论】:

    • 在我发布的示例中,有 2 个哈希值。我使用了 appDelegate 方法,因为它比使用 info.plist 更具可读性(对我来说)。还是您在谈论其他事情?
    • 是的。你的例子是正确的。我的意思是当我做我的项目时,我只放了 1 个哈希值,应用程序在构建时崩溃了。检查了几个小时后,发现应用程序由于缺少备份哈希而崩溃。只是为了分享我必须提供 2 个哈希的经验。
    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 2022-06-11
    • 2017-12-11
    • 2017-01-09
    • 1970-01-01
    • 2019-03-22
    • 2021-10-18
    • 2021-10-30
    相关资源
    最近更新 更多