#include #include #include #include #include #include #include #include using namespace vanetza::security; #define WRITEABLE(path) WORK_DIR "/" path #define ASSET(path) ASSET_DIR "/" path namespace { const std::array expected_private_key = { 0x53, 0xb7, 0x7e, 0xb8, 0x48, 0x2e, 0x3c, 0x1a, 0xd3, 0x36, 0x70, 0xc0, 0xc6, 0xc4, 0x6b, 0xc0, 0x36, 0x90, 0xd4, 0x00, 0x59, 0xd3, 0xcb, 0xb5, 0x81, 0xb3, 0x36, 0xaf, 0x8a, 0x98, 0x93, 0xf4 }; const std::array expected_public_x = { 0x1c, 0x85, 0x0d, 0xc7, 0x45, 0x63, 0x29, 0x3c, 0xb0, 0xf3, 0xe5, 0x5e, 0xda, 0x7b, 0x10, 0xec, 0xb4, 0xe9, 0x74, 0x6f, 0x83, 0x6f, 0x84, 0x76, 0x96, 0xc3, 0x1e, 0xe8, 0x68, 0x4e, 0x37, 0x76 }; const std::array expected_public_y = { 0x28, 0xf1, 0x67, 0xfb, 0x64, 0xce, 0x7b, 0x79, 0xa6, 0x02, 0x06, 0x2a, 0xac, 0x11, 0x7f, 0x59, 0x6b, 0xac, 0x77, 0xc7, 0x1c, 0xd6, 0xf4, 0xca, 0xa7, 0x08, 0x7a, 0xcc, 0xcd, 0xab, 0x91, 0xab }; void check_key_pair(const ecdsa256::KeyPair& kp) { EXPECT_EQ(kp.private_key.key, expected_private_key); EXPECT_EQ(kp.public_key.x, expected_public_x); EXPECT_EQ(kp.public_key.y, expected_public_y); } ecdsa256::KeyPair make_test_key_pair() { ecdsa256::KeyPair kp; kp.private_key.key = expected_private_key; kp.public_key.x = expected_public_x; kp.public_key.y = expected_public_y; return kp; } std::string read_file_bytes(const std::string& path) { std::ifstream file(path, std::ios::binary); return {std::istreambuf_iterator(file), std::istreambuf_iterator()}; } } // namespace #if defined VANETZA_WITH_OPENSSL || defined VANETZA_WITH_CRYPTOPP TEST(Persistence, load_private_key_from_file) { check_key_pair(v2::load_private_key_from_file(ASSET("test_key.der"))); } TEST(Persistence, save_and_load_pkcs8_der) { auto kp = make_test_key_pair(); const std::string path = WRITEABLE("test_save.der"); std::ofstream ofs(path, std::ios::binary); EXPECT_TRUE(v2::save_private_key_pkcs8_der(ofs, kp)); ofs.flush(); check_key_pair(v2::load_private_key_from_file(path)); std::remove(path.c_str()); } TEST(Persistence, save_der_matches_reference_file) { auto kp = make_test_key_pair(); std::ostringstream oss(std::ios::binary); EXPECT_TRUE(v2::save_private_key_pkcs8_der(oss, kp)); auto reference = read_file_bytes(ASSET("test_key.der")); EXPECT_EQ(oss.str(), reference); } #endif /* VANETZA_WITH_OPENSSL || VANETZA_WITH_CRYPTOPP */