/*
- * Copyright (c) 2014 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014 - 2019 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 <exception.h>
#include <key-provider.h>
#include <dpl/log/log.h>
+#include <string.h>
namespace {
WrappedKeyAndInfoContainer::WrappedKeyAndInfoContainer()
{
- wrappedKeyAndInfo = new WrappedKeyAndInfo;
- memset(wrappedKeyAndInfo, 0, sizeof(WrappedKeyAndInfo));
+ memset(&wrappedKeyAndInfo, 0, sizeof(WrappedKeyAndInfo));
}
WrappedKeyAndInfoContainer::WrappedKeyAndInfoContainer(const unsigned char
*data)
{
- wrappedKeyAndInfo = new WrappedKeyAndInfo;
- memcpy(wrappedKeyAndInfo, data, sizeof(WrappedKeyAndInfo));
+ memcpy(&wrappedKeyAndInfo, data, sizeof(WrappedKeyAndInfo));
+
+ if (wrappedKeyAndInfo.keyInfo.keyLength > sizeof(wrappedKeyAndInfo.wrappedKey)) {
+ ThrowErr(Exc::InternalError,
+ "Wrapped key info is corrupted. Key length exceeds the size of the key buffer.");
+ }
+
+ size_t maxlen = sizeof(wrappedKeyAndInfo.keyInfo.client);
+ if (strnlen(wrappedKeyAndInfo.keyInfo.client, maxlen) == maxlen) {
+ ThrowErr(Exc::InternalError,
+ "Wrapped key info is corrupted. Client id is not NULL terminated.");
+ }
}
WrappedKeyAndInfo &WrappedKeyAndInfoContainer::getWrappedKeyAndInfo()
{
- return *wrappedKeyAndInfo;
+ return wrappedKeyAndInfo;
}
void WrappedKeyAndInfoContainer::setKeyInfoKeyLength(const unsigned int length)
{
- wrappedKeyAndInfo->keyInfo.keyLength = length;
+ wrappedKeyAndInfo.keyInfo.keyLength = length;
}
void WrappedKeyAndInfoContainer::setKeyInfoClient(const std::string resized_client)
{
- if (resized_client.size() >= sizeof(wrappedKeyAndInfo->keyInfo.client)) {
+ if (resized_client.size() >= sizeof(wrappedKeyAndInfo.keyInfo.client)) {
ThrowErr(Exc::InternalError, "Client name too long");
}
- strcpy(wrappedKeyAndInfo->keyInfo.client, resized_client.c_str());
+ strcpy(wrappedKeyAndInfo.keyInfo.client, resized_client.c_str());
}
void WrappedKeyAndInfoContainer::setKeyInfoSalt(const unsigned char *salt,
const int size)
{
- memcpy(wrappedKeyAndInfo->keyInfo.salt, salt, size);
+ memcpy(wrappedKeyAndInfo.keyInfo.salt, salt, size);
}
void WrappedKeyAndInfoContainer::setKeyInfo(const KeyComponentsInfo
*keyComponentsInfo)
{
- memcpy(&(wrappedKeyAndInfo->keyInfo), keyComponentsInfo,
+ memcpy(&(wrappedKeyAndInfo.keyInfo), keyComponentsInfo,
sizeof(KeyComponentsInfo));
}
WrappedKeyAndInfoContainer::~WrappedKeyAndInfoContainer()
{
- delete wrappedKeyAndInfo;
}
KeyAndInfoContainer::KeyAndInfoContainer()
{
- keyAndInfo = new KeyAndInfo;
- memset(keyAndInfo, 0, sizeof(KeyAndInfo));
+ memset(&keyAndInfo, 0, sizeof(KeyAndInfo));
}
KeyAndInfoContainer::KeyAndInfoContainer(const unsigned char *data)
{
- keyAndInfo = new KeyAndInfo;
- memcpy(keyAndInfo, data, sizeof(KeyAndInfo));
+ memcpy(&keyAndInfo, data, sizeof(KeyAndInfo));
}
KeyAndInfo &KeyAndInfoContainer::getKeyAndInfo()
{
- return *keyAndInfo;
+ return keyAndInfo;
}
void KeyAndInfoContainer::setKeyInfoKeyLength(unsigned int length)
{
- keyAndInfo->keyInfo.keyLength = length;
+ keyAndInfo.keyInfo.keyLength = length;
}
void KeyAndInfoContainer::setKeyInfo(const KeyComponentsInfo *keyComponentsInfo)
{
- memcpy(&(keyAndInfo->keyInfo), keyComponentsInfo, sizeof(KeyComponentsInfo));
+ memcpy(&(keyAndInfo.keyInfo), keyComponentsInfo, sizeof(KeyComponentsInfo));
}
KeyAndInfoContainer::~KeyAndInfoContainer()
{
// overwrite key
- char *ptr = reinterpret_cast<char *>(keyAndInfo);
+ char *ptr = reinterpret_cast<char *>(&keyAndInfo);
memset(ptr, 0, sizeof(KeyAndInfo));
// verification
for (size_t size = 0; size < sizeof(KeyAndInfo); ++size) {
if (ptr[size])
- LogError("Write momory error! Memory used by key was not owerwritten.");
+ LogError("Write memory error! Memory used by key was not owerwritten.");
}
-
- delete keyAndInfo;
}
KeyProvider::KeyProvider() :
/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2016 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
*
* Contact: Kyungwook Tak <k.tak@samsung.com>
*
wrappedContainer.getWrappedKeyAndInfo().keyInfo.client,
wrappedContainer2.getWrappedKeyAndInfo().keyInfo.client,
sizeof(wrappedContainer.getWrappedKeyAndInfo().keyInfo.client)) == 0);
+
+ CKM::WrappedKeyAndInfo wrapped3;
+ wrapped3.keyInfo.keyLength = MAX_WRAPPED_KEY_SIZE;
+ BOOST_REQUIRE_NO_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3(
+ reinterpret_cast<unsigned char*>(&wrapped3)));
+
+ wrapped3.keyInfo.keyLength++;
+ BOOST_REQUIRE_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3(
+ reinterpret_cast<unsigned char*>(&wrapped3)),
+ CKM::Exc::InternalError);
+
+ // missing NULL termination in wrapped4.keyInfo.client
+ CKM::WrappedKeyAndInfo wrapped4;
+ memset(&wrapped4, 0x01, sizeof(CKM::WrappedKeyAndInfo));
+ BOOST_REQUIRE_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3(
+ reinterpret_cast<unsigned char*>(&wrapped4)),
+ CKM::Exc::InternalError);
}
BOOST_AUTO_TEST_CASE(container)