Ejemplo n.º 1
0
END_TEST

/*******************************************************************************
 * chunk_skip[_zero]
 */

START_TEST(test_chunk_skip)
{
	chunk_t foobar, a;

	foobar = chunk_from_str("foobar");
	a = foobar;
	a = chunk_skip(a, 0);
	ck_assert(chunk_equals(a, foobar));
	a = chunk_skip(a, 1);
	ck_assert(chunk_equals(a, chunk_from_str("oobar")));
	a = chunk_skip(a, 2);
	ck_assert(chunk_equals(a, chunk_from_str("bar")));
	a = chunk_skip(a, 3);
	assert_chunk_empty(a);

	a = foobar;
	a = chunk_skip(a, 6);
	assert_chunk_empty(a);

	a = foobar;
	a = chunk_skip(a, 10);
	assert_chunk_empty(a);
}
Ejemplo n.º 2
0
END_TEST

/*******************************************************************************
 * chunk_length
 */

START_TEST(test_chunk_length)
{
	chunk_t a, b, c;
	size_t len;

	a = chunk_empty;
	b = chunk_empty;
	c = chunk_empty;
	len = chunk_length("ccc", a, b, c);
	ck_assert_int_eq(len, 0);

	a = chunk_from_str("foo");
	b = chunk_from_str("bar");
	len = chunk_length("ccc", a, b, c);
	ck_assert_int_eq(len, 6);

	len = chunk_length("zcc", a, b, c);
	ck_assert_int_eq(len, 0);

	len = chunk_length("czc", a, b, c);
	ck_assert_int_eq(len, 3);

	a = chunk_from_str("foo");
	b = chunk_from_str("bar");
	c = chunk_from_str("baz");
	len = chunk_length("ccc", a, b, c);
	ck_assert_int_eq(len, 9);
}
Ejemplo n.º 3
0
/**
 * Parse an EC domain parameter identifier as defined in RFC 5656
 */
static chunk_t parse_ec_identifier(chunk_t identifier)
{
	chunk_t oid = chunk_empty;

	if (chunk_equals(identifier, chunk_from_str("nistp256")))
	{
		oid = asn1_build_known_oid(OID_PRIME256V1);
	}
	else if (chunk_equals(identifier, chunk_from_str("nistp384")))
	{
		oid = asn1_build_known_oid(OID_SECT384R1);
	}
	else if (chunk_equals(identifier, chunk_from_str("nistp521")))
	{
		oid = asn1_build_known_oid(OID_SECT521R1);
	}
	else
	{
		char ascii[64];

		if (snprintf(ascii, sizeof(ascii), "%.*s", (int)identifier.len,
					 identifier.ptr) < sizeof(ascii))
		{
			oid = asn1_wrap(ASN1_OID, "m", asn1_oid_from_string(ascii));
		}
	}
	return oid;
}
Ejemplo n.º 4
0
/**
 * Described in header.
 */
pa_tnc_attr_t* swid_error_create(swid_error_code_t code, u_int32_t request_id,
								 u_int32_t max_attr_size, char *description)
{
	bio_writer_t *writer;
	chunk_t msg_info;
	pa_tnc_attr_t *attr;
	pen_type_t error_code;

	error_code = pen_type_create( PEN_TCG, code);
	writer = bio_writer_create(4);
	writer->write_uint32(writer, request_id);
	if (code == TCG_SWID_RESPONSE_TOO_LARGE)
	{
		writer->write_uint16(writer, max_attr_size);
	}
	if (description)
	{
		writer->write_data(writer, chunk_from_str(description));
	}
	msg_info = writer->get_buf(writer);
	attr = ietf_attr_pa_tnc_error_create(error_code, msg_info);
	writer->destroy(writer);

	return attr;
}
Ejemplo n.º 5
0
/**
 * Callback function to prompt for private key passwords
 */
CALLBACK(password_cb, shared_key_t*,
	char *prompt, shared_key_type_t type,
	identification_t *me, identification_t *other,
	id_match_t *match_me, id_match_t *match_other)
{
	char *pwd;

	if (type != SHARED_PRIVATE_KEY_PASS)
	{
		return NULL;
	}
	pwd = getpass(prompt);
	if (!pwd || strlen(pwd) == 0)
	{
		return NULL;
	}
	if (match_me)
	{
		*match_me = ID_MATCH_PERFECT;
	}
	if (match_other)
	{
		*match_other = ID_MATCH_PERFECT;
	}
	return shared_key_create(type, chunk_clone(chunk_from_str(pwd)));
}
Ejemplo n.º 6
0
/**
 * Write an EC domain parameter identifier as defined in RFC 5656
 */
