6d394f
Author: Vaclav Dolezal <vdolezal@redhat.com>
6d394f
Date:   Mon Aug 12 14:51:39 2019 +0200
6d394f
6d394f
    Use functions from OpenSSL for HMAC, MD5 and random bytes
6d394f
6d394f
diff --git a/bacula/src/dird/dird_conf.c b/bacula/src/dird/dird_conf.c
6d394f
index 02fae0bab..dff241356 100644
6d394f
--- a/bacula/src/dird/dird_conf.c
6d394f
+++ b/bacula/src/dird/dird_conf.c
6d394f
@@ -42,6 +42,10 @@
6d394f
 #include "bacula.h"
6d394f
 #include "dird.h"
6d394f
 
6d394f
+#if HAVE_OPENSSL
6d394f
+# include <openssl/evp.h>
6d394f
+#endif
6d394f
+
6d394f
 /* Define the first and last resource ID record
6d394f
  * types. Note, these should be unique for each
6d394f
  * daemon though not a requirement.
6d394f
@@ -1645,6 +1649,11 @@ void free_resource(RES *rres, int type)
6d394f
          free(res->res_fs.exclude_items);
6d394f
       }
6d394f
       res->res_fs.num_excludes = 0;
6d394f
+#if HAVE_OPENSSL
6d394f
+      EVP_MD_CTX_free(res->res_fs.md5c);
6d394f
+      res->res_fs.md5c = NULL;
6d394f
+      res->res_fs.have_MD5 = false;
6d394f
+#endif
6d394f
       break;
6d394f
    case R_POOL:
6d394f
       if (res->res_pool.pool_type) {
6d394f
diff --git a/bacula/src/dird/dird_conf.h b/bacula/src/dird/dird_conf.h
6d394f
index 5174a7a14..4e910c5bd 100644
6d394f
--- a/bacula/src/dird/dird_conf.h
6d394f
+++ b/bacula/src/dird/dird_conf.h
6d394f
@@ -24,6 +24,10 @@
6d394f
 
6d394f
 /* NOTE:  #includes at the end of this file */
6d394f
 
6d394f
+#if HAVE_OPENSSL
6d394f
+# include <openssl/evp.h>
6d394f
+#endif
6d394f
+
6d394f
 /*
6d394f
  * Resource codes -- they must be sequential for indexing
6d394f
  */
6d394f
@@ -591,7 +595,11 @@ public:
6d394f
    INCEXE **exclude_items;
6d394f
    int32_t num_excludes;
6d394f
    bool have_MD5;                     /* set if MD5 initialized */
6d394f
+#if HAVE_OPENSSL
6d394f
+   EVP_MD_CTX *md5c;                  /* MD5 of include/exclude */
6d394f
+#else
6d394f
    struct MD5Context md5c;            /* MD5 of include/exclude */
6d394f
+#endif
6d394f
    char MD5[30];                      /* base 64 representation of MD5 */
6d394f
    bool ignore_fs_changes;            /* Don't force Full if FS changed */
6d394f
    bool enable_vss;                   /* Enable Volume Shadow Copy */
6d394f
diff --git a/bacula/src/dird/inc_conf.c b/bacula/src/dird/inc_conf.c
6d394f
index 3f4fbf55e..64b422242 100644
6d394f
--- a/bacula/src/dird/inc_conf.c
6d394f
+++ b/bacula/src/dird/inc_conf.c
6d394f
@@ -32,6 +32,10 @@
6d394f
 #include <regex.h>
6d394f
 #endif
6d394f
 
6d394f
+#if HAVE_OPENSSL
6d394f
+# include <openssl/evp.h>
6d394f
+#endif
6d394f
+
6d394f
 /* Forward referenced subroutines */
6d394f
 
6d394f
 void store_inc(LEX *lc, RES_ITEM *item, int index, int pass);
6d394f
@@ -354,7 +358,17 @@ static void store_newinc(LEX *lc, RES_ITEM *item, int index, int pass)
6d394f
    bool options;
6d394f
 
