From 7a3358b7875622c93853b76b6ec34f5c2edb1939 Mon Sep 17 00:00:00 2001 From: Chinmoy Ranjan Pradhan Date: Sat, 7 Apr 2018 17:40:24 +0000 Subject: [PATCH] Add ability to extract signature information to pdfsig tool This patch adds '-dump' option to pdfsig tool which can be used to extract signatures into the current working directory. --- poppler/Form.cc | 5 +++++ poppler/Form.h | 2 ++ utils/pdfsig.cc | 30 +++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/poppler/Form.cc b/poppler/Form.cc index 6aff30d1..19088616 100644 --- a/poppler/Form.cc +++ b/poppler/Form.cc @@ -461,6 +461,11 @@ FormWidgetSignature::FormWidgetSignature(PDFDoc *docA, Object *aobj, unsigned nu type = formSignature; } +GooString *FormWidgetSignature::getSignature() +{ + return static_cast(field)->getSignature(); +} + SignatureInfo *FormWidgetSignature::validateSignature(bool doVerifyCert, bool forceRevalidation, time_t validationTime) { return static_cast(field)->validateSignature(doVerifyCert, forceRevalidation, validationTime); diff --git a/poppler/Form.h b/poppler/Form.h index 9df6b22e..3a85f094 100644 --- a/poppler/Form.h +++ b/poppler/Form.h @@ -283,6 +283,8 @@ public: // if the check passed (and the checked file size as output parameter in checkedFileSize) // otherwise a nullptr is returned GooString* getCheckedSignature(Goffset *checkedFileSize); + + GooString *getSignature(); }; //------------------------------------------------------------------------ diff --git a/utils/pdfsig.cc b/utils/pdfsig.cc index 2f08c82a..04bdd730 100644 --- a/utils/pdfsig.cc +++ b/utils/pdfsig.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include "parseargs.h" #include "Object.h" #include "Array.h" @@ -88,13 +89,32 @@ static char *getReadableTime(time_t unix_time) return time_str; } +static void dumpSignature(int sig_num, FormWidgetSignature *sig_widget, const char *filename) +{ + GooString *signature = sig_widget->getSignature(); + if (!signature) { + printf("Cannot dump signature #%d\n", sig_num); + return; + } + + char buf[1024]; + snprintf(buf, sizeof buf, "%s.sig%02u", basename(filename), sig_num); + printf("Signature #%d (%u bytes) => %s\n", sig_num, signature->getLength(), buf); + std::ofstream outfile(buf, std::ofstream::binary); + outfile.write(signature->getCString(), signature->getLength()); + outfile.close(); +} + static GBool printVersion = gFalse; static GBool printHelp = gFalse; static GBool dontVerifyCert = gFalse; +static GBool dumpSignatures = gFalse; static const ArgDesc argDesc[] = { {"-nocert", argFlag, &dontVerifyCert, 0, "don't perform certificate validation"}, + {"-dump", argFlag, &dumpSignatures, 0, + "dump all signatures into current directory"}, {"-v", argFlag, &printVersion, 0, "print copyright and version info"}, @@ -150,7 +170,15 @@ int main(int argc, char *argv[]) sigCount = sig_widgets.size(); if (sigCount >= 1) { - printf("Digital Signature Info of: %s\n", fileName->getCString()); + if (dumpSignatures) { + printf("Dumping Signatures : %u\n", sigCount); + for (unsigned int i = 0; i < sigCount; i++) { + dumpSignature(i, sig_widgets.at(i), fileName->getCString()); + } + goto end; + } else { + printf("Digital Signature Info of: %s\n", fileName->getCString()); + } } else { printf("File '%s' does not contain any signatures\n", fileName->getCString()); exitCode = 2; -- 2.15.1