Exemplo n.º 1
0
END_TEST

/*******************************************************************************
 * parse_integer_uint64
 */

START_TEST(test_asn1_parse_integer_uint64)
{
	typedef struct {
		u_int64_t n;
		chunk_t chunk;
	} testdata_t;


	testdata_t test[] = {
		{             67305985ULL, chunk_from_chars(
						0x04, 0x03, 0x02, 0x01) },
		{   578437695752307201ULL, chunk_from_chars(
						0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01) },
		{ 18446744073709551615ULL, chunk_from_chars(
						0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff) }
	};

	int i;

	for (i = 0; i < countof(test); i++)
	{
		ck_assert(asn1_parse_integer_uint64(test[i].chunk) == test[i].n);
	}
}
Exemplo n.º 2
0
END_TEST

/*******************************************************************************
 * known_oid
 */

START_TEST(test_asn1_known_oid)
{
	typedef struct {
		int n;
		chunk_t oid;
	} testdata_t;

	testdata_t test[] = {
		{ OID_UNKNOWN,    chunk_empty },
		{ OID_UNKNOWN,    chunk_from_chars(0x55, 0x04, 0x02) },
		{ OID_COUNTRY,    chunk_from_chars(0x55, 0x04, 0x06) },
		{ OID_STRONGSWAN, chunk_from_chars(0x2b, 0x06, 0x01, 0x04, 0x01,
										   0x82, 0xa0, 0x2a, 0x01) }
	};

	int i;

	for (i = 0; i < countof(test); i++)
	{
		ck_assert(asn1_known_oid(test[i].oid) == test[i].n);
	}
}
Exemplo n.º 3
0
END_TEST

/*******************************************************************************
 * htoun/untoh
 */

START_TEST(test_htoun)
{
	chunk_t net64, expected;
	uint16_t host16 = 513;
	uint32_t net16 = 0, host32 = 67305985;
	uint64_t net32 = 0, host64 = 578437695752307201ULL;

	net64 = chunk_alloca(16);
	memset(net64.ptr, 0, net64.len);

	expected = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
	htoun16((char*)&net16 + 1, host16);
	ck_assert(chunk_equals(expected, chunk_from_thing(net16)));

	expected = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
	htoun32((uint16_t*)&net32 + 1, host32);
	ck_assert(chunk_equals(expected, chunk_from_thing(net32)));

	expected = chunk_from_chars(0x00, 0x00, 0x00, 0x00,
								0x08, 0x07, 0x06, 0x05,
								0x04, 0x03, 0x02, 0x01,
								0x00, 0x00, 0x00, 0x00);
	htoun64((uint32_t*)net64.ptr + 1, host64);
	ck_assert(chunk_equals(expected, net64));
}
Exemplo n.º 4
0
END_TEST

START_TEST(test_skip)
{
	chunk_t skipped, buf, data = chunk_from_chars(0x00, 0x01, 0x02);
	bio_writer_t *writer;

	writer = bio_writer_create(4);
	skipped = writer->skip(writer, 3);
	ck_assert_int_eq(skipped.len, 3);
	buf = writer->get_buf(writer);
	ck_assert(skipped.ptr == buf.ptr);
	memset(skipped.ptr, 0, skipped.len);

	writer->write_data(writer, data);
	buf = writer->get_buf(writer);
	ck_assert(chunk_equals(buf, chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x01, 0x02)));
	writer->destroy(writer);

	writer = bio_writer_create(1);
	skipped = writer->skip(writer, 3);
	memcpy(skipped.ptr, data.ptr, data.len);

	writer->write_data(writer, data);
	assert_writer_after_write(writer, 2);
	writer->destroy(writer);
}
Exemplo n.º 5
0
END_TEST

/*******************************************************************************
 * parse_time
 */