static void write_ec_identifier(bio_writer_t *writer, char *prefix, int oid,
								chunk_t enc)
{
	char *curve, identifier[128];

	switch (oid)
	{
		case OID_PRIME256V1:
			curve = strdup("nistp256");
			break;
		case OID_SECT384R1:
			curve = strdup("nistp384");
			break;
		case OID_SECT521R1:
			curve = strdup("nistp521");
			break;
		default:
			curve = asn1_oid_to_string(enc);
			break;
	}
	if (curve && snprintf(identifier, sizeof(identifier), "%s%s", prefix,
						  curve) < sizeof(identifier))
	{
		writer->write_data32(writer, chunk_from_str(identifier));
	}
	free(curve);
}
Ejemplo n.º 7
0
END_TEST

/*******************************************************************************
 * chunk_create_cat
 */

START_TEST(test_chunk_create_cat)
{
	chunk_t foo, bar;
	chunk_t a, b, c;
	u_char *ptra, *ptrb;

	foo = chunk_from_str("foo");
	bar = chunk_from_str("bar");

	/* to simplify things we use the chunk_cata macro */

	a = chunk_empty;
	b = chunk_empty;
	c = chunk_cata("cc", a, b);
	ck_assert_int_eq(c.len, 0);
	ck_assert(c.ptr != NULL);

	a = foo;
	b = bar;
	c = chunk_cata("cc", a, b);
	ck_assert_int_eq(c.len, 6);
	ck_assert(chunk_equals(c, chunk_from_str("foobar")));

	a = chunk_clone(foo);
	b = chunk_clone(bar);
	c = chunk_cata("mm", a, b);
	ck_assert_int_eq(c.len, 6);
	ck_assert(chunk_equals(c, chunk_from_str("foobar")));

	a = chunk_clone(foo);
	b = chunk_clone(bar);
	ptra = a.ptr;
	ptrb = b.ptr;
	c = chunk_cata("ss", a, b);
	ck_assert_int_eq(c.len, 6);
	ck_assert(chunk_equals(c, chunk_from_str("foobar")));
	/* check memory area of cleared chunk */
	ck_assert(!chunk_equals(foo, chunk_create(ptra, 3)));
	ck_assert(!chunk_equals(bar, chunk_create(ptrb, 3)));
}
END_TEST

START_TEST(test_create_own)
{
	chunk_t data = chunk_from_str("foobar");
	bio_reader_t *reader;

	data = chunk_clone(data);
	reader = bio_reader_create_own(data);
	reader->destroy(reader);
}
Ejemplo n.º 9
0
END_TEST

START_TEST(test_printable_sanitize)
{
	chunk_t sane, expected;
	bool printable;

	printable = chunk_printable(printable_data[_i].in, &sane, '?');
	ck_assert(printable == printable_data[_i].printable);
	expected = chunk_from_str(printable_data[_i].out);
	ck_assert(chunk_equals(sane, expected));
	chunk_free(&sane);
}
Ejemplo n.º 10
0
END_TEST

START_TEST(test_stress)
{
	vici_dispatcher_t *dispatcher;
	vici_conn_t *conn;
	int count = 0, i, total = 50;

	lib->processor->set_threads(lib->processor, 8);

	dispatcher = vici_dispatcher_create(URI);
	ck_assert(dispatcher);

	dispatcher->manage_event(dispatcher, "test", TRUE);
	dispatcher->manage_event(dispatcher, "dummy", TRUE);

	vici_init();
	conn = vici_connect(URI);
	ck_assert(conn);

	vici_register(conn, "test", event_cb, &count);

	for (i = 0; i < total; i++)
	{
		/* do some event re/deregistration in between */
		ck_assert(vici_register(conn, "dummy", event_cb, NULL) == 0);

		dispatcher->raise_event(dispatcher, "test", 0,
			vici_message_create_from_args(
				 VICI_KEY_VALUE, "key1", chunk_from_str("value1"),
				VICI_END));

		ck_assert(vici_register(conn, "dummy", NULL, NULL) == 0);
	}

	while (count < total)
	{
		usleep(1000);
	}

	vici_disconnect(conn);

	dispatcher->manage_event(dispatcher, "test", FALSE);
	dispatcher->manage_event(dispatcher, "dummy", FALSE);

	lib->processor->cancel(lib->processor);
	dispatcher->destroy(dispatcher);

	vici_deinit();
}
Ejemplo n.º 11
0
END_TEST

