SSL connection on iOS error

I have a server that is written in C ++ using OpenSSL. The client I'm using is for iOS. I am using SSL security on iOS. To connect to the server here is what I do.

  • Create SSL context

  • Create a socket and connect to the server

  • Install certificates

  • Make a handshake with the server

SSLHandshake returns -9807 invalid invalid certificate chain and on the server side the Server is still waiting for SSL_Accept.

Here is the code

-(int)SSLInitClientSocket
{
@try
{

    UINT uitimeout = 40 ; 
    int  inret = 1,err=-1;
    int verify_client = 1;

    m_sslCTX= SSLCreateContext(NULL, kSSLClientSide, kSSLStreamType);
    if (!m_sslCTX)
    {
        //ERR_print_errors_fp(stderr);
        printf("\n ERROR in SSL_CTX_new \n");
        return OS_SOCK_ERROR;
    }

    //Socket is created using socket() function
    if ( OS_SockCreate(&listenerSocket, OS_TCP_SOCK, FALSE) != OS_SOCK_OK)
    {
        CFRelease(m_sslCTX);
        //SSL_CTX_free(m_sslCTX);
        m_sslCTX = NULL;
        return OS_SOCK_ERROR;
    }


    fcntl(listenerSocket, F_SETFD, O_NONBLOCK);

    /* Setting the socket to ignore Nagle Algorithm */
    OS_SockSetOpt(listenerSocket, TCP_OPT_NODELAY, &inret);

    /* Set  Timeout for send and recv */
    OS_SockSetOpt(listenerSocket, TCP_OPT_RCVTIMEOUT, &uitimeout);
    OS_SockSetOpt(listenerSocket, TCP_OPT_SNDTIMEOUT, &uitimeout);
    OS_SockInfo sockinfo;
    strcpy(sockinfo.strName,"172.16.30.209");
    sockinfo.usPort = 5212;
    //connected using connect
    if(OS_SockConnect(listenerSocket, &sockinfo) != OS_SOCK_OK)
    {
        printf("ERROR :: OS_SockConnect \n");
        return OS_SOCK_ERROR;
    }

    OSStatus osstatus;

    osstatus = SSLSetIOFuncs(m_sslCTX, readFromSocket,writeToSocket);
    if(osstatus) {
        return OS_SOCK_ERROR;
    }


    //connectionRef = (SSLConnectionRef)listenerSocket;
    err=SSLSetConnection(m_sslCTX,(SSLConnectionRef)listenerSocket);
    if ((err)==-1)
    {

        return OS_SOCK_ERROR;
    }
    SSLSessionState state;
    SSLGetSessionState(m_sslCTX,  &state);

    osstatus = SSLSetProtocolVersionMin(m_sslCTX,kTLSProtocol1);

    //Set certificates
    if(verify_client == 1)
    {
        CFArrayRef arrcert = CFArrayCreate(NULL, 0, 0, NULL);
        NSData *certData = [[NSData alloc]
                            initWithContentsOfFile:@"/Users/xyz/Desktop/cert/client.p12"];
        CFDataRef myCertData = (CFDataRef)certData;
        const void *keys[] = { kSecImportExportPassphrase};
        const void *values[] = { CFSTR("password")};


        CFDictionaryRef options = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1, NULL, NULL);

        SecPKCS12Import(myCertData, options, &arrcert);
        CFRelease(options);
        CFDictionaryRef item = CFArrayGetValueAtIndex(arrcert, 0);


        SecIdentityRef identity = (SecIdentityRef)CFDictionaryGetValue(item, kSecImportItemIdentity);
        CFArrayRef certs = CFArrayCreate(kCFAllocatorDefault, (const void **)&identity, 1, NULL);

        err=SSLSetCertificate(m_sslCTX,certs);

    }

    SSLGetSessionState(m_sslCTX,  &state); //Here Session state is Idle


    do
    {
        err = SSLHandshake(m_sslCTX);

    } while (err == errSSLWouldBlock);

    SSLGetSessionState(m_sslCTX,  &state); //here session state is idle and the server is still blocked on SSL_Accept


    return OS_SOCK_OK;
}
@catch(...)
{
    return OS_SOCK_ERROR;
}

}

      

My server code works fine with C ++ and java clients. I don't know what is missing.

+3


source to share


2 answers


Using a self-signed SSL certificate in iOS can be done with some work, but it is highly recommended not to do so and instead use a trusted certificate. Additionally, Apple may reject apps that use self-signed certificates.



0


source


This problem: NSOSStatusErrorDomain errSSLXCertChainInvalid

If you are using OpenSSL

, you can try:

$ openssl s_client -connect www.apple.com:443

openssl s_client -showcerts -host www.apple.com -port 443



You can get more links here: https://developer.apple.com/library/ios/technotes/tn2232/_index.html https://developer.apple.com/library/mac/navigation/index.html#topic= Sample + Code & section = Resource + Types

I hope this helps

0


source







All Articles