53 lines
3.3 KiB
C++
53 lines
3.3 KiB
C++
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
// The OpenSSL helpers (Nostro:Crypto): keys made, written and read back, signatures made and checked, including every
|
||
|
|
// way a signature can fail to verify, and the error checks every OpenSSL call goes through.
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Nostro;
|
||
|
|
|
||
|
|
#include "../Support/Check.inc"
|
||
|
|
|
||
|
|
using namespace Nostro;
|
||
|
|
using Test::Check;
|
||
|
|
using Test::Equal;
|
||
|
|
using Test::Throws;
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
const auto [made, publicPem] = Crypto::CreateKey();
|
||
|
|
const Crypto::Key key = Crypto::OwnKey(made);
|
||
|
|
const Crypto::Key other = Crypto::OwnKey(std::get<0>(Crypto::CreateKey()));
|
||
|
|
Check(publicPem.starts_with("-----BEGIN PUBLIC KEY-----"), "CreateKey gives the public half in PEM");
|
||
|
|
|
||
|
|
const std::string privatePem = Crypto::PrivateKeyToPem(key.get());
|
||
|
|
const Crypto::Key reread = Crypto::OwnKey(Crypto::LoadKeyFromPem(privatePem));
|
||
|
|
const Crypto::Key publicKey = Crypto::OwnKey(Crypto::LoadPublicKeyFromPem(publicPem));
|
||
|
|
Equal(Crypto::PublicKeyToPem(publicKey.get()), publicPem, "a public key reads back as it was written");
|
||
|
|
Equal(Crypto::PublicKeyToPem(reread.get()), publicPem, "a private key reads back to the same pair");
|
||
|
|
Throws([] { Crypto::LoadKeyFromPem("not a key"); }, "openssl error", "garbage is not a private key");
|
||
|
|
Throws([] { Crypto::LoadPublicKeyFromPem("not a key"); }, "openssl error", "garbage is not a public key");
|
||
|
|
|
||
|
|
const std::string body = R"({"amount":{"value":"12.50","currency":"EUR"}})";
|
||
|
|
const std::string signature = Crypto::SignBody(key.get(), body);
|
||
|
|
Check(Crypto::VerifyBody(publicKey.get(), body, signature), "a signature verifies against its key");
|
||
|
|
Check(Crypto::VerifyBody(reread.get(), body, Crypto::SignBody(reread.get(), body)), "and so does one from the key read back");
|
||
|
|
Check(!Crypto::VerifyBody(publicKey.get(), std::format("{} ", body), signature), "a changed body does not verify");
|
||
|
|
Check(!Crypto::VerifyBody(other.get(), body, signature), "another key does not verify");
|
||
|
|
Check(!Crypto::VerifyBody(publicKey.get(), body, ""), "an empty signature does not verify");
|
||
|
|
Check(!Crypto::VerifyBody(publicKey.get(), body, signature.substr(1)), "a signature that is not whole base64 does not verify");
|
||
|
|
Check(!Crypto::VerifyBody(publicKey.get(), body, "!!!!"), "a signature with characters outside base64 does not verify");
|
||
|
|
Check(!Crypto::VerifyBody(publicKey.get(), body, "QUI="), "one byte of padding decodes, and does not verify");
|
||
|
|
Check(!Crypto::VerifyBody(publicKey.get(), body, "QQ=="), "two bytes of padding decode, and do not verify");
|
||
|
|
|
||
|
|
Equal(Crypto::RandomHex(16).size(), std::size_t{32}, "16 random bytes are 32 hex digits");
|
||
|
|
Check(Crypto::RandomHex(8) != Crypto::RandomHex(8), "random bytes differ");
|
||
|
|
Check(std::ranges::all_of(Crypto::RandomHex(32), [](char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); }), "lower-case hex");
|
||
|
|
|
||
|
|
Equal(Crypto::ThrowOnOpenssl(std::int64_t{1}), std::int64_t{1}, "a positive status passes");
|
||
|
|
Throws([] { Crypto::ThrowOnOpenssl(std::int64_t{0}); }, "openssl error", "zero is a failure");
|
||
|
|
std::int32_t value = 7;
|
||
|
|
Check(Crypto::ThrowOnOpenssl(&value) == &value, "a pointer passes");
|
||
|
|
Throws([] { Crypto::ThrowOnOpenssl(static_cast<std::int32_t*>(nullptr)); }, "openssl error", "a null pointer is a failure");
|
||
|
|
return Test::Result();
|
||
|
|
}
|