From: erickson Date: Fri, 18 Feb 2005 00:57:21 +0000 (+0000) Subject: sha.* is necessary for the component authentication, component is just an X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=cfe51f55885c4af32adb67ba2f2ea0b3272a5218;p=working%2FOpenSRF.git sha.* is necessary for the component authentication, component is just an example component test client git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@86 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/include/sha.h b/include/sha.h new file mode 100644 index 0000000..6c3d2d4 --- /dev/null +++ b/include/sha.h @@ -0,0 +1,41 @@ +// sha.h +// Jabber client library +// +// Original Code Copyright (C) 1999-2001 Dave Smith (dave@jabber.org) +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// Contributor(s): Julian Missig +// +// This Original Code has been modified by IBM Corporation. Modifications +// made by IBM described herein are Copyright (c) International Business +// Machines Corporation, 2002. +// +// Date Modified by Description of modification +// 01/20/2002 IBM Corp. Updated to libjudo 1.1.1 +// 2002-03-05 IBM Corp. Updated to libjudo 1.1.5 +// 2002-07-09 IBM Corp. Added Roster::getSession() +// +// ===================================================================================== + + +//#ifdef WIN32 + char* shahash(const char* str); +//#else +//extern "C" { +// char* shahash(const char* str); +//} +//#endif + diff --git a/src/libtransport/component.c b/src/libtransport/component.c new file mode 100644 index 0000000..0dcb70c --- /dev/null +++ b/src/libtransport/component.c @@ -0,0 +1,41 @@ +#include "opensrf/transport_client.h" +#include "signal.h" + + +/* connects and registers with the router */ +int main( int argc, char** argv ) { + + if( argc < 5 ) { + fatal_handler( "Usage: %s ", argv[0] ); + } + + int port = atoi(argv[2]); + transport_client* client = client_init( argv[1], port, 1 ); + + // try to connect, allow 15 second connect timeout + if( client_connect( client, argv[3], argv[4], "", 15 ) ) + info_handler("Connected...\n"); + else + fatal_handler( "NOT Connected...\n" ); + + transport_message* recv; + while( (recv=client_recv( client, -1)) ) { + if( recv->is_error ) + fprintf( stderr, "\nReceived Error\t: ------------------\nFrom:\t\t" + "%s\nRouterFrom:\t%s\nBody:\t\t%s\nType %s\nCode %d\n=> ", + recv->sender, recv->router_from, recv->body, recv->error_type, recv->error_code ); + else + fprintf( stderr, "\nReceived\t: ------------------\nFrom:\t\t" + "%s\nRouterFrom:\t%s\nBody:\t\t%s\n=> ", recv->sender, recv->router_from, recv->body ); + transport_message* send = message_init( "Hello...", "", "123454321", recv->sender, argv[3] ); + client_send_message( client, send ); + message_free( recv ); + message_free( send ); + } + return 0; + +} + + + + diff --git a/src/libtransport/sha.c b/src/libtransport/sha.c new file mode 100644 index 0000000..05cf14b --- /dev/null +++ b/src/libtransport/sha.c @@ -0,0 +1,262 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Gabber + * Copyright (C) 1999-2000 Dave Smith & Julian Missig + */ + + +/* + Implements the Secure Hash Algorithm (SHA1) + + Copyright (C) 1999 Scott G. Miller + + Released under the terms of the GNU General Public License v2 + see file COPYING for details + + Credits: + Robert Klep -- Expansion function fix + Thomas "temas" Muldowney : + -- shahash() for string fun + -- Will add the int32 stuff in a few + + --- + FIXME: This source takes int to be a 32 bit integer. This + may vary from system to system. I'd use autoconf if I was familiar + with it. Anyone want to help me out? +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#ifdef MACOS +# include +#else +# include +# include +#endif + +#include + +#ifndef WIN32 +# include +# define INT64 long long +#else +# include +# define snprintf _snprintf +# define INT64 __int64 +#endif + +#define switch_endianness(x) (x<<24 & 0xff000000) | \ + (x<<8 & 0x00ff0000) | \ + (x>>8 & 0x0000ff00) | \ + (x>>24 & 0x000000ff) + +/* Initial hash values */ +#define Ai 0x67452301 +#define Bi 0xefcdab89 +#define Ci 0x98badcfe +#define Di 0x10325476 +#define Ei 0xc3d2e1f0 + +/* SHA1 round constants */ +#define K1 0x5a827999 +#define K2 0x6ed9eba1 +#define K3 0x8f1bbcdc +#define K4 0xca62c1d6 + +/* Round functions. Note that f2() is used in both rounds 2 and 4 */ +#define f1(B,C,D) ((B & C) | ((~B) & D)) +#define f2(B,C,D) (B ^ C ^ D) +#define f3(B,C,D) ((B & C) | (B & D) | (C & D)) + +/* left circular shift functions (rotate left) */ +#define rol1(x) ((x<<1) | ((x>>31) & 1)) +#define rol5(A) ((A<<5) | ((A>>27) & 0x1f)) +#define rol30(B) ((B<<30) | ((B>>2) & 0x3fffffff)) + +/* + Hashes 'data', which should be a pointer to 512 bits of data (sixteen + 32 bit ints), into the ongoing 160 bit hash value (five 32 bit ints) + 'hash' +*/ +int +sha_hash(int *data, int *hash) +{ + int W[80]; + unsigned int A=hash[0], B=hash[1], C=hash[2], D=hash[3], E=hash[4]; + unsigned int t, x, TEMP; + + for (t=0; t<16; t++) + { +#ifndef WORDS_BIGENDIAN + W[t]=switch_endianness(data[t]); +#else + W[t]=data[t]; +#endif + } + + + /* SHA1 Data expansion */ + for (t=16; t<80; t++) + { + x=W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]; + W[t]=rol1(x); + } + + /* SHA1 main loop (t=0 to 79) + This is broken down into four subloops in order to use + the correct round function and constant */ + for (t=0; t<20; t++) + { + TEMP=rol5(A) + f1(B,C,D) + E + W[t] + K1; + E=D; + D=C; + C=rol30(B); + B=A; + A=TEMP; + } + for (; t<40; t++) + { + TEMP=rol5(A) + f2(B,C,D) + E + W[t] + K2; + E=D; + D=C; + C=rol30(B); + B=A; + A=TEMP; + } + for (; t<60; t++) + { + TEMP=rol5(A) + f3(B,C,D) + E + W[t] + K3; + E=D; + D=C; + C=rol30(B); + B=A; + A=TEMP; + } + for (; t<80; t++) + { + TEMP=rol5(A) + f2(B,C,D) + E + W[t] + K4; + E=D; + D=C; + C=rol30(B); + B=A; + A=TEMP; + } + hash[0]+=A; + hash[1]+=B; + hash[2]+=C; + hash[3]+=D; + hash[4]+=E; + return 0; +} + +/* + Takes a pointer to a 160 bit block of data (five 32 bit ints) and + intializes it to the start constants of the SHA1 algorithm. This + must be called before using hash in the call to sha_hash +*/ +int +sha_init(int *hash) +{ + hash[0]=Ai; + hash[1]=Bi; + hash[2]=Ci; + hash[3]=Di; + hash[4]=Ei; + return 0; +} + +int strprintsha(char *dest, int *hashval) +{ + int x; + char *hashstr = dest; + for (x=0; x<5; x++) + { + snprintf(hashstr, 9, "%08x", hashval[x]); + hashstr+=8; + } + snprintf(hashstr++, 1, "\0"); + + return 0; +} + +char *shahash(const char *str) +{ + char read_buffer[65]; + //int read_buffer[64]; + int c=1, i; + + INT64 length=0; + + int strsz; + static char final[40]; + int *hashval; + + hashval = (int *)malloc(20); + + sha_init(hashval); + + strsz = strlen(str); + + if(strsz == 0) + { + memset(read_buffer, 0, 65); + read_buffer[0] = 0x80; + sha_hash((int *)read_buffer, hashval); + } + + while (strsz>0) + { + memset(read_buffer, 0, 65); + strncpy((char*)read_buffer, str, 64); + c = strlen((char *)read_buffer); + length+=c; + strsz-=c; + if (strsz<=0) + { + length<<=3; + read_buffer[c]=(char)0x80; + for (i=c+1; i<64; i++) + read_buffer[i]=0; + if (c>55) + { + /* we need to do an entire new block */ + sha_hash((int *)read_buffer, hashval); + for (i=0; i<14; i++) + ((int*)read_buffer)[i]=0; + } +#ifndef WORDS_BIGENDIAN + for (i=0; i<8; i++) + { + read_buffer[56+i]=(char)(length>>(56-(i*8))) & 0xff; + } +#else + memcpy(read_buffer+56, &length, 8); +#endif + } + + sha_hash((int *)read_buffer, hashval); + str+=64; + } + + strprintsha((char *)final, hashval); + free(hashval); + return (char *)final; +}