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 | #include "md5.h" |
---|
32 | #include "crypting.h" |
---|
33 | #include <string.h> |
---|
34 | #include <stdio.h> |
---|
35 | #include <stdlib.h> |
---|
36 | |
---|
37 | /*\ |
---|
38 | * [SH] Do _not_ call this if it's not entirely sure that it will not cause |
---|
39 | * harm to another users file, since this does not check the password for |
---|
40 | * correctness. |
---|
41 | \*/ |
---|
42 | |
---|
43 | int checkpass (const char *pass, const char *md5sum) |
---|
44 | { |
---|
45 | md5_state_t md5state; |
---|
46 | md5_byte_t digest[16]; |
---|
47 | int i, j; |
---|
48 | char digits[3]; |
---|
49 | |
---|
50 | md5_init (&md5state); |
---|
51 | md5_append (&md5state, (unsigned char *)pass, strlen (pass)); |
---|
52 | md5_finish (&md5state, digest); |
---|
53 | |
---|
54 | for (i = 0, j = 0; i < 16; i++, j += 2) { |
---|
55 | /* Check password for correctness */ |
---|
56 | g_snprintf (digits, sizeof (digits), "%02x\n", digest[i]); |
---|
57 | |
---|
58 | if (digits[0] != md5sum[j]) return (-1); |
---|
59 | if (digits[1] != md5sum[j + 1]) return (-1); |
---|
60 | } |
---|
61 | |
---|
62 | return( 0 ); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | char *hashpass (const char *password) |
---|
67 | { |
---|
68 | md5_state_t md5state; |
---|
69 | md5_byte_t digest[16]; |
---|
70 | int i; |
---|
71 | char digits[3]; |
---|
72 | char *rv; |
---|
73 | |
---|
74 | if (password == NULL) return (NULL); |
---|
75 | |
---|
76 | rv = g_new0 (char, 33); |
---|
77 | |
---|
78 | md5_init (&md5state); |
---|
79 | md5_append (&md5state, (const unsigned char *)password, strlen (password)); |
---|
80 | md5_finish (&md5state, digest); |
---|
81 | |
---|
82 | for (i = 0; i < 16; i++) { |
---|
83 | /* Build a hash of the pass */ |
---|
84 | g_snprintf (digits, sizeof (digits), "%02x", digest[i]); |
---|
85 | strcat (rv, digits); |
---|
86 | } |
---|
87 | |
---|
88 | return (rv); |
---|
89 | } |
---|
90 | |
---|
91 | char *obfucrypt (char *line, const char *password) |
---|
92 | { |
---|
93 | int i, j; |
---|
94 | char *rv; |
---|
95 | |
---|
96 | if (password == NULL) return (NULL); |
---|
97 | |
---|
98 | rv = g_new0 (char, strlen (line) + 1); |
---|
99 | |
---|
100 | i = j = 0; |
---|
101 | while (*line) { |
---|
102 | /* Encrypt/obfuscate the line, using the password */ |
---|
103 | if (*(signed char*)line < 0) *line = - (*line); |
---|
104 | |
---|
105 | rv[j] = *line + password[i]; /* Overflow intended */ |
---|
106 | |
---|
107 | line++; |
---|
108 | if (!password[++i]) i = 0; |
---|
109 | j++; |
---|
110 | } |
---|
111 | |
---|
112 | return (rv); |
---|
113 | } |
---|
114 | |
---|
115 | char *deobfucrypt (char *line, const char *password) |
---|
116 | { |
---|
117 | int i, j; |
---|
118 | char *rv; |
---|
119 | |
---|
120 | if (password == NULL) return (NULL); |
---|
121 | |
---|
122 | rv = g_new0 (char, strlen (line) + 1); |
---|
123 | |
---|
124 | i = j = 0; |
---|
125 | while (*line) { |
---|
126 | /* Decrypt/deobfuscate the line, using the pass */ |
---|
127 | rv[j] = *line - password[i]; /* Overflow intended */ |
---|
128 | |
---|
129 | line++; |
---|
130 | if (!password[++i]) i = 0; |
---|
131 | j++; |
---|
132 | } |
---|
133 | |
---|
134 | return (rv); |
---|
135 | } |
---|
136 | |
---|
137 | #ifdef CRYPTING_MAIN |
---|
138 | |
---|
139 | /* A little main() function for people who want a stand-alone program to |
---|
140 | encode/decode BitlCrypted files. */ |
---|
141 | |
---|
142 | int main( int argc, char *argv[] ) |
---|
143 | { |
---|
144 | char *hash, *action, line[256]; |
---|
145 | char* (*func)( char *, const char * ); |
---|
146 | |
---|
147 | if( argc < 2 ) |
---|
148 | { |
---|
149 | fprintf( stderr, "Usage: %s <password>\n\n" |
---|
150 | "Reads from stdin, writes to stdout.\n" |
---|
151 | "Call as \"encode\" to encode, \"decode\" to decode.\n", argv[0] ); |
---|
152 | return( 1 ); |
---|
153 | } |
---|
154 | |
---|
155 | hash = hashpass( argv[1] ); |
---|
156 | action = argv[0] + strlen( argv[0] ) - strlen( "encode" ); |
---|
157 | |
---|
158 | if( strcmp( action, "encode" ) == 0 ) |
---|
159 | { |
---|
160 | fwrite( hash, 32, 1, stdout ); |
---|
161 | func = obfucrypt; |
---|
162 | } |
---|
163 | else if( strcmp( action, "decode" ) == 0 ) |
---|
164 | { |
---|
165 | char hash2[32]; |
---|
166 | |
---|
167 | fread( hash2, 32, 1, stdin ); |
---|
168 | if( memcmp( hash, hash2, 32 ) != 0 ) |
---|
169 | { |
---|
170 | fprintf( stderr, "Passwords don't match. Can't decode.\n" ); |
---|
171 | return( 1 ); |
---|
172 | } |
---|
173 | func = deobfucrypt; |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | return( main( 0, NULL ) ); |
---|
178 | } |
---|
179 | |
---|
180 | while( fscanf( stdin, "%[^\n]255s", line ) > 0 ) |
---|
181 | { |
---|
182 | char *out; |
---|
183 | |
---|
184 | /* Flush the newline */ |
---|
185 | fgetc( stdin ); |
---|
186 | |
---|
187 | out = func( line, argv[1] ); |
---|
188 | printf( "%s\n", out ); |
---|
189 | g_free( out ); |
---|
190 | } |
---|
191 | |
---|
192 | return( 0 ); |
---|
193 | } |
---|
194 | |
---|
195 | #endif |
---|