File: ipmi_auth.cpp

package info (click to toggle)
openhpi 3.8.0-2.3
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 31,888 kB
  • sloc: ansic: 225,326; cpp: 63,687; java: 16,472; cs: 15,161; python: 11,884; sh: 11,508; makefile: 4,945; perl: 1,529; xml: 36; asm: 13
file content (218 lines) | stat: -rw-r--r-- 3,937 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2003,2004 by FORCE Computers
 *
 * Note that this file is based on parts of OpenIPMI
 * written by Corey Minyard <minyard@mvista.com>
 * of MontaVista Software. Corey's code was helpful
 * and many thanks go to him. He gave the permission
 * to use this code in OpenHPI under BSD license.
 *
 * 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.  This
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Authors:
 *     Thomas Kanngieser <thomas.kanngieser@fci.com>
 */

#include "ipmi_auth.h"
#include <string.h>
#include <errno.h>
#include <openssl/opensslconf.h>


cIpmiAuth *
IpmiAuthFactory( tIpmiAuthType type )
{
  switch( type )
     {
       case eIpmiAuthTypeNone:
            return new cIpmiAuthNone;

       case eIpmiAuthTypeMd2:
#if defined(HAVE_OPENSSL_MD2_H) && !defined(OPENSSL_NO_MD2)
            return new cIpmiAuthMd2;
#else
            break;
#endif

       case eIpmiAuthTypeMd5:
#ifdef HAVE_OPENSSL_MD5_H
            return new cIpmiAuthMd5;
#else
            break;
#endif

       case eIpmiAuthTypeStraight:
            return new cIpmiAuthStraight;

       case eIpmiAuthTypeOem:
            break;
     }

  return 0;
}


int
cIpmiAuthNone::Init( const unsigned char * /*password*/ )
{
  return 0;
}


int
cIpmiAuthNone::Gen( cIpmiAuthSg /*d*/[], void *output )
{
  memset( output, 0, 16 );
  return 0;
}


int
cIpmiAuthNone::Check( cIpmiAuthSg /*d*/[], void * /*code*/ )
{
  return 0;
}


#if defined(HAVE_OPENSSL_MD2_H) && !defined(OPENSSL_NO_MD2)
#include <openssl/md2.h>


int
cIpmiAuthMd2::Init( const unsigned char *password )
{
  memcpy( data, password, 16 );

  return 0;
}


int
cIpmiAuthMd2::Gen( cIpmiAuthSg d[],
                   void       *output )
{
  MD2_CTX ctx;

  MD2_Init( &ctx );
  MD2_Update( &ctx, data, 16 );

  for( int i = 0; d[i].data != 0; i++ )
       MD2_Update( &ctx, (unsigned char *)d[i].data, d[i].len );

  MD2_Update( &ctx, data, 16 );
  MD2_Final( (unsigned char *)output, &ctx );

  return 0;
}


int
cIpmiAuthMd2::Check( cIpmiAuthSg d[],
                     void       *code )
{
  MD2_CTX ctx;
  unsigned char md[16];

  MD2_Init( &ctx );
  MD2_Update( &ctx, data, 16 );

  for( int i = 0; d[i].data != 0; i++ )
       MD2_Update( &ctx, (unsigned char *)d[i].data, d[i].len );

  MD2_Update( &ctx, data, 16 );
  MD2_Final( md, &ctx );

  if ( memcmp( code, md, 16 ) != 0 )
       return EINVAL;

  return 0;
}
#endif


#ifdef HAVE_OPENSSL_MD5_H
#include <openssl/md5.h>


int
cIpmiAuthMd5::Init( const unsigned char *password )
{
  memcpy( data, password, 16 );

  return 0;
}


int
cIpmiAuthMd5::Gen( cIpmiAuthSg d[],
                   void        *output )
{
  MD5_CTX ctx;

  MD5_Init( &ctx );
  MD5_Update( &ctx, data, 16 );

  for( int i = 0; d[i].data != 0; i++ )
       MD5_Update( &ctx, d[i].data, d[i].len );

  MD5_Update( &ctx, data, 16 );
  MD5_Final( (unsigned char *)output, &ctx );

  return 0;
}


int
cIpmiAuthMd5::Check( cIpmiAuthSg d[],
                     void       *code )
{
  MD5_CTX ctx;
  unsigned char md[16];

  MD5_Init( &ctx );
  MD5_Update( &ctx, data, 16 );

  for( int i = 0; d[i].data != 0; i++ )
       MD5_Update( &ctx, d[i].data, d[i].len );

  MD5_Update( &ctx, data, 16 );
  MD5_Final( md, &ctx );

  if ( memcmp( code, md, 16 ) != 0 )
       return EINVAL;

  return 0;
}
#endif


int
cIpmiAuthStraight::Init( const unsigned char *password )
{
  memcpy( data, password, 16 );

  return 0;
}


int
cIpmiAuthStraight::Gen( cIpmiAuthSg /*d*/[], void *output )
{
  memcpy( output, data, 16 );
  return 0;
}


int
cIpmiAuthStraight::Check( cIpmiAuthSg /*d*/[], void *code )
{
  if ( strncmp( (char *)data, (char *)code, 16 ) != 0 )
       return EINVAL;

  return 0;
}