source: crypting.c @ c2295f7

Last change on this file since c2295f7 was c2295f7, checked in by Jelmer Vernooij <jelmer@…>, at 2005-12-08T14:51:06Z

Move some crypting-unrelated code

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Sjoerd Hemminga and others                     *
5  \********************************************************************/
6
7/* A little bit of encryption for the users' passwords                  */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26/* [WvG] This file can also be compiled into a stand-alone program
27   which can encode/decode BitlBee account files. The main() will be
28   included if CRYPTING_MAIN is defined. Or just do "make decode" and
29   the programs will be built. */
30
31#ifndef CRYPTING_MAIN
32#define BITLBEE_CORE
33#include "bitlbee.h"
34#include "irc.h"
35#include "md5.h"
36#include "crypting.h"
37#include <string.h>
38#include <stdio.h>
39#include <stdlib.h>
40
41#else
42
43typedef struct irc
44{
45        char *password;
46} irc_t;
47
48#include "md5.h"
49#include "crypting.h"
50#include <string.h>
51#include <stdio.h>
52#include <stdlib.h>
53
[43e3368]54#define irc_usermsg
55
[b7d3cc34]56#endif
57
58/*\
59 * [SH] Do _not_ call this if it's not entirely sure that it will not cause
60 * harm to another users file, since this does not check the password for
61 * correctness.
62\*/
63
64/* USE WITH CAUTION!
65   Sets pass without checking */
[1eddf6b]66void setpassnc (irc_t *irc, const char *pass) 
67{
[b7d3cc34]68        if (irc->password) g_free (irc->password);
69       
70        if (pass) {
71                irc->password = g_strdup (pass);
72                irc_usermsg (irc, "Password successfully changed");
73        } else {
74                irc->password = NULL;
75        }
76}
77
[1eddf6b]78int setpass (irc_t *irc, const char *pass, const char* md5sum) 
79{
[b7d3cc34]80        md5_state_t md5state;
81        md5_byte_t digest[16];
82        int i, j;
83        char digits[3];
84       
85        md5_init (&md5state);
86        md5_append (&md5state, (unsigned char *)pass, strlen (pass));
87        md5_finish (&md5state, digest);
88       
89        for (i = 0, j = 0; i < 16; i++, j += 2) {
90                /* Check password for correctness */
91                g_snprintf (digits, sizeof (digits), "%02x\n", digest[i]);
92               
93                if (digits[0] != md5sum[j]) return (-1);
94                if (digits[1] != md5sum[j + 1]) return (-1);
95        }
96       
97        /* If pass is correct, we end up here and we set the pass */
98        setpassnc (irc, pass);
99       
100        return (0);
101}
102
103char *hashpass (irc_t *irc) {
104        md5_state_t md5state;
105        md5_byte_t digest[16];
106        int i;
107        char digits[3];
108        char *rv;
109       
110        if (irc->password == NULL) return (NULL);
111       
112        rv = (char *)g_malloc (33);
113        memset (rv, 0, 33);
114       
115        md5_init (&md5state);
116        md5_append (&md5state, (unsigned char *)irc->password, strlen (irc->password));
117        md5_finish (&md5state, digest);
118       
119        for (i = 0; i < 16; i++) {
120                /* Build a hash of the pass */
121                g_snprintf (digits, sizeof (digits), "%02x", digest[i]);
122                strcat (rv, digits);
123        }
124       
125        return (rv);
126}
127
128char *obfucrypt (irc_t *irc, char *line) {
129        int i, j;
130        char *rv;
131       
132        if (irc->password == NULL) return (NULL);
133       
134        rv = (char *)g_malloc (strlen (line) + 1);
135        memset (rv, '\0', strlen (line) + 1);
136       
137        i = j = 0;
138        while (*line) {
139                /* Encrypt/obfuscate the line, using the password */
140                if (*(signed char*)line < 0) *line = - (*line);
141                if (((signed char*)irc->password)[i] < 0) irc->password[i] = - irc->password[i];
142               
143                rv[j] = *line + irc->password[i]; /* Overflow intended */
144               
145                line++;
146                if (!irc->password[++i]) i = 0;
147                j++;
148        }
149       
150        return (rv);
151}
152
153char *deobfucrypt (irc_t *irc, char *line) {
154        int i, j;
155        char *rv;
156       
157        if (irc->password == NULL) return (NULL);
158       
159        rv = (char *)g_malloc (strlen (line) + 1);
160        memset (rv, '\0', strlen (line) + 1);
161       
162        i = j = 0;
163        while (*line) {
164                /* Decrypt/deobfuscate the line, using the pass */
165                rv[j] = *line - irc->password[i]; /* Overflow intended */
166               
167                line++;
168                if (!irc->password[++i]) i = 0;
169                j++;
170        }
171       
172        return (rv);
173}
174
175#ifdef CRYPTING_MAIN
176
177/* A little main() function for people who want a stand-alone program to
178   encode/decode BitlCrypted files. */
179
180int main( int argc, char *argv[] )
181{
182        irc_t *irc = g_malloc( sizeof( irc_t ) );
183        char *hash, *action, line[256];
184        char* (*func)( irc_t *, char * );
185       
186        if( argc < 2 )
187        {
188                fprintf( stderr, "Usage: %s <password>\n\n"
189                                 "Reads from stdin, writes to stdout.\n"
190                                 "Call as \"encode\" to encode, \"decode\" to decode.\n", argv[0] );
191                return( 1 );
192        }
193       
194        memset( irc, 0, sizeof( irc_t ) );
195        irc->password = g_strdup( argv[1] );
196       
197        hash = hashpass( irc );
198        action = argv[0] + strlen( argv[0] ) - strlen( "encode" );
199       
200        if( strcmp( action, "encode" ) == 0 )
201        {
202                fwrite( hash, 32, 1, stdout );
203                func = obfucrypt;
204        }
205        else if( strcmp( action, "decode" ) == 0 )
206        {
207                char hash2[32];
208               
209                fread( hash2, 32, 1, stdin );
210                if( memcmp( hash, hash2, 32 ) != 0 )
211                {
212                        fprintf( stderr, "Passwords don't match. Can't decode.\n" );
213                        return( 1 );
214                }
215                func = deobfucrypt;
216        }
217        else
218        {
219                return( main( 0, NULL ) );
220        }
221       
222        while( fscanf( stdin, "%[^\n]255s", line ) > 0 )
223        {
224                char *out;
225               
226                /* Flush the newline */
227                fgetc( stdin );
228               
229                out = func( irc, line );
230                printf( "%s\n", out );
231                g_free( out );
232        }
233       
234        return( 0 );
235}
236
237#endif
Note: See TracBrowser for help on using the repository browser.