Skip to content

Commit

Permalink
itemencrypted: Fix handling native/non-native key paths
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Sep 20, 2023
1 parent b8f31c4 commit d2e3591
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
34 changes: 20 additions & 14 deletions plugins/itemencrypted/itemencrypted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,19 @@ class GpgExecutable {
const bool needsSecring = version.major == 2 && version.minor == 0;

const QString path = getConfigurationFilePath("");
ensureSettingsDirectoryExists();
m_pubring = QDir::toNativeSeparators(path + ".pub");
if (needsSecring)
m_secring = QDir::toNativeSeparators(path + ".sec");
m_pubring = path + ".pub";
m_pubringNative = QDir::toNativeSeparators(m_pubring);
if (needsSecring) {
m_secring = path + ".sec";
m_secringNative = QDir::toNativeSeparators(m_secring);
}

#ifdef Q_OS_WIN
const bool isUnixGpg = versionOutput.contains("Home: /c/");
if (isUnixGpg) {
m_pubring = QDir::fromNativeSeparators(m_pubring).replace(":", "").insert(0, '/');
m_pubringNative = QString(m_pubring).replace(":", "").insert(0, '/');
if (needsSecring)
m_secring = QDir::fromNativeSeparators(m_secring).replace(":", "").insert(0, '/');
m_secringNative = QString(m_secring).replace(":", "").insert(0, '/');
}
#endif
}
Expand All @@ -160,11 +162,15 @@ class GpgExecutable {
bool needsSecring() const { return !m_secring.isEmpty(); }
const QString &pubring() const { return m_pubring; }
const QString &secring() const { return m_secring; }
const QString &pubringNative() const { return m_pubringNative; }
const QString &secringNative() const { return m_secringNative; }

private:
QString m_executable;
QString m_pubring;
QString m_secring;
QString m_pubringNative;
QString m_secringNative;
bool m_isSupported = false;
};

Expand Down Expand Up @@ -195,7 +201,7 @@ QStringList getDefaultEncryptCommandArguments(const QString &publicKeyPath)
void startGpgProcess(QProcess *p, const QStringList &args, QIODevice::OpenModeFlag mode)
{
const auto &gpg = gpgExecutable();
p->start(gpg.executable(), getDefaultEncryptCommandArguments(gpg.pubring()) + args, mode);
p->start(gpg.executable(), getDefaultEncryptCommandArguments(gpg.pubringNative()) + args, mode);
}

QString importGpgKey()
Expand All @@ -205,7 +211,7 @@ QString importGpgKey()
return QString();

QProcess p;
p.start(gpg.executable(), getDefaultEncryptCommandArguments(gpg.pubring()) << "--import" << gpg.secring());
p.start(gpg.executable(), getDefaultEncryptCommandArguments(gpg.pubringNative()) << "--import" << gpg.secringNative());
if ( !verifyProcess(&p) )
return "Failed to import private key (see log).";

Expand All @@ -223,7 +229,7 @@ QString exportGpgKey()
return QString();

QProcess p;
p.start(gpg.executable(), getDefaultEncryptCommandArguments(gpg.pubring()) << "--export-secret-key" << gpg.secring());
p.start(gpg.executable(), getDefaultEncryptCommandArguments(gpg.pubringNative()) << "--export-secret-key" << gpg.secringNative());
if ( !verifyProcess(&p) )
return "Failed to export private key (see log).";

Expand Down Expand Up @@ -311,11 +317,11 @@ void startGenerateKeysProcess(QProcess *process, bool useTransientPasswordlessKe
"\nKey-Length: 4096"
"\nName-Real: copyq"
+ transientOptions +
"\n%pubring " + gpg.pubring().toUtf8()
"\n%pubring " + gpg.pubringNative().toUtf8()
);

if ( gpg.needsSecring() )
process->write("\n%secring " + gpg.secring().toUtf8());
process->write("\n%secring " + gpg.secringNative().toUtf8());

process->write("\n%commit\n");
process->closeWriteChannel();
Expand Down Expand Up @@ -568,7 +574,7 @@ QString ItemEncryptedScriptable::generateTestKeys()
{
const auto &gpg = gpgExecutable();

QStringList keys = gpg.needsSecring()
const QStringList keys = gpg.needsSecring()
? QStringList{gpg.pubring(), gpg.secring()}
: QStringList{gpg.pubring()};

Expand Down Expand Up @@ -679,14 +685,14 @@ QWidget *ItemEncryptedLoader::createSettingsWidget(QWidget *parent)
"<li>%1</li>"
"<li>%2</li>"
"</ul>"
).arg(quoteString(gpg.pubring()), quoteString(gpg.secring()))
).arg(quoteString(gpg.pubringNative()), quoteString(gpg.secringNative()))
);
} else {
text.append( QStringLiteral(
"<ul>"
"<li>%1</li>"
"</ul>"
).arg(quoteString(gpg.pubring()))
).arg(quoteString(gpg.pubringNative()))
);
}
ui->labelShareInfo->setText(text);
Expand Down
6 changes: 3 additions & 3 deletions plugins/itemencrypted/tests/itemencryptedtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ void ItemEncryptedTests::cleanup()

void ItemEncryptedTests::encryptDecryptData()
{
RUN("-e" << "plugins.itemencrypted.generateTestKeys()", "\n");
RUN("plugins.itemencrypted.generateTestKeys()", "\n");

// Test gpg errors first.
RUN("-e" << "plugins.itemencrypted.encrypt(input());print('')", "");
RUN("plugins.itemencrypted.encrypt(input());print('')", "");

const QByteArray input("\x00\x01\x02\x03\x04", 5);
QByteArray stdoutActual;
Expand All @@ -59,7 +59,7 @@ void ItemEncryptedTests::encryptDecryptItems()
SKIP("Ctrl+L shortcut doesn't seem work on OS X");
#endif

RUN("-e" << "plugins.itemencrypted.generateTestKeys()", "\n");
RUN("plugins.itemencrypted.generateTestKeys()", "\n");

// Load commands from the plugin generating keys.
RUN("keys" << "Ctrl+P" << "ENTER", "");
Expand Down

0 comments on commit d2e3591

Please sign in to comment.