简体   繁体   English

iocp openssl peer server 与 ConnectEx 连接后关闭连接

[英]iocp openssl peer server closes the connection after connecting with ConnectEx

I have problems making openssl working with iocp on windows , currently trying client mode only我在 windows 上使用 iocp 使用 openssl 时遇到问题,目前仅尝试客户端模式

I could make async write and read using the memory bios but but I'm struggling to get the async connect and handshake work我可以使用内存 bios 进行异步写入和读取,但是我正在努力使异步连接和握手工作

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如果我使用 SSL_connect 连接然后使用 SSL_do_handshake 进行握手然后用内存 bios 替换 bios 我可以很好地读写异步

in async connect I do the following :在异步连接中,我执行以下操作:

  • issue a connection with ConnectEx与 ConnectEx 建立连接
  • handle the result with iocp使用 iocp 处理结果
  • call SSL_do_handshake and retrieve the result调用 SSL_do_handshake 并检索结果
  • check if there is error if SSL_WANT_READ or SSL_WANT_WRITE and I always get the former检查 SSL_WANT_READ 或 SSL_WANT_WRITE 是否有错误,我总是得到前者
  • I then try to receive some data into the in bio (tried async and sync) but here the failure .然后我尝试将一些数据接收到生物中(尝试异步和同步),但这里失败了。 I tried using recv and complete the handshake sync but recv returned 0 .我尝试使用 recv 并完成握手同步,但 recv 返回 0 。 also tried WSARecv but in the completion notification I get 0 bytes transferred so I end the connection也尝试过 WSARecv 但在完成通知中我得到了 0 个字节的传输所以我结束了连接

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 .似乎 SSL_do_handshake 将一些数据放入 out bio 并返回 SSL_get_error 返回 SSL_ERROR_WANT_READ 所以我去从服务器读取更多数据而服务器正在等待从客户端接收一些数据,所以 WSARecv 或 recv 等待几秒钟(也许是服务器超时?)直到服务器关闭连接并且我收到 0 个字节。

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 !但是现在错误是 SSL_ERROR_WANT_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 ?我读到在通信过程中的任何时候都可能会重新协商,所以如果 SSL_write 返回 SSL_ERROR_WANT_READ 我应该开始读取或发送挂起的数据吗? also if SSL_read returned SSL_ERROR_WANT_WRITE should I begin to send data or receive data ?如果 SSL_read 返回 SSL_ERROR_WANT_WRITE 我应该开始发送数据还是接收数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM