]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
auth: Fallback to sha1 for the signatures cache if md5 is not available 7284/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 13 Dec 2018 14:49:58 +0000 (15:49 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 17 Dec 2018 16:10:37 +0000 (17:10 +0100)
modules/goraclebackend/soracle.cc
pdns/Makefile.am
pdns/digests.hh [new file with mode: 0644]
pdns/dnssecsigner.cc
pdns/md5.hh [deleted file]
pdns/test-digests_hh.cc [moved from pdns/test-md5_hh.cc with 60% similarity]
pdns/tsig-tests.cc

index fdf01cacaf2b28472bcf3e50b4d306aa76fe1c61..5442b174ce2b1a46dc2423f6286431fc490d4342 100644 (file)
@@ -29,7 +29,7 @@
 #include "pdns/logger.hh"
 #include "pdns/dns.hh"
 #include "pdns/namespaces.hh"
-#include "pdns/md5.hh"
+#include "pdns/digests.hh"
 
 static AtomicCounter s_txid;
 
index 838af8dd0f80d7ebf617b0f8baa2a51f505af370..eddb1f342cc83536cf3219f7d30b73befc85e423 100644 (file)
@@ -166,6 +166,7 @@ pdns_server_SOURCES = \
        common_startup.cc common_startup.hh \
        communicator.cc communicator.hh \
        dbdnsseckeeper.cc \
+       digests.hh \
        distributor.hh \
        dns.cc dns.hh \
        dns_random.cc dns_random.hh \
@@ -194,7 +195,6 @@ pdns_server_SOURCES = \
        lua-base4.cc lua-base4.hh \
        lua-auth4.cc lua-auth4.hh \
        mastercommunicator.cc \
-       md5.hh \
        misc.cc misc.hh \
        nameserver.cc nameserver.hh \
        namespaces.hh \
@@ -822,6 +822,7 @@ tsig_tests_SOURCES = \
        arguments.cc \
        base32.cc \
        base64.cc base64.hh \
+       digests.hh \
        dns.cc \
        dns_random_urandom.cc dns_random.hh \
        dnslabeltext.cc \
@@ -1287,6 +1288,7 @@ testrunner_SOURCES = \
        test-bindparser_cc.cc \
        test-common.hh \
        test-dnsrecordcontent.cc \
+       test-digests_hh.cc \
        test-distributor_hh.cc \
        test-dns_random_hh.cc \
        test-dnsname_cc.cc \
@@ -1297,7 +1299,6 @@ testrunner_SOURCES = \
        test-ixfr_cc.cc \
        test-lock_hh.cc \
        test-lua_auth4_cc.cc \
-       test-md5_hh.cc \
        test-misc_hh.cc \
        test-nameserver_cc.cc \
        test-packetcache_cc.cc \
diff --git a/pdns/digests.hh b/pdns/digests.hh
new file mode 100644 (file)
index 0000000..8e32acc
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#pragma once
+
+#include <stdexcept>
+#include <string>
+
+#include <openssl/evp.h>
+
+inline std::string pdns_hash(const EVP_MD * md, const std::string& input)
+{
+#if OPENSSL_VERSION_NUMBER < 0x1010000fL
+  auto mdctx = std::unique_ptr<EVP_MD_CTX, void(*)(EVP_MD_CTX*)>(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
+#else
+  auto mdctx = std::unique_ptr<EVP_MD_CTX, void(*)(EVP_MD_CTX*)>(EVP_MD_CTX_new(), EVP_MD_CTX_free);
+#endif
+  if (!mdctx) {
+    throw std::runtime_error(std::string(EVP_MD_name(md)) + " context initialization failed");
+  }
+
+  if (EVP_DigestInit_ex(mdctx.get(), md, nullptr) != 1) {
+    throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP initialization failed");
+  }
+
+  if (EVP_DigestUpdate(mdctx.get(), input.data(), input.size()) != 1) {
+    throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP update failed");
+  }
+
+  unsigned int written;
+  std::string result;
+  result.resize(EVP_MD_size(md));
+
+  if (EVP_DigestFinal_ex(mdctx.get(), const_cast<unsigned char *>(reinterpret_cast<const unsigned char*>(result.c_str())), &written) != 1) {
+    throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP final failed");
+  }
+
+  if (written != result.size()) {
+    throw std::runtime_error(std::string(EVP_MD_name(md)) + " EVP final wrote " + std::to_string(written) + ", expected " + std::to_string(result.size()));
+  }
+
+  return result;
+}
+
+inline std::string pdns_md5sum(const std::string& input)
+{
+  const auto md = EVP_md5();
+  if (md == nullptr) {
+    throw std::runtime_error("The MD5 digest is not available via the OpenSSL EVP interface");
+  }
+
+  return pdns_hash(md, input);
+}
+
+inline std::string pdns_sha1sum(const std::string& input)
+{
+  const auto md = EVP_sha1();
+  if (md == nullptr) {
+    throw std::runtime_error("The SHA1 digest is not available via the OpenSSL EVP interface");
+  }
+
+  return pdns_hash(md, input);
+}
index 5c563d7efde837ad18ca3059d087076626757483..22abee7cc3cb8675ff33eef56f7165682d2b0318 100644 (file)
@@ -25,7 +25,7 @@
 #include "dnssecinfra.hh"
 #include "namespaces.hh"
 
-#include "md5.hh"
+#include "digests.hh"
 #include "dnsseckeeper.hh"
 #include "dns_random.hh"
 #include "lock.hh"
@@ -41,6 +41,16 @@ static int g_cacheweekno;
 const static std::set<uint16_t> g_KSKSignedQTypes {QType::DNSKEY, QType::CDS, QType::CDNSKEY};
 AtomicCounter* g_signatureCount;
 
+static std::string getLookupKey(const std::string& msg)
+{
+  try {
+    return pdns_md5sum(msg);
+  }
+  catch(const std::runtime_error& e) {
+    return pdns_sha1sum(msg);
+  }
+}
+
 static void fillOutRRSIG(DNSSECPrivateKey& dpk, const DNSName& signQName, RRSIGRecordContent& rrc, vector<shared_ptr<DNSRecordContent> >& toSign)
 {
   if(!g_signatureCount)
@@ -52,7 +62,7 @@ static void fillOutRRSIG(DNSSECPrivateKey& dpk, const DNSName& signQName, RRSIGR
   rrc.d_algorithm = drc.d_algorithm;
 
   string msg=getMessageForRRSET(signQName, rrc, toSign); // this is what we will hash & sign
-  pair<string, string> lookup(rc->getPubKeyHash(), pdns_md5sum(msg));  // this hash is a memory saving exercise
+  pair<string, string> lookup(rc->getPubKeyHash(), getLookupKey(msg));  // this hash is a memory saving exercise
 
   bool doCache=1;
   if(doCache)
diff --git a/pdns/md5.hh b/pdns/md5.hh
deleted file mode 100644 (file)
index 13f69b1..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * This file is part of PowerDNS or dnsdist.
- * Copyright -- PowerDNS.COM B.V. and its contributors
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * In addition, for the avoidance of any doubt, permission is granted to
- * link this program with OpenSSL and to (re)distribute the binaries
- * produced as the result of such linking.
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-#ifndef _MD5_H
-#define _MD5_H
-
-#include <string>
-#include <stdint.h>
-#include <openssl/md5.h>
-
-inline std::string pdns_md5sum(const std::string& input)
-{
-  unsigned char result[16] = {0};
-  MD5(reinterpret_cast<const unsigned char*>(input.c_str()), input.length(), result);
-  return std::string(result, result + sizeof result);
-}
-
-#endif /* md5.h */
similarity index 60%
rename from pdns/test-md5_hh.cc
rename to pdns/test-digests_hh.cc
index 5d9e9089801946db5dbd310787db893eac9ddd79..c061a8c204736a9efcb920bca7eecea21e64edc7 100644 (file)
@@ -6,18 +6,26 @@
 #include <boost/test/unit_test.hpp>
 #include <boost/assign/list_of.hpp>
 
-#include "md5.hh"
+#include "digests.hh"
 #include "misc.hh"
 
 using namespace boost;
 
-BOOST_AUTO_TEST_SUITE(test_md5_hh)
+BOOST_AUTO_TEST_SUITE(test_digests_hh)
 
 BOOST_AUTO_TEST_CASE(test_pdns_md5sum)
 {
    std::string result = "a3 24 8c e3 1a 88 a6 40 e6 30 73 98 57 6d 06 9e ";
    std::string sum = pdns_md5sum("a quick brown fox jumped over the lazy dog");
-   
+
+   BOOST_CHECK_EQUAL(makeHexDump(sum), result);
+}
+
+BOOST_AUTO_TEST_CASE(test_pdns_sha1sum)
+{
+   std::string result = "b9 37 10 0d c9 57 b3 86 d9 cb 77 fc 90 c0 18 22 fd eb 6e 7f ";
+   std::string sum = pdns_sha1sum("a quick brown fox jumped over the lazy dog");
+
    BOOST_CHECK_EQUAL(makeHexDump(sum), result);
 }
 
index 93900fb258c6641f33c568a55e1749f3a3acf226..3a705096cd5369949b86a083677d973daccbf5f6 100644 (file)
@@ -8,7 +8,7 @@
 #include "dnswriter.hh"
 #include "dnsrecords.hh"
 #include "statbag.hh"
-#include "md5.hh"
+#include "digests.hh"
 #include "base64.hh"
 #include "dnssecinfra.hh"
 #include "resolver.hh"