예제 #1
0
int
md_open_binary(const char *filename)
{
    return md_open(filename);
}
예제 #2
0
int era_create(int argc, char **argv)
{
	char table[64], uuid[64];
	char *name, *meta, *data, *orig;
	unsigned orig_major, orig_minor;
	unsigned meta_major, meta_minor;
	unsigned data_major, data_minor;
	struct era_dm_info orig_info;
	uint64_t sectors;
	struct md *md;
	int chunk;
	int fd;

	/*
	 * check and save arguments
	 */

	switch (argc)
	{
	case 0:
		error(0, "device name argument expected");
		usage(stderr, 1);
	case 1:
		error(0, "metadata device argument expected");
		usage(stderr, 1);
	case 2:
		error(0, "data device argument expected");
		usage(stderr, 1);
	case 3:
		chunk = DEF_CHUNK_SIZE;
		break;
	case 4:
		chunk = parse_chunk(argv[3]);
		if (chunk == -1)
		{
			error(0, "can't parse chunk size: %s", argv[3]);
			return -1;
		}
		break;
	default:
		error(0, "unknown argument: %s", argv[4]);
		usage(stderr, 1);
	}

	name = argv[0];
	meta = argv[1];
	data = argv[2];

	orig = malloc(16 + strlen(data));
	if (!orig)
	{
		error(ENOMEM, NULL);
		return -1;
	}

	sprintf(orig, "%s-orig", name);

	/*
	 * stat data device
	 */

	fd = blkopen(data, 0, &data_major, &data_minor, &sectors);
	if (fd == -1)
		goto out;

	close(fd);

	/*
	 * open metadata device
	 */

	md = md_open(meta, 1);
	if (!md)
		goto out;

	meta_major = md->major;
	meta_minor = md->minor;

	/*
	 * create empty era target
	 */

	snprintf(uuid, sizeof(uuid), "%s%u-%u",
	         UUID_PREFIX, meta_major, meta_minor);

	if (era_dm_create_empty(name, uuid, NULL))
	{
		md_close(md);
		goto out;
	}

	/*
	 * clear and close metadata
	 */

	if (clear_metadata(md, meta))
	{
		(void)era_dm_remove(name);
		md_close(md);
		goto out;
	}

	md_close(md);

	/*
	 * create origin target
	 */

	snprintf(uuid, sizeof(uuid), "%s%u-%u-orig",
	         UUID_PREFIX, meta_major, meta_minor);

	snprintf(table, sizeof(table), "%u:%u 0",
	         data_major, data_minor);

	if (era_dm_create(orig, uuid, 0, sectors,
	                  TARGET_LINEAR, table, &orig_info) == -1)
	{
		(void)era_dm_remove(name);
		goto out;
	}

	orig_major = (unsigned)orig_info.major;
	orig_minor = (unsigned)orig_info.minor;

	/*
	 * load and resume era target
	 */

	snprintf(table, sizeof(table), "%u:%u %u:%u %d",
	         meta_major, meta_minor, orig_major, orig_minor, chunk);

	if (era_dm_load(name, 0, sectors, TARGET_ERA, table, NULL))
	{
		(void)era_dm_remove(orig);
		(void)era_dm_remove(name);
		goto out;
	}

	if (era_dm_resume(name))
	{
		(void)era_dm_remove(orig);
		(void)era_dm_remove(name);
		goto out;
	}

	free(orig);
	return 0;

out:
	free(orig);
	return -1;
}
int
signature_check2( PKT_signature *sig, MD_HANDLE digest, u32 *r_expiredate, 
		  int *r_expired, int *r_revoked, PKT_public_key *ret_pk )
{
    PKT_public_key *pk = xmalloc_clear( sizeof *pk );
    int rc=0;

    if( (rc=check_digest_algo(sig->digest_algo)) )
      ; /* we don't have this digest */
    else if((rc=check_pubkey_algo(sig->pubkey_algo)))
      ; /* we don't have this pubkey algo */
    else if(!md_algo_present(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=G10ERR_GENERAL;
      }
    else if( get_pubkey( pk, sig->keyid ) )
	rc = G10ERR_NO_PUBKEY;
    else if(!pk->is_valid && !pk->is_primary)
        rc=G10ERR_BAD_PUBKEY; /* you cannot have a good sig from an
				 invalid subkey */
    else
      {
        if(r_expiredate)
	  *r_expiredate = pk->expiredate;

	rc = do_check( pk, sig, digest, r_expired, r_revoked, ret_pk );

	/* 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->is_primary && pk->backsig<2)
	  {
	    if(pk->backsig==0)
	      {
		log_info(_("WARNING: signing subkey %s is not"
			   " cross-certified\n"),keystr_from_pk(pk));
		log_info(_("please see %s for more information\n"),
			 "http://www.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=G10ERR_GENERAL;
	      }
	    else if(pk->backsig==1)
	      {
		log_info(_("WARNING: signing subkey %s has an invalid"
			   " cross-certification\n"),keystr_from_pk(pk));
		rc=G10ERR_GENERAL;
	      }
	  }
      }

    free_public_key( 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 */
	MD_HANDLE md;
	u32 a = sig->timestamp;
	int i, nsig = pubkey_get_nsig( sig->pubkey_algo );
	byte *p, *buffer;

	md = md_open( DIGEST_ALGO_RMD160, 0);
	md_putc( digest, sig->pubkey_algo );
	md_putc( digest, sig->digest_algo );
	md_putc( digest, (a >> 24) & 0xff );
	md_putc( digest, (a >> 16) & 0xff );
	md_putc( digest, (a >>	8) & 0xff );
	md_putc( digest,  a	   & 0xff );
	for(i=0; i < nsig; i++ ) {
	    unsigned n = mpi_get_nbits( sig->data[i]);

	    md_putc( md, n>>8);
	    md_putc( md, n );
	    p = mpi_get_buffer( sig->data[i], &n, NULL );
	    md_write( md, p, n );
	    xfree(p);
	}
	md_final( md );
	p = make_radix64_string( md_read( md, 0 ), 20 );
	buffer = xmalloc( strlen(p) + 60 );
	sprintf( buffer, "%s %s %lu",
		 p, strtimestamp( sig->timestamp ), (ulong)sig->timestamp );
	write_status_text( STATUS_SIG_ID, buffer );
	xfree(buffer);
	xfree(p);
	md_close(md);
    }