aboutsummaryrefslogtreecommitdiffstats
path: root/src/m_paper_key/extract.cpp
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-07-28 08:18:33 +0000
committersaturneric <[email protected]>2024-07-28 08:18:33 +0000
commitc27c541257585878e4db705559c5cb953dc860b7 (patch)
tree7dc69115de96151a40acfc8891bf9a065eee0a9b /src/m_paper_key/extract.cpp
parentfix: solve a memory leak issue (diff)
downloadModules-c27c541257585878e4db705559c5cb953dc860b7.tar.gz
Modules-c27c541257585878e4db705559c5cb953dc860b7.zip
feat: add pinentry module and paper key module
Diffstat (limited to 'src/m_paper_key/extract.cpp')
-rw-r--r--src/m_paper_key/extract.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/m_paper_key/extract.cpp b/src/m_paper_key/extract.cpp
new file mode 100644
index 0000000..388a6c7
--- /dev/null
+++ b/src/m_paper_key/extract.cpp
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2007, 2017 David Shaw <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "extract.h"
+
+#include <cstdio>
+
+#include "output.h"
+#include "packets.h"
+#include "parse.h"
+
+extern int verbose;
+
+int extract(FILE *input, const char *outname, enum data_type output_type) {
+ struct packet *packet;
+ int offset;
+ unsigned char fingerprint[20];
+ unsigned char version = 0;
+
+ packet = parse(input, 5, 0);
+ if (!packet) {
+ fprintf(stderr, "Unable to find secret key packet\n");
+ return 1;
+ }
+
+ offset = extract_secrets(packet);
+ if (offset == -1) return 1;
+
+ if (verbose > 1) fprintf(stderr, "Secret offset is %d\n", offset);
+
+ calculate_fingerprint(packet, offset, fingerprint);
+
+ if (verbose) {
+ fprintf(stderr, "Primary key fingerprint: ");
+ print_bytes(stderr, fingerprint, 20);
+ fprintf(stderr, "\n");
+ }
+
+ output_start(outname, output_type, fingerprint);
+ output_bytes(&version, 1);
+ output_bytes(packet->buf, 1);
+ output_bytes(fingerprint, 20);
+ output_length16(packet->len - offset);
+ output_bytes(&packet->buf[offset], packet->len - offset);
+
+ free_packet(packet);
+
+ while ((packet = parse(input, 7, 5))) {
+ offset = extract_secrets(packet);
+ if (offset == -1) return 1;
+
+ if (verbose > 1) fprintf(stderr, "Secret subkey offset is %d\n", offset);
+
+ calculate_fingerprint(packet, offset, fingerprint);
+
+ if (verbose) {
+ fprintf(stderr, "Subkey fingerprint: ");
+ print_bytes(stderr, fingerprint, 20);
+ fprintf(stderr, "\n");
+ }
+
+ output_bytes(packet->buf, 1);
+ output_bytes(fingerprint, 20);
+ output_length16(packet->len - offset);
+ output_bytes(&packet->buf[offset], packet->len - offset);
+
+ free_packet(packet);
+ }
+
+ output_finish();
+
+ if (input == stdin) {
+ /* Consume everything else on input */
+ while ((fgetc(input) != EOF));
+ }
+
+ return 0;
+}