Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2011, 09:35 AM   #1 (permalink)
Registered Member
 
wheli's Avatar
 
Join Date: Jan 2010
Posts: 130
wheli is on a distinguished road
Default SSL Connection

Hey Guys,

I have a quick question for you. I am connecting to a server through my application by means of SSL/HTTPS. I have all the code working to accept an untrusted certificate and connect when the certificate is trusted. Using the below delegate methods:

Code:
// prompted by SSL connection
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
    if([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        NSLog(@"Attempting SSL Connection...");
        return YES;
    }
    
    NSLog(@"Cannot connect through SSL");
    return NO;
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)theChallenge
{   
    challenge = theChallenge;
    
    // "trust" related authentication challenge, prompted by SSL connection
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        //This takes the serverTrust object and checkes it against your keychain
        SecTrustResultType result;
        SecTrustEvaluate(challenge.protectionSpace.serverTrust, &result);
        
        if (result == kSecTrustResultInvalid)
            NSLog(@"SSL Challenge Result: Invalid");
        else if (result == kSecTrustResultProceed)
            NSLog(@"SSL Challenge Result: Proceed");
        else if (result == kSecTrustResultConfirm)
            NSLog(@"SSL Challenge Result: Confirm");
        else if (result == kSecTrustResultDeny)
            NSLog(@"SSL Challenge Result: Deny");
        else if (result == kSecTrustResultUnspecified)
            NSLog(@"SSL Challenge Result: Unspecified");
        else if (result == kSecTrustResultRecoverableTrustFailure)
            NSLog(@"SSL Challenge Result: Recoverable Trust Failure");
        else if (result == kSecTrustResultFatalTrustFailure)
            NSLog(@"SSL Challenge Result: Fatal Trust Failure");
        else if (result == kSecTrustResultOtherError)
            NSLog(@"SSL Challenge Result: Other Error");
        
        if(result == kSecTrustResultProceed || result == kSecTrustResultConfirm || result == kSecTrustResultUnspecified)
        {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust] forAuthenticationChallenge: challenge];
        }
        else
        { 
            [self promptForTrust];
        }
    }
}

-(void)promptForTrust
{
    // display an error if there are any issues with the connection
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Verify Server Identity" message:[NSString stringWithFormat:@"xxxx can't verify the identity of \"%@\". Would you like to continue anyway?", [[Model sharedManager] returnServer]] delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:@"Cancel",@"Details",nil];
    [alert show];
    [alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        //May need to add a method to add serverTrust to the keychain like Firefox's "Add Exception"
        
        // if the user decides to trust
        [challenge.sender useCredential:[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust] forAuthenticationChallenge: challenge];
    }
    else if (buttonIndex == 1)
    {
        // if the user decides not to trust 
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
    else if (buttonIndex == 2)
    {
        // show details of certificate
       
    }
}
I am trying to give the option for the user to install the untrusted certificate and view the properties just like when hitting an untrusted server with safari. Any ideas how to do this? I can find a lot of information on making the connection, but am struggling to find this last bit.

Thanks!
wheli is offline   Reply With Quote
Old 09-07-2011, 10:45 AM   #2 (permalink)
Registered Member
 
wheli's Avatar
 
Join Date: Jan 2010
Posts: 130
wheli is on a distinguished road
Default

So I figured out how to print out part of the certificate information:

Code:
        // show details of certificate
        SecTrustRef trustRef = [[challenge protectionSpace] serverTrust];
        CFIndex count = SecTrustGetCertificateCount(trustRef);
        for (CFIndex i = 0; i < count; i++) 
        {
            SecCertificateRef certRef = SecTrustGetCertificateAtIndex(trustRef, i);
            CFStringRef certSummary = SecCertificateCopySubjectSummary(certRef); 
            NSLog(@"%@", certSummary);
        }
This doesn't really print out too much for untrusted certs, but for trusted ones I see a whole bunch of information. I'd still like to display everything that you see if you hit an untrusted site with safari though....
wheli is offline   Reply With Quote
Old 09-07-2011, 12:18 PM   #3 (permalink)
Registered Member
 
wheli's Avatar
 
Join Date: Jan 2010
Posts: 130
wheli is on a distinguished road
Default

It looks like if I can get more data using:

Code:
CFDataRef data = SecCertificateCopyData(certRef);
Now just trying to figure out how to pull content from the CFDataRef object...
wheli is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 374
8 members and 366 guests
apatsufas, JackReidy, jeroenkeij, Sami Gh, tim0504, UMAD, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:53 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0