-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgpengine.cpp
More file actions
192 lines (155 loc) · 6.68 KB
/
pgpengine.cpp
File metadata and controls
192 lines (155 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "pgpengine.h"
#include <QDateTime>
#include <QFile>
#include <QProcess>
#include <QRegularExpression>
// ── helpers ──────────────────────────────────────────────
QString PGPEngine::findGpg() {
static QString cached;
if (!cached.isEmpty()) return cached;
for (const auto &p : {"gpg2", "gpg",
"C:/Program Files (x86)/GnuPG/bin/gpg.exe",
"C:/Program Files/GnuPG/bin/gpg.exe"}) {
QProcess proc;
proc.start(QString(p), {"--version"});
if (proc.waitForFinished(3000) && proc.exitCode() == 0) {
cached = QString(p);
return cached;
}
}
cached = "gpg";
return cached;
}
PGPEngine::Result PGPEngine::runGpg(const QStringList &args, const QString &stdinData) {
QProcess proc;
QStringList full = {"--batch", "--yes", "--no-tty"};
full.append(args);
proc.start(findGpg(), full);
if (!proc.waitForStarted(5000))
return {false, {}, "Failed to start GPG. Is GnuPG installed?"};
if (!stdinData.isEmpty()) {
proc.write(stdinData.toUtf8());
proc.closeWriteChannel();
}
if (!proc.waitForFinished(60000)) {
proc.kill();
return {false, {}, "GPG timed out"};
}
QString out = QString::fromUtf8(proc.readAllStandardOutput());
QString err = QString::fromUtf8(proc.readAllStandardError());
if (proc.exitCode() != 0)
return {false, out, err.isEmpty() ? "GPG returned error" : err};
return {true, out, err};
}
// ── status ───────────────────────────────────────────────
bool PGPEngine::isGpgAvailable() {
return runGpg({"--version"}).success;
}
QString PGPEngine::gpgVersion() {
auto r = runGpg({"--version"});
if (!r.success) return "Not available";
return r.data.split('\n').value(0).trimmed();
}
// ── encrypt / decrypt ────────────────────────────────────
PGPEngine::Result PGPEngine::encrypt(const QString &plaintext, const QStringList &recipientKeyIds) {
if (recipientKeyIds.isEmpty())
return {false, {}, "No recipient specified"};
QStringList args = {"--encrypt", "--armor", "--trust-model", "always"};
for (const auto &k : recipientKeyIds)
args << "--recipient" << k;
return runGpg(args, plaintext);
}
PGPEngine::Result PGPEngine::decrypt(const QString &ciphertext) {
return runGpg({"--decrypt"}, ciphertext);
}
PGPEngine::Result PGPEngine::encryptFile(const QString &inPath, const QString &outPath,
const QStringList &recipients) {
QStringList args = {"--encrypt", "--armor", "--trust-model", "always",
"--output", outPath};
for (const auto &k : recipients)
args << "--recipient" << k;
args << inPath;
return runGpg(args);
}
PGPEngine::Result PGPEngine::decryptFile(const QString &inPath, const QString &outPath) {
return runGpg({"--decrypt", "--output", outPath, inPath});
}
// ── key listing ──────────────────────────────────────────
QList<PGPEngine::KeyInfo> PGPEngine::parseColonListing(const QString &output, const QString &keyType) {
QList<KeyInfo> keys;
KeyInfo current;
bool haveCurrent = false;
for (const auto &line : output.split('\n')) {
QStringList fields = line.split(':');
if (fields.size() < 10) continue;
const QString &recType = fields[0];
if (recType == keyType) { // "pub" or "sec"
if (haveCurrent) keys.append(current);
current = KeyInfo();
current.type = keyType;
current.bits = fields[2].toInt();
current.algorithm = fields[3];
current.keyId = fields[4];
if (!fields[5].isEmpty()) {
qint64 ts = fields[5].toLongLong();
current.created = QDateTime::fromSecsSinceEpoch(ts).toString("yyyy-MM-dd");
}
if (!fields[6].isEmpty()) {
qint64 ts = fields[6].toLongLong();
current.expires = QDateTime::fromSecsSinceEpoch(ts).toString("yyyy-MM-dd");
}
haveCurrent = true;
} else if (recType == "uid" && haveCurrent) {
if (current.userId.isEmpty())
current.userId = fields[9];
} else if (recType == "fpr" && haveCurrent) {
if (current.fingerprint.isEmpty())
current.fingerprint = fields[9];
}
}
if (haveCurrent) keys.append(current);
return keys;
}
QList<PGPEngine::KeyInfo> PGPEngine::listPublicKeys() {
auto r = runGpg({"--list-keys", "--with-colons", "--with-fingerprint"});
if (!r.success) return {};
return parseColonListing(r.data, "pub");
}
QList<PGPEngine::KeyInfo> PGPEngine::listSecretKeys() {
auto r = runGpg({"--list-secret-keys", "--with-colons", "--with-fingerprint"});
if (!r.success) return {};
return parseColonListing(r.data, "sec");
}
// ── key management ───────────────────────────────────────
PGPEngine::Result PGPEngine::importKey(const QString &keyData) {
return runGpg({"--import"}, keyData);
}
PGPEngine::Result PGPEngine::importKeyFile(const QString &filePath) {
return runGpg({"--import", filePath});
}
PGPEngine::Result PGPEngine::exportPublicKey(const QString &keyId) {
return runGpg({"--export", "--armor", keyId});
}
PGPEngine::Result PGPEngine::deleteKey(const QString &fingerprint, bool secret) {
if (secret)
return runGpg({"--delete-secret-and-public-key", fingerprint});
return runGpg({"--delete-keys", fingerprint});
}
PGPEngine::Result PGPEngine::generateKeyPair(const QString &name, const QString &email,
const QString &passphrase,
const QString &algo, int bits) {
// Use unattended key generation with parameter block
QString script;
script += "%no-protection\n";
if (!passphrase.isEmpty())
script = "Passphrase: " + passphrase + "\n";
script += "Key-Type: " + algo + "\n";
script += "Key-Length: " + QString::number(bits) + "\n";
script += "Subkey-Type: " + algo + "\n";
script += "Subkey-Length: " + QString::number(bits) + "\n";
script += "Name-Real: " + name + "\n";
script += "Name-Email: " + email + "\n";
script += "Expire-Date: 0\n";
script += "%commit\n";
return runGpg({"--gen-key"}, script);
}