/* Print the BEGIN_SIGNING status message. If MD is not NULL it is used to retrieve the hash algorithms used for the message. */ void write_status_begin_signing (gcry_md_hd_t md) { if (md) { char buf[100]; size_t buflen; int i; /* We use a hard coded list of possible algorithms. Using other algorithms than specified by OpenPGP does not make sense anyway. We do this out of performance reasons: Walking all the 110 allowed Ids is not a good idea given the way the check is implemented in libgcrypt. Recall that the only use of this status code is to create the micalg algorithm for PGP/MIME. */ buflen = 0; for (i=1; i <= 11; i++) if (i < 4 || i > 7) if ( gcry_md_is_enabled (md, i) && buflen < DIM(buf) ) { snprintf (buf+buflen, DIM(buf) - buflen - 1, "%sH%d", buflen? " ":"",i); buflen += strlen (buf+buflen); } write_status_text ( STATUS_BEGIN_SIGNING, buf ); } else write_status ( STATUS_BEGIN_SIGNING ); }
int md5hash(void * buffer, int length, void * digest){ gcry_md_hd_t md5_ctx; gcry_md_open ( &md5_ctx, GCRY_MD_MD5, 0); if(!gcry_md_is_enabled(md5_ctx, GCRY_MD_MD5)){ fprintf(stderr,"[libcrypt] unable to initiate MD5 hash algorithm.\n"); } gcry_md_write(md5_ctx,buffer,length); memcpy (digest, gcry_md_read (md5_ctx, 0), 16); gcry_md_close (md5_ctx); return 0; }
int signature_check2 (PKT_signature *sig, gcry_md_hd_t digest, u32 *r_expiredate, int *r_expired, int *r_revoked, PKT_public_key *pk ) { int rc=0; int pk_internal; if (pk) pk_internal = 0; else { pk_internal = 1; pk = xmalloc_clear( sizeof *pk ); } if ( (rc=openpgp_md_test_algo(sig->digest_algo)) ) ; /* We don't have this digest. */ else if ((rc=openpgp_pk_test_algo(sig->pubkey_algo))) ; /* We don't have this pubkey algo. */ else if (!gcry_md_is_enabled (digest,sig->digest_algo)) { /* Sanity check that the md has a context for the hash that the sig is expecting. This can happen if a onepass sig header does not match the actual sig, and also if the clearsign "Hash:" header is missing or does not match the actual sig. */ log_info(_("WARNING: signature digest conflict in message\n")); rc = GPG_ERR_GENERAL; } else if( get_pubkey( pk, sig->keyid ) ) rc = GPG_ERR_NO_PUBKEY; else if(!pk->flags.valid && !pk->flags.primary) { /* You cannot have a good sig from an invalid subkey. */ rc = GPG_ERR_BAD_PUBKEY; } else { if(r_expiredate) *r_expiredate = pk->expiredate; rc = do_check( pk, sig, digest, r_expired, r_revoked, NULL ); /* Check the backsig. This is a 0x19 signature from the subkey on the primary key. The idea here is that it should not be possible for someone to "steal" subkeys and claim them as their own. The attacker couldn't actually use the subkey, but they could try and claim ownership of any signaures issued by it. */ if(rc==0 && !pk->flags.primary && pk->flags.backsig < 2) { if (!pk->flags.backsig) { log_info(_("WARNING: signing subkey %s is not" " cross-certified\n"),keystr_from_pk(pk)); log_info(_("please see %s for more information\n"), "https://gnupg.org/faq/subkey-cross-certify.html"); /* --require-cross-certification makes this warning an error. TODO: change the default to require this after more keys have backsigs. */ if(opt.flags.require_cross_cert) rc = GPG_ERR_GENERAL; } else if(pk->flags.backsig == 1) { log_info(_("WARNING: signing subkey %s has an invalid" " cross-certification\n"),keystr_from_pk(pk)); rc = GPG_ERR_GENERAL; } } } if (pk_internal || rc) { release_public_key_parts (pk); if (pk_internal) xfree (pk); else /* Be very sure that the caller doesn't try to use *PK. */ memset (pk, 0, sizeof (*pk)); } if( !rc && sig->sig_class < 2 && is_status_enabled() ) { /* This signature id works best with DLP algorithms because * they use a random parameter for every signature. Instead of * this sig-id we could have also used the hash of the document * and the timestamp, but the drawback of this is, that it is * not possible to sign more than one identical document within * one second. Some remote batch processing applications might * like this feature here. * * Note that before 2.0.10, we used RIPE-MD160 for the hash * and accidently didn't include the timestamp and algorithm * information in the hash. Given that this feature is not * commonly used and that a replay attacks detection should * not solely be based on this feature (because it does not * work with RSA), we take the freedom and switch to SHA-1 * with 2.0.10 to take advantage of hardware supported SHA-1 * implementations. We also include the missing information * in the hash. Note also the SIG_ID as computed by gpg 1.x * and gpg 2.x didn't matched either because 2.x used to print * MPIs not in PGP format. */ u32 a = sig->timestamp; int nsig = pubkey_get_nsig( sig->pubkey_algo ); unsigned char *p, *buffer; size_t n, nbytes; int i; char hashbuf[20]; nbytes = 6; for (i=0; i < nsig; i++ ) { if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, 0, &n, sig->data[i])) BUG(); nbytes += n; } /* Make buffer large enough to be later used as output buffer. */ if (nbytes < 100) nbytes = 100; nbytes += 10; /* Safety margin. */ /* Fill and hash buffer. */ buffer = p = xmalloc (nbytes); *p++ = sig->pubkey_algo; *p++ = sig->digest_algo; *p++ = (a >> 24) & 0xff; *p++ = (a >> 16) & 0xff; *p++ = (a >> 8) & 0xff; *p++ = a & 0xff; nbytes -= 6; for (i=0; i < nsig; i++ ) { if (gcry_mpi_print (GCRYMPI_FMT_PGP, p, nbytes, &n, sig->data[i])) BUG(); p += n; nbytes -= n; } gcry_md_hash_buffer (GCRY_MD_SHA1, hashbuf, buffer, p-buffer); p = make_radix64_string (hashbuf, 20); sprintf (buffer, "%s %s %lu", p, strtimestamp (sig->timestamp), (ulong)sig->timestamp); xfree (p); write_status_text (STATUS_SIG_ID, buffer); xfree (buffer); }