Add FileSystem class.
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Wed, 4 Jun 2014 16:29:01 +0000 (18:29 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:57:08 +0000 (14:57 +0200)
Change-Id: I537bbf2ce8667a3920c2824c5ac0933744ee522f

src/CMakeLists.txt
src/manager/service/FileSystem.cpp [new file with mode: 0644]
src/manager/service/FileSystem.h [new file with mode: 0644]

index 043d00c..20fe78f 100644 (file)
@@ -21,6 +21,7 @@ SET(KEY_MANAGER_SOURCES
     ${KEY_MANAGER_PATH}/service/ocsp.cpp
     ${KEY_MANAGER_PATH}/service/DBCryptoModule.cpp
     ${KEY_MANAGER_PATH}/service/CryptoService.cpp
+    ${KEY_MANAGER_PATH}/service/FileSystem.cpp
     )
 
 SET_SOURCE_FILES_PROPERTIES(
diff --git a/src/manager/service/FileSystem.cpp b/src/manager/service/FileSystem.cpp
new file mode 100644 (file)
index 0000000..d1c1bee
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ *  Copyright (c) 2000 - 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        FileSystem.cpp
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Sample service implementation.
+ */
+#include <sys/types.h>
+#include <pwd.h>
+
+#include <string>
+#include <sstream>
+#include <fstream>
+
+#include <FileSystem.h>
+
+namespace {
+
+static const std::string CKM_DATA_PATH = "/opt/data/ckm/";
+static const std::string CKM_KEY_PREFIX = "key-";
+static const std::string CKM_DB_PREFIX = "db-";
+
+} // namespace anonymous
+
+namespace CKM {
+
+FileSystem::FileSystem(int uid)
+  : m_uid(uid)
+{
+    passwd *pass = getpwuid(uid);
+    if (pass->pw_name)
+        m_user = pass->pw_name;
+    else
+        m_user = std::to_string(uid);
+}
+
+std::string FileSystem::getDBPath() const
+{
+    std::stringstream ss;
+    ss << CKM_DATA_PATH << CKM_DB_PREFIX << m_user;
+    return ss.str();
+}
+
+RawBuffer FileSystem::getDomainKEK() const
+{
+    std::stringstream ss;
+    ss << CKM_DATA_PATH << CKM_KEY_PREFIX << m_user;
+
+    std::ifstream is(ss.str());
+    std::istreambuf_iterator<char> begin(is),end;
+    RawBuffer buffer(begin, end);
+    return buffer;
+}
+
+bool FileSystem::saveDomainKEK(const RawBuffer &buffer) const
+{
+    std::stringstream ss;
+    ss << CKM_DATA_PATH << CKM_KEY_PREFIX << m_user;
+
+    std::ofstream os(ss.str(), std::ios::out | std::ofstream::binary);
+    std::copy(buffer.begin(), buffer.end(), std::ostreambuf_iterator<char>(os));
+    return !os.fail();
+}
+
+} // namespace CKM
+
diff --git a/src/manager/service/FileSystem.h b/src/manager/service/FileSystem.h
new file mode 100644 (file)
index 0000000..e53a5a5
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ *  Copyright (c) 2000 - 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        FileSystem.h
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Sample service implementation.
+ */
+#pragma once
+
+#include <ckm/ckm-type.h>
+#include <string>
+
+namespace CKM {
+
+class FileSystem {
+public:
+    FileSystem(int uid);
+
+    std::string getDBPath() const;
+    RawBuffer getDomainKEK() const;
+    bool saveDomainKEK(const RawBuffer &buffer) const;
+
+    virtual ~FileSystem(){}
+protected:
+    int m_uid;
+    std::string m_user;
+};
+
+}
+