/**
 * @given transaction field values and sample command values, reference tx
 * @when create transaction with sample command using transaction builder
 * @then transaction is built correctly
 */
TEST(ProtoTransaction, Builder) {
  iroha::protocol::Transaction proto_tx = generateEmptyTransaction();

  std::string account_id = "admin@test", asset_id = "coin#test",
              amount = "10.00";
  auto command =
      proto_tx.mutable_payload()->add_commands()->mutable_add_asset_quantity();

  command->CopyFrom(generateAddAssetQuantity(account_id, asset_id));

  auto keypair =
      shared_model::crypto::CryptoProviderEd25519Sha3::generateKeypair();
  auto signedProto = shared_model::crypto::CryptoSigner<>::sign(
      shared_model::crypto::Blob(proto_tx.payload().SerializeAsString()),
      keypair);

  auto sig = proto_tx.add_signatures();
  sig->set_pubkey(shared_model::crypto::toBinaryString(keypair.publicKey()));
  sig->set_signature(shared_model::crypto::toBinaryString(signedProto));

  auto tx = shared_model::proto::TransactionBuilder()
                .creatorAccountId(creator_account_id)
                .addAssetQuantity(account_id, asset_id, amount)
                .createdTime(created_time)
                .quorum(1)
                .build();

  auto signedTx = tx.signAndAddSignature(keypair).finish();
  auto &proto = signedTx.getTransport();

  ASSERT_EQ(proto_tx.SerializeAsString(), proto.SerializeAsString());
}
Esempio n. 2
0
    bool BlocksQuery::addSignature(const crypto::Signed &signed_blob,
                                   const crypto::PublicKey &public_key) {
      if (proto_->has_signature()) {
        return false;
      }

      auto sig = proto_->mutable_signature();
      sig->set_signature(crypto::toBinaryString(signed_blob));
      sig->set_pubkey(crypto::toBinaryString(public_key));
      return true;
    }
