Esempio n. 1
0
int main(int argc, char *argv[])
{
	QApplication::setStyle("plastique");

	QApplication app(argc, argv);

	QApplication::setWindowIcon( QIcon( ":/megatendb.ico") );

	QTranslator translator;
	translator.load( QString("megatendb_%1").arg(settings->lang()) );
	app.installTranslator(&translator);

	QFile paletteFile(":/dark.xml");
	paletteFile.open(QIODevice::ReadOnly);
	QPalette palette = PaletteEditor::importPalette( paletteFile.readAll() );
	paletteFile.close();

	app.setPalette(palette);

	QFile cert_file(":/ca.crt");
    cert_file.open(QIODevice::ReadOnly);

	QSslCertificate cert(&cert_file);
	QSslSocket::addDefaultCaCertificate(cert);

	if( settings->email().isEmpty() )
		(new Register)->show();
	else
		(new Taskbar)->show();

	if( !app.arguments().contains("--no-check") )
		VersionCheck::getSingletonPtr();

	return app.exec();
}
Esempio n. 2
0
SslServer::SslServer(QObject *parent) : QObject(parent)
{
    qRegisterMetaType<PeerData>("PeerData");

    m_max_connections = 0;
    m_server = nullptr;

    QByteArray password;

    if (!QFileInfo("../data/cert.pem").exists() || !QFileInfo("../data/key.pem").exists())
    {
        QByteArray confirm_password;

        password = getPassword("Enter password to encrypt private key:");

        confirm_password = getPassword("Repeat password to encrypt private key:");

        printf("\n");
        fflush(stdout);

        if (password != confirm_password)
        {
            password.fill(char(0));
            confirm_password.fill(char(0));

            DEBUG_FUNCTION("Different passwords!");
            return;
        }

        confirm_password.fill(char(0));

        DEBUG_FUNCTION("Generating certificate...");

        Certificate certificate_genarator;

        bool success = certificate_genarator.generate("US", "Server", "127.0.0.1", password);

        if (!success)
        {
            DEBUG_FUNCTION("Keys not generated!" << "Error(s):" << qPrintable(QString("\n%0").arg(certificate_genarator.errorString())));
            return;
        }

        DEBUG_FUNCTION("Keys generated!");
    }

    QFile key_file("../data/key.pem");

    if (!key_file.open(QFile::ReadOnly))
    {
        DEBUG_FUNCTION("Private key file could not be openned!");
        return;
    }

    QFile cert_file("../data/cert.pem");

    if (!cert_file.open(QFile::ReadOnly))
    {
        DEBUG_FUNCTION("Public key file could not be openned!");
        return;
    }

    if (password.isNull())
        password = getPassword("Enter password to decrypt private key:");

    printf("\n");
    fflush(stdout);

    m_key = QSslKey(&key_file, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, password);

    password.fill(char(0));

    if (m_key.isNull())
    {
        DEBUG_FUNCTION("Private key is null!");
        return;
    }

    m_cert = QSslCertificate(&cert_file, QSsl::Pem);

    if (m_cert.isNull())
    {
        DEBUG_FUNCTION("Public key is null!");
        return;
    }

    QByteArray hash = m_cert.digest(QCryptographicHash::Sha256).toHex().toUpper();

    QByteArray formatted_hash;

    for (int i = hash.size() - 2; i >= 0; i -= 2)
    {
        formatted_hash.prepend(hash.mid(i, 2));
        formatted_hash.prepend(":");
    }

    formatted_hash.remove(0, 1);

    DEBUG_FUNCTION("Certificate fingerprint:" << qPrintable(formatted_hash));

    key_file.close();
    cert_file.close();

    DEBUG_FUNCTION("Openning database...");

    if (!m_sql.open())
    {
        DEBUG_FUNCTION("Could not open database!");
        return;
    }

    DEBUG_FUNCTION("Database openned!");

    listen();
}