--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <base64_generic.h>
+
+#include <algorithm>
+#include <cctype>
+#include <memory>
+#include <stdexcept>
+
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+
+namespace {
+
+bool isBase64Char(char c)
+{
+ return isalnum(c) || c == '+' || c == '/' || c == '=';
+}
+
+std::unique_ptr<BIO, decltype(&BIO_free)> makeBioPtr(BIO* ptr)
+{
+ if (!ptr)
+ throw std::bad_alloc();
+ return {ptr, &BIO_free};
+}
+
+} // anonymous namespace
+
+int base64EncodeGenericImpl(const void* input, size_t inputLen, void* output)
+{
+ return EVP_EncodeBlock(reinterpret_cast<unsigned char*>(output),
+ reinterpret_cast<const unsigned char*>(input), inputLen);
+}
+
+int base64DecodeGenericImpl(const void* input, size_t inputLen, void* output, size_t outputLen)
+{
+ if (inputLen == 0)
+ return 0;
+ if (!std::all_of(static_cast<const char*>(input), static_cast<const char*>(input) + inputLen, isBase64Char))
+ throw std::invalid_argument("decoding base64 failed, unexpected characters");
+
+ auto inputBio = makeBioPtr(BIO_new_mem_buf(input, inputLen));
+ auto base64Bio = makeBioPtr(BIO_new(BIO_f_base64()));
+ BIO_set_flags(base64Bio.get(), BIO_FLAGS_BASE64_NO_NL);
+ BIO_push(base64Bio.get(), inputBio.get());
+
+ int length = BIO_read(base64Bio.get(), output, outputLen);
+ if (length < 0)
+ throw std::invalid_argument("decoding base64 failed, openssl bio api error");
+
+ return length;
+}
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <algorithm>
+
+#include <symbol-visibility.h>
+
+COMMON_API
+int base64EncodeGenericImpl(const void* input, size_t inputLen, void* output);
+
+COMMON_API
+int base64DecodeGenericImpl(const void* input, size_t inputLen, void* output, size_t outputLen);
+
+template <typename Output, typename Input>
+Output base64EncodeGeneric(const Input& input)
+{
+ // For each 3 input bytes (rounded up), 4 encoded bytes and a null terminator.
+ Output output((input.size() + 2) / 3 * 4 + 1, '\0');
+
+ static_assert(sizeof(input[0]) == 1);
+ static_assert(sizeof(output[0]) == 1);
+ int length = base64EncodeGenericImpl(input.data(), input.size(), &output[0]);
+
+ output.resize(length);
+ return output;
+}
+
+// Due to backwards compatibility, this function contains a few quirks. All '\n' newlines will be
+// removed, other non-base64 characters will throw an exception, and base64 must be implemented
+// using the OpenSSL BIO API. The last fact also implies that this function can accept padding in
+// the middle of text, or too much padding at the end.
+template <typename Output, typename Input>
+Output base64DecodeGeneric(Input input)
+{
+ input.erase(std::remove(input.begin(), input.end(), '\n'), input.end());
+
+ // For each 4 encoded bytes (rounded up), 3 decoded bytes.
+ Output output((input.size() + 3) / 4 * 3, '\0');
+
+ static_assert(sizeof(input[0]) == 1);
+ static_assert(sizeof(output[0]) == 1);
+ size_t length = base64DecodeGenericImpl(input.data(), input.size(), &output[0], output.size());
+
+ output.resize(length);
+ return output;
+}
INCLUDE_DIRECTORIES(
${KEY_MANAGER_DEP_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}
+ ${PROJECT_SOURCE_DIR}/common
${KEY_MANAGER_PATH}/main
${KEY_MANAGER_PATH}/common
${KEY_MANAGER_PATH}/service
*/
if (std::any_of(col.begin(),
col.end(),
- [](const char c){ return isprint(c) + iscntrl(c) == 0; })) {
- Base64Encoder b64enc;
- RawBuffer tmp(col.begin(), col.end());
- b64enc.append(tmp);
- b64enc.finalize();
- col = "b64:";
- RawBuffer encoded = b64enc.get();
- std::copy(encoded.begin(), encoded.end(), std::back_inserter(col));
- }
+ [](const char c){ return isprint(c) + iscntrl(c) == 0; }))
+ col = "b64:" + CKM::base64Encode<std::string>(col);
if (trim && col.size() > MAX_LEN) {
col.resize(MAX_LEN);
INCLUDE_DIRECTORIES(
${CKM_INITIAL_VALUES_DEP_INCLUDE_DIRS}
+ ${PROJECT_SOURCE_DIR}/common
+ ${PROJECT_SOURCE_DIR}/src/manager/common
)
SET(CKM_INITIAL_VALUES_SOURCES
+ ${PROJECT_SOURCE_DIR}/common/base64_generic.cpp
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/base64.cpp
)
LINK_DIRECTORIES(${CKM_INITIAL_VALUES_DEP_LIBRARY_DIRS})
+++ /dev/null
-/*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "base64.h"
-
-#include <algorithm>
-#include <memory>
-
-#include <string.h>
-#include <openssl/bio.h>
-#include <openssl/evp.h>
-#include <openssl/buffer.h>
-
-
-namespace CKM {
-
-Base64Encoder::Base64Encoder() :
- m_b64(0),
- m_bmem(0),
- m_finalized(false)
-{
-}
-
-void Base64Encoder::append(const RawBuffer &data)
-{
- if (m_finalized) {
- throw std::logic_error("Already finalized");
- }
-
- if (!m_b64)
- reset();
-
- BIO_write(m_b64, data.data(), data.size());
-}
-
-void Base64Encoder::finalize()
-{
- if (m_finalized) {
- throw std::logic_error("Already finalized.");
- }
-
- m_finalized = true;
- (void)BIO_flush(m_b64);
-}
-
-RawBuffer Base64Encoder::get()
-{
- if (!m_finalized) {
- throw std::logic_error("Not finalized");
- }
-
- BUF_MEM *bptr = nullptr;
- BIO_get_mem_ptr(m_b64, &bptr);
-
- if (!bptr) {
- throw std::logic_error("Bio internal error");
- }
-
- if (bptr->length > 0)
- return RawBuffer(bptr->data, bptr->data + bptr->length);
-
- return RawBuffer();
-}
-
-void Base64Encoder::reset()
-{
- m_finalized = false;
- BIO_free_all(m_b64);
- m_b64 = BIO_new(BIO_f_base64());
- m_bmem = BIO_new(BIO_s_mem());
-
- if (!m_b64 || !m_bmem) {
- throw std::logic_error("Error during allocation memory in BIO");
- }
-
- BIO_set_flags(m_b64, BIO_FLAGS_BASE64_NO_NL);
- m_b64 = BIO_push(m_b64, m_bmem);
-}
-
-Base64Encoder::~Base64Encoder()
-{
- BIO_free_all(m_b64);
-}
-
-Base64Decoder::Base64Decoder() :
- m_finalized(false)
-{
-}
-
-void Base64Decoder::append(const RawBuffer &data)
-{
- if (m_finalized) {
- throw std::logic_error("Already finalized.");
- }
-
- std::copy(data.begin(), data.end(), std::back_inserter(m_input));
-}
-
-static bool whiteCharacter(char a)
-{
- return a == '\n';
-}
-
-bool Base64Decoder::finalize()
-{
- if (m_finalized) {
- throw std::logic_error("Already finalized.");
- }
-
- m_finalized = true;
-
- m_input.erase(std::remove_if(m_input.begin(),
- m_input.end(),
- whiteCharacter),
- m_input.end());
-
- for (size_t i = 0; i < m_input.size(); ++i) {
- if (isalnum(m_input[i])
- || m_input[i] == '+'
- || m_input[i] == '/'
- || m_input[i] == '=')
- continue;
-
- return false;
- }
-
- BIO *b64, *bmem;
- size_t len = m_input.size();
-
- RawBuffer buffer(len);
-
- if (!buffer.data()) {
- throw std::logic_error("Error in malloc.");
- }
-
- memset(buffer.data(), 0, buffer.size());
- b64 = BIO_new(BIO_f_base64());
-
- if (!b64) {
- throw std::logic_error("Couldn't create BIO object.");
- }
-
- BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
- RawBuffer tmp(m_input);
- m_input.clear();
-
- bmem = BIO_new_mem_buf(tmp.data(), len);
-
- if (!bmem) {
- BIO_free(b64);
- throw std::logic_error("Internal error in BIO");
- }
-
- bmem = BIO_push(b64, bmem);
-
- if (!bmem) {
- BIO_free(b64);
- throw std::logic_error("Internal error in BIO");
- }
-
- int readlen = BIO_read(bmem, buffer.data(), buffer.size());
- m_output.clear();
-
- bool status = true;
-
- if (readlen > 0) {
- buffer.resize(readlen);
- m_output = std::move(buffer);
- } else {
- status = false;
- }
-
- BIO_free_all(bmem);
- return status;
-}
-
-RawBuffer Base64Decoder::get() const
-{
- if (!m_finalized) {
- throw std::logic_error("Not finalized");
- }
-
- return m_output;
-}
-
-void Base64Decoder::reset()
-{
- m_finalized = false;
- m_input.clear();
- m_output.clear();
-}
-
-} // namespace CKM
+++ /dev/null
-/*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#ifndef _BASE64_H_
-#define _BASE64_H_
-
-#include <string>
-#include <vector>
-
-typedef std::vector<unsigned char> RawBuffer;
-
-struct bio_st;
-typedef bio_st BIO;
-
-namespace CKM {
-
-class Base64Encoder {
-public:
- Base64Encoder();
- void append(const RawBuffer &data);
- void finalize();
- RawBuffer get();
- void reset();
- ~Base64Encoder();
-
-private:
- BIO *m_b64;
- BIO *m_bmem;
- bool m_finalized;
-};
-
-class Base64Decoder {
-public:
- Base64Decoder();
- void append(const RawBuffer &data);
-
- /*
- * Function will return false when BIO_read fails
- * (for example: when string was not in base64 format).
- */
- bool finalize();
- RawBuffer get() const;
- void reset();
- ~Base64Decoder() {}
-
-private:
- RawBuffer m_input;
- RawBuffer m_output;
- bool m_finalized;
-};
-} // namespace CKM
-
-#endif
/*
- * Copyright (c) 2018-2019 Samsung Electronics Co., Ltd. All rights reserved
+ * Copyright (c) 2018-2020 Samsung Electronics Co., Ltd. All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <libxml/tree.h>
#include <libxml/parser.h>
-#include "base64.h"
+#include <base64_generic.h>
typedef std::vector<unsigned char> Buffer;
typedef std::istreambuf_iterator<char> InputIterator;
std::string base64(const Buffer& data)
{
try {
- CKM::Base64Encoder encoder;
- encoder.append(data);
- encoder.finalize();
- auto result = encoder.get();
- return std::string(result.begin(), result.end());
+ return base64EncodeGeneric<std::string>(data);
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
INCLUDE_DIRECTORIES(
${KEY_MANAGER_SRC_PATH}/include
+ ${PROJECT_SOURCE_DIR}/common
${KEY_MANAGER_PATH}/main
${KEY_MANAGER_PATH}/common
${KEY_MANAGER_PATH}/service
SET(COMMON_PATH ${PROJECT_SOURCE_DIR}/src/manager)
SET(COMMON_SOURCES
+ ${PROJECT_SOURCE_DIR}/common/base64_generic.cpp
${COMMON_PATH}/common/algo-param.cpp
- ${COMMON_PATH}/common/base64.cpp
${COMMON_PATH}/common/data-type.cpp
${COMMON_PATH}/common/openssl-error-handler.cpp
${COMMON_PATH}/common/exception.cpp
+++ /dev/null
-/*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <algorithm>
-#include <memory>
-
-#include <string.h>
-#include <openssl/bio.h>
-#include <openssl/evp.h>
-#include <openssl/buffer.h>
-
-#include <dpl/log/log.h>
-
-#include <exception.h>
-#include <base64.h>
-
-namespace CKM {
-
-Base64Encoder::Base64Encoder() :
- m_b64(0),
- m_bmem(0),
- m_finalized(false)
-{
-}
-
-void Base64Encoder::append(const RawBuffer &data)
-{
- if (m_finalized) {
- ThrowErr(Exc::InternalError, "Already finalized");
- }
-
- if (!m_b64)
- reset();
-
- BIO_write(m_b64, data.data(), data.size());
-}
-
-void Base64Encoder::finalize()
-{
- if (m_finalized) {
- ThrowErr(Exc::InternalError, "Already finalized.");
- }
-
- m_finalized = true;
- (void)BIO_flush(m_b64);
-}
-
-RawBuffer Base64Encoder::get()
-{
- if (!m_finalized) {
- ThrowErr(Exc::InternalError, "Not finalized");
- }
-
- BUF_MEM *bptr = nullptr;
- BIO_get_mem_ptr(m_b64, &bptr);
-
- if (!bptr) {
- ThrowErr(Exc::InternalError, "Bio internal error");
- }
-
- if (bptr->length > 0)
- return RawBuffer(bptr->data, bptr->data + bptr->length);
-
- return RawBuffer();
-}
-
-void Base64Encoder::reset()
-{
- m_finalized = false;
- BIO_free_all(m_b64);
- m_b64 = BIO_new(BIO_f_base64());
- m_bmem = BIO_new(BIO_s_mem());
-
- if (!m_b64 || !m_bmem) {
- ThrowErr(Exc::InternalError, "Error during allocation memory in BIO");
- }
-
- BIO_set_flags(m_b64, BIO_FLAGS_BASE64_NO_NL);
- m_b64 = BIO_push(m_b64, m_bmem);
-}
-
-Base64Encoder::~Base64Encoder()
-{
- BIO_free_all(m_b64);
-}
-
-Base64Decoder::Base64Decoder() :
- m_finalized(false)
-{
-}
-
-void Base64Decoder::append(const RawBuffer &data)
-{
- if (m_finalized) {
- ThrowErr(Exc::InternalError, "Already finalized.");
- }
-
- std::copy(data.begin(), data.end(), std::back_inserter(m_input));
-}
-
-static bool whiteCharacter(char a)
-{
- return a == '\n';
-}
-
-bool Base64Decoder::finalize()
-{
- if (m_finalized) {
- ThrowErr(Exc::InternalError, "Already finalized.");
- }
-
- m_finalized = true;
-
- m_input.erase(std::remove_if(m_input.begin(),
- m_input.end(),
- whiteCharacter),
- m_input.end());
-
- for (size_t i = 0; i < m_input.size(); ++i) {
- if (isalnum(m_input[i])
- || m_input[i] == '+'
- || m_input[i] == '/'
- || m_input[i] == '=')
- continue;
-
- LogError("Base64 input contains illegal chars: " << m_input[i]);
- return false;
- }
-
- BIO *b64, *bmem;
- size_t len = m_input.size();
-
- RawBuffer buffer(len);
-
- if (!buffer.data()) {
- ThrowErr(Exc::InternalError, "Error in malloc.");
- }
-
- memset(buffer.data(), 0, buffer.size());
- b64 = BIO_new(BIO_f_base64());
-
- if (!b64) {
- ThrowErr(Exc::InternalError, "Couldn't create BIO object.");
- }
-
- BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
- RawBuffer tmp(m_input);
- m_input.clear();
-
- bmem = BIO_new_mem_buf(tmp.data(), len);
-
- if (!bmem) {
- BIO_free(b64);
- ThrowErr(Exc::InternalError, "Internal error in BIO");
- }
-
- bmem = BIO_push(b64, bmem);
-
- if (!bmem) {
- BIO_free(b64);
- ThrowErr(Exc::InternalError, "Internal error in BIO");
- }
-
- int readlen = BIO_read(bmem, buffer.data(), buffer.size());
- m_output.clear();
-
- bool status = true;
-
- if (readlen > 0) {
- buffer.resize(readlen);
- m_output = std::move(buffer);
- } else {
- status = false;
- }
-
- BIO_free_all(bmem);
- return status;
-}
-
-RawBuffer Base64Decoder::get() const
-{
- if (!m_finalized) {
- ThrowErr(Exc::InternalError, "Not finalized");
- }
-
- return m_output;
-}
-
-void Base64Decoder::reset()
-{
- m_finalized = false;
- m_input.clear();
- m_output.clear();
-}
-
-} // namespace CKM
/*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2011 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#ifndef _BASE64_H_
-#define _BASE64_H_
-#include <string>
+#pragma once
-#include <ckm/ckm-type.h>
-#include <noncopyable.h>
-#include <symbol-visibility.h>
+#include <base64_generic.h>
+#include <exception.h>
-struct bio_st;
-typedef bio_st BIO;
+#include <stdexcept>
namespace CKM {
-class COMMON_API Base64Encoder {
-public:
- NONCOPYABLE(Base64Encoder)
+template <typename Output, typename Input>
+Output base64Encode(const Input& input)
+{
+ return base64EncodeGeneric<Output, Input>(input);
+}
+
+template <typename Output, typename Input>
+Output base64Decode(const Input& input)
+{
+ try {
+ return base64DecodeGeneric<Output, Input>(input);
+ } catch (const std::invalid_argument& e) {
+ ThrowErr(Exc::InternalError, e.what());
+ }
+}
- Base64Encoder();
- void append(const RawBuffer &data);
- void finalize();
- RawBuffer get();
- void reset();
- ~Base64Encoder();
-
-private:
- BIO *m_b64;
- BIO *m_bmem;
- bool m_finalized;
-};
-
-class COMMON_API Base64Decoder {
-public:
- NONCOPYABLE(Base64Decoder)
-
- Base64Decoder();
- void append(const RawBuffer &data);
-
- /*
- * Function will return false when BIO_read fails
- * (for example: when string was not in base64 format).
- */
- bool finalize();
- RawBuffer get() const;
- void reset();
- ~Base64Decoder() {}
-
-private:
- RawBuffer m_input;
- RawBuffer m_output;
- bool m_finalized;
-};
} // namespace CKM
-
-#endif
* @brief Certificate implementation.
*/
#include <cassert>
+#include <stdexcept>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <key-impl.h>
#include <certificate-impl.h>
-#include <base64.h>
+#include <base64_generic.h>
+#include <exception.h>
namespace CKM {
LogDebug("Certificate to parse. Size: " << der.size());
if (DataFormat::FORM_DER_BASE64 == format) {
- Base64Decoder base64;
- base64.reset();
- base64.append(der);
- base64.finalize();
- auto tmp = base64.get();
+ RawBuffer tmp;
+ try {
+ tmp = base64DecodeGeneric<RawBuffer>(der);
+ } catch (const std::invalid_argument&) {
+ LogError("Certificate in FORM_DER_BASE64 format is not valid base64.");
+ }
auto ptr = reinterpret_cast<const unsigned char *>(tmp.data());
auto size = static_cast<int>(tmp.size());
m_x509 = d2i_X509(nullptr, &ptr, size);
/*
- * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <string>
#include <algorithm>
#include <cctype>
+#include <stdexcept>
#include <BufferHandler.h>
#include <xml-utils.h>
-#include <base64.h>
+#include <base64_generic.h>
+#include <dpl/log/log.h>
+#include <exception.h>
namespace {
const char *const XML_ATTR_IV = "IV";
{
if (attr.find(XML_ATTR_IV) != attr.end()) {
std::string IVstring = attr.at(XML_ATTR_IV);
- Base64Decoder base64;
- base64.reset();
- base64.append(RawBuffer(IVstring.begin(), IVstring.end()));
- base64.finalize();
- m_encryptionParams.iv = base64.get();
+ try {
+ m_encryptionParams.iv = base64DecodeGeneric<RawBuffer>(IVstring);
+ } catch (const std::invalid_argument&) {
+ LogError("Encryption param IV is not valid base64.");
+ }
}
if (attr.find(XML_ATTR_TAG) != attr.end()) {
std::string tag = attr.at(XML_ATTR_TAG);
- Base64Decoder base64;
- base64.reset();
- base64.append(RawBuffer(tag.begin(), tag.end()));
- base64.finalize();
- m_encryptionParams.tag = base64.get();
+ try {
+ m_encryptionParams.tag = base64DecodeGeneric<RawBuffer>(tag);
+ } catch (const std::invalid_argument&) {
+ LogError("Encryption param tag is not valid base64.");
+ }
}
}
case ENCRYPTED: {
std::string trimmed = XML::trimEachLine(std::string(m_data.begin(),
m_data.end()));
- Base64Decoder base64;
- base64.reset();
- base64.append(RawBuffer(trimmed.begin(), trimmed.end()));
- base64.finalize();
- m_data = base64.get();
+ try {
+ m_data = base64DecodeGeneric<RawBuffer>(trimmed);
+ } catch (const std::invalid_argument&) {
+ LogError("Data is not valid base64.");
+ m_data.clear();
+ }
break;
}
/*
- * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2000 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
#include <iostream>
+#include <stdexcept>
#include <InitialValuesFile.h>
#include <InitialValueHandler.h>
#include <BufferHandler.h>
#include <DataHandler.h>
#include <EncodingType.h>
#include <sw-backend/obj.h>
+#include <base64_generic.h>
#include <dpl/log/log.h>
namespace {
{
std::string trimmed = XML::trimEachLine(std::string(m_encryptedKey.begin(),
m_encryptedKey.end()));
- Base64Decoder base64;
- base64.reset();
- base64.append(RawBuffer(trimmed.begin(), trimmed.end()));
- base64.finalize();
- m_encryptedKey = base64.get();
+ try {
+ m_encryptedKey = base64DecodeGeneric<RawBuffer>(trimmed);
+ } catch (const std::invalid_argument&) {
+ LogError("Encrypted key is not valid base64.");
+ m_encryptedKey.clear();
+ }
};
CKM::RawBuffer InitialValuesFile::EncryptionKeyHandler::getEncryptedKey() const
void CryptoLogic::encBase64(RawBuffer &data)
{
- Base64Encoder benc;
- RawBuffer encdata;
-
- benc.append(data);
- benc.finalize();
- encdata = benc.get();
+ RawBuffer encdata = base64Encode<RawBuffer>(data);
if (encdata.size() == 0)
- ThrowErr(Exc::InternalError, "Base64Encoder returned empty data.");
+ ThrowErr(Exc::InternalError, "base64Encode returned empty data.");
data = std::move(encdata);
}
void CryptoLogic::decBase64(RawBuffer &data)
{
- Base64Decoder bdec;
- RawBuffer decdata;
-
- bdec.reset();
- bdec.append(data);
-
- if (!bdec.finalize())
- ThrowErr(Exc::InternalError, "Failed in Base64Decoder.finalize.");
-
- decdata = bdec.get();
+ RawBuffer decdata = base64Decode<RawBuffer>(data);
if (decdata.size() == 0)
- ThrowErr(Exc::InternalError, "Base64Decoder returned empty data.");
+ ThrowErr(Exc::InternalError, "base64Decode returned empty data.");
data = std::move(decdata);
}
LINK_DIRECTORIES(${KEY_MANAGER_DEP_LIBRARY_DIRS})
SET(UNIT_TESTS_SOURCES
+ ${PROJECT_SOURCE_DIR}/common/base64_generic.cpp
${PROJECT_SOURCE_DIR}/common/colour_log_formatter.cpp
${PROJECT_SOURCE_DIR}/common/DBFixture.cpp
${PROJECT_SOURCE_DIR}/common/test_common.cpp
${MANAGER_PATH}/client-async/descriptor-set.cpp
${MANAGER_PATH}/common/algo-param.cpp
- ${MANAGER_PATH}/common/base64.cpp
${MANAGER_PATH}/common/certificate-impl.cpp
${MANAGER_PATH}/common/ckm-zero-memory.cpp
${MANAGER_PATH}/common/data-type.cpp
#include <ckm/ckm-type.h>
#include <exception.h>
-using CKM::Base64Encoder;
-using CKM::Base64Decoder;
+using CKM::base64Encode;
+using CKM::base64Decode;
using CKM::RawBuffer;
namespace {
POSITIVE_TEST_CASE(ENCODE_DECODE)
{
/* try encode */
- Base64Encoder encoder;
- BOOST_REQUIRE_NO_THROW(encoder.append(rawbuf));
- BOOST_REQUIRE_NO_THROW(encoder.finalize());
-
RawBuffer encdata;
- BOOST_REQUIRE_NO_THROW(encdata = encoder.get());
- BOOST_REQUIRE_NO_THROW(encoder.reset());
+ BOOST_REQUIRE_NO_THROW(encdata = base64Encode<RawBuffer>(rawbuf));
/* try decode */
- Base64Decoder decoder;
- BOOST_REQUIRE_NO_THROW(decoder.append(encdata));
- BOOST_REQUIRE_NO_THROW(decoder.finalize());
-
RawBuffer decdata;
- BOOST_REQUIRE_NO_THROW(decdata = decoder.get());
- BOOST_REQUIRE_NO_THROW(decoder.reset());
+ BOOST_REQUIRE_NO_THROW(decdata = base64Decode<RawBuffer>(encdata));
/* compare with orig data */
BOOST_REQUIRE_MESSAGE(
"Original data and encoded-decoded data is different!");
}
-NEGATIVE_TEST_CASE(THROW_SOMETHING)
-{
- /* encode data */
- Base64Encoder encoder;
- BOOST_REQUIRE_THROW(encoder.get(), CKM::Exc::InternalError);
-
- BOOST_REQUIRE_NO_THROW(encoder.append(rawbuf));
- BOOST_REQUIRE_NO_THROW(encoder.finalize());
-
- BOOST_REQUIRE_THROW(encoder.append(rawbuf),
- CKM::Exc::InternalError);
- BOOST_REQUIRE_THROW(encoder.finalize(),
- CKM::Exc::InternalError);
-
- RawBuffer encdata;
- BOOST_REQUIRE_NO_THROW(encdata = encoder.get());
-
- /* decode data */
- Base64Decoder decoder;
- BOOST_REQUIRE_THROW(decoder.get(), CKM::Exc::InternalError);
-
- BOOST_REQUIRE_NO_THROW(decoder.append(encdata));
- BOOST_REQUIRE_NO_THROW(decoder.finalize());
-
- BOOST_REQUIRE_THROW(decoder.append(encdata),
- CKM::Exc::InternalError);
- BOOST_REQUIRE_THROW(decoder.finalize(),
- CKM::Exc::InternalError);
-
- RawBuffer decdata;
- BOOST_REQUIRE_NO_THROW(decdata = decoder.get());
-}
-
NEGATIVE_TEST_CASE(ILLEGAL_DATA)
{
- Base64Decoder decoder;
- BOOST_REQUIRE_NO_THROW(decoder.append(rawbuf));
- BOOST_REQUIRE(!decoder.finalize());
+ BOOST_REQUIRE_THROW(base64Decode<RawBuffer>(rawbuf), CKM::Exc::InternalError);
}
BOOST_AUTO_TEST_SUITE_END()
void changeBase64(RawBuffer& data)
{
- auto b64 = [](auto&& coder, RawBuffer& data){
- coder.append(data);
- coder.finalize();
- data = coder.get();
-
- BOOST_REQUIRE(!data.empty());
- };
- b64(Base64Decoder(), data);
+ data = base64Decode<RawBuffer>(data);
+ BOOST_REQUIRE(!data.empty());
++data[0];
- b64(Base64Encoder(), data);
+ data = base64Encode<RawBuffer>(data);
+ BOOST_REQUIRE(!data.empty());
}
} // namespace anonymous