From: Kees Monshouwer Date: Sat, 3 Jan 2015 22:24:24 +0000 (+0100) Subject: use polarssl base64 X-Git-Tag: rec-3.7.0-rc1~39^2~1^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=488360551009784ab35c43ee4580e773a2a8a227;p=pdns use polarssl base64 --- diff --git a/pdns/Makefile-recursor b/pdns/Makefile-recursor index d1bfcf426..d0f6412fd 100644 --- a/pdns/Makefile-recursor +++ b/pdns/Makefile-recursor @@ -20,7 +20,7 @@ PDNS_RECURSOR_OBJECTS=syncres.o misc.o unix_utility.o qtype.o logger.o \ arguments.o lwres.o pdns_recursor.o recursor_cache.o dnsparser.o \ dnswriter.o dnsrecords.o rcpgenerator.o base64.o zoneparser-tng.o \ rec_channel.o rec_channel_rec.o selectmplexer.o sillyrecords.o \ -dns_random.o pubsuffix.o ext/polarssl/library/aes.o dnslabeltext.o \ +dns_random.o pubsuffix.o ext/polarssl/library/aes.o ext/polarssl/library/base64.o dnslabeltext.o \ lua-pdns.o lua-recursor.o randomhelper.o recpacketcache.o dns.o \ reczones.o base32.o nsecrecords.o json.o ws-recursor.o ws-api.o \ version.o responsestats.o webserver.o ext/yahttp/yahttp/reqresp.o ext/yahttp/yahttp/router.o \ diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 0b0ee94c5..7d4e82c1b 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -439,6 +439,8 @@ sdig_SOURCES = \ statbag.cc \ unix_utility.cc +sdig_LDADD = $(POLARSSL_LIBS) + saxfr_SOURCES = \ base32.cc \ base64.cc base64.hh \ @@ -458,7 +460,7 @@ saxfr_SOURCES = \ statbag.cc \ unix_utility.cc -saxfr_LDADD= $(POLARSSL_LIBS) +saxfr_LDADD = $(POLARSSL_LIBS) if PKCS11 saxfr_SOURCES += pkcs11signers.cc pkcs11signers.hh @@ -483,8 +485,13 @@ dnstcpbench_SOURCES = \ statbag.cc \ unix_utility.cc -dnstcpbench_LDFLAGS = $(THREADFLAGS) $(BOOST_PROGRAM_OPTIONS_LDFLAGS) -dnstcpbench_LDADD = $(BOOST_PROGRAM_OPTIONS_LIBS) +dnstcpbench_LDFLAGS = \ + $(THREADFLAGS) \ + $(BOOST_PROGRAM_OPTIONS_LDFLAGS) + +dnstcpbench_LDADD = \ + $(POLARSSL_LIBS) \ + $(BOOST_PROGRAM_OPTIONS_LIBS) dnsdist_SOURCES = \ arguments.cc \ @@ -507,8 +514,13 @@ dnsdist_SOURCES = \ statbag.cc \ unix_utility.cc -dnsdist_LDFLAGS = $(THREADFLAGS) $(BOOST_PROGRAM_OPTIONS_LDFLAGS) -dnsdist_LDADD = $(BOOST_PROGRAM_OPTIONS_LIBS) +dnsdist_LDFLAGS = \ + $(THREADFLAGS) \ + $(BOOST_PROGRAM_OPTIONS_LDFLAGS) + +dnsdist_LDADD = \ + $(POLARSSL_LIBS) \ + $(BOOST_PROGRAM_OPTIONS_LIBS) nsec3dig_SOURCES = \ base32.cc \ @@ -607,6 +619,8 @@ speedtest_SOURCES = \ statbag.cc \ unix_utility.cc +speedtest_LDADD = $(POLARSSL_LIBS) + dnswasher_SOURCES = \ dnsparser.hh \ dnspcap.cc dnspcap.hh \ @@ -640,7 +654,9 @@ dnsbulktest_LDFLAGS = \ $(THREADFLAGS) \ $(BOOST_PROGRAM_OPTIONS_LDFLAGS) -dnsbulktest_LDADD = $(BOOST_PROGRAM_OPTIONS_LIBS) +dnsbulktest_LDADD = \ + $(POLARSSL_LIBS) \ + $(BOOST_PROGRAM_OPTIONS_LIBS) dnsscan_SOURCES = \ anadns.hh \ @@ -662,6 +678,8 @@ dnsscan_SOURCES = \ unix_utility.cc \ utility.hh +dnsscan_LDADD = $(POLARSSL_LIBS) + dnsreplay_SOURCES = \ anadns.hh \ base32.cc \ @@ -686,7 +704,9 @@ dnsreplay_LDFLAGS = \ $(THREADFLAGS) \ $(BOOST_PROGRAM_OPTIONS_LDFLAGS) -dnsreplay_LDADD = $(BOOST_PROGRAM_OPTIONS_LIBS) +dnsreplay_LDADD = \ + $(POLARSSL_LIBS) \ + $(BOOST_PROGRAM_OPTIONS_LIBS) nproxy_SOURCES = \ base32.cc \ @@ -711,7 +731,9 @@ nproxy_LDFLAGS = \ $(THREADFLAGS) \ $(BOOST_PROGRAM_OPTIONS_LDFLAGS) -nproxy_LDADD = $(BOOST_PROGRAM_OPTIONS_LIBS) +nproxy_LDADD = \ + $(POLARSSL_LIBS) \ + $(BOOST_PROGRAM_OPTIONS_LIBS) notify_SOURCES = \ arguments.cc \ @@ -737,7 +759,9 @@ notify_LDFLAGS = \ $(THREADFLAGS) \ $(BOOST_PROGRAM_OPTIONS_LDFLAGS) -notify_LDADD = $(BOOST_PROGRAM_OPTIONS_LIBS) +notify_LDADD = \ + $(POLARSSL_LIBS) \ + $(BOOST_PROGRAM_OPTIONS_LIBS) dnsscope_SOURCES = \ arguments.cc \ @@ -764,7 +788,9 @@ dnsscope_LDFLAGS = \ $(THREADFLAGS) \ $(BOOST_PROGRAM_OPTIONS_LDFLAGS) -dnsscope_LDADD = $(BOOST_PROGRAM_OPTIONS_LIBS) +dnsscope_LDADD = \ + $(POLARSSL_LIBS) \ + $(BOOST_PROGRAM_OPTIONS_LIBS) dnsgram_SOURCES = \ base32.cc \ @@ -785,6 +811,8 @@ dnsgram_SOURCES = \ unix_utility.cc \ utility.hh +dnsgram_LDADD = $(POLARSSL_LIBS) + dnsdemog_SOURCES = \ base32.cc \ base64.cc base64.hh \ @@ -804,6 +832,8 @@ dnsdemog_SOURCES = \ unix_utility.cc \ utility.hh +dnsdemog_LDADD = $(POLARSSL_LIBS) + rec_control_SOURCES = \ arguments.cc arguments.hh \ logger.cc \ diff --git a/pdns/base64.cc b/pdns/base64.cc index f7f042244..5c04d01cb 100644 --- a/pdns/base64.cc +++ b/pdns/base64.cc @@ -1,239 +1,29 @@ #include "base64.hh" -#include -#include "dns.hh" -namespace anonpdns { -char B64Decode1(char cInChar) -{ - // The incoming character will be A-Z, a-z, 0-9, +, /, or =. - // The idea is to quickly determine which grouping the - // letter belongs to and return the associated value - // without having to search the global encoding string - // (the value we're looking for would be the resulting - // index into that string). - // - // To do that, we'll play some tricks... - unsigned char iIndex = '\0'; - switch ( cInChar ) { - case '+': - iIndex = 62; - break; - - case '/': - iIndex = 63; - break; - - case '=': - iIndex = 0; - break; - - default: - // Must be 'A'-'Z', 'a'-'z', '0'-'9', or an error... - // - // Numerically, small letters are "greater" in value than - // capital letters and numerals (ASCII value), and capital - // letters are "greater" than numerals (again, ASCII value), - // so we check for numerals first, then capital letters, - // and finally small letters. - iIndex = '9' - cInChar; - if ( iIndex > 0x3F ) { - // Not from '0' to '9'... - iIndex = 'Z' - cInChar; - if ( iIndex > 0x3F ) { - // Not from 'A' to 'Z'... - iIndex = 'z' - cInChar; - if ( iIndex > 0x3F ) { - // Invalid character...cannot - // decode! - iIndex = 0x80; // set the high bit - } // if - else { - // From 'a' to 'z' - iIndex = (('z' - iIndex) - 'a') + 26; - } // else - } // if - else { - // From 'A' to 'Z' - iIndex = ('Z' - iIndex) - 'A'; - } // else - } // if - else { - // Adjust the index... - iIndex = (('9' - iIndex) - '0') + 52; - } // else - break; - - } // switch - - return iIndex; -} +#include +#include -inline char B64Encode1(unsigned char uc) +int B64Decode(const std::string& src, std::string& dst) { - if (uc < 26) - { - return 'A'+uc; - } - if (uc < 52) - { - return 'a'+(uc-26); - } - if (uc < 62) - { - return '0'+(uc-52); - } - if (uc == 62) - { - return '+'; - } - return '/'; -}; - - - + if (src.empty() ) { + dst.clear(); + return 0; + } + size_t dlen = ( src.length() * 6 + 7 ) / 8 ; + boost::scoped_ptr d( new unsigned char[dlen] ); + if ( base64_decode( d.get(), &dlen, (const unsigned char*) src.c_str(), src.length() ) == 0 ) { + dst = std::string( (const char*) d.get(), dlen ); + return 0; + } + return -1; } -using namespace anonpdns; -int B64Decode(const std::string& strInput, std::string& strOutput) +std::string Base64Encode (const std::string& src) { - // Set up a decoding buffer - long cBuf = 0; - char* pBuf = (char*)&cBuf; - - // Decoding management... - int iBitGroup = 0, iInNum = 0; - - // While there are characters to process... - // - // We'll decode characters in blocks of 4, as - // there are 4 groups of 6 bits in 3 bytes. The - // incoming Base64 character is first decoded, and - // then it is inserted into the decode buffer - // (with any relevant shifting, as required). - // Later, after all 3 bytes have been reconstituted, - // we assign them to the output string, ultimately - // to be returned as the original message. - int iInSize = strInput.size(); - unsigned char cChar = '\0'; - uint8_t pad = 0; - while ( iInNum < iInSize ) { - // Fill the decode buffer with 4 groups of 6 bits - cBuf = 0; // clear - pad = 0; - for ( iBitGroup = 0; iBitGroup < 4; ++iBitGroup ) { - if ( iInNum < iInSize ) { - // Decode a character - if(strInput.at(iInNum)=='=') - pad++; - while(isspace(strInput.at(iInNum))) - iInNum++; - cChar = B64Decode1(strInput.at(iInNum++)); - - } // if - else { - // Decode a padded zero - cChar = '\0'; - } // else - - // Check for valid decode - if ( cChar > 0x7F ) - return -1; - - // Adjust the bits - switch ( iBitGroup ) { - case 0: - // The first group is copied into - // the least significant 6 bits of - // the decode buffer...these 6 bits - // will eventually shift over to be - // the most significant bits of the - // third byte. - cBuf = cBuf | cChar; - break; - - default: - // For groupings 1-3, simply shift - // the bits in the decode buffer over - // by 6 and insert the 6 from the - // current decode character. - cBuf = (cBuf << 6) | cChar; - break; - - } // switch - } // for - - // Interpret the resulting 3 bytes...note there - // may have been padding, so those padded bytes - // are actually ignored. -#if BYTE_ORDER == BIG_ENDIAN - strOutput += pBuf[sizeof(long)-3]; - strOutput += pBuf[sizeof(long)-2]; - strOutput += pBuf[sizeof(long)-1]; -#else - strOutput += pBuf[2]; - strOutput += pBuf[1]; - strOutput += pBuf[0]; -#endif - - } // while - if(pad) - strOutput.resize(strOutput.length()-pad); - - return 1; + if (!src.empty()) { + size_t dlen = ( ( ( src.length() + 2 ) / 3 ) * 4 ) + 1; + boost::scoped_ptr dst( new unsigned char[dlen] ); + if( base64_encode( dst.get(), &dlen, (const unsigned char*) src.c_str(), src.length() ) == 0 ) + return std::string( (const char*) dst.get(), dlen ); + } + return ""; } - -/* -www.kbcafe.com -Copyright 2001-2002 Randy Charles Morin -The Encode static method takes an array of 8-bit values and returns a base-64 stream. -*/ - - -std::string Base64Encode (const std::string& vby) -{ - std::string retval; - if (vby.size () == 0) - { - return retval; - }; - for (unsigned int i = 0; i < vby.size (); i += 3) - { - unsigned char by1 = 0, by2 = 0, by3 = 0; - by1 = vby[i]; - if (i + 1 < vby.size ()) - { - by2 = vby[i + 1]; - }; - if (i + 2 < vby.size ()) - { - by3 = vby[i + 2]; - } - unsigned char by4 = 0, by5 = 0, by6 = 0, by7 = 0; - by4 = by1 >> 2; - by5 = ((by1 & 0x3) << 4) | (by2 >> 4); - by6 = ((by2 & 0xf) << 2) | (by3 >> 6); - by7 = by3 & 0x3f; - retval += B64Encode1 (by4); - retval += B64Encode1 (by5); - if (i + 1 < vby.size ()) - { - retval += B64Encode1 (by6); - } - else - { - retval += "="; - }; - if (i + 2 < vby.size ()) - { - retval += B64Encode1 (by7); - } - else - { - retval += "="; - }; - /* if ((i % (76 / 4 * 3)) == 0) - { - retval += "\r\n"; - }*/ - }; - return retval; -}; diff --git a/pdns/base64.hh b/pdns/base64.hh index 06b0bc729..2cc866bb8 100644 --- a/pdns/base64.hh +++ b/pdns/base64.hh @@ -1,8 +1,9 @@ #ifndef PDNS_BASE64_HH #define PDNS_BASE64_HH + #include -#include -int B64Decode(const std::string& strInput, std::string& strOutput); -std::string Base64Encode (const std::string& vby); +int B64Decode(const std::string& src, std::string& dst); +std::string Base64Encode (const std::string& src); + #endif diff --git a/pdns/dist-recursor b/pdns/dist-recursor index 163270b36..8e334bee2 100755 --- a/pdns/dist-recursor +++ b/pdns/dist-recursor @@ -62,9 +62,9 @@ mkdir -p $DIRNAME/ext/rapidjson/include/rapidjson/internal cp -a ext/rapidjson/include/rapidjson/*.h $DIRNAME/ext/rapidjson/include/rapidjson/ cp -a ext/rapidjson/include/rapidjson/internal/*.h $DIRNAME/ext/rapidjson/include/rapidjson/internal mkdir -p $DIRNAME/ext/polarssl/include/polarssl -cp -a ext/polarssl/include/polarssl/config.h ext/polarssl/include/polarssl/check_config.h ext/polarssl/include/polarssl/aes.h ext/polarssl/include/polarssl/platform.h ext/polarssl/include/polarssl/version.h $DIRNAME/ext/polarssl/include/polarssl +cp -a ext/polarssl/include/polarssl/config.h ext/polarssl/include/polarssl/check_config.h ext/polarssl/include/polarssl/aes.h ext/polarssl/include/polarssl/base64.h ext/polarssl/include/polarssl/platform.h ext/polarssl/include/polarssl/version.h $DIRNAME/ext/polarssl/include/polarssl mkdir -p $DIRNAME/ext/polarssl/library -cp -a ext/polarssl/library/aes.c $DIRNAME/ext/polarssl/library +cp -a ext/polarssl/library/aes.c ext/polarssl/library/base64.c $DIRNAME/ext/polarssl/library cp -a ext/yahttp/ $DIRNAME/ext/yahttp echo '#include "../../../config.h"' > $DIRNAME/ext/yahttp/yahttp/yahttp-config.h mkdir $DIRNAME/rrd diff --git a/pdns/test-base64_cc.cc b/pdns/test-base64_cc.cc index e1b536a04..bc9a3cb68 100644 --- a/pdns/test-base64_cc.cc +++ b/pdns/test-base64_cc.cc @@ -26,7 +26,9 @@ BOOST_AUTO_TEST_CASE(test_Base64_Roundtrip) { BOOST_AUTO_TEST_CASE(test_Base64_Encode) { typedef std::map cases_t; cases_t cases; - assign::insert(cases)("z","eg==") + assign::insert(cases) + ("", "") + ("z","eg==") ("x4","eDQ=") ("J07","SjA3") ("kl8F","a2w4Rg==")