START_TEST(test_asn1_parse_time)
{
	typedef struct {
		time_t time;
		chunk_t chunk;
	} testdata_t;

	testdata_t test[] = {
		{ 352984, chunk_from_chars(
					0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
					0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
		{ 352984, chunk_from_chars(
					0x17, 0x0d, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
					0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
		{      0, chunk_from_chars(0x05, 0x00) }
	};

	int i;

	for (i = 0; i < countof(test); i++)
	{
		ck_assert(asn1_parse_time(test[i].chunk, 0) == test[i].time);
	}
}
Exemplo n.º 6
0
END_TEST

START_TEST(test_memxor_aligned)
{
	uint64_t a = 0, b = 0;
	chunk_t ca, cb;
	int i;

	ca = chunk_from_thing(a);
	cb = chunk_from_thing(b);

	for (i = 0; i < 8; i++)
	{
		cb.ptr[i] = i + 1;
	}

	/* 64-bit aligned */
	memxor(ca.ptr, cb.ptr, 8);
	ck_assert(a == b);
	/* 32-bit aligned source */
	a = 0;
	memxor(ca.ptr, cb.ptr + 4, 4);
	ck_assert(chunk_equals(ca, chunk_from_chars(0x05, 0x06, 0x07, 0x08,
												0x00, 0x00, 0x00, 0x00)));
	/* 16-bit aligned source */
	a = 0;
	memxor(ca.ptr, cb.ptr + 2, 6);
	ck_assert(chunk_equals(ca, chunk_from_chars(0x03, 0x04, 0x05, 0x06,
												0x07, 0x08, 0x00, 0x00)));
	/* 8-bit aligned source */
	a = 0;
	memxor(ca.ptr, cb.ptr + 1, 7);
	ck_assert(chunk_equals(ca, chunk_from_chars(0x02, 0x03, 0x04, 0x05,
												0x06, 0x07, 0x08, 0x00)));
}
Exemplo n.º 7
0
END_TEST

/*******************************************************************************
 * parse_algorithm_identifier
 */

START_TEST(test_asn1_parse_algorithmIdentifier)
{
	typedef struct {
		int alg;
		bool empty;
		chunk_t parameters;
	} testdata_t;

	testdata_t test[] = {
		{ OID_ECDSA_WITH_SHA1, TRUE,  chunk_empty },
		{ OID_SHA1_WITH_RSA,   TRUE,  chunk_from_chars(0x05, 0x00) },
		{ OID_3DES_EDE_CBC,    FALSE, chunk_from_chars(0x04, 0x01, 0xaa) },
		{ OID_PBKDF2,          FALSE, chunk_from_chars(0x30, 0x01, 0xaa) }
	};

	chunk_t algid, parameters;
	int i, alg;

	for (i = 0; i < countof(test); i++)
	{
		algid = asn1_wrap(ASN1_SEQUENCE, "mc",
					 asn1_build_known_oid(test[i].alg), test[i].parameters);
		parameters = chunk_empty;
		if (i == 2)
		{
			alg = asn1_parse_algorithmIdentifier(algid, 0, NULL);
		}
		else
		{
			alg = asn1_parse_algorithmIdentifier(algid, 0, &parameters);
			if (test[i].empty)
			{
				ck_assert(parameters.len == 0 && parameters.ptr == NULL);
			}
				else
			{
				ck_assert(chunk_equals(parameters, test[i].parameters));
			}
		}
		ck_assert(alg == test[i].alg);
		chunk_free(&algid);
	}
}
Exemplo n.º 8
0
/**
 * Described in header.
 */
char* path_dirname(const char *path)
{
	char *pos;

	pos = path ? strrchr(path, DIRECTORY_SEPARATOR[0]) : NULL;

	if (pos && !pos[1])
	{	/* if path ends with slashes we have to look beyond them */
		while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
		{	/* skip trailing slashes */
			pos--;
		}
		pos = memrchr(path, DIRECTORY_SEPARATOR[0], pos - path + 1);
	}
	if (!pos)
	{
#ifdef WIN32
		if (path && strlen(path))
		{
			if ((isalpha(path[0]) && path[1] == ':'))
			{	/* if just a drive letter given, return that as dirname */
				return chunk_clone(chunk_from_chars(path[0], ':', 0)).ptr;
			}
		}
#endif
		return strdup(".");
	}
	while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
	{	/* skip superfluous slashes */
		pos--;
	}
	return strndup(path, pos - path + 1);
}
Exemplo n.º 9
0
END_TEST

START_TEST(test_create_from_dns_v6)
{
	test_create_from_dns(AF_INET6,
						 chunk_from_chars(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1));
}
Exemplo n.º 10
0
/**
 * Common test wrapper function for different test variants
 */
static void test_tls(tls_version_t version, uint16_t port, bool cauth, u_int i)
{
	echo_server_config_t *config;
	tls_cipher_suite_t *suites;
	char suite[128];
	int count;

	INIT(config,
		.version = version,
		.addr = "127.0.0.1",
		.port = port,
		.cauth = cauth,
		.data = chunk_from_chars(0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08),
	);

	start_echo_server(config);

	count = tls_crypto_get_supported_suites(TRUE, &suites);

	ck_assert(i < count);
	snprintf(suite, sizeof(suite), "%N", tls_cipher_suite_names, suites[i]);
	lib->settings->set_str(lib->settings, "%s.tls.suites", suite, lib->ns);

	run_echo_client(config);

	free(suites);

	shutdown(config->fd, SHUT_RDWR);
	close(config->fd);

	free(config);
}
Exemplo n.º 11
0
END_TEST

START_TEST(test_newhope_ke_fail_r)
{
    diffie_hellman_t *r_nh;
    char buf_ff[1824];
    int i;

    chunk_t i_msg[] = {
        chunk_empty,
        chunk_from_chars(0x00),
        chunk_create(buf_ff, 1823),
        chunk_create(buf_ff, 1824),
    };

    memset(buf_ff, 0xff, sizeof(buf_ff));

    for (i = 0; i < countof(i_msg); i++)
    {
        r_nh = lib->crypto->create_dh(lib->crypto, NH_128_BIT);
        ck_assert(r_nh != NULL);
        ck_assert(!r_nh->set_other_public_value(r_nh, i_msg[i]));
        r_nh->destroy(r_nh);
    }
}
Exemplo n.º 12
0
END_TEST

START_TEST(test_newhope_ke_fail_i)
{
    diffie_hellman_t *i_nh;
    char buf_ff[2048];
    int i;

    chunk_t i_msg;

    chunk_t r_msg[] = {
        chunk_empty,
        chunk_from_chars(0x00),
        chunk_create(buf_ff, 2047),
        chunk_create(buf_ff, 2048),
    };

    memset(buf_ff, 0xff, sizeof(buf_ff));

    for (i = 0; i < countof(r_msg); i++)
    {
        i_nh = lib->crypto->create_dh(lib->crypto, NH_128_BIT);
        ck_assert(i_nh != NULL);
        ck_assert(i_nh->get_my_public_value(i_nh, &i_msg));
        ck_assert(!i_nh->set_other_public_value(i_nh, r_msg[i]));
        chunk_free(&i_msg);
        i_nh->destroy(i_nh);
    }
}
Exemplo n.º 13
0
END_TEST

/*******************************************************************************
 * simple_object
 */

START_TEST(test_asn1_simple_object)
{
	chunk_t a = chunk_empty;
	chunk_t b = chunk_from_chars(0x04, 0x05, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5);
	chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4, 0xa5);

	a = asn1_simple_object(0x04, c);
	ck_assert(chunk_equals(a, b));
	chunk_free(&a);
}
Exemplo n.º 14
0
END_TEST

/*******************************************************************************
 * unwrap
 */

START_TEST(test_asn1_unwrap)
{
	chunk_t c0 = chunk_from_chars(0x30);
	chunk_t c1 = chunk_from_chars(0x30, 0x01, 0xaa);
	chunk_t c2 = chunk_from_chars(0x30, 0x80);
	chunk_t c3 = chunk_from_chars(0x30, 0x81);
	chunk_t c4 = chunk_from_chars(0x30, 0x81, 0x01, 0xaa);
	chunk_t c5 = chunk_from_chars(0x30, 0x81, 0x02, 0xaa);

	chunk_t inner;
	chunk_t inner_ref = chunk_from_chars(0xaa);

	ck_assert(asn1_unwrap(&c0, &inner) == ASN1_INVALID);

	ck_assert(asn1_unwrap(&c1, &inner) == ASN1_SEQUENCE);

	ck_assert(chunk_equals(inner, inner_ref));

	ck_assert(asn1_unwrap(&c2, &inner) == ASN1_INVALID);

	ck_assert(asn1_unwrap(&c3, &inner) == ASN1_INVALID);

	ck_assert(asn1_unwrap(&c4, &inner) == ASN1_SEQUENCE);

	ck_assert(chunk_equals(inner, inner_ref));

	ck_assert(asn1_unwrap(&c5, &inner) == ASN1_INVALID);
}
END_TEST

/*******************************************************************************
 * different integer reads from the end of a buffer
 */

#define assert_integer_read_end(data, bits, val) ({ \
	bio_reader_t *reader = bio_reader_create(data); \
	typeof(val) i; \
	for (i = 0; reader->remaining(reader) >= (bits / 8); i++) \
	{ \
		ck_assert(reader->read_uint##bits##_end(reader, &val)); \
		ck_assert_int_eq(i, val); \
	} \
	ck_assert_int_eq(i, data.len / (bits / 8)); \
	ck_assert_int_eq(reader->remaining(reader), data.len % (bits / 8)); \
	ck_assert(!reader->read_uint##bits##_end(reader, &val)); \
	reader->destroy(reader); \
})

#define assert_integer_read_end_uneven(data, bits, val) ({ \
	int i; \
	data.ptr += bits / 8; \
	for (i = 0; i <= bits / 8; i++, data.ptr--, data.len++) \
	{ \
		assert_integer_read_end(data, bits, val); \
	} \
})

#define assert_basic_read_end(bits, val) ({ \
	chunk_t data; \
	data = chunk_empty; \
	assert_integer_read_end(data, bits, val); \
	data = chunk_alloca(bits / 8); \
	memset(data.ptr, 0, data.len); \
	data.len = 0; \
	assert_integer_read_end_uneven(data, bits, val); \
})

