Blame SOURCES/bacula-9.0.6-use-crypto-from-openssl.patch

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