Remove TransitoryString
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 9 Jul 2014 10:15:04 +0000 (12:15 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:59:27 +0000 (14:59 +0200)
Change-Id: Icc38818df54e49f1ca7a8e71b14c4c2f7933eb74

src/CMakeLists.txt
src/manager/CMakeLists.txt
src/manager/dpl/core/include/dpl/TransitoryString.h [deleted file]
src/manager/dpl/core/src/TransitoryString.cpp [deleted file]
src/manager/dpl/db/src/sql_connection.cpp

index ad6e213..4b2f776 100644 (file)
@@ -103,7 +103,6 @@ SET(KEY_MANAGER_CLIENT_SOURCES
     ${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
     )
 
@@ -161,7 +160,6 @@ SET(KEY_MANAGER_CONTROL_CLIENT_SOURCES
     ${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
     )
 
index f753440..274c130 100644 (file)
@@ -38,7 +38,6 @@ SET(COMMON_SOURCES
     ${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
diff --git a/src/manager/dpl/core/include/dpl/TransitoryString.h b/src/manager/dpl/core/include/dpl/TransitoryString.h
deleted file mode 100644 (file)
index 5e06748..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * 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
diff --git a/src/manager/dpl/core/src/TransitoryString.cpp b/src/manager/dpl/core/src/TransitoryString.cpp
deleted file mode 100644 (file)
index ba81a49..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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
index a5478fc..8fcbded 100644 (file)
@@ -28,7 +28,6 @@
 #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>
@@ -704,28 +703,34 @@ const std::string SQLCIPHER_RAW_PREFIX="x'";
 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){
@@ -736,8 +741,8 @@ 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 {
@@ -764,8 +769,8 @@ void SqlConnection::ResetKey(const RawBuffer &rawPassOld,
     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 {