Example: Establish a secure client with SSL_ APIs

This example enables a client application using the SSL_ APIs to communicate with a server application that uses the SSL_APIs.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/* SSL Client Program using SSL_Init_Application      */

/* Assummes that application id is already registered */
/* and a certificate has been associated with the     */
/* application id.                                    */
/* No parameters, some comments and many hardcoded    */
/* values to keep it short and simple                 */

/* use following command to create bound program:     */
/* CRTBNDC PGM(MYLIB/SSLCLIAPP)                       */
/*         SRCFILE(MYLIB/CSRC)                        */
/*         SRCMBR(SSLCLIAPP                           */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/socket.h>
#include <qsossl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>

/*  Making this simple - no parameters  */
void main(void)
{
    SSLHandle *sslh;
    SSLInitApp sslinit;
    struct sockaddr_in address;
    int buf_len, rc = 0, sd;
    char buff1[1024];
    char buff2[1024];

    /*  only want to use 1 cipher suite     */
    unsigned short int cipher = SSL_RSA_WITH_RC4_128_SHA;

    /*  hardcoded IP address                */
    char addr[12] = "16.35.146.84";

    void * malloc_ptr = (void *) NULL;
    unsigned int malloc_size = 8192;

    /* memset sslinit structure to hex zeros */
    memset((char *)&sslinit, 0, sizeof(sslinit));

    /* fill in values for sslinitapp structure */
    /* using an existing app id                */
    sslinit.applicationID = "MY_CLIENT_APP";
    sslinit.applicationIDLen = 13;
    sslinit.localCertificate = NULL;
    sslinit.localCertificateLen = 0;
    sslinit.cipherSuiteList = NULL;
    sslinit.cipherSuiteListLen = 0;

    /* allocate and set pointers for certificate buffer */
    malloc_ptr = (void*) malloc(malloc_size);
    sslinit.localCertificate = (unsigned char*) malloc_ptr;
    sslinit.localCertificateLen = malloc_size;

    /* initialize ssl call SSL_Init_Application */
    rc = SSL_Init_Application(&sslinit);
    if (rc != 0)
    {
      printf("SSL_Init_Application() failed with rc = %d and errno = %d.\n",
           rc,errno);
      return;
    }

    /* initialize a socket */
    sd = socket(AF_INET, SOCK_STREAM, 0);
    if (sd < 0)
    {
      perror("socket() failed");
      return;
    }

    /* enable SSL support for the socket */
    sslh = SSL_Create(sd, SSL_ENCRYPT);
    if (sslh == NULL)
    {
      printf("SSL_Create() failed with errno = %d.\n", errno);
      close(sd);
      return;
    }

    /* connect to the server using a set port number */
    memset((char *) &address, 0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_port = 13333;
    address.sin_addr.s_addr = inet_addr(addr);
    rc = connect(sd, (struct sockaddr *) &address, sizeof(address));
    if (rc < 0)
    {
      perror("connect() failed");
      close(sd);
      return;
    }

    /* set up to call handshake, setting cipher     */
    sslh -> protocol = 0;
    sslh -> timeout = 0;
    sslh -> cipherSuiteList = &cipher;
    sslh -> cipherSuiteListLen = 1;

    /* initiate the SSL handshake - as a CLIENT */
    rc = SSL_Handshake(sslh, SSL_HANDSHAKE_AS_CLIENT);
    if (rc != 0)
    {
      printf("SSL_Handshake() failed with rc = %d and errno = %d.\n",
           rc, errno);
      close(sd);
      return;
    }

    /* send a message to the server using the secure session */
    strcpy(buff1,"Test of SSL_Write \n\n");
    buf_len = strlen(buff1);
    rc = SSL_Write(sslh, buff1, buf_len);
    if (rc != buf_len)
    {
      if (rc < 0)
      {
       printf("SSL_Write() failed with rc = %d and errno = %d.\n",rc,errno);
        SSL_Destroy(sslh);
        close(sd);
        return;
      }
      else
      {
        printf("SSL_Write() did not write all data.\n");
        SSL_Destroy(sslh);
        close(sd);
        return;
      }
    }

    /* write the results to the screen */
    printf("SSL_Write() wrote ...\n");
    printf("%s\n",buff1);

    memset((char *) buff2, 0x00, sizeof(buff2));

    /* receive the message from the server using the secure session */
    rc = SSL_Read(sslh, buff2, buf_len);
    if (rc < 0)
    {
      printf("SSL_Read() failed with rc = %d.\n",rc);
      SSL_Destroy(sslh);
      close(sd);
      return;
    }

    /* write the results to the screen */
    printf("SSL_Read() read ...\n");
    printf("%s\n",buff2);

    /* disable SSL support for the socket */
    SSL_Destroy(sslh);

    /* close the connection by closing the local socket */
    close(sd);
    return;
}
Related concepts
SSL_ APIs
Related reference
Example: Establish a secure server with SSL_ APIs