Make key files extensible, store key name in key file

Storing the key name in the key file makes it unnecessary to pass the
--key-name option to git-crypt unlock.

This breaks compatibility with post-revamp keys.  On the plus side,
keys are now extensible so in the future it will be easier to make
changes to the format without breaking compatibility.
This commit is contained in:
Andrew Ayer
2014-06-29 21:54:28 -07:00
parent 3c8273cd4b
commit 3511033f7f
3 changed files with 206 additions and 33 deletions

32
key.hpp
View File

@@ -45,12 +45,16 @@ enum {
struct Key_file {
public:
struct Entry {
uint32_t version;
unsigned char aes_key[AES_KEY_LEN];
unsigned char hmac_key[HMAC_KEY_LEN];
Entry ();
void load (std::istream&);
void load_legacy (uint32_t version, std::istream&);
void store (std::ostream&) const;
void generate ();
void generate (uint32_t version);
};
struct Malformed { }; // exception class
@@ -59,7 +63,7 @@ public:
const Entry* get_latest () const;
const Entry* get (uint32_t version) const;
void add (uint32_t version, const Entry&);
void add (const Entry&);
void load_legacy (std::istream&);
void load (std::istream&);
@@ -77,11 +81,33 @@ public:
uint32_t latest () const;
void set_key_name (const char* k) { key_name = k ? k : ""; }
const char* get_key_name () const { return key_name.empty() ? 0 : key_name.c_str(); }
private:
typedef std::map<uint32_t, Entry, std::greater<uint32_t> > Map;
enum { FORMAT_VERSION = 1 };
enum { FORMAT_VERSION = 2 };
Map entries;
std::string key_name;
void load_header (std::istream&);
enum {
HEADER_FIELD_END = 0,
HEADER_FIELD_KEY_NAME = 1
};
enum {
KEY_FIELD_END = 0,
KEY_FIELD_VERSION = 1,
KEY_FIELD_AES_KEY = 3,
KEY_FIELD_HMAC_KEY = 5
};
};
enum {
KEY_NAME_MAX_LEN = 128
};
bool validate_key_name (const char* key_name, std::string* reason =0);
#endif