コード例 #1
0
ファイル: bad_inputs_test.c プロジェクト: cedric-d/libcbor
static void test_6(void **state)
{
	item = cbor_load(data6, 5, &res);
	assert_null(item);
	assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
	assert_int_equal(res.error.position, 2);
}
コード例 #2
0
ファイル: iot_service.cpp プロジェクト: tfar/ibce2eiot
void IoTService::authenticateMessage(boost::asio::ip::address_v6 senderAddress, std::vector<uint8_t> data, ec taKey) {
	LOG(INFO) << "CBOR decode message...";

	Signature sig;
	std::vector<uint8_t> message;
	std::array<uint8_t, 16> senderBytes = senderAddress.to_bytes();
	std::vector<uint8_t> senderBytesVec(senderBytes.begin(), senderBytes.end());

	struct cbor_load_result result;
	cbor_item_t* item = cbor_load(data.data(), data.size(), &result);
	size_t pairs = cbor_map_size(item);
	for (cbor_pair* pair = cbor_map_handle(item); pairs > 0; pair++, pairs--) {
		if (strncmp(reinterpret_cast<char*>(cbor_string_handle(pair->key)), "sig", 3) == 0) {
			sig = Signature::fromCBORArray(pair->value);
		}
		else if (strncmp(reinterpret_cast<char*>(cbor_string_handle(pair->key)), "msg", 3) == 0) {
			size_t length = cbor_bytestring_length(pair->value);
			message = std::vector<uint8_t>(cbor_bytestring_handle(pair->value), cbor_bytestring_handle(pair->value) + length);
		}
	}
	LOG(INFO) << "Authenticating message...";
	bool sigCorrect = Signature::verify(senderBytesVec, message, taKey.p, sig);
	if (sigCorrect) {
		LOG(INFO) << "Signature correct:	msg: " << byteVecToStr(message);
	}
	else {
		LOG(INFO) << "Signature invalid.";
	}
}
コード例 #3
0
ファイル: bad_inputs_test.c プロジェクト: cedric-d/libcbor
static void test_2(void **state)
{
	item = cbor_load(data2, 1, &res);
	assert_null(item);
	assert_true(res.error.code == CBOR_ERR_MALFORMATED);
	assert_int_equal(res.error.position, 0);
}
コード例 #4
0
ファイル: bad_inputs_test.c プロジェクト: cedric-d/libcbor
static void test_3(void **state)
{
	item = cbor_load(data3, 1, &res);
	assert_null(item);
	assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
	assert_int_equal(res.error.position, 1);
}
コード例 #5
0
ファイル: bad_inputs_test.c プロジェクト: cedric-d/libcbor
static void test_7(void **state)
{
	item = cbor_load(data7, 16, &res);
	assert_null(item);
	assert_true(res.error.code == CBOR_ERR_MEMERROR);
	assert_int_equal(res.error.position, 10);
}
コード例 #6
0
ファイル: bad_inputs_test.c プロジェクト: cedric-d/libcbor
static void test_5(void **state)
{
	assert_true(res.error.code == CBOR_ERR_MEMERROR);
	item = cbor_load(data5, 6, &res);
	assert_null(item);
	assert_int_equal(res.error.position, 5);
/* Indef string expectation mismatch */
}
コード例 #7
0
ファイル: type_0_test.c プロジェクト: EmuxEvans/libcbor
static void test_refcounting(void **state)
{
	number = cbor_load(data5, 10, &res);
	cbor_incref(number);
	assert_true(number->refcount == 2);
	cbor_decref(&number);
	assert_non_null(number);
	cbor_decref(&number);
	assert_null(number);
}
コード例 #8
0
ファイル: type_2_test.c プロジェクト: cedric-d/libcbor
static void test_empty_bs(void **state)
{
	bs = cbor_load(data1, 2, &res);
	assert_non_null(bs);
	assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
	assert_true(cbor_isa_bytestring(bs));
	assert_int_equal(cbor_bytestring_length(bs), 0);
	assert_true(res.read == 1);
	cbor_decref(&bs);
	assert_null(bs);
}
コード例 #9
0
ファイル: type_2_test.c プロジェクト: cedric-d/libcbor
static void test_long_bs(void **state)
{
	bs = cbor_load(data7, 265, &res);
	assert_non_null(bs);
	assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
	assert_true(cbor_isa_bytestring(bs));
	assert_true(cbor_bytestring_length(bs) == 255);
	assert_true(res.read == 264);
	assert_memory_equal(cbor_bytestring_handle(bs), data7 + 9, 0xFF);
	cbor_decref(&bs);
	assert_null(bs);
}
コード例 #10
0
ファイル: type_2_test.c プロジェクト: cedric-d/libcbor
static void test_zero_indef(void **state)
{
	bs = cbor_load(data9, 2, &res);
	assert_non_null(bs);
	assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
	assert_true(cbor_isa_bytestring(bs));
	assert_true(cbor_bytestring_is_indefinite(bs));
	assert_true(cbor_bytestring_chunk_count(bs) == 0);
	assert_true(res.read == 2);
	cbor_decref(&bs);
	assert_null(bs);
}
コード例 #11
0
ファイル: type_2_test.c プロジェクト: cedric-d/libcbor
static void test_short_bs1(void **state)
{
	bs = cbor_load(data3, 4, &res);
	assert_non_null(bs);
	assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
	assert_true(cbor_isa_bytestring(bs));
	assert_true(cbor_bytestring_length(bs) == 1);
	assert_true(res.read == 3);
	assert_true(*cbor_bytestring_handle(bs) == 0xA1);
	assert_memory_equal(cbor_bytestring_handle(bs), data3 + 2, 1);
	cbor_decref(&bs);
	assert_null(bs);
}
コード例 #12
0
ファイル: type_1_test.c プロジェクト: cedric-d/libcbor
static void test_very_short_int(void **state)
{
	number = cbor_load(data1, 2, &res);
	assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
	assert_true(cbor_int_get_width(number) == CBOR_INT_8);
	assert_false(cbor_isa_uint(number));
	assert_true(cbor_isa_negint(number));
	assert_true(cbor_get_uint8(number) == 2);
	assert_true(res.error.code == 0);
	assert_true(res.read == 1);
	assert_true(cbor_is_int(number));
	cbor_decref(&number);
	assert_null(number);
}
コード例 #13
0
ファイル: type_1_test.c プロジェクト: cedric-d/libcbor
static void test_half_int(void **state)
{
	number = cbor_load(data3, 5, &res);
	assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
	assert_true(cbor_int_get_width(number) == CBOR_INT_16);
	assert_false(cbor_isa_uint(number));
	assert_true(cbor_isa_negint(number));
	assert_true(cbor_get_uint16(number) == 500);
	assert_true(res.error.code == 0);
	assert_true(res.read == 3);
	assert_true(cbor_is_int(number));
	cbor_decref(&number);
	assert_null(number);
}
コード例 #14
0
ファイル: type_1_test.c プロジェクト: cedric-d/libcbor
static void test_int(void **state)
{
	number = cbor_load(data4, 6, &res);
	assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
	assert_true(cbor_int_get_width(number) == CBOR_INT_32);
	assert_false(cbor_isa_uint(number));
	assert_true(cbor_isa_negint(number));
	assert_true(cbor_get_uint32(number) == 2784428723);
	assert_true(res.error.code == 0);
	assert_true(res.read == 5);
	assert_true(cbor_is_int(number));
	cbor_decref(&number);
	assert_null(number);
}
コード例 #15
0
ファイル: type_1_test.c プロジェクト: cedric-d/libcbor
static void test_long_int(void **state)
{
	number = cbor_load(data5, 10, &res);
	assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
	assert_true(cbor_int_get_width(number) == CBOR_INT_64);
	assert_false(cbor_isa_uint(number));
	assert_true(cbor_isa_negint(number));
	assert_true(cbor_get_uint64(number) == 11959030306112471731ULL);
	assert_true(res.error.code == 0);
	assert_true(res.read == 9);
	assert_true(cbor_is_int(number));
	cbor_decref(&number);
	assert_null(number);
}
コード例 #16
0
ファイル: type_2_test.c プロジェクト: cedric-d/libcbor
static void test_short_indef(void **state)
{
	bs = cbor_load(data10, 6, &res);
	assert_non_null(bs);
	assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
	assert_true(cbor_isa_bytestring(bs));
	assert_true(cbor_bytestring_length(bs) == 0);
	assert_true(cbor_bytestring_is_indefinite(bs));
	assert_true(cbor_bytestring_chunk_count(bs) == 1);
	assert_true(res.read == 5);
	assert_true(cbor_isa_bytestring(cbor_bytestring_chunks_handle(bs)[0]));
	assert_true(cbor_bytestring_length(cbor_bytestring_chunks_handle(bs)[0]) == 1);
	assert_true(*cbor_bytestring_handle(cbor_bytestring_chunks_handle(bs)[0]) == 0xA1);
	cbor_decref(&bs);
	assert_null(bs);
}
コード例 #17
0
ファイル: fuzz_test.c プロジェクト: smorin/libcbor
static void run_round()
{
	cbor_item_t *item;
	struct cbor_load_result res;

	size_t length = rand() % MAXLEN + 1;
	unsigned char * data = malloc(length);
	for (size_t i = 0; i < length; i++) {
		data[i] = rand() % 0xFF;
	}

	#ifdef CBOR_PRINT_FUZZ
	printmem(data, length);
	#endif

	item = cbor_load(data, length, &res);

	if (res.error.code == CBOR_ERR_NONE)
		cbor_decref(&item);
	/* Otherwise there should be nothing left behind by the decoder */

	free(data);
}
コード例 #18
0
ファイル: type_0_test.c プロジェクト: EmuxEvans/libcbor
static void test_empty_input(void **state)
{
	number = cbor_load(data5, 0, &res);
	assert_null(number);
	assert_true(res.error.code == CBOR_ERR_NODATA);
}
コード例 #19
0
ファイル: type_2_test.c プロジェクト: cedric-d/libcbor
static void test_missing_indef(void **state)
{
	bs = cbor_load(data12, 3, &res);
	assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
	assert_null(bs);
}
コード例 #20
0
ファイル: type_0_test.c プロジェクト: EmuxEvans/libcbor
static void test_incomplete_data(void **state)
{
	number = cbor_load(data2, 1, &res);
	assert_null(number);
	assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
}
コード例 #21
0
ファイル: network.cpp プロジェクト: tfar/ibce2eiot
void DynamicConfigurationServer::handleRequestReceived(const boost::system::error_code& error, size_t bytes_transferred) {
	LOG(INFO) << "Request received from " << remote_endpoint_;

	LOG(INFO) << "Begin CBOR decoding";
	struct cbor_load_result result;
	cbor_item_t* item = cbor_load((uint8_t*)recv_buffer_.data(), bytes_transferred, &result);

	size_t items = cbor_array_size(item);
	assert(items == 2);

	cbor_item_t* nonceItem = cbor_array_get(item, 0);
	cbor_item_t* ciphertextItem = cbor_array_get(item, 1);

	//cbor_describe(item, stdout);
	//fflush(stdout);
	std::array<uint8_t, 16> nonce;
	memcpy(nonce.data(), cbor_bytestring_handle(nonceItem), 16);

	std::vector<uint8_t> ciphertext;
	ciphertext.resize(cbor_bytestring_length(ciphertextItem));
	memcpy(ciphertext.data(), cbor_bytestring_handle(ciphertextItem), ciphertext.size());

	std::array<uint8_t, 16> requestKey;
	memcpy(requestKey.data(), confRequestKey, 16);

	LOG(INFO) << "End CBOR decoding";

	LOG(INFO) << "Begin NORX decryption";
	std::tuple<bool, std::vector<uint8_t> > plaintext = NORX::decrypt(
		std::vector<uint8_t>(), 
		ciphertext,
		std::vector<uint8_t>(),
		nonce,
		requestKey);
	LOG(INFO) << "END NORX decryption";
	cbor_decref(&item);

	if (std::get<0>(plaintext)) {
		LOG(INFO) << "decryption successful";
		item = cbor_load((uint8_t*)std::get<1>(plaintext).data(), std::get<1>(plaintext).size(), &result);

		//LOG(INFO) << "plaintext: " << byteVecToStr(std::get<1>(plaintext));

		if (strncmp("REQ", (const char*)cbor_string_handle(item), 3) == 0) {
			LOG(INFO) << "received correct request";
			generateCredentialsAndSendResponse(nonce);
		}
		else {
			cbor_describe(item, stdout);
		}
		fflush(stdout);

		/* Deallocate the result */
		cbor_decref(&item);
	}
	else {
		LOG(ERROR) << "failed decrypting dynamic initialisation request.";
		LOG(ERROR) << "ciphertext: " << byteVecToStr(ciphertext);
		LOG(ERROR) << "nonce:      " << byteVecToStr(std::vector<uint8_t>(nonce.begin(), nonce.end()));
	}
	startReceive();
}
コード例 #22
0
ファイル: readfile.c プロジェクト: cedric-d/libcbor
int main(int argc, char * argv[])
{
	if (argc != 2)
		usage();
	FILE * f = fopen(argv[1], "rb");
	if (f == NULL)
		usage();
	fseek(f, 0, SEEK_END);
	size_t length = (size_t)ftell(f);
	fseek(f, 0, SEEK_SET);
	unsigned char * buffer = malloc(length);
	fread(buffer, length, 1, f);

	/* Assuming `buffer` contains `length` bytes of input data */
	struct cbor_load_result result;
	cbor_item_t * item = cbor_load(buffer, length, &result);

	if (result.error.code != CBOR_ERR_NONE) {
		printf("There was an error while reading the input near byte %zu (read %zu bytes in total): ", result.error.position, result.read);
		switch (result.error.code) {
			case CBOR_ERR_MALFORMATED:
			{
				printf("Malformed data\n");
				break;
			}
			case CBOR_ERR_MEMERROR:
			{
				printf("Memory error -- perhaps the input is too large?\n");
				break;
			}
			case CBOR_ERR_NODATA:
			{
				printf("The input is empty\n");
				break;
			}
			case CBOR_ERR_NOTENOUGHDATA:
			{
				printf("Data seem to be missing -- is the input complete?\n");
				break;
			}
			case CBOR_ERR_SYNTAXERROR:
			{
				printf("Syntactically malformed data -- see http://tools.ietf.org/html/rfc7049\n");
				break;
			}
			case CBOR_ERR_NONE:
			{
				// GCC's cheap dataflow analysis gag
				break;
			}
		}
		exit(1);
	}

	/* Pretty-print the result */
	cbor_describe(item, stdout);
	fflush(stdout);
	/* Deallocate the result */
	cbor_decref(&item);

	fclose(f);
}
コード例 #23
0
ファイル: type_2_test.c プロジェクト: cedric-d/libcbor
static void test_notenough_data(void **state)
{
	bs = cbor_load(data3, 2, &res);
	assert_null(bs);
	assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
}