/*******************************************************************************
 * to_time
 */

START_TEST(test_asn1_to_time)
{
	typedef struct {
		time_t time;
		u_int8_t type;
		char *string;
	} testdata_t;

	testdata_t test[] = {
		{     352980, 0x18, "197001050203Z" },
		{     352984, 0x18, "19700105020304Z" },
		{     352980, 0x17, "7001050203Z" },
		{     347580, 0x17, "7001050203+0130" },
		{     358380, 0x17, "7001050203-0130" },
		{     352984, 0x17, "700105020304Z" },
		{     347584, 0x17, "700105020304+0130" },
		{     358384, 0x17, "700105020304-0130" },
		{          0, 0x17, "700105020304+01" },
		{          0, 0x17, "700105020304-01" },
		{          0, 0x17, "700105020304" },
		{          0, 0x17, "70010502Z" },
		{          0, 0x17, "7001050203xxZ" },
		{          0, 0x17, "7000050203Z" },
		{          0, 0x17, "7013050203Z" },
		{    5097600, 0x17, "7003010000Z" },
		{   68256000, 0x17, "7203010000Z" },
		{  951868800, 0x17, "0003010000Z" },
		{ 4107542400, 0x18, "210003010000Z" }
	};

	int i;
	chunk_t chunk;

	for (i = 0; i < countof(test); i++)
	{
		if (test[i].time > TIME_32_BIT_SIGNED_MAX && sizeof(time_t) == 4)
		{
			continue;
		}
		chunk = chunk_from_str(test[i].string);
		ck_assert(asn1_to_time(&chunk, test[i].type) == test[i].time);
	}
}
END_TEST

/*******************************************************************************
 * test constructors
 */

START_TEST(test_create)
{
	chunk_t data = chunk_from_str("foobar");
	bio_reader_t *reader;

	data = chunk_clone(data);
	reader = bio_reader_create(data);
	reader->destroy(reader);
	chunk_free(&data);
}
Ejemplo n.º 13
0
END_TEST

START_TEST(test_chunk_skip_zero)
{
	chunk_t foobar, a;

	a = chunk_empty;
	a = chunk_skip_zero(a);
	assert_chunk_empty(a);

	foobar = chunk_from_str("foobar");
	a = foobar;
	a = chunk_skip_zero(a);
	ck_assert(chunk_equals(a, foobar));

	a = chunk_from_chars(0x00, 0xaa, 0xbb, 0xcc);
	a = chunk_skip_zero(a);
	ck_assert(chunk_equals(a, chunk_from_chars(0xaa, 0xbb, 0xcc)));
	a = chunk_skip_zero(a);
	ck_assert(chunk_equals(a, chunk_from_chars(0xaa, 0xbb, 0xcc)));
}
Ejemplo n.º 14
0
END_TEST

/*******************************************************************************
 * is_printablestring
 */

START_TEST(test_asn1_is_printablestring)
{
	typedef struct {
		bool printable;
		char *string;
	} testdata_t;


	testdata_t test[] = {
		{ TRUE,  "" },
		{ TRUE,  "Z" },
		{ FALSE, "Z#" },
		{ FALSE, "&Z" },
		{ FALSE, "Z@z" },
		{ FALSE, "!" },  { FALSE, "*" },  { FALSE, "$" },  { FALSE, "%" },
		{ FALSE, "[" },  { FALSE, "]" },  { FALSE, "{" },  { FALSE, "}" },
		{ FALSE, "|" },  { FALSE, "~" },  { FALSE, "^" },  { FALSE, "_" },
		{ FALSE, "\"" }, { FALSE, "\\" }, { FALSE, "ä" },  { FALSE, "à" },
		{ TRUE,  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
				 "0123456789 '()+,-./:=?" },
	};

	chunk_t chunk;
	int i;

	ck_assert(asn1_is_printablestring(chunk_empty));

	for (i = 0; i < countof(test); i++)
	{
		chunk = chunk_from_str(test[i].string);
		ck_assert(asn1_is_printablestring(chunk) == test[i].printable);
	}
}
Ejemplo n.º 15
0
END_TEST

