【发布时间】: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初始化客户端遗漏了一些东西,但我没有找到。
【问题讨论】: