@@ -27,8 +27,6 @@ @interface BNCNetworkOperation ()
2727#pragma mark - BNCNetworkService
2828
2929@interface BNCNetworkService () <NSURLSessionDelegate > {
30- NSMutableArray *_pinnedPublicKeys;
31- NSMutableSet <NSString *>*_anySSLCertHosts;
3230 NSOperationQueue *_serviceQueue;
3331 NSURLSession *_session;
3432}
@@ -117,19 +115,6 @@ - (NSInteger) maxConcurrentOperationCount {
117115 return self.serviceQueue .maxConcurrentOperationCount ;
118116}
119117
120- - (NSMutableSet <NSString*>*) anySSLCertHosts {
121- @synchronized (self) {
122- if (!_anySSLCertHosts) _anySSLCertHosts = [NSMutableSet new ];
123- return _anySSLCertHosts;
124- }
125- }
126-
127- - (void ) setAnySSLCertHosts : (NSMutableSet <NSString*>*)anySSLCertHosts_ {
128- @synchronized (self) {
129- _anySSLCertHosts = [anySSLCertHosts_ copy ];
130- }
131- }
132-
133118- (id <BNCNetworkOperationProtocol>) networkOperationWithURLRequest : (NSMutableURLRequest *)request
134119 completion : (void (^)(id <BNCNetworkOperationProtocol>operation))completion {
135120 BNCNetworkOperation *operation = [BNCNetworkOperation new ];
@@ -187,104 +172,6 @@ - (void) startOperation:(BNCNetworkOperation*)operation {
187172 [operation.sessionTask resume ];
188173}
189174
190- #pragma mark - Transport Security
191-
192- - (NSError *_Nullable) pinSessionToPublicSecKeyRefs : (NSArray /* *<SecKeyRef>*/ *)publicKeys {
193- @synchronized (self) {
194- NSError *error = nil ;
195- _pinnedPublicKeys = [NSMutableArray array ];
196- for (id secKey in publicKeys) {
197- if (CFGetTypeID ((SecKeyRef)secKey) == SecKeyGetTypeID ())
198- [_pinnedPublicKeys addObject: secKey];
199- else {
200- error = [NSError errorWithDomain: NSNetServicesErrorDomain
201- code: NSNetServicesBadArgumentError userInfo: nil ];
202- }
203- }
204- return error;
205- }
206- }
207-
208- - (NSArray *) pinnedPublicKeys {
209- @synchronized (self) {
210- return _pinnedPublicKeys;
211- }
212- }
213-
214- - (void ) URLSession : (NSURLSession *)session
215- task : (NSURLSessionTask *)task
216- didReceiveChallenge : (NSURLAuthenticationChallenge *)challenge
217- completionHandler : (void (^)(NSURLSessionAuthChallengeDisposition disposition,
218- NSURLCredential *credential))completionHandler {
219-
220- BOOL trusted = NO ;
221- SecTrustResultType trustResult = 0 ;
222- OSStatus err = 0 ;
223-
224- // Keep a local copy in case they mutate.
225- NSArray *localPinnedKeys = [self .pinnedPublicKeys copy ];
226- NSSet <NSString *>*localAllowedHosts = [self .anySSLCertHosts copy ];
227-
228- // Release these:
229- SecKeyRef key = nil ;
230- SecPolicyRef hostPolicy = nil ;
231-
232- // Get remote certificate
233- SecTrustRef serverTrust = challenge.protectionSpace .serverTrust ;
234- @synchronized ((__bridge id <NSObject , OS_dispatch_semaphore>)serverTrust) {
235-
236- // Set SSL policies for domain name check
237- hostPolicy = SecPolicyCreateSSL (true , (__bridge CFStringRef)challenge.protectionSpace .host );
238- if (!hostPolicy) goto exit;
239- SecTrustSetPolicies (serverTrust, (__bridge CFTypeRef _Nonnull)(@[ (__bridge id )hostPolicy ]));
240-
241- // Evaluate server certificate
242- SecTrustEvaluate (serverTrust, &trustResult);
243- switch (trustResult) {
244- case kSecTrustResultRecoverableTrustFailure :
245- if ([localAllowedHosts containsObject: challenge.protectionSpace.host])
246- break ;
247- else
248- goto exit;
249- case kSecTrustResultUnspecified :
250- case kSecTrustResultProceed :
251- break ;
252- default :
253- goto exit;
254- }
255-
256- if (localPinnedKeys == nil ) {
257- trusted = YES ;
258- goto exit;
259- }
260-
261- key = SecTrustCopyPublicKey (serverTrust);
262- if (!key) goto exit;
263- }
264-
265- for (id <NSObject > pinnedKey in localPinnedKeys) {
266- if ([pinnedKey isEqual: (__bridge id <NSObject >)key]) {
267- trusted = YES ;
268- goto exit;
269- }
270- }
271-
272- exit:
273- if (err) {
274- NSError *error = [NSError errorWithDomain: NSOSStatusErrorDomain code: err userInfo: nil ];
275- BNCLogError (@" Error while validating cert: %@ ." , error);
276- }
277- if (key) CFRelease (key);
278- if (hostPolicy) CFRelease (hostPolicy);
279-
280- if (trusted) {
281- NSURLCredential *credential = [NSURLCredential credentialForTrust: serverTrust];
282- completionHandler (NSURLSessionAuthChallengeUseCredential , credential);
283- } else {
284- completionHandler (NSURLSessionAuthChallengeCancelAuthenticationChallenge , NULL );
285- }
286- }
287-
288175- (void ) cancelAllOperations {
289176 @synchronized (self) {
290177 [self .session invalidateAndCancel ];
0 commit comments