7

I got a sample projct that uses QSslSocket and QSslCertificate to get information from a SSL certificate (Qt 5.7, Windows 10). QSslSocket::supportsSsl() function returns false and I get these errors at runtime:

qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
qt.network.ssl: QSslSocket: cannot call unresolved function SSL_CTX_new
qt.network.ssl: QSslSocket: cannot call unresolved function SSL_library_init
qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error

So I compiled OpenSSL_1.1.0 and linked it to my project:

INCLUDEPATH += . "C:/OpenSSL-Win32/include/" LIBS +=
-L"C:/OpenSSL-Win32/lib/" -llibcrypto -llibssl

But still same errors. I downloaded OpenSSL installer and still the same.

Weird is QSslSocket::sslLibraryBuildVersionString() returns OpenSSL 1.0.2g 1 Mar 2016, even though I compiled and installed OpenSSL_1.1.0.

I know I'm missing a simple detail. Please tell me what is it. Thanks.

Mustafa Chelik
  • 2,113
  • 2
  • 17
  • 29
  • The `INCLUDEPATH` is unnecessary, of course, since you don't compile any code using openssl directly. All you need is the `LIBS += ...` line. If openssl is compiled as a dynamically linked library, you not only need to add its import library in the `.pro` file, but you also need to ensure that it's in the path when you run your project! – Kuba hasn't forgotten Monica Sep 14 '16 at 15:22
  • I removed `INCLUDEPATH` and kept `LIBS` in `.pro` file. Added OpenSSL's `bin` directory in `%PATH%`. Still `QSslSocket::supportsSsl()` returns false and `QSslSocket::sslLibraryVersionString()` returns `OpenSSL 1.0.2g 1 Mar 2016` even though I compiled OpenSSL 1.1.0 – Mustafa Chelik Sep 14 '16 at 15:38
  • Add `qDebug() << qgetenv("PATH");` to `main()` and ensure that your library path appears there and that it is correct. Copy-paste the library path from the debug output to an explorer window and make sure it's valid. – Kuba hasn't forgotten Monica Sep 14 '16 at 15:46
  • It has `C:\\OpenSSL-Win32\\lib` but it's not valid. Recently I deleted everything and built OpenSSL from scratch in `C:\openssl_1.1.0`. How can I change it? I couldn't find its setting. – Mustafa Chelik Sep 14 '16 at 16:21
  • Read up on superuser how to set environment variables on Windows. Such question is truly off-topic here. – Kuba hasn't forgotten Monica Sep 14 '16 at 18:00
  • "Recently I deleted everything" That's some cargo cult programming. Don't do it. You must first understand *why* the wrong copy was used. And then rectify that, not delete stuff randomly. Deleting it taught you nothing, but you needed to learn. You should be able to have multiple builds of openssl and Qt coexist on the same machine, in fact that's the only sane way to develop with Qt: you'll want a debug build of Qt and all its dependencies, and a release build, and perhaps test builds with new versions, etc. – Kuba hasn't forgotten Monica Sep 14 '16 at 18:02
  • @KubaOber I surely know how to set environment variables in Windows but `C:\OpenSSL-Win32\lib` is not in `%PATH%`. I don't know from where Qt gets it! I deleted everything because OpenSSL_1.1.0 doesn't work with Qt. I compiled OpenSSL_1.0.2g that Qt uses. Anyway still I get the same errors unfortunately. Thanks for your replies Kuba. – Mustafa Chelik Sep 14 '16 at 18:27
  • Did you compile Qt yourself? If not, Qt binaries come with their own built-in copy, apparently. – Kuba hasn't forgotten Monica Sep 14 '16 at 18:36
  • No, my Qt is precompiled. Since `QSslSocket::sslLibraryBuildVersionString()` returns `OpenSSL 1.0.2g 1 Mar 2016`, I downloaded OpenSSL_1.0.2g source code and compiled it, but inside bin directory there was only `openssl.exe`. So I copied `ssleay32.dll` and `libeay32.dll` from `C:\Qt\Tools\QtCreator\bin` to `C:\Windows`. But still I get same error, nothing changed. What should I do use QSslSocket? I'm really confused. I searched a lot but nothing helped. – Mustafa Chelik Sep 14 '16 at 18:42
  • `C:\Windows` is off-limits. Don't touch it. It's not yours to put stuff in. It belongs to the OS. Secondly, `C:\Qt\Tools\QtCreator` is a related to the installation of Qt Creator, not Qt itself. – Kuba hasn't forgotten Monica Sep 14 '16 at 18:52
  • Since I was desperate I tried to copy them in `C:\Windows`. I know, I just copied `ssleay32.dll` and `libeay32.dll` from there. Even copied them beside my application's executable but still I get same errors. I don't understand. Everyone uses `QSslSocket` without problem and it's only me that wrestles with it. In Qt documents there is no mention to install OpenSSL. What should i do to use `QSslSocket`? Offf (¤﹏¤) – Mustafa Chelik Sep 14 '16 at 18:58
  • I'm sorry but I don't use pre-built Qt, so I wouldn't know what special approach it takes to get it to work :( – Kuba hasn't forgotten Monica Sep 14 '16 at 18:59

1 Answers1

10

From the documentation:

QSslSocket::sslLibraryBuildVersionString() Returns the version string of the SSL library in use at compile time. (emphasis mine)

That you get a result from it doesn't mean that Qt has access to OpenSSL, only that it was compiled against a certain version. That lets you know what binary-compatible version you should provide at runtime if it's dynamically linked (as it is by default).

You want to check QSslSocket::sslLibraryVersionNumber() - [...] the version of the library in use at run-time (emphasis mine).

Qt will presumably attempt to find a copy of OpenSSL dlls in PATH, and if not, in the paths given by QCoreApplication::libraryPaths. You should add the location of OpenSSL dlls to either one before attempting to use OpenSSL.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Ok, so I should provide OpenSSL 1.0.2g DLLs and put them in `PATH` to make my application work. Can you tell me what is exact DLLs that my application needs? – Mustafa Chelik Sep 16 '16 at 00:28
  • You don't put "dlls" in `PATH`, you *add* the `bin` subfolder of OpenSSL's install prefix *to* the `PATH`. So, suppose you built OpenSSL 1.0.2h (**newest** 1.0.2 you can get - all 1.0.2 are binary compatible), and set the prefix as `C:\OpenSSL`. Then, add `C:\OpenSSL\bin` to `PATH` or `libraryPaths`. – Kuba hasn't forgotten Monica Sep 16 '16 at 12:27
  • 1
    The problem was i was putting 32-bit `libeay32.dll` and `ssleay32.dll` files beside my application. When I installed Win64-OpenSSL and copied mentioned DLLs beside my application, it worked. Thank you. – Mustafa Chelik Sep 17 '16 at 23:54