From 400f1a318d8e768297ae00dd3611be3c18f82629 Mon Sep 17 00:00:00 2001 From: ac-stef Date: Thu, 5 Feb 2015 16:36:37 +0100 Subject: [PATCH] add authkey command authkey manages a list of keys other than the game key (which is managed by authsetup). For example, this can be server owner keys or clan boss keys. authkey clear Empties the list and comments out all lines in config/authkeys.cfg. authkey list Lists all authkeys that are currently in memory. authkey delete keyname Deletes the key from memory. authkey new keyname Generates a new key with the name . If a key of that name already exists, it is deleted. The key is added to the list in memory and also written to the file config/authkeys.cfg. authkey add keyname privkey Adds a key with the name to the list in memory. The privkey is a 32-byte hexadecimal string, preferrably generated by authkey new. The matching public key is generated automatically from the private key. authkey selfcert keyname [comment] Generates a self-signed certificate for the key . Being able to create such a cert is proof, that you own the private key of a certain public key. DOCREF authkey --- source/src/crypto.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++ source/src/main.cpp | 1 + source/src/protos.h | 33 +++++------- 3 files changed, 138 insertions(+), 19 deletions(-) diff --git a/source/src/crypto.cpp b/source/src/crypto.cpp index 1f9ddfba..1ad31eac 100644 --- a/source/src/crypto.cpp +++ b/source/src/crypto.cpp @@ -873,6 +873,129 @@ void authsetup(char **args, int numargs) // set up private and public keys intret(res); } COMMAND(authsetup, "v"); + +///////////////////////////////////////////////// misc key management ////////////////////////////////////////////////////// +// * used to sign certs or other stuff +// * dev/admin/master keys +// * clanboss keys +// + +#define AUTHKEYSCFGFILE "config" PATHDIVS "authkeys.cfg" + +vector authkeys; + +authkey::authkey(const char *aname, const char *privkey) : name(NULL) +{ + if(*aname && strlen(privkey) == 64 && hex2bin(sk, privkey, 32) == 32) + { + ed25519_pubkey_from_private(sk + 32, sk); + name = newstring(aname); + } +} + +bool delauthkey(const char *name) +{ + int before = authkeys.length(); + loopvrev(authkeys) if(!strcmp(authkeys[i]->name, name)) delete authkeys.remove(i); + return authkeys.length() < before; +} + +const uchar *getauthkey(const char *name) +{ + loopvrev(authkeys) if(!strcmp(authkeys[i]->name, name)) return authkeys[i]->sk; + return NULL; +} + +void authkey_(char **args, int numargs) // set up misc keys +{ + if(numargs > 0) + { + string buf; + if(!strcasecmp(args[0], "CLEAR")) + { // authkey clear + if(authkeys.length()) + { + char *oldfile = loadfile(AUTHKEYSCFGFILE, NULL); + if(oldfile) + { + stream *f = openfile(AUTHKEYSCFGFILE, "wb"); + if(f) + { // don't really delete the old keys, just comment them out + for(char *l = strtok(oldfile, "\n\r"); l; l = strtok(NULL, "\n\r")) if(*l) f->printf("// %s\n", l); + delete f; + } + delete[] oldfile; + } + authkeys.shrink(0); + conoutf("all authkeys deleted"); + } + } + else if(!strcasecmp(args[0], "LIST")) + { // authkey list + if(authkeys.length()) + { + conoutf("name\tpubkey"); + loopv(authkeys) conoutf("%s\t%s", authkeys[i]->name, bin2hex(buf, authkeys[i]->sk + 32, 32)); + } + else conoutf("no authkeys"); + } + else if(!strcasecmp(args[0], "DELETE")) + { // authkey delete name + if(numargs > 1) delauthkey(args[1]); + } + else if(!strcasecmp(args[0], "NEW")) + { // authkey new name + if(numargs > 1 && args[1][0]) + { + uchar prepriv[42], priv[32], pub[32]; + delauthkey(args[1]); + entropy_get(prepriv, 42); + privkey_from_prepriv(priv, prepriv, 42); + ed25519_pubkey_from_private(pub, priv); + authkey *ak = new authkey(args[1], bin2hex(buf, priv, 32)); + if(ak->name) + { + authkeys.add(ak); + stream *f = openfile(AUTHKEYSCFGFILE, "ab"); + if(f) + { + f->printf("// authkey \"%s\", generated %s\n", ak->name, timestring("%c")); + f->printf("// prepriv %s\n", bin2hex(buf, prepriv, 42)); + f->printf("// priv %s\n", bin2hex(buf, priv, 32)); + f->printf("// pub %s\n", bin2hex(buf, pub, 32)); + f->printf("authkey add %s %s\n\n", ak->name, bin2hex(buf, priv, 32)); + delete f; + } + conoutf("authkey %s generated.", ak->name); + } + } + } + else if(!strcasecmp(args[0], "ADD")) + { // authkey add name privkey + if(numargs > 2) + { + delauthkey(args[1]); + authkey *ak = new authkey(args[1], args[2]); + if(ak->name) authkeys.add(ak); + } + } + else if(!strcasecmp(args[0], "SELFCERT")) + { // authkey selfcert name + const uchar *sk; + if(numargs > 1 && (sk = getauthkey(args[1]))) + { + defformatstring(cmd)("newcert start\n" + "newcert line pubkey %s\n" + "newcert line name \"%s\" \"%s\"\n" + "newcert sign \"%s\" \"self-signed cert\"\n" + "\n", bin2hex(buf, sk + 32, 32), args[1], numargs > 2 ? args[2] : "", args[1]); + execute(cmd); + } + } + } +} +COMMANDN(authkey,authkey_, "v"); + #endif diff --git a/source/src/main.cpp b/source/src/main.cpp index e5a898fa..da79b717 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -1283,6 +1283,7 @@ int main(int argc, char **argv) exec("config/admin.cfg"); execfile("config/servers.cfg"); execfile("config/pcksources.cfg"); + execfile("config/authkeys.cfg"); execfile("config/authprivate.cfg"); loadhistory(); setupautodownload(); diff --git a/source/src/protos.h b/source/src/protos.h index 6c005276..9d2813e2 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -1,5 +1,19 @@ // protos for ALL external functions in cube... +struct authkey +{ + const char *name; + uchar sk[64]; + + authkey(const char *aname, const char *privkey); + + ~authkey() + { + if(name) printf("deleting %s\n", name); + DELSTRING(name); + } +}; + #ifndef STANDALONE extern bool hasTE, hasMT, hasMDA, hasDRE, hasstencil, hasST2, hasSTW, hasSTS, hasAF; @@ -32,25 +46,6 @@ struct color color(float r, float g, float b, float a) : r(r), g(g), b(b), alpha(a) {} }; -struct authkey // for AUTH -{ - char *name, *key, *desc; - int lastauth; - - authkey(const char *name, const char *key, const char *desc) - : name(newstring(name)), key(newstring(key)), desc(newstring(desc)), - lastauth(0) - { - } - - ~authkey() - { - DELETEA(name); - DELETEA(key); - DELETEA(desc); - } -}; - // console extern stream *clientlogfile; extern vector *bootclientlog;