Refactored certificate class (part 2) #4549
This commit is contained in:
parent
7e239c6f71
commit
dbc5da7a48
|
@ -31,6 +31,7 @@ static const char kUnixOpenSslCommand[] = "openssl";
|
|||
|
||||
#if defined(Q_OS_WIN)
|
||||
static const char kWinOpenSslBinary[] = "OpenSSL\\openssl.exe";
|
||||
static const char kConfigFile[] = "OpenSSL\\synergy.conf";
|
||||
#endif
|
||||
|
||||
SslCertificate::SslCertificate(QObject *parent) :
|
||||
|
@ -42,24 +43,26 @@ SslCertificate::SslCertificate(QObject *parent) :
|
|||
}
|
||||
}
|
||||
|
||||
bool SslCertificate::checkOpenSslBinary()
|
||||
bool SslCertificate::runTool(const QStringList& args)
|
||||
{
|
||||
// assume OpenSsl is unavailable on Windows,
|
||||
// but always available on both Mac and Linux
|
||||
QString program;
|
||||
#if defined(Q_OS_WIN)
|
||||
return false;
|
||||
program = QCoreApplication::applicationDirPath();
|
||||
program.append("\\").append(kWinOpenSslBinary);
|
||||
#else
|
||||
return true;
|
||||
program = kUnixOpenSslCommand;
|
||||
#endif
|
||||
|
||||
|
||||
QStringList environment;
|
||||
#if defined(Q_OS_WIN)
|
||||
environment << QString("OPENSSL_CONF=%1\\%2")
|
||||
.arg(QCoreApplication::applicationDirPath())
|
||||
.arg(kConfigFile);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool SslCertificate::runProgram(
|
||||
const QString& program,
|
||||
const QStringList& args,
|
||||
const QStringList& env)
|
||||
{
|
||||
QProcess process;
|
||||
process.setEnvironment(env);
|
||||
process.setEnvironment(environment);
|
||||
process.start(program, args);
|
||||
|
||||
bool success = process.waitForStarted();
|
||||
|
@ -67,7 +70,7 @@ bool SslCertificate::runProgram(
|
|||
QString standardError;
|
||||
if (success && process.waitForFinished())
|
||||
{
|
||||
m_standardOutput = process.readAllStandardOutput().trimmed();
|
||||
m_ToolOutput = process.readAllStandardOutput().trimmed();
|
||||
standardError = process.readAllStandardError().trimmed();
|
||||
}
|
||||
|
||||
|
@ -75,7 +78,7 @@ bool SslCertificate::runProgram(
|
|||
if (!success || code != 0)
|
||||
{
|
||||
emit error(
|
||||
QString("Program failed: %1\n\nCode: %2\nError: %3")
|
||||
QString("SSL tool failed: %1\n\nCode: %2\nError: %3")
|
||||
.arg(program)
|
||||
.arg(process.exitCode())
|
||||
.arg(standardError.isEmpty() ? "Unknown" : standardError));
|
||||
|
@ -87,15 +90,6 @@ bool SslCertificate::runProgram(
|
|||
|
||||
void SslCertificate::generateCertificate()
|
||||
{
|
||||
QString openSslProgramFile;
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
openSslProgramFile = QCoreApplication::applicationDirPath();
|
||||
openSslProgramFile.append("\\").append(kWinOpenSslBinary);
|
||||
#else
|
||||
openSslProgramFile = kUnixOpenSslCommand;
|
||||
#endif
|
||||
|
||||
QStringList arguments;
|
||||
|
||||
// self signed certificate
|
||||
|
@ -140,45 +134,42 @@ void SslCertificate::generateCertificate()
|
|||
arguments.append("-out");
|
||||
arguments.append(filename);
|
||||
|
||||
QStringList environment;
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
environment << QString("OPENSSL_CONF=%1\\OpenSSL\\synergy.conf")
|
||||
.arg(QCoreApplication::applicationDirPath());
|
||||
#endif
|
||||
|
||||
if (!runProgram(openSslProgramFile, arguments, environment)) {
|
||||
if (!runTool(arguments)) {
|
||||
return;
|
||||
}
|
||||
|
||||
emit info(tr("SSL certificate generated"));
|
||||
emit info(tr("SSL certificate generated."));
|
||||
|
||||
// generate fingerprint
|
||||
arguments.clear();
|
||||
generateFingerprint(filename);
|
||||
|
||||
emit generateFinished();
|
||||
}
|
||||
|
||||
void SslCertificate::generateFingerprint(const QString& certificateFilename)
|
||||
{
|
||||
QStringList arguments;
|
||||
arguments.append("x509");
|
||||
arguments.append("-fingerprint");
|
||||
arguments.append("-sha1");
|
||||
arguments.append("-noout");
|
||||
arguments.append("-in");
|
||||
arguments.append(filename);
|
||||
arguments.append(certificateFilename);
|
||||
|
||||
if (!runProgram(openSslProgramFile, arguments, environment)) {
|
||||
if (!runTool(arguments)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// write the standard output into file
|
||||
filename.clear();
|
||||
filename.append(Fingerprint::local().filePath());
|
||||
|
||||
// only write the fingerprint part
|
||||
int i = m_standardOutput.indexOf("=");
|
||||
// find the fingerprint from the tool output
|
||||
int i = m_ToolOutput.indexOf("=");
|
||||
if (i != -1) {
|
||||
i++;
|
||||
QString fingerprint = m_standardOutput.mid(i, m_standardOutput.size() - i);
|
||||
QString fingerprint = m_ToolOutput.mid(
|
||||
i, m_ToolOutput.size() - i);
|
||||
|
||||
Fingerprint::local().trust(fingerprint, false);
|
||||
emit info(tr("SSL fingerprint generated"));
|
||||
emit info(tr("SSL fingerprint generated."));
|
||||
}
|
||||
else {
|
||||
emit error(tr("Failed to find SSL fingerprint."));
|
||||
}
|
||||
|
||||
emit generateCertificateFinished();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue