How to connect to Self-Signed Certificate server (no private API call)

Discussion in 'iOS Programming' started by adriweb, Oct 23, 2010.

  1. adriweb macrumors newbie

    Joined:
    Oct 23, 2010
    Location:
    France
    #1
    Hello,

    I've been looking for a solution to connection to a self-signed certificate server (https://web-toulon.isen.fr) from a UIWebView. I still can't make it work, but I saw this stackoverflow topic : http://stackoverflow.com/questions/933331/how-to-use-nsurlconnection-to-connect-with-ssl-for-an-untrusted-cert and I don't know how to implement this on my project.

    I'm kind of new to iPhone development, so can you tell me how and where I can write the piece of code given in the topic in order to make it work properly ?

    Here is what I currently have for the loading part (so, it's pretty basic)

    Code:
    [moodle loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://web-toulon.isen.fr/moodle/"]]];
    
    So, That doesn't work, unless I write this private API in the AppDelegate :

    Code:
    @implementation NSURLRequest(IgnoreSSL)
    
    + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
    {
    		return YES;
    }
    @end

    What I basically want to do is to avoid this private API call :
    Code:
    +(BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host

    Is there anything 'simple' (well, if it's not simple but if it works, it's great too :) ) that can do what I'm looking for ?



    Thanks a lot for your help !

    Adrien Bertrand
     
  2. Don Kosak macrumors 6502a

    Don Kosak

    Joined:
    Mar 12, 2010
    Location:
    Hilo, Hawaii
    #2
    Hi Adrien,

    Yeah, Self Signed SSL Certificates require a little bit of work.

    There is an iOS Sample that shows you how to do it. It even has code to remember self-signed certificates that the user has accepted -- probably more than you need for this.

    The Sample is called "Advanced URL Connections". If you search the SDK Documentation you will find it.

    Basically, you need to handle some NSURLConnection Delegate Calls:

    Code:
    - (BOOL)connection:(NSURLConnection *)connection 
    canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)space
    ^--- return YES if the authenticationMethod is "NSURLAuthenticationMethodServerTrust"

    and

    Code:
    - (void)connection:(NSURLConnection *)connection 
    didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
    ^--- you need to respond to this using something like the call below:

    Code:
    [[challenge sender] useCredential:[NSURLCredential 
       credentialForTrust:challenge.protectionSpace.serverTrust]
       forAuthenticationChallenge:challenge];
    You will want to look up these methods in the SDK Documentation to get a better idea of how they work.

    Let us know if you get stuck on anything in particular.
     
  3. adriweb thread starter macrumors newbie

    Joined:
    Oct 23, 2010
    Location:
    France
    #3
    Thanks a lot for replying.

    I'll look more into that but I think I made the ASIHTTPRequest library working, so it might be easier with that :)
     
  4. Don Kosak macrumors 6502a

    Don Kosak

    Joined:
    Mar 12, 2010
    Location:
    Hilo, Hawaii
    #4
    The ASI library is very nice. It does have a method you can call to accept SSL Certificates.

    Good luck with your project!

    - Don
     

Share This Page