Skip to content

Commit

Permalink
Cookies: use constant time comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterlexis committed Jul 7, 2021
1 parent 6709274 commit a8af6b0
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions pdns/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ EXTRA_DIST = \
lua-record.cc \
minicurl.cc \
minicurl.hh \
string_compare.hh \
api-swagger.yaml \
api-swagger.json \
requirements.txt \
Expand Down
1 change: 1 addition & 0 deletions pdns/dnsdistdist/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ EXTRA_DIST=COPYING \
kqueuemplexer.cc \
portsmplexer.cc \
cdb.cc cdb.hh \
string_compare.hh \
ext/lmdb-safe/lmdb-safe.cc ext/lmdb-safe/lmdb-safe.hh \
ext/protozero/include/* \
builder-support/gen-version
Expand Down
1 change: 1 addition & 0 deletions pdns/dnsdistdist/string_compare.hh
6 changes: 5 additions & 1 deletion pdns/ednscookies.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ednscookies.hh"
#include "misc.hh"
#include "string_compare.hh"

#ifdef HAVE_CRYPTO_SHORTHASH
#include <sodium.h>
#endif


EDNSCookiesOpt::EDNSCookiesOpt(const std::string& option)
{
getEDNSCookiesOptFromString(option.c_str(), option.length());
Expand Down Expand Up @@ -106,7 +110,7 @@ bool EDNSCookiesOpt::isValid(const string& secret, const ComboAddress& source)
reinterpret_cast<const unsigned char*>(&toHash[0]),
toHash.length(),
reinterpret_cast<const unsigned char*>(&secret[0]));
return server.substr(8) == hashResult;
return constantTimeStringEquals(server.substr(8), hashResult);
#else
return false;
#endif
Expand Down
1 change: 1 addition & 0 deletions pdns/recursordist/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ EXTRA_DIST = \
mtasker_fcontext.cc mtasker_ucontext.cc \
NOTICE \
opensslsigners.hh opensslsigners.cc \
string_compare.hh \
portsmplexer.cc \
dnstap.proto dnstap.cc dnstap.hh fstrm_logger.cc fstrm_logger.hh \
ext/protozero/include/* \
Expand Down
1 change: 1 addition & 0 deletions pdns/recursordist/string_compare.hh
5 changes: 2 additions & 3 deletions pdns/string_compare.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ static bool constantTimeStringEquals(const std::string& a, const std::string& b)
#ifdef HAVE_CRYPTO_MEMCMP
return CRYPTO_memcmp(a.c_str(), b.c_str(), size) == 0;
#else
const volatile unsigned char *_a = (const volatile unsigned char *) a.c_str();
const volatile unsigned char *_b = (const volatile unsigned char *) b.c_str();
const volatile unsigned char* _a = (const volatile unsigned char*)a.c_str();
const volatile unsigned char* _b = (const volatile unsigned char*)b.c_str();
unsigned char res = 0;

for (size_t idx = 0; idx < size; idx++) {
Expand All @@ -50,4 +50,3 @@ static bool constantTimeStringEquals(const std::string& a, const std::string& b)
return res == 0;
#endif
}

0 comments on commit a8af6b0

Please sign in to comment.