1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
* Ostatnia aktualizacja:
*
* - $Id: auth.c,v 1.19 2002/11/25 18:20:49 mati Exp $
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libtlen.h"
static void shaHashBlock (j_SHA_CTX * ctx);
void
shaInit (j_SHA_CTX * ctx)
{
int i;
ctx->lenW = 0;
ctx->sizeHi = ctx->sizeLo = 0;
/* Initialize H with the magic constants (see FIPS180 for constants)
*/
ctx->H[0] = 0x67452301L;
ctx->H[1] = 0xefcdab89L;
ctx->H[2] = 0x98badcfeL;
ctx->H[3] = 0x10325476L;
ctx->H[4] = 0xc3d2e1f0L;
for (i = 0; i < 80; i++)
ctx->W[i] = 0;
}
void
shaUpdate (j_SHA_CTX * ctx, unsigned char *dataIn, int len)
{
int i;
/* Read the data into W and process blocks as they get full
*/
for (i = 0; i < len; i++)
{
ctx->W[ctx->lenW / 4] <<= 8;
ctx->W[ctx->lenW / 4] |= (unsigned long) dataIn[i];
if ((++ctx->lenW) % 64 == 0)
{
shaHashBlock (ctx);
ctx->lenW = 0;
}
ctx->sizeLo += 8;
ctx->sizeHi += (ctx->sizeLo < 8);
}
}
void
shaFinal (j_SHA_CTX * ctx, unsigned char hashout[20])
{
unsigned char pad0x80 = 0x80;
unsigned char pad0x00 = 0x00;
unsigned char padlen[8];
int i;
/* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
*/
padlen[0] = (unsigned char) ((ctx->sizeHi >> 24) & 255);
padlen[1] = (unsigned char) ((ctx->sizeHi >> 16) & 255);
padlen[2] = (unsigned char) ((ctx->sizeHi >> 8) & 255);
padlen[3] = (unsigned char) ((ctx->sizeHi >> 0) & 255);
padlen[4] = (unsigned char) ((ctx->sizeLo >> 24) & 255);
padlen[5] = (unsigned char) ((ctx->sizeLo >> 16) & 255);
padlen[6] = (unsigned char) ((ctx->sizeLo >> 8) & 255);
padlen[7] = (unsigned char) ((ctx->sizeLo >> 0) & 255);
shaUpdate (ctx, &pad0x80, 1);
while (ctx->lenW != 56)
shaUpdate (ctx, &pad0x00, 1);
shaUpdate (ctx, padlen, 8);
/* Output hash
*/
for (i = 0; i < 20; i++)
{
hashout[i] = (unsigned char) (ctx->H[i / 4] >> 24);
ctx->H[i / 4] <<= 8;
}
/*
* Re-initialize the context (also zeroizes contents)
*/
shaInit (ctx);
}
void
shaBlock (unsigned char *dataIn, int len, unsigned char hashout[20])
{
j_SHA_CTX ctx;
shaInit (&ctx);
shaUpdate (&ctx, dataIn, len);
shaFinal (&ctx, hashout);
}
#define SHA_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xffffffffL)
static void
shaHashBlock (j_SHA_CTX * ctx)
{
int t;
unsigned long A, B, C, D, E, TEMP;
for (t = 16; t <= 79; t++)
ctx->W[t] =
SHA_ROTL (ctx->W[t - 3] ^ ctx->W[t - 8] ^ ctx->
W[t - 14] ^ ctx->W[t - 16], 1);
A = ctx->H[0];
B = ctx->H[1];
C = ctx->H[2];
D = ctx->H[3];
E = ctx->H[4];
for (t = 0; t <= 19; t++)
{
TEMP = (SHA_ROTL (A, 5) + (((C ^ D) & B) ^ D) + E +
ctx->W[t] + 0x5a827999L) & 0xffffffffL;
E = D;
D = C;
C = SHA_ROTL (B, 30);
B = A;
A = TEMP;
}
for (t = 20; t <= 39; t++)
{
TEMP = (SHA_ROTL (A, 5) + (B ^ C ^ D) + E + ctx->W[t] +
0x6ed9eba1L) & 0xffffffffL;
E = D;
D = C;
C = SHA_ROTL (B, 30);
B = A;
A = TEMP;
}
for (t = 40; t <= 59; t++)
{
TEMP = (SHA_ROTL (A, 5) + ((B & C) | (D & (B | C))) + E +
ctx->W[t] + 0x8f1bbcdcL) & 0xffffffffL;
E = D;
D = C;
C = SHA_ROTL (B, 30);
B = A;
A = TEMP;
}
for (t = 60; t <= 79; t++)
{
TEMP = (SHA_ROTL (A, 5) + (B ^ C ^ D) + E + ctx->W[t] +
0xca62c1d6L) & 0xffffffffL;
E = D;
D = C;
C = SHA_ROTL (B, 30);
B = A;
A = TEMP;
}
ctx->H[0] += A;
ctx->H[1] += B;
ctx->H[2] += C;
ctx->H[3] += D;
ctx->H[4] += E;
}
/*----------------------------------------------------------------------------
*
* This code added by Thomas "temas" Muldowney for Jabber compatability
*
*---------------------------------------------------------------------------*/
char *
shahash (char *str)
{
static char final[41];
char *pos;
unsigned char hashval[20];
int x;
if (!str || strlen (str) == 0)
return NULL;
shaBlock ((unsigned char *) str, strlen (str), hashval);
pos = final;
for (x = 0; x < 20; x++)
{
snprintf (pos, 3, "%02x", hashval[x]);
pos += 2;
}
return (char *) final;
}
void
shahash_r (const char *str, char hashbuf[41])
{
int x;
char *pos;
unsigned char hashval[20];
if (!str || strlen (str) == 0)
return;
shaBlock ((unsigned char *) str, strlen (str), hashval);
pos = hashbuf;
for (x = 0; x < 20; x++)
{
snprintf (pos, 3, "%02x", hashval[x]);
pos += 2;
}
return;
}
/*********************************************/
// Ten kod dodany przeze mnie i Piotra Pawowa
void
calc_passcode (const char *pass, char *code)
{
int magic1 = 0x50305735, magic2 = 0x12345671, sum = 7;
char z;
while ((z = *pass++) != 0)
{
if (z == ' ')
continue;
if (z == '\t')
continue;
magic1 ^= (((magic1 & 0x3f) + sum) * z) + (magic1 << 8);
magic2 += (magic2 << 8) ^ magic1;
sum += z;
}
magic1 &= 0x7fffffff;
magic2 &= 0x7fffffff;
sprintf (code, "%08x%08x", magic1, magic2);
}
/*
* tlen_hash()
*
* Generuje hash potrzebny do zalogowania si
*
* - pass - haso
* - id - identyfikator sesji
*
* Funkcja wewntrzna na potrzeby tlen_authorize
*
*/
char *tlen_hash (const char *pass, const char *id)
{
char passcode[17];
char dohasha[25];
char *hash;
hash = (char *) malloc (41);
calc_passcode (pass, passcode);
strcpy (dohasha, id);
strcat (dohasha, passcode);
dohasha[24]=0;
shahash_r (dohasha, hash);
return (char *) hash;
}
/*
* tlen_getid()
*
* Wysya prob o ID do serwera
*
* - sesja - nasza sesja
*
* Wywoywane zaraz po poczeniu
*
*/
int tlen_getid (struct tlen_session *sesja)
{
tlen_socket_write_string (sesja, "<s v='2'>");
return 1;
}
/*
* tlen_authorize()
*
* Wysya zapytanie logujce do serwera
*
* - sesja - nasza sesja
*
* Wywoywane po otrzymaniu ID
*
*/
int tlen_authorize (struct tlen_session *sesja)
{
char *query;
char *hash;
tlen_debug ("Username: %s\nHaslo: <hidden>\nID: %s\n", sesja->username, sesja->sid);
if (!(query = (char *)malloc (strlen("<iq type='set' id=''><query xmlns='jabber:iq:auth'><username></username><digest></digest><resource>t</resource></query></iq>") + strlen (sesja->username) + 40 + 8 + 1)))
{
perror ("malloc");
sesja->error = TLEN_ERROR_MALLOC;
}
hash = tlen_hash (sesja->password, sesja->sid);
sprintf (query,"<iq type='set' id='%s'><query xmlns='jabber:iq:auth'><username>%s</username><digest>%s</digest><resource>t</resource></query></iq>",sesja->sid, sesja->username, hash);
tlen_socket_write_string (sesja, query);
free (query);
free (hash);
return 1;
}
|