/*******************************************************************************
 * test for chunk_hash[_inc]()
 */

START_TEST(test_chunk_hash)
{
	chunk_t chunk;
	u_int32_t hash_a, hash_b, hash_c;

	chunk = chunk_from_str("asdf");

	/* output is randomized, so there are no test-vectors we could use */
	hash_a = chunk_hash(chunk);
	hash_b = chunk_hash(chunk);
	ck_assert(hash_a == hash_b);
	hash_b = chunk_hash_inc(chunk, hash_a);
	ck_assert(hash_a != hash_b);
	hash_c = chunk_hash_inc(chunk, hash_a);
	ck_assert(hash_b == hash_c);
}
Ejemplo n.º 16
0
static bool get_next_test_vector(test_vector_t *test)
{
	param_t param = PARAM_UNKNOWN;
	char line[512];

	memset(test, 0, sizeof(test_vector_t));

	while (fgets(line, sizeof(line), ctx.in))
	{
		enumerator_t *enumerator;
		chunk_t value = chunk_empty;
		char *token;
		int i;

		switch (line[0])
		{
			case '\n':
			case '\r':
			case '#':
			case '\0':
				/* copy comments, empty lines etc. directly to the output */
				if (param != PARAM_UNKNOWN)
				{	/* seems we got a complete test vector */
					return TRUE;
				}
				fputs(line, ctx.out);
				continue;
			case '[':
				/* control directives */
				fputs(line, ctx.out);
				if (strpfx(line, "[ENCRYPT]"))
				{
					ctx.decrypt = FALSE;
				}
				else if (strpfx(line, "[DECRYPT]"))
				{
					ctx.decrypt = TRUE;
				}
				else if (strcasepfx(line, "[IVlen = "))
				{
					ctx.ivlen = atoi(line + strlen("[IVlen = "));
				}
				else if (strcasepfx(line, "[Taglen = "))
				{
					ctx.icvlen = atoi(line + strlen("[Taglen = "));
				}
				continue;
			default:
				/* we assume the rest of the lines are PARAM = VALUE pairs*/
				fputs(line, ctx.out);
				break;
		}

		i = 0;
		enumerator = enumerator_create_token(line, "=", " \n\r");
		while (enumerator->enumerate(enumerator, &token))
		{
			switch (i++)
			{
				case 0: /* PARAM */
					param = parse_parameter(token);
					continue;
				case 1: /* VALUE */
					if (param != PARAM_UNKNOWN && param != PARAM_COUNT)
					{
						value = chunk_from_hex(chunk_from_str(token), NULL);
					}
					else
					{
						value = chunk_empty;
					}
					continue;
				default:
					break;
			}
			break;
		}
		enumerator->destroy(enumerator);
		if (i < 2)
		{
			value = chunk_empty;
		}
		switch (param)
		{
			case PARAM_KEY:
				test->key = value;
				break;
			case PARAM_IV:
				test->iv = value;
				test->external_iv = TRUE;
				break;
			case PARAM_PLAINTEXT:
				test->plain = value;
				break;
			case PARAM_CIPHERTEXT:
				test->cipher = value;
				break;
			case PARAM_AAD:
				test->aad = value;
				break;
			case PARAM_ICV:
				test->icv = value;
				break;
			default:
				chunk_free(&value);
				break;
		}
	}
	if (param != PARAM_UNKNOWN)
	{	/* could be that the file ended with a complete test vector */
		return TRUE;
	}
	return FALSE;
}
Ejemplo n.º 17
0
/**
 * Encode the public key as Base64 encoded SSH key blob
 */
