Statically compiled Qt 5.13.1 with OpenSSL 1.1.1d producing QSslSocket::connectToHostEncrypted: TLS initialization failed

前端 未结 2 1819
滥情空心
滥情空心 2021-01-26 23:04

What am I trying to achieve?

I need to create a portable (all-in-one) application, with SSL support.

What is the problem?

2条回答
  •  情深已故
    2021-01-26 23:22

    OpenSSL Thread Local Storage is predicated by the "library loaded" per Qt source code. Then you need to make sure either the library statically linked with your executable OR accessible through the path.

    if (!supportsSsl()) {
        qCWarning(lcSsl, "QSslSocket::connectToHostEncrypted: TLS initialization failed");
        d->setErrorAndEmit(QAbstractSocket::SslInternalError, tr("TLS initialization failed"));
        return;
    }
    

    and what about supportsSsl()?

    /*!
        \internal
    
        Does the minimum amount of initialization to determine whether SSL
        is supported or not.
    */
    
    bool QSslSocketPrivate::supportsSsl()
    {
        return ensureLibraryLoaded();
    }
    

    But that "library loaded" is a bit "deeper" concept per Qt source code here:

    bool QSslSocketPrivate::ensureLibraryLoaded()
    {
        if (!q_resolveOpenSslSymbols())
            return false;
    
        const QMutexLocker locker(qt_opensslInitMutex);
    
        if (!s_libraryLoaded) {
            // Initialize OpenSSL.
            if (q_OPENSSL_init_ssl(0, nullptr) != 1)
                return false;
            q_SSL_load_error_strings();
            q_OpenSSL_add_all_algorithms();
    
            QSslSocketBackendPrivate::s_indexForSSLExtraData
                = q_CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, 0L, nullptr, nullptr,
                                            nullptr, nullptr);
    
            // Initialize OpenSSL's random seed.
            if (!q_RAND_status()) {
                qWarning("Random number generator not seeded, disabling SSL support");
                return false;
            }
    
            s_libraryLoaded = true;
        }
        return true;
    }
    

    As long as this is not a remote debug session, and the answer to this question matters you need to build Qt library in DEBUG mode with symbols (or get ready to use ones for the DEBUG build) and then simply put a breakpoint in this function and find the last detail preventing your Qt application from using OpenSSL.

    The answer from the debugger should point to the reason of this "TLS initialization failed".

提交回复
热议问题