Esempio n. 3
0
int main(int argc, char **argv)
{
	struct mkswap_control ctl = { .fd = -1 };
	int c;
	uint64_t sz;
	int version = SWAP_VERSION;
	char *block_count = NULL, *strsz = NULL;
#ifdef HAVE_LIBUUID
	const char *opt_uuid = NULL;
	uuid_t uuid_dat;
#endif
	static const struct option longopts[] = {
		{ "check",       no_argument,       0, 'c' },
		{ "force",       no_argument,       0, 'f' },
		{ "pagesize",    required_argument, 0, 'p' },
		{ "label",       required_argument, 0, 'L' },
		{ "swapversion", required_argument, 0, 'v' },
		{ "uuid",        required_argument, 0, 'U' },
		{ "version",     no_argument,       0, 'V' },
		{ "help",        no_argument,       0, 'h' },
		{ NULL,          0, 0, 0 }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while((c = getopt_long(argc, argv, "cfp:L:v:U:Vh", longopts, NULL)) != -1) {
		switch (c) {
		case 'c':
			ctl.check = 1;
			break;
		case 'f':
			ctl.force = 1;
			break;
		case 'p':
			ctl.user_pagesize = strtou32_or_err(optarg, _("parsing page size failed"));
			break;
		case 'L':
			ctl.opt_label = optarg;
			break;
		case 'v':
			version = strtos32_or_err(optarg, _("parsing version number failed"));
			if (version != SWAP_VERSION)
				errx(EXIT_FAILURE,
					_("swapspace version %d is not supported"), version);
			break;
		case 'U':
#ifdef HAVE_LIBUUID
			opt_uuid = optarg;
#else
			warnx(_("warning: ignoring -U (UUIDs are unsupported by %s)"),
				program_invocation_short_name);
#endif
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
		case 'h':
			usage(stdout);
		default:
			usage(stderr);
		}
	}

	if (optind < argc)
		ctl.devname = argv[optind++];
	if (optind < argc)
		block_count = argv[optind++];
	if (optind != argc) {
		warnx(_("only one device argument is currently supported"));
		usage(stderr);
	}

#ifdef HAVE_LIBUUID
	if(opt_uuid) {
		if (uuid_parse(opt_uuid, uuid_dat) != 0)
			errx(EXIT_FAILURE, _("error: parsing UUID failed"));
	} else
		uuid_generate(uuid_dat);
	ctl.uuid = uuid_dat;
#endif

	init_signature_page(&ctl);	/* get pagesize and allocate signature page */

	if (!ctl.devname) {
		warnx(_("error: Nowhere to set up swap on?"));
		usage(stderr);
	}
	if (block_count) {
		/* this silly user specified the number of blocks explicitly */
		uint64_t blks = strtou64_or_err(block_count,
					_("invalid block count argument"));
		ctl.npages = blks / (ctl.pagesize / 1024);
	}

	sz = get_size(&ctl);
	if (!ctl.npages)
		ctl.npages = sz;
	else if (ctl.npages > sz && !ctl.force)
		errx(EXIT_FAILURE,
			_("error: "
			  "size %llu KiB is larger than device size %ju KiB"),
			ctl.npages * (ctl.pagesize / 1024), sz * (ctl.pagesize / 1024));

	if (ctl.npages < MIN_GOODPAGES)
		errx(EXIT_FAILURE,
		     _("error: swap area needs to be at least %ld KiB"),
		     (long)(MIN_GOODPAGES * ctl.pagesize / 1024));
	if (ctl.npages > UINT32_MAX) {
		/* true when swap is bigger than 17.59 terabytes */
		ctl.npages = UINT32_MAX;
		warnx(_("warning: truncating swap area to %llu KiB"),
			ctl.npages * ctl.pagesize / 1024);
	}

	if (is_mounted(ctl.devname))
		errx(EXIT_FAILURE, _("error: "
			"%s is mounted; will not make swapspace"),
			ctl.devname);

	open_device(&ctl);

	if (ctl.check)
		check_blocks(&ctl);

	wipe_device(&ctl);

	assert(ctl.hdr);
	ctl.hdr->version = version;
	ctl.hdr->last_page = ctl.npages - 1;
	ctl.hdr->nr_badpages = ctl.nbadpages;

	if ((ctl.npages - MIN_GOODPAGES) < ctl.nbadpages)
		errx(EXIT_FAILURE, _("Unable to set up swap-space: unreadable"));

	sz = (ctl.npages - ctl.nbadpages - 1) * ctl.pagesize;
	strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, sz);

	printf(_("Setting up swapspace version %d, size = %s (%ju bytes)\n"),
		version, strsz, sz);
	free(strsz);

	set_signature(&ctl);
	set_uuid_and_label(&ctl);

	write_header_to_device(&ctl);

	deinit_signature_page(&ctl);

#ifdef HAVE_LIBSELINUX
	if (S_ISREG(ctl.devstat.st_mode) && is_selinux_enabled() > 0) {
		security_context_t context_string;
		security_context_t oldcontext;
		context_t newcontext;

		if (fgetfilecon(ctl.fd, &oldcontext) < 0) {
			if (errno != ENODATA)
				err(EXIT_FAILURE,
					_("%s: unable to obtain selinux file label"),
					ctl.devname);
			if (matchpathcon(ctl.devname, ctl.devstat.st_mode, &oldcontext))
				errx(EXIT_FAILURE, _("unable to matchpathcon()"));
		}
		if (!(newcontext = context_new(oldcontext)))
			errx(EXIT_FAILURE, _("unable to create new selinux context"));
		if (context_type_set(newcontext, SELINUX_SWAPFILE_TYPE))
			errx(EXIT_FAILURE, _("couldn't compute selinux context"));

		context_string = context_str(newcontext);

		if (strcmp(context_string, oldcontext)!=0) {
			if (fsetfilecon(ctl.fd, context_string))
				err(EXIT_FAILURE, _("unable to relabel %s to %s"),
						ctl.devname, context_string);
		}
		context_free(newcontext);
		freecon(oldcontext);
	}
#endif
	/*
	 * A subsequent swapon() will fail if the signature
	 * is not actually on disk. (This is a kernel bug.)
	 * The fsync() in close_fd() will take care of writing.
	 */
	if (close_fd(ctl.fd) != 0)
		err(EXIT_FAILURE, _("write failed"));
	return EXIT_SUCCESS;
}
Esempio n. 4
0
int main(
    int argc,
    char **argv)
{
    /*
     * Args are: file TBS, keyfile, [update]
     */
    struct Certificate cert;
    Certificate(&cert, (ushort) 0);
    struct CertificateRevocationList crl;
    CertificateRevocationList(&crl, (ushort) 0);
    struct Blob blob;
    Blob(&blob, (ushort) 0);
    struct AlgorithmIdentifier *algp,
       *tbsalgp;
    struct casn *casnp,
       *sigp,
       *selfp;
    const char *keyfile = NULL;

    OPEN_LOG("sign_cert", LOG_USER);

    if (argc < 3)
        FATAL(MSG_USAGE);
    char *sfx = strrchr(argv[1], (int)'.');
    keyfile = argv[2];
    if (!strcmp(sfx, ".cer"))
    {
        selfp = &cert.self;
        casnp = &cert.toBeSigned.self;
        tbsalgp = &cert.toBeSigned.signature;
        sigp = &cert.signature;
        algp = &cert.algorithm;
    }
    else if (!strcmp(sfx, ".crl"))
    {
        selfp = &crl.self;
        casnp = &crl.toBeSigned.self;
        tbsalgp = &crl.toBeSigned.signature;
        sigp = &crl.signature;
        algp = &crl.algorithm;
    }
    else if (!strcmp(sfx, ".blb"))
    {
        selfp = &blob.self;
        casnp = &blob.toBeSigned;
        tbsalgp = NULL;
        sigp = &blob.signature;
        algp = &blob.algorithm;
    }
    else
    {
        FATAL(MSG_UNK_EXT, argv[1]);
    }
    if (get_casn_file(selfp, argv[1], 0) < 0)
        FATAL(MSG_OPEN, argv[1]);
    if (argv[3] && (*argv[3] & 1))
    {
        if (!strcmp(sfx, ".cer"))
            adjust_time(&cert.toBeSigned.validity.notBefore.utcTime,
                        &cert.toBeSigned.validity.notAfter.utcTime);
        else if (!strcmp(sfx, ".crl"))
            adjust_time((struct casn *)&crl.toBeSigned.lastUpdate,
                        (struct casn *)&crl.toBeSigned.nextUpdate);
    }
    if (tbsalgp && (!argv[3] || !(*argv[3] & 2)))
    {
        write_objid(&tbsalgp->algorithm, id_sha_256WithRSAEncryption);
        write_casn(&tbsalgp->parameters.rsadsi_SHA256_WithRSAEncryption,
                   (uchar *) "", 0);
    }
    if (!set_signature(casnp, sigp, keyfile, "label", "password", false))
    {
        FATAL(MSG_IN, "set_signature()");
    }
    if (!argv[3] || !(*argv[3] & 4))
    {
        write_objid(&algp->algorithm, id_sha_256WithRSAEncryption);
        write_casn(&algp->parameters.rsadsi_SHA256_WithRSAEncryption,
                   (uchar *) "", 0);
    }
    put_casn_file(selfp, argv[1], 0);
    DONE(MSG_OK, argv[1]);
    return 0;
}