#define assert_extended_read_end(data, bits, val) ({ \
	chunk_t extended = chunk_alloca(data.len + bits / 8); \
	memset(extended.ptr, 0, extended.len); \
	extended.ptr[bits / 8 - 1] = data.len / (bits / 8); \
	memcpy(extended.ptr + bits / 8, data.ptr, data.len); \
	extended.len = data.len; \
	assert_integer_read_end_uneven(extended, bits, val); \
})

START_TEST(test_read_uint8_end)
{
	chunk_t data = chunk_from_chars(0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00);
	uint8_t val;

	assert_integer_read_end(data, 8, val);
	assert_basic_read_end(8, val);
	assert_extended_read_end(data, 8, val);
}
Exemplo n.º 16
0
END_TEST

/*******************************************************************************
 * from_time
 */

START_TEST(test_asn1_from_time)
{
	typedef struct {
		time_t time;
		u_int8_t type;
		chunk_t chunk;
	} testdata_t;

	testdata_t test[] = {
		{       352984, 0x18, chunk_from_chars(
						0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
						0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
		{       352984, 0x17, chunk_from_chars(
						0x17, 0x0d, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
						0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
		{   1078099200, 0x17, chunk_from_chars(
						0x17, 0x0d, 0x30, 0x34, 0x30, 0x33, 0x30, 0x31,
						0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a) },
		{ 4107542400UL, 0x18, chunk_from_chars(
						0x18, 0x0f, 0x32, 0x31, 0x30, 0x30, 0x30, 0x33, 0x30, 0x31,
						0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a) }
	};

	int i;
	chunk_t chunk;

	for (i = 0; i < countof(test); i++)
	{
		if (sizeof(time_t) == 4 && test[i].time < 0)
		{
			continue;
		}
		chunk = asn1_from_time(&test[i].time, test[i].type);
		ck_assert(chunk_equals(chunk, test[i].chunk));
		free(chunk.ptr);
	}
}
END_TEST

START_TEST(test_read_uint24)
{
	chunk_t data = chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x03);
	uint32_t val;

	assert_integer_read(data, 24, val);
	assert_basic_read(24, val);
	assert_extended_read(data, 24, val);
}
END_TEST

START_TEST(test_read_uint16_end)
{
	chunk_t data = chunk_from_chars(0x00, 0x03, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00);
	uint16_t val;

	assert_integer_read_end(data, 16, val);
	assert_basic_read_end(16, val);
	assert_extended_read_end(data, 16, val);
}
Exemplo n.º 19
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)));
}
Exemplo n.º 20
0
END_TEST

