Sfoglia il codice sorgente

* Link libcrypto in at runtime to avoid mismatched libcrypto/libssl

Bryan Drewery 15 anni fa
parent
commit
0bc04fb643
10 ha cambiato i file con 167 aggiunte e 17 eliminazioni
  1. 1 1
      Makefile.in
  2. 1 0
      src/Makefile.in
  3. 1 1
      src/crypt.c
  4. 1 1
      src/crypt.h
  5. 1 1
      src/crypto/aes_util.c
  6. 1 1
      src/crypto/bf_util.c
  7. 0 12
      src/crypto/crypto.h
  8. 105 0
      src/libcrypto.c
  9. 52 0
      src/libcrypto.h
  10. 4 0
      src/main.c

+ 1 - 1
Makefile.in

@@ -39,7 +39,7 @@ STRIP = @STRIP@
 DIFF = @DIFF@
 
 #LIBS = @LIBS@ @ZLIB@
-LIBS = @LIBS@ @SSL_LIBS@
+LIBS = @LIBS@
 
 DEBCXXFLAGS = -DDEBUG -fno-inline -g3 -ggdb3 -O0 -Wshadow -Wpointer-arith -Wcast-align @GCC3DEB@ @GCC4DEB@
 CFLGS = @GCC3@ -fno-rtti @SSL_INCLUDES@

+ 1 - 0
src/Makefile.in

@@ -35,6 +35,7 @@ OBJS = auth.o \
 	EncryptedStream.o \
 	flags.o \
 	garble.o \
+	libcrypto.o \
 	libssl.o \
 	log.o \
 	main.o \

+ 1 - 1
src/crypt.c

@@ -11,7 +11,7 @@
 #include "settings.h"
 #include "misc.h"
 #include "base64.h"
-#include "src/crypto/crypto.h"
+#include "libcrypto.h"
 #include <stdarg.h>
 #include <bdlib/src/String.h>
 #include <bdlib/src/Stream.h>

+ 1 - 1
src/crypt.h

@@ -8,7 +8,7 @@
 #endif
 
 #include <sys/types.h>
-#include "src/crypto/crypto.h"
+#include "libcrypto.h"
 #include "users.h"
 
 #define SHA_HASH_LENGTH (SHA_DIGEST_LENGTH << 1)

+ 1 - 1
src/crypto/aes_util.c

@@ -2,7 +2,7 @@
  *
  */
 
-#include "crypto.h"
+#include "src/libcrypto.h"
 #include "src/compat/compat.h"
 #include <bdlib/src/String.h>
 

+ 1 - 1
src/crypto/bf_util.c

@@ -2,7 +2,7 @@
  *
  */
 
-#include "crypto.h"
+#include "src/libcrypto.h"
 #include "src/compat/compat.h"
 #include <bdlib/src/String.h>
 

+ 0 - 12
src/crypto/crypto.h

@@ -1,12 +0,0 @@
-#ifndef _CRYPTO_H
-#define _CRYPTO_H
-
-#include "aes_util.h"
-#include "bf_util.h"
-#include <openssl/crypto.h>
-#include <openssl/aes.h>
-#include <openssl/blowfish.h>
-#include <openssl/md5.h>
-#include <openssl/sha.h>
-
-#endif /* !_CRYPTO_H */

+ 105 - 0
src/libcrypto.c

