Blame SOURCES/gnupg-2.2.20-file-is-digest.patch

4538af
diff -up gnupg-2.2.20/g10/gpg.c.file-is-digest gnupg-2.2.20/g10/gpg.c
4538af
--- gnupg-2.2.20/g10/gpg.c.file-is-digest	2020-04-14 16:33:42.630269318 +0200
4538af
+++ gnupg-2.2.20/g10/gpg.c	2020-04-14 16:34:46.455100086 +0200
4538af
@@ -380,6 +380,7 @@ enum cmd_and_opt_values
4538af
     oTTYtype,
4538af
     oLCctype,
4538af
     oLCmessages,
4538af
+    oFileIsDigest,
4538af
     oXauthority,
4538af
     oGroup,
4538af
     oUnGroup,
4538af
@@ -831,6 +832,7 @@ static ARGPARSE_OPTS opts[] = {
4538af
   ARGPARSE_s_s (oPersonalCompressPreferences,
4538af
                                          "personal-compress-preferences", "@"),
4538af
   ARGPARSE_s_s (oFakedSystemTime, "faked-system-time", "@"),
4538af
+  ARGPARSE_s_n (oFileIsDigest, "file-is-digest", "@"),
4538af
   ARGPARSE_s_s (oWeakDigest, "weak-digest","@"),
4538af
   ARGPARSE_s_n (oUnwrap, "unwrap", "@"),
4538af
   ARGPARSE_s_n (oOnlySignTextIDs, "only-sign-text-ids", "@"),
4538af
@@ -2419,6 +2421,7 @@ main (int argc, char **argv)
4538af
     opt.keyid_format = KF_NONE;
4538af
     opt.def_sig_expire = "0";
4538af
     opt.def_cert_expire = "0";
4538af
+    opt.file_is_digest = 0;
4538af
     gnupg_set_homedir (NULL);
4538af
     opt.passphrase_repeat = 1;
4538af
     opt.emit_version = 0;
4538af
@@ -2997,6 +3000,7 @@ main (int argc, char **argv)
4538af
 	    opt.verify_options&=~VERIFY_SHOW_PHOTOS;
4538af
 	    break;
4538af
 	  case oPhotoViewer: opt.photo_viewer = pargs.r.ret_str; break;
4538af
+	  case oFileIsDigest: opt.file_is_digest = 1; break;
4538af
 
4538af
           case oDisableSignerUID: opt.flags.disable_signer_uid = 1; break;
4538af
           case oIncludeKeyBlock:  opt.flags.include_key_block = 1; break;
4538af
diff -up gnupg-2.2.20/g10/options.h.file-is-digest gnupg-2.2.20/g10/options.h
4538af
--- gnupg-2.2.20/g10/options.h.file-is-digest	2020-03-14 19:54:05.000000000 +0100
4538af
+++ gnupg-2.2.20/g10/options.h	2020-04-14 16:33:42.634269245 +0200
4538af
@@ -202,6 +202,7 @@ struct
4538af
   int no_auto_check_trustdb;
4538af
   int preserve_permissions;
4538af
   int no_homedir_creation;
4538af
+  int file_is_digest;
4538af
   struct groupitem *grouplist;
4538af
   int mangle_dos_filenames;
4538af
   int enable_progress_filter;
4538af
diff -up gnupg-2.2.20/g10/sign.c.file-is-digest gnupg-2.2.20/g10/sign.c
4538af
--- gnupg-2.2.20/g10/sign.c.file-is-digest	2020-03-14 19:35:46.000000000 +0100
4538af
+++ gnupg-2.2.20/g10/sign.c	2020-04-14 16:36:54.661751422 +0200
4538af
@@ -40,6 +40,7 @@
4538af
 #include "pkglue.h"
4538af
 #include "../common/sysutils.h"
4538af
 #include "call-agent.h"
4538af
+#include "../common/host2net.h"
4538af
 #include "../common/mbox-util.h"
4538af
 #include "../common/compliance.h"
4538af
 
4538af
@@ -834,6 +835,8 @@ write_signature_packets (ctrl_t ctrl,
4538af
       if (duration || opt.sig_policy_url
4538af
           || opt.sig_notations || opt.sig_keyserver_url)
4538af
         sig->version = 4;
4538af
+	  else if (opt.file_is_digest)
4538af
+        sig->version = 3;
4538af
       else
4538af
         sig->version = pk->version;
4538af
 
4538af
@@ -860,8 +863,11 @@ write_signature_packets (ctrl_t ctrl,
4538af
           else
4538af
             err = 0;
4538af
         }
4538af
-      hash_sigversion_to_magic (md, sig);
4538af
-      gcry_md_final (md);
4538af
+
4538af
+      if (!opt.file_is_digest) {
4538af
+	hash_sigversion_to_magic (md, sig);
4538af
+	gcry_md_final (md);
4538af
+      }
4538af
 
4538af
       if (!err)
4538af
         err = do_sign (ctrl, pk, sig, md, hash_for (pk), cache_nonce, 0);
4538af
@@ -924,6 +930,8 @@ sign_file (ctrl_t ctrl, strlist_t filena
4538af
     SK_LIST sk_rover = NULL;
4538af
     int multifile = 0;
4538af
     u32 duration=0;
4538af
+    int sigclass = 0x00;
4538af
+    u32 timestamp = 0;
4538af
 
4538af
     pfx = new_progress_context ();
4538af
     afx = new_armor_context ();
4538af
@@ -941,7 +949,16 @@ sign_file (ctrl_t ctrl, strlist_t filena
4538af
 	fname = NULL;
4538af
 
4538af
     if( fname && filenames->next && (!detached || encryptflag) )
4538af
-	log_bug("multiple files can only be detached signed");
4538af
+	log_bug("multiple files can only be detached signed\n");
4538af
+
4538af
+    if (opt.file_is_digest && (multifile || !fname))
4538af
+	log_bug("file-is-digest only works with one file\n");
4538af
+    if (opt.file_is_digest && !detached)
4538af
+	log_bug("file-is-digest can only write detached signatures\n");
4538af
+    if (opt.file_is_digest && !opt.def_digest_algo)
4538af
+	log_bug("file-is-digest needs --digest-algo\n");
4538af
+    if (opt.file_is_digest && opt.textmode)
4538af
+	log_bug("file-is-digest doesn't work with --textmode\n");
4538af
 
4538af
     if(encryptflag==2
4538af
        && (rc=setup_symkey(&efx.symkey_s2k,&efx.symkey_dek)))
4538af
@@ -962,7 +979,7 @@ sign_file (ctrl_t ctrl, strlist_t filena
4538af
       goto leave;
4538af
 
4538af
     /* prepare iobufs */
4538af
-    if( multifile )  /* have list of filenames */
4538af
+    if( multifile || opt.file_is_digest)  /* have list of filenames */
4538af
 	inp = NULL; /* we do it later */
4538af
     else {
4538af
       inp = iobuf_open(fname);
4538af
@@ -1100,7 +1117,7 @@ sign_file (ctrl_t ctrl, strlist_t filena
4538af
     for (sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next)
4538af
       gcry_md_enable (mfx.md, hash_for (sk_rover->pk));
4538af
 
4538af
-    if( !multifile )
4538af
+    if( !multifile && !opt.file_is_digest )
4538af
 	iobuf_push_filter( inp, md_filter, &mfx );
4538af
 
4538af
     if( detached && !encryptflag)
4538af
@@ -1155,6 +1172,8 @@ sign_file (ctrl_t ctrl, strlist_t filena
4538af
 
4538af
     write_status_begin_signing (mfx.md);
4538af
 
4538af
+    sigclass = opt.textmode && !outfile? 0x01 : 0x00;
4538af
+
4538af
     /* Setup the inner packet. */
4538af
     if( detached ) {
4538af
 	if( multifile ) {
4538af
@@ -1195,6 +1214,45 @@ sign_file (ctrl_t ctrl, strlist_t filena
4538af
 	    if( opt.verbose )
4538af
               log_printf ("\n");
4538af
 	}
4538af
+	else if (opt.file_is_digest) {
4538af
+	    byte *mdb, ts[5];
4538af
+	    size_t mdlen;
4538af
+	    const char *fp;
4538af
+	    int c, d;
4538af
+
4538af
+	    gcry_md_final(mfx.md);
4538af
+	    /* this assumes gcry_md_read returns the same buffer */
4538af
+	    mdb = gcry_md_read(mfx.md, opt.def_digest_algo);
4538af
+		mdlen = gcry_md_get_algo_dlen(opt.def_digest_algo);
4538af
+	    if (strlen(fname) != mdlen * 2 + 11)
4538af
+	        log_bug("digests must be %zu + @ + 5 bytes\n", mdlen);
4538af
+	    d = -1;
4538af
+	    for (fp = fname ; *fp; ) {
4538af
+		c = *fp++;
4538af
+		if (c >= '0' && c <= '9')
4538af
+		    c -= '0';
4538af
+		else if (c >= 'a' && c <= 'f')
4538af
+		    c -= 'a' - 10;
4538af
+		else if (c >= 'A' && c <= 'F')
4538af
+		    c -= 'A' - 10;
4538af
+		else
4538af
+		    log_bug("filename is not hex\n");
4538af
+		if (d >= 0) {
4538af
+		    *mdb++ = d << 4 | c;
4538af
+		    c = -1;
4538af
+		    if (--mdlen == 0) {
4538af
+			mdb = ts;
4538af
+			if (*fp++ != '@')
4538af
+			    log_bug("missing time separator\n");
4538af
+		    }
4538af
+		}
4538af
+		d = c;
4538af
+	    }
4538af
+	    sigclass = ts[0];
4538af
+	    if (sigclass != 0x00 && sigclass != 0x01)
4538af
+		log_bug("bad cipher class\n");
4538af
+	    timestamp = buf32_to_u32(ts + 1);
4538af
+	}
4538af
 	else {
4538af
 	    /* read, so that the filter can calculate the digest */
4538af
 	    while( iobuf_get(inp) != -1 )
4538af
@@ -1213,8 +1271,8 @@ sign_file (ctrl_t ctrl, strlist_t filena
4538af
 
4538af
     /* write the signatures */
4538af
     rc = write_signature_packets (ctrl, sk_list, out, mfx.md,
4538af
-                                  opt.textmode && !outfile? 0x01 : 0x00,
4538af
-				  0, duration, detached ? 'D':'S', NULL);
4538af
+                                  sigclass,
4538af
+				  timestamp, duration, detached ? 'D':'S', NULL);
4538af
     if( rc )
4538af
         goto leave;
4538af