SSL_Accept fails, SSL_get_error returns 1
<p>I am new to SSL socket programming and my first task is to get the SSL server client to be working and understanding.</p>

<p>I have obtained the server and client sources floating on the web and compiled them against my openssl library (compiled from source).</p>

<p>When I start the server I am able to create a normal clientfd using accept system call, however SSL_accept fails.</p>

<pre class="lang-c prettyprint-override"><code>clientsocketfd = accept(serversocketfd, NULL, 0);
serverssl = SSL_new(ssl_server_ctx);
if(!serverssl)
{
    printf("Error SSL_new\n");
    return -1;
}
SSL_set_fd(serverssl, clientsocketfd);

if((ret = SSL_accept(serverssl))!= 1)
{
    printf("Handshake Error %d\n", SSL_get_error(serverssl, ret));
    return -1;
}
</code></pre>

<p><code>SSL_accept</code> fails printing <code>Handshake error 1</code>.</p>

<p>Why is <code>SSL_accept</code> failing with error 1?</p>

<hr>

<p>Server source:</p>

<pre class="lang-c prettyprint-override"><code>/*---------------------------------------------------------------------*/
/*--- main - create SSL socket server.                              ---*/
/*---------------------------------------------------------------------*/
int main(int count, char *strings[])
{   SSL_CTX *ctx;
    int server;
    char *portnum;

    if ( count != 2 )
    {
        printf("Usage: %s &lt;portnum&gt;\n", strings[0]);
        exit(0);
    }
    portnum = strings[1];
    SSL_library_init();
    ctx = InitServerCTX();                                                              /* initialize SSL */
    LoadCertificates(ctx, "newreq.pem", "newreq.pem");  /* load certs */
    server = OpenListener(atoi(portnum));                               /* create server socket */
    while (1)
    {   struct sockaddr_in addr;
        int len = sizeof(addr);
        SSL *ssl;

        int client = accept(server, (struct sockaddr*)&amp;addr, &amp;len);             /* accept connection as usual */
        printf("Connection: %s:%d\n",
                inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
        ssl = SSL_new(ctx);                                             /* get new SSL state with context */
        SSL_set_fd(ssl, client);                                                /* set connection socket to SSL state */
        Servlet(ssl);                                                                   /* service connection */
    }
    close(server);                                                                              /* close server socket */
    SSL_CTX_free(ctx);                                                                  /* release context */
}

/*---------------------------------------------------------------------*/
/*--- InitServerCTX - initialize SSL server  and create context     ---*/
/*---------------------------------------------------------------------*/
SSL_CTX* InitServerCTX(void)
{   SSL_METHOD *method;
    SSL_CTX *ctx;

    OpenSSL_add_all_algorithms();               /* load &amp; register all cryptos, etc. */
    SSL_load_error_strings();                   /* load all error messages */
    method = SSLv3_server_method();             /* create new server-method instance */
    ctx = SSL_CTX_new(method);                  /* create new context from method */
    if ( ctx == NULL )
    {
        ERR_print_errors_fp(stderr);
        abort();
    }
    return ctx;
}
</code></pre>

<p>The OpenSSL version and compilation options are as folows:</p>

<pre class="lang-none prettyprint-override"><code>/usr/bin/openssl version -a
OpenSSL 1.0.2g  1 Mar 2016
built on: reproducible build, date unspecified
platform: debian-amd64
options:  bn(64,64) rc4(16x,int) des(idx,cisc,16,int) blowfish(idx) 
compiler: cc -I. -I.. -I../include  -fPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT 
-DDSO_DLFCN -DHAVE_DLFCN_H -m64 -DL_ENDIAN -g -O2 -fstack-protector-strong 
-Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -Wl,
-Bsymbolic-functions -Wl,-z,relro -Wa,--noexecstack -Wall -DMD32_REG_T=int 
-DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT 
-DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM 
-DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM 
-DWHIRLPOOL_ASM -DGHASH_ASM -DECP_NISTZ256_ASM
OPENSSLDIR: "/usr/lib/ssl"
</code></pre>