POCO:如何正确将 StreamSocket 连接到 SecureStreamSocket

POCO: How to properly attach StreamSocket to SecureStreamSocket?

本文关键字:连接 SecureStreamSocket StreamSocket 何正确 POCO      更新时间:2023-10-16

我尝试将连接的StreamSocket连接到SecureStreamSocket:

class MyConnection : public TCPServerConnection {
  private:
    Context::Ptr m_pContext;
  public:
    MyConnection(const StreamSocket& socket, Context::Ptr pContext)
    : TCPServerConnection(socket),
      m_pContext(pContext)
    {
    }
    virtual void run() {
      SecureStreamSocket secureSock(SecureStreamSocket::attach(socket(), m_pContext));
      socket() = secureSock;
    }
}

但我有SSLException:

SSL Exception [N4Poco3Net12SSLExceptionE]: error:140C5042:SSL routines:SSL_UNDEFINED_FUNCTION:called a function you should not call

执行了 SSL 库的初始化:

Poco::Net::initializeSSL(); // inside Application::initialize

SSL 初始化后初始化的上下文:

 Poco::Net::Context::Ptr pContext =
      new Poco::Net::Context(
          Poco::Net::Context::SERVER_USE,
          "server.key",
          "server.crt",
          "/path/to/certs/"
          );

我们可以这样重写 SecureStreamSocket::attach(...):

SecureStreamSocket SecureStreamSocket::attach(const StreamSocket& streamSocket, Context::Ptr pContext)
{
  SecureStreamSocketImpl* pImpl = new SecureStreamSocketImpl(static_cast<StreamSocketImpl*>streamSocket.impl()), pContext);
  SecureStreamSocket result(pImpl);
  if (pImpl->context()->isForServerUse())
      pImpl->acceptSSL();
  else
      pImpl->connectSSL();
  return result;
}

感谢这篇文章。现在它适用于服务器和客户端。