static bool build_public_key(chunk_t *encoding, va_list args)
{
	bio_writer_t *writer;
	chunk_t n, e;

	if (cred_encoding_args(args, CRED_PART_RSA_MODULUS, &n,
						   CRED_PART_RSA_PUB_EXP, &e, CRED_PART_END))
	{
		writer = bio_writer_create(0);
		writer->write_data32(writer, chunk_from_str("ssh-rsa"));

		writer->write_data32(writer, e);
		writer->write_data32(writer, n);
		*encoding = chunk_to_base64(writer->get_buf(writer), NULL);
		writer->destroy(writer);
		return TRUE;
	}
	else if (cred_encoding_args(args, CRED_PART_EDDSA_PUB_ASN1_DER, &n,
								CRED_PART_END))
	{
		chunk_t alg;
		char *prefix;
		int oid;

		/* parse subjectPublicKeyInfo */
		if (asn1_unwrap(&n, &n) != ASN1_SEQUENCE)
		{
			return FALSE;
		}
		oid = asn1_parse_algorithmIdentifier(n, 1, NULL);
		switch (oid)
		{
			case OID_ED25519:
				prefix = "ssh-ed25519";
				break;
			case OID_ED448:
				prefix = "ssh-ed448";
				break;
			default:
				return FALSE;
		}
		if (asn1_unwrap(&n, &alg) != ASN1_SEQUENCE ||
			asn1_unwrap(&n, &n) != ASN1_BIT_STRING || !n.len)
		{
			return FALSE;
		}
		writer = bio_writer_create(0);
		writer->write_data32(writer, chunk_from_str(prefix));
		writer->write_data32(writer, chunk_skip(n, 1));
		*encoding = chunk_to_base64(writer->get_buf(writer), NULL);
		writer->destroy(writer);
		return TRUE;
	}
	else if (cred_encoding_args(args, CRED_PART_ECDSA_PUB_ASN1_DER, &n,
								CRED_PART_END))
	{
		chunk_t params, alg, q;
		int oid;

		/* parse subjectPublicKeyInfo */
		if (asn1_unwrap(&n, &n) != ASN1_SEQUENCE)
		{
			return FALSE;
		}
		oid = asn1_parse_algorithmIdentifier(n, 1, &params);
		if (oid != OID_EC_PUBLICKEY ||
			asn1_unwrap(&params, &params) != ASN1_OID)
		{
			return FALSE;
		}
		oid = asn1_known_oid(params);
		if (oid == OID_UNKNOWN)
		{
			return FALSE;
		}
		if (asn1_unwrap(&n, &alg) != ASN1_SEQUENCE ||
			asn1_unwrap(&n, &q) != ASN1_BIT_STRING)
		{
			return FALSE;
		}
		writer = bio_writer_create(0);
		write_ec_identifier(writer, ECDSA_PREFIX, oid, params);
		write_ec_identifier(writer, "", oid, params);

		q = chunk_skip_zero(q);
		writer->write_data32(writer, q);
		*encoding = chunk_to_base64(writer->get_buf(writer), NULL);
		writer->destroy(writer);
		return TRUE;
	}
	return FALSE;
}
Ejemplo n.º 18
0
/**
 * Load a generic public key from an SSH key blob
 */
