2016-09-14 50 views
0

我想在两台计算机之间安装TCP over SSL连接。 我在版本1.7.3中使用了poco librairies。如何通过poco使用SecureStreamSocket连接TCP服务器时发送证书

我成功进行了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()始终返回假。我一定错过了secureSocket初始化客户端的一些东西,但是我没有找到它。

回答

0

我找到了一种方法使它工作。

我已经添加下面的服务器代码检查客户端证书前行

securedSocket.completeHandshake(); 

最终代码:

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

securedSocket.completeHandshake(); 
if (!securedSocket.havePeerCertificate()) 
{ 
    ..... 
    ..... 

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

但我不知道我正在制作正确的工作流来验证SSL通信。任何其他方式?

相关问题