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