Ejemplo n.º 1
0
bool ImapPrivate::connectToHost (const QString& host, quint16 port, bool useSsl)
{
#ifndef QT_NO_OPENSSL
    if (useSsl)
        socket = new QSslSocket;       
    else
        socket = new QTcpSocket;
#else
    Q_UNUSED(useSsl)
    socket = new QTcpSocket;
#endif

#ifndef QT_NO_OPENSSL
    if (useSsl) {
        QSslSocket *sslSocket = static_cast<QSslSocket *>(socket);
        sslSocket->connectToHostEncrypted(host, port);
        return(sslSocket->waitForEncrypted());
    } else {
        socket->connectToHost(host, port);
        return(socket->waitForConnected());
    }
#else
    socket->connectToHost(host, port);
    return(socket->waitForConnected());
#endif
}
Ejemplo n.º 2
0
void ConnectDialog::connectToServer(void)
{
    delete m_client;
    m_client = 0;
    m_ui->m_labelStatus->setText("Verbindungsaufbau ...");

    /* SelfSignedCertificate akzeptieren */
    QList<QSslError> expectedSslErrors;
    QSslCertificate cert = QSslCertificate::fromPath("cacert.pem").value(0);
    expectedSslErrors.append(QSslError(QSslError::SelfSignedCertificate, cert));

    /* Neue Verbindung aufbauen */
    QSslSocket* socket = new QSslSocket;
    socket->addCaCertificate(cert);
    socket->ignoreSslErrors(expectedSslErrors);
    socket->connectToHostEncrypted(m_ui->m_lineAddress->text(), m_ui->m_spinPort->value());

    /* Warte bis Verbindung steht */
    if (!socket->waitForEncrypted(10000))
    {
        qDebug() << socket->errorString();
        m_ui->m_labelStatus->setText(QString("Fehler: ").append(socket->errorString()));
        delete socket;

        return;
    }

    m_ui->m_labelStatus->setText("Verbindung erfolgreich aufgebaut.");
    m_client = new Client(socket);
    this->disconnect(m_ui->m_buttonClose, SIGNAL(clicked()), this, SLOT(reject()));
    this->connect(m_ui->m_buttonClose, SIGNAL(clicked()), this, SLOT(accept()));
}
Ejemplo n.º 3
0
bool FvUpdater::checkSslFingerPrint(QUrl urltoCheck)
{
	if(urltoCheck.scheme()!="https")
	{
		qWarning()<<tr("SSL fingerprint check: The url %1 is not a ssl connection!").arg(urltoCheck.toString());
		return false;
	}

	QSslSocket *socket = new QSslSocket(this);
	socket->connectToHostEncrypted(urltoCheck.host(), 443);
	if( !socket->waitForEncrypted(1000))	// waits until ssl emits encrypted(), max 1000msecs
	{
		qWarning()<<"SSL fingerprint check: Unable to connect SSL server: "<<socket->sslErrors();
		return false;
	}

	QSslCertificate cert = socket->peerCertificate();

	if(cert.isNull())
	{
		qWarning()<<"SSL fingerprint check: Unable to retrieve SSL server certificate.";
		return false;
	}

	// COmpare digests
	if(cert.digest().toHex() != m_requiredSslFingerprint)
	{
		qWarning()<<"SSL fingerprint check: FINGERPRINT MISMATCH! Server digest="<<cert.digest().toHex()<<", requiered ssl digest="<<m_requiredSslFingerprint;
		return false;
	}
	
	return true;
}
Ejemplo n.º 4
0
void Server::handleNewConnection(){
    QSslSocket *clientConnection = sslServer->nextPendingConnection();
    if (!clientConnection->waitForEncrypted(1000)){
        qDebug() << "Waited for 1 second for encryption handshake without success";
        return;
    }
    qDebug() << "Successfully waited for secure handshake...";
    connect(clientConnection, SIGNAL(disconnected()),
            clientConnection, SLOT(deleteLater()));

    socketList["First"] = clientConnection;
    sendString("stefdfgd");
}
Ejemplo n.º 5
0
int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    if (argc < 3) {
        QTextStream out(stdout);
        out << "Usage: " << argv[0] << " host port [options]" << endl;
        out << "The options can be one or more of the following:" << endl;
        out << "enable_empty_fragments" << endl;
        out << "disable_session_tickets" << endl;
        out << "disable_compression" << endl;
        out << "disable_sni" << endl;
        out << "enable_unsafe_reneg" << endl;
        return 1;
    }

    QString host = QString::fromLocal8Bit(argv[1]);
    int port = QString::fromLocal8Bit(argv[2]).toInt();

    QSslConfiguration config = QSslConfiguration::defaultConfiguration();

    for (int i=3; i < argc; i++) {
        QString option = QString::fromLocal8Bit(argv[i]);

        if (option == QStringLiteral("enable_empty_fragments"))
            config.setSslOption(QSsl::SslOptionDisableEmptyFragments, false);
        else if (option == QStringLiteral("disable_session_tickets"))
            config.setSslOption(QSsl::SslOptionDisableSessionTickets, true);
        else if (option == QStringLiteral("disable_compression"))
            config.setSslOption(QSsl::SslOptionDisableCompression, true);
        else if (option == QStringLiteral("disable_sni"))
            config.setSslOption(QSsl::SslOptionDisableServerNameIndication, true);
        else if (option == QStringLiteral("enable_unsafe_reneg"))
            config.setSslOption(QSsl::SslOptionDisableLegacyRenegotiation, false);
    }

    QSslConfiguration::setDefaultConfiguration(config);

    QSslSocket socket;
    //socket.setSslConfiguration(config);
    socket.connectToHostEncrypted(host, port);

    if ( !socket.waitForEncrypted() ) {
        qDebug() << socket.errorString();
        return 1;
    }

    return 0;
}
Ejemplo n.º 6
0
void Server::sendFortune()
{
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);
    qDebug() << "In sendFortune";
    out << (quint16)0;
    out << fortunes.at(qrand() % fortunes.size());
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

    QSslSocket *clientConnection = sslServer->nextPendingConnection();
    if (!clientConnection->waitForEncrypted(1000)){
        qDebug() << "Waited for 1 second for encryption handshake without success";
        return;
    }
    qDebug() << "Successfully waited for secure handshake...";
    connect(clientConnection, SIGNAL(disconnected()),
            clientConnection, SLOT(deleteLater()));
    clientConnection->write(block);
    clientConnection->disconnectFromHost();
}
Ejemplo n.º 7
0
void WebSocketWorker::SetupSocket()
{
    if (m_connectionType == kSSLServer)
    {

#ifndef QT_NO_OPENSSL
        QSslSocket *pSslSocket = new QSslSocket();
        if (pSslSocket->setSocketDescriptor(m_socketFD)
           && gCoreContext->CheckSubnet(pSslSocket))
        {
            pSslSocket->setSslConfiguration(m_sslConfig);
            pSslSocket->startServerEncryption();
            if (pSslSocket->waitForEncrypted(5000))
            {
                LOG(VB_HTTP, LOG_INFO, "SSL Handshake occurred, connection encrypted");
                LOG(VB_HTTP, LOG_INFO, QString("Using %1 cipher").arg(pSslSocket->sessionCipher().name()));
            }
            else
            {
                LOG(VB_HTTP, LOG_WARNING, "SSL Handshake FAILED, connection terminated");
                delete pSslSocket;
                pSslSocket = nullptr;
            }
        }
        else
        {
            delete pSslSocket;
            pSslSocket = nullptr;
        }

        if (pSslSocket)
            m_socket = dynamic_cast<QTcpSocket *>(pSslSocket);
        else
            return;
#else
        return;
#endif
    }
    else // Plain old unencrypted socket
    {
        m_socket = new QTcpSocket();
        m_socket->setSocketDescriptor(m_socketFD);
        if (!gCoreContext->CheckSubnet(m_socket))
        {
            delete m_socket;
            m_socket = nullptr;
            return;
        }

    }

    m_socket->setSocketOption(QAbstractSocket::KeepAliveOption, QVariant(1));

    connect(m_socket, SIGNAL(readyRead()), SLOT(doRead()));
    connect(m_socket, SIGNAL(disconnected()), SLOT(CloseConnection()));

    // Setup heartbeat
    m_heartBeat->setInterval(20000); // 20 second
    m_heartBeat->setSingleShot(false);
    connect(m_heartBeat, SIGNAL(timeout()), SLOT(SendHeartBeat()));
}
Ejemplo n.º 8
0
bool SenderPrivate::connectToHost()
{
    Q_Q(Sender);

    QSslSocket *sslSock = nullptr;
    switch (connectionType) {
    case Sender::TlsConnection:
    case Sender::TcpConnection:
        qCDebug(SIMPLEMAIL_SENDER) << "Connecting to host" << host << port;
        socket->connectToHost(host, port);
        break;
    case Sender::SslConnection:
    {
        sslSock = qobject_cast<QSslSocket*>(socket);
        if (sslSock) {
            qCDebug(SIMPLEMAIL_SENDER) << "Connecting to host encrypted" << host << port;
            sslSock->connectToHostEncrypted(host, port);
        } else {
            return false;
        }
    }
        break;
    }

    // Tries to connect to server
    if (!socket->waitForConnected(connectionTimeout)) {
        lastError = socket->errorString();
        qCDebug(SIMPLEMAIL_SENDER) << "Connection failed" << socket->errorString();
        Q_EMIT q->smtpError(Sender::ConnectionTimeoutError);
        return false;
    }

    // If the response code is not 220 (Service ready)
    // means that is something wrong with the server
    if (!waitForResponse(220)) {
        Q_EMIT q->smtpError(Sender::ServerError);
        return false;
    }

    qCDebug(SIMPLEMAIL_SENDER) << "Sending EHLO" << name;
    // Send a EHLO/HELO message to the server
    // The client's first command must be EHLO/HELO
    sendMessage("EHLO " + name.toLatin1());

    // The response code needs to be 250.
    if (!waitForResponse(250)) {
        Q_EMIT q->smtpError(Sender::ServerError);
        return false;
    }
    qCDebug(SIMPLEMAIL_SENDER) << "Sent hello";

    if (connectionType == Sender::TlsConnection) {
        qCDebug(SIMPLEMAIL_SENDER) << "Sending STARTTLS";

        // send a request to start TLS handshake
        sendMessage(QByteArrayLiteral("STARTTLS"));

        // The response code needs to be 220.
        if (!waitForResponse(220)) {
            Q_EMIT q->smtpError(Sender::ServerError);
            return false;
        };

        if (sslSock) {
            qCDebug(SIMPLEMAIL_SENDER) << "Starting client encryption";
            sslSock->startClientEncryption();

            if (!sslSock->waitForEncrypted(connectionTimeout)) {
                qCDebug(SIMPLEMAIL_SENDER) << "Failed to encrypt connection" << sslSock->errorString();
                Q_EMIT q->smtpError(Sender::ConnectionTimeoutError);
                return false;
            }
        }

        qCDebug(SIMPLEMAIL_SENDER) << "Sending second EHLO" << name;
        // Send ELHO one more time
        sendMessage(QByteArrayLiteral("EHLO ") + name.toLatin1());

        // The response code needs to be 250.
        if (!waitForResponse(250)) {
            Q_EMIT q->smtpError(Sender::ServerError);
            return false;
        }
    }

    state = SenderPrivate::Connected;

    // If no errors occured the function returns true.
    return true;

}
Ejemplo n.º 9
0
bool ssh::dossh()
{
#ifdef USE_QSSH
    {
        if(m_connection && m_connection->state() != QSsh::SshConnection::Unconnected)
        {
            helpers::log("ssh: already connecting...", LOG_INF, qApp, 0);
            return true;
        }

        m_connection = new QSsh::SshConnection(params, this);
        connect(m_connection, SIGNAL(connected()), SLOT(onQsshConnected()));
        connect(m_connection, SIGNAL(error(QSsh::SshError)), SLOT(onQsshConnectionError(QSsh::SshError)));
        helpers::log("ssh: connecting START...", LOG_INF, qApp, 0);
        m_connection->connectToHost();
        return false;
    }
#else
    helpers::log("ssh: START: " + QString::number(QSslSocket::supportsSsl()), QSslSocket::supportsSsl() ? LOG_INF : LOG_ERR, qApp, 0);

//http://stackoverflow.com/questions/15213139/simple-qssl-client-server-cannot-start-handshake-on-non-plain-connection

    QSslSocket *socket = new QSslSocket(this);

    socket->ignoreSslErrors();
    socket->setPeerVerifyMode(QSslSocket::VerifyNone);
    socket->setProtocol(QSsl::SslV3);

    connect(socket, SIGNAL(encrypted()), this, SLOT(ready()));
    connect(socket, SIGNAL(encryptedBytesWritten(qint64)), this, SLOT(encryptedBytesWritten(qint64)));
    connect(socket, SIGNAL(modeChanged(QSslSocket::SslMode)), this, SLOT(modeChanged(QSslSocket::SslMode)));
    connect(socket, SIGNAL(peerVerifyError(const QSslError &)), this, SLOT(peerVerifyError(const QSslError &)));
    connect(socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));

    connect(socket, SIGNAL(connected()), this, SLOT(connected()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
    connect(socket, SIGNAL(hostFound()), this, SLOT(hostFound()));
    connect(socket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)), this, SLOT(proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)));
    connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChanged(QAbstractSocket::SocketState)));
    connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));

    {
        {
              QFile file( "c:/Users/gherczeg/.ssh/id_boot2docker" );
              if( ! file.open( QIODevice::ReadOnly ) )
              {
                  QMessageBox::question(0, "Erreur", "Impossible de charger id_boot2docker");
                  return;
              }
              QSslKey key(&file);
              file.close();
              helpers::log("ssh:keyok: "+QString::number(!key.isNull()), !key.isNull() ? LOG_INF : LOG_ERR, qApp, 0);
              socket->setPrivateKey( key );
        }
        foreach (const QSslCertificate &cert, QSslCertificate::fromPath("c:/Users/gherczeg/.boot2docker/certs/boot2docker-vm/*.pem", QSsl::Pem, QRegExp::Wildcard))
        {
            helpers::log("ssh:certok1: "+QString::number(!cert.isNull()), !cert.isNull() ? LOG_INF : LOG_ERR, qApp, 0);
            socket->setLocalCertificate( cert );
            socket->sslConfiguration().caCertificates().append(cert);
            socket->addCaCertificate( cert );
            socket->addDefaultCaCertificate(cert);
        }
    }

    socket->connectToHostEncrypted("127.0.0.1", 2022);
    //socket->connectToHost("127.0.0.1", 2022);

    bool bok = socket->waitForEncrypted(100000);
    //bool bok = socket->waitForConnected(100000);
    if(!bok)
    {
        helpers::log("ssh:!waited:"+QString::number(bok),LOG_ERR, qApp, 0);
        return;
    }
    helpers::log("ssh:waited4ecnrypt/connect:"+QString::number(bok),LOG_INF, qApp, 0);
    socket->startClientEncryption();
    bool wait4Read1 = socket->waitForReadyRead(100000);
    helpers::log("ssh:wait4Read1:"+QString::number(wait4Read1),wait4Read1 ? LOG_INF : LOG_ERR, qApp, 0);
    QString s = "docker: do!";
    qint64 written = socket->write(s.toStdString().c_str());
    helpers::log("ssh:written:"+QString::number(written),written > 0 ? LOG_INF : LOG_ERR, qApp, 0);
    bool flushed = socket->flush();
    helpers::log("ssh:flush:"+QString::number(flushed),flushed ? LOG_INF : LOG_ERR, qApp, 0);
    bool wait4Write = socket->waitForBytesWritten(100000);
    helpers::log("ssh:wait4Write:"+QString::number(wait4Write),wait4Write ? LOG_INF : LOG_ERR, qApp, 0);
    bool wait4Read2 = socket->waitForReadyRead(100000);
    helpers::log("ssh:wait4Read2:"+QString::number(wait4Read2),wait4Read2 ? LOG_INF : LOG_ERR, qApp, 0);
    socket->disconnectFromHost();
#endif
}