jpokorny / rpms / gnupg2

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