How to get OpenSSL BIO_do_connect() failure reason?










1















I'm using Ubuntu 18.04/gcc 7.3/OpenSSL 1.1.0g to make C++ app performing TLS/SSL connection with non-blocking BIO API.



When BIO_do_connect() fails connecting, e.g. if using wrong host name or port, there is no errors reported by OpenSSL. ERR_get_error() returns zero and ERR_print_errors_xx() doesn't print anything.



So the question is - how to get actual connection failure reason, e.g. 'Connection refused' or 'Host not resolved' etc?



Used code snippet below:



#include <cstdio>
#include <cstring>
#include <iostream>

#include "openssl/bio.h"
#include "openssl/err.h"
#include "openssl/ssl.h"

int main(int argc, char *argv)

OPENSSL_init_ssl(OPENSSL_INIT_SSL_DEFAULT, nullptr);

std::cout << OpenSSL_version(OPENSSL_VERSION) << std::endl;

SSL_CTX* ctx = SSL_CTX_new(TLS_client_method());
if (!ctx)

std::cerr << "Error creating SSL context:" << std::endl;
ERR_print_errors_fp(stderr);
return 1;


SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);

if(!SSL_CTX_load_verify_locations(ctx,
"/etc/ssl/certs/ca-certificates.crt",
nullptr))

std::cerr << "Error loading trust store into SSL context" << std::endl;
ERR_print_errors_fp(stderr);
return 1;


BIO* cbio = BIO_new_ssl_connect(ctx);

SSL* ssl = nullptr;
BIO_get_ssl(cbio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

BIO_set_conn_hostname(cbio, "not_actually_existing_host.com:https");
BIO_set_nbio(cbio, 1);

std::cout << "Start connecting" << std::endl;

next:
if (BIO_do_connect(cbio) <= 0)

if (!BIO_should_retry(cbio))

std::cerr << "Error attempting to connect:" << std::endl;
ERR_print_errors_fp(stderr); // <---- PRINTS NOTHING!!!
BIO_free_all(cbio);
SSL_CTX_free(ctx);
return 1;

else goto next;


std::cout << "Connected OK" << std::endl;

BIO_free_all(cbio);
SSL_CTX_free(ctx);
return 0;










share|improve this question

















  • 1





    You seem to be doing what the manual suggests, if it is a socket error you may be able to squeeze some information out of errno using std::strerror(errno)?

    – Galik
    Nov 14 '18 at 11:51











  • Yes, good idea, its working!

    – Rost
    Nov 14 '18 at 13:25















1















I'm using Ubuntu 18.04/gcc 7.3/OpenSSL 1.1.0g to make C++ app performing TLS/SSL connection with non-blocking BIO API.



When BIO_do_connect() fails connecting, e.g. if using wrong host name or port, there is no errors reported by OpenSSL. ERR_get_error() returns zero and ERR_print_errors_xx() doesn't print anything.



So the question is - how to get actual connection failure reason, e.g. 'Connection refused' or 'Host not resolved' etc?



Used code snippet below:



#include <cstdio>
#include <cstring>
#include <iostream>

#include "openssl/bio.h"
#include "openssl/err.h"
#include "openssl/ssl.h"

int main(int argc, char *argv)

OPENSSL_init_ssl(OPENSSL_INIT_SSL_DEFAULT, nullptr);

std::cout << OpenSSL_version(OPENSSL_VERSION) << std::endl;

SSL_CTX* ctx = SSL_CTX_new(TLS_client_method());
if (!ctx)

std::cerr << "Error creating SSL context:" << std::endl;
ERR_print_errors_fp(stderr);
return 1;


SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);

if(!SSL_CTX_load_verify_locations(ctx,
"/etc/ssl/certs/ca-certificates.crt",
nullptr))

std::cerr << "Error loading trust store into SSL context" << std::endl;
ERR_print_errors_fp(stderr);
return 1;


BIO* cbio = BIO_new_ssl_connect(ctx);

