47a3b7e47bdf831984d608806f87dd06f45edccb
[platform/core/security/key-manager.git] / src / manager / service / file-system.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  *
16  *
17  * @file        FileSystem.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Sample service implementation.
21  */
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <string>
28 #include <sstream>
29 #include <fstream>
30
31 #include <safe-buffer.h>
32 #include <buffer-conversion.h>
33
34 #include <dpl/log/log.h>
35
36 #include <file-system.h>
37
38 namespace {
39
40 static const std::string CKM_DATA_PATH = "/opt/data/ckm/";
41 static const std::string CKM_KEY_PREFIX = "key-";
42 static const std::string CKM_DB_PREFIX = "db-";
43
44 } // namespace anonymous
45
46 namespace CKM {
47
48 FileSystem::FileSystem(uid_t uid)
49   : m_uid(uid)
50 {}
51
52 std::string FileSystem::getDBPath() const
53 {
54     std::stringstream ss;
55     ss << CKM_DATA_PATH << CKM_DB_PREFIX << m_uid;
56     return ss.str();
57 }
58
59 std::string FileSystem::getDKEKPath() const {
60     std::stringstream ss;
61     ss << CKM_DATA_PATH << CKM_KEY_PREFIX << m_uid;
62     return ss.str();
63 }
64
65 SafeBuffer FileSystem::getDomainKEK() const
66 {
67     std::ifstream is(getDKEKPath());
68     std::istreambuf_iterator<char> begin(is),end;
69     RawBuffer buffer(begin, end);
70     return toSafeBuffer(buffer);
71 }
72
73 bool FileSystem::saveDomainKEK(const SafeBuffer &buffer) const
74 {
75     std::ofstream os(getDKEKPath(), std::ios::out | std::ofstream::binary);
76     std::copy(buffer.begin(), buffer.end(), std::ostreambuf_iterator<char>(os));
77     return !os.fail();
78 }
79
80 int FileSystem::init() {
81     errno = 0;
82     if ((mkdir(CKM_DATA_PATH.c_str(), 0700)) && (errno != EEXIST)) {
83         int err = errno;
84         LogError("Error in mkdir. Data directory could not be created. Errno: "
85             << err << " (" << strerror(err) << ")");
86         return -1; // TODO set up some error code
87     }
88     return 0;
89 }
90
91 int FileSystem::removeUserData() const {
92     int err, retCode = 0;
93     if (unlink(getDBPath().c_str())) {
94         retCode = -1;
95         err = errno;
96         LogError("Error in unlink user database: " << getDBPath()
97             << "Errno: " << errno << " " << strerror(err));
98     }
99     if (unlink(getDKEKPath().c_str())) {
100         retCode = -1;
101         err = errno;
102         LogError("Error in unlink user DKEK: " << getDKEKPath()
103             << "Errno: " << errno << " " << strerror(err));
104     }
105     return retCode;
106 }
107
108 } // namespace CKM
109