[MOD] Use specific SHA1/256 abstraction functions

This commit is contained in:
LDA 2024-08-19 16:40:30 +02:00
commit 4c158ea186
9 changed files with 80 additions and 22 deletions

View file

@ -25,8 +25,7 @@ JoinMUC(ParseeData *data, HashMap *event, char *jid, char *muc, char *name)
{ {
char *nonce_str = StrInt(nonce); char *nonce_str = StrInt(nonce);
char *input = StrConcat(4, sender, name, data->id, nonce_str); char *input = StrConcat(4, sender, name, data->id, nonce_str);
unsigned char *digest = Sha256(input); char *hex = ParseeSHA256(input);
char *hex = ShaToHex(digest);
if (strlen(hex) >= 8) if (strlen(hex) >= 8)
{ {
@ -41,7 +40,6 @@ JoinMUC(ParseeData *data, HashMap *event, char *jid, char *muc, char *name)
nonce++; nonce++;
Free(nonce_str); Free(nonce_str);
Free(digest);
Free(input); Free(input);
Free(hex); Free(hex);
} }

57
src/Parsee/SHA.c Normal file
View file

@ -0,0 +1,57 @@
#include <Parsee.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Sha.h>
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;
}

View file

@ -12,8 +12,6 @@ static HashMap *nick_table = NULL;
static char * static char *
GenerateKey(char *muc, char *mxid) GenerateKey(char *muc, char *mxid)
{ {
unsigned char *shaDigest;
char *concatStr; char *concatStr;
char *hexDigest; char *hexDigest;
if (!muc || !mxid) if (!muc || !mxid)
@ -22,10 +20,8 @@ GenerateKey(char *muc, char *mxid)
} }
concatStr = StrConcat(3, muc, ":", mxid); concatStr = StrConcat(3, muc, ":", mxid);
shaDigest = Sha256(concatStr); hexDigest = ParseeSHA256(concatStr);
hexDigest = ShaToHex(shaDigest);
Free (shaDigest);
Free (concatStr); Free (concatStr);
return hexDigest; return hexDigest;
} }

View file

@ -295,19 +295,17 @@ char *
ParseeGetDMID(char *mxid, char *jid) ParseeGetDMID(char *mxid, char *jid)
{ {
char *concat, *sha; char *concat, *sha;
unsigned char *raw;
if (!mxid || !jid) if (!mxid || !jid)
{ {
return NULL; return NULL;
} }
/* We really don't care about safety here. */
jid = ParseeTrimJID(jid); jid = ParseeTrimJID(jid);
concat = StrConcat(2, mxid, jid); concat = StrConcat(2, mxid, jid);
raw = Sha1(concat); sha = ParseeSHA1(concat);
sha = ShaToHex(raw);
Free(concat); Free(concat);
Free(raw);
Free(jid); Free(jid);
return sha; return sha;

View file

@ -11,6 +11,7 @@
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
#include <Parsee.h>
#include <XML.h> #include <XML.h>
/* The default component port Prosody uses. */ /* The default component port Prosody uses. */
@ -81,14 +82,11 @@ static char *
ComputeHandshake(char *shared, char *stream) ComputeHandshake(char *shared, char *stream)
{ {
char *source; char *source;
unsigned char *raw_sha;
char *sha; char *sha;
source = StrConcat(2, stream, shared); source = StrConcat(2, stream, shared);
raw_sha = Sha1(source); sha = ParseeSHA1(source);
sha = ShaToHex(raw_sha);
Free(raw_sha);
Free(source); Free(source);
return sha; return sha;

View file

@ -166,7 +166,6 @@ ServerHasXEP421(ParseeData *data, char *from)
static char * static char *
ScrambleOID(ParseeData *data, char *opaque_oid) ScrambleOID(ParseeData *data, char *opaque_oid)
{ {
unsigned char *raw;
char *sha, *mxid; char *sha, *mxid;
const ParseeConfig *c = data->config; const ParseeConfig *c = data->config;
if (!opaque_oid) if (!opaque_oid)
@ -175,9 +174,7 @@ ScrambleOID(ParseeData *data, char *opaque_oid)
} }
/* Turns this into a 128-byte long, Matrix-safe value. */ /* Turns this into a 128-byte long, Matrix-safe value. */
raw = Sha256(opaque_oid); sha = ParseeSHA256(opaque_oid);
sha = ShaToHex(raw);
Free(raw);
/* TODO: Either mark this specially, or drop Parsee JID flags /* TODO: Either mark this specially, or drop Parsee JID flags
* altogether before any 1.0.0 */ * altogether before any 1.0.0 */

View file

@ -165,7 +165,7 @@ end_error:
room = ParseeFindDMRoom(args, to, from); room = ParseeFindDMRoom(args, to, from);
data = body ? ArrayGet(body->children, 0) : NULL; data = body ? ArrayGet(body->children, 0) : NULL;
/* TODO: CLEAN THAT UP */ /* TODO: CLEAN THAT UP INTO A CREATEDM FUNCTION */
mroom_id = ParseeGetBridgedRoom(args, stanza); mroom_id = ParseeGetBridgedRoom(args, stanza);
if (!mroom_id && !room && !XMPPIsParseeStanza(stanza) && if (!mroom_id && !room && !XMPPIsParseeStanza(stanza) &&
to && *to == '@') to && *to == '@')

View file

@ -99,6 +99,20 @@ extern char * ParseeGenerateGetopt(const Argument *list);
* Modifies: NOTHING */ * Modifies: NOTHING */
extern void ParseeGenerateHelp(const Argument *list); 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 /* Initialises a Parsee config from scratch, and writes to it
* as JSON in the CWD. */ * as JSON in the CWD. */
extern void ParseeConfigInit(void); extern void ParseeConfigInit(void);

View file

@ -12,7 +12,7 @@
XMPPSetFormTitle(cmd, "Admin addition form"); \ XMPPSetFormTitle(cmd, "Admin addition form"); \
XMPPSetFormInstruction(cmd, "Select a glob pattern to add as an admin"); \ 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); \ XMPPCmdOptionsCreator(cmd, FormDelAdminCallback); \
XMPPSetFormTitle(cmd, "Admin removal form"); \ XMPPSetFormTitle(cmd, "Admin removal form"); \
XMPPSetFormInstruction(cmd, "Select a glob pattern to remove as an admin"); \ XMPPSetFormInstruction(cmd, "Select a glob pattern to remove as an admin"); \