From 0f0e59029f298d1cfd27535e18d4d37abca94114 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Sun, 17 Jan 2021 09:52:38 -0600 Subject: [PATCH] update: so I don't like this base64 lib anymore. It has no error handling. --- bare_minimum_crypto/cpp/bmc_test.cpp | 124 ++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 2 deletions(-) diff --git a/bare_minimum_crypto/cpp/bmc_test.cpp b/bare_minimum_crypto/cpp/bmc_test.cpp index a310586..e34cad6 100644 --- a/bare_minimum_crypto/cpp/bmc_test.cpp +++ b/bare_minimum_crypto/cpp/bmc_test.cpp @@ -19,6 +19,28 @@ using nlohmann::json; const int64_t about_3_months = (int64_t)105 * 86400; +// Not sure why the Base64 lib fails to provide this API +string b64_encode (const vector & v) { + string s; + s.resize (Base64::EncodedLength (v.size ())); + + Base64::Encode ((const char *)v.data (), v.size (), s.data (), s.size ()); + + return s; +} + +optional > b64_decode (const string & s) { + vector v; + v.resize (Base64::DecodedLength (s.data (), s.size ())); + + if (! Base64::Decode (s.data (), s.size (), (char *)v.data (), v.size ())) + { + return nullopt; + } + + return v; +} + struct ExpiringSignature { string cert_s; vector sig; @@ -47,6 +69,69 @@ void try_sodium_init () { } } +struct VerifiedData { + vector payload; + string purpose; +}; + +optional try_verify_signed_data ( + const ExpiringSignature & sig, + int64_t now, + const vector & pubkey +) { + try_sodium_init (); + + if (pubkey.size () != crypto_sign_PUBLICKEYBYTES) { + return nullopt; + } + + if (crypto_sign_verify_detached ( + sig.sig.data (), + (const uint8_t *)sig.cert_s.data (), + sig.cert_s.size (), + pubkey.data () + ) != 0) { + return nullopt; + } + + const json j = json::parse (sig.cert_s); + + const int64_t not_before = j ["not_before"]; + const int64_t not_after = j ["not_after"]; + + if (now < not_before) { + return nullopt; + } + if (now > not_after) { + return nullopt; + } + + const string purpose = j ["purpose"]; + const string payload_b64 = j ["payload_b64"]; + string payload_s; + Base64::Decode (payload_b64, &payload_s); + + const vector payload; + + return VerifiedData { + payload, + purpose + }; +} + +optional verify_signed_data ( + const ExpiringSignature & sig, + int64_t now, + const vector & pubkey +) { + try { + return try_verify_signed_data (sig, now, pubkey); + } + catch (json::exception &) { + return nullopt; + } +} + string to_base64 (const vector & v) { const string s ((const char *)v.data (), v.size ()); string b64; @@ -132,7 +217,39 @@ int check_real_time () { return 0; } +int check_base64 () { + vector v {1, 2, 3, 4, 5, 6}; + const auto s = b64_encode (v); + + if (s != "AQIDBAUG") { + cerr << "Base64 encoding failed" << endl; + return 1; + } + + // Trivial decode + const auto v2 = std::move (*b64_decode (s)); + + if (v2 != v) { + cerr << "Base64 trivial decode failed" << endl; + return 1; + } + + // Decode should fail + const auto v3 = b64_decode ("AQIDBAUG."); + + if (v3 != nullopt) { + cerr << "Base64 decode should have failed" << endl; + return 1; + } + + return 0; +} + int main () { + if (check_base64 () != 0) { + return 1; + } + // Suppose we generate a root key and keep it somewhere safe // (not a server) SigningKey root_key; @@ -188,8 +305,11 @@ int main () { return 1; } - const json j = json::parse (cert.cert_s); - + { + const json j = json::parse (cert.cert_s); + + cerr << "not_before: " << (int64_t)j ["not_before"] << endl; + } cerr << "Done." << endl; return 0;