Refactored certificate class (part 2) #4549

This commit is contained in:
Nick Bolton 2015-04-21 12:55:45 +01:00
parent 7e239c6f71
commit dbc5da7a48
1 changed files with 104 additions and 113 deletions

View File

@ -31,6 +31,7 @@ static const char kUnixOpenSslCommand[] = "openssl";
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
static const char kWinOpenSslBinary[] = "OpenSSL\\openssl.exe"; static const char kWinOpenSslBinary[] = "OpenSSL\\openssl.exe";
static const char kConfigFile[] = "OpenSSL\\synergy.conf";
#endif #endif
SslCertificate::SslCertificate(QObject *parent) : SslCertificate::SslCertificate(QObject *parent) :
@ -38,147 +39,137 @@ SslCertificate::SslCertificate(QObject *parent) :
{ {
m_ProfileDir = m_CoreInterface.getProfileDir(); m_ProfileDir = m_CoreInterface.getProfileDir();
if (m_ProfileDir.isEmpty()) { if (m_ProfileDir.isEmpty()) {
emit error(tr("Failed to get profile directory.")); emit error(tr("Failed to get profile directory."));
} }
} }
bool SslCertificate::checkOpenSslBinary() bool SslCertificate::runTool(const QStringList& args)
{ {
// assume OpenSsl is unavailable on Windows, QString program;
// but always available on both Mac and Linux
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
return false; program = QCoreApplication::applicationDirPath();
program.append("\\").append(kWinOpenSslBinary);
#else #else
return true; program = kUnixOpenSslCommand;
#endif #endif
}
bool SslCertificate::runProgram(
const QString& program,
const QStringList& args,
const QStringList& env)
{
QProcess process;
process.setEnvironment(env);
process.start(program, args);
bool success = process.waitForStarted(); QStringList environment;
#if defined(Q_OS_WIN)
environment << QString("OPENSSL_CONF=%1\\%2")
.arg(QCoreApplication::applicationDirPath())
.arg(kConfigFile);
#endif
QString standardError; QProcess process;
if (success && process.waitForFinished()) process.setEnvironment(environment);
{ process.start(program, args);
m_standardOutput = process.readAllStandardOutput().trimmed();
standardError = process.readAllStandardError().trimmed();
}
int code = process.exitCode(); bool success = process.waitForStarted();
if (!success || code != 0)
{
emit error(
QString("Program failed: %1\n\nCode: %2\nError: %3")
.arg(program)
.arg(process.exitCode())
.arg(standardError.isEmpty() ? "Unknown" : standardError));
return false;
}
return true; QString standardError;
if (success && process.waitForFinished())
{
m_ToolOutput = process.readAllStandardOutput().trimmed();
standardError = process.readAllStandardError().trimmed();
}
int code = process.exitCode();
if (!success || code != 0)
{
emit error(
QString("SSL tool failed: %1\n\nCode: %2\nError: %3")
.arg(program)
.arg(process.exitCode())
.arg(standardError.isEmpty() ? "Unknown" : standardError));
return false;
}
return true;
} }
void SslCertificate::generateCertificate() void SslCertificate::generateCertificate()
{ {
QString openSslProgramFile; QStringList arguments;
#if defined(Q_OS_WIN) // self signed certificate
openSslProgramFile = QCoreApplication::applicationDirPath(); arguments.append("req");
openSslProgramFile.append("\\").append(kWinOpenSslBinary); arguments.append("-x509");
#else arguments.append("-nodes");
openSslProgramFile = kUnixOpenSslCommand;
#endif
QStringList arguments; // valide duration
arguments.append("-days");
arguments.append(kCertificateLifetime);
// self signed certificate // subject information
arguments.append("req"); arguments.append("-subj");
arguments.append("-x509");
arguments.append("-nodes");
// valide duration QString subInfo(kCertificateSubjectInfo);
arguments.append("-days"); arguments.append(subInfo);
arguments.append(kCertificateLifetime);
// subject information // private key
arguments.append("-subj"); arguments.append("-newkey");
arguments.append("rsa:1024");
QString subInfo(kCertificateSubjectInfo); QString sslDirPath = QString("%1%2%3")
arguments.append(subInfo); .arg(m_ProfileDir)
.arg(QDir::separator())
.arg(kSslDir);
// private key QDir sslDir(sslDirPath);
arguments.append("-newkey"); if (!sslDir.exists()) {
arguments.append("rsa:1024"); sslDir.mkdir(".");
}
QString sslDirPath = QString("%1%2%3") QString filename = QString("%1%2%3")
.arg(m_ProfileDir) .arg(sslDirPath)
.arg(QDir::separator()) .arg(QDir::separator())
.arg(kSslDir); .arg(kCertificateFilename);
QDir sslDir(sslDirPath); // key output filename
if (!sslDir.exists()) { arguments.append("-keyout");
sslDir.mkdir("."); arguments.append(filename);
}
QString filename = QString("%1%2%3") // certificate output filename
.arg(sslDirPath) arguments.append("-out");
.arg(QDir::separator()) arguments.append(filename);
.arg(kCertificateFilename);
// key output filename if (!runTool(arguments)) {
arguments.append("-keyout"); return;
arguments.append(filename); }
// certificate output filename emit info(tr("SSL certificate generated."));
arguments.append("-out");
arguments.append(filename);
QStringList environment; generateFingerprint(filename);
#if defined(Q_OS_WIN) emit generateFinished();
environment << QString("OPENSSL_CONF=%1\\OpenSSL\\synergy.conf") }
.arg(QCoreApplication::applicationDirPath());
#endif void SslCertificate::generateFingerprint(const QString& certificateFilename)
{
if (!runProgram(openSslProgramFile, arguments, environment)) { QStringList arguments;
return; arguments.append("x509");
} arguments.append("-fingerprint");
arguments.append("-sha1");
emit info(tr("SSL certificate generated")); arguments.append("-noout");
arguments.append("-in");
// generate fingerprint arguments.append(certificateFilename);
arguments.clear();
arguments.append("x509"); if (!runTool(arguments)) {
arguments.append("-fingerprint"); return;
arguments.append("-sha1"); }
arguments.append("-noout");
arguments.append("-in"); // find the fingerprint from the tool output
arguments.append(filename); int i = m_ToolOutput.indexOf("=");
if (i != -1) {
if (!runProgram(openSslProgramFile, arguments, environment)) { i++;
return; QString fingerprint = m_ToolOutput.mid(
} i, m_ToolOutput.size() - i);
// write the standard output into file Fingerprint::local().trust(fingerprint, false);
filename.clear(); emit info(tr("SSL fingerprint generated."));
filename.append(Fingerprint::local().filePath()); }
else {
// only write the fingerprint part emit error(tr("Failed to find SSL fingerprint."));
int i = m_standardOutput.indexOf("="); }
if (i != -1) {
i++;
QString fingerprint = m_standardOutput.mid(i, m_standardOutput.size() - i);
Fingerprint::local().trust(fingerprint, false);
emit info(tr("SSL fingerprint generated"));
}
emit generateCertificateFinished();
} }