feat: paper key use module log system

This commit is contained in:
saturneric 2024-09-28 14:37:53 +02:00
parent 8d7546ad5b
commit 5b5e450b8f
3 changed files with 32 additions and 32 deletions

View File

@ -20,6 +20,7 @@
#include <cstdio> #include <cstdio>
#include "GFModuleCommonUtils.hpp"
#include "output.h" #include "output.h"
#include "packets.h" #include "packets.h"
#include "parse.h" #include "parse.h"
@ -34,14 +35,14 @@ int extract(FILE *input, FILE *output, enum data_type output_type) {
packet = parse(input, 5, 0); packet = parse(input, 5, 0);
if (!packet) { if (!packet) {
fprintf(stderr, "Unable to find secret key packet\n"); LOG_ERROR("Unable to find secret key packet");
return 1; return 1;
} }
offset = extract_secrets(packet); offset = extract_secrets(packet);
if (offset == -1) return 1; if (offset == -1) return 1;
if (verbose > 1) fprintf(stderr, "Secret offset is %d\n", offset); if (verbose > 1) FLOG_DEBUG("Secret offset is %d", offset);
calculate_fingerprint(packet, offset, fingerprint); calculate_fingerprint(packet, offset, fingerprint);
@ -64,7 +65,7 @@ int extract(FILE *input, FILE *output, enum data_type output_type) {
offset = extract_secrets(packet); offset = extract_secrets(packet);
if (offset == -1) return 1; if (offset == -1) return 1;
if (verbose > 1) fprintf(stderr, "Secret subkey offset is %d\n", offset); if (verbose > 1) FLOG_DEBUG("Secret subkey offset is %d\n", offset);
calculate_fingerprint(packet, offset, fingerprint); calculate_fingerprint(packet, offset, fingerprint);

View File

@ -24,6 +24,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include "GFModuleCommonUtils.hpp"
#include "GFSDKBasic.h" #include "GFSDKBasic.h"
#include "output.h" #include "output.h"
#include "packets.h" #include "packets.h"
@ -74,7 +75,7 @@ struct packet *parse(FILE *input, unsigned char want, unsigned char stop) {
} else if (byte >= 224) { } else if (byte >= 224) {
/* Partial body length, so fail (keys can't use /* Partial body length, so fail (keys can't use
partial body) */ partial body) */
fprintf(stderr, "Invalid partial packet encoding\n"); LOG_ERROR("Invalid partial packet encoding");
goto fail; goto fail;
} else if (byte >= 192) { } else if (byte >= 192) {
/* 2-byte length */ /* 2-byte length */
@ -120,17 +121,16 @@ struct packet *parse(FILE *input, unsigned char want, unsigned char stop) {
break; break;
default: default:
fprintf(stderr, "Error: unable to parse old-style length\n"); LOG_ERROR("unable to parse old-style length");
goto fail; goto fail;
} }
} }
if (verbose > 1) if (verbose > 1) {
fprintf(stderr, "Found packet of type %d, length %d\n", type, length); FLOG_DEBUG("Found packet of type %d, length %d", type, length);
}
} else { } else {
fprintf(stderr, LOG_ERROR("unable to parse OpenPGP packets (is this armored data?)");
"Error: unable to parse OpenPGP packets"
" (is this armored data?)\n");
goto fail; goto fail;
} }
@ -142,7 +142,7 @@ struct packet *parse(FILE *input, unsigned char want, unsigned char stop) {
packet->len = length; packet->len = length;
packet->size = length; packet->size = length;
if (fread(packet->buf, 1, packet->len, input) < packet->len) { if (fread(packet->buf, 1, packet->len, input) < packet->len) {
fprintf(stderr, "Short read on packet type %d\n", type); FLOG_ERROR("Short read on packet type %d", type);
goto fail; goto fail;
} }
break; break;
@ -201,7 +201,7 @@ ssize_t extract_secrets(struct packet *packet) {
public stuff. */ public stuff. */
if (packet->buf[0] == 3) { if (packet->buf[0] == 3) {
fprintf(stderr, "Version 3 (PGP 2.x style) keys are not supported.\n"); LOG_ERROR("Version 3 (PGP 2.x style) keys are not supported.");
return -1; return -1;
} else if (packet->buf[0] == 4) { } else if (packet->buf[0] == 4) {
/* Jump 5 bytes in. That gets us past 1 byte of version, and 4 /* Jump 5 bytes in. That gets us past 1 byte of version, and 4
@ -268,8 +268,7 @@ ssize_t extract_secrets(struct packet *packet) {
default: default:
/* What algorithm? */ /* What algorithm? */
fprintf(stderr, "Unable to parse algorithm %u\n", FLOG_ERROR("Unable to parse algorithm %u", packet->buf[offset - 1]);
packet->buf[offset - 1]);
return -1; return -1;
} }
@ -289,7 +288,7 @@ struct packet *read_secrets_file(FILE *secrets, enum data_type input_type) {
packet = append_packet(packet, buffer, got); packet = append_packet(packet, buffer, got);
if (got == 0 && !feof(secrets)) { if (got == 0 && !feof(secrets)) {
fprintf(stderr, "Error: unable to read secrets file\n"); LOG_ERROR("unable to read secrets file");
free_packet(packet); free_packet(packet);
return NULL; return NULL;
} }
@ -315,8 +314,8 @@ struct packet *read_secrets_file(FILE *secrets, enum data_type input_type) {
linenum = atoi(line); linenum = atoi(line);
if (linenum != next_linenum) { if (linenum != next_linenum) {
fprintf(stderr, "Error: missing line number %u (saw %u)\n", FLOG_ERROR("missing line number %u (saw %u)", next_linenum,
next_linenum, linenum); linenum);
free_packet(packet); free_packet(packet);
return NULL; return NULL;
} else } else
@ -340,10 +339,9 @@ struct packet *read_secrets_file(FILE *secrets, enum data_type input_type) {
if (sscanf(tok, "%06lX", &new_crc)) { if (sscanf(tok, "%06lX", &new_crc)) {
if (did_digit) { if (did_digit) {
if ((new_crc & 0xFFFFFFL) != (line_crc & 0xFFFFFFL)) { if ((new_crc & 0xFFFFFFL) != (line_crc & 0xFFFFFFL)) {
fprintf(stderr, FLOG_ERROR("CRC on line %d does not match (%06lX!=%06lX)",
"CRC on line %d does not" linenum, new_crc & 0xFFFFFFL,
" match (%06lX!=%06lX)\n", line_crc & 0xFFFFFFL);
linenum, new_crc & 0xFFFFFFL, line_crc & 0xFFFFFFL);
if (!ignore_crc_error) { if (!ignore_crc_error) {
free_packet(packet); free_packet(packet);
return NULL; return NULL;
@ -368,7 +366,7 @@ struct packet *read_secrets_file(FILE *secrets, enum data_type input_type) {
tok = next; tok = next;
} }
} else { } else {
fprintf(stderr, "No colon ':' found in line %u\n", linenum); FLOG_ERROR("No colon ':' found in line %u", linenum);
free_packet(packet); free_packet(packet);
return NULL; return NULL;
} }
@ -381,7 +379,7 @@ struct packet *read_secrets_file(FILE *secrets, enum data_type input_type) {
do_crc24(&all_crc, packet->buf, packet->len); do_crc24(&all_crc, packet->buf, packet->len);
if ((my_crc & 0xFFFFFFL) != (all_crc & 0xFFFFFFL)) { if ((my_crc & 0xFFFFFFL) != (all_crc & 0xFFFFFFL)) {
fprintf(stderr, "CRC of secret does not match (%06lX!=%06lX)\n", FLOG_ERROR("CRC of secret does not match (%06lX!=%06lX)",
my_crc & 0xFFFFFFL, all_crc & 0xFFFFFFL); my_crc & 0xFFFFFFL, all_crc & 0xFFFFFFL);
if (!ignore_crc_error) { if (!ignore_crc_error) {
free_packet(packet); free_packet(packet);
@ -389,7 +387,7 @@ struct packet *read_secrets_file(FILE *secrets, enum data_type input_type) {
} }
} }
} else { } else {
fprintf(stderr, "CRC of secret is missing\n"); LOG_ERROR("CRC of secret is missing");
if (!ignore_crc_error) { if (!ignore_crc_error) {
free_packet(packet); free_packet(packet);
return NULL; return NULL;

View File

@ -24,6 +24,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include "GFModuleCommonUtils.hpp"
#include "GFSDKBasic.h" #include "GFSDKBasic.h"
#include "output.h" #include "output.h"
#include "packets.h" #include "packets.h"
@ -41,7 +42,7 @@ static auto extract_keys(struct packet *packet) -> struct key * {
/* Check the version */ /* Check the version */
if (packet->len && packet->buf[0] != 0) { if (packet->len && packet->buf[0] != 0) {
fprintf(stderr, "Cannot handle secrets file version %d\n", packet->buf[0]); FLOG_ERROR("Cannot handle secrets file version %d", packet->buf[0]);
return NULL; return NULL;
} }
@ -68,7 +69,7 @@ static auto extract_keys(struct packet *packet) -> struct key * {
newkey->packet = append_packet(NULL, &packet->buf[idx], len); newkey->packet = append_packet(NULL, &packet->buf[idx], len);
idx += len; idx += len;
} else { } else {
fprintf(stderr, "Warning: Short data in secret image\n"); LOG_WARN("Short data in secret image");
free(newkey); free(newkey);
break; break;
} }
@ -76,11 +77,11 @@ static auto extract_keys(struct packet *packet) -> struct key * {
newkey->next = key; newkey->next = key;
key = newkey; key = newkey;
} else { } else {
fprintf(stderr, "Warning: Corrupt data in secret image\n"); LOG_WARN("Corrupt data in secret image");
break; break;
} }
} else { } else {
fprintf(stderr, "Warning: Short header in secret image\n"); LOG_WARN("Short header in secret image");
break; break;
} }
} }
@ -105,7 +106,7 @@ auto restore(FILE *pubring, FILE *secrets, enum data_type input_type,
int test = fgetc(secrets); int test = fgetc(secrets);
if (test == EOF) { if (test == EOF) {
fprintf(stderr, "Unable to check type of secrets file\n"); LOG_ERROR("Unable to check type of secrets file");
return 1; return 1;
} else if (isascii(test) && isprint(test)) } else if (isascii(test) && isprint(test))
input_type = BASE16; input_type = BASE16;
@ -170,11 +171,11 @@ auto restore(FILE *pubring, FILE *secrets, enum data_type input_type,
free_keys(keys); free_keys(keys);
output_end(); output_end();
} else { } else {
fprintf(stderr, "Unable to parse secret data\n"); LOG_ERROR("Unable to parse secret data");
return 1; return 1;
} }
} else { } else {
fprintf(stderr, "Unable to read secrets file\n"); LOG_ERROR("Unable to read secrets file");
return 1; return 1;
} }