${KEY_MANAGER_PATH}/dpl/core/src/serialization.cpp
${KEY_MANAGER_PATH}/dpl/core/src/singleton.cpp
${KEY_MANAGER_PATH}/dpl/core/src/string.cpp
- ${KEY_MANAGER_PATH}/dpl/core/src/TransitoryString.cpp
${KEY_MANAGER_PATH}/dpl/core/src/errno_string.cpp
)
${KEY_MANAGER_PATH}/dpl/core/src/serialization.cpp
${KEY_MANAGER_PATH}/dpl/core/src/singleton.cpp
${KEY_MANAGER_PATH}/dpl/core/src/string.cpp
- ${KEY_MANAGER_PATH}/dpl/core/src/TransitoryString.cpp
${KEY_MANAGER_PATH}/dpl/core/src/errno_string.cpp
)
${COMMON_PATH}/dpl/core/src/serialization.cpp
${COMMON_PATH}/dpl/core/src/singleton.cpp
${COMMON_PATH}/dpl/core/src/string.cpp
- ${COMMON_PATH}/dpl/core/src/TransitoryString.cpp
${COMMON_PATH}/dpl/core/src/errno_string.cpp
${COMMON_PATH}/dpl/db/src/sql_connection.cpp
${COMMON_PATH}/dpl/db/src/naive_synchronization_object.cpp
+++ /dev/null
-/*
- * Copyright (c) 2014 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.
- */
-/*
- * @file TransitoryString.h
- * @author Zofia Abramowska (z.abramowska@samsung.com)
- * @version 1.0
- * @brief Header of self wiping out string for sensitive data
- */
-#ifndef TRANSITORY_STRING_H
-#define TRANSITORY_STRING_H
-
-#include <cstring>
-
-namespace CKM {
-class TransitoryString {
- public:
- static const std::size_t PREFERRED_SIZE = 64;
-
- static std::size_t getPreferredSize() {
- return PREFERRED_SIZE;
- }
-
- TransitoryString() = delete;
- TransitoryString(const TransitoryString&) = delete;
- TransitoryString(TransitoryString&) = delete;
- TransitoryString(TransitoryString&& tString);
- TransitoryString(char c, std::size_t length);
- ~TransitoryString();
-
- TransitoryString& operator=(const TransitoryString& other) = delete;
- TransitoryString& operator=(TransitoryString&& other);
-
- char& operator[](std::size_t index) {
- return m_tString[index];
- }
-
- const char* c_str() const {
- return m_tString;
- }
-
- const char* data() const {
- return m_tString;
- }
-
- std::size_t length() const {
- return m_length;
- }
- private:
- char* m_tString;
- std::size_t m_length;
-
- void wipeOut();
-
-};
-} // CKM
-#endif // TRANSITORY_STRING_H
+++ /dev/null
-/*
- * Copyright (c) 2014 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.
- */
-/*
- * @file TransitoryString.h
- * @author Zofia Abramowska (z.abramowska@samsung.com)
- * @version 1.0
- * @brief Implementation of self wiping out string for sensitive data
- */
-#include <dpl/TransitoryString.h>
-#include <cstring>
-#include <dpl/assert.h>
-namespace CKM {
-TransitoryString::TransitoryString(char c, std::size_t length){
- m_length = length;
- m_tString = new char[m_length+1];
- memset(m_tString, c, m_length);
- m_tString[m_length] = '\0';
-}
-
-TransitoryString::TransitoryString(TransitoryString&& other)
- : m_tString(other.m_tString),
- m_length(other.m_length)
-{
- other.m_length = 0;
- other.m_tString = NULL;
-}
-
-TransitoryString::~TransitoryString(){
- if(m_tString != NULL) {
- wipeOut();
- delete[] m_tString;
- m_length = 0;
- }
-}
-
-TransitoryString& TransitoryString::operator=(TransitoryString&& other) {
- if (this != &other) {
- wipeOut();
- delete[] m_tString;
-
- m_tString = other.m_tString;
- m_length = other.m_length;
-
- other.m_tString = NULL;
- other.m_length = 0;
- }
- return *this;
-}
-
-void TransitoryString::wipeOut(){
- for(std::size_t i = 0; i < m_length; i++)
- m_tString[i] = '\0';
- AssertMsg(strlen(m_tString) == 0, "Wiping out string didn't work!");
- for(std::size_t i = 0; i < m_length; i++)
- AssertMsg(m_tString[i] == '\0', "Wiping out string didn't work!");
-}
-} // CKM
#include <dpl/noncopyable.h>
#include <dpl/assert.h>
#include <dpl/scoped_ptr.h>
-#include <dpl/TransitoryString.h>
#include <db-util.h>
#include <unistd.h>
#include <cstdio>
const std::string SQLCIPHER_RAW_SUFIX="'";
const std::size_t SQLCIPHER_RAW_DATA_SIZE = 32;
-void rawToHexString(TransitoryString& str,
- std::size_t offset,
- const RawBuffer &raw) {
- for (std::size_t i = 0; i < raw.size(); i++)
- sprintf(&str[offset + i*2], "%02X", raw[i]);
+RawBuffer rawToHexString(const RawBuffer &raw)
+{
+ RawBuffer output;
+ for(auto &e: raw) {
+ char result[3];
+ snprintf(result, sizeof(result), "%02X", static_cast<unsigned int>(e));
+ output.push_back(static_cast<unsigned char>(result[0]));
+ output.push_back(static_cast<unsigned char>(result[1]));
+ }
+ return output;
}
-TransitoryString createHexPass(const RawBuffer &rawPass){
- TransitoryString pass = TransitoryString('0', SQLCIPHER_RAW_PREFIX.length() +
- //We are required to pass 64byte
- //long hex password made out of
- //32byte raw binary data
- rawPass.size() * 2 +
- SQLCIPHER_RAW_SUFIX.length());
- for(std::size_t i = 0; i < SQLCIPHER_RAW_PREFIX.size(); i++)
- pass[i] = SQLCIPHER_RAW_PREFIX[i];
- rawToHexString(pass, SQLCIPHER_RAW_PREFIX.size(), rawPass);
- for(std::size_t i = 0; i < SQLCIPHER_RAW_SUFIX.size(); i++)
- pass[i + SQLCIPHER_RAW_PREFIX.size() + rawPass.size() * 2]
- = SQLCIPHER_RAW_SUFIX[i];
- return pass;
+RawBuffer createHexPass(const RawBuffer &rawPass){
+ // We are required to pass 64byte long hex password made out of 32byte raw
+ // binary data
+ RawBuffer output;
+ std::copy(SQLCIPHER_RAW_PREFIX.begin(), SQLCIPHER_RAW_PREFIX.end(),
+ std::back_inserter(output));
+
+ RawBuffer password = rawToHexString(rawPass);
+
+ std::copy(password.begin(), password.end(),
+ std::back_inserter(output));
+
+ std::copy(SQLCIPHER_RAW_SUFIX.begin(), SQLCIPHER_RAW_SUFIX.end(),
+ std::back_inserter(output));
+ return output;
}
void SqlConnection::SetKey(const RawBuffer &rawPass){
if (rawPass.size() != SQLCIPHER_RAW_DATA_SIZE)
ThrowMsg(Exception::InvalidArguments,
"Binary data for raw password should be 32 bytes long.");
- TransitoryString pass = createHexPass(rawPass);
- int result = sqlcipher3_key(m_connection, pass.c_str(), pass.length());
+ RawBuffer pass = createHexPass(rawPass);
+ int result = sqlcipher3_key(m_connection, pass.data(), pass.size());
if (result == SQLCIPHER_OK) {
LogPedantic("Set key on DB");
} else {
if (!m_isKeySet)
SetKey(rawPassOld);
- TransitoryString pass = createHexPass(rawPassNew);
- int result = sqlcipher3_rekey(m_connection, pass.c_str(), pass.length());
+ RawBuffer pass = createHexPass(rawPassNew);
+ int result = sqlcipher3_rekey(m_connection, pass.data(), pass.size());
if (result == SQLCIPHER_OK) {
LogPedantic("Reset key on DB");
} else {