Search form

Qt Notes: Working with QSslSocket

Submitted by dgraves on Fri, 06/17/2011 - 17:54

The Qt cross-platform application and UI framework provides a great deal of functionality to explore. After working with it off and on for the past two years I am still finding new and useful features to include in my projects. Recently I have had the opportunity to work with the Secure Sockets Layer (SSL) support, and have put together some notes to group some key documentation points together and to clarify a few points from the documentation that I found to be unclear.

1. Detecting SSL support

The main class that you will be working with when using Qt's SSL support, QSslSocket, provides a static member function named supportsSsl(), which will tell you if SSL is supported by the library. SSL may not be supported if your version of Qt was compiled with SSL disabled, or if the OpenSSLlibraries on which Qt depends for its SSL support are not installed on your system.

QSslSocket::supportsSsl() can be used to programmatically detect that SSL is not supported, so that the application's user can be informed of the lack of SSL support.

void MyApplication::openConnection(const QString &hostname, unsigned short port) {
if (QSslSocket::supportsSsl()) {
QSslSocket *socket = new QSslSocket(this);
connect(socket, SIGNAL(encrypted()), this, SLOT(ready()));
socket->connectToHostEncrypted(hostname, port);
} else {
QMessageBox::critical(this, "No SSL Support",
"SSL is not supported by your version of Qt. You must obtain a version of Qt"
"that has SSL support enabled. If you believe that your version of Qt has"
"SSL support enabled, you may need to install the OpenSSL run-time libraries.");
}
}

2. Accepting an SSL connection

The QSslSocket documentation provides an example of accepting an SSL connection with a server socket that omits the important steps of key and certificate specification. If a valid key and certificate pair is not specified, the SSL handshake will fail.

Qt does not currently provide support for creating keys and certificates. As described by the Qt SSL certificate guide, the OpenSSL utilities can be used to create key and certificate files that are loaded by a Qt application.

3. The sslErrors(const QList<QSslError> &) signal

According to the QSslSocket documentation, the QSslSocket::sslErrors(const QList<QSslError> &) signal is emitted when one or more errors have occurred while establishing the identity of the peer. My experience has been that this signal is always emitted, even if no errors have occurred. If no errors have occurred the error list contains a single entry, QSslError::NoError [update: this appears to be a bug related to the OpenSSL version being used, and is not normal behavior; see QTBUG-19981].

Errors reported by QSslSocket::sslErrors(const QList<QSslError> &) are not necessarily fatal errors. It is possible that the handshake will succeed and the connection will complete, even if an error occurs while establishing the identity of the peer. Receipt of this signal should not be treated as an indication of connection failure.

Errors indicating that authentication of a peer's certificate has failed may be received, but if the peer verify mode is set to QSslSocket::VerifyNone the handshake will succeed and the connection will complete successfully. As noted by the QSslSocket documentation, when the peer verify mode is not set to QSslSocket::VerifyNone, SSL errors can be ignored by calling QSslSocket::ignoreSslErrors() from within the slot receiving the QSslSocket::sslErrors(const QList<QSslError &) signal.

That should help you get started writing Qt applications with SSL support. It will also serve as a nice reminder to me of what must be done to enable SSL support for a Qt application the next time I need to do it.

Update (July 14, 2011): I have created simple chat client and server applications to demonstrate the use of QSslSocket when opening secure connections as a client and accepting secure connections as a server. These examples provide notifications to the user when SSL support is not available, when key and certificate files are needed, and when SSL errors are encountered. They can be found in the following Github repository:

Comments

Hi, I am beginning to work with SSL in Qt, but still having problems. As you said, this part is poorly documented, and it's hard to find up-to-date examples. I followed the steps on this tutorial, but it's still not working. So my question is

Hi George, you will need to generate the SSL key and certificate with the third-party openssl tool. The Qt "Guide to SSL certificates and certificate authorities" has some examples demonstrating how this is done. The easiest option to get a key and certificate pair for testing is to generate a self-signed certificate like this:

This assumes the files are stored in the application's current working directory. You'll most likely need to specify the full path to the files. The key and certificate files are also only needed by server sockets. Client sockets should not require these files.

I think that it is also worth noting that the Qt libraries for Windows that are distributed by Nokia have SSL support enabled, but are not packaged with the OpenSSL DLLs that are required to make SSL work. If you are working on Windows and using the libraries provided by Nokia, you will need to install OpenSSL. I have been using the Win32 OpenSSL install package found here: http://www.slproweb.com/products/Win32OpenSSL.html

I'll try to put a simple QSslSocket example together, which includes working server and client code, and post it here.