From 4c158ea18607efded85f036472905c7114600a8e Mon Sep 17 00:00:00 2001 From: LDA Date: Mon, 19 Aug 2024 16:40:30 +0200 Subject: [PATCH] [MOD] Use specific SHA1/256 abstraction functions --- src/MatrixEventHandler.c | 4 +-- src/Parsee/SHA.c | 57 ++++++++++++++++++++++++++++++++ src/Parsee/Tables/NickTable.c | 6 +--- src/Parsee/User.c | 6 ++-- src/XMPP/Component.c | 6 ++-- src/XMPPThread/Bridged.c | 5 +-- src/XMPPThread/Stanzas/Message.c | 2 +- src/include/Parsee.h | 14 ++++++++ src/include/XMPPCommands.x.h | 2 +- 9 files changed, 80 insertions(+), 22 deletions(-) create mode 100644 src/Parsee/SHA.c diff --git a/src/MatrixEventHandler.c b/src/MatrixEventHandler.c index d688c3c..6ead22a 100644 --- a/src/MatrixEventHandler.c +++ b/src/MatrixEventHandler.c @@ -25,8 +25,7 @@ JoinMUC(ParseeData *data, HashMap *event, char *jid, char *muc, char *name) { char *nonce_str = StrInt(nonce); char *input = StrConcat(4, sender, name, data->id, nonce_str); - unsigned char *digest = Sha256(input); - char *hex = ShaToHex(digest); + char *hex = ParseeSHA256(input); if (strlen(hex) >= 8) { @@ -41,7 +40,6 @@ JoinMUC(ParseeData *data, HashMap *event, char *jid, char *muc, char *name) nonce++; Free(nonce_str); - Free(digest); Free(input); Free(hex); } diff --git a/src/Parsee/SHA.c b/src/Parsee/SHA.c new file mode 100644 index 0000000..9cd4484 --- /dev/null +++ b/src/Parsee/SHA.c @@ -0,0 +1,57 @@ +#include + +#include +#include + +static char * +ToHex(unsigned char *input, size_t length) +{ + char *hex = Malloc(length * 2 + 1); + size_t i; + + for (i = 0; i < length; i++) + { + const char *table = + "0123456789abcdef"; + unsigned char byte = input[i]; + hex[2 * i] = table[(byte >> 4) & 0xF]; + hex[2*i+1] = table[(byte >> 0) & 0xF]; + } + + hex[length * 2] = '\0'; + + return hex; +} + +char * +ParseeSHA256(char *string) +{ + unsigned char *sha; + char *returnString; + if (!string) + { + return NULL; + } + + sha = Sha256(string); + returnString = ToHex(sha, 32); + Free(sha); + + return returnString; +} +char * +ParseeSHA1(char *string) +{ + unsigned char *sha; + char *returnString; + if (!string) + { + return NULL; + } + + sha = Sha1(string); + returnString = ToHex(sha, 20); + Free(sha); + + return returnString; +} diff --git a/src/Parsee/Tables/NickTable.c b/src/Parsee/Tables/NickTable.c index a9bb601..d8e7141 100644 --- a/src/Parsee/Tables/NickTable.c +++ b/src/Parsee/Tables/NickTable.c @@ -12,8 +12,6 @@ static HashMap *nick_table = NULL; static char * GenerateKey(char *muc, char *mxid) { - unsigned char *shaDigest; - char *concatStr; char *hexDigest; if (!muc || !mxid) @@ -22,10 +20,8 @@ GenerateKey(char *muc, char *mxid) } concatStr = StrConcat(3, muc, ":", mxid); - shaDigest = Sha256(concatStr); - hexDigest = ShaToHex(shaDigest); + hexDigest = ParseeSHA256(concatStr); - Free (shaDigest); Free (concatStr); return hexDigest; } diff --git a/src/Parsee/User.c b/src/Parsee/User.c index 096530d..ad7ae03 100644 --- a/src/Parsee/User.c +++ b/src/Parsee/User.c @@ -295,19 +295,17 @@ char * ParseeGetDMID(char *mxid, char *jid) { char *concat, *sha; - unsigned char *raw; if (!mxid || !jid) { return NULL; } + /* We really don't care about safety here. */ jid = ParseeTrimJID(jid); concat = StrConcat(2, mxid, jid); - raw = Sha1(concat); - sha = ShaToHex(raw); + sha = ParseeSHA1(concat); Free(concat); - Free(raw); Free(jid); return sha; diff --git a/src/XMPP/Component.c b/src/XMPP/Component.c index aa518cd..0eee988 100644 --- a/src/XMPP/Component.c +++ b/src/XMPP/Component.c @@ -11,6 +11,7 @@ #include #include +#include #include /* The default component port Prosody uses. */ @@ -81,14 +82,11 @@ static char * ComputeHandshake(char *shared, char *stream) { char *source; - unsigned char *raw_sha; char *sha; source = StrConcat(2, stream, shared); - raw_sha = Sha1(source); - sha = ShaToHex(raw_sha); + sha = ParseeSHA1(source); - Free(raw_sha); Free(source); return sha; diff --git a/src/XMPPThread/Bridged.c b/src/XMPPThread/Bridged.c index 4afab8e..806c979 100644 --- a/src/XMPPThread/Bridged.c +++ b/src/XMPPThread/Bridged.c @@ -166,7 +166,6 @@ ServerHasXEP421(ParseeData *data, char *from) static char * ScrambleOID(ParseeData *data, char *opaque_oid) { - unsigned char *raw; char *sha, *mxid; const ParseeConfig *c = data->config; if (!opaque_oid) @@ -175,9 +174,7 @@ ScrambleOID(ParseeData *data, char *opaque_oid) } /* Turns this into a 128-byte long, Matrix-safe value. */ - raw = Sha256(opaque_oid); - sha = ShaToHex(raw); - Free(raw); + sha = ParseeSHA256(opaque_oid); /* TODO: Either mark this specially, or drop Parsee JID flags * altogether before any 1.0.0 */ diff --git a/src/XMPPThread/Stanzas/Message.c b/src/XMPPThread/Stanzas/Message.c index 5d33297..3c6949c 100644 --- a/src/XMPPThread/Stanzas/Message.c +++ b/src/XMPPThread/Stanzas/Message.c @@ -165,7 +165,7 @@ end_error: room = ParseeFindDMRoom(args, to, from); data = body ? ArrayGet(body->children, 0) : NULL; - /* TODO: CLEAN THAT UP */ + /* TODO: CLEAN THAT UP INTO A CREATEDM FUNCTION */ mroom_id = ParseeGetBridgedRoom(args, stanza); if (!mroom_id && !room && !XMPPIsParseeStanza(stanza) && to && *to == '@') diff --git a/src/include/Parsee.h b/src/include/Parsee.h index 030e748..5af946a 100644 --- a/src/include/Parsee.h +++ b/src/include/Parsee.h @@ -99,6 +99,20 @@ extern char * ParseeGenerateGetopt(const Argument *list); * Modifies: NOTHING */ extern void ParseeGenerateHelp(const Argument *list); +/** Generates a hexadecimal version of the SHA-256 digest of the + * original string. + * ---------------- + * Returns: a hexadecimal string[HEAP] + * Modifies: NOTHING */ +extern char * ParseeSHA256(char *string); + +/** Generates a hexadecimal version of the SHA-1 digest of the + * original string. + * ---------------- + * Returns: a hexadecimal string[HEAP] + * Modifies: NOTHING */ +extern char * ParseeSHA1(char *string); + /* Initialises a Parsee config from scratch, and writes to it * as JSON in the CWD. */ extern void ParseeConfigInit(void); diff --git a/src/include/XMPPCommands.x.h b/src/include/XMPPCommands.x.h index 688b0ca..d305340 100644 --- a/src/include/XMPPCommands.x.h +++ b/src/include/XMPPCommands.x.h @@ -12,7 +12,7 @@ XMPPSetFormTitle(cmd, "Admin addition form"); \ XMPPSetFormInstruction(cmd, "Select a glob pattern to add as an admin"); \ }) \ - XMPP_COMMAND(DelAdminCallback, "del-admin", "Removes a glob from admin rights", { \ + XMPP_COMMAND(DelAdminCallback, "del-admin", "Removes glob from being admin", { \ XMPPCmdOptionsCreator(cmd, FormDelAdminCallback); \ XMPPSetFormTitle(cmd, "Admin removal form"); \ XMPPSetFormInstruction(cmd, "Select a glob pattern to remove as an admin"); \