diff --git a/tests/unittests/tests-credman/Makefile.include b/tests/unittests/tests-credman/Makefile.include index 58aba54c52..b9851e279d 100644 --- a/tests/unittests/tests-credman/Makefile.include +++ b/tests/unittests/tests-credman/Makefile.include @@ -1 +1,2 @@ USEMODULE += credman +USEMODULE += credman_load diff --git a/tests/unittests/tests-credman/tests-credman.c b/tests/unittests/tests-credman/tests-credman.c index c73180a6b6..6ec8420747 100644 --- a/tests/unittests/tests-credman/tests-credman.c +++ b/tests/unittests/tests-credman/tests-credman.c @@ -15,6 +15,86 @@ #define CREDMAN_TEST_TAG (1) +/* $ openssl ecparam -name secp256r1 -genkey -outform der -out key.der */ +static const uint8_t key_pair_der[] = { + 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x04, + 0x43, 0x81, 0x68, 0xa8, 0x63, 0x9e, 0xf7, 0xe3, + 0x0d, 0x40, 0x5c, 0xf1, 0xea, 0xc9, 0x90, 0xf6, + 0x5f, 0x63, 0x86, 0x29, 0xca, 0x20, 0x4f, 0x9f, + 0x34, 0xb1, 0x9d, 0xa5, 0x17, 0x7b, 0x92, 0xa0, + 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, + 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, + 0x04, 0x95, 0x38, 0x6c, 0x0e, 0x45, 0xd3, 0x15, + 0x22, 0xad, 0x29, 0xd5, 0x24, 0xd8, 0x6b, 0x29, + 0xf7, 0x5c, 0x7b, 0x71, 0x89, 0xac, 0xa2, 0x86, + 0xb5, 0x9b, 0xdc, 0x00, 0x28, 0xa4, 0xae, 0xd8, + 0x9f, 0x8b, 0xe8, 0x63, 0xab, 0xd2, 0x30, 0x3c, + 0x06, 0xcd, 0xec, 0x8a, 0xf7, 0xb9, 0xf8, 0xb4, + 0x46, 0xca, 0x01, 0x55, 0x03, 0xe1, 0xbe, 0x99, + 0xf7, 0x13, 0x13, 0x78, 0xbd, 0x40, 0xf9, 0xd4, + 0x9c +}; + +/* $ openssl pkcs8 -topk8 -inform DER -outform DER -in key.der -out key_pkcs8.der -nocrypt */ +static const uint8_t key_pair_pkcs8_der[] = { + 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, + 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, + 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, + 0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, + 0x01, 0x01, 0x04, 0x20, 0x04, 0x43, 0x81, 0x68, + 0xa8, 0x63, 0x9e, 0xf7, 0xe3, 0x0d, 0x40, 0x5c, + 0xf1, 0xea, 0xc9, 0x90, 0xf6, 0x5f, 0x63, 0x86, + 0x29, 0xca, 0x20, 0x4f, 0x9f, 0x34, 0xb1, 0x9d, + 0xa5, 0x17, 0x7b, 0x92, 0xa1, 0x44, 0x03, 0x42, + 0x00, 0x04, 0x95, 0x38, 0x6c, 0x0e, 0x45, 0xd3, + 0x15, 0x22, 0xad, 0x29, 0xd5, 0x24, 0xd8, 0x6b, + 0x29, 0xf7, 0x5c, 0x7b, 0x71, 0x89, 0xac, 0xa2, + 0x86, 0xb5, 0x9b, 0xdc, 0x00, 0x28, 0xa4, 0xae, + 0xd8, 0x9f, 0x8b, 0xe8, 0x63, 0xab, 0xd2, 0x30, + 0x3c, 0x06, 0xcd, 0xec, 0x8a, 0xf7, 0xb9, 0xf8, + 0xb4, 0x46, 0xca, 0x01, 0x55, 0x03, 0xe1, 0xbe, + 0x99, 0xf7, 0x13, 0x13, 0x78, 0xbd, 0x40, 0xf9, + 0xd4, 0x9c +}; + +/* $ openssl ec -in key.der -inform DER -pubout -outform DER -out pub.der */ +static const uint8_t public_key_der[] = { + 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, + 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, + 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, + 0x42, 0x00, 0x04, 0x95, 0x38, 0x6c, 0x0e, 0x45, + 0xd3, 0x15, 0x22, 0xad, 0x29, 0xd5, 0x24, 0xd8, + 0x6b, 0x29, 0xf7, 0x5c, 0x7b, 0x71, 0x89, 0xac, + 0xa2, 0x86, 0xb5, 0x9b, 0xdc, 0x00, 0x28, 0xa4, + 0xae, 0xd8, 0x9f, 0x8b, 0xe8, 0x63, 0xab, 0xd2, + 0x30, 0x3c, 0x06, 0xcd, 0xec, 0x8a, 0xf7, 0xb9, + 0xf8, 0xb4, 0x46, 0xca, 0x01, 0x55, 0x03, 0xe1, + 0xbe, 0x99, 0xf7, 0x13, 0x13, 0x78, 0xbd, 0x40, + 0xf9, 0xd4, 0x9c +}; + +/* $ openssl ec -in key.der -inform DER -text */ +static const uint8_t private_key[] = { + 0x04, 0x43, 0x81, 0x68, 0xa8, 0x63, 0x9e, 0xf7, + 0xe3, 0x0d, 0x40, 0x5c, 0xf1, 0xea, 0xc9, 0x90, + 0xf6, 0x5f, 0x63, 0x86, 0x29, 0xca, 0x20, 0x4f, + 0x9f, 0x34, 0xb1, 0x9d, 0xa5, 0x17, 0x7b, 0x92 +}; + +static const uint8_t public_key_x[] = { + 0x95, 0x38, 0x6c, 0x0e, 0x45, 0xd3, 0x15, 0x22, + 0xad, 0x29, 0xd5, 0x24, 0xd8, 0x6b, 0x29, 0xf7, + 0x5c, 0x7b, 0x71, 0x89, 0xac, 0xa2, 0x86, 0xb5, + 0x9b, 0xdc, 0x00, 0x28, 0xa4, 0xae, 0xd8, 0x9f +}; + +static const uint8_t public_key_y[] = { + 0x8b, 0xe8, 0x63, 0xab, 0xd2, 0x30, 0x3c, 0x06, + 0xcd, 0xec, 0x8a, 0xf7, 0xb9, 0xf8, 0xb4, 0x46, + 0xca, 0x01, 0x55, 0x03, 0xe1, 0xbe, 0x99, 0xf7, + 0x13, 0x13, 0x78, 0xbd, 0x40, 0xf9, 0xd4, 0x9c +}; + static int _compare_credentials(const credman_credential_t *a, const credman_credential_t *b) { @@ -228,6 +308,46 @@ static void test_credman_add_delete_all(void) TEST_ASSERT_EQUAL_INT(2, credman_get_used_count()); } +static void test_credman_load_public_key_from_buffer(void) +{ + ecdsa_public_key_t pub; + + int res = credman_load_public_key(public_key_der, sizeof(public_key_der), &pub); + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, res); + TEST_ASSERT_EQUAL_INT(memcmp(public_key_x, pub.x, sizeof(public_key_x)), 0); + TEST_ASSERT_EQUAL_INT(memcmp(public_key_y, pub.y, sizeof(public_key_y)), 0); +} + +static void test_credman_load_private_key_from_buffer(void) +{ + credman_credential_t cred; + + int res = credman_load_private_key(key_pair_pkcs8_der, sizeof(key_pair_pkcs8_der), &cred); + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, res); + TEST_ASSERT_EQUAL_INT(memcmp(private_key, cred.params.ecdsa.private_key, sizeof(private_key)), + 0); + TEST_ASSERT_EQUAL_INT(memcmp(public_key_x, cred.params.ecdsa.public_key.x, + sizeof(public_key_x)), 0); + TEST_ASSERT_EQUAL_INT(memcmp(public_key_y, cred.params.ecdsa.public_key.y, + sizeof(public_key_y)), 0); + TEST_ASSERT_EQUAL_INT(cred.type, CREDMAN_TYPE_ECDSA); +} + +static void test_credman_load_private_ecc_key_from_buffer(void) +{ + credman_credential_t cred; + + int res = credman_load_private_ecc_key(key_pair_der, sizeof(key_pair_der), &cred); + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, res); + TEST_ASSERT_EQUAL_INT(memcmp(private_key, cred.params.ecdsa.private_key, sizeof(private_key)), + 0); + TEST_ASSERT_EQUAL_INT(memcmp(public_key_x, cred.params.ecdsa.public_key.x, + sizeof(public_key_x)), 0); + TEST_ASSERT_EQUAL_INT(memcmp(public_key_y, cred.params.ecdsa.public_key.y, + sizeof(public_key_y)), 0); + TEST_ASSERT_EQUAL_INT(cred.type, CREDMAN_TYPE_ECDSA); +} + Test *tests_credman_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures) { @@ -236,6 +356,9 @@ Test *tests_credman_tests(void) new_TestFixture(test_credman_delete), new_TestFixture(test_credman_delete_random_order), new_TestFixture(test_credman_add_delete_all), + new_TestFixture(test_credman_load_public_key_from_buffer), + new_TestFixture(test_credman_load_private_key_from_buffer), + new_TestFixture(test_credman_load_private_ecc_key_from_buffer), }; EMB_UNIT_TESTCALLER(credman_tests,