Connect with https (SSL) in my iOS App - ios

I want connect my app to my database with https .
But I don know what i have to do ?
I have files with jks format, crt format .
I want to know what is the real format in iOS ?
what are steps to connect with https ?
I see a lot of forums with the method :
(void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
But i don t understand what send in Nsurlauthenticationchallenge etc ...
I really need help please .

You can get detail info to beget connected with https using Apple's Making HTTP and HTTPS Requests

Related

How to implement SSL in client side?

I am new to web development and currently I am developing an Rest API which is to be consumed by an IOS app. So I developed the API and also implemented jwt token with oauth2 security in it.Now I want to provide the API to be consumed by the mobile app.So my backend server has SSL certificate. So the consumed Rest API will be something like
https://server:port/dataapiurl
So far I have read about SSL and JWT and i already they are for different reasons where
SSL is used for encrypted channel between client server communication and
JWT is used for Authorization.
So there will be no point if even I implement JWT and the communication is not in SSL.So to make sure the communication is done between client and server what have to be done on the client (mobile app) side?
1.Does the mobile app need to install a new certificate Or the SSL certificate of our backend server?
2.If it is our backend server's SSL certificate then how to install it in the mobile app ?
Any Help is appreciated.
You you can but haven't to set your ssl cert on the client.
You can just conform to the NSURLSessionDelegate protocol and implement this :
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
NSArray* netTrusts = #["your hostname here"];
if(netTrusts != nil && [netTrusts containsObject:challenge.protectionSpace.host]){
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
}
}

Check if SSL Certificate is present or not in iOS?

I have an API end point which has an SSL Certificate (secure https API). My iOS app bundle also includes that certificate so when I try to reach the API end point, objective C verifies if the certificate matches or not. My implementation of matching certificates works fine.
But I also need to verify if the API provides an SSL Certificate or not.
I am using AFNetworking for matching the certificates, and the place where it verifies the certificate: - (void)connection:(NSURLConnection *)connection
willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge does not even gets called when there is no certificate present. Hence the app doesn't verify anything.
I need something to verify in first place if the API has certificate or not, and if it doesn't then the app should not reach to the API end point.

HTTPS + Form Logon NSURLConnection

I am using HTTPS to a form-logon page.
When intercepting via
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge`
and extracting the Authentication Method used via
NSString *authenticationMethod = [[challenge protectionSpace] authenticationMethod];
I get the following
NSURLAuthenticationMethodServerTrust
But expected result should be
NSURLAuthenticationMethodHTMLForm
Is this due to using HTTPS?
Short answer: Yes
The purpose of the NSURLAuthenticationMethodServerTrust authentication method is that the client can verify and trust that the server is actual the server it pretends to be.
The NSURLAuthenticationMethodHTMLFormis used to authenticate a user via a web form. The server sends a web form and requests user credentials. This authentication does not require to be send over SSL/TLS. But then the user's credentials will be send in the clear, which is a bad thing from a security point of view.
Client authentication is also part of the TLS protocol. In this case, you may receive a challenge whose method is NSURLAuthenticationMethodClientCertificate.
Notice, you may receive more than one authentication challenges.

iPhone - send a certificate in a request

Now, I'm planing to send a NSURLRequest to the server with a local certificate for server to validate. But I check the function in NSURLRequest, I cannot find a way to include the certificate. Does anyone know how to include a certificate in a NSUrlRequest? Or must I use a open source function, like ASIHttpRequest.
Try to give a look to this quite good tutorial
Authenticating an HTTP Request with Client Certificates in iOS
It explains how to use the NSURLConnectionDelegate method
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
In order to provide the certificate.
Let me know if that helped.
UPDATE
In case support just for iOS >= 5.0 is provided, it's better to adopt the new non deprecated method
connection:willSendRequestForAuthenticationChallenge

If a client connects to a server using a hard-coded url is it secure?

I have a client (on iOS) that connects to a server using a hard-coded https url.
When a connection is established the server may indicate that for future connections a different machine name and/or port should be used. In addition the server can specify url location suffixes to fetch data from.
i.e. the following URL might be hardcoded in the client:
https://machineName.address.port/url-suffix
and after a connection is established the sever could inform it to use machineName2 and portX and url-suffix /someLocation/somewhere, so the next time the client connects it will use the url
https://machineName2.address.portX/someLocation/somewhere.
The address part or the url cannot change.
At the moment the client has the following for the connection authentication challenge, i.e. it'll connect to anything:
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
SecTrustRef trust = challenge.protectionSpace.serverTrust;
NSURLCredential *cred;
cred = [NSURLCredential credentialForTrust:trust];
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
}
At the moment this app isn't doing anything that requires heavy security - there's no bank info being accessed, the user doesn't log onto anything, no user info is being transmitted. The client is just downloading data from the server onto the device.
Without adding certificate checking on the client side could a spoof server send porn to the device or something, or is the fact a https connection is made and the url address is hardcoded sufficient?
HTTPS provides data encryption and authentication, but your certificate should be signed by a certificate authority. Accessing a hardcoded URL is not a security flaw—that's how webservices/APIs work. However, without proper certificate setup someone could potentially impersonate your server. Just using SSL/TLS is not enough.

Resources