SSL* ssl = nullptr;
BIO_get_ssl(cbio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

BIO_set_conn_hostname(cbio, "not_actually_existing_host.com:https");
BIO_set_nbio(cbio, 1);

std::cout << "Start connecting" << std::endl;

next:
if (BIO_do_connect(cbio) <= 0)

if (!BIO_should_retry(cbio))

std::cerr << "Error attempting to connect:" << std::endl;
ERR_print_errors_fp(stderr); // <---- PRINTS NOTHING!!!
BIO_free_all(cbio);
SSL_CTX_free(ctx);
return 1;

else goto next;


std::cout << "Connected OK" << std::endl;

BIO_free_all(cbio);
SSL_CTX_free(ctx);
return 0;










share|improve this question

















  • 1





    You seem to be doing what the manual suggests, if it is a socket error you may be able to squeeze some information out of errno using std::strerror(errno)?

    – Galik
    Nov 14 '18 at 11:51











  • Yes, good idea, its working!

    – Rost
    Nov 14 '18 at 13:25













1












1








1








I'm using Ubuntu 18.04/gcc 7.3/OpenSSL 1.1.0g to make C++ app performing TLS/SSL connection with non-blocking BIO API.



When BIO_do_connect() fails connecting, e.g. if using wrong host name or port, there is no errors reported by OpenSSL. ERR_get_error() returns zero and ERR_print_errors_xx() doesn't print anything.



So the question is - how to get actual connection failure reason, e.g. 'Connection refused' or 'Host not resolved' etc?



Used code snippet below:



#include <cstdio>
#include <cstring>
#include <iostream>

#include "openssl/bio.h"
#include "openssl/err.h"
#include "openssl/ssl.h"

int main(int argc, char *argv)

OPENSSL_init_ssl(OPENSSL_INIT_SSL_DEFAULT, nullptr);

std::cout << OpenSSL_version(OPENSSL_VERSION) << std::endl;

SSL_CTX* ctx = SSL_CTX_new(TLS_client_method());
if (!ctx)

std::cerr << "Error creating SSL context:" << std::endl;
ERR_print_errors_fp(stderr);
return 1;


SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);

if(!SSL_CTX_load_verify_locations(ctx,
"/etc/ssl/certs/ca-certificates.crt",
nullptr))

std::cerr << "Error loading trust store into SSL context" << std::endl;
ERR_print_errors_fp(stderr);
return 1;


BIO* cbio = BIO_new_ssl_connect(ctx);

SSL* ssl = nullptr;
BIO_get_ssl(cbio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

BIO_set_conn_hostname(cbio, "not_actually_existing_host.com:https");
BIO_set_nbio(cbio, 1);

std::cout << "Start connecting" << std::endl;

next:
if (BIO_do_connect(cbio) <= 0)

if (!BIO_should_retry(cbio))

std::cerr << "Error attempting to connect:" << std::endl;
ERR_print_errors_fp(stderr); // <---- PRINTS NOTHING!!!
BIO_free_all(cbio);
SSL_CTX_free(ctx);
return 1;

else goto next;


std::cout << "Connected OK" << std::endl;

BIO_free_all(cbio);
SSL_CTX_free(ctx);
return 0;










share|improve this question














I'm using Ubuntu 18.04/gcc 7.3/OpenSSL 1.1.0g to make C++ app performing TLS/SSL connection with non-blocking BIO API.



When BIO_do_connect() fails connecting, e.g. if using wrong host name or port, there is no errors reported by OpenSSL. ERR_get_error() returns zero and ERR_print_errors_xx() doesn't print anything.



So the question is - how to get actual connection failure reason, e.g. 'Connection refused' or 'Host not resolved' etc?



Used code snippet below:



#include <cstdio>
#include <cstring>
#include <iostream>

#include "openssl/bio.h"
#include "openssl/err.h"
#include "openssl/ssl.h"

int main(int argc, char *argv)

OPENSSL_init_ssl(OPENSSL_INIT_SSL_DEFAULT, nullptr);

std::cout << OpenSSL_version(OPENSSL_VERSION) << std::endl;

SSL_CTX* ctx = SSL_CTX_new(TLS_client_method());
if (!ctx)

std::cerr << "Error creating SSL context:" << std::endl;
ERR_print_errors_fp(stderr);
return 1;


SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);

if(!SSL_CTX_load_verify_locations(ctx,
"/etc/ssl/certs/ca-certificates.crt",
nullptr))

std::cerr << "Error loading trust store into SSL context" << std::endl;
ERR_print_errors_fp(stderr);
return 1;


BIO* cbio = BIO_new_ssl_connect(ctx);

