OpenSSL connection fails with non-blocking socket

别来无恙 提交于 2020-01-17 08:54:28

问题


I have an OpenSSL server that has echo functionality as described here and a client as described here. I made some minor changes to the server (e.g. changing the certifiacte and private key paths, adding some more debug outputs,...) and in the client I just removed the "BIO* out" and printed the BIO_read result to the console instead. Also both client and server use TLSv1_1_client_method and TLSv1_1_server_method respectively.

The codes work fine together, but if I add "BIO_set_nbio(web, 1);" to the client just before BIO_do_connect, the connection doesn't work anymore. BIO_do_connect returns -1. Can this be a handshake problem and if so, how can I handshake properly with non-blocking sockets?

This error first occurred in a larger project of mine. I just used the example codes to verify it. My problem is, I need non-blocking sockets, because I am calling BIO_read from a different thread and I can't join the thread if it's stuck trying to read.

I also tried to set the socket to non-blocking by using fd_set and select, but that throws the WSAENOTSOCK error (10038). I did the following:

Header:

BIO* _bio;
SSL* _ssl;
FD_SET _fdSet;
SOCKET _socket;
timeval t;

on connecting:

_bio = BIO_new_ssl_connect(_ctx);
BIO_get_ssl(_bio, &_ssl);
SSL_set_mode(_ssl, SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(_bio, _address); // _address is "hostname:port"
int connectResult = BIO_do_connect(_bio);

// ... error handling for connect, certificate verification

BIO_get_fd(_bio, _socket);
FD_ZERO(&_fdSet);
FD_SET(_socket, &_fdSet);
t.tv_sec = 2;
t.tv_usec = 0;

In the thread that polls the socket:

int selectResult = select(0, &fdSet, NULL, NULL, &t); // <-- throws WSAENOTSOCK
if (selectResult > 0)
    x = BIO_read(_bio, &buf, 1);

Is select even doable if you have a BIO socket?

The client is on Windows 7 64 Bit, the server runs on Ubuntu 64 bit on a VM on the same host as my Windows 7.

来源:https://stackoverflow.com/questions/29388113/openssl-connection-fails-with-non-blocking-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!