/*******************************************************************************
 * bitstring
 */

START_TEST(test_asn1_bitstring)
{
	chunk_t a = chunk_empty;
	chunk_t b = chunk_from_chars(0x03, 0x05, 0x00, 0xa1, 0xa2, 0xa3, 0xa4);
	chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4);
	chunk_t d = chunk_clone(c);

	a = asn1_bitstring("c", c);
	ck_assert(chunk_equals(a, b));
	chunk_free(&a);

	a = asn1_bitstring("m", d);
	ck_assert(chunk_equals(a, b));
	chunk_free(&a);
}
Exemplo n.º 21
0
END_TEST

/*******************************************************************************
 * is_asn1
 */

START_TEST(test_is_asn1)
{
	typedef struct {
		bool asn1;
		chunk_t chunk;
	} testdata_t;

	u_char buf[8];
	chunk_t chunk_zero = { buf, 0 };
	chunk_t chunk_mean = {   0, 1 };

	testdata_t test[] = {
		{ FALSE, chunk_zero },
		{ FALSE, chunk_empty },
		{ FALSE, chunk_mean },
		{ TRUE,  chunk_from_chars(0x30, 0x00) },
		{ TRUE,  chunk_from_chars(0x31, 0x00) },
		{ TRUE,  chunk_from_chars(0x04, 0x00) },
		{ FALSE, chunk_from_chars(0x02, 0x00) },
		{ FALSE, chunk_from_chars(0x30, 0x01) },
		{ FALSE, chunk_from_chars(0x30, 0x80) },
		{ TRUE,  chunk_from_chars(0x30, 0x01, 0xa1) },
		{ FALSE, chunk_from_chars(0x30, 0x01, 0xa1, 0xa2) },
		{ TRUE,  chunk_from_chars(0x30, 0x01, 0xa1, 0x0a) },
		{ FALSE, chunk_from_chars(0x30, 0x01, 0xa1, 0xa2, 0x0a) },
	};

	int i;

	for (i = 0; i < countof(test); i++)
	{
		ck_assert(is_asn1(test[i].chunk) == test[i].asn1);
	}
}
Exemplo n.º 22
0
END_TEST

