SRSecurityPolicy.m 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Copyright (c) 2016-present, Facebook, Inc.
  3. // All rights reserved.
  4. //
  5. // This source code is licensed under the BSD-style license found in the
  6. // LICENSE file in the root directory of this source tree. An additional grant
  7. // of patent rights can be found in the PATENTS file in the same directory.
  8. //
  9. #import "SRSecurityPolicy.h"
  10. #import "SRPinningSecurityPolicy.h"
  11. NS_ASSUME_NONNULL_BEGIN
  12. @interface SRSecurityPolicy ()
  13. @property (nonatomic, assign, readonly) BOOL certificateChainValidationEnabled;
  14. @end
  15. @implementation SRSecurityPolicy
  16. + (instancetype)defaultPolicy
  17. {
  18. return [self new];
  19. }
  20. + (instancetype)pinnningPolicyWithCertificates:(NSArray *)pinnedCertificates
  21. {
  22. return [[SRPinningSecurityPolicy alloc] initWithCertificates:pinnedCertificates];
  23. }
  24. - (instancetype)initWithCertificateChainValidationEnabled:(BOOL)enabled
  25. {
  26. self = [super init];
  27. if (!self) { return self; }
  28. _certificateChainValidationEnabled = enabled;
  29. return self;
  30. }
  31. - (instancetype)init
  32. {
  33. return [self initWithCertificateChainValidationEnabled:YES];
  34. }
  35. - (void)updateSecurityOptionsInStream:(NSStream *)stream
  36. {
  37. // Enforce TLS 1.2
  38. [stream setProperty:(__bridge id)CFSTR("kCFStreamSocketSecurityLevelTLSv1_2") forKey:(__bridge id)kCFStreamPropertySocketSecurityLevel];
  39. // Validate certificate chain for this stream if enabled.
  40. NSDictionary<NSString *, id> *sslOptions = @{ (__bridge NSString *)kCFStreamSSLValidatesCertificateChain : @(self.certificateChainValidationEnabled) };
  41. [stream setProperty:sslOptions forKey:(__bridge NSString *)kCFStreamPropertySSLSettings];
  42. }
  43. - (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust forDomain:(NSString *)domain
  44. {
  45. // No further evaluation happens in the default policy.
  46. return YES;
  47. }
  48. @end
  49. NS_ASSUME_NONNULL_END