0

What is the correct way to create a SSL socket connection in iOS?

I've implemented a SSL server in Java by:

  • creating a SSL certificate with a store password
  • loading it by setting the system properties "javax.net.ssl.keyStore" and "javax.net.ssl.keyStorePassword"
  • creating a SSLServerSocket with the SSLServerSocket factory

Now I want to create a client app which opens a socket connection in a thread and communicates over that. What is the proper way to create such a connection and do the communication with my servers certificate?

2 Answers 2

2

You can use NSInputStream and NSOutputStream to connect using TLS as per the answer to this question.

EDIT: Rather than use the SSL settings in that answer, I would suggest this:

NSDictionary *settings = @{ 
    (__bridge NSString *)kCFStreamPropertySocketSecurityLevel:(__bridge NSString *)kCFStreamSocketSecurityLevelNegotiatedSSL 
};

This uses the following defaults:

kCFStreamSSLAllowsExpiredCertificates:  NO
kCFStreamSSLAllowsAnyRoot: NO
kCFStreamSSLValidatesCertificateChain: YES

If you want to set the security level to use a particular version of SSL or TLS, take a look at the values in CFSocketStream.h.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but is it safe to use kCFStreamSSLAllowsAnyRoot?
Safe, yes because it's a valid option. Is it secure? No.
So then.. what would be the right way to create a secure connection? ;)
Okay, so far so good, but as my streams do not allow "any root" anymore, how do I make my servers SSL certificate to be allowed?
If you need to allow any root, then apply that setting. If you want to use a custom root certificate, ensure it's in the keychain on your device.
-1

There is no need to create a certificate if you are making a client app not a server one. To do it in iOS, there are a number of ways, the simplest is to use AFNetworking library. Just put the url (https) inside one of its methods, and you are ready. I used it many times. Just spend 5 mins to read the doc.

2 Comments

SSLServerSocket is, as one would imagine, a socket server and not a web server.
@guiseppe I don't want to create just a client that opens some kind of HTTPS connection. I want to create a SSL socket connection to my own Java server where I cant send plain bytes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.