[gs-commits] mupdf 1.16.1.epub-prerelease-36 Add time of signing to di

[email protected] (Paul Gardiner) Thu, 2 Jan 2020 12:50:34 +0000 (UTC)
Newsgroups gmane.comp.printing.ghostscript.cvs
Message-ID <[email protected]>
commit 68ba6fadd62a28c49fc31603e6ab5d007d305314
Author: Paul Gardiner <[email protected]>
Date:   Fri Dec 13 11:23:06 2019 +0000

    Add time of signing to digital signatures.
    
    This add the time to both the field value and to the appearance stream.
    Apparently, time can be added to the signature itself. Possibly, we
    should investigate doing that at some stage.

diff --git a/include/mupdf/pdf/annot.h b/include/mupdf/pdf/annot.h
index afaf618..c3fa68c 100644
--- a/include/mupdf/pdf/annot.h
+++ b/include/mupdf/pdf/annot.h
@@ -206,8 +206,8 @@ void pdf_set_annot_contents(fz_context *ctx, pdf_annot *annot, const char *text)
 const char *pdf_annot_author(fz_context *ctx, pdf_annot *annot);
 void pdf_set_annot_author(fz_context *ctx, pdf_annot *annot, const char *author);
 
+void pdf_format_date(fz_context *ctx, char *s, int n, int64_t secs);
 int64_t pdf_annot_modification_date(fz_context *ctx, pdf_annot *annot);
-
 void pdf_set_annot_modification_date(fz_context *ctx, pdf_annot *annot, int64_t time);
 
 void pdf_parse_default_appearance(fz_context *ctx, const char *da, const char **font, float *size, float color[3]);
diff --git a/include/mupdf/pdf/form.h b/include/mupdf/pdf/form.h
index 2db66cc..e09b00e 100644
--- a/include/mupdf/pdf/form.h
+++ b/include/mupdf/pdf/form.h
@@ -100,7 +100,7 @@ int pdf_set_text_field_value(fz_context *ctx, pdf_widget *widget, const char *va
 int pdf_set_choice_field_value(fz_context *ctx, pdf_widget *widget, const char *value);
 
 int pdf_signature_is_signed(fz_context *ctx, pdf_document *doc, pdf_obj *field);
-void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_pkcs7_signer *signer);
+void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_pkcs7_signer *signer, int64_t stime);
 
 void pdf_field_reset(fz_context *ctx, pdf_document *doc, pdf_obj *field);
 
diff --git a/source/pdf/pdf-annot.c b/source/pdf/pdf-annot.c
index 2c13ff5..00fba42 100644
--- a/source/pdf/pdf-annot.c
+++ b/source/pdf/pdf-annot.c
@@ -1426,9 +1426,10 @@ pdf_add_annot_ink_list(fz_context *ctx, pdf_annot *annot, int n, fz_point p[])
 	pdf_dirty_annot(ctx, annot);
 }
 
-static void
-pdf_format_date(fz_context *ctx, char *s, int n, time_t secs)
+void
+pdf_format_date(fz_context *ctx, char *s, int n, int64_t isecs)
 {
+	time_t secs = isecs;
 #ifdef _POSIX_SOURCE
 	struct tm tmbuf, *tm = gmtime_r(&secs, &tmbuf);
 #else
diff --git a/source/pdf/pdf-form.c b/source/pdf/pdf-form.c
index c5ef15d..1ca92c6 100644
--- a/source/pdf/pdf-form.c
+++ b/source/pdf/pdf-form.c
@@ -1330,13 +1330,14 @@ int pdf_signature_contents(fz_context *ctx, pdf_document *doc, pdf_obj *signatur
 	return len;
 }
 
-void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_pkcs7_signer *signer)
+void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_pkcs7_signer *signer, int64_t stime)
 {
 	pdf_obj *v = NULL;
 	pdf_obj *indv;
 	int vnum;
 	int max_digest_size;
 	char *buf = NULL;
+	char date_string[40];
 
 	vnum = pdf_create_object(ctx, doc);
 	indv = pdf_new_indirect(ctx, doc, vnum, 0);
@@ -1362,6 +1363,8 @@ void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field,
 		pdf_dict_put(ctx, v, PDF_NAME(Filter), PDF_NAME(Adobe_PPKLite));
 		pdf_dict_put(ctx, v, PDF_NAME(SubFilter), PDF_NAME(adbe_pkcs7_detached));
 		pdf_dict_put(ctx, v, PDF_NAME(Type), PDF_NAME(Sig));
+		pdf_format_date(ctx, date_string, sizeof date_string, stime);
+		pdf_dict_put_text_string(ctx, v, PDF_NAME(M), date_string);
 
 		/* Record details within the document structure so that contents
 		* and byte_range can be updated with their correct values at
diff --git a/source/pdf/pdf-signature.c b/source/pdf/pdf-signature.c
index c6839b8..37c1be9 100644
--- a/source/pdf/pdf-signature.c
+++ b/source/pdf/pdf-signature.c
@@ -3,6 +3,7 @@
 #include "../fitz/fitz-imp.h"
 
 #include <string.h>
+#include <time.h>
 
 void pdf_write_digest(fz_context *ctx, fz_output *out, pdf_obj *byte_range, int hexdigest_offset, int hexdigest_length, pdf_pkcs7_signer *signer)
 {
@@ -74,6 +75,14 @@ void pdf_sign_signature(fz_context *ctx, pdf_document *doc, pdf_widget *widget,
 		const char *dn_str;
 		pdf_obj *wobj = ((pdf_annot *)widget)->obj;
 		fz_rect rect;
+		time_t now = time(NULL);
+#ifdef _POSIX_SOURCE
+		struct tm tmbuf, *tm = gmtime_r(&now, &tmbuf);
+#else
+		struct tm *tm = gmtime(&now);
+#endif
+		char now_str[40];
+		int len = 0;
 
 		rect = pdf_dict_get_rect(ctx, wobj, PDF_NAME(Rect));
 
@@ -100,10 +109,13 @@ void pdf_sign_signature(fz_context *ctx, pdf_document *doc, pdf_widget *widget,
 				fz_append_printf(ctx, fzbuf, ", c=%s", dn->c);
 
 			dn_str = fz_string_from_buffer(ctx, fzbuf);
-			pdf_update_signature_appearance(ctx, (pdf_annot *)widget, dn->cn, dn_str, NULL);
+
+			if (tm)
+				len = strftime(now_str, sizeof now_str, "%Y.%m.%d %H:%M:%SZ", tm);
+			pdf_update_signature_appearance(ctx, (pdf_annot *)widget, dn->cn, dn_str, len?now_str:NULL);
 		}
 
-		pdf_signature_set_value(ctx, doc, wobj, signer);
+		pdf_signature_set_value(ctx, doc, wobj, signer, now);
 	}
 	fz_always(ctx)
 	{

http://git.ghostscript.com/?p=mupdf.git;a=commit;h=68ba6fadd62a28c49fc31603e6ab5d007d305314

--
MuPDF library
Artifex Software, Inc.