Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-56946

Failed to connect using client certificate

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: P2: Important P2: Important
    • None
    • 5.7.0
    • Network: SSL
    • None

      Hello!
      I'm using Qt 5.7 on Windows and compile a project using Qt Creator (qmake/mingw).

      When I try to connect to https://api.development.push.apple.com to send push requests using client certificate I got the error "Connection closed".

      I tested it on Chrome and IE and they connected successfully.

      I use QSslCertificate::importPkcs12 function to read certificate.
      Here is my code:

      if (QSslCertificate::importPkcs12(&cert,
                                           &_certKey,
                                           &_cert,
                                           &_certs_chain,
                                           certPassPhrase.toLocal8Bit())) {
      QSslConfiguration sslConfig = QSslConfiguration::defaultConfiguration();
      
      QList<QSslCertificate> certs      = sslConfig.caCertificates();
      QList<QSslCertificate> localCerts = sslConfig.localCertificateChain();
      localCerts.append(_cert);
      certs.append(_certs_chain);
      
      sslConfig.setLocalCertificateChain(localCerts);
      sslConfig.setCaCertificates(certs);
      sslConfig.setPrivateKey(_certKey);
      sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
      
      tools::http::HttpClient client(false, sslConfig);
      
      auto requestJson = message->Serialize()->Stringify();
      auto request     = vector<uint8_t>(requestJson.begin(), requestJson.end());
      client.AddHeader("Content-Type", "application/json");
      
      ret = client.Post(("https://api.development.push.apple.com/3/device/" + deviceToken).toStdString(),
                            request,
                            response,
                            cancelState);
        }
      
      HttpClient
      ...
        QNetworkRequest netRequest;
      
        QNetworkProxyFactory::setUseSystemConfiguration(true);
        netRequest.setSslConfiguration(_sslConf);
        netRequest.setUrl(QUrl(url.c_str()));
      
      ...
        _lastReply = _manager.sendCustomRequest(
          netRequest,
          QByteArray(reinterpret_cast<const char *>(verb.data())), &dataBuffer);
      
        // TODO: repair several use
        QTimer timer;
        QEventLoop loop;
        QObject::connect(&timer,     SIGNAL(timeout()),  &loop, SLOT(quit()));
        QObject::connect(_lastReply, SIGNAL(finished()), &loop, SLOT(quit()));
        QObject::connect(_lastReply, &QNetworkReply::sslErrors,
                         [ = ](QList<QSslError>errorList) {
              for (auto & error : errorList) {
                Logger::Error("QSslError: %s", error.errorString().toStdString().c_str());
                throw NetworkException(error.errorString().toStdString(), NetworkException::ServerError);
              }
            });
      
        do {
          timer.start(500);
          loop.exec();
      
          // check abandom
          if ((cancelState != nullptr) && cancelState->load()) {
            throw NetworkException(
                    "Network operation was cancelled by user",
                    NetworkException::CancelledByUser);
          }
        } while (!timer.isActive() || !_lastReply->isFinished());
        QVariant statusCode       = _lastReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
      

      So 'statusCode' is undefined here. Also there is no SSL Error in request.

      _lastReply->errorString() returns "Connection closed"

        1. TestApp.zip
          3 kB
          Andrey Pogoreltsev
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            tpochep Timur Pocheptsov
            agentrx Andrey Pogoreltsev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes