How to pin the Public key of a certificate on iOS

后端 未结 7 963
执笔经年
执笔经年 2020-11-30 18:15

While improving the security of an iOS application that we are developing, we found the need to PIN (the entire or parts of) the SSL certificate of server to prevent man-in-

7条回答
  •  一个人的身影
    2020-11-30 18:51

    If you use AFNetworking (more specifically, AFSecurityPolicy), and you choose the mode AFSSLPinningModePublicKey, it doesn't matter if your certificates change or not, as long as the public keys stay the same. Yes, it is true that AFSecurityPolicy doesn't provide a method for you to directly set your public keys; you can only set your certificates by calling setPinnedCertificates. However, if you look at the implementation of setPinnedCertificates, you'll see that the framework is extracting the public keys from the certificates and then comparing the keys.

    In short, pass in the certificates, and don't worry about them changing in the future. The framework only cares about the public keys in those certificates.

    The following code works for me.

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey];
    [manager.securityPolicy setPinnedCertificates:myCertificate];
    

提交回复
热议问题