From d99b4bb86fbde7ed3b727387a85d6d37c7029485 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 1 May 2007 16:12:02 +0000 Subject: [PATCH] misc fixes git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5053 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- libs/libdingaling/src/libdingaling.c | 23 +- libs/libdingaling/src/sha1.c | 788 ++++++++++++++++++------ libs/libdingaling/src/sha1.h | 129 ++-- src/mod/endpoints/mod_sofia/mod_sofia.c | 14 +- src/switch_odbc.c | 5 + 5 files changed, 679 insertions(+), 280 deletions(-) diff --git a/libs/libdingaling/src/libdingaling.c b/libs/libdingaling/src/libdingaling.c index 7b766d776d..d14d93fc44 100644 --- a/libs/libdingaling/src/libdingaling.c +++ b/libs/libdingaling/src/libdingaling.c @@ -35,7 +35,7 @@ #include #include #include - +#include #endif #include @@ -82,7 +82,7 @@ static int opt_timeout = 30; -static void sha1_hash(char *out, char *in, unsigned int len); +static void sha1_hash(char *out, unsigned char *in, unsigned int len); static int b64encode(unsigned char *in, size_t ilen, unsigned char *out, size_t olen); static void ldl_random_string(char *buf, uint16_t len, char *set); @@ -725,7 +725,7 @@ static ldl_avatar_t *ldl_get_avatar(ldl_handle_t *handle, char *path, char *from int fd = -1; size_t bytes; char *key; - char hash[128] = ""; + //char hash[128] = ""; if (from && (ap = (ldl_avatar_t *) apr_hash_get(globals.avatar_hash, from, APR_HASH_KEY_STRING))) { return ap; @@ -756,8 +756,7 @@ static ldl_avatar_t *ldl_get_avatar(ldl_handle_t *handle, char *path, char *from ap = malloc(sizeof(*ap)); assert(ap != NULL); memset(ap, 0, sizeof(*ap)); - ldl_random_string(hash, sizeof(hash) -1, NULL); - sha1_hash(ap->hash, hash, (unsigned)strlen(hash)); + sha1_hash(ap->hash, (unsigned char *) image, (unsigned int)bytes); ap->path = strdup(path); key = ldl_handle_strdup(handle, from); @@ -1048,21 +1047,21 @@ static int b64encode(unsigned char *in, size_t ilen, unsigned char *out, size_t return 0; } -static void sha1_hash(char *out, char *in, unsigned int len) +static void sha1_hash(char *out, unsigned char *in, unsigned int len) { - sha_context_t sha; + SHA1Context sha; char *p; int x; - unsigned char digest[20] = ""; + unsigned char digest[SHA1_HASH_SIZE] = ""; SHA1Init(&sha); - SHA1Update(&sha, (unsigned char *) in, len); + SHA1Update(&sha, in, len); - SHA1Final(digest, &sha); + SHA1Final(&sha, digest); p = out; - for (x = 0; x < 20; x++) { + for (x = 0; x < SHA1_HASH_SIZE; x++) { p += sprintf(p, "%2.2x", digest[x]); } } @@ -1084,7 +1083,7 @@ static int on_stream_component(ldl_handle_t *handle, int type, iks *node) char handshake[512] = ""; snprintf(secret, sizeof(secret), "%s%s", pak->id, handle->password); - sha1_hash(hash, secret, (unsigned)strlen(secret)); + sha1_hash(hash, (unsigned char *) secret, (unsigned int)strlen(secret)); snprintf(handshake, sizeof(handshake), "%s", hash); iks_send_raw(handle->parser, handshake); handle->state = CS_START; diff --git a/libs/libdingaling/src/sha1.c b/libs/libdingaling/src/sha1.c index 0c1b7e864b..513e4e0b7f 100644 --- a/libs/libdingaling/src/sha1.c +++ b/libs/libdingaling/src/sha1.c @@ -1,224 +1,626 @@ -/* - * libDingaLing XMPP Jingle Library - * Copyright (C) 2005/2006, Anthony Minessale II +/*- + * Copyright (c) 2001-2003 Allan Saddi + * All rights reserved. * - * Version: MPL 1.1 + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is libDingaLing XMPP Jingle Library - * - * The Initial Developer of the Original Code is - * Steve Reid - * - * Portions created by the Initial Developer are Copyright (C) - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * Steve Reid - * - * sha1.h + * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. * + * $Id: sha1.c 680 2003-07-25 21:57:38Z asaddi $ */ -/*! \file sha1.c - \brief SHA1 -*/ /* -ORIGINAL HEADERS ----------------------------------------------------------------------------------------------- -SHA-1 in C -By Steve Reid -100% Public Domain + * Define WORDS_BIGENDIAN if compiling on a big-endian architecture. + * + * Define SHA1_TEST to test the implementation using the NIST's + * sample messages. The output should be: + * + * a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d + * 84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1 + * 34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f + */ -Test Vectors (from FIPS PUB 180-1) -"abc" - A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D -"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" - 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 -A million repetitions of "a" - 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F -*/ +#ifdef HAVE_CONFIG_H +#include +#endif /* HAVE_CONFIG_H */ -/* #define LITTLE_ENDIAN * This should be #define'd if true. */ -/* #define SHA1HANDSOFF * Copies data before messing with it. */ - -#include -#include -#ifdef _MSC_VER -#ifndef __LITTLE_ENDIAN -#define __LITTLE_ENDIAN 1234 -#endif -#ifndef __BIG_ENDIAN -#define __BIG_ENDIAN 4321 -#endif -#ifndef __BYTE_ORDER -#define __BYTE_ORDER __LITTLE_ENDIAN -#endif +#if HAVE_INTTYPES_H +# include #else -#include "config.h" +# if HAVE_STDINT_H +# include +# endif #endif + +#include + #include "sha1.h" -#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) +#ifndef lint +static const char rcsid[] = + "$Id: sha1.c 680 2003-07-25 21:57:38Z asaddi $"; +#endif /* !lint */ -/* blk0() and blk() perform the initial expand. */ -/* I got the idea of expanding during the round function from SSLeay */ -#if __BYTE_ORDER == __LITTLE_ENDIAN -#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ - |(rol(block->l[i],8)&0x00FF00FF)) -#else -#define blk0(i) block->l[i] +#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) +#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) + +#define F_0_19(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define F_20_39(x, y, z) ((x) ^ (y) ^ (z)) +#define F_40_59(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) +#define F_60_79(x, y, z) ((x) ^ (y) ^ (z)) + +#define DO_ROUND(F, K) { \ + temp = ROTL(a, 5) + F(b, c, d) + e + *(W++) + K; \ + e = d; \ + d = c; \ + c = ROTL(b, 30); \ + b = a; \ + a = temp; \ +} + +#define K_0_19 0x5a827999L +#define K_20_39 0x6ed9eba1L +#define K_40_59 0x8f1bbcdcL +#define K_60_79 0xca62c1d6L + +#ifndef RUNTIME_ENDIAN + +#ifdef WORDS_BIGENDIAN + +#define BYTESWAP(x) (x) +#define BYTESWAP64(x) (x) + +#else /* WORDS_BIGENDIAN */ + +#define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \ + (ROTL((x), 8) & 0x00ff00ffL)) +#define BYTESWAP64(x) _byteswap64(x) + +static inline uint64_t _byteswap64(uint64_t x) +{ + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a); +} + +#endif /* WORDS_BIGENDIAN */ + +#else /* !RUNTIME_ENDIAN */ + +#define BYTESWAP(x) _byteswap(sc->littleEndian, x) +#define BYTESWAP64(x) _byteswap64(sc->littleEndian, x) + +#define _BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \ + (ROTL((x), 8) & 0x00ff00ffL)) +#define _BYTESWAP64(x) __byteswap64(x) + +static inline uint64_t __byteswap64(uint64_t x) +{ + uint32_t a = x >> 32; + uint32_t b = (uint32_t) x; + return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a); +} + +static inline uint32_t _byteswap(int littleEndian, uint32_t x) +{ + if (!littleEndian) + return x; + else + return _BYTESWAP(x); +} + +static inline uint64_t _byteswap64(int littleEndian, uint64_t x) +{ + if (!littleEndian) + return x; + else + return _BYTESWAP64(x); +} + +static inline void setEndian(int *littleEndianp) +{ + union { + uint32_t w; + uint8_t b[4]; + } endian; + + endian.w = 1L; + *littleEndianp = endian.b[0] != 0; +} + +#endif /* !RUNTIME_ENDIAN */ + +static const uint8_t padding[64] = { + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +void +SHA1Init (SHA1Context *sc) +{ +#ifdef RUNTIME_ENDIAN + setEndian (&sc->littleEndian); +#endif /* RUNTIME_ENDIAN */ + + sc->totalLength = 0LL; + sc->hash[0] = 0x67452301L; + sc->hash[1] = 0xefcdab89L; + sc->hash[2] = 0x98badcfeL; + sc->hash[3] = 0x10325476L; + sc->hash[4] = 0xc3d2e1f0L; + sc->bufferLength = 0L; +} + +static void +burnStack (int size) +{ + char buf[128]; + + memset (buf, 0, sizeof (buf)); + size -= sizeof (buf); + if (size > 0) + burnStack (size); +} + +static void +SHA1Guts (SHA1Context *sc, const uint32_t *cbuf) +{ + uint32_t buf[80]; + uint32_t *W, *W3, *W8, *W14, *W16; + uint32_t a, b, c, d, e, temp; + int i; + + W = buf; + + for (i = 15; i >= 0; i--) { + *(W++) = BYTESWAP(*cbuf); + cbuf++; + } + + W16 = &buf[0]; + W14 = &buf[2]; + W8 = &buf[8]; + W3 = &buf[13]; + + for (i = 63; i >= 0; i--) { + *W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++); + *W = ROTL(*W, 1); + W++; + } + + a = sc->hash[0]; + b = sc->hash[1]; + c = sc->hash[2]; + d = sc->hash[3]; + e = sc->hash[4]; + + W = buf; + +#ifndef SHA1_UNROLL +#define SHA1_UNROLL 20 +#endif /* !SHA1_UNROLL */ + +#if SHA1_UNROLL == 1 + for (i = 19; i >= 0; i--) + DO_ROUND(F_0_19, K_0_19); + + for (i = 19; i >= 0; i--) + DO_ROUND(F_20_39, K_20_39); + + for (i = 19; i >= 0; i--) + DO_ROUND(F_40_59, K_40_59); + + for (i = 19; i >= 0; i--) + DO_ROUND(F_60_79, K_60_79); +#elif SHA1_UNROLL == 2 + for (i = 9; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } + + for (i = 9; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } + + for (i = 9; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } + + for (i = 9; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } +#elif SHA1_UNROLL == 4 + for (i = 4; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } + + for (i = 4; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } + + for (i = 4; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } + + for (i = 4; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } +#elif SHA1_UNROLL == 5 + for (i = 3; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } + + for (i = 3; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } + + for (i = 3; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } + + for (i = 3; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } +#elif SHA1_UNROLL == 10 + for (i = 1; i >= 0; i--) { + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + } + + for (i = 1; i >= 0; i--) { + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + } + + for (i = 1; i >= 0; i--) { + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + } + + for (i = 1; i >= 0; i--) { + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + } +#elif SHA1_UNROLL == 20 + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + DO_ROUND(F_0_19, K_0_19); + + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + DO_ROUND(F_20_39, K_20_39); + + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + DO_ROUND(F_40_59, K_40_59); + + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); + DO_ROUND(F_60_79, K_60_79); +#else /* SHA1_UNROLL */ +#error SHA1_UNROLL must be 1, 2, 4, 5, 10 or 20! #endif -#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ - ^block->l[(i+2)&15]^block->l[i&15],1)) -/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ -#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); -#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); -#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); -#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); -#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); - - -/* Hash a single 512-bit block. This is the core of the algorithm. */ - -void SHA1Transform(uint32_t state[5], unsigned char buffer[64]) -{ -uint32_t a, b, c, d, e; -typedef union { - unsigned char c[64]; - uint32_t l[16]; -} CHAR64LONG16; -CHAR64LONG16* block; -#ifdef SHA1HANDSOFF -static unsigned char workspace[64]; - block = (CHAR64LONG16*)workspace; - memcpy(block, buffer, 64); -#else - block = (CHAR64LONG16*)buffer; -#endif - /* Copy context->state[] to working vars */ - a = state[0]; - b = state[1]; - c = state[2]; - d = state[3]; - e = state[4]; - /* 4 rounds of 20 operations each. Loop unrolled. */ - R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); - R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); - R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); - R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); - R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); - R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); - R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); - R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); - R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); - R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); - R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); - R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); - R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); - R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); - R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); - R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); - R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); - R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); - R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); - R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); - /* Add the working vars back into context.state[] */ - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; - state[4] += e; - /* Wipe variables */ - a = b = c = d = e = 0; + sc->hash[0] += a; + sc->hash[1] += b; + sc->hash[2] += c; + sc->hash[3] += d; + sc->hash[4] += e; } - -/* SHA1Init - Initialize new context */ - -void SHA1Init(sha_context_t* context) +void +SHA1Update (SHA1Context *sc, const void *vdata, uint32_t len) { - /* SHA1 initialization constants */ - context->state[0] = 0x67452301; - context->state[1] = 0xEFCDAB89; - context->state[2] = 0x98BADCFE; - context->state[3] = 0x10325476; - context->state[4] = 0xC3D2E1F0; - context->count[0] = context->count[1] = 0; + const uint8_t *data = vdata; + uint32_t bufferBytesLeft; + uint32_t bytesToCopy; + int needBurn = 0; + +#ifdef SHA1_FAST_COPY + if (sc->bufferLength) { + bufferBytesLeft = 64L - sc->bufferLength; + + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; + + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + + sc->totalLength += bytesToCopy * 8L; + + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; + + if (sc->bufferLength == 64L) { + SHA1Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } + } + + while (len > 63) { + sc->totalLength += 512L; + + SHA1Guts (sc, data); + needBurn = 1; + + data += 64L; + len -= 64L; + } + + if (len) { + memcpy (&sc->buffer.bytes[sc->bufferLength], data, len); + + sc->totalLength += len * 8L; + + sc->bufferLength += len; + } +#else /* SHA1_FAST_COPY */ + while (len) { + bufferBytesLeft = 64L - sc->bufferLength; + + bytesToCopy = bufferBytesLeft; + if (bytesToCopy > len) + bytesToCopy = len; + + memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy); + + sc->totalLength += bytesToCopy * 8L; + + sc->bufferLength += bytesToCopy; + data += bytesToCopy; + len -= bytesToCopy; + + if (sc->bufferLength == 64L) { + SHA1Guts (sc, sc->buffer.words); + needBurn = 1; + sc->bufferLength = 0L; + } + } +#endif /* SHA1_FAST_COPY */ + + if (needBurn) + burnStack (sizeof (uint32_t[86]) + sizeof (uint32_t *[5]) + sizeof (int)); } - -/* Run your data through this. */ - -void SHA1Update(sha_context_t* context, unsigned char* data, uint32_t len) +void +SHA1Final (SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE]) { -uint32_t i, j; + uint32_t bytesToPad; + uint64_t lengthPad; + int i; - j = (context->count[0] >> 3) & 63; - if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; - context->count[1] += (len >> 29); - if ((j + len) > 63) { - memcpy(&context->buffer[j], data, (i = 64-j)); - SHA1Transform(context->state, context->buffer); - for ( ; i + 63 < len; i += 64) { - SHA1Transform(context->state, &data[i]); - } - j = 0; + bytesToPad = 120L - sc->bufferLength; + if (bytesToPad > 64L) + bytesToPad -= 64L; + + lengthPad = BYTESWAP64(sc->totalLength); + + SHA1Update (sc, padding, bytesToPad); + SHA1Update (sc, &lengthPad, 8L); + + if (hash) { + for (i = 0; i < SHA1_HASH_WORDS; i++) { +#ifdef SHA1_FAST_COPY + *((uint32_t *) hash) = BYTESWAP(sc->hash[i]); +#else /* SHA1_FAST_COPY */ + hash[0] = (uint8_t) (sc->hash[i] >> 24); + hash[1] = (uint8_t) (sc->hash[i] >> 16); + hash[2] = (uint8_t) (sc->hash[i] >> 8); + hash[3] = (uint8_t) sc->hash[i]; +#endif /* SHA1_FAST_COPY */ + hash += 4; } - else i = 0; - memcpy(&context->buffer[j], &data[i], len - i); + } } +#ifdef SHA1_TEST -/* Add padding and return the message digest. */ +#include +#include +#include -void SHA1Final(unsigned char digest[20], sha_context_t* context) +int +main (int argc, char *argv[]) { -uint32_t i, j; -unsigned char finalcount[8]; + SHA1Context foo; + uint8_t hash[SHA1_HASH_SIZE]; + char buf[1000]; + int i; - for (i = 0; i < 8; i++) { - finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] - >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ - } - SHA1Update(context, (unsigned char *)"\200", 1); - while ((context->count[0] & 504) != 448) { - SHA1Update(context, (unsigned char *)"\0", 1); - } - SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ - for (i = 0; i < 20; i++) { - digest[i] = (unsigned char) - ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); - } - /* Wipe variables */ - i = j = 0; - memset(context->buffer, 0, 64); - memset(context->state, 0, 20); - memset(context->count, 0, 8); - memset(&finalcount, 0, 8); -#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */ - SHA1Transform(context->state, context->buffer); -#endif + SHA1Init (&foo); + SHA1Update (&foo, "abc", 3); + SHA1Final (&foo, hash); + + for (i = 0; i < SHA1_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); + + SHA1Init (&foo); + SHA1Update (&foo, + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + 56); + SHA1Final (&foo, hash); + + for (i = 0; i < SHA1_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); + + SHA1Init (&foo); + memset (buf, 'a', sizeof (buf)); + for (i = 0; i < 1000; i++) + SHA1Update (&foo, buf, sizeof (buf)); + SHA1Final (&foo, hash); + + for (i = 0; i < SHA1_HASH_SIZE;) { + printf ("%02x", hash[i++]); + if (!(i % 4)) + printf (" "); + } + printf ("\n"); + + exit (0); } -/* For Emacs: - * Local Variables: - * mode:c - * indent-tabs-mode:t - * tab-width:4 - * c-basic-offset:4 - * End: - * For VIM: - * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: - */ +#endif /* SHA1_TEST */ diff --git a/libs/libdingaling/src/sha1.h b/libs/libdingaling/src/sha1.h index b32c3ffe92..7a6beb7f5d 100644 --- a/libs/libdingaling/src/sha1.h +++ b/libs/libdingaling/src/sha1.h @@ -1,83 +1,72 @@ -/* - * libDingaLing XMPP Jingle Library - * Copyright (C) 2005/2006, Anthony Minessale II +/*- + * Copyright (c) 2001-2003 Allan Saddi + * All rights reserved. * - * Version: MPL 1.1 + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is libDingaLing XMPP Jingle Library - * - * The Initial Developer of the Original Code is - * Steve Reid - * Portions created by the Initial Developer are Copyright (C) - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * - * Steve Reid - * - * sha1.h + * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. * + * $Id: sha1.h 347 2003-02-23 22:11:49Z asaddi $ */ -/*! \file sha1.h - \brief SHA1 -*/ -#ifndef __SHA1_H -#define __SHA1_H +#ifndef _SHA1_H +#define _SHA1_H + +#if HAVE_INTTYPES_H +# include +#else +# if HAVE_STDINT_H +# include +# endif +#endif + +#define SHA1_HASH_SIZE 20 + +/* Hash size in 32-bit words */ +#define SHA1_HASH_WORDS 5 + +struct _SHA1Context { + uint64_t totalLength; + uint32_t hash[SHA1_HASH_WORDS]; + uint32_t bufferLength; + union { + uint32_t words[16]; + uint8_t bytes[64]; + } buffer; +#ifdef RUNTIME_ENDIAN + int littleEndian; +#endif /* RUNTIME_ENDIAN */ +}; + +typedef struct _SHA1Context SHA1Context; + #ifdef __cplusplus extern "C" { #endif -#ifdef __STUPIDFORMATBUG__ -} -#endif -#undef inline -#define inline __inline - -#ifndef uint32_t -#ifdef WIN32 -typedef unsigned __int8 uint8_t; -typedef unsigned __int16 uint16_t; -typedef unsigned __int32 uint32_t; -typedef unsigned __int64 uint64_t; -typedef __int8 int8_t; -typedef __int16 int16_t; -typedef __int32 int32_t; -typedef __int64 int64_t; -typedef unsigned long in_addr_t; -#else -#include -#include -#include -#include -#include -#include -#include -#endif -#endif - -typedef struct { - uint32_t state[5]; - uint32_t count[2]; - unsigned char buffer[64]; -} sha_context_t; - -void SHA1Transform(uint32_t state[5], unsigned char buffer[64]); -void SHA1Init(sha_context_t* context); -void SHA1Update(sha_context_t* context, unsigned char* data, uint32_t len); -void SHA1Final(unsigned char digest[20], sha_context_t* context); +void SHA1Init (SHA1Context *sc); +void SHA1Update (SHA1Context *sc, const void *data, uint32_t len); +void SHA1Final (SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE]); #ifdef __cplusplus } #endif -#endif + +#endif /* _SHA1_H */ diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index 3569922728..24dc2c4e89 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -1420,15 +1420,11 @@ SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_mod switch_find_local_ip(mod_sofia_globals.guess_ip, sizeof(mod_sofia_globals.guess_ip), AF_INET); - if (switch_event_bind((char *) modname, SWITCH_EVENT_CUSTOM, MULTICAST_EVENT, event_handler, NULL) != SWITCH_STATUS_SUCCESS) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n"); - return SWITCH_STATUS_TERM; - } switch_core_hash_init(&mod_sofia_globals.profile_hash, module_pool); switch_core_hash_init(&mod_sofia_globals.gateway_hash, module_pool); switch_mutex_init(&mod_sofia_globals.hash_mutex, SWITCH_MUTEX_NESTED, module_pool); - + if (config_sofia(0, NULL) != SWITCH_STATUS_SUCCESS) { return SWITCH_STATUS_GENERR; } @@ -1437,6 +1433,14 @@ SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_mod mod_sofia_globals.running = 1; switch_mutex_unlock(mod_sofia_globals.mutex); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Waiting for profiles to start\n"); + switch_yield(1500000); + + if (switch_event_bind((char *) modname, SWITCH_EVENT_CUSTOM, MULTICAST_EVENT, event_handler, NULL) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n"); + return SWITCH_STATUS_TERM; + } + if (switch_event_bind((char *) modname, SWITCH_EVENT_PRESENCE_IN, SWITCH_EVENT_SUBCLASS_ANY, sofia_presence_event_handler, NULL) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n"); diff --git a/src/switch_odbc.c b/src/switch_odbc.c index 58d84e4b95..127362b3d1 100644 --- a/src/switch_odbc.c +++ b/src/switch_odbc.c @@ -176,6 +176,11 @@ static int db_is_up(switch_odbc_handle_t *handle) char *err_str = NULL; SQLCHAR sql[] = "select 1"; + if (!handle) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "No DB Handle\n"); + goto done; + } + if (SQLAllocHandle(SQL_HANDLE_STMT, handle->con, &stmt) != SQL_SUCCESS) { goto error; }