6d394f
    if (!res_all.res_fs.have_MD5) {
6d394f
+#if HAVE_OPENSSL
6d394f
+      res_all.res_fs.md5c = EVP_MD_CTX_new();
6d394f
+      if (!res_all.res_fs.md5c
6d394f
+         || !EVP_DigestInit_ex(res_all.res_fs.md5c, EVP_md5(), NULL)
6d394f
+      ) {
6d394f
+         Emsg1(M_ERROR_TERM, 0, "MD5 computation failed: %s\n",
6d394f
+               ERR_reason_error_string(ERR_peek_last_error()));
6d394f
+      }
6d394f
+#else
6d394f
       MD5Init(&res_all.res_fs.md5c);
6d394f
+#endif
6d394f
       res_all.res_fs.have_MD5 = true;
6d394f
    }
6d394f
    memset(&res_incexe, 0, sizeof(INCEXE));
6d394f
@@ -620,7 +634,13 @@ static void store_fname(LEX *lc, RES_ITEM2 *item, int index, int pass, bool excl
6d394f
          }
6d394f
       case T_QUOTED_STRING:
6d394f
          if (res_all.res_fs.have_MD5) {
6d394f
+#if HAVE_OPENSSL
6d394f
+            if (!EVP_DigestUpdate(res_all.res_fs.md5c, (void *)lc->str, (size_t) lc->str_len))
6d394f
+               Emsg1(M_ERROR_TERM, 0, "MD5 computation failed: %s\n",
6d394f
+                     ERR_reason_error_string(ERR_peek_last_error()));
6d394f
+#else
6d394f
             MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
6d394f
+#endif
6d394f
          }
6d394f
          incexe = &res_incexe;
