Replace boost-base64 with an openssl implementation

Also add unit tests

fixes #10714
This commit is contained in:
Jean Flach 2015-12-18 16:03:57 +01:00
parent 82961d8977
commit 957cf3169f
3 changed files with 102 additions and 29 deletions

View File

@ -18,44 +18,50 @@
******************************************************************************/ ******************************************************************************/
#include "remote/base64.hpp" #include "remote/base64.hpp"
#include <boost/archive/iterators/base64_from_binary.hpp> #include <openssl/bio.h>
#include <boost/archive/iterators/binary_from_base64.hpp> #include <openssl/evp.h>
#include <boost/archive/iterators/insert_linebreaks.hpp> #include <openssl/buffer.h>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>
#include <sstream> #include <sstream>
using namespace icinga; using namespace icinga;
const String base64_padding[] = {"", "==", "="}; String Base64::Encode(const String& input)
String Base64::Encode(const String& data)
{ {
typedef boost::archive::iterators::base64_from_binary <boost::archive::iterators::transform_width<const char *, 6, 8> > base64_encode; BIO *biomem = BIO_new(BIO_s_mem());
BIO *bio64 = BIO_new(BIO_f_base64());
BIO_push(bio64, biomem);
BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio64, input.CStr(), input.GetLength());
BIO_flush(bio64);
std::ostringstream msgbuf; char *outbuf;
std::copy(base64_encode(data.CStr()), base64_encode(data.CStr() + data.GetLength()), std::ostream_iterator<char>(msgbuf)); long len = BIO_get_mem_data(biomem, &outbuf);
msgbuf << base64_padding[data.GetLength() % 3];
return msgbuf.str(); String ret = String(outbuf, outbuf + len);
BIO_free_all(bio64);
return ret;
} }
String Base64::Decode(const String& data) String Base64::Decode(const String& input)
{ {
typedef boost::archive::iterators::transform_width<boost::archive::iterators::binary_from_base64<const char *>, 8, 6> base64_decode; BIO *biomem = BIO_new_mem_buf(
const_cast<char*>(input.CStr()), input.GetLength());
BIO *bio64 = BIO_new(BIO_f_base64());
BIO_push(bio64, biomem);
BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
String::SizeType size = data.GetLength(); char *outbuf = new char[input.GetLength()];
if (size && data[size - 1] == '=') { size_t rc, len = 0;
--size; while (rc = BIO_read(bio64, outbuf + len, input.GetLength() - len))
len += rc;
if (size && data[size - 1] == '=') String ret = String(outbuf, outbuf + len);
--size; BIO_free_all(bio64);
}
if (size == 0) if (ret.IsEmpty() && !input.IsEmpty())
return String(); throw std::invalid_argument("Not a valid base64 string");
std::ostringstream msgbuf; return ret;
std::copy(base64_decode(data.CStr()), base64_decode(data.CStr() + size), std::ostream_iterator<char>(msgbuf));
return msgbuf.str();
} }

View File

@ -24,7 +24,7 @@ set(base_test_SOURCES
base-stream.cpp base-string.cpp base-timer.cpp base-type.cpp base-stream.cpp base-string.cpp base-timer.cpp base-type.cpp
base-value.cpp config-ops.cpp icinga-macros.cpp base-value.cpp config-ops.cpp icinga-macros.cpp
icinga-perfdata.cpp test.cpp icinga-perfdata.cpp test.cpp
remote-url.cpp remote-base64.cpp remote-url.cpp
) )
set(livestatus_test_SOURCES set(livestatus_test_SOURCES
@ -107,6 +107,7 @@ add_boost_test(base
icinga_perfdata/ignore_invalid_warn_crit_min_max icinga_perfdata/ignore_invalid_warn_crit_min_max
icinga_perfdata/invalid icinga_perfdata/invalid
icinga_perfdata/multi icinga_perfdata/multi
remote_base64/base64
remote_url/id_and_path remote_url/id_and_path
remote_url/parameters remote_url/parameters
remote_url/get_and_set remote_url/get_and_set

66
test/remote-base64.cpp Normal file
View File

@ -0,0 +1,66 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* 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 St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "remote/base64.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/foreach.hpp>
#include <iostream>
using namespace icinga;
BOOST_AUTO_TEST_SUITE(remote_base64)
BOOST_AUTO_TEST_CASE(base64)
{
std::vector<String> clearText;
clearText.push_back("");
clearText.push_back("1");
clearText.push_back("12");
clearText.push_back("123");
clearText.push_back("1234");
clearText.push_back(
"VsowLvPqEiAeITDmo-5L_NB-k7fsT3sT2d3K9O4iC2uBk41hvCPAxrgGSxrdeX5s"
"Zo0Z9b1kxDZlzf8GHQ9ARW6YLeGODMtiZo8cKkUzfSbxyZ_wlE9u6pCTTg9kODCM"
"Ve-X_a3jWkOy89RoDkT5ahKBY-8S25L6wlvWt8ZyQ2bLxfplzEzuHgEknTMKKp2K"
"jRlwI2p3gF4FYeQM7dx0E5O782Lh1P3IC6jPNqiZgTgWmsRYZbAN8oU2V626bQxD"
"n8prQ0Xr_3aPdP7VIVgxNZMnF0NJrQvB_rzq1Dip1UM_xH_9nansbX25E64OQU-r"
"q54EdO-vb_9FvyqdeVTJ3UTgXIP7OXtz4K8xlEHWdb1-hJChVvDc0KSnN5HVN2NJ"
"yJrAofVyHBxXGRnGMdN8cOwvxzBFsz2Hih_lIqm1NVULm9_J9GoesY-aN8JzGocU"
"U3hbhFQBiUlzliuodhwg4RXRcfmPHQRo7kWKaohpySkvqmWcXEAt2LPJ8nH70fW7"
"vudgzwwWTnNcMlf0Wa-nKL4xXNNPQD0obDCfogN8uKuGqi0DltOUmFK62Zkkb0_d"
"45grssnD5q89MjDGBkGMXuLY_JLOqc7Y9VV6H48vzoTNK1a2kOGV2TrAD8syuA5Z"
"o8RLKjTqAYjKTUqEJjg0MflpiBnbDQvRqiSXs1cJuFNXRLpEC5GoqGqMd0zAGn4u"
"5J3OurVd0SFp8_vkYUI6YwNUe00y8_Dn6DOBh_0KKADphZBgple82_8HrnQNreQn"
"GkB2TpIsjwWud0yuhI-jQZEMNNlhEYMLwx7B-xTGhn0LFC1pLEXn_kZ2NOgDgUHd"
"bdj906o3N2Jjo9Fb5GXkCrt-fNEYBjeXvIu73yeTGmsiAzfiICNHi_PmGkgq8fYQ"
"O9lQgyRHCMic8zU7ffWuSoUPRgHsqztLHaCDbYIrNmgrn2taxcXSb57Xm_l-1xBH"
"bZqdMvBziapJXaLJmhUg03lgdsIc_OuJmzt-sytDLVGIuNqpa4dETdhLsI7qis4B"
);
// 1024 chars
BOOST_FOREACH(const String str, clearText) {
String enc = Base64::Encode(str);
String dec = Base64::Decode(enc);
BOOST_CHECK(str == dec);
}
}
BOOST_AUTO_TEST_SUITE_END()