summaryrefslogtreecommitdiffstats
path: root/lib/plugins/stonith/drac3_hash.c
blob: 605a1266ccce70469d78385607f96a9d2430e439 (plain)
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
/*
 * Stonith module for Dell DRACIII (Dell Remote Access Card)
 *
 * Copyright (C) 2003 Alfa21 Outsourcing
 * Copyright (C) 2003 Roberto Moreda <moreda@alfa21.com>
 *
 * 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
 *
 */

#include <lha_internal.h>

#include <string.h>
#include <stdio.h>
#include <clplumbing/base64.h>
#include <clplumbing/md5.h>
#include <glib.h>

#include "drac3_hash.h"

#define BUFLEN        1024    /* buffer */
#define SBUFLEN        256    /* small buffer */
#define MD5LEN          16    /* md5 buffer */

/* Hash functions for DRAC3 authentication */

guint16 
drac3Crc16(const char *str, 
	const int l) {

    int i,j;
    guint16 crc = 0;
    
    for (i=0; i<l; i++) {
        crc = crc ^ (str[i] << 8);
        for (j=0; j<8; j++)  
            crc = ( (crc & 0x8000) == 32768 ? (crc<<1) ^ 0x1021 : crc<<1);
    }    
    crc = crc & 0xFFFF;
    return crc;
}

void 
drac3AuthHash(const char * chall, 
	const char * pass, 
	char * token, 
	int len) {
    
    char * chall_dup;    
    char challBytes[MD5LEN];
    char passMD5[MD5LEN];
    char xorBytes[MD5LEN];
    char xorBytesMD5[MD5LEN];
    guint16 crc;
    char response[MD5LEN+2];
    char responseb64[SBUFLEN];
    int i;
    
    /* decodes chall -> challBytes */
    memset(challBytes, 0, MD5LEN);
    chall_dup = g_strdup(chall);
    if (chall_dup[strlen(chall_dup) - 1] == '\n' ) {
        chall_dup[strlen(chall_dup) - 1] = '\0';
    }
    base64_to_binary(chall_dup, strlen(chall_dup), challBytes, MD5LEN);

    /* gets MD5 from pass -> passMD5 */
    MD5((const unsigned char *)pass, strlen(pass), (unsigned char *)passMD5);
    
    /* calculate challBytes and passMD5 xor -> xorBytes */
    for (i=0; i<MD5LEN; i++) {
        xorBytes[i] = challBytes[i] ^ passMD5[i];
    }
    
    /* calculate xorBytes MD5 -> xorBytesMD5 */
    MD5((unsigned char *)xorBytes, MD5LEN, (unsigned char *)xorBytesMD5);

    /* calculate xorBytesMD5 crc16 */
    crc = drac3Crc16(xorBytesMD5, MD5LEN);
    
    /* joins xorBytesMD5 and crc16 -> response */
    memcpy(response, xorBytesMD5, MD5LEN);
    memcpy(response+MD5LEN, &crc, 2);
    
    /* calculate response base64 -> responseb64 */
    memset(responseb64, 0, SBUFLEN);
    binary_to_base64(response, MD5LEN+2, responseb64, SBUFLEN);

    /* assuring null-termination */
    responseb64[SBUFLEN-1]=0x00;
    
    snprintf(token, len, "%s", responseb64);
    token[len-1]=0x00;
}