Просмотр исходного кода

* Added base64[.c|.h]
* Recoded some of the AES functions in crypt.c
* remove_crlf(char *) now instead of remove_crlf(char **)
* Fixed several bugs in Encrypt_File and Decrypt_File
* Added a remove_crlf() after every fgets()


svn: 1045

Bryan Drewery 22 лет назад
Родитель
Сommit
c865444578
10 измененных файлов с 170 добавлено и 115 удалено
  1. 1 5
      Makefile.in
  2. 1 0
      doc/UPDATES
  3. 1 0
      src/Makefile.in
  4. 81 0
      src/base64.c
  5. 10 0
      src/base64.h
  6. 7 6
      src/conf.c
  7. 63 98
      src/crypt.c
  8. 3 5
      src/misc.c
  9. 1 1
      src/misc.h
  10. 2 0
      src/users.c

+ 1 - 5
Makefile.in

@@ -86,7 +86,6 @@ all:
 
 cleanutils:
 	@rm -f src/stringfix src/makesettings src/sorthelp src/makehelp
-	@misc/dostrings l
 
 clean:  cleanutils
 	+@cd src && $(MAKE) clean
@@ -189,10 +188,7 @@ utils: makesettings stringfix makehelp
 crypto:
 	+@cd src/crypto && ${MAKE_UTILS} crypto
 
-dostrings: stringfix
-	@misc/dostrings
-
-general: makesettings packconf salt help dostrings
+general: makesettings packconf salt help 
 
 leaf:	general
 	@echo ""

+ 1 - 0
doc/UPDATES

@@ -25,6 +25,7 @@ This is a summary of ChangeLog basically.
 21.Unrolled some loops in the botlink encrypt.
 22.Added server 005 support, fixes many potential bugs.
 23.Fixed many bugs with color system, including color over relay/botcmd.
+24.Recoded some of the AES core, fixed several outstanding bugs.
 
 1.1.7
 

+ 1 - 0
src/Makefile.in

@@ -14,6 +14,7 @@ CPPFLAGS = @CPPFLAGS@
 OBJCOPY = @OBJCOPY@
 
 OBJS = auth.o \
+	base64.o \
 	binary.o \
 	bg.o \
 	botcmd.o \

+ 81 - 0
src/base64.c

@@ -0,0 +1,81 @@
+/* base64.c: base64 encoding/decoding
+ *
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include "base64.h"
+
+static const char base64[64] = ".\\0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+static const char base64r[256] = {
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0,
+  0, 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, 0, 1, 0, 0, 0,
+  0, 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, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+};
+
+char *
+b64enc(const unsigned char *data, int len)
+{
+  char *dest = NULL;
+
+  dest = calloc(1, (len * 4) / 3 + 4 + 1);
+  b64enc_buf(data, len, dest);
+  return (dest);
+}
+
+void
+b64enc_buf(const unsigned char *data, int len, char *dest)
+{
+  int i, t;
+
+#define DB(x) ((unsigned char) (x + i < len ? data[x + i] : 0))
+    for (i = 0, t = 0; i < len; i += 3, t += 4) {
+      dest[t] = base64[DB(0) >> 2];
+      dest[t + 1] = base64[((DB(0) & 3) << 4) | (DB(1) >> 4)];
+      dest[t + 2] = base64[((DB(1) & 0x0F) << 2) | (DB(2) >> 6)];
+      dest[t + 3] = base64[(DB(2) & 0x3F)];
+    }
+#undef DB
+  dest[t] = 0;
+}
+
+char *
+b64dec(const unsigned char *data, int *len)
+{
+  char *dest = NULL;
+
+  dest = calloc(1, ((*len * 3) / 4) + 6 + 1);
+  b64dec_buf(data, len, dest);
+  return (dest);
+}
+
+void
+b64dec_buf(const unsigned char *data, int *len, char *dest)
+{
+  int t, i;
+
+#define DB(x) ((unsigned char) (x + i < *len ? base64r[(unsigned char) data[x + i]] : 0))
+  for (i = 0, t = 0; i < *len; i += 4, t += 3) {
+    dest[t] = (DB(0) << 2) + (DB(1) >> 4);
+    dest[t + 1] = ((DB(1) & 0x0F) << 4) + (DB(2) >> 2);
+    dest[t + 2] = ((DB(2) & 3) << 6) + DB(3);
+  };
+#undef DB
+  t += 3;
+  t -= (t % 4);
+//printf("t: %d len: %d strlen: %d : %s\n", t, *len, strlen(dest), dest);
+  dest[t] = 0;
+  *len = t;
+}

+ 10 - 0
src/base64.h

@@ -0,0 +1,10 @@
+#ifndef _BASE64_H_
+#  define _BASE64_H_
+
+char *b64enc(const unsigned char *data, int len);
+void b64enc_buf(const unsigned char *data, int len, char *dest);
+
+char *b64dec(const unsigned char *data, int *len);
+void b64dec_buf(const unsigned char *data, int *len, char *dest);
+
+#endif /* !_BASE64_H_ */

