SSL_write returns SSL_ERR_SYSCALL while Err_get_error() returns 0












0














I edited my code and this is what it looks like now. The server is trying to write some bytes on the socket.



    int r;
FILE *fp;
fp = stderr;

const SSL_METHOD *method;
method = SSLv23_server_method();
ERR_clear_error();

if ( !ctx)
{
ctx = SSL_CTX_new(method);
if (!ctx) {
return 0;
}

SSL_CTX_set_ecdh_auto(ctx, 1);



if (SSL_CTX_use_certificate_file(ctx, "/home/cert.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(fp);
}

if (SSL_CTX_use_PrivateKey_file(ctx, "/home/key.pem", SSL_FILETYPE_PEM) <= 0 ) {
ERR_print_errors_fp(fp);
}
}

if (!ctx) {
return;
}

SSL *ssl;
ssl = SSL_new(ctx);
SSL_set_fd(ssl, fd);

ERR_clear_error();

if (SSL_accept(ssl) <= 0) {
ERR_print_errors_fp(fp);
}

int rce = 0;
ERR_clear_error();

char *buffer = (char *) malloc(iova.iov_len);
memcpy(buffer, iova.iov_base, iova.iov_len);

r = SSL_write(ssl, buffer, (int)iova.iov_len);

// check how many bytes were written
if ( r <= 0 )
{
ERR_print_errors_fp(fp);
int err = errno;

switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
continue;
case SSL_ERROR_ZERO_RETURN:
syslog(LOG_INFO, "SSL read encountered an error %d. Peer disconnected.", SSL_ERROR_ZERO_RETURN);
break;
case SSL_RECEIVED_SHUTDOWN:
syslog(LOG_INFO, "SSL read encountered an error %d. Shutdown.", SSL_RECEIVED_SHUTDOWN);
break;
case SSL_ERROR_SYSCALL:
syslog(LOG_INFO, "SSL write encountered an error %d and %lu. SYSCALL", SSL_get_error(ssl,r), ERR_get_error());
break;
default:
syslog(LOG_INFO, "SSL write encountered an error %d and %d. Quitting", SSL_get_error(ssl,r), err);
break;
}


Couple issues that I could not figure out -




  1. SSL_accept() fails


  2. SSL_write() then fails with SSL_RECEIVED_SHUTDOWN and err string = error:00000002:lib(0):func(0):system lib



What am I doing wrong here with SSL_accept and SSL_write() ?





I am trying to use Openssl to do SSL_write() and I get the following error during SSL_write(). The server is trying to write some bytes to the socket connection fd -



error:140D0114:SSL routines:SSL_write:uninitialized:ssl_lib.c:1039:
Error: error:00000005:lib(0):func(0):DH lib


Here is my code



    SSL *ssl;
if ( !ctx )
return 0;
ssl = SSL_new(ctx);

//SSL_set_fd(ssl, fd);

BIO *sbio = NULL;
sbio = BIO_new_socket(fd, BIO_NOCLOSE);
SSL_set_bio(ssl, sbio, sbio);
ERR_clear_error();

FILE *fp;
fp = stderr;
r = SSL_write(ssl, buffer, (len);

if ( r <= 0 )
{
ERR_print_errors_fp(fp);

switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
syslog(LOG_INFO, "No real error. Continue.");
continue;
case SSL_ERROR_ZERO_RETURN:
syslog(LOG_INFO, "SSL read encountered an error %d. Peer disconnected.", SSL_ERROR_ZERO_RETURN);
break;
case SSL_RECEIVED_SHUTDOWN:
syslog(LOG_INFO, "SSL read encountered an error %d. Shutdown.", SSL_RECEIVED_SHUTDOWN);
break;
case SSL_ERROR_SYSCALL:
syslog(LOG_INFO, "SSL write encountered an error %d and %lu. SYSCALL", SSL_get_error(ssl,r), ERR_get_error());
break;
default:
syslog(LOG_INFO, "SSL write encountered an error %d and %d. Quitting", SSL_get_error(ssl,r), err);
break;
}


The SSL context (ctx) is valid to begin with.



I tried looking up an explanation of the error but did not find it. Nor did I find a similar issue that would resolve it. Can anyone help me understand what I can do to further debug this ? Or if they have a solution ?



Openssl version 1.0.2g










share|improve this question
























  • The server and the client can go thru the same code. There are no separate programs for the 2.
    – PeterJ
    Nov 21 at 4:52










  • Perhaps SSL_accept() fails because it is non-blocking and does not wait for an incoming SSL_connect() from a client ? I will see what SSL_get_error() is returned by SSL_accept(). In the meantine, how can I make it blocking ?
    – PeterJ
    Nov 21 at 5:17










  • So SSL_accept() returned error:00000002 which is SSL_ERROR_WANT_READ. My socket that I am trying to write to is non-blocking. So when I do a SSL_set_fd(ctx, fd) the SSL ctx is non-blocking as well.
    – PeterJ
    Nov 21 at 5:46


















0














I edited my code and this is what it looks like now. The server is trying to write some bytes on the socket.



    int r;
FILE *fp;
fp = stderr;

const SSL_METHOD *method;
method = SSLv23_server_method();
ERR_clear_error();

if ( !ctx)
{
ctx = SSL_CTX_new(method);
if (!ctx) {
return 0;
}

SSL_CTX_set_ecdh_auto(ctx, 1);



if (SSL_CTX_use_certificate_file(ctx, "/home/cert.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(fp);
}

if (SSL_CTX_use_PrivateKey_file(ctx, "/home/key.pem", SSL_FILETYPE_PEM) <= 0 ) {
ERR_print_errors_fp(fp);
}
}

if (!ctx) {
return;
}

SSL *ssl;
ssl = SSL_new(ctx);
SSL_set_fd(ssl, fd);

ERR_clear_error();

if (SSL_accept(ssl) <= 0) {
ERR_print_errors_fp(fp);
}

int rce = 0;
ERR_clear_error();

char *buffer = (char *) malloc(iova.iov_len);
memcpy(buffer, iova.iov_base, iova.iov_len);

r = SSL_write(ssl, buffer, (int)iova.iov_len);

// check how many bytes were written
if ( r <= 0 )
{
ERR_print_errors_fp(fp);
int err = errno;

switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
continue;
case SSL_ERROR_ZERO_RETURN:
syslog(LOG_INFO, "SSL read encountered an error %d. Peer disconnected.", SSL_ERROR_ZERO_RETURN);
break;
case SSL_RECEIVED_SHUTDOWN:
syslog(LOG_INFO, "SSL read encountered an error %d. Shutdown.", SSL_RECEIVED_SHUTDOWN);
break;
case SSL_ERROR_SYSCALL:
syslog(LOG_INFO, "SSL write encountered an error %d and %lu. SYSCALL", SSL_get_error(ssl,r), ERR_get_error());
break;
default:
syslog(LOG_INFO, "SSL write encountered an error %d and %d. Quitting", SSL_get_error(ssl,r), err);
break;
}


Couple issues that I could not figure out -




  1. SSL_accept() fails


  2. SSL_write() then fails with SSL_RECEIVED_SHUTDOWN and err string = error:00000002:lib(0):func(0):system lib



What am I doing wrong here with SSL_accept and SSL_write() ?





I am trying to use Openssl to do SSL_write() and I get the following error during SSL_write(). The server is trying to write some bytes to the socket connection fd -



error:140D0114:SSL routines:SSL_write:uninitialized:ssl_lib.c:1039:
Error: error:00000005:lib(0):func(0):DH lib


Here is my code



    SSL *ssl;
if ( !ctx )
return 0;
ssl = SSL_new(ctx);

//SSL_set_fd(ssl, fd);

BIO *sbio = NULL;
sbio = BIO_new_socket(fd, BIO_NOCLOSE);
SSL_set_bio(ssl, sbio, sbio);
ERR_clear_error();

FILE *fp;
fp = stderr;
r = SSL_write(ssl, buffer, (len);

if ( r <= 0 )
{
ERR_print_errors_fp(fp);

switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
syslog(LOG_INFO, "No real error. Continue.");
continue;
case SSL_ERROR_ZERO_RETURN:
syslog(LOG_INFO, "SSL read encountered an error %d. Peer disconnected.", SSL_ERROR_ZERO_RETURN);
break;
case SSL_RECEIVED_SHUTDOWN:
syslog(LOG_INFO, "SSL read encountered an error %d. Shutdown.", SSL_RECEIVED_SHUTDOWN);
break;
case SSL_ERROR_SYSCALL:
syslog(LOG_INFO, "SSL write encountered an error %d and %lu. SYSCALL", SSL_get_error(ssl,r), ERR_get_error());
break;
default:
syslog(LOG_INFO, "SSL write encountered an error %d and %d. Quitting", SSL_get_error(ssl,r), err);
break;
}


The SSL context (ctx) is valid to begin with.



I tried looking up an explanation of the error but did not find it. Nor did I find a similar issue that would resolve it. Can anyone help me understand what I can do to further debug this ? Or if they have a solution ?



Openssl version 1.0.2g










share|improve this question
























  • The server and the client can go thru the same code. There are no separate programs for the 2.
    – PeterJ
    Nov 21 at 4:52










  • Perhaps SSL_accept() fails because it is non-blocking and does not wait for an incoming SSL_connect() from a client ? I will see what SSL_get_error() is returned by SSL_accept(). In the meantine, how can I make it blocking ?
    – PeterJ
    Nov 21 at 5:17










  • So SSL_accept() returned error:00000002 which is SSL_ERROR_WANT_READ. My socket that I am trying to write to is non-blocking. So when I do a SSL_set_fd(ctx, fd) the SSL ctx is non-blocking as well.
    – PeterJ
    Nov 21 at 5:46
















0












0








0







I edited my code and this is what it looks like now. The server is trying to write some bytes on the socket.



    int r;
FILE *fp;
fp = stderr;

const SSL_METHOD *method;
method = SSLv23_server_method();
ERR_clear_error();

if ( !ctx)
{
ctx = SSL_CTX_new(method);
if (!ctx) {
return 0;
}

SSL_CTX_set_ecdh_auto(ctx, 1);



if (SSL_CTX_use_certificate_file(ctx, "/home/cert.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(fp);
}

if (SSL_CTX_use_PrivateKey_file(ctx, "/home/key.pem", SSL_FILETYPE_PEM) <= 0 ) {
ERR_print_errors_fp(fp);
}
}

if (!ctx) {
return;
}

SSL *ssl;
ssl = SSL_new(ctx);
SSL_set_fd(ssl, fd);

ERR_clear_error();

if (SSL_accept(ssl) <= 0) {
ERR_print_errors_fp(fp);
}

int rce = 0;
ERR_clear_error();

char *buffer = (char *) malloc(iova.iov_len);
memcpy(buffer, iova.iov_base, iova.iov_len);

r = SSL_write(ssl, buffer, (int)iova.iov_len);

// check how many bytes were written
if ( r <= 0 )
{
ERR_print_errors_fp(fp);
int err = errno;

switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
continue;
case SSL_ERROR_ZERO_RETURN:
syslog(LOG_INFO, "SSL read encountered an error %d. Peer disconnected.", SSL_ERROR_ZERO_RETURN);
break;
case SSL_RECEIVED_SHUTDOWN:
syslog(LOG_INFO, "SSL read encountered an error %d. Shutdown.", SSL_RECEIVED_SHUTDOWN);
break;
case SSL_ERROR_SYSCALL:
syslog(LOG_INFO, "SSL write encountered an error %d and %lu. SYSCALL", SSL_get_error(ssl,r), ERR_get_error());
break;
default:
syslog(LOG_INFO, "SSL write encountered an error %d and %d. Quitting", SSL_get_error(ssl,r), err);
break;
}


Couple issues that I could not figure out -




  1. SSL_accept() fails


  2. SSL_write() then fails with SSL_RECEIVED_SHUTDOWN and err string = error:00000002:lib(0):func(0):system lib



What am I doing wrong here with SSL_accept and SSL_write() ?





I am trying to use Openssl to do SSL_write() and I get the following error during SSL_write(). The server is trying to write some bytes to the socket connection fd -



error:140D0114:SSL routines:SSL_write:uninitialized:ssl_lib.c:1039:
Error: error:00000005:lib(0):func(0):DH lib


Here is my code



    SSL *ssl;
if ( !ctx )
return 0;
ssl = SSL_new(ctx);

//SSL_set_fd(ssl, fd);

BIO *sbio = NULL;
sbio = BIO_new_socket(fd, BIO_NOCLOSE);
SSL_set_bio(ssl, sbio, sbio);
ERR_clear_error();

FILE *fp;
fp = stderr;
r = SSL_write(ssl, buffer, (len);

if ( r <= 0 )
{
ERR_print_errors_fp(fp);

switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
syslog(LOG_INFO, "No real error. Continue.");
continue;
case SSL_ERROR_ZERO_RETURN:
syslog(LOG_INFO, "SSL read encountered an error %d. Peer disconnected.", SSL_ERROR_ZERO_RETURN);
break;
case SSL_RECEIVED_SHUTDOWN:
syslog(LOG_INFO, "SSL read encountered an error %d. Shutdown.", SSL_RECEIVED_SHUTDOWN);
break;
case SSL_ERROR_SYSCALL:
syslog(LOG_INFO, "SSL write encountered an error %d and %lu. SYSCALL", SSL_get_error(ssl,r), ERR_get_error());
break;
default:
syslog(LOG_INFO, "SSL write encountered an error %d and %d. Quitting", SSL_get_error(ssl,r), err);
break;
}


The SSL context (ctx) is valid to begin with.



I tried looking up an explanation of the error but did not find it. Nor did I find a similar issue that would resolve it. Can anyone help me understand what I can do to further debug this ? Or if they have a solution ?



Openssl version 1.0.2g










share|improve this question















I edited my code and this is what it looks like now. The server is trying to write some bytes on the socket.



    int r;
FILE *fp;
fp = stderr;

const SSL_METHOD *method;
method = SSLv23_server_method();
ERR_clear_error();

if ( !ctx)
{
ctx = SSL_CTX_new(method);
if (!ctx) {
return 0;
}

SSL_CTX_set_ecdh_auto(ctx, 1);



if (SSL_CTX_use_certificate_file(ctx, "/home/cert.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(fp);
}

if (SSL_CTX_use_PrivateKey_file(ctx, "/home/key.pem", SSL_FILETYPE_PEM) <= 0 ) {
ERR_print_errors_fp(fp);
}
}

if (!ctx) {
return;
}

SSL *ssl;
ssl = SSL_new(ctx);
SSL_set_fd(ssl, fd);

ERR_clear_error();

if (SSL_accept(ssl) <= 0) {
ERR_print_errors_fp(fp);
}

int rce = 0;
ERR_clear_error();

char *buffer = (char *) malloc(iova.iov_len);
memcpy(buffer, iova.iov_base, iova.iov_len);

r = SSL_write(ssl, buffer, (int)iova.iov_len);

// check how many bytes were written
if ( r <= 0 )
{
ERR_print_errors_fp(fp);
int err = errno;

switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
continue;
case SSL_ERROR_ZERO_RETURN:
syslog(LOG_INFO, "SSL read encountered an error %d. Peer disconnected.", SSL_ERROR_ZERO_RETURN);
break;
case SSL_RECEIVED_SHUTDOWN:
syslog(LOG_INFO, "SSL read encountered an error %d. Shutdown.", SSL_RECEIVED_SHUTDOWN);
break;
case SSL_ERROR_SYSCALL:
syslog(LOG_INFO, "SSL write encountered an error %d and %lu. SYSCALL", SSL_get_error(ssl,r), ERR_get_error());
break;
default:
syslog(LOG_INFO, "SSL write encountered an error %d and %d. Quitting", SSL_get_error(ssl,r), err);
break;
}


Couple issues that I could not figure out -




  1. SSL_accept() fails


  2. SSL_write() then fails with SSL_RECEIVED_SHUTDOWN and err string = error:00000002:lib(0):func(0):system lib



What am I doing wrong here with SSL_accept and SSL_write() ?





I am trying to use Openssl to do SSL_write() and I get the following error during SSL_write(). The server is trying to write some bytes to the socket connection fd -



error:140D0114:SSL routines:SSL_write:uninitialized:ssl_lib.c:1039:
Error: error:00000005:lib(0):func(0):DH lib


Here is my code



    SSL *ssl;
if ( !ctx )
return 0;
ssl = SSL_new(ctx);

//SSL_set_fd(ssl, fd);

BIO *sbio = NULL;
sbio = BIO_new_socket(fd, BIO_NOCLOSE);
SSL_set_bio(ssl, sbio, sbio);
ERR_clear_error();

FILE *fp;
fp = stderr;
r = SSL_write(ssl, buffer, (len);

if ( r <= 0 )
{
ERR_print_errors_fp(fp);

switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
syslog(LOG_INFO, "No real error. Continue.");
continue;
case SSL_ERROR_ZERO_RETURN:
syslog(LOG_INFO, "SSL read encountered an error %d. Peer disconnected.", SSL_ERROR_ZERO_RETURN);
break;
case SSL_RECEIVED_SHUTDOWN:
syslog(LOG_INFO, "SSL read encountered an error %d. Shutdown.", SSL_RECEIVED_SHUTDOWN);
break;
case SSL_ERROR_SYSCALL:
syslog(LOG_INFO, "SSL write encountered an error %d and %lu. SYSCALL", SSL_get_error(ssl,r), ERR_get_error());
break;
default:
syslog(LOG_INFO, "SSL write encountered an error %d and %d. Quitting", SSL_get_error(ssl,r), err);
break;
}


The SSL context (ctx) is valid to begin with.



I tried looking up an explanation of the error but did not find it. Nor did I find a similar issue that would resolve it. Can anyone help me understand what I can do to further debug this ? Or if they have a solution ?



Openssl version 1.0.2g







openssl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 4:47

























asked Nov 21 at 2:35









PeterJ

54




54












  • The server and the client can go thru the same code. There are no separate programs for the 2.
    – PeterJ
    Nov 21 at 4:52










  • Perhaps SSL_accept() fails because it is non-blocking and does not wait for an incoming SSL_connect() from a client ? I will see what SSL_get_error() is returned by SSL_accept(). In the meantine, how can I make it blocking ?
    – PeterJ
    Nov 21 at 5:17










  • So SSL_accept() returned error:00000002 which is SSL_ERROR_WANT_READ. My socket that I am trying to write to is non-blocking. So when I do a SSL_set_fd(ctx, fd) the SSL ctx is non-blocking as well.
    – PeterJ
    Nov 21 at 5:46




















  • The server and the client can go thru the same code. There are no separate programs for the 2.
    – PeterJ
    Nov 21 at 4:52










  • Perhaps SSL_accept() fails because it is non-blocking and does not wait for an incoming SSL_connect() from a client ? I will see what SSL_get_error() is returned by SSL_accept(). In the meantine, how can I make it blocking ?
    – PeterJ
    Nov 21 at 5:17










  • So SSL_accept() returned error:00000002 which is SSL_ERROR_WANT_READ. My socket that I am trying to write to is non-blocking. So when I do a SSL_set_fd(ctx, fd) the SSL ctx is non-blocking as well.
    – PeterJ
    Nov 21 at 5:46


















The server and the client can go thru the same code. There are no separate programs for the 2.
– PeterJ
Nov 21 at 4:52




The server and the client can go thru the same code. There are no separate programs for the 2.
– PeterJ
Nov 21 at 4:52












Perhaps SSL_accept() fails because it is non-blocking and does not wait for an incoming SSL_connect() from a client ? I will see what SSL_get_error() is returned by SSL_accept(). In the meantine, how can I make it blocking ?
– PeterJ
Nov 21 at 5:17




Perhaps SSL_accept() fails because it is non-blocking and does not wait for an incoming SSL_connect() from a client ? I will see what SSL_get_error() is returned by SSL_accept(). In the meantine, how can I make it blocking ?
– PeterJ
Nov 21 at 5:17












So SSL_accept() returned error:00000002 which is SSL_ERROR_WANT_READ. My socket that I am trying to write to is non-blocking. So when I do a SSL_set_fd(ctx, fd) the SSL ctx is non-blocking as well.
– PeterJ
Nov 21 at 5:46






So SSL_accept() returned error:00000002 which is SSL_ERROR_WANT_READ. My socket that I am trying to write to is non-blocking. So when I do a SSL_set_fd(ctx, fd) the SSL ctx is non-blocking as well.
– PeterJ
Nov 21 at 5:46



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404548%2fssl-write-returns-ssl-err-syscall-while-err-get-error-returns-0%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404548%2fssl-write-returns-ssl-err-syscall-while-err-get-error-returns-0%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python