I have problems making openssl working with iocp on windows , currently trying client mode only
I could make async write and read using the memory bios but but I'm struggling to get the async connect and handshake work
if I connect using SSL_connect then do the handshake using SSL_do_handshake then replace the bios with memory bios I can read and write async well
in async connect I do the following :
this is some related code I'm using :
class AsyncSSLSocket;
using SSLOnConnect = std::function<void(AsyncSSLSocket&, std::error_code, SockAddr&)>;
struct SSLConnectCtx : public io::BaseIoCtx
{
AsyncSSLSocket *sslsock;
std::variant<IpV4Addr, IpV6Addr> addr;
SSLOnConnect OnConnect;
virtual void HandleIocpPacket(io::iocp::CompletionResult& IocpPacket) override;
void CompleteHandShake();
};
inline void SSLConnectCtx::HandleIocpPacket(io::iocp::CompletionResult& IocpPacket)
{
DWORD Transferred, RecvFlags;
WSAGetOverlappedResult(sock->GetHandle().get(), reinterpret_cast<LPWSAOVERLAPPED>(IocpPacket.Ctx), &Transferred, 0, &RecvFlags);
auto error = sock->LastError(); // WSAGetLastError()
if (!error)
{
IocpPacket.Ctx = nullptr;
sslsock->sslhandle.SetClientMode(); // SSL_set_connect_state
CompleteHandShake();
return;
}
auto& peer_addr = ExtractAddr(addr);
OnConnect(*sslsock, error, peer_addr);
}
inline void SSLConnectCtx::CompleteHandShake()
{
auto& sslhandle = sslsock->sslhandle;
int ret = sslhandle.DoHandShake(); // SSL_do_handshake
std::error_code error;
if (ret < 0)
{
error = sslhandle.LastError(ret);
if (sslhandle.WantRead(error.value())) // always get here
{
error = sslsock->FillInBIO([this](auto&, auto error, uint32_t trans)
{
MakeErrorIfZeroIsRecved(trans, error); // set error to std::errc::connection_abort (106) if WSARecv received 0 bytes
if (!error)
{
CompleteHandShake();
return;
}
// always get here
// prints : [!] failed to get data for the handshake !, error : generic:106 ==> connection aborted
std::cout << "[!] failed to get data for the handshake !, error : " << error << " ==> " << error.message() << std::endl;
OnConnect(*sslsock, error, ExtractAddr(addr));
delete this;
});
if (!error)
return;
}
else if (sslhandle.WantWrite(error.value()))
{
error = sslsock->FlushOutBIO([this](auto&, auto error, uint32_t)
{
if (!error)
{
CompleteHandShake();
return;
}
OnConnect(*sslsock, error, ExtractAddr(addr));
delete this;
});
if (!error)
return;
}
}
OnConnect(*sslsock, error, ExtractAddr(addr));
delete this;
}
void AsyncSSLSocket::AsyncConnect(SockAddr& addr, const SSLOnConnect& OnConnect)
{
SSLConnectCtx * ctx{ new SSLConnectCtx{} };
ctx->sslsock = this;
ctx->sock = &sock;
SetAddrs(ctx->addr, addr);
ctx->OnConnect = OnConnect;
auto result = sock.AsyncConnect(addr, *ctx);
if (result)
return;
delete ctx;
OnConnect(*this, sock.LastError(), addr);
}
std::error_code AsyncSSLSocket::FillInBIO(SSLOnRead&& OnRead)
{
char *buff = new char[1024];
CachedReadBuff = io::IoBuffer(buff, 1024);
CachedOnRead = std::move(OnRead);
SockRecvCtx * ctx{ new SockRecvCtx{} };
ctx->sock = &sock;
ctx->OnRecv = [this](Socket&, std::error_code error, uint32_t transferred)
{
MakeErrorIfZeroIsRecved(transferred, error);
if (!error)
inBIO.Write(CachedReadBuff.data(), static_cast<int>(transferred));
delete CachedReadBuff.data();
CachedOnRead(*this, error, transferred);
};
auto result = sock.AsyncRecv(CachedReadBuff, *ctx);
if (result)
return std::error_code{};
delete ctx;
return result.error;
}
it seems that SSL_do_handshake put some data in the out bio and returned SSL_get_error returned SSL_ERROR_WANT_READ so I went to read more data from the server while the server was waiting to receive some data from the client , so WSARecv or recv waits for some seconds ( maybe the server has timeout ?) until the server closes the connection and I get 0 bytes received .
so I used this code :
inline void SSLConnectCtx::CompleteHandShake()
{
auto& sslhandle = sslsock->sslhandle;
int ret = sslhandle.DoHandShake(); // SSL_do_handshake
std::error_code error;
if (ret < 0)
{
int pending = sslsock->outBIO.Pending();
if (pending > 0)
{
std::cout << "[!!!] there is some data to send !" << std::endl;
error = sslsock->FlushOutBIO([this](auto&, auto error, uint32_t)
{
if (!error)
{
CompleteHandShake();
return;
}
OnConnect(*sslsock, error, ExtractAddr(addr));
delete this;
});
if (!error)
return;
std::cout << "[!] failed to flush ! , error " << error << " ==> " << error.message() << std::endl;
}
error = sslhandle.LastError(ret);
if (sslhandle.WantRead(error.value())) // always get here
{
std::cout << "[!!!] needs to read in the bio" << std::endl;
error = sslsock->FillInBIO([this](auto&, auto error, uint32_t trans)
{
MakeErrorIfZeroIsRecved(trans, error); // set error to std::errc::connection_abort (106) if WSARecv received 0 bytes
if (!error)
{
CompleteHandShake();
return;
}
// always get here
// prints : [!] failed to get data for the handshake !, error : generic:106 ==> connection aborted
std::cout << "[!] failed to get data for the handshake !, error : " << error << " ==> " << error.message() << std::endl;
OnConnect(*sslsock, error, ExtractAddr(addr));
delete this;
});
if (!error)
return;
}
else if (sslhandle.WantWrite(error.value()))
{
error = sslsock->FlushOutBIO([this](auto&, auto error, uint32_t)
{
if (!error)
{
CompleteHandShake();
return;
}
OnConnect(*sslsock, error, ExtractAddr(addr));
delete this;
});
if (!error)
return;
}
}
OnConnect(*sslsock, error, ExtractAddr(addr));
delete this;
}
but now how the error was SSL_ERROR_WANT_READ while there is a need to write before read !
I read that there may be a re-negotiation at any point during the communication so if SSL_write returned SSL_ERROR_WANT_READ should I begin to read or send pending data ? also if SSL_read returned SSL_ERROR_WANT_WRITE should I begin to send data or receive data ?