Esempio n. 1
1
void HTTPSManager::incomingConnection(qintptr socketDescriptor) {
    QSslSocket* sslSocket = new QSslSocket(this);

    sslSocket->setLocalCertificate(_certificate);
    sslSocket->setPrivateKey(_privateKey);

    if (sslSocket->setSocketDescriptor(socketDescriptor)) {
        new HTTPSConnection(sslSocket, this);
    } else {
        delete sslSocket;
    }
}
Esempio n. 2
1
void HttpsServer::incomingConnection(qintptr socketDescriptor)
#endif
{
    QSslSocket* sslSocket = new QSslSocket(this);
    if (sslSocket->setSocketDescriptor(socketDescriptor))
    {
        sslSocket->setPrivateKey(privateKey());
        sslSocket->setLocalCertificate(certificate());
        sslSocket->startServerEncryption();
        connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslSocket_sslErrors(QList<QSslError>)));
        connect(sslSocket, SIGNAL(encrypted()), this, SLOT(sslSocket_encrypted()));
        addPendingConnection(sslSocket);
        nextPendingConnection();
        createHttpConnection()->initialize(sslSocket, sslSocket);
    }
    else
    {
        qWarning() << "HttpsServer::incomingConnection: failed to set socket descriptor '" << socketDescriptor << "' on ssl socket.";
        delete sslSocket;
    }
}
Esempio n. 3
0
void QTlsServer::incomingConnection(int socketDescriptor)
{
	QSslSocket* serverSocket = new QSslSocket;
	QObject::connect(serverSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(displayTlsErrors(const QList<QSslError>&)));

	if (serverSocket->setSocketDescriptor(socketDescriptor))
	{
		QFile file("server-key.pem");
		if (!file.open(QIODevice::ReadOnly))
		{
			std::cout << "can't open key" << "server-key.pem";
			return;
		}
		QSslKey key(&file, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, QByteArray("qtwebsocket-server-key"));
		file.close();
		serverSocket->setPrivateKey(key);

		if (!serverSocket->addCaCertificates("ca.pem"))
		{
			std::cout << "open certificate ca error" << "ca.pem";
			return;
		}
		
		serverSocket->setLocalCertificate("server-crt.pem");
		serverSocket->setPeerVerifyMode(QSslSocket::VerifyNone);
		//serverSocket->ignoreSslErrors();

		QObject::connect(serverSocket, SIGNAL(encrypted()), this, SLOT(tlsSocketEncrypted()));
		serverSocket->startServerEncryption();
	}
	else
	{
		serverSocket->deleteLater();
	}
}
Esempio n. 4
0
QTcpSocket *Nuria::Internal::TcpServer::handleToSocket (qintptr handle) {
	if (!this->m_ssl) {
		QTcpSocket *socket = new QTcpSocket;
		socket->setSocketDescriptor (handle);
		return socket;
	}
	
	// SSL
#ifndef NURIA_NO_SSL_HTTP
	QSslSocket *socket = new QSslSocket (this);
	
	// Certificate and private key
	socket->setPrivateKey (this->m_key);
	socket->setLocalCertificate (this->m_cert);
	
	// Set handle
	if (!socket->setSocketDescriptor (handle)) {
		delete socket;
		return nullptr;
	}
	
	// 
	socket->startServerEncryption ();
	return socket;
#else	
	return nullptr;
#endif
	
}
Esempio n. 5
0
void SshServer::slot_newIncommingConnection( int socketDescriptor )
{
    QSslSocket* sslSocket = new QSslSocket();
	
	// before the handshake, we need to adjust some security parameters for SSL
    
	QSsl::SslProtocol sslProtocol;
	if( "SSL-v3" == _sshServerSettings._version )
		sslProtocol = QSsl::SslV3;
    else if( "TLS-v1" == _sshServerSettings._version )
		sslProtocol = QSsl::TlsV1;
	else
    {
		logError( this, "no valid SSL version to use" );
		delete sslSocket;        
		return;
    }
    QSsl::EncodingFormat ecodingFormat = ("PER"==_sshServerSettings._format) ? QSsl::Pem : QSsl::Der;
    QSsl::KeyAlgorithm algorithm = ("RSA"==_sshServerSettings._cipher) ? QSsl::Rsa : QSsl::Dsa;
    QByteArray password;

    // setting the SSL version to use
    sslSocket->setProtocol( sslProtocol );
    
    // ensure that the peer's certificate will be verified 
    sslSocket->setPeerVerifyMode( QSslSocket::VerifyPeer );
    
    // ensure that the peer's cerficiate and its issuer's certificate will be verified
    sslSocket->setPeerVerifyDepth( 2 );

    
    // setting server's certificate
    sslSocket->setLocalCertificate( _sshServerSettings._certificate, ecodingFormat );
    
    // setting server's private key
    sslSocket->setPrivateKey( _sshServerSettings._privateKey, algorithm, ecodingFormat, password );
    
    // setting the CA ceritificate
    QList<QSslCertificate> caCertificates = QSslCertificate::fromPath( _sshServerSettings._certificate, ecodingFormat );
    sslSocket->setDefaultCaCertificates( caCertificates );
    
    // setup some traps for the socket events
	connect( sslSocket, SIGNAL(disconnected()), sslSocket, SLOT(deleteLater()) );
	connect( sslSocket, SIGNAL(encrypted()), SLOT(slot_SuccessfulConnected()) );
	connect( sslSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(slot_UnSuccessfulConnected(const QList<QSslError>&)) );
    connect( sslSocket, SIGNAL(readyRead()), this, SLOT(slot_IncommingData()) );
    
	// start the handshake
	bool result = sslSocket->setSocketDescriptor( socketDescriptor );
    if( false == result )
    {
        logError( this, QString("failed to set socket descriptor: %1").arg(sslSocket->errorString()) );
        delete sslSocket;
        return;
    }

    sslSocket->startServerEncryption();
}
Esempio n. 6
0
void SslServer::incomingConnection(qintptr handle)
{
	QSslSocket *socket = new QSslSocket(this);
	socket->setSocketDescriptor(handle);
	socket->setLocalCertificate(_cert);
	socket->setPrivateKey(_key);

	addPendingConnection(socket);
}
Esempio n. 7
0
QAbstractSocket* SslSocketCreation::operator()() const {
	QSslSocket* socket = new QSslSocket();
	socket->setSocketDescriptor(socketDescriptor);

	socket->setLocalCertificate(certificate);
	socket->setPrivateKey(privateKey);

	socket->startServerEncryption();

	return socket;
}
Esempio n. 8
0
QAbstractSocket * HttpsSocket::createSocket(qintptr socketDescriptor)
{
    QSslSocket * socket = new QSslSocket();

    socket->setSocketDescriptor(socketDescriptor);

    socket->setLocalCertificate(m_certificate);
    socket->setPrivateKey(m_privateKey);

    socket->startServerEncryption();

    return socket;
}
QIODevice* QxtSslConnectionManager::incomingConnection(int socketDescriptor)
#endif
{
    QSslSocket* socket = new QSslSocket(this);
    if(socket->setSocketDescriptor(socketDescriptor)) {
        socket->setLocalCertificate(qxt_d().localCertificate());
        socket->setPrivateKey(qxt_d().privateKey());
        if(qxt_d().autoEncrypt()) socket->startServerEncryption();
        return socket;
    } else {
        delete socket;
        return 0;
    }
}
Esempio n. 10
0
/**
 * Called when a new connection is available. The newConnection()
 *  signal is emitted when the connection is added to the pending
 *  connections queue
 *
 * @brief SslServer::incomingConnection
 * @param socketDescriptor
 *
 * Ref: http://doc.qt.io/qt-5/qtcpserver.html#incomingConnection
 */
