feat: paper key module is now useable

This commit is contained in:
saturneric 2024-07-28 17:01:41 +02:00
parent 55d29163da
commit b384411156
7 changed files with 117 additions and 43 deletions

View File

@ -28,10 +28,13 @@
#include "PaperKeyModule.h" #include "PaperKeyModule.h"
#include <unistd.h>
#include <QtCore> #include <QtCore>
#include "GFModuleDefine.h" #include "GFModuleDefine.h"
#include "extract.h" #include "extract.h"
#include "restore.h"
GF_MODULE_API_DEFINE("com.bktus.gpgfrontend.module.paper_key", "PaperKey", GF_MODULE_API_DEFINE("com.bktus.gpgfrontend.module.paper_key", "PaperKey",
"1.0.0", "Integrated PaperKey Functions.", "Saturneric") "1.0.0", "Integrated PaperKey Functions.", "Saturneric")
@ -52,70 +55,98 @@ EXECUTE_MODULE() {
FLOG_DEBUG("paper key module executing, event id: %1", event["event_id"]); FLOG_DEBUG("paper key module executing, event id: %1", event["event_id"]);
if (event["event_id"] == "REQUEST_TRANS_KEY_2_PAPER_KEY") { if (event["event_id"] == "REQUEST_TRANS_KEY_2_PAPER_KEY") {
if (event["secret_key"].isEmpty() || event["output_path"].isEmpty()) { if (event["secret_key"].isEmpty()) CB_ERR(event, -1, "secret key is empty");
CB_ERR(event, -1, "secret key or output path is empty");
}
QByteArray secret_key_data = QByteArray secret_key_rdata =
QByteArray::fromBase64(event["secret_key"].toUtf8()); QByteArray::fromBase64(event["secret_key"].toLatin1());
QTemporaryFile secret_key_t_file; QTemporaryFile secret_key_t_file;
if (!secret_key_t_file.open()) { if (!secret_key_t_file.open())
CB_ERR(event, -1, "unable to open temporary file"); CB_ERR(event, -1, "unable to open temporary file");
}
secret_key_t_file.write(secret_key_data); secret_key_t_file.write(secret_key_rdata);
secret_key_t_file.flush(); secret_key_t_file.flush();
secret_key_t_file.seek(0); secret_key_t_file.reset();
FILE *file = fdopen(secret_key_t_file.handle(), "rb"); FILE *secret_key_file = fdopen(secret_key_t_file.handle(), "rb");
if (file == nullptr) { if (secret_key_file == nullptr)
CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*"); CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
}
extract(file, event["output_path"].toUtf8(), AUTO); QTemporaryFile paper_key_t_file;
if (!paper_key_t_file.open())
CB_ERR(event, -1, "unable to open temporary file");
fclose(file); FILE *paper_key_fp = fdopen(dup(paper_key_t_file.handle()), "w");
} else if (event["event_id"] == "REQUEST_TRANS_PAPER_KEY_2_KEY") { if (paper_key_fp == nullptr)
if (event["public_key"].isEmpty() || event["paper_key_secrets"].isEmpty()) { CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
auto ret = extract(secret_key_file, paper_key_fp, BASE16);
paper_key_t_file.flush();
paper_key_t_file.reset();
CB(event, GFGetModuleID(),
{
{"ret", QString::number(ret)},
{"paper_key", QString::fromLatin1(paper_key_t_file.readAll())},
});
return ret;
}
if (event["event_id"] == "REQUEST_TRANS_PAPER_KEY_2_KEY") {
if (event["public_key"].isEmpty() || event["paper_key_secrets"].isEmpty())
CB_ERR(event, -1, "public key or paper key secrets is empty"); CB_ERR(event, -1, "public key or paper key secrets is empty");
}
QByteArray public_key_data = QByteArray public_key_data =
QByteArray::fromBase64(event["public_key"].toUtf8()); QByteArray::fromBase64(event["public_key"].toLatin1());
QTemporaryFile public_key_t_file; QTemporaryFile public_key_t_file;
if (!public_key_t_file.open()) { if (!public_key_t_file.open())
CB_ERR(event, -1, "unable to open temporary file"); CB_ERR(event, -1, "unable to open temporary file");
}
public_key_t_file.write(public_key_data); public_key_t_file.write(public_key_data);
public_key_t_file.flush(); public_key_t_file.flush();
public_key_t_file.seek(0); public_key_t_file.seek(0);
FILE *pubring = fdopen(public_key_t_file.handle(), "rb"); FILE *pubring = fdopen(public_key_t_file.handle(), "rb");
if (pubring == nullptr) { if (pubring == nullptr)
CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*"); CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
}
QByteArray secrets_data = QByteArray secrets_data =
QByteArray::fromBase64(event["paper_key_secrets"].toUtf8()); QByteArray::fromBase64(event["paper_key_secrets"].toLatin1());
QTemporaryFile secrets_data_file; QTemporaryFile secrets_data_t_file;
if (!secrets_data_file.open()) { if (!secrets_data_t_file.open())
CB_ERR(event, -1, "unable to open temporary file"); CB_ERR(event, -1, "unable to open temporary file");
}
secrets_data_file.write(public_key_data); secrets_data_t_file.write(secrets_data);
secrets_data_file.flush(); secrets_data_t_file.flush();
secrets_data_file.seek(0); secrets_data_t_file.reset();
FILE *secrets = fdopen(secrets_data_file.handle(), "rb"); FILE *secrets_fp = fdopen(secrets_data_t_file.handle(), "r");
if (secrets == nullptr) { if (secrets_fp == nullptr)
CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*"); CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
}
restore(pubring, secrets, AUTO, ) QTemporaryFile secret_key_t_file;
if (!secret_key_t_file.open())
CB_ERR(event, -1, "unable to open temporary file");
FILE *secret_key_fp = fdopen(dup(secret_key_t_file.handle()), "wb");
if (secret_key_fp == nullptr)
CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
auto ret = restore(pubring, secrets_fp, AUTO, secret_key_fp);
secret_key_t_file.reset();
FLOG_DEBUG("secret key temp file size: %1, ret: %2",
secret_key_t_file.size(), ret);
CB(event, GFGetModuleID(),
{
{"ret", QString::number(ret)},
{"secret_key",
QString::fromLocal8Bit(secret_key_t_file.readAll().toBase64())},
});
return ret;
} }
CB_SUCC(event); CB_SUCC(event);

View File

@ -26,7 +26,7 @@
extern int verbose; extern int verbose;
int extract(FILE *input, const char *outname, enum data_type output_type) { int extract(FILE *input, FILE *output, enum data_type output_type) {
struct packet *packet; struct packet *packet;
int offset; int offset;
unsigned char fingerprint[20]; unsigned char fingerprint[20];
@ -51,7 +51,7 @@ int extract(FILE *input, const char *outname, enum data_type output_type) {
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
output_start(outname, output_type, fingerprint); output_start(output, output_type, fingerprint);
output_bytes(&version, 1); output_bytes(&version, 1);
output_bytes(packet->buf, 1); output_bytes(packet->buf, 1);
output_bytes(fingerprint, 20); output_bytes(fingerprint, 20);
@ -83,6 +83,7 @@ int extract(FILE *input, const char *outname, enum data_type output_type) {
} }
output_finish(); output_finish();
output_end();
if (input == stdin) { if (input == stdin) {
/* Consume everything else on input */ /* Consume everything else on input */

View File

@ -22,5 +22,4 @@
#include "output.h" #include "output.h"
auto extract(FILE *input, const char *outname, auto extract(FILE *input, FILE *output, enum data_type output_type) -> int;
enum data_type output_type) -> int;

View File

@ -147,6 +147,42 @@ void output_file_format(FILE *stream, const char *prefix) {
fprintf(stream, "%smay simply be copied from the public key.\n", prefix); fprintf(stream, "%smay simply be copied from the public key.\n", prefix);
} }
int output_start(FILE *fp, enum data_type type, unsigned char fingerprint[20]) {
output = fp;
if (!output) return -1;
output_type = type;
switch (type) {
case RAW:
break;
case AUTO:
case BASE16: {
time_t now = time(NULL);
line_items = (output_width - 5 - 6) / 3;
fprintf(output, "# Secret portions of key ");
print_bytes(output, fingerprint, 20);
fprintf(output, "\n");
fprintf(output, "# Base16 data extracted %.24s\n", ctime(&now));
fprintf(output,
"# Created with "
"Paper Key Module of GpgFrontend"
" by Saturneric\n#\n");
output_file_format(output, "# ");
fprintf(output,
"#\n# Each base16 line ends with a CRC-24 of that line.\n");
fprintf(output,
"# The entire block of data ends with a CRC-24 of the entire "
"block of data.\n\n");
// if (comment != nullptr) fprintf(output, "# %s\n\n", comment);
} break;
}
return 0;
}
int output_start(const char *name, enum data_type type, int output_start(const char *name, enum data_type type,
unsigned char fingerprint[20]) { unsigned char fingerprint[20]) {
if (name) { if (name) {
@ -286,9 +322,13 @@ ssize_t output_openpgp_header(unsigned char tag, size_t length) {
return output_bytes(encoded, bytes); return output_bytes(encoded, bytes);
} }
void output_finish(void) { void output_finish(void) { output_bytes(nullptr, 0); }
output_bytes(nullptr, 0);
if (output != nullptr && output != stdout) fclose(output); void output_end() {
if (output != nullptr) {
fflush(output);
fclose(output);
}
} }
void set_binary_mode(FILE *stream) { void set_binary_mode(FILE *stream) {

View File

@ -29,9 +29,11 @@ void print_bytes(FILE *stream, const unsigned char *buf, size_t length);
void output_file_format(FILE *stream, const char *prefix); void output_file_format(FILE *stream, const char *prefix);
int output_start(const char *name, enum data_type type, int output_start(const char *name, enum data_type type,
unsigned char fingerprint[20]); unsigned char fingerprint[20]);
int output_start(FILE *fp, enum data_type type, unsigned char fingerprint[20]);
ssize_t output_bytes(const unsigned char *buf, size_t length); ssize_t output_bytes(const unsigned char *buf, size_t length);
#define output_packet(_packet) output_bytes((_packet)->buf, (_packet)->len) #define output_packet(_packet) output_bytes((_packet)->buf, (_packet)->len)
ssize_t output_length16(size_t length); ssize_t output_length16(size_t length);
ssize_t output_openpgp_header(unsigned char tag, size_t length); ssize_t output_openpgp_header(unsigned char tag, size_t length);
void output_finish(void); void output_finish(void);
void set_binary_mode(FILE *stream); void set_binary_mode(FILE *stream);
void output_end();

View File

@ -98,7 +98,7 @@ static void free_keys(struct key *key) {
} }
auto restore(FILE *pubring, FILE *secrets, enum data_type input_type, auto restore(FILE *pubring, FILE *secrets, enum data_type input_type,
const char *outname) -> int { FILE *output) -> int {
struct packet *secret; struct packet *secret;
if (input_type == AUTO) { if (input_type == AUTO) {
@ -128,7 +128,7 @@ auto restore(FILE *pubring, FILE *secrets, enum data_type input_type,
keys = extract_keys(secret); keys = extract_keys(secret);
if (keys) { if (keys) {
output_start(outname, RAW, NULL); output_start(output, RAW, NULL);
while ((pubkey = parse(pubring, 0, 0))) { while ((pubkey = parse(pubring, 0, 0))) {
unsigned char ptag; unsigned char ptag;
@ -168,6 +168,7 @@ auto restore(FILE *pubring, FILE *secrets, enum data_type input_type,
} }
free_keys(keys); free_keys(keys);
output_end();
} else { } else {
fprintf(stderr, "Unable to parse secret data\n"); fprintf(stderr, "Unable to parse secret data\n");
return 1; return 1;

View File

@ -22,6 +22,6 @@
#include "output.h" #include "output.h"
auto restore(FILE *pubring, FILE *secrets, enum data_type input_type, auto restore(FILE *pubring, FILE *secrets, enum data_type input_type,
const char *outname) -> int; FILE *output) -> int;
#endif /* !_RESTORE_H_ */ #endif /* !_RESTORE_H_ */