6d394f
          if (incexe->name_list.size() == 0) {
6d394f
@@ -663,7 +683,13 @@ static void store_plugin_name(LEX *lc, RES_ITEM2 *item, int index, int pass, boo
6d394f
          }
6d394f
       case T_QUOTED_STRING:
6d394f
          if (res_all.res_fs.have_MD5) {
6d394f
+#if HAVE_OPENSSL
6d394f
+            if (!EVP_DigestUpdate(res_all.res_fs.md5c, (void *)lc->str, (size_t) lc->str_len))
6d394f
+               Emsg1(M_ERROR_TERM, 0, "MD5 computation failed: %s\n",
6d394f
+                     ERR_reason_error_string(ERR_peek_last_error()));
6d394f
+#else
6d394f
             MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
6d394f
+#endif
6d394f
          }
6d394f
          incexe = &res_incexe;
6d394f
          if (incexe->plugin_list.size() == 0) {
6d394f
diff --git a/bacula/src/dird/job.c b/bacula/src/dird/job.c
6d394f
index b5b39c7d5..7d69f0157 100644
6d394f
--- a/bacula/src/dird/job.c
6d394f
+++ b/bacula/src/dird/job.c
6d394f
@@ -25,6 +25,10 @@
6d394f
 #include "bacula.h"
6d394f
 #include "dird.h"
6d394f
 
6d394f
+#if HAVE_OPENSSL
6d394f
+# include <openssl/evp.h>
6d394f
+#endif
6d394f
+
6d394f
 /* Forward referenced subroutines */
6d394f
 static void *job_thread(void *arg);
6d394f
 static void job_monitor_watchdog(watchdog_t *self);
6d394f
@@ -1308,10 +1312,27 @@ bool get_or_create_fileset_record(JCR *jcr)
6d394f
    memset(&fsr, 0, sizeof(FILESET_DBR));
6d394f
    bstrncpy(fsr.FileSet, jcr->fileset->hdr.name, sizeof(fsr.FileSet));
6d394f
    if (jcr->fileset->have_MD5) {
6d394f
+#if HAVE_OPENSSL
6d394f
+      EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
6d394f
+      if (!mdctx)
6d394f
+         Emsg1(M_ERROR_TERM, 0, "MD5 computation failed: %s\n",
6d394f
+               ERR_reason_error_string(ERR_peek_last_error()));
6d394f
+#else
6d394f
       struct MD5Context md5c;
6d394f
+#endif
6d394f
       unsigned char digest[MD5HashSize];
6d394f
+#if HAVE_OPENSSL
6d394f
+      if (!EVP_MD_CTX_copy_ex(mdctx, jcr->fileset->md5c)
6d394f
+         || !EVP_DigestFinal_ex(mdctx, digest, NULL)
6d394f
+      ) {
6d394f
+         Emsg1(M_ERROR_TERM, 0, "MD5 computation failed: %s\n",
6d394f
+               ERR_reason_error_string(ERR_peek_last_error()));
6d394f
+      }
6d394f
+      EVP_MD_CTX_free(mdctx);
6d394f
+#else
6d394f
       memcpy(&md5c, &jcr->fileset->md5c, sizeof(md5c));
6d394f
       MD5Final(digest, &md5c);
6d394f
+#endif
6d394f
       /*
6d394f
        * Keep the flag (last arg) set to false otherwise old FileSets will
6d394f
        * get new MD5 sums and the user will get Full backups on everything
6d394f
diff --git a/bacula/src/lib/hmac.c b/bacula/src/lib/hmac.c
6d394f
index a8d5e3dc0..dc3b78383 100644
6d394f
--- a/bacula/src/lib/hmac.c
6d394f
+++ b/bacula/src/lib/hmac.c
6d394f
@@ -26,6 +26,10 @@
6d394f
  */
6d394f
 #include "bacula.h"
6d394f
 
6d394f
+#if HAVE_OPENSSL
6d394f
+# include <openssl/hmac.h>
6d394f
+#endif
6d394f
+
6d394f
 #define PAD_LEN 64           /* PAD length */
6d394f
 #define SIG_LEN MD5HashSize  /* MD5 digest length */
6d394f
 
6d394f
@@ -36,6 +40,19 @@ hmac_md5(
6d394f
     uint8_t*  key,             /* pointer to authentication key */
6d394f
     int   key_len,             /* length of authentication key */
6d394f
     uint8_t  *hmac)            /* returned hmac-md5 */
6d394f
+#if HAVE_OPENSSL
6d394f
+{
6d394f
+    if (!HMAC(
6d394f
+        EVP_md5(),
6d394f
+        key, key_len,
6d394f
+        text, text_len,
6d394f
+        hmac, NULL
6d394f
+    )) {
6d394f
+        Emsg0(M_ERROR_TERM, 0, "HMAC computation failed\n");
6d394f
+    }
6d394f
+
6d394f
+}
6d394f
+#else
6d394f
 {
6d394f
    MD5Context md5c;
6d394f
    uint8_t k_ipad[PAD_LEN];    /* inner padding - key XORd with ipad */
6d394f
@@ -90,6 +107,7 @@ hmac_md5(
6d394f
    MD5Update(&md5c, hmac, SIG_LEN);   /* hash inner hash */
6d394f
    MD5Final(hmac, &md5c);             /* store results */
6d394f
 }
6d394f
+#endif
6d394f
 /*
6d394f
 Test Vectors (Trailing '\0' of a character string not included in test):
6d394f
 
6d394f
diff --git a/bacula/src/lib/parse_conf.c b/bacula/src/lib/parse_conf.c
6d394f
index cb3573fbd..3f3f93fdc 100644
6d394f
--- a/bacula/src/lib/parse_conf.c
6d394f
+++ b/bacula/src/lib/parse_conf.c
6d394f
@@ -59,6 +59,10 @@
6d394f
 #define MAX_PATH  1024
6d394f
 #endif
6d394f
 
6d394f
+#if HAVE_OPENSSL
6d394f
+# include <openssl/evp.h>
6d394f
+#endif
6d394f
+
6d394f
 /*
6d394f
  * Define the Union of all the common resource structure definitions.
6d394f
  */
6d394f
@@ -538,7 +542,11 @@ void store_dir(LEX *lc, RES_ITEM *item, int index, int pass)
6d394f
 void store_password(LEX *lc, RES_ITEM *item, int index, int pass)
6d394f
 {
6d394f
    unsigned int i, j;
6d394f
+#if HAVE_OPENSSL
6d394f
+   EVP_MD_CTX *mdctx = NULL;
6d394f
+#else
6d394f
    struct MD5Context md5c;
6d394f
+#endif
6d394f
    unsigned char digest[CRYPTO_DIGEST_MD5_SIZE];
6d394f
    char sig[100];
6d394f
 
6d394f
@@ -548,9 +556,21 @@ void store_password(LEX *lc, RES_ITEM *item, int index, int pass)
6d394f
    } else {
6d394f
       lex_get_token(lc, T_STRING);
6d394f
       if (pass == 1) {
6d394f
+#if HAVE_OPENSSL
6d394f
+         mdctx = EVP_MD_CTX_new();
6d394f
+         if (!mdctx
6d394f
+            || !EVP_DigestInit_ex(mdctx, EVP_md5(), NULL)
6d394f
+            || !EVP_DigestUpdate(mdctx, (const void *) lc->str, (size_t) lc->str_len)
6d394f
+            || !EVP_DigestFinal_ex(mdctx, digest, NULL)
6d394f
+         ) {
6d394f
+            Emsg1(M_ERROR_TERM, 0, "MD5 computation failed: %s\n",
6d394f
+                  ERR_reason_error_string(ERR_peek_last_error()));
6d394f
+         }
6d394f
+#else
6d394f
          MD5Init(&md5c);
6d394f
          MD5Update(&md5c, (unsigned char *) (lc->str), lc->str_len);
6d394f
          MD5Final(digest, &md5c);
6d394f
+#endif
6d394f
          for (i = j = 0; i < sizeof(digest); i++) {
6d394f
             sprintf(&sig[j], "%02x", digest[i]);
6d394f
             j += 2;
6d394f
diff --git a/bacula/src/lib/util.c b/bacula/src/lib/util.c
6d394f
index 2c425aa4c..e82b907d8 100644
6d394f
--- a/bacula/src/lib/util.c
6d394f
+++ b/bacula/src/lib/util.c
6d394f
@@ -707,6 +707,35 @@ int do_shell_expansion(char *name, int name_len)
6d394f
     from SpeakFreely by John Walker */
6d394f
 
6d394f
 void make_session_key(char *key, char *seed, int mode)
6d394f
+#if HAVE_OPENSSL
6d394f
+{
6d394f
+   int j, k;
6d394f
+   unsigned char buf[16];
6d394f
+
6d394f
+   (void) seed;
6d394f
+
6d394f
+   if (!RAND_bytes(buf, sizeof(buf)))
6d394f
+       Emsg1(M_ERROR_TERM, 0, "Random bytes generation failed: %s\n",
6d394f
+             ERR_reason_error_string(ERR_peek_last_error()));
6d394f
+
6d394f
+   if (mode) {
6d394f
+     for (j = k = 0; j < 16; j++) {
6d394f
+        unsigned char rb = buf[j];
6d394f
+
6d394f
+#define Rad16(x) ((x) + 'A')
6d394f
+        key[k++] = Rad16((rb >> 4) & 0xF);
6d394f
+        key[k++] = Rad16(rb & 0xF);
6d394f
+#undef Rad16
6d394f
+        if (j & 1) {
6d394f
+           key[k++] = '-';
6d394f
+        }
6d394f
+     }
6d394f
+     key[--k] = 0;
6d394f
+   } else {
6d394f
+      memcpy(key, buf, sizeof(buf));
6d394f
+   }
6d394f
+}
6d394f
+#else
6d394f
 {
6d394f
    int j, k;
6d394f
    struct MD5Context md5c;
6d394f
@@ -790,6 +819,7 @@ void make_session_key(char *key, char *seed, int mode)
6d394f
    }
6d394f
 }
6d394f
 #undef nextrand
6d394f
+#endif
6d394f
 
6d394f
 void encode_session_key(char *encode, char *session, char *key, int maxlen)
6d394f
 {