1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Moved sha1 generation to caps module

This commit is contained in:
James Booth 2013-01-21 22:18:15 +00:00
parent 7991bc5154
commit c828e397b6
4 changed files with 108 additions and 105 deletions

View File

@ -24,9 +24,12 @@
#include <string.h>
#include <glib.h>
#include <openssl/evp.h>
#include <strophe.h>
#include "common.h"
#include "capabilities.h"
#include "stanza.h"
static GHashTable *capabilities;
@ -65,6 +68,107 @@ caps_get(const char * const caps_str)
return g_hash_table_lookup(capabilities, caps_str);
}
char *
caps_get_sha1_str(xmpp_stanza_t *query)
{
GSList *identities = NULL;
GSList *features = NULL;
GSList *form_names = NULL;
GHashTable *forms = g_hash_table_new(g_str_hash, g_str_equal);
GString *s = g_string_new("");
xmpp_stanza_t *child = xmpp_stanza_get_children(query);
while (child != NULL) {
if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_IDENTITY) == 0) {
char *category = xmpp_stanza_get_attribute(child, "category");
char *type = xmpp_stanza_get_attribute(child, "type");
char *lang = xmpp_stanza_get_attribute(child, "xml:lang");
char *name = xmpp_stanza_get_attribute(child, "name");
GString *identity_str = g_string_new(category);
g_string_append(identity_str, "/");
if (type != NULL) {
g_string_append(identity_str, type);
}
g_string_append(identity_str, "/");
if (lang != NULL) {
g_string_append(identity_str, lang);
}
g_string_append(identity_str, "/");
if (name != NULL) {
g_string_append(identity_str, name);
}
g_string_append(identity_str, "<");
identities = g_slist_insert_sorted(identities, identity_str->str, (GCompareFunc)octet_compare);
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_FEATURE) == 0) {
char *feature_str = xmpp_stanza_get_attribute(child, "var");
features = g_slist_insert_sorted(features, feature_str, (GCompareFunc)octet_compare);
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) {
if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) {
DataForm *form = stanza_get_form(child);
form_names = g_slist_insert_sorted(form_names, form->form_type, (GCompareFunc)octet_compare);
g_hash_table_insert(forms, form->form_type, form);
}
}
child = xmpp_stanza_get_next(child);
}
GSList *curr = identities;
while (curr != NULL) {
g_string_append(s, curr->data);
curr = g_slist_next(curr);
}
curr = features;
while (curr != NULL) {
g_string_append(s, curr->data);
g_string_append(s, "<");
curr = g_slist_next(curr);
}
curr = form_names;
while (curr != NULL) {
DataForm *form = g_hash_table_lookup(forms, curr->data);
g_string_append(s, form->form_type);
g_string_append(s, "<");
GSList *curr_field = form->fields;
while (curr_field != NULL) {
FormField *field = curr_field->data;
g_string_append(s, field->var);
GSList *curr_value = field->values;
while (curr_value != NULL) {
g_string_append(s, curr_value->data);
g_string_append(s, "<");
curr_value = g_slist_next(curr_value);
}
curr_field = g_slist_next(curr_value);
}
}
EVP_MD_CTX mdctx;
const EVP_MD *md;
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname("SHA1");
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, s->str, strlen(s->str));
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);
char *result = g_base64_encode(md_value, md_len);
g_string_free(s, TRUE);
g_slist_free(identities);
g_slist_free(features);
return result;
}
void
caps_close(void)
{

View File

@ -24,6 +24,7 @@
#define CAPABILITIES_H
#include <glib.h>
#include <strophe.h>
typedef struct capabilities_t {
char *client;
@ -33,6 +34,7 @@ void caps_init(void);
void caps_add(const char * const caps_str, const char * const client);
gboolean caps_contains(const char * const caps_str);
Capabilities* caps_get(const char * const caps_str);
char* caps_get_sha1_str(xmpp_stanza_t * const query);
void caps_close(void);
#endif

View File

@ -24,7 +24,6 @@
#include <stdlib.h>
#include <assert.h>
#include <openssl/evp.h>
#include <strophe.h>
#include "capabilities.h"
@ -185,107 +184,6 @@ jabber_disconnect(void)
}
}
char *
sha1_caps_str(xmpp_stanza_t *query)
{
GSList *identities = NULL;
GSList *features = NULL;
GSList *form_names = NULL;
GHashTable *forms = g_hash_table_new(g_str_hash, g_str_equal);
GString *s = g_string_new("");
xmpp_stanza_t *child = xmpp_stanza_get_children(query);
while (child != NULL) {
if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_IDENTITY) == 0) {
char *category = xmpp_stanza_get_attribute(child, "category");
char *type = xmpp_stanza_get_attribute(child, "type");
char *lang = xmpp_stanza_get_attribute(child, "xml:lang");
char *name = xmpp_stanza_get_attribute(child, "name");
GString *identity_str = g_string_new(category);
g_string_append(identity_str, "/");
if (type != NULL) {
g_string_append(identity_str, type);
}
g_string_append(identity_str, "/");
if (lang != NULL) {
g_string_append(identity_str, lang);
}
g_string_append(identity_str, "/");
if (name != NULL) {
g_string_append(identity_str, name);
}
g_string_append(identity_str, "<");
identities = g_slist_insert_sorted(identities, identity_str->str, (GCompareFunc)octet_compare);
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_FEATURE) == 0) {
char *feature_str = xmpp_stanza_get_attribute(child, "var");
features = g_slist_insert_sorted(features, feature_str, (GCompareFunc)octet_compare);
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) {
if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) {
DataForm *form = stanza_get_form(child);
form_names = g_slist_insert_sorted(form_names, form->form_type, (GCompareFunc)octet_compare);
g_hash_table_insert(forms, form->form_type, form);
}
}
child = xmpp_stanza_get_next(child);
}
GSList *curr = identities;
while (curr != NULL) {
g_string_append(s, curr->data);
curr = g_slist_next(curr);
}
curr = features;
while (curr != NULL) {
g_string_append(s, curr->data);
g_string_append(s, "<");
curr = g_slist_next(curr);
}
curr = form_names;
while (curr != NULL) {
DataForm *form = g_hash_table_lookup(forms, curr->data);
g_string_append(s, form->form_type);
g_string_append(s, "<");
GSList *curr_field = form->fields;
while (curr_field != NULL) {
FormField *field = curr_field->data;
g_string_append(s, field->var);
GSList *curr_value = field->values;
while (curr_value != NULL) {
g_string_append(s, curr_value->data);
g_string_append(s, "<");
curr_value = g_slist_next(curr_value);
}
curr_field = g_slist_next(curr_value);
}
}
EVP_MD_CTX mdctx;
const EVP_MD *md;
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname("SHA1");
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, s->str, strlen(s->str));
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);
char *result = g_base64_encode(md_value, md_len);
g_string_free(s, TRUE);
g_slist_free(identities);
g_slist_free(features);
return result;
}
void
jabber_process_events(void)
{
@ -569,7 +467,7 @@ jabber_update_presence(jabber_presence_t status, const char * const msg,
xmpp_stanza_add_child(query, feature_caps);
xmpp_stanza_add_child(query, feature_version);
char *sha1 = sha1_caps_str(query);
char *sha1 = caps_get_sha1_str(query);
xmpp_stanza_set_attribute(caps, STANZA_ATTR_VER, sha1);
xmpp_stanza_add_child(presence, caps);
@ -1232,7 +1130,7 @@ _disco_response_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
// validate sha1
gchar **split = g_strsplit(node, "#", -1);
char *given_sha1 = split[1];
char *generated_sha1 = sha1_caps_str(query);
char *generated_sha1 = caps_get_sha1_str(query);
if (g_strcmp0(given_sha1, generated_sha1) != 0) {
log_info("Invalid SHA1 recieved for caps.");

View File

@ -83,6 +83,5 @@ char * jabber_get_status(void);
void jabber_free_resources(void);
void jabber_restart(void);
void jabber_set_autoping(int seconds);
char * sha1_caps_str(xmpp_stanza_t *query);
#endif