【问题标题】:Using Openssl with Async Winsock将 Openssl 与异步 Winsock 一起使用
【发布时间】:2012-06-08 19:38:55
【问题描述】:

我有一个异步 winsock 实现(服务器和客户端),我想将 Openssl 添加到以实现安全连接。例如,服务器会处理这些消息:

FD_ACCEPT、FD_READ、FD_WRITE 和 FD_CLOSE

我发现一篇文章提出了一种将 Openssl 添加到异步程序中的方法

http://funcptr.net/2012/04/08/openssl-as-a-filter-%28or-non-blocking-openssl%29/

其代码如下所示:

    class SSLFilter {
        public:
            SSLFilter(SSL_CTX* ctxt,
                      std::string* nread,
                      std::string* nwrite,
                      std::string* aread,
                      std::string* awrite);
            virtual ~SSLFilter();

            void update();

        private:
            bool continue_ssl_(int function_return);

            SSL * ssl;
            BIO * rbio;
            BIO * wbio;

            std::string* nread;
            std::string* nwrite;
            std::string* aread;
            std::string* awrite;
    };


    SSLFilter::SSLFilter(SSL_CTX* ctxt, 
                         std::string* nread, 
                         std::string* nwrite,
                         std::string* aread,
                         std::string* awrite)
                          :
                         nread(nread), 
                         nwrite(nwrite), 
                         aread(aread), 
                         awrite(awrite)

        rbio = BIO_new(BIO_s_mem());
        wbio = BIO_new(BIO_s_mem());

        ssl = SSL_new(ctxt);

        SSL_set_accept_state(ssl);
        SSL_set_bio(ssl, rbio, wbio);
    }

    SSLFilter::~SSLFilter() {
        SSL_free(_ssl);
    }

    void SSLFilter::update(Filter::FilterDirection) {
        // If we have data from the network to process, put it the memory BIO for OpenSSL
        if (!nread->empty()) {
            int written = BIO_write(rbio, nread->c_str(), nread->length());
            if (written > 0) nread->erase(0, written);
        }

        // If the application wants to write data out to the network, process it with SSL_write
        if (!awrite->empty()) {
            int written = SSL_write(ssl, awrite->c_str(), awrite->length());

            if (!continue_ssl_()) {
                throw std::runtime_error("An SSL error occured.");
            }

            if (written > 0) awrite->erase(0, written);
        }

        // Read data for the application from the encrypted connection and place it in the string for the app to read
        while (1) {
            char *readto = new char[1024];
            int read = SSL_read(ssl, readto, 1024);

            if (!continue_ssl_()) {
                delete readto;
                throw std::runtime_error("An SSL error occured.");
            }

            if (read > 0) {
                size_t cur_size = aread->length();
                aread->resize(cur_size + read);
                std::copy(readto, readto + read, aread->begin() + cur_size);
            }

            delete readto;

            if (static_cast<size_t>(read) != 1024 || written == 0) break;
        }

        // Read any data to be written to the network from the memory BIO and copy it to nwrite
        while (1) {
            char *readto = new char[1024];
            int read = BIO_read(wbio, readto, 1024);

            if (read > 0) {
                size_t cur_size = nwrite->length();
                nwrite->resize(cur_size + read);
                std::copy(readto, readto + read, nwrite->begin() + cur_size);
            }

            delete readto;

            if (static_cast<size_t>(read) != 1024 || read == 0) break;
        }
    }

    bool SSLFilter::continue_ssl_(int function_return) {
        int err = SSL_get_error(ssl, function_return);

        if (err == SSL_ERROR_NONE || err == SSL_ERROR_WANT_READ) {
            return true;
        }

        if (err == SSL_ERROR_SYSCALL) {
            ERR_print_errors_fp(stderr);
            perror("syscall error: ");
            return false;
        }

        if (err == SSL_ERROR_SSL) {
            ERR_print_errors_fp(stderr);
            return false;
        }
        return true;
    }

以下是使用此过滤器的步骤:

在程序启动时根据需要设置 SSL_CTX 结构,以便在 SSLFilter 中使用它。设置套接字/事件循环并接受()新套接字。创建四个新的 std::string 并创建一个新的 SSLFilter,传入您创建的 SSL_CTX 和四个缓冲区。将新的套接字添加到事件循环中等待读取。

现在在从事件循环接收到读取就绪状态后,应执行以下操作:

将数据读入 nread 缓冲区。 调用 SSLFilter::update() 看 aread 是否为空,随意处理数据 根据需要将数据写入awrite,如果数据写入awrite,调用SSLFilter::update(WRITE)处理数据 查看 nwrite 是否不为空,如果是,则将套接字添加到事件循环以准备写入。

一旦您从事件循环收到写入就绪状态,您应该执行以下操作:

将 nwrite 中的数据写入套接字。 如果 nwrite 为空,则从事件循环中删除套接字以进行写入(这样您就没有事件循环通知您可以写入,即使您没有要写入的内容)。

我的问题是: * 我可以将此方法与异步 winsock 一起使用吗?还是我应该考虑另一种实现? * 是否需要进行任何更改? * 如何处理 WOULDBLOCKS?

感谢您的意见。

【问题讨论】:

    标签: asynchronous openssl winsock


    【解决方案1】:

    早在 2002 年,我就曾为 Windows Developer Magazine 写过一篇关于此的文章,它是 reprinted here。本文解释了如何将 OpenSSL 与异步 winsock 调用一起使用,并提供了可与标准异步 winsock 调用一起使用且易于集成到 IOCP 样式异步调用中的代码。

    【讨论】:

    • Len,感谢您的代码。我在我的问题中引用的代码看起来像你的缩小版本,所以这对我来说是一个很好的起点,因为我是 openSSL 的新手。实现您的代码将是我的下一步。
    • Len,我对您的代码还有一些问题:1)我们需要处理 ssl_error_want_write 吗? 2) 我们是否需要调用 ssl_shutdown、ssl_clear 或 ssl_free 才能正确关闭 ssl 连接? 3)我们需要将bios设置为非阻塞吗?
    • 我已经有一段时间没有查看您将使用的代码版本了。但是...查看我的 IOCP 版本的最新版本... 1) 我从来没有明确处理过SSL_ERROR_WANT_WRITE,并且我对所有我不处理的错误都抛出异常,所以我会说不. 2)当我完成它时,我在上下文中调用SSL_free()。我不打电话给SSL_shutdown()。 3)我没有将bios设置为非阻塞,我只是创建两个BIO_s_mems并调用SSL_set_bio()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2019-11-10
    • 2018-07-03
    • 2012-09-02
    相关资源
    最近更新 更多