void SslServer::incomingConnection(qintptr socketDescriptor)
{
    QSslSocket *mSslSocket = new QSslSocket(this);
    if(mSslSocket->setSocketDescriptor(socketDescriptor))
    {
        mSslSocket->setProtocol(mProtocol);
        mSslSocket->setLocalCertificate(mLocalCertificate);
        mSslSocket->setPrivateKey(mPrivateKey);
        this->addPendingConnection(mSslSocket);
    }
    else
    {
        delete mSslSocket;
        qDebug() << "QSslSocket pointer deleted";
    }
}
Esempio n. 11
0
void SslServer::incomingConnection(int socketDescriptor)
{
    QSslSocket *serverSocket = new QSslSocket(this);
    if (serverSocket->setSocketDescriptor(socketDescriptor)) {
        if (isCertValid()) {
            serverSocket->setLocalCertificate(_cert);
            serverSocket->setPrivateKey(_key);
            serverSocket->addCaCertificates(_ca);
        }
        _pendingConnections << serverSocket;
        emit newConnection();
    }
    else {
        delete serverSocket;
    }
}
Esempio n. 12
0
// Accept connection from server and initiate the SSL handshake
void Server::acceptConnection()
{
  QSslSocket *socket = server.nextPendingConnection();
  assert(socket);

  // QSslSocket emits the encrypted() signal after the encrypted connection is established
  connect(socket, SIGNAL(encrypted()), this, SLOT(handshakeComplete()));

  // Report any SSL errors that occur
  connect(socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));

  connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionFailure()));

  socket->setPrivateKey(key);
  socket->setLocalCertificate(certificate);

  socket->setPeerVerifyMode(QSslSocket::VerifyNone);
  socket->startServerEncryption();
}
Esempio n. 13
0
void QxtSslServer::incomingConnection(int socketDescriptor)
#endif
{
    QSslSocket* socket = new QSslSocket(this);
    if(socket->setSocketDescriptor(socketDescriptor)) {
        socket->setLocalCertificate(qxt_d().localCertificate);
        socket->setPrivateKey(qxt_d().privateKey);
        if(parent()){
            connect(socket, SIGNAL(sslErrors(const QList<QSslError>&)),
                    parent(), SLOT(sslErrors(const QList<QSslError>&)));
            connect(socket, SIGNAL(peerVerifyError(const QSslError&)),
                    parent(), SLOT(peerVerifyError(const QSslError&)));
        }
        qxt_d().pendingConnections.enqueue(socket);
        // emit newConnection(); // removed: QTcpServerPrivate emits this for us
        if(qxt_d().autoEncrypt) socket->startServerEncryption();
    } else {
        delete socket;
    }
}
Esempio n. 14
0
void SslServer::newConnectionPrivate(qintptr descriptor)
{
    QSslSocket *socket = new QSslSocket(this);
    socket->setSocketDescriptor(descriptor);

    if (m_max_connections == 0)
    {
        socket->abort();
        return;
    }

    socket->setProtocol(QSsl::TlsV1_2OrLater);

    socket->addCaCertificate(m_cert);
    socket->setLocalCertificate(m_cert);
    socket->setPrivateKey(m_key);

    //New connection done, set one less available connection
    m_max_connections--;

    QByteArray m_buffer;
    qint32 size = 0;

    m_socket_list.append(socket);
    m_descriptor_hash.insert(socket, descriptor);
    m_socket_hash.insert(descriptor, socket);
    m_buffer_hash.insert(socket, m_buffer);
    m_size_hash.insert(socket, size);

    connect(socket, &QSslSocket::encrypted, this, &SslServer::encrypted);
    connect(socket, &QSslSocket::disconnected, this, &SslServer::disconnectedPrivate);
    connect(socket, static_cast<void(QSslSocket::*)(const QList<QSslError>&)>(&QSslSocket::sslErrors), this, &SslServer::sslErrors);

    m_alive_hash[socket].start();

    socket->startServerEncryption();
}
Esempio n. 15
0
void SSLServer::incomingConnection(int socketDescriptor)
{
    // On an incoming connection we want
    // to create a new secure socket.
    QSslSocket *secureSocket = new QSslSocket;

    // Add to list so that we can find it with
    // nextConnection
    m_secureSocketList.append(secureSocket);

    // We need to read in the local certificate and
    // and the private key that we generated
    // with openssl.  Read the README to see
    // how these are generated.
    secureSocket->setLocalCertificate("cacert.pem");
    secureSocket->setPrivateKey("privkey.pem");

    // check that the certificate / private key are not null
    if (secureSocket->localCertificate().isNull()) {
        qDebug() << "WARNING: The local certificate appears to be null! ";
    }
    if (secureSocket->privateKey().isNull()) {
        qDebug() << "WARNING: The private key appears to be null! ";
    }

    // debug message on success
    qDebug() << "Created the SSL socket, Read local cert. / private key files";

    // From incoming connection we obtain the socket descriptor,
    // we associate this with our new SSL socket
    secureSocket->setSocketDescriptor(socketDescriptor);

    // Begin encryption.  Note from the documentation
    // all the key stuff must be done prior to doing this.
    secureSocket->startServerEncryption();
    qDebug() << "Started encryption for new secure socket";
}
Esempio n. 16
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
}
Esempio n. 17
-1
// Accept connection from server and initiate the SSL handshake
void Server::acceptConnection()
{
	if (sockets.empty() == false)
		std::cout << "Server is mad efor 1 connection also. Need to update to handle multiple connections" << std::endl;

  QSslSocket *socket = dynamic_cast<QSslSocket *>(server.nextPendingConnection());
  assert(socket);


  // Report any SSL errors that occur
  connect(socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));

  connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionFailure()));

  
  // QSslSocket emits the encrypted() signal after the encrypted connection is established
#define _USE_ENCRYPTION
#ifdef _USE_ENCRYPTION
  connect(socket, SIGNAL(encrypted()), this, SLOT(handshakeComplete()));
  socket->setPrivateKey(key);
  socket->setLocalCertificate(certificate);

  socket->setPeerVerifyMode(QSslSocket::VerifyNone);
  socket->startServerEncryption();
#else
  connect(socket, SIGNAL(disconnected()), this, SLOT(connectionClosed()));
  connect(socket, SIGNAL(readyRead()), this, SLOT(receiveMessage()));
  sockets.push_back(socket);
  std::cout << "Accepted connection from " << socket->peerAddress().toString().toStdString() << ":" << socket->peerPort() << " .Encrypted : " << socket->isEncrypted() << std::endl;
#endif
}