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);
}
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);
}
Example #3
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;
}