@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 1997 Robey Pointer
+ * Copyright (C) 1999 - 2002 Eggheads Development Team
+ * Copyright (C) 2002 - 2010 Bryan Drewery
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * 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.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+
+/*
+ * libcrypto.c -- handles:
+ *   libcrypto handling
+ *
+ */
+
+
+#include "common.h"
+#include "main.h"
+#include "dl.h"
+#include <bdlib/src/String.h>
+#include <bdlib/src/Array.h>
+
+#include "libcrypto.h"
+
+void *libcrypto_handle = NULL;
+
+static int load_symbols(void *handle) {
+  const char *dlsym_error = NULL;
+
+  DLSYM_GLOBAL(handle, AES_cbc_encrypt);
+  DLSYM_GLOBAL(handle, AES_decrypt);
+  DLSYM_GLOBAL(handle, AES_encrypt);
+  DLSYM_GLOBAL(handle, AES_set_decrypt_key);
+  DLSYM_GLOBAL(handle, AES_set_encrypt_key);
+  DLSYM_GLOBAL(handle, BF_decrypt);
+  DLSYM_GLOBAL(handle, BF_encrypt);
+  DLSYM_GLOBAL(handle, BF_set_key);
+  DLSYM_GLOBAL(handle, ERR_error_string);
+  DLSYM_GLOBAL(handle, ERR_get_error);
+  DLSYM_GLOBAL(handle, OPENSSL_cleanse);
+  DLSYM_GLOBAL(handle, RAND_file_name);
+  DLSYM_GLOBAL(handle, RAND_load_file);
+  DLSYM_GLOBAL(handle, RAND_seed);
+  DLSYM_GLOBAL(handle, RAND_status);
+  DLSYM_GLOBAL(handle, RAND_write_file);
+  DLSYM_GLOBAL(handle, MD5_Final);
+  DLSYM_GLOBAL(handle, MD5_Init);
+  DLSYM_GLOBAL(handle, MD5_Update);
+  DLSYM_GLOBAL(handle, SHA1_Final);
+  DLSYM_GLOBAL(handle, SHA1_Init);
+  DLSYM_GLOBAL(handle, SHA1_Update);
+  DLSYM_GLOBAL(handle, SHA256_Final);
+  DLSYM_GLOBAL(handle, SHA256_Init);
+  DLSYM_GLOBAL(handle, SHA256_Update);
+
+  return 0;
+}
+
+
+int load_libcrypto() {
+  if (libcrypto_handle) {
+    return 0;
+  }
+
+  sdprintf("Loading libcrypto");
+
+  bd::Array<bd::String> libs_list(bd::String("libcrypto.so libcrypto.so.0.9.8 libcrypto.so.7 libcrypto.so.6").split(' '));
+
+  for (size_t i = 0; i < libs_list.length(); ++i) {
+    dlerror(); // Clear Errors
+    libcrypto_handle = dlopen(bd::String(libs_list[i]).c_str(), RTLD_LAZY);
+    if (libcrypto_handle) {
+      sdprintf("Found libcrypto: %s", bd::String(libs_list[i]).c_str());
+      break;
+    }
+  }
+  if (!libcrypto_handle) {
+    sdprintf("Unable to find libcrypto");
+    return(1);
+  }
+
+  load_symbols(libcrypto_handle);
+
+  return 0;
+}
+
+int unload_libcrypto() {
+  if (libcrypto_handle) {
+    dlclose(libcrypto_handle);
+    libcrypto_handle = NULL;
+    return 0;
+  }
+  return 1;
+}

+ 52 - 0
src/libcrypto.h

@@ -0,0 +1,52 @@
+#ifndef _LIBCRYPTO_H
+#define _LIBCRYPTO_H
+
+#include <openssl/crypto.h>
+#include <openssl/aes.h>
+#include <openssl/blowfish.h>
+#include <openssl/md5.h>
+#include <openssl/sha.h>
+#include "src/crypto/aes_util.h"
+#include "src/crypto/bf_util.h"
+
+#include "common.h"
+#include "dl.h"
+#include <bdlib/src/String.h>
+
+typedef void (*AES_cbc_encrypt_t)(const unsigned char*, unsigned char*, const unsigned long, const AES_KEY*, unsigned char*, const int);
+typedef void (*AES_decrypt_t)(const unsigned char*, unsigned char*, const AES_KEY*);
+typedef void (*AES_encrypt_t)(const unsigned char*, unsigned char*, const AES_KEY*);
+typedef int (*AES_set_decrypt_key_t)(const unsigned char*, const int, AES_KEY*);
+typedef int (*AES_set_encrypt_key_t)(const unsigned char*, const int, AES_KEY*);
+
+typedef void (*BF_decrypt_t)(BF_LONG*, const BF_KEY*);
+typedef void (*BF_encrypt_t)(BF_LONG*, const BF_KEY*);
+typedef void (*BF_set_key_t)(BF_KEY*, int, const unsigned char*);
+
+typedef char* (*ERR_error_string_t)(unsigned long, char*);
+typedef unsigned long (*ERR_get_error_t)(void);
+
+typedef void (*OPENSSL_cleanse_t)(void*, size_t);
+
+typedef const char* (*RAND_file_name_t)(char*, size_t);
+typedef int (*RAND_load_file_t)(const char*, long);
+typedef void (*RAND_seed_t)(const void*, int);
+typedef int (*RAND_status_t)(void);
+typedef int (*RAND_write_file_t)(const char*);
+
+typedef int (*MD5_Final_t)(unsigned char*, MD5_CTX*);
+typedef int (*MD5_Init_t)(MD5_CTX*);
+typedef int (*MD5_Update_t)(MD5_CTX*, const void*, size_t);
+typedef int (*SHA1_Final_t)(unsigned char*, SHA_CTX*);
+typedef int (*SHA1_Init_t)(SHA_CTX*);
+typedef int (*SHA1_Update_t)(SHA_CTX*, const void*, size_t);
+typedef int (*SHA256_Final_t)(unsigned char*, SHA256_CTX *);
+typedef int (*SHA256_Init_t)(SHA256_CTX*);
+typedef int (*SHA256_Update_t)(SHA256_CTX*, const void*, size_t);
+
+#include ".defs/libcrypto_defs.h"
+
+int load_libcrypto();
+int unload_libcrypto();
+
+#endif /* !_LIBCRYPTO_H */

+ 4 - 0
src/main.c

@@ -691,6 +691,10 @@ int main(int argc, char **argv)
   check_trace(1);
 #endif
 
+  if (load_libcrypto()) {
+    fatal("Unable to load libcrypto.", 0);
+  }
+
   /* Initialize variables and stuff */
   timer_update_now(&egg_timeval_now);
   now = egg_timeval_now.sec;