SSLCAHelper.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2013-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "SSLCAHelper.h"
  21. #import "BundleUtil.h"
  22. #import "TrustKit.h"
  23. @implementation SSLCAHelper
  24. + (void)initTrustKit {
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. NSDictionary *trustKitConfig = @{
  28. kTSKSwizzleNetworkDelegates: @NO,
  29. kTSKPinnedDomains: @{
  30. @"threema.ch" : @{
  31. kTSKIncludeSubdomains: @YES,
  32. kTSKPublicKeyHashes : @[
  33. @"8SLubAXo6MrrGziVya6HjCS/Cuc7eqtzw1v6AfIW57c=",
  34. @"8kTK9HP1KHIP0sn6T2AFH3Bq+qq3wn2i/OJSMjewpFw=",
  35. @"KKBJHJn1PQSdNTmoAfhxqWTO61r8O8bPi/JeGtP/6gg=",
  36. @"h2gHawxPZyMCiZSkJN0dQ4RsDxowVuTmuiNQyjeU+Sk=",
  37. @"HXqz8rMr6nBDdUX3CdyIwln8ym3qFUBwv4QGyMN2uEg=",
  38. @"2Vpy8qUQCqc2+Lg6BgRO8G6e6vh7NmvVHTljfwP/Pfk=",
  39. @"vGQZ8hm2h+km+q7rnJ7kF9S17BwSY0rbhwjz6nIupf0=",
  40. @"jsQHAHKQ2oOf3rvMn9GJVIKslkhLpODGOMPSxgLeIyo="
  41. ],
  42. kTSKEnforcePinning : @YES,
  43. kTSKReportUris: @[
  44. @"https://3ma.ch/pinreport"
  45. ],
  46. kTSKDisableDefaultReportUri: @YES
  47. },
  48. }
  49. };
  50. [TrustKit initSharedInstanceWithConfiguration:trustKitConfig];
  51. });
  52. }
  53. + (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
  54. return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
  55. }
  56. + (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  57. [SSLCAHelper initTrustKit];
  58. TSKPinningValidator *pinningValidator = [[TrustKit sharedInstance] pinningValidator];
  59. // Pass the authentication challenge to the validator; if the validation fails, the connection will be blocked
  60. [pinningValidator handleChallenge:challenge completionHandler:^(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential) {
  61. switch (disposition) {
  62. case NSURLSessionAuthChallengeUseCredential:
  63. [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
  64. break;
  65. case NSURLSessionAuthChallengePerformDefaultHandling:
  66. [challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
  67. break;
  68. case NSURLSessionAuthChallengeCancelAuthenticationChallenge:
  69. [challenge.sender cancelAuthenticationChallenge:challenge];
  70. break;
  71. case NSURLSessionAuthChallengeRejectProtectionSpace:
  72. [challenge.sender rejectProtectionSpaceAndContinueWithChallenge:challenge];
  73. break;
  74. default:
  75. [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
  76. break;
  77. }
  78. }];
  79. }
  80. + (void)session:(NSURLSession *)session didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completion:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completion {
  81. [SSLCAHelper initTrustKit];
  82. TSKPinningValidator *pinningValidator = [[TrustKit sharedInstance] pinningValidator];
  83. // Pass the authentication challenge to the validator; if the validation fails, the connection will be blocked
  84. [pinningValidator handleChallenge:challenge completionHandler:^(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential) {
  85. switch (disposition) {
  86. case NSURLSessionAuthChallengeUseCredential:
  87. [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
  88. break;
  89. case NSURLSessionAuthChallengePerformDefaultHandling:
  90. [challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
  91. break;
  92. case NSURLSessionAuthChallengeCancelAuthenticationChallenge:
  93. [challenge.sender cancelAuthenticationChallenge:challenge];
  94. break;
  95. case NSURLSessionAuthChallengeRejectProtectionSpace:
  96. [challenge.sender rejectProtectionSpaceAndContinueWithChallenge:challenge];
  97. break;
  98. default:
  99. [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
  100. break;
  101. }
  102. completion(disposition, credential);
  103. }];
  104. }
  105. @end