Use common typedef for binary data
[platform/core/security/ode.git] / server / key-server.cpp
1 /*
2  *  Copyright (c) 2015-2017 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 #include <stdlib.h>
18
19 #include <string>
20 #include <algorithm>
21 #include <map>
22 #include <utility>
23
24 #include "key-server.h"
25 #include "file-footer.h"
26 #include "logger.h"
27 #include "misc.h"
28 #include "rmi/common.h"
29 #include "key-manager/encrypted-key.h"
30 #include "key-manager/key-generator.h"
31
32 #include <klay/exception.h>
33
34 namespace ode {
35
36 namespace {
37
38 const char *PRIVILEGE_PLATFORM = "http://tizen.org/privilege/internal/default/platform";
39
40 const std::map<int, size_t> KEY_SIZE = {
41         { Key::DEFAULT_256BIT, 32 },
42         { Key::DEFAULT_512BIT, 64 }
43 };
44
45 } // anonymous namespace
46
47 KeyServer::KeyServer(ServerContext& srv) :
48         server(srv)
49 {
50         server.expose(this, "",                                 (int)(KeyServer::isInitialized)(std::string));
51         server.expose(this, PRIVILEGE_PLATFORM, (int)(KeyServer::init)(std::string, std::string, int));
52         server.expose(this, PRIVILEGE_PLATFORM, (int)(KeyServer::remove)(std::string, std::string));
53         server.expose(this, PRIVILEGE_PLATFORM, (int)(KeyServer::changePassword)(std::string, std::string, std::string));
54         server.expose(this, PRIVILEGE_PLATFORM, (int)(KeyServer::verifyPassword)(std::string, std::string));
55 }
56
57 KeyServer::~KeyServer()
58 {
59 }
60
61 int KeyServer::isInitialized(const std::string& dev)
62 {
63         if (dev.empty())
64                 return error::InvalidParameter;
65
66         return FileFooter::exist(dev) ? error::None : error::NoSuchFile;
67 }
68
69 int KeyServer::init(const std::string& dev,
70                                         const std::string& password,
71                                         int params)
72 {
73         BinaryData dummy;
74         return initAndGet(dev, password, params, dummy);
75 }
76
77 int KeyServer::initAndGet(const std::string& dev,
78                                                   const std::string& password,
79                                                   int params,
80                                                   BinaryData& masterKey)
81 {
82         if (dev.empty() || password.empty() || KEY_SIZE.find(params) == KEY_SIZE.end())
83                 return error::InvalidParameter;
84
85         masterKey = KeyGenerator::RNG(KEY_SIZE.at(params));
86
87         EncryptedKey ek(masterKey, password);
88         FileFooter::write(dev, ek.serialize());
89
90         return error::None;
91 }
92
93 int KeyServer::remove(const std::string& dev, const std::string& password)
94 {
95         int ret = verifyPassword(dev, password);
96         if (ret != error::None) {
97                 if (ret == error::WrongPassword)
98                         ERROR(SINK, "Wrong password passed.");
99                 return ret;
100         }
101
102         FileFooter::clear(dev);
103         return error::None;
104 }
105
106 int KeyServer::changePassword(const std::string& dev,
107                                                           const std::string& curPassword,
108                                                           const std::string& newPassword)
109 {
110         if (dev.empty() || curPassword.empty() || newPassword.empty())
111                 return error::InvalidParameter;
112
113         if (!FileFooter::exist(dev)) {
114                 ERROR(SINK, "Given device has no master key");
115                 return error::NoSuchFile;
116         }
117
118         EncryptedKey ek(FileFooter::read(dev));
119
120         auto key = ek.decrypt(curPassword);
121         if (key.empty()) {
122                 ERROR(SINK, "Wrong password passed");
123                 return error::WrongPassword;
124         }
125         ek.encrypt(key, newPassword);
126
127         FileFooter::write(dev, ek.serialize());
128         return error::None;
129 }
130
131 int KeyServer::verifyPassword(const std::string& dev,
132                                                           const std::string& password)
133 {
134         if (dev.empty() || password.empty())
135                 return error::InvalidParameter;
136
137         if (!FileFooter::exist(dev)) {
138                 ERROR(SINK, "Given device has no master key");
139                 return error::NoSuchFile;
140         }
141
142         EncryptedKey ek(FileFooter::read(dev));
143
144         auto key = ek.decrypt(password);
145         if (key.empty())
146                 return error::WrongPassword;
147
148         return error::None;
149 }
150
151 int KeyServer::get(const std::string& dev,
152                                    const std::string& password,
153                                    BinaryData& masterKey) const
154 {
155         if (dev.empty() || password.empty())
156                 return error::InvalidParameter;
157
158         if (!FileFooter::exist(dev)) {
159                 ERROR(SINK, "Given device has no master key");
160                 return error::NoSuchFile;
161         }
162
163         EncryptedKey ek(FileFooter::read(dev));
164
165         masterKey = ek.decrypt(password);
166         if (masterKey.empty()) {
167                 ERROR(SINK, "Wrong password passed");
168                 return error::WrongPassword;
169         }
170         return error::None;
171 }
172
173 void KeyServer::removePassword(const std::string& dev)
174 {
175         if (dev.empty())
176                 return;
177
178         FileFooter::clear(dev);
179 }
180
181 } // namespace ode