START_TEST(test_untoh)
{
	chunk_t net;
	uint16_t host16;
	uint32_t host32;
	uint64_t host64;

	net = chunk_from_chars(0x00, 0x02, 0x01, 0x00);
	host16 = untoh16(net.ptr + 1);
	ck_assert(host16 == 513);

	net = chunk_from_chars(0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00);
	host32 = untoh32(net.ptr + 2);
	ck_assert(host32 == 67305985);

	net = chunk_from_chars(0x00, 0x00, 0x00, 0x00, 0x08, 0x07, 0x06, 0x05,
						   0x04, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00);
	host64 = untoh64(net.ptr + 4);
	ck_assert(host64 == 578437695752307201ULL);
}
Exemplo n.º 23
0
END_TEST

/*******************************************************************************
 * build_known_oid
 */

START_TEST(test_asn1_build_known_oid)
{
	typedef struct {
		int n;
		chunk_t oid;
	} testdata_t;

	testdata_t test[] = {
		{ OID_UNKNOWN,    chunk_empty },
		{ OID_MAX,        chunk_empty },
		{ OID_COUNTRY,    chunk_from_chars(0x06, 0x03, 0x55, 0x04, 0x06) },
		{ OID_STRONGSWAN, chunk_from_chars(0x06, 0x09, 0x2b, 0x06, 0x01, 0x04,
										   0x01, 0x82, 0xa0, 0x2a, 0x01) }
	};

	int i;
	chunk_t oid = chunk_empty;

	for (i = 0; i < countof(test); i++)
	{
		oid = asn1_build_known_oid(test[i].n);
		if (test[i].oid.len == 0)
		{
			ck_assert(oid.len == 0 && oid.ptr == NULL);
		}
		else
		{
			ck_assert(chunk_equals(oid, test[i].oid));
			chunk_free(&oid);
		}
	}
}
Exemplo n.º 24
0
END_TEST

START_TEST(test_chunk_from_fd_skt)
{
	chunk_t in, contents = chunk_from_chars(0x01,0x02,0x03,0x04,0x05);
	int s[2];

	ck_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0);
	ck_assert(write(s[1], contents.ptr, contents.len) == contents.len);
	close(s[1]);
	ck_assert_msg(chunk_from_fd(s[0], &in), "%s", strerror(errno));
	close(s[0]);
	ck_assert_msg(chunk_equals(in, contents), "%B", &in);
	free(in.ptr);
}
Exemplo n.º 25
0
END_TEST

/*******************************************************************************
 * oid_to_string
 */

START_TEST(test_asn1_oid_to_string)
{
	typedef struct {
		char *string;
		chunk_t oid;
	} testdata_t;

	testdata_t test[] = {
		{  NULL,  chunk_empty },
		{ "0.2.262.1", chunk_from_chars(
			0x02, 0x82, 0x06, 0x01) },
		{ "1.2.840.10045.4.1", chunk_from_chars(
			0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
		{ "1.3.6.1.4.1.36906.1", chunk_from_chars(
			0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
		{ "2.16.840.1.101.3.4.2.1", chunk_from_chars(
			0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
		{ "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
			chunk_from_chars( 0x0a, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
			0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
			0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
		{ NULL, chunk_from_chars(
			0x0a, 0x02, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d, 0x20,
			0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00, 0xaf, 0xd7, 0xc2, 0x00,
		    0xff, 0xff, 0xff, 0x7f) },
		{ NULL, chunk_from_chars(0x0a, 0x87) }
	};

	int i;
	char *string = NULL;

	for (i = 0; i < countof(test); i++)
	{
		string = asn1_oid_to_string(test[i].oid);
		if (test[i].string == NULL)
		{
			ck_assert(string == NULL);
		}
		else
		{
			ck_assert(streq(string, test[i].string));
			free(string);
		}
	}
}
END_TEST

START_TEST(test_read_data_end)
{
	chunk_t read, data = chunk_from_chars(0x00, 0x00, 0x00, 0x00);
	bio_reader_t *reader;

	reader = bio_reader_create(chunk_empty);
	ck_assert_int_eq(reader->remaining(reader), 0);
	ck_assert(reader->read_data_end(reader, 0, &read));
	ck_assert(!reader->read_data_end(reader, 1, &read));
	reader->destroy(reader);

	reader = bio_reader_create(data);
	ck_assert(reader->read_data_end(reader, 0, &read));
	ck_assert_int_eq(read.len, 0);
	ck_assert(read.ptr == data.ptr + data.len);
	assert_reader_after_read(reader, data);

	ck_assert(reader->read_data_end(reader, 1, &read));
	ck_assert_int_eq(read.len, 1);
	data.len--;
	ck_assert(read.ptr == data.ptr + data.len);
	assert_reader_after_read(reader, data);

	ck_assert(reader->read_data_end(reader, 2, &read));
	ck_assert_int_eq(read.len, 2);
	data.len -= 2;
	ck_assert(read.ptr == data.ptr + data.len);
	assert_reader_after_read(reader, data);

	ck_assert(!reader->read_data(reader, 2, &read));
	ck_assert(reader->read_data(reader, 1, &read));
	ck_assert_int_eq(read.len, 1);
	ck_assert(read.ptr == data.ptr);
	assert_reader_after_read(reader, chunk_empty);

	ck_assert_int_eq(reader->remaining(reader), 0);
	ck_assert(reader->read_data(reader, 0, &read));
	ck_assert(!reader->read_data(reader, 1, &read));
	reader->destroy(reader);
}
Exemplo n.º 27
0
/**
 * Perform a signature verification "good" test having a keypair
 */
static void test_good_sig(private_key_t *privkey, public_key_t *pubkey)
{
    chunk_t sig, data = chunk_from_chars(0x01,0x02,0x03,0xFD,0xFE,0xFF);
    int i;

    for (i = 0; i < countof(schemes); i++)
    {
        if (!lib->plugins->has_feature(lib->plugins,
                                       PLUGIN_PROVIDE(PUBKEY_VERIFY, schemes[i])) ||
                !lib->plugins->has_feature(lib->plugins,
                                           PLUGIN_PROVIDE(PRIVKEY_SIGN, schemes[i])))
        {
            continue;
        }
        fail_unless(privkey->sign(privkey, schemes[i], data, &sig),
                    "sign %N", signature_scheme_names, schemes[i]);
        fail_unless(pubkey->verify(pubkey, schemes[i], data, sig),
                    "verify %N", signature_scheme_names, schemes[i]);
        free(sig.ptr);
    }
}
Exemplo n.º 28
0
/**
 * Check public key that it properly fails against some crafted sigs
 */
static void test_bad_sigs(public_key_t *pubkey)
{
    chunk_t data = chunk_from_chars(0x01,0x02,0x03,0xFD,0xFE,0xFF);
    int s, i;

    for (s = 0; s < countof(schemes); s++)
    {
        if (!lib->plugins->has_feature(lib->plugins,
                                       PLUGIN_PROVIDE(PUBKEY_VERIFY, schemes[s])))
        {
            continue;
        }
        for (i = 0; i < countof(invalid_sigs); i++)
        {
            fail_if(
                pubkey->verify(pubkey, schemes[s], data, invalid_sigs[i]),
                "bad %N sig accepted %B", signature_scheme_names, schemes[s],
                &invalid_sigs[i]);
        }
    }
}
Exemplo n.º 29
0
END_TEST

/*******************************************************************************
 * test for chunk_from_fd
 */

START_TEST(test_chunk_from_fd_file)
{
	chunk_t in, contents = chunk_from_chars(0x01,0x02,0x03,0x04,0x05);
	char *path = "/tmp/strongswan-chunk-fd-test";
	int fd;

	ck_assert(chunk_write(contents, path, 022, TRUE));

	fd = open(path, O_RDONLY);
	ck_assert(fd != -1);

	ck_assert(chunk_from_fd(fd, &in));
	close(fd);
	ck_assert_msg(chunk_equals(in, contents), "%B", &in);
	unlink(path);
	free(in.ptr);
}
Exemplo n.º 30
0
END_TEST

/*******************************************************************************
 * test for chunk_map and friends
 */

START_TEST(test_chunk_map)
{
	chunk_t *map, contents = chunk_from_chars(0x01,0x02,0x03,0x04,0x05);
	char *path = "/tmp/strongswan-chunk-map-test";

	ck_assert(chunk_write(contents, path, 022, TRUE));

	/* read */
	map = chunk_map(path, FALSE);
	ck_assert(map != NULL);
	ck_assert_msg(chunk_equals(*map, contents), "%B", map);
	/* altering mapped chunk should not hurt */
	*map = chunk_empty;
	ck_assert(chunk_unmap(map));

	/* write */
	map = chunk_map(path, TRUE);
	ck_assert(map != NULL);
	ck_assert_msg(chunk_equals(*map, contents), "%B", map);
	map->ptr[0] = 0x06;
	ck_assert(chunk_unmap(map));

	/* verify write */
	contents.ptr[0] = 0x06;
	map = chunk_map(path, FALSE);
	ck_assert(map != NULL);
	ck_assert_msg(chunk_equals(*map, contents), "%B", map);
	ck_assert(chunk_unmap(map));

	unlink(path);
}