0

I'm using macOS native framework to sent https requests (nsurlsession). In order to handle TLS flows of server-side authentication, I've implemented the didReceiveChallenge callback.

In my special case, My https request's URL composed of IP instead of URL, so i'd like to remove one default check of matching the URL with the server certificate's common name, but remains all the other checks. Perhaps anybody can verify my code ?

Thanks

is it possible to eliminate this check for specific connections ?

UPDATE:

Thanks to the comment below by Larme, I was able to further research the topic and bind the hostname check to a specific URL instead of the ip address.

- (void)URLSession:(NSURLSession*)session
    didReceiveChallenge:(NSURLAuthenticationChallenge*)challenge
      completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition,
                                  NSURLCredential* credential))completionHandler {

  if (challenge.protectionSpace.authenticationMethod ==
             NSURLAuthenticationMethodServerTrust) {

    BOOL allow = true;
    if (URL_IS_IP) { 
        SecTrustRef     trust;

        trust = challenge.protectionSpace.serverTrust;
        OSStatus        err;
        SecPolicyRef    policy;

        policy = SecPolicyCreateSSL(true, CFSTR("matchinUrl.com"));
        err = SecTrustSetPolicies(trust, policy);
        if (err == errSecSuccess) {
           allow = evaluateTrust(trust);
        }
        CFRelease(policy);
    } 

    if (allow) {
      completionHandler(
        NSURLSessionAuthChallengeUseCredential, 
        [NSURLCredential credentialForTrust:trust]);
    } else {
      completionHandler(
        NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
    }
  } else {
    completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  }
}

Zohar81
  • 4,554
  • 5
  • 29
  • 82
  • 1
    Do you mean you want to check `challenge.protectionSpace.host`? – Larme Jul 26 '23 at 16:14
  • @Larme, thanks for you comment. I've changed my code, I think this should do the trick, perhaps you can approve it ? thnaks ! – Zohar81 Jul 27 '23 at 08:03

0 Answers0