static sshkey_public_key_t *parse_public_key(chunk_t blob)
{
	bio_reader_t *reader;
	chunk_t format;

	reader = bio_reader_create(blob);
	if (!reader->read_data32(reader, &format))
	{
		DBG1(DBG_LIB, "invalid key format in SSH key");
		reader->destroy(reader);
		return NULL;
	}
	if (chunk_equals(format, chunk_from_str("ssh-rsa")))
	{
		chunk_t n, e;

		if (!reader->read_data32(reader, &e) ||
			!reader->read_data32(reader, &n))
		{
			DBG1(DBG_LIB, "invalid RSA key in SSH key");
			reader->destroy(reader);
			return NULL;
		}
		reader->destroy(reader);
		return lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
						BUILD_RSA_MODULUS, n, BUILD_RSA_PUB_EXP, e, BUILD_END);
	}
	else if (format.len > strlen(ECDSA_PREFIX) &&
			 strneq(format.ptr, ECDSA_PREFIX, strlen(ECDSA_PREFIX)))
	{
		chunk_t ec_blob, identifier, q, oid, encoded;
		sshkey_public_key_t *key;

		ec_blob = reader->peek(reader);
		reader->destroy(reader);
		reader = bio_reader_create(ec_blob);
		if (!reader->read_data32(reader, &identifier) ||
			!reader->read_data32(reader, &q))
		{
			DBG1(DBG_LIB, "invalid ECDSA key in SSH key");
			reader->destroy(reader);
			return NULL;
		}
		oid = parse_ec_identifier(identifier);
		if (!oid.ptr)
		{
			DBG1(DBG_LIB, "invalid ECDSA key identifier in SSH key");
			reader->destroy(reader);
			return NULL;
		}
		reader->destroy(reader);
		/* build key from subjectPublicKeyInfo */
		encoded = asn1_wrap(ASN1_SEQUENCE, "mm",
						asn1_wrap(ASN1_SEQUENCE, "mm",
							asn1_build_known_oid(OID_EC_PUBLICKEY), oid),
						asn1_bitstring("c", q));
		key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY,
							KEY_ECDSA, BUILD_BLOB_ASN1_DER, encoded, BUILD_END);
		chunk_free(&encoded);
		return key;
	}
	DBG1(DBG_LIB, "unsupported SSH key format %.*s", (int)format.len,
		 format.ptr);
	reader->destroy(reader);
	return NULL;
}
Ejemplo n.º 19
0
int main(int argc, char *argv[])
{
	char *address = NULL, *identity = "%any", *secret = NULL;
	int port = PT_TLS_PORT;

	init();

	while (TRUE)
	{
		struct option long_opts[] = {
			{"help",		no_argument,			NULL,		'h' },
			{"connect",		required_argument,		NULL,		'c' },
			{"client",		required_argument,		NULL,		'i' },
			{"secret",		required_argument,		NULL,		's' },
			{"port",		required_argument,		NULL,		'p' },
			{"cert",		required_argument,		NULL,		'x' },
			{"key",			required_argument,		NULL,		'k' },
			{"mutual",		no_argument,			NULL,		'm' },
			{"quiet",		no_argument,			NULL,		'q' },
			{"debug",		required_argument,		NULL,		'd' },
			{"optionsfrom",	required_argument,		NULL,		'+' },
			{0,0,0,0 }
		};
		switch (getopt_long(argc, argv, "", long_opts, NULL))
		{
			case EOF:
				break;
			case 'h':			/* --help */
				usage(stdout);
				return 0;
			case 'x':			/* --cert <file> */
				if (!load_certificate(optarg))
				{
					return 1;
				}
				continue;
			case 'k':			/* --key <file> */
				if (!load_key(optarg))
				{
					return 1;
				}
				continue;
			case 'c':			/* --connect <hostname|address> */
				if (address)
				{
					usage(stderr);
					return 1;
				}
				address = optarg;
				continue;
			case 'i':			/* --client <client-id> */
				identity = optarg;
				continue;
			case 's':			/* --secret <password> */
				secret = optarg;
				continue;
			case 'p':			/* --port <port> */
				port = atoi(optarg);
				continue;
			case 'm':			/* --mutual */
				lib->settings->set_bool(lib->settings,
								"%s.plugins.tnccs-20.mutual", TRUE, lib->ns);
				continue;
			case 'q':       	/* --quiet */
				log_to_stderr = FALSE;
				continue;
			case 'd':			/* --debug <level> */
				default_loglevel = atoi(optarg);
				continue;
			case '+':			/* --optionsfrom <filename> */
				if (!options->from(options, optarg, &argc, &argv, optind))
				{
					return 1;
				}
				continue;
			default:
				usage(stderr);
				return 1;
		}
		break;
	}
	if (!address)
	{
		usage(stderr);
		return 1;
	}
	if (secret)
	{
		creds->add_shared(creds, shared_key_create(SHARED_EAP,
										chunk_clone(chunk_from_str(secret))),
							identification_create_from_string(identity), NULL);
	}

	return client(address, port, identity);
}
Ejemplo n.º 20
0
/**
 * Add ITA Device ID attribute to the send queue
 */