SSL* ssl = nullptr;
BIO_get_ssl(cbio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

BIO_set_conn_hostname(cbio, "not_actually_existing_host.com:https");
BIO_set_nbio(cbio, 1);

std::cout << "Start connecting" << std::endl;

next:
if (BIO_do_connect(cbio) <= 0)

if (!BIO_should_retry(cbio))

std::cerr << "Error attempting to connect:" << std::endl;
ERR_print_errors_fp(stderr); // <---- PRINTS NOTHING!!!
BIO_free_all(cbio);
SSL_CTX_free(ctx);
return 1;

else goto next;


std::cout << "Connected OK" << std::endl;

BIO_free_all(cbio);
SSL_CTX_free(ctx);
return 0;







c++ ssl openssl network-programming






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 11:45









RostRost

7,5432039




7,5432039







  • 1





    You seem to be doing what the manual suggests, if it is a socket error you may be able to squeeze some information out of errno using std::strerror(errno)?

    – Galik
    Nov 14 '18 at 11:51











  • Yes, good idea, its working!

    – Rost
    Nov 14 '18 at 13:25












  • 1





    You seem to be doing what the manual suggests, if it is a socket error you may be able to squeeze some information out of errno using std::strerror(errno)?

    – Galik
    Nov 14 '18 at 11:51











  • Yes, good idea, its working!

    – Rost
    Nov 14 '18 at 13:25







1




1





You seem to be doing what the manual suggests, if it is a socket error you may be able to squeeze some information out of errno using std::strerror(errno)?

– Galik
Nov 14 '18 at 11:51





You seem to be doing what the manual suggests, if it is a socket error you may be able to squeeze some information out of errno using std::strerror(errno)?

– Galik
Nov 14 '18 at 11:51













Yes, good idea, its working!

– Rost
Nov 14 '18 at 13:25





Yes, good idea, its working!

– Rost
Nov 14 '18 at 13:25












1 Answer
1






active

oldest

votes


















0














This approach finally works for me:



 const auto sysErrorCode = errno;
const auto sslErrorCode = ERR_get_error();

std::string errorDescription;
if (sslErrorCode != 0) errorDescription = ERR_error_string(sslErrorCode, nullptr);
if (sysErrorCode != 0)

if (!errorDescription.empty()) errorDescription += 'n';
errorDescription += "System error, code=" + std::to_string(sysErrorCode);
errorDescription += ", ";
errorDescription += strerror(sysErrorCode);






share|improve this answer






















    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%2f53299498%2fhow-to-get-openssl-bio-do-connect-failure-reason%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This approach finally works for me:



     const auto sysErrorCode = errno;
    const auto sslErrorCode = ERR_get_error();

    std::string errorDescription;
    if (sslErrorCode != 0) errorDescription = ERR_error_string(sslErrorCode, nullptr);
    if (sysErrorCode != 0)

    if (!errorDescription.empty()) errorDescription += 'n';
    errorDescription += "System error, code=" + std::to_string(sysErrorCode);
    errorDescription += ", ";
    errorDescription += strerror(sysErrorCode);






    share|improve this answer



























      0














      This approach finally works for me:



       const auto sysErrorCode = errno;
      const auto sslErrorCode = ERR_get_error();

      std::string errorDescription;
      if (sslErrorCode != 0) errorDescription = ERR_error_string(sslErrorCode, nullptr);
      if (sysErrorCode != 0)

      if (!errorDescription.empty()) errorDescription += 'n';
      errorDescription += "System error, code=" + std::to_string(sysErrorCode);
      errorDescription += ", ";
      errorDescription += strerror(sysErrorCode);






      share|improve this answer

























        0












        0








        0







        This approach finally works for me:



         const auto sysErrorCode = errno;
        const auto sslErrorCode = ERR_get_error();

        std::string errorDescription;
        if (sslErrorCode != 0) errorDescription = ERR_error_string(sslErrorCode, nullptr);
        if (sysErrorCode != 0)

        if (!errorDescription.empty()) errorDescription += 'n';
        errorDescription += "System error, code=" + std::to_string(sysErrorCode);
        errorDescription += ", ";
        errorDescription += strerror(sysErrorCode);






        share|improve this answer













        This approach finally works for me:



         const auto sysErrorCode = errno;
        const auto sslErrorCode = ERR_get_error();

        std::string errorDescription;
        if (sslErrorCode != 0) errorDescription = ERR_error_string(sslErrorCode, nullptr);
        if (sysErrorCode != 0)

        if (!errorDescription.empty()) errorDescription += 'n';
        errorDescription += "System error, code=" + std::to_string(sysErrorCode);
        errorDescription += ", ";
        errorDescription += strerror(sysErrorCode);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 3 '18 at 7:42









        RostRost

        7,5432039




        7,5432039





























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299498%2fhow-to-get-openssl-bio-do-connect-failure-reason%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo