Search code examples
xcodecocoasslx509

SSL using x.509 certificates cocoa


I wanted to know where exactly in xcode do I place a x.509 certificate that I have. Taking the example from here: http://code.google.com/p/cocoaasyncsocket/downloads/detail?name=CertTest.zip&can=1&q=

I wanted to be able to communicate with a secure server I have running. I just cant seem to add the certificate in and everytime I do, my mac opens up a keychain window asking m if the certificate belongs to any of my system (other than my project) settings. So I wanted to know where do I place it in my project and how I would use it(I guess the project at the link would help with that)


Solution

  • If you are using NSURLConnection for connecting to your server then you should implement next methods in your delegate:

    - (BOOL)connection:(NSURLConnection *)conn canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace;
    // A delegate method called by the NSURLConnection when something happens with the 
    // connection security-wise.  We defer all of the logic for how to handle this to 
    // the ChallengeHandler module (and it's very custom subclasses).
    
    
    - (void)connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    // A delegate method called by the NSURLConnection when you accept a specific 
    // authentication challenge by returning YES from -connection:canAuthenticateAgainstProtectionSpace:. 
    // Again, most of the logic has been shuffled off to the ChallengeHandler module; the only 
    // policy decision we make here is that, if the challenge handle doesn't get it right in 5 tries, 
    // we bail out.
    

    I advice you to look through this sample by Apple: Advanced Url Connections