static void add_device_id(imc_msg_t *msg)
{
	pa_tnc_attr_t *attr;
	chunk_t value = chunk_empty, keyid;
	char *name, *device_id, *cert_path;
	certificate_t *cert = NULL;
	public_key_t *pubkey;

	/* Get the device ID as a character string */
	device_id = lib->settings->get_str(lib->settings,
						"%s.plugins.imc-os.device_id", NULL, lib->ns);
	if (device_id)
	{
		value = chunk_clone(chunk_from_str(device_id));
	}

	if (value.len == 0)
	{
		/* Derive the device ID from a raw public key */
		cert_path = lib->settings->get_str(lib->settings,
							"%s.plugins.imc-os.device_pubkey", NULL, lib->ns);
		if (cert_path)
		{
			cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
									  CERT_TRUSTED_PUBKEY, BUILD_FROM_FILE,
									  cert_path, BUILD_END);
			if (cert)
			{
				DBG2(DBG_IMC, "loaded device public key from '%s'", cert_path);
			}
			else
			{
				DBG1(DBG_IMC, "loading device public key from '%s' failed",
							   cert_path);
			}
		}

		if (!cert)
		{
			/* Derive the device ID from the public key contained in a certificate */
			cert_path = lib->settings->get_str(lib->settings,
								"%s.plugins.imc-os.device_cert", NULL, lib->ns);
			if (cert_path)
			{
				cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
										  CERT_X509, BUILD_FROM_FILE,
										  cert_path, BUILD_END);
				if (cert)
				{
					DBG2(DBG_IMC, "loaded device certificate from '%s'", cert_path);
				}
				else
				{
					DBG1(DBG_IMC, "loading device certificate from '%s' failed",
								   cert_path);
				}
			}
		}

		/* Compute the SHA-1 keyid of the retrieved device public key */
		if (cert)
		{
			pubkey = cert->get_public_key(cert);
			if (pubkey)
			{
				if (pubkey->get_fingerprint(pubkey, KEYID_PUBKEY_INFO_SHA1,
											&keyid))
				{
					value = chunk_to_hex(keyid, NULL, FALSE);
				}
				pubkey->destroy(pubkey);
			}
			cert->destroy(cert);
		}
	}

	if (value.len == 0)
	{
		/* Derive the device ID from some unique OS settings */
		name = os->get_type(os) == OS_TYPE_ANDROID ?
					  "android_id" : "/var/lib/dbus/machine-id";
		value = os->get_setting(os, name);

		/* Trim trailing newline character */
		if (value.len > 0 && value.ptr[value.len - 1] == '\n')
		{
			value.len--;
		}
	}

	if (value.len == 0)
	{
		DBG1(DBG_IMC, "no device ID available");
		return;
	}

	DBG1(DBG_IMC, "device ID is %.*s", value.len, value.ptr);
	attr = ita_attr_device_id_create(value);
	msg->add_attribute(msg, attr);
	free(value.ptr);
}
Ejemplo n.º 21
0
END_TEST

/*******************************************************************************
 * to_time
 */

START_TEST(test_asn1_to_time)
{
	typedef struct {
		time_t time;
		u_int8_t type;
		char *string;
	} testdata_t;

	testdata_t test[] = {
		{       352980, 0x18, "197001050203Z" },
		{       352984, 0x18, "19700105020304Z" },
		{       352980, 0x17, "7001050203Z" },
		{       347580, 0x17, "7001050203+0130" },
		{       358380, 0x17, "7001050203-0130" },
		{       352984, 0x17, "700105020304Z" },
		{       347584, 0x17, "700105020304+0130" },
		{       358384, 0x17, "700105020304-0130" },
		{            0, 0x17, "700105020304+01" },
		{            0, 0x17, "700105020304-01" },
		{            0, 0x17, "700105020304" },
		{            0, 0x17, "70010502Z" },
		{            0, 0x17, "7001050203xxZ" },
		{            0, 0x17, "7000050203Z" },
		{            0, 0x17, "7013050203Z" },
		{            0, 0x17, "7001004203Z" },
		{            0, 0x17, "7001320203Z" },
		{            0, 0x17, "700101-103Z" },
		{            0, 0x17, "7001016003Z" },
		{            0, 0x17, "70010102-1Z" },
		{            0, 0x17, "7001010260Z" },
		{            0, 0x17, "7001010203-1Z" },
		{            0, 0x17, "700101020361Z" },
		{   -631152000, 0x17, "500101000000Z" }, /* UTCTime min */
		{           59, 0x17, "691231235959-0001" },
		{           -1, 0x17, "691231235959Z" },
		{            0, 0x17, "700101000000Z" },
		{          -60, 0x17, "700101000000+0001" },
		{ 2524607999UL, 0x17, "491231235959Z" }, /* UTCTime max */
		{      5097600, 0x17, "7003010000Z" },
		{     68256000, 0x17, "7203010000Z" },
		{    951868800, 0x17, "0003010000Z" },
		{ 4107542400UL, 0x18, "210003010000Z" }
	};

	int i;
	chunk_t chunk;

	for (i = 0; i < countof(test); i++)
	{
		if (sizeof(time_t) == 4 && test[i].time < 0)
		{
			continue;
		}
		chunk = chunk_from_str(test[i].string);
		ck_assert(asn1_to_time(&chunk, test[i].type) == test[i].time);
	}
}