Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching from http to https. Invalid certificate

I have an app that connects to my home routers web interface. I want to convert this to use https instead of just http. I was originally using ASIHttpRequest, but as it's no longer supported i'm switching over to AFNetworking. The problem is, whenever I try to connect, I get this error message:

_block_invoke_0220 [Line 243] ERROR: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “192.168.1.1” which could put your confidential information at risk." UserInfo=0x9792ad0 {NSErrorFailingURLStringKey=https://192.168.1.1/Info.live.htm, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https://192.168.1.1/Info.live.htm, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “192.168.1.1” which could put your confidential information at risk., NSUnderlyingError=0xa6a3560 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “192.168.1.1” which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=< SecTrustRef:

If I navigate to the url i safari, I get a message that Safari can't verify the identity.... and I have to click continue to carry on. How can I achieve this? I don't really know anything about ssl or https unfortunately. Here is the code i'm currently using:

NSString *urlString = @"https://192.168.1.1/";
NSURL *url = [NSURL URLWithString:urlString];

// Set authorization
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setAuthorizationHeaderWithUsername:user password:pass];

NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"Info.live.htm" parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *responceString = [operation responseString];
    //        NSLog(@"%@",responceString);
    if ([self parseInfoLive:responceString])
        [[NSNotificationCenter defaultCenter] postNotificationName:@"downloadsComplete" object:nil];
}
                                 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                     NSLog(@"ERROR: %@",error.description);
                                 }];


[operation start];
like image 579
Darren Avatar asked Sep 16 '12 13:09

Darren


1 Answers

For getting around the validity check of the host certificate, add the following code.

First add an interface for the setter method that is already within the SDK but not exposed into public:

@interface NSURLRequest(Private)
+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString *)inHost;
@end

Now, whenever you are rendering a new request, invoke that setter:

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[inURL host]];

Warning

Do not use this code for production but only while developing your app in cases where the certificate is not yet approved/submitted/installed. Typical would be the use of a development server that does not have a trusted certificate installed. The use of this code will get your App rejected from distribution via iTunes as it uses a private API method.

For making sure that things work smoothly in a production environment, you will have to get a trusted SSL certificate for your host. There are various authoritative companies providing such thing. To mention at least one (there are MANY more), you could use GoDaddy.


Update (31st May 2013)

AFNetworking got updated to support invalid certificates out of the box, without using any private API's. Kudos to Peter Steinberger!

For enabling that feature, the most convenient solution is to add the following to your prefix header (.pch):

#ifdef DEBUG
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
#endif

Once again, I can not emphasize enough that you should refrain from enabling that feature in production code - you would pretty much invalidate the entire point of SSL connections and render them vulnerable.

like image 110
Till Avatar answered Sep 22 '22 13:09

Till