2016-09-14 49 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通信。任何其他方式?

相關問題