【问题标题】:How to send a certificate when connecting a TCP server using SecureStreamSocket with poco使用 SecureStreamSocket 和 poco 连接 TCP 服务器时如何发送证书
【发布时间】:2017-01-22 09:43:08
【问题描述】:

我想在两台计算机之间安装 TCP over SSL 连接。 我在 1.7.3 版本中使用 poco 库。

我在客户端的TCP通信和服务器证书验证成功。

我想在服务器端验证客户端证书。

这是我的客户端连接

Poco::Net::initializeSSL();
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler> keyHandler = new Poco::Net::KeyFileHandler(false);
SharedPtr<InvalidCertificateHandler> invalidCertHandler = new Poco::Net::ConsoleCertificateHandler(true);

// get parameters from configuration file
unsigned short port = (unsigned short) config().getInt("tcpClient.port", 9443);

Context::Ptr pClientContext = new Context(
                    Context::CLIENT_USE,
                    Application::instance().config().getString("openSSL.client.privateKeyFile"),
                    Application::instance().config().getString("openSSL.client.certificateFile"),
                    Application::instance().config().getString("openSSL.client.caConfig"),
                    Context::VERIFY_RELAXED,
                    9,
                    true,
                    Application::instance().config().getString("openSSL.client.cipherList"));

pClientContext->enableSessionCache(true);

SSLManager::instance().initializeClient(keyHandler, invalidCertHandler, pClientContext);

Poco::Net::SocketAddress sa("127.0.0.1", port);
SecureStreamSocket ss1(sa, pClientContext);

app.logger().information("Client connecté");

这是我的服务器端监听连接

Poco::Net::initializeSSL();
// get parameters from configuration file
unsigned short port = (unsigned short) config().getInt("tcpServer.port", 9443);

// set-up a server socket
SecureServerSocket svs(port);
Poco::Net::TCPServer srv(new EchoServerConnectionFactory(), svs);
// start the Server
srv.start();
logger().information("Attente de connexion client ...");

还有我的 EchoServerConnectionFactory 的 createConnection 方法

Poco::Net::TCPServerConnection* createConnection(const Poco::Net::StreamSocket& socket)
    {
        Application& app = Application::instance();

        app.logger().information("Tentative de connexion d'un client");

        if (!socket.secure())
        {
            app.logger().error("Client non sécurisé. Connexion refusée");
            return NULL;
        }

        try
        {
            Poco::Net::SecureStreamSocket securedSocket = (Poco::Net::SecureStreamSocket)dynamic_cast<const Poco::Net::StreamSocket&>(socket);

            if (!securedSocket.havePeerCertificate())
            {
                app.logger().error("Le client n'a pas présenté de certificat. Connexion refusée");
                return NULL;
            }
         ....
         ....

createConnection方法中,securedSocket.havePeerCertificate()方法总是返回false。我一定是在secureSocket初始化客户端遗漏了一些东西,但我没有找到。

【问题讨论】:

    标签: c++ sockets ssl tcp poco


    【解决方案1】:

    我已经找到了一种让它起作用的方法。

    我添加了以下行

    securedSocket.completeHandshake();
    

    在检查服务器代码上的客户端证书之前。

    最终代码:

    Poco::Net::SecureStreamSocket securedSocket = (Poco::Net::SecureStreamSocket)dynamic_cast<const Poco::Net::StreamSocket&>(socket);
    
    securedSocket.completeHandshake();
    if (!securedSocket.havePeerCertificate())
    {
         .....
         .....
    

    方法 securedSocket.havePeerCertificate() 现在成功了。

    但我不确定我是否制定了正确的工作流程来验证 SSL 通信。还有什么办法吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多