#include "pdns/logger.hh"
#include "pdns/dns.hh"
#include "pdns/namespaces.hh"
-#include "pdns/md5.hh"
+#include "pdns/digests.hh"
static AtomicCounter s_txid;
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 \
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 \
arguments.cc \
base32.cc \
base64.cc base64.hh \
+ digests.hh \
dns.cc \
dns_random_urandom.cc dns_random.hh \
dnslabeltext.cc \
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 \
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 \
--- /dev/null
+/*
+ * 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);
+}
#include "dnssecinfra.hh"
#include "namespaces.hh"
-#include "md5.hh"
+#include "digests.hh"
#include "dnsseckeeper.hh"
#include "dns_random.hh"
#include "lock.hh"
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)
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)
+++ /dev/null
-/*
- * 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 */
#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);
}
#include "dnswriter.hh"
#include "dnsrecords.hh"
#include "statbag.hh"
-#include "md5.hh"
+#include "digests.hh"
#include "base64.hh"
#include "dnssecinfra.hh"
#include "resolver.hh"