/* * This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "zm_catch2.h" #include "zm_crypt.h" TEST_CASE("JWT validation") { std::string key = "testsecret-at-least-32-bytes-long"; SECTION("Valid token") { std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJab25lTWluZGVyIiwidXNlciI6ImpvaG5kb2UiLCJ0eXBlIjoiYWNjZXNzIiwiaWF0IjoxMjM0fQ.hSW6iU0Y-IUNlYpJ0sRwjo0jKXWPZHoM3FVvNNLAE7U"; std::pair result = verifyToken(token, key); REQUIRE(result.first == "johndoe"); REQUIRE(result.second == 1234); } SECTION("Invalid signature") { std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJab25lTWluZGVyIiwidXNlciI6ImpvaG5kb2UiLCJ0eXBlIjoiYWNjZXNzIiwiaWF0IjoxMjM0fQ.LU8uZkxFND3E44mRz3m4Cm67aABFd8GRYk58tM5Yvww"; std::pair result = verifyToken(token, key); REQUIRE(result.first == ""); REQUIRE(result.second == 0); } SECTION("Missing user claim") { std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJab25lTWluZGVyIiwidHlwZSI6ImFjY2VzcyIsImlhdCI6MTIzNH0.f4gXwEyo-R--h0lDsZAUai8_Rgq3cw_7ajU5o7vhuEk"; std::pair result = verifyToken(token, key); REQUIRE(result.first == ""); REQUIRE(result.second == 0); } SECTION("Missing type claim") { std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJab25lTWluZGVyIiwidXNlciI6ImpvaG5kb2UiLCJpYXQiOjEyMzR9.0VZ2Pj8eL2q688kgZB8UsoEeq0Y-Uo544NPgOgFPYwE"; std::pair result = verifyToken(token, key); REQUIRE(result.first == ""); REQUIRE(result.second == 0); } SECTION("Wrong type claim") { std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJab25lTWluZGVyIiwidXNlciI6ImpvaG5kb2UiLCJ0eXBlIjoid3JvbmciLCJpYXQiOjEyMzR9.q04wqlXmKZ4obB4NqrEuFIM17C0EiM96UJEdN64GCc4"; std::pair result = verifyToken(token, key); REQUIRE(result.first == ""); REQUIRE(result.second == 0); } SECTION("Missing iat claim") { std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJab25lTWluZGVyIiwidXNlciI6ImpvaG5kb2UiLCJ0eXBlIjoid3JvbmcifQ.EmENxsHBJTZM6sb1N50-ekTJfwpoI-sgMvxNTLM4RFs"; std::pair result = verifyToken(token, key); REQUIRE(result.first == ""); REQUIRE(result.second == 0); } } TEST_CASE("zm::crypto::MD5") { using namespace zm::crypto; MD5 md5; REQUIRE(md5.GetDigest() == MD5::Digest()); SECTION("hash from const char*") { md5.UpdateData("abcdefghijklmnopqrstuvwxyz"); md5.Finalize(); REQUIRE(md5.GetDigest() == MD5::Digest{0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b}); } SECTION("hash from std::string") { md5.UpdateData(std::string("abcdefghijklmnopqrstuvwxyz")); md5.Finalize(); REQUIRE(md5.GetDigest() == MD5::Digest{0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b}); } } TEST_CASE("zm::crypto::MD5::GetDigestOf") { using namespace zm::crypto; std::array data = {'a', 'b', 'c'}; SECTION("data and len") { MD5::Digest digest = MD5::GetDigestOf(reinterpret_cast(data.data()), data.size()); REQUIRE(digest == MD5::Digest{0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72}); } SECTION("container") { MD5::Digest digest = MD5::GetDigestOf(data); REQUIRE(digest == MD5::Digest{0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72}); } SECTION("multiple containers") { MD5::Digest digest = MD5::GetDigestOf(data, data); REQUIRE(digest == MD5::Digest{0x44, 0x0a, 0xc8, 0x58, 0x92, 0xca, 0x43, 0xad, 0x26, 0xd4, 0x4c, 0x7a, 0xd9, 0xd4, 0x7d, 0x3e}); } } TEST_CASE("zm::crypto::SHA1::GetDigestOf") { using namespace zm::crypto; std::array data = {'a', 'b', 'c'}; SHA1::Digest digest = SHA1::GetDigestOf(data); REQUIRE(digest == SHA1::Digest{0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d}); } TEST_CASE("zm::crypto::SHA256::GetDigestOf") { using namespace zm::crypto; std::array data = {'a', 'b', 'c'}; SHA256::Digest digest = SHA256::GetDigestOf(data); REQUIRE(digest == SHA256::Digest{0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad}); } TEST_CASE("zm::crypto::LogFingerprint") { using namespace zm::crypto; // First 8 hex digits of SHA-256("abc"); the web side's logFingerprint('abc') // must give the same tag so web and daemon log lines can be matched up. REQUIRE(LogFingerprint("abc") == "fp:ba7816bf"); REQUIRE(LogFingerprint("") == "fp:none"); std::string auth = "0123456789abcdef0123456789abcdef"; std::string fp = LogFingerprint(auth); REQUIRE(fp.size() == 11); REQUIRE(fp.find(auth) == std::string::npos); REQUIRE(LogFingerprint(auth) == fp); REQUIRE(LogFingerprint(auth + "0") != fp); }