+ 7 - 6
src/conf.c

@@ -288,7 +288,7 @@ checkpid(char *nick, conf_bot * bot)
     pid_t xx = 0;
 
     fgets(s, 10, f);
-    s[10] = 0;
+    remove_crlf(s);
     fclose(f);
     xx = atoi(s);
     if (bot) {
@@ -470,24 +470,23 @@ readconf(char *fname, int bits)
 {
   FILE *f = NULL;
   int i = 0, enc = (bits & CONF_ENC) ? 1 : 0;
-  char inbuf[201] = "";
+  char *inbuf = NULL;
 
   sdprintf("readconf(%s, %d)", fname, enc);
   Context;
   if (!(f = fopen(fname, "r")))
     fatal("Cannot read config", 0);
 
-  while (fgets(inbuf, sizeof inbuf, f) != NULL) {
+  inbuf = calloc(1, 201);
+  while (fgets(inbuf, 201, f) != NULL) {
     char *line = NULL, *temp_ptr = NULL;
 
+    remove_crlf(inbuf);
     if (enc)
       line = temp_ptr = decrypt_string(SALT1, inbuf);
     else
       line = inbuf;
 
-    /* fucking DOS */
-    remove_crlf(&line);
-
     if ((line && !line[0]) || line[0] == '\n') {
       if (enc)
         free(line);
@@ -595,10 +594,12 @@ readconf(char *fname, int bits)
         conf_addbot(nick, ip, host, ipsix);
       }
     }
+    inbuf[0] = 0;
     if (enc)
       free(temp_ptr);
   }                             /* while(fgets()) */
   fclose(f);
+  free(inbuf);
 
   return 0;
 }

+ 63 - 98
src/crypt.c

@@ -10,6 +10,7 @@
 #include "crypt.h"
 #include "salt.h"
 #include "misc.h"
+#include "base64.h"
 #include "src/crypto/crypto.h"
 #include <stdarg.h>
 
@@ -19,110 +20,74 @@
 
 AES_KEY e_key, d_key;
 
-char *encrypt_binary(const char *keydata, unsigned char *data, int *datalen)
+char *encrypt_binary(const char *keydata, const unsigned char *in, size_t *inlen)
 {
-  int newdatalen = *datalen, blockcount = 0, blockndx = 0;
-  unsigned char *newdata = NULL;
+  size_t len = *inlen;
+  int blocks = 0, block = 0;
+  unsigned char *out = NULL;
 
-  /* First pad indata to CRYPT_BLOCKSIZE multiplum */
-  if (newdatalen % CRYPT_BLOCKSIZE)             /* more than 1 block? */
-    newdatalen += (CRYPT_BLOCKSIZE - (newdatalen % CRYPT_BLOCKSIZE));
+  /* First pad indata to CRYPT_BLOCKSIZE multiple */
+  if (len % CRYPT_BLOCKSIZE)             /* more than 1 block? */
+    len += (CRYPT_BLOCKSIZE - (len % CRYPT_BLOCKSIZE));
 
-  newdata = calloc(1, newdatalen);
-  egg_memcpy(newdata, data, *datalen);
-  if (newdatalen != *datalen)
-    egg_bzero((void *) &newdata[*datalen], (newdatalen - *datalen));
-  *datalen = newdatalen;
+  out = calloc(1, len + 1);
+  egg_memcpy(out, in, *inlen);
+  *inlen = len;
 
-  if ((!keydata) || (!keydata[0])) {
+  if (!keydata || !*keydata) {
     /* No key, no encryption */
-    egg_memcpy(newdata, data, newdatalen);
+    egg_memcpy(out, in, len);
   } else {
     char key[CRYPT_KEYSIZE + 1] = "";
 
     strncpyz(key, keydata, sizeof(key));
-/*      strncpyz(&key[sizeof(key) - strlen(keydata)], keydata, sizeof(key)); */
     AES_set_encrypt_key(key, CRYPT_KEYBITS, &e_key);
-
     /* Now loop through the blocks and crypt them */
-    blockcount = newdatalen / CRYPT_BLOCKSIZE;
-    for (blockndx = blockcount - 1; blockndx >= 0; blockndx--) {
-      AES_encrypt(&newdata[blockndx * CRYPT_BLOCKSIZE], &newdata[blockndx * CRYPT_BLOCKSIZE], &e_key);
-    }
+    blocks = len / CRYPT_BLOCKSIZE;
+    for (block = blocks - 1; block >= 0; block--)
+      AES_encrypt(&out[block * CRYPT_BLOCKSIZE], &out[block * CRYPT_BLOCKSIZE], &e_key);
   }
-  return newdata;
+  out[len] = 0;
+  return out;
 }
 
-char *decrypt_binary(const char *keydata, unsigned char *data, int datalen)
+char *decrypt_binary(const char *keydata, unsigned char *in, size_t len)
 {
-  int blockcount = 0, blockndx = 0;
-  unsigned char *newdata = NULL;
+  int blocks = 0, block = 0;
+  unsigned char *out = NULL;
 
-  datalen -= datalen % CRYPT_BLOCKSIZE;
-  newdata = calloc(1, datalen);
-  egg_memcpy(newdata, data, datalen);
+  len -= len % CRYPT_BLOCKSIZE;
+  out = calloc(1, len + 1);
+  egg_memcpy(out, in, len);
 
-  if ((!keydata) || (!keydata[0])) {
+  if (!keydata || !*keydata) {
     /* No key, no decryption */
   } else {
     /* Init/fetch key */
     char key[CRYPT_KEYSIZE + 1] = "";
 
     strncpyz(key, keydata, sizeof(key));
-/*      strncpy(&key[sizeof(key) - strlen(keydata)], keydata, sizeof(key)); */
     AES_set_decrypt_key(key, CRYPT_KEYBITS, &d_key);
-
     /* Now loop through the blocks and crypt them */
-    blockcount = datalen / CRYPT_BLOCKSIZE;
-
-    for (blockndx = blockcount - 1; blockndx >= 0; blockndx--) {
-      AES_decrypt(&newdata[blockndx * CRYPT_BLOCKSIZE], &newdata[blockndx * CRYPT_BLOCKSIZE], &d_key);
-    }
+    blocks = len / CRYPT_BLOCKSIZE;
 
+    for (block = blocks - 1; block >= 0; block--)
+      AES_decrypt(&out[block * CRYPT_BLOCKSIZE], &out[block * CRYPT_BLOCKSIZE], &d_key);
   }
 
-  return newdata;
+  return out;
 }
 
-const char base64[64] = ".\\0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-const char base64r[256] = {
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0,
-  0, 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, 0, 1, 0, 0, 0,
-  0, 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, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-};
-
-char *encrypt_string(const char *keydata, char *data)
+char *encrypt_string(const char *keydata, char *in)
 {
-  int l, i, t;
+  size_t len = 0;
   unsigned char *bdata = NULL;
   char *res = NULL;
 
-  l = strlen(data) + 1;
-  bdata = encrypt_binary(keydata, data, &l);
-  if ((keydata) && (keydata[0])) {
-    res = calloc(1, (l * 4) / 3 + 5);
-#define DB(x) ((unsigned char) (x+i<l ? bdata[x+i] : 0))
-    for (i = 0, t = 0; i < l; i += 3, t += 4) {
-      res[t] = base64[DB(0) >> 2];
-      res[t + 1] = base64[((DB(0) & 3) << 4) | (DB(1) >> 4)];
-      res[t + 2] = base64[((DB(1) & 0x0F) << 2) | (DB(2) >> 6)];
-      res[t + 3] = base64[(DB(2) & 0x3F)];
-    }
-#undef DB
-    res[t] = 0;
+  len = strlen(in) + 1;
+  bdata = encrypt_binary(keydata, in, &len);
+  if (keydata && *keydata) {
+    res = b64enc(bdata, len);
     free(bdata);
     return res;
   } else {
@@ -130,29 +95,20 @@ char *encrypt_string(const char *keydata, char *data)
   }
 }
 
-char *decrypt_string(const char *keydata, char *data)
+char *decrypt_string(const char *keydata, char *in)
 {
-  int i, l, t;
+  size_t len = 0;
   char *buf = NULL, *res = NULL;
 
-  l = strlen(data);
-  if ((keydata) && (keydata[0])) {
-    buf = calloc(1, (l * 3) / 4 + 6);
-#define DB(x) ((unsigned char) (x+i<l ? base64r[(unsigned char) data[x+i]] : 0))
-    for (i = 0, t = 0; i < l; i += 4, t += 3) {
-      buf[t] = (DB(0) << 2) + (DB(1) >> 4);
-      buf[t + 1] = ((DB(1) & 0x0F) << 4) + (DB(2) >> 2);
-      buf[t + 2] = ((DB(2) & 3) << 6) + DB(3);
-    };
-#undef DB
-    t += 3;
-    t -= (t % 4);
-    res = decrypt_binary(keydata, buf, t);
+  len = strlen(in);
+  if (keydata && *keydata) {
+    buf = b64dec(in, &len);
+    res = decrypt_binary(keydata, buf, len);
     free(buf);
     return res;
   } else {
-    res = calloc(1, l + 1);
-    strcpy(res, data);
+    res = calloc(1, len + 1);
+    strcpy(res, in);
     return res;
   }
 }
@@ -173,7 +129,7 @@ void encrypt_pass(char *s1, char *s2)
 int lfprintf (FILE *stream, char *format, ...)
 {
   va_list va;
-  char buf[8192], *ln = NULL, *nln = NULL, *tmp = NULL;
+  char buf[2048], *ln = NULL, *nln = NULL, *tmp = NULL;
   int res;
 
   buf[0] = 0;
@@ -182,10 +138,10 @@ int lfprintf (FILE *stream, char *format, ...)
   va_end(va);
 
   ln = buf;
-  while ((ln) && (ln[0])) {
-    nln = strchr(ln, '\n');
-    if (nln)
+  while (ln && *ln) {
+    if ((nln = strchr(ln, '\n')))
       *nln++ = 0;
+
     tmp = encrypt_string(SALT1, ln);
     res = fprintf(stream, "%s\n", tmp);
     free(tmp);
@@ -198,7 +154,7 @@ int lfprintf (FILE *stream, char *format, ...)
 
 void Encrypt_File(char *infile, char *outfile)
 {
-  char  buf[1024];
+  char *buf = NULL;
   FILE *f = NULL, *f2 = NULL;
   int std = 0;
 
@@ -215,13 +171,17 @@ void Encrypt_File(char *infile, char *outfile)
     printf("----------------------------------START----------------------------------\n");
   }
 
-  buf[0] = 0;
-  while (fgets(buf, sizeof buf, f) != NULL) {
+  buf = calloc(1, 1024);
+  while (fgets(buf, 1024, f) != NULL) {
+    remove_crlf(buf);
+
     if (std)
       printf("%s\n", encrypt_string(SALT1, buf));
     else
       lfprintf(f2, "%s\n", buf);
+    buf[0] = 0;
   }
+  free(buf);
   if (std)
     printf("-----------------------------------END-----------------------------------\n");
 
@@ -232,7 +192,7 @@ void Encrypt_File(char *infile, char *outfile)
 
 void Decrypt_File(char *infile, char *outfile)
 {
-  char buf[8192], *temps = NULL;
+  char *buf = NULL;
   FILE *f = NULL, *f2 = NULL;
   int std = 0;
 
@@ -248,16 +208,21 @@ void Decrypt_File(char *infile, char *outfile)
   } else {
     printf("----------------------------------START----------------------------------\n");
   }
-  
-  buf[0] = 0;
-  while (fgets(buf, sizeof buf, f) != NULL) {
+
+  buf = calloc(1, 2048);
+  while (fgets(buf, 2048, f) != NULL) {
+    char *temps = NULL;
+
+    remove_crlf(buf);
     temps = (char *) decrypt_string(SALT1, buf);
     if (!std)
       fprintf(f2, "%s\n",temps);
     else
       printf("%s\n", temps);
     free(temps);
+    buf[0] = 0;
   }
+  free(buf);
   if (std)
     printf("-----------------------------------END-----------------------------------\n");
 

+ 3 - 5
src/misc.c

@@ -175,15 +175,13 @@ char *splitnick(char **blah)
   return "";
 }
 
-void remove_crlf(char **line)
+void remove_crlf(char *line)
 {
   char *p = NULL;
 
-  p = strchr(*line, '\n');
-  if (p != NULL)
+  if ((p = strchr(line, '\n')))
     *p = 0;
-  p = strchr(*line, '\r');
-  if (p != NULL)
+  if ((p = strchr(line, '\r')))
     *p = 0;
 }
 

+ 1 - 1
src/misc.h

@@ -46,7 +46,7 @@ void maskhost(const char *, char *);
 char *stristr(char *, char *);
 void splitc(char *, char *, char);
 void splitcn(char *, char *, char, size_t);
-void remove_crlf(char **);
+void remove_crlf(char *);
 char *newsplit(char **);
 char *splitnick(char **);
 void stridx(char *, char *, int);

+ 2 - 0
src/users.c

@@ -688,6 +688,7 @@ int readuserfile(const char *file, struct userrec **ret)
   /* read opening comment */
   s = buf;
   fgets(cbuf, 180, f);
+  remove_crlf(cbuf);
   temps = (char *) decrypt_string(SALT1, cbuf);
   egg_snprintf(s, 180, "%s", temps);
   free(temps);
@@ -699,6 +700,7 @@ int readuserfile(const char *file, struct userrec **ret)
   while (!feof(f)) {
     s = buf;
     fgets(cbuf, 1024, f);
+    remove_crlf(cbuf);
     temps = (char *) decrypt_string(SALT1, cbuf);
     egg_snprintf(s, 1024, "%s", temps);
     free(temps);