Add SDcard password confirm popup
[platform/core/security/ode.git] / server / file-footer.cpp
1 /*
2  *  Copyright (c) 2016 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 #include <fcntl.h>
17
18 #include <sstream>
19 #include <iomanip>
20
21 #include <klay/exception.h>
22 #include <klay/filesystem.h>
23 #include <klay/audit/logger.h>
24
25 #include "file-footer.h"
26 #include "key-manager/key-generator.h"
27
28 namespace ode {
29
30 namespace {
31
32 const std::string getFileName(const std::string &key)
33 {
34         KeyGenerator::data hash = KeyGenerator::MD5(KeyGenerator::data(key.begin(), key.end()));
35         std::stringstream fileName;
36
37         fileName << "/opt/etc/.ode_";
38         fileName << std::hex << std::setfill('0') << std::setw(2);
39         for (unsigned int byte : hash) {
40                 fileName << byte;
41         }
42
43         return fileName.str();
44 }
45
46 } // namsepace
47
48 bool FileFooter::exist(const std::string &key)
49 {
50         std::string fileName(getFileName(key));
51
52         INFO("Footer file : " + fileName);
53
54         runtime::File file(fileName);
55
56         return file.exists();
57 }
58
59 const FileFooter::data FileFooter::read(const std::string &key)
60 {
61         std::string fileName(getFileName(key));
62
63         INFO("Footer file : " + fileName);
64
65         runtime::File file(fileName);
66         data value(file.size());
67
68         file.open(O_RDONLY);
69         file.read(value.data(), value.size());
70
71         return value;
72 }
73
74 void FileFooter::write(const std::string &key, const data &value)
75 {
76         std::string fileName(getFileName(key));
77
78         INFO("Footer file : " + fileName);
79
80         runtime::File file(fileName);
81
82         file.create(0600);
83         file.write(value.data(), value.size());
84 }
85
86 void FileFooter::clear(const std::string &key)
87 {
88         std::string fileName(getFileName(key));
89
90         INFO("Footer file : " + fileName);
91
92         runtime::File file(fileName);
93
94         file.remove();
95 }
96
97 } // namespace ode