2021-01-17 15:11:21 +00:00
|
|
|
#include <chrono>
|
2021-01-20 00:22:22 +00:00
|
|
|
#include <filesystem>
|
2021-01-20 00:16:43 +00:00
|
|
|
#include <fstream>
|
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>
|
|
|
|
|
2021-01-19 22:52:02 +00:00
|
|
|
#include "cxxopts.hpp"
|
2021-01-17 15:11:21 +00:00
|
|
|
#include "json.hpp"
|
2021-01-17 00:44:11 +00:00
|
|
|
|
2021-01-18 00:00:45 +00:00
|
|
|
#include "expiring_signature.h"
|
|
|
|
#include "receiver.h"
|
2021-01-18 22:57:33 +00:00
|
|
|
#include "sender.h"
|
|
|
|
#include "signing_key.h"
|
2021-01-18 00:00:45 +00:00
|
|
|
#include "sodium_helpers.h"
|
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 21:36:56 +00:00
|
|
|
using namespace BareMinimumCrypto;
|
2021-01-20 00:22:22 +00:00
|
|
|
namespace fs = std::filesystem;
|
2021-01-17 15:52:38 +00:00
|
|
|
|
2021-01-20 00:16:43 +00:00
|
|
|
int file (const string & file_path) {
|
|
|
|
cout << "Reading `" << file_path << "`" << endl;
|
|
|
|
|
2021-01-20 02:58:24 +00:00
|
|
|
const auto j = std::move (*try_load_msgpack_file (file_path));
|
2021-01-20 00:16:43 +00:00
|
|
|
|
2021-01-21 23:00:10 +00:00
|
|
|
if (! j.contains ("app")) {
|
|
|
|
cout << "Can't find app magic string. This file was probably not made by BMC." << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string app = j ["app"];
|
|
|
|
|
|
|
|
if (app != "4B27CL32") {
|
|
|
|
cout << "Can't find app magic string. This file was probably not made by BMC." << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-20 00:16:43 +00:00
|
|
|
const string schema = j ["schema"];
|
|
|
|
|
|
|
|
cout << "Schema: " << schema << endl;
|
|
|
|
|
|
|
|
if (schema == "3T6XF5DZ") {
|
2021-01-21 23:00:10 +00:00
|
|
|
cout << "File is a secret key for humans. (It's passphrase-protected)" << endl;
|
2021-01-20 00:16:43 +00:00
|
|
|
|
2021-01-20 00:22:22 +00:00
|
|
|
// Read msgpack fields
|
2021-01-20 01:01:27 +00:00
|
|
|
const auto key = std::move (*HumanKeyFile::try_from_msgpack (j));
|
2021-01-20 00:16:43 +00:00
|
|
|
|
2021-01-20 00:22:22 +00:00
|
|
|
// Read data from other places
|
2021-01-20 00:16:43 +00:00
|
|
|
const auto now = Instant::now ();
|
|
|
|
|
2021-01-20 00:22:22 +00:00
|
|
|
// Print normal stuff
|
2021-01-20 01:01:27 +00:00
|
|
|
cout << "Generated at Unix time " << key.time_created.x
|
|
|
|
<< " (" << now.x - key.time_created.x << " seconds ago)"
|
2021-01-20 00:16:43 +00:00
|
|
|
<< endl;
|
2021-01-20 01:01:27 +00:00
|
|
|
cout << "Generated on machine ID " << key.machine_id << endl;
|
2021-01-20 01:31:41 +00:00
|
|
|
cout << "Claims to have Base64 pubkey `" << base64_encode (key.pubkey) << "`" << endl;
|
2021-01-20 00:16:43 +00:00
|
|
|
|
2021-01-20 00:22:22 +00:00
|
|
|
// Print warnings
|
2021-01-20 01:01:27 +00:00
|
|
|
if (now.x < key.time_created.x) {
|
2021-01-20 00:22:22 +00:00
|
|
|
cout << "* The key was generated in the past. Someone's clock is wrong." << endl;
|
2021-01-20 00:16:43 +00:00
|
|
|
}
|
2021-01-20 01:01:27 +00:00
|
|
|
if (get_machine_id () != key.machine_id) {
|
|
|
|
cout << "* The key was generated on another machine. You should report this." << endl;
|
|
|
|
}
|
|
|
|
if (fs::status (file_path).permissions () != fs::perms::owner_read) {
|
|
|
|
cout << "* The key doesn't have the right permissions. Try `chmod 400` on it." << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (schema == "2PVHIKMA") {
|
2021-01-21 23:00:10 +00:00
|
|
|
cout << "File is a secret key for machines. (No passphrase)" << endl;
|
2021-01-20 01:01:27 +00:00
|
|
|
|
|
|
|
// Read msgpack fields
|
|
|
|
const auto key = std::move (*MachineKeyFile::try_from_msgpack (j));
|
|
|
|
|
|
|
|
// Read data from other places
|
|
|
|
const auto now = Instant::now ();
|
|
|
|
|
|
|
|
// Print normal stuff
|
|
|
|
cout << "Generated at Unix time " << key.time_created.x
|
|
|
|
<< " (" << now.x - key.time_created.x << " seconds ago)"
|
|
|
|
<< endl;
|
|
|
|
cout << "Generated on machine ID " << key.machine_id << endl;
|
2021-01-20 01:31:41 +00:00
|
|
|
cout << "Claims to have Base64 pubkey `" << base64_encode (key.pubkey ()) << "`" << endl;
|
2021-01-20 01:01:27 +00:00
|
|
|
|
|
|
|
// Print warnings
|
|
|
|
if (now.x < key.time_created.x) {
|
|
|
|
cout << "* The key was generated in the past. Someone's clock is wrong." << endl;
|
|
|
|
}
|
|
|
|
if (get_machine_id () != key.machine_id) {
|
2021-01-20 00:22:22 +00:00
|
|
|
cout << "* The key was generated on another machine. You should report this." << endl;
|
|
|
|
}
|
|
|
|
if (fs::status (file_path).permissions () != fs::perms::owner_read) {
|
|
|
|
cout << "* The key doesn't have the right permissions. Try `chmod 400` on it." << endl;
|
2021-01-20 00:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-21 23:00:10 +00:00
|
|
|
else if (schema == "MSYZGDBI") {
|
|
|
|
cout << "File is a signed certificate, possibly for a pubkey." << endl;
|
|
|
|
|
|
|
|
cout << "Claims to be signed by pubkey `" << base64_encode (j ["signer_pubkey"].get_binary ()) << "`" << endl;
|
|
|
|
|
|
|
|
const auto cert_bytes = j ["cert"].get_binary ();
|
|
|
|
const auto cert = json::from_msgpack (cert_bytes);
|
|
|
|
|
|
|
|
const TimeRange tr {
|
|
|
|
cert ["not_before"],
|
|
|
|
cert ["not_after"],
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto now = Instant::now ();
|
|
|
|
|
|
|
|
if (! tr.contains (now)) {
|
|
|
|
cout << "Cert is NOT valid." << endl;
|
|
|
|
|
|
|
|
if (now.x <= tr.not_before) {
|
|
|
|
cout << "Cert is from the future. Is your clock behind? Is the signer's clock ahead?" << endl;
|
|
|
|
}
|
|
|
|
else if (now.x >= tr.not_after) {
|
|
|
|
cout << "Cert expired " << now.x - tr.not_after << " seconds ago. Don't trust it." << endl;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Cert is valid for another " << tr.not_after - now.x << " seconds." << endl;
|
|
|
|
|
|
|
|
const auto payload = cert ["payload"].get_binary ();
|
|
|
|
|
|
|
|
if (payload.size () == crypto_sign_PUBLICKEYBYTES) {
|
|
|
|
cout << "Payload might be the pubkey `" << base64_encode (payload) << "`" << endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cout << "Payload doesn't have the right size to be a pubkey." << endl;
|
|
|
|
}
|
|
|
|
}
|
2021-01-20 00:16:43 +00:00
|
|
|
else {
|
|
|
|
cout << "Unknown schema. Maybe this file is from a newer version of BMC?" << endl;
|
2021-01-21 23:00:10 +00:00
|
|
|
return 1;
|
2021-01-20 00:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-19 22:52:02 +00:00
|
|
|
int test () {
|
|
|
|
if (test_base64 () != 0) {
|
2021-01-19 02:41:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-20 01:10:33 +00:00
|
|
|
cerr << "crypto_sign_PUBLICKEYBYTES = " << crypto_sign_PUBLICKEYBYTES << endl;
|
|
|
|
cerr << "crypto_sign_SECRETKEYBYTES = " << crypto_sign_SECRETKEYBYTES << endl;
|
|
|
|
|
2021-01-19 22:52:02 +00:00
|
|
|
// We generate a root key and keep it somewhere safe
|
|
|
|
// (offline, hopefully)
|
2021-01-19 02:41:05 +00:00
|
|
|
|
2021-01-17 15:11:21 +00:00
|
|
|
SigningKey root_key;
|
2021-01-18 22:23:38 +00:00
|
|
|
cerr << "Root pub key " << base64_encode (root_key.pubkey ()) << endl;
|
2021-01-17 15:11:21 +00:00
|
|
|
|
2021-01-17 21:53:04 +00:00
|
|
|
if (test_time () != 0) {
|
2021-01-17 15:11:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-17 23:31:28 +00:00
|
|
|
const auto now = Instant::now ();
|
2021-01-17 21:36:56 +00:00
|
|
|
|
2021-01-18 22:57:33 +00:00
|
|
|
// The sender generates a key
|
|
|
|
SigningKey sender_key;
|
|
|
|
cerr << "Sender key " << base64_encode (sender_key.pubkey ()) << endl;
|
2021-01-17 15:11:21 +00:00
|
|
|
|
2021-01-18 22:57:33 +00:00
|
|
|
// The root signs the sender key
|
2021-01-21 23:00:10 +00:00
|
|
|
const auto sender_cert = std::move (*root_key.sign_key (sender_key.pubkey (), now));
|
2021-01-17 15:11:21 +00:00
|
|
|
|
2021-01-18 22:57:33 +00:00
|
|
|
const auto sender = std::move (*Sender::create (sender_key, sender_cert));
|
2021-01-17 15:11:21 +00:00
|
|
|
|
2021-01-18 22:57:33 +00:00
|
|
|
// The sender 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 auto signed_data = std::move (*sender.sign (important_data));
|
2021-01-18 01:17:06 +00:00
|
|
|
|
2021-01-18 22:57:33 +00:00
|
|
|
// The receiver verifies the data by the root public key,
|
|
|
|
// even though the receiver has never seen the sender-key.
|
2021-01-18 01:17:06 +00:00
|
|
|
|
2021-01-18 22:57:33 +00:00
|
|
|
auto verified_opt = Receiver::verify_cert_and_data (root_key.pubkey (), signed_data);
|
2021-01-17 23:03:32 +00:00
|
|
|
if (! verified_opt) {
|
|
|
|
cerr << "Receiver couldn't verify cert and data" << endl;
|
|
|
|
return 1;
|
2021-01-17 15:11:21 +00:00
|
|
|
}
|
2021-01-17 23:03:32 +00:00
|
|
|
const auto verified = std::move (*verified_opt);
|
2021-01-17 00:44:11 +00:00
|
|
|
|
2021-01-17 23:03:32 +00:00
|
|
|
if (verified != important_data) {
|
|
|
|
cerr << "Verified payload did not match expected payload" << endl;
|
|
|
|
return 1;
|
2021-01-17 21:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-19 22:52:02 +00:00
|
|
|
int main (int argc, char ** argv) {
|
|
|
|
cxxopts::Options options ("BareMinimumCrypto", "Simple crypto things you might need.");
|
|
|
|
options.add_options ()
|
2021-01-20 02:58:24 +00:00
|
|
|
// Commands
|
|
|
|
("help", "Print usage")
|
|
|
|
("file", "Print info about any file generated by BMC", cxxopts::value <string> ())
|
2021-01-20 01:01:27 +00:00
|
|
|
("generate-human-key", "Generate a passphrase-protected key for human use", cxxopts::value <string> ())
|
|
|
|
("generate-machine-key", "Generate a key for machine use, with no passphrase", cxxopts::value <string> ())
|
2021-01-20 02:58:24 +00:00
|
|
|
("generate-key-cert", "Certify a key for 3 months and save the cert here", cxxopts::value <string> ())
|
2021-02-08 22:26:49 +00:00
|
|
|
("generate-data-cert", "Certify data for 1 week and save the cert here", cxxopts::value <string> ())
|
2021-01-20 02:58:24 +00:00
|
|
|
|
|
|
|
// cxxopts nonsense
|
2021-01-20 01:31:41 +00:00
|
|
|
("using-key", "Key to load for other operations", cxxopts::value <string> ())
|
2021-01-20 02:58:24 +00:00
|
|
|
|
|
|
|
// Other stuff
|
2021-01-19 22:52:02 +00:00
|
|
|
("test", "Run self-test")
|
|
|
|
;
|
|
|
|
|
|
|
|
auto result = options.parse (argc, argv);
|
|
|
|
|
|
|
|
if (result.count ("help")) {
|
|
|
|
cout << options.help () << endl;
|
2021-01-17 21:36:56 +00:00
|
|
|
}
|
2021-01-20 02:58:24 +00:00
|
|
|
else if (result.count ("file")) {
|
|
|
|
const auto file_path = result ["file"].as <string> ();
|
|
|
|
return file (file_path);
|
|
|
|
}
|
2021-01-20 01:01:27 +00:00
|
|
|
else if (result.count ("generate-human-key")) {
|
|
|
|
const auto file_path = result ["generate-human-key"].as <string> ();
|
2021-01-20 02:58:24 +00:00
|
|
|
const auto passphrase = get_passphrase_from_user ();
|
2021-01-19 22:52:02 +00:00
|
|
|
|
2021-01-20 01:10:33 +00:00
|
|
|
auto key_opt = HumanKeyFile::generate (file_path, passphrase);
|
2021-01-20 01:01:27 +00:00
|
|
|
if (! key_opt) {
|
|
|
|
cerr << "Error. Key was not generated" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
cout << "The pubkey is `" << base64_encode (key_opt->pubkey ()) << "`" << endl;
|
|
|
|
}
|
|
|
|
else if (result.count ("generate-machine-key")) {
|
|
|
|
const auto file_path = result ["generate-machine-key"].as <string> ();
|
|
|
|
|
2021-01-20 01:10:33 +00:00
|
|
|
auto key_opt = MachineKeyFile::generate (file_path);
|
2021-01-19 22:52:02 +00:00
|
|
|
if (! key_opt) {
|
|
|
|
cerr << "Error. Key was not generated" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
cout << "The pubkey is `" << base64_encode (key_opt->pubkey ()) << "`" << endl;
|
|
|
|
}
|
2021-01-20 02:58:24 +00:00
|
|
|
else if (result.count ("generate-key-cert")) {
|
|
|
|
if (! result.count ("using-key")) {
|
|
|
|
cerr << "Usage: bmc --generate-key-cert output.cert --using-key human.key" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto key_path = result ["using-key"].as <string> ();
|
|
|
|
const auto output_path = result ["generate-key-cert"].as <string> ();
|
|
|
|
const auto passphrase = get_passphrase_from_user ();
|
|
|
|
|
|
|
|
auto key_opt = HumanKeyFile::load (key_path, passphrase);
|
|
|
|
if (! key_opt) {
|
|
|
|
cerr << "Error, could not load human key." << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const auto key = std::move (*key_opt);
|
|
|
|
|
|
|
|
cout << "Paste Base64-encoded public key" << endl;
|
|
|
|
string pubkey_b64;
|
|
|
|
cin >> pubkey_b64;
|
|
|
|
|
|
|
|
auto pubkey_opt = base64_decode (pubkey_b64);
|
|
|
|
const auto pubkey = std::move (*pubkey_opt);
|
|
|
|
|
2021-01-20 23:24:55 +00:00
|
|
|
auto sig_opt = key.sign (pubkey, TimeRange::from_start_and_dur (Instant::now (), about_3_months));
|
|
|
|
if (! sig_opt) {
|
|
|
|
cerr << "Error, could not sign pubkey." << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const auto sig = std::move (*sig_opt);
|
2021-01-21 23:00:10 +00:00
|
|
|
const auto sig_bytes = sig.to_msgpack ();
|
2021-01-20 23:24:55 +00:00
|
|
|
|
2021-01-21 23:00:10 +00:00
|
|
|
ofstream f;
|
|
|
|
f.open (output_path, ofstream::binary);
|
|
|
|
|
|
|
|
if (! f.is_open ()) {
|
|
|
|
cerr << "Error, could not open output file" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2021-01-20 02:58:24 +00:00
|
|
|
|
2021-01-21 23:00:10 +00:00
|
|
|
f.write ((const char *)sig_bytes.data (), sig_bytes.size ());
|
2021-01-20 00:16:43 +00:00
|
|
|
}
|
2021-01-19 22:52:02 +00:00
|
|
|
else if (result.count ("test")) {
|
|
|
|
if (test () != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cerr << "All good." << endl;
|
2021-01-17 15:52:38 +00:00
|
|
|
}
|
2021-01-17 00:44:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|