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)
|
#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) :
|
||||||
|
@ -42,24 +43,26 @@ SslCertificate::SslCertificate(QObject *parent) :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
|
QStringList environment;
|
||||||
|
#if defined(Q_OS_WIN)
|
||||||
|
environment << QString("OPENSSL_CONF=%1\\%2")
|
||||||
|
.arg(QCoreApplication::applicationDirPath())
|
||||||
|
.arg(kConfigFile);
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
bool SslCertificate::runProgram(
|
|
||||||
const QString& program,
|
|
||||||
const QStringList& args,
|
|
||||||
const QStringList& env)
|
|
||||||
{
|
|
||||||
QProcess process;
|
QProcess process;
|
||||||
process.setEnvironment(env);
|
process.setEnvironment(environment);
|
||||||
process.start(program, args);
|
process.start(program, args);
|
||||||
|
|
||||||
bool success = process.waitForStarted();
|
bool success = process.waitForStarted();
|
||||||
|
@ -67,7 +70,7 @@ bool SslCertificate::runProgram(
|
||||||
QString standardError;
|
QString standardError;
|
||||||
if (success && process.waitForFinished())
|
if (success && process.waitForFinished())
|
||||||
{
|
{
|
||||||
m_standardOutput = process.readAllStandardOutput().trimmed();
|
m_ToolOutput = process.readAllStandardOutput().trimmed();
|
||||||
standardError = process.readAllStandardError().trimmed();
|
standardError = process.readAllStandardError().trimmed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +78,7 @@ bool SslCertificate::runProgram(
|
||||||
if (!success || code != 0)
|
if (!success || code != 0)
|
||||||
{
|
{
|
||||||
emit error(
|
emit error(
|
||||||
QString("Program failed: %1\n\nCode: %2\nError: %3")
|
QString("SSL tool failed: %1\n\nCode: %2\nError: %3")
|
||||||
.arg(program)
|
.arg(program)
|
||||||
.arg(process.exitCode())
|
.arg(process.exitCode())
|
||||||
.arg(standardError.isEmpty() ? "Unknown" : standardError));
|
.arg(standardError.isEmpty() ? "Unknown" : standardError));
|
||||||
|
@ -87,15 +90,6 @@ bool SslCertificate::runProgram(
|
||||||
|
|
||||||
void SslCertificate::generateCertificate()
|
void SslCertificate::generateCertificate()
|
||||||
{
|
{
|
||||||
QString openSslProgramFile;
|
|
||||||
|
|
||||||
#if defined(Q_OS_WIN)
|
|
||||||
openSslProgramFile = QCoreApplication::applicationDirPath();
|
|
||||||
openSslProgramFile.append("\\").append(kWinOpenSslBinary);
|
|
||||||
#else
|
|
||||||
openSslProgramFile = kUnixOpenSslCommand;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
QStringList arguments;
|
QStringList arguments;
|
||||||
|
|
||||||
// self signed certificate
|
// self signed certificate
|
||||||
|
@ -140,45 +134,42 @@ void SslCertificate::generateCertificate()
|
||||||
arguments.append("-out");
|
arguments.append("-out");
|
||||||
arguments.append(filename);
|
arguments.append(filename);
|
||||||
|
|
||||||
QStringList environment;
|
if (!runTool(arguments)) {
|
||||||
|
|
||||||
#if defined(Q_OS_WIN)
|
|
||||||
environment << QString("OPENSSL_CONF=%1\\OpenSSL\\synergy.conf")
|
|
||||||
.arg(QCoreApplication::applicationDirPath());
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!runProgram(openSslProgramFile, arguments, environment)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
emit info(tr("SSL certificate generated"));
|
emit info(tr("SSL certificate generated."));
|
||||||
|
|
||||||
// generate fingerprint
|
generateFingerprint(filename);
|
||||||
arguments.clear();
|
|
||||||
|
emit generateFinished();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SslCertificate::generateFingerprint(const QString& certificateFilename)
|
||||||
|
{
|
||||||
|
QStringList arguments;
|
||||||
arguments.append("x509");
|
arguments.append("x509");
|
||||||
arguments.append("-fingerprint");
|
arguments.append("-fingerprint");
|
||||||
arguments.append("-sha1");
|
arguments.append("-sha1");
|
||||||
arguments.append("-noout");
|
arguments.append("-noout");
|
||||||
arguments.append("-in");
|
arguments.append("-in");
|
||||||
arguments.append(filename);
|
arguments.append(certificateFilename);
|
||||||
|
|
||||||
if (!runProgram(openSslProgramFile, arguments, environment)) {
|
if (!runTool(arguments)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the standard output into file
|
// find the fingerprint from the tool output
|
||||||
filename.clear();
|
int i = m_ToolOutput.indexOf("=");
|
||||||
filename.append(Fingerprint::local().filePath());
|
|
||||||
|
|
||||||
// only write the fingerprint part
|
|
||||||
int i = m_standardOutput.indexOf("=");
|
|
||||||
if (i != -1) {
|
if (i != -1) {
|
||||||
i++;
|
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);
|
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