2021-01-17 15:11:21 +00:00
|
|
|
#include <chrono>
|
2021-01-17 00:44:11 +00:00
|
|
|
#include <iostream>
|
2021-01-17 15:11:21 +00:00
|
|
|
#include <optional>
|
2021-01-17 00:44:11 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <sodium.h>
|
|
|
|
|
2021-01-17 15:11:21 +00:00
|
|
|
// From https://github.com/tkislan/base64
|
2021-01-17 16:16:57 +00:00
|
|
|
#include "cpp-base64/base64.h"
|
2021-01-17 15:11:21 +00:00
|
|
|
#include "json.hpp"
|
2021-01-17 00:44:11 +00:00
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
#include "string_helpers.h"
|
2021-01-17 21:45:59 +00:00
|
|
|
#include "time_helpers.h"
|
2021-01-17 21:36:56 +00:00
|
|
|
|
2021-01-17 00:44:11 +00:00
|
|
|
using namespace std;
|
2021-01-17 15:11:21 +00:00
|
|
|
using nlohmann::json;
|
2021-01-17 00:44:11 +00:00
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
using namespace BareMinimumCrypto;
|
2021-01-17 15:52:38 +00:00
|
|
|
|
2021-01-17 15:11:21 +00:00
|
|
|
struct ExpiringSignature {
|
|
|
|
string cert_s;
|
|
|
|
vector <uint8_t> sig;
|
|
|
|
|
|
|
|
// C++ nonsense
|
|
|
|
bool operator == (const ExpiringSignature & o) const {
|
|
|
|
return
|
|
|
|
cert_s == o.cert_s &&
|
|
|
|
sig == o.sig
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator != (const ExpiringSignature & o) const {
|
|
|
|
return ! (*this == o);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void try_sodium_init () {
|
|
|
|
if (sodium_init () < 0) {
|
|
|
|
throw std::runtime_error ("Can't initialize libsodium");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 15:52:38 +00:00
|
|
|
struct VerifiedData {
|
|
|
|
vector <uint8_t> payload;
|
|
|
|
string purpose;
|
|
|
|
};
|
|
|
|
|
|
|
|
optional <VerifiedData> try_verify_signed_data (
|
|
|
|
const ExpiringSignature & sig,
|
|
|
|
int64_t now,
|
|
|
|
const vector <uint8_t> & 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"];
|
2021-01-17 16:16:57 +00:00
|
|
|
const auto payload = std::move (*b64_decode (payload_b64));
|
2021-01-17 15:52:38 +00:00
|
|
|
|
|
|
|
return VerifiedData {
|
|
|
|
payload,
|
|
|
|
purpose
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
optional <VerifiedData> verify_signed_data (
|
|
|
|
const ExpiringSignature & sig,
|
|
|
|
int64_t now,
|
|
|
|
const vector <uint8_t> & pubkey
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
return try_verify_signed_data (sig, now, pubkey);
|
|
|
|
}
|
|
|
|
catch (json::exception &) {
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 15:11:21 +00:00
|
|
|
class SigningKey {
|
2021-01-17 00:44:11 +00:00
|
|
|
vector <uint8_t> pk;
|
|
|
|
vector <uint8_t> sk;
|
|
|
|
|
2021-01-17 15:11:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
string pub_to_base64 () const {
|
2021-01-17 21:36:56 +00:00
|
|
|
return base64_encode (pk);
|
2021-01-17 15:11:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
optional <ExpiringSignature> sign_base64 (
|
|
|
|
const string & payload_b64,
|
2021-01-17 15:11:21 +00:00
|
|
|
string purpose,
|
2021-01-17 21:36:56 +00:00
|
|
|
int64_t now,
|
|
|
|
int64_t duration
|
2021-01-17 15:11:21 +00:00
|
|
|
) const {
|
|
|
|
try_sodium_init ();
|
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
if (duration > about_1_year) {
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto not_after = now + duration;
|
2021-01-17 15:11:21 +00:00
|
|
|
|
|
|
|
const json j {
|
|
|
|
{"not_before", now},
|
|
|
|
{"not_after", not_after},
|
|
|
|
{"purpose", purpose},
|
2021-01-17 21:36:56 +00:00
|
|
|
{"payload_b64", payload_b64},
|
2021-01-17 15:11:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto cert_s = j.dump ();
|
|
|
|
|
|
|
|
vector <uint8_t> sig;
|
|
|
|
sig.resize (crypto_sign_BYTES);
|
|
|
|
|
|
|
|
crypto_sign_detached (sig.data (), nullptr, (const uint8_t *)cert_s.data (), cert_s.size (), sk.data ());
|
|
|
|
|
|
|
|
return ExpiringSignature {
|
|
|
|
cert_s,
|
|
|
|
sig,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
optional <ExpiringSignature> sign_key (const SigningKey & k, int64_t now) const
|
|
|
|
{
|
2021-01-17 21:36:56 +00:00
|
|
|
return sign_base64 (k.pub_to_base64 (), "4QHAB7O5 trusted public key", now, about_3_months);
|
|
|
|
}
|
|
|
|
|
|
|
|
optional <ExpiringSignature> sign_data (const vector <uint8_t> & v, int64_t now) const
|
|
|
|
{
|
|
|
|
return sign_base64 (base64_encode (v), "MS7WL26L signed data", now, about_1_week);
|
2021-01-17 15:11:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Most tests will use a virtual clock. But just as a smoke test,
|
|
|
|
// make sure real time is realistic.
|
|
|
|
|
|
|
|
int check_real_time () {
|
|
|
|
const auto seconds_since_epoch = get_seconds_since_epoch ();
|
|
|
|
|
|
|
|
const auto time_of_writing = 1610844872;
|
|
|
|
if (seconds_since_epoch < time_of_writing) {
|
|
|
|
cerr << "Error: Real time is in the past." << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int64_t about_100_years = (int64_t)100 * 365 * 86400;
|
|
|
|
if (seconds_since_epoch > time_of_writing + about_100_years) {
|
|
|
|
cerr << "Error: Real time is in the far future." << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-17 15:52:38 +00:00
|
|
|
int check_base64 () {
|
2021-01-17 16:16:57 +00:00
|
|
|
// I assume that char is 8 bits
|
|
|
|
// char is C++ nonsense inherited from C nonsense
|
|
|
|
if (sizeof (char) != sizeof (uint8_t)) {
|
|
|
|
cerr << "char is not the same size as uint8_t" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-17 15:52:38 +00:00
|
|
|
vector <uint8_t> v {1, 2, 3, 4, 5, 6};
|
2021-01-17 21:36:56 +00:00
|
|
|
const auto s = base64_encode (v);
|
2021-01-17 15:52:38 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
int happy_path () {
|
|
|
|
// We generate a root key and keep it somewhere safe
|
|
|
|
// (offline, hopefully)
|
2021-01-17 15:11:21 +00:00
|
|
|
SigningKey root_key;
|
|
|
|
cerr << "Root pub key " << root_key.pub_to_base64 () << endl;
|
|
|
|
|
|
|
|
if (check_real_time () != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
// The server generates a signing key
|
2021-01-17 15:11:21 +00:00
|
|
|
SigningKey signing_key;
|
|
|
|
cerr << "Signing key " << signing_key.pub_to_base64 () << endl;
|
|
|
|
|
|
|
|
const auto now = get_seconds_since_epoch ();
|
2021-01-17 21:36:56 +00:00
|
|
|
|
|
|
|
// 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
|
2021-01-17 15:11:21 +00:00
|
|
|
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, now + 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
cerr << "Cert:" << endl;
|
|
|
|
cerr << cert.cert_s << endl;
|
2021-01-17 21:36:56 +00:00
|
|
|
cerr << base64_encode (cert.sig) << endl;
|
2021-01-17 15:11:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
// The client knows our root public key
|
2021-01-17 15:11:21 +00:00
|
|
|
const auto root_pubkey = root_key.pubkey ();
|
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
{
|
|
|
|
auto verified_opt = verify_signed_data (cert, now, root_pubkey);
|
|
|
|
if (! verified_opt) {
|
|
|
|
cerr << "Couldn't verify cert" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const auto verified = std::move (*verified_opt);
|
|
|
|
|
|
|
|
if (verified.purpose != "4QHAB7O5 trusted public key") {
|
|
|
|
cerr << "Purpose did not match" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (verified.payload != signing_key.pubkey ()) {
|
|
|
|
cerr << "Pubkey payload did not match" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2021-01-17 15:11:21 +00:00
|
|
|
}
|
2021-01-17 00:44:11 +00:00
|
|
|
|
2021-01-17 15:52:38 +00:00
|
|
|
{
|
2021-01-17 21:36:56 +00:00
|
|
|
auto verified_opt = verify_signed_data (signed_data, now, signing_key.pubkey ());
|
|
|
|
if (! verified_opt) {
|
|
|
|
cerr << "Couldn't verify cert" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const auto verified = std::move (*verified_opt);
|
2021-01-17 15:52:38 +00:00
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
if (verified.purpose != "MS7WL26L signed data") {
|
|
|
|
cerr << "Purpose did not match" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (verified.payload != important_data) {
|
|
|
|
cerr << "Pubkey payload did not match" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main () {
|
|
|
|
if (check_base64 () != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (happy_path () != 0) {
|
|
|
|
return 1;
|
2021-01-17 15:52:38 +00:00
|
|
|
}
|
2021-01-17 00:44:11 +00:00
|
|
|
|
2021-01-17 21:36:56 +00:00
|
|
|
cerr << "All good." << endl;
|
2021-01-17 00:44:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|