XEP-0114: minor fixes to auth.c

- src/sha1.c is used instead of openssl;
 - xmpp_alloc/free should be used instead of malloc/free;
 - coding style fixes.
This commit is contained in:
Dmitry Podgorny
2014-08-26 14:23:02 +03:00
parent 5d76e052ec
commit 3b906f6ceb
2 changed files with 28 additions and 42 deletions

View File

@@ -1,6 +1,7 @@
0.8.7 0.8.7
- SCRAM-SHA-1 authentication mechanism - SCRAM-SHA-1 authentication mechanism
- pkg-config support - pkg-config support
- XEP-0114 support
0.8.5 0.8.5
- libtoolize to generate .so - libtoolize to generate .so

View File

@@ -18,13 +18,10 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
/* Added by Milan Kubik. <email@kubikmilan.sk>
* Needed for component handshake handler (xep-0114) to compute SHA1 digest. */
#include <openssl/evp.h>
#include "strophe.h" #include "strophe.h"
#include "common.h" #include "common.h"
#include "sasl.h" #include "sasl.h"
#include "sha1.h"
#ifdef _MSC_VER #ifdef _MSC_VER
#define strcasecmp stricmp #define strcasecmp stricmp
@@ -1171,16 +1168,10 @@ void auth_handle_component_open(xmpp_conn_t * const conn)
/* reset all timed handlers */ /* reset all timed handlers */
handler_reset_timed(conn, 0); handler_reset_timed(conn, 0);
/* Handler for component accept */ handler_add(conn, _handle_error, NULL, "stream:error", NULL, NULL);
handler_add(conn, _handle_error,
NULL, "stream:error", NULL, NULL);
handler_add(conn, _handle_component_hs_response, NULL, handler_add(conn, _handle_component_hs_response, NULL,
"handshake", NULL, NULL); "handshake", NULL, NULL);
handler_add_timed(conn, _handle_missing_handshake, HANDSHAKE_TIMEOUT, NULL);
handler_add_timed(conn, _handle_missing_handshake,
HANDSHAKE_TIMEOUT, NULL);
_handle_component_auth(conn); _handle_component_auth(conn);
} }
@@ -1188,41 +1179,37 @@ void auth_handle_component_open(xmpp_conn_t * const conn)
/* Will compute SHA1 and authenticate the component to the server */ /* Will compute SHA1 and authenticate the component to the server */
int _handle_component_auth(xmpp_conn_t * const conn) int _handle_component_auth(xmpp_conn_t * const conn)
{ {
unsigned char md_value[EVP_MAX_MD_SIZE]; uint8_t md_value[SHA1_DIGEST_SIZE];
EVP_MD_CTX *mdctx; SHA1_CTX mdctx;
const EVP_MD *md;
char *digest; char *digest;
unsigned int md_len, i; size_t i;
/* Feed the session id and passphrase to the algorithm. /* Feed the session id and passphrase to the algorithm.
* We need to compute SHA1(session_id + passphrase) * We need to compute SHA1(session_id + passphrase)
*/ */
md = EVP_get_digestbyname("sha1"); SHA1_Init(&mdctx);
mdctx = EVP_MD_CTX_create(); SHA1_Update(&mdctx, (uint8_t*)conn->stream_id, strlen(conn->stream_id));
EVP_DigestInit_ex(mdctx, md, NULL); SHA1_Update(&mdctx, (uint8_t*)conn->pass, strlen(conn->pass));
SHA1_Final(&mdctx, md_value);
/* Feed in the data. */ digest = xmpp_alloc(conn->ctx, 2*sizeof(md_value)+1);
EVP_DigestUpdate(mdctx, conn->stream_id, strlen(conn->stream_id));
EVP_DigestUpdate(mdctx, conn->pass, strlen(conn->pass));
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_destroy(mdctx);
digest = malloc(2*md_len+1);
if (digest) { if (digest) {
memset(digest, 0, 2*md_len+1);
/* convert the digest into string representation */ /* convert the digest into string representation */
for (i = 0; i < md_len; i++) for (i = 0; i < sizeof(md_value); i++)
snprintf(digest+i*2, 3,"%02x", md_value[i]); snprintf(digest+i*2, 3, "%02x", md_value[i]);
digest[2*sizeof(md_value)] = '\0';
xmpp_debug(conn->ctx, "auth", "Digest: %s, len: %d", (char *) digest, strlen((char *)digest)); xmpp_debug(conn->ctx, "auth", "Digest: %s, len: %d",
digest, strlen(digest));
/* Send the digest to the server */ /* Send the digest to the server */
xmpp_send_raw_string(conn, "<handshake xmlns='%s'>%s</handshake>", xmpp_send_raw_string(conn, "<handshake xmlns='%s'>%s</handshake>",
XMPP_NS_COMPONENT, digest); XMPP_NS_COMPONENT, digest);
xmpp_debug(conn->ctx, "auth", "Sent component handshake to the server."); xmpp_debug(conn->ctx, "auth", "Sent component handshake to the server.");
free(digest); xmpp_free(conn->ctx, digest);
} else { } else {
xmpp_debug(conn->ctx, "auth", "Couldn't allocate memory for component handshake digest."); xmpp_debug(conn->ctx, "auth", "Couldn't allocate memory for component "\
"handshake digest.");
xmpp_disconnect(conn); xmpp_disconnect(conn);
return XMPP_EMEM; return XMPP_EMEM;
} }
@@ -1230,29 +1217,27 @@ int _handle_component_auth(xmpp_conn_t * const conn)
return 0; return 0;
} }
/* Check if the received stanza is <handshake/> and set auth to true
* and fire connection handler.
*/
int _handle_component_hs_response(xmpp_conn_t * const conn, int _handle_component_hs_response(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, xmpp_stanza_t * const stanza,
void * const userdata) void * const userdata)
{ {
/* Check if the received stanza is <handshake/> and set auth to true char *name;
* and fire connection handler.
*/
char * name;
xmpp_timed_handler_delete(conn, _handle_missing_handshake); xmpp_timed_handler_delete(conn, _handle_missing_handshake);
name = xmpp_stanza_get_name(stanza); name = xmpp_stanza_get_name(stanza);
if (strcmp(name, "handshake") != 0) { if (strcmp(name, "handshake") != 0) {
char *msg; char *msg;
size_t msg_size; size_t msg_size;
xmpp_stanza_to_text(stanza, &msg, &msg_size); xmpp_stanza_to_text(stanza, &msg, &msg_size);
if (msg) {
xmpp_debug(conn->ctx, "auth", "Handshake failed: %s", xmpp_debug(conn->ctx, "auth", "Handshake failed: %s", msg);
msg); xmpp_free(conn->ctx, msg);
}
xmpp_disconnect(conn); xmpp_disconnect(conn);
free(msg);
return XMPP_EINT; return XMPP_EINT;
} else { } else {
conn->authenticated = 1; conn->authenticated = 1;