POCO:如何正确地将StreamSocket附加到SecureStreamSocket?

时间:2013-10-03 13:10:34

标签: c++ ssl openssl poco-libraries

我尝试将连接的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初始化后的上下文init:

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

1 个答案:

答案 0 :(得分:0)

我们可以这样重写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;
}

感谢this post。现在它适用于服务器和客户端。