ptth/bare_minimum_crypto/cpp/bmc_test.cpp

178 lines
3.9 KiB
C++

#include <chrono>
#include <iostream>
#include <optional>
#include <stdint.h>
#include <string>
#include <vector>
#include <sodium.h>
// #include "cpp-base64/base64.h"
#include "json.hpp"
#include "expiring_signature.h"
#include "receiver.h"
#include "sodium_helpers.h"
#include "string_helpers.h"
#include "time_helpers.h"
using namespace std;
using nlohmann::json;
using namespace BareMinimumCrypto;
class SigningKey {
vector <uint8_t> pk;
vector <uint8_t> sk;
public:
SigningKey () {
try_sodium_init ();
pk.resize (crypto_sign_PUBLICKEYBYTES);
sk.resize (crypto_sign_SECRETKEYBYTES);
crypto_sign_keypair (pk.data (), sk.data ());
}
vector <uint8_t> pubkey () const {
return pk;
}
vector <uint8_t> pub_to_msgpack () const {
const json j = {
{"key", json::binary (pk)},
};
return json::to_msgpack (j);
}
optional <ExpiringSignature> sign (
const vector <uint8_t> & payload,
TimeRange tr
) const {
try_sodium_init ();
if (tr.duration () > about_1_year) {
return nullopt;
}
const json j {
{"not_before", tr.not_before},
{"not_after", tr.not_after},
{"payload", json::binary (payload)},
};
const auto cert = json::to_msgpack (j);
vector <uint8_t> sig;
sig.resize (crypto_sign_BYTES);
crypto_sign_detached (sig.data (), nullptr, cert.data (), cert.size (), sk.data ());
return ExpiringSignature {
cert,
sig,
};
}
optional <ExpiringSignature> sign_key (const SigningKey & k, Instant now) const
{
return sign (k.pub_to_msgpack (), TimeRange::from_start_and_dur (now, about_3_months));
}
optional <ExpiringSignature> sign_data (const vector <uint8_t> & v, Instant now) const
{
return sign (v, TimeRange::from_start_and_dur (now, about_1_week));
}
};
int happy_path () {
// We generate a root key and keep it somewhere safe
// (offline, hopefully)
SigningKey root_key;
cerr << "Root pub key " << base64_encode (root_key.pubkey ()) << endl;
if (test_time () != 0) {
return 1;
}
// The server generates a signing key
SigningKey signing_key;
cerr << "Signing key " << base64_encode (signing_key.pubkey ()) << endl;
const auto now = Instant::now ();
// That signing key signs some important data
const auto important_data = copy_to_bytes ("Nikolai, Anna, Ivan, Mikhail, Ivan, Nikolai, Anna. 7 4 1 4 3 5 7 4");
const ExpiringSignature signed_data = std::move (*signing_key.sign_data (important_data, now));
// The server signs our temporary signing key
const ExpiringSignature cert = std::move (*root_key.sign_key (signing_key, now));
{
// Check that a different time results in a different cert
const auto cert_2 = std::move (*root_key.sign_key (signing_key, now));
const auto cert_3 = std::move (*root_key.sign_key (signing_key, Instant {now.x + 1}));
if (cert != cert_2) {
cerr << "Certs should have been identical" << endl;
return 1;
}
if (cert == cert_3) {
cerr << "Certs should have been different" << endl;
return 1;
}
if (cert == cert_3) {
cerr << "Signatures should have been different" << endl;
return 1;
}
}
// The receiver verifies the data by the root public key,
// even though the receiver has never seen the sub-key.
const auto root_pubkey = root_key.pubkey ();
const json j {
{"cert", {
{"sig", json::binary (cert.sig)},
{"cert", json::binary (cert.cert)},
}},
{"data", {
{"sig", json::binary (signed_data.sig)},
{"cert", json::binary (signed_data.cert)},
}},
};
auto verified_opt = Receiver::verify_cert_and_data (root_pubkey, json::to_msgpack (j));
if (! verified_opt) {
cerr << "Receiver couldn't verify cert and data" << endl;
return 1;
}
const auto verified = std::move (*verified_opt);
if (verified != important_data) {
cerr << "Verified payload did not match expected payload" << endl;
return 1;
}
return 0;
}
int main () {
if (test_base64 () != 0) {
return 1;
}
if (happy_path () != 0) {
return 1;
}
cerr << "All good." << endl;
return 0;
}