Index: b/doc/gpg.texi =================================================================== --- b/doc/gpg.texi +++ b/doc/gpg.texi @@ -2223,6 +2223,13 @@ algorithm in this list is also used when there are no recipient keys to consider (e.g. @option{--symmetric}). +@item --file-is-digest +@opindex file-is-digest +Write the detached signatures using the digest of the file. Only one +file is allowed to be used with this option. Use this option only if +you are sure that the digest is really the digest of that file. This +can be a security risk in a multi-user environment. + @item --s2k-cipher-algo @code{name} @opindex s2k-cipher-algo Use @code{name} as the cipher algorithm used to protect secret keys. Index: b/g10/gpg.c =================================================================== --- b/g10/gpg.c +++ b/g10/gpg.c @@ -355,6 +355,7 @@ oTTYtype, oLCctype, oLCmessages, + oFileIsDigest, oXauthority, oGroup, oUnGroup, @@ -741,6 +742,7 @@ ARGPARSE_s_s (oPersonalDigestPreferences, "personal-digest-preferences","@"), ARGPARSE_s_s (oPersonalCompressPreferences, "personal-compress-preferences", "@"), + ARGPARSE_s_n (oFileIsDigest, "file-is-digest", "@"), ARGPARSE_s_s (oFakedSystemTime, "faked-system-time", "@"), /* Aliases. I constantly mistype these, and assume other people do @@ -2100,6 +2102,7 @@ opt.def_sig_expire = "0"; opt.def_cert_expire = "0"; set_homedir (default_homedir ()); + opt.file_is_digest=0; opt.passphrase_repeat = 1; opt.emit_version = 1; /* Limit to the major number. */ @@ -2602,6 +2605,7 @@ case oPhotoViewer: opt.photo_viewer = pargs.r.ret_str; break; case oForceV3Sigs: opt.force_v3_sigs = 1; break; case oNoForceV3Sigs: opt.force_v3_sigs = 0; break; + case oFileIsDigest: opt.file_is_digest = 1; break; case oForceV4Certs: opt.force_v4_certs = 1; break; case oNoForceV4Certs: opt.force_v4_certs = 0; break; case oForceMDC: opt.force_mdc = 1; break; Index: b/g10/options.h =================================================================== --- b/g10/options.h +++ b/g10/options.h @@ -195,6 +195,7 @@ int no_auto_check_trustdb; int preserve_permissions; int no_homedir_creation; + int file_is_digest; struct groupitem *grouplist; int mangle_dos_filenames; int enable_progress_filter; Index: b/g10/sign.c =================================================================== --- b/g10/sign.c +++ b/g10/sign.c @@ -738,8 +738,12 @@ mk_notation_policy_etc (sig, pk, NULL); } - hash_sigversion_to_magic (md, sig); - gcry_md_final (md); + if (!opt.file_is_digest) { + hash_sigversion_to_magic (md, sig); + gcry_md_final (md); + } else if (sig->version >= 4) { + log_bug("file-is-digest doesn't work with v4 sigs\n"); + } rc = do_sign (pk, sig, md, hash_for (pk), cache_nonce); gcry_md_close (md); @@ -797,6 +801,8 @@ SK_LIST sk_rover = NULL; int multifile = 0; u32 duration=0; + int sigclass = 0x00; + u32 timestamp = 0; pfx = new_progress_context (); afx = new_armor_context (); @@ -813,7 +819,15 @@ fname = NULL; if( fname && filenames->next && (!detached || encryptflag) ) - log_bug("multiple files can only be detached signed"); + log_bug("multiple files can only be detached signed\n"); + if (opt.file_is_digest && (multifile || !fname)) + log_bug("file-is-digest only works with one file\n"); + if (opt.file_is_digest && !detached) + log_bug("file-is-digest can only write detached signatures\n"); + if (opt.file_is_digest && !opt.def_digest_algo) + log_bug("file-is-digest needs --digest-algo\n"); + if (opt.file_is_digest && opt.textmode) + log_bug("file-is-digest doesn't work with --textmode\n"); if(encryptflag==2 && (rc=setup_symkey(&efx.symkey_s2k,&efx.symkey_dek))) @@ -844,7 +858,7 @@ goto leave; /* prepare iobufs */ - if( multifile ) /* have list of filenames */ + if( multifile || opt.file_is_digest) /* have list of filenames */ inp = NULL; /* we do it later */ else { inp = iobuf_open(fname); @@ -983,7 +997,7 @@ for (sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next) gcry_md_enable (mfx.md, hash_for (sk_rover->pk)); - if( !multifile ) + if( !multifile && !opt.file_is_digest ) iobuf_push_filter( inp, md_filter, &mfx ); if( detached && !encryptflag && !RFC1991 ) @@ -1038,6 +1052,8 @@ write_status_begin_signing (mfx.md); + sigclass = opt.textmode && !outfile? 0x01 : 0x00; + /* Setup the inner packet. */ if( detached ) { if( multifile ) { @@ -1078,7 +1094,46 @@ if( opt.verbose ) putc( '\n', stderr ); } - else { + else if (opt.file_is_digest) { + byte *mdb, ts[5]; + size_t mdlen; + const char *fp; + int c, d; + + gcry_md_final(mfx.md); + /* this assumes gcry_md_read returns the same buffer */ + mdb = gcry_md_read(mfx.md, opt.def_digest_algo); + mdlen = gcry_md_get_algo_dlen(opt.def_digest_algo); + if (strlen(fname) != mdlen * 2 + 11) + log_bug("digests must be %d + @ + 5 bytes\n", mdlen); + d = -1; + for (fp = fname ; *fp; ) { + c = *fp++; + if (c >= '0' && c <= '9') + c -= '0'; + else if (c >= 'a' && c <= 'f') + c -= 'a' - 10; + else if (c >= 'A' && c <= 'F') + c -= 'A' - 10; + else + log_bug("filename is not hex\n"); + if (d >= 0) { + *mdb++ = d << 4 | c; + c = -1; + if (--mdlen == 0) { + mdb = ts; + if (*fp++ != '@') + log_bug("missing time separator\n"); + } + } + d = c; + } + sigclass = ts[0]; + if (sigclass != 0x00 && sigclass != 0x01) + log_bug("bad cipher class\n"); + timestamp = buffer_to_u32(ts + 1); + } + else { /* read, so that the filter can calculate the digest */ while( iobuf_get(inp) != -1 ) ; @@ -1095,8 +1150,8 @@ /* write the signatures */ rc = write_signature_packets (sk_list, out, mfx.md, - opt.textmode && !outfile? 0x01 : 0x00, - 0, duration, detached ? 'D':'S', NULL); + sigclass, + timestamp, duration, detached ? 'D':'S'); if( rc ) goto leave; @@ -1568,7 +1623,7 @@ if( !rc ) { hash_sigversion_to_magic (md, sig); - gcry_md_final (md); + gcry_md_final (md); rc = complete_sig (sig, pksk, md, cache_nonce); } @@ -1662,7 +1717,7 @@ if (!rc) { hash_sigversion_to_magic (md, sig); - gcry_md_final (md); + gcry_md_final (md); rc = complete_sig (sig, pksk, md, NULL); }