AFSecurityPolicy.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // AFSecurityPolicy.h
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import <Foundation/Foundation.h>
  22. #import <Security/Security.h>
  23. typedef NS_ENUM(NSUInteger, AFSSLPinningMode) {
  24. AFSSLPinningModeNone,
  25. AFSSLPinningModePublicKey,
  26. AFSSLPinningModeCertificate,
  27. };
  28. /**
  29. `AFSecurityPolicy` evaluates server trust against pinned X.509 certificates and public keys over secure connections.
  30. Adding pinned SSL certificates to your app helps prevent man-in-the-middle attacks and other vulnerabilities. Applications dealing with sensitive customer data or financial information are strongly encouraged to route all communication over an HTTPS connection with SSL pinning configured and enabled.
  31. */
  32. NS_ASSUME_NONNULL_BEGIN
  33. @interface AFSecurityPolicy : NSObject <NSSecureCoding, NSCopying>
  34. /**
  35. The criteria by which server trust should be evaluated against the pinned SSL certificates. Defaults to `AFSSLPinningModeNone`.
  36. */
  37. @property (readonly, nonatomic, assign) AFSSLPinningMode SSLPinningMode;
  38. /**
  39. The certificates used to evaluate server trust according to the SSL pinning mode.
  40. Note that if pinning is enabled, `evaluateServerTrust:forDomain:` will return true if any pinned certificate matches.
  41. @see policyWithPinningMode:withPinnedCertificates:
  42. */
  43. @property (nonatomic, strong, nullable) NSSet <NSData *> *pinnedCertificates;
  44. /**
  45. Whether or not to trust servers with an invalid or expired SSL certificates. Defaults to `NO`.
  46. */
  47. @property (nonatomic, assign) BOOL allowInvalidCertificates;
  48. /**
  49. Whether or not to validate the domain name in the certificate's CN field. Defaults to `YES`.
  50. */
  51. @property (nonatomic, assign) BOOL validatesDomainName;
  52. ///-----------------------------------------
  53. /// @name Getting Certificates from the Bundle
  54. ///-----------------------------------------
  55. /**
  56. Returns any certificates included in the bundle. If you are using AFNetworking as an embedded framework, you must use this method to find the certificates you have included in your app bundle, and use them when creating your security policy by calling `policyWithPinningMode:withPinnedCertificates`.
  57. @return The certificates included in the given bundle.
  58. */
  59. + (NSSet <NSData *> *)certificatesInBundle:(NSBundle *)bundle;
  60. ///-----------------------------------------
  61. /// @name Getting Specific Security Policies
  62. ///-----------------------------------------
  63. /**
  64. Returns the shared default security policy, which does not allow invalid certificates, validates domain name, and does not validate against pinned certificates or public keys.
  65. @return The default security policy.
  66. */
  67. + (instancetype)defaultPolicy;
  68. ///---------------------
  69. /// @name Initialization
  70. ///---------------------
  71. /**
  72. Creates and returns a security policy with the specified pinning mode.
  73. Certificates with the `.cer` extension found in the main bundle will be pinned. If you want more control over which certificates are pinned, please use `policyWithPinningMode:withPinnedCertificates:` instead.
  74. @param pinningMode The SSL pinning mode.
  75. @return A new security policy.
  76. @see -policyWithPinningMode:withPinnedCertificates:
  77. */
  78. + (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode;
  79. /**
  80. Creates and returns a security policy with the specified pinning mode.
  81. @param pinningMode The SSL pinning mode.
  82. @param pinnedCertificates The certificates to pin against.
  83. @return A new security policy.
  84. @see +certificatesInBundle:
  85. @see -pinnedCertificates
  86. */
  87. + (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode withPinnedCertificates:(NSSet <NSData *> *)pinnedCertificates;
  88. ///------------------------------
  89. /// @name Evaluating Server Trust
  90. ///------------------------------
  91. /**
  92. Whether or not the specified server trust should be accepted, based on the security policy.
  93. This method should be used when responding to an authentication challenge from a server.
  94. @param serverTrust The X.509 certificate trust of the server.
  95. @param domain The domain of serverTrust. If `nil`, the domain will not be validated.
  96. @return Whether or not to trust the server.
  97. */
  98. - (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
  99. forDomain:(nullable NSString *)domain;
  100. @end
  101. NS_ASSUME_NONNULL_END
  102. ///----------------
  103. /// @name Constants
  104. ///----------------
  105. /**
  106. ## SSL Pinning Modes
  107. The following constants are provided by `AFSSLPinningMode` as possible SSL pinning modes.
  108. enum {
  109. AFSSLPinningModeNone,
  110. AFSSLPinningModePublicKey,
  111. AFSSLPinningModeCertificate,
  112. }
  113. `AFSSLPinningModeNone`
  114. Do not used pinned certificates to validate servers.
  115. `AFSSLPinningModePublicKey`
  116. Validate host certificates against public keys of pinned certificates.
  117. `AFSSLPinningModeCertificate`
  118. Validate host certificates against pinned certificates.
  119. */