Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / libcaptive / client / options-module.c
index daefd7e..a81c2e5 100644 (file)
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <openssl/md5.h>
+#include <openssl/bn.h>
+#include <openssl/crypto.h>
+#include <ctype.h>
 
 
 gboolean captive_options_module_load(struct captive_options_module *options_module,const gchar *pathname_utf8)
@@ -51,7 +55,27 @@ gboolean captive_options_module_load(struct captive_options_module *options_modu
 
        if (options_module->u.pe32.length>=2
                        && (('M'<<8U)|('Z'<<0U))==GUINT16_FROM_BE(*(const guint16 *)options_module->u.pe32.base)) {
+unsigned char md5_bin[1+128/8];        /* 128 bits==16 bytes; '1+' for leading stub to prevent shorter output of BN_bn2hex() */
+BIGNUM *bignum;
+char *hex,*s;
+
                /* already done above */
+               /* Calculate MD5 sum and convert it to hex string: */
+               MD5(options_module->u.pe32.base,options_module->u.pe32.length,md5_bin+1);
+               md5_bin[0]=0xFF;        /* stub to prevent shorter output of BN_bn2hex() */
+               bignum=BN_bin2bn(md5_bin,1+128/8,NULL);
+               hex=BN_bn2hex(bignum);
+               g_assert(strlen(hex)==2*(1+128/8));
+               options_module->u.pe32.md5=g_strdup(hex+2);
+               OPENSSL_free(hex);
+               BN_free(bignum);
+               /* BN_bn2hex() returns uppercased string: */
+               g_assert(strlen(options_module->u.pe32.md5)==32);
+               for (s=options_module->u.pe32.md5;*s;s++) {
+                       g_assert(isxdigit(*s));
+                       *s=tolower(*s);
+                       g_assert(isxdigit(*s));
+                       }
                }
        else {
                captive_rtl_file_munmap(options_module->u.pe32.base);
@@ -78,6 +102,7 @@ void captive_options_module_copy(struct captive_options_module *dest,const struc
                        dest->u.pe32.base=g_memdup(src->u.pe32.base,src->u.pe32.length);
                        dest->u.pe32.length=src->u.pe32.length;
                        dest->u.pe32.mapped=FALSE;
+                       dest->u.pe32.md5=g_strdup(src->u.pe32.md5);
                        break;
 
                case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
@@ -104,6 +129,7 @@ void captive_options_module_free(struct captive_options_module *options_module)
                                captive_rtl_file_munmap(options_module->u.pe32.base);
                        else
                                g_free(options_module->u.pe32.base);
+                       g_free(options_module->u.pe32.md5);
                        break;
 
                case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE: