Key/password management API implementation
[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
23 #include "key-server.h"
24 #include "file-footer.h"
25 #include "logger.h"
26 #include "key-manager/key-manager.h"
27 #include "misc.h"
28 #include "rmi/common.h"
29
30 namespace ode {
31
32 namespace {
33
34 const char *PRIVILEGE_PLATFORM = "http://tizen.org/privilege/internal/default/platform";
35
36 const std::map<int, size_t> KEY_SIZE = {
37         { Key::DEFAULT_256BIT, 32 },
38         { Key::DEFAULT_512BIT, 64 }
39 };
40
41 } // anonymous namespace
42
43 KeyServer::KeyServer(ServerContext& srv) :
44         server(srv)
45 {
46         server.expose(this, "",                                 (int)(KeyServer::isInitialized)(std::string));
47         server.expose(this, PRIVILEGE_PLATFORM, (int)(KeyServer::init)(std::string, std::string, int));
48         server.expose(this, PRIVILEGE_PLATFORM, (int)(KeyServer::remove)(std::string, std::string));
49         server.expose(this, PRIVILEGE_PLATFORM, (int)(KeyServer::changePassword)(std::string, std::string, std::string));
50         server.expose(this, PRIVILEGE_PLATFORM, (int)(KeyServer::verifyPassword)(std::string, std::string));
51 }
52
53 KeyServer::~KeyServer()
54 {
55 }
56
57 int KeyServer::isInitialized(const std::string& dev)
58 {
59         if (dev.empty())
60                 return error::InvalidParameter;
61
62         return FileFooter::exist(dev) ? error::None : error::NoSuchFile;
63 }
64
65 int KeyServer::init(const std::string& dev,
66                                         const std::string& password,
67                                         int params)
68 {
69         if (dev.empty() || password.empty() || KEY_SIZE.find(params) == KEY_SIZE.end())
70                 return error::InvalidParameter;
71
72         KeyManager::data pwData(password.begin(), password.end());
73         KeyManager keyManager;
74
75         keyManager.initPassword(pwData, KEY_SIZE.at(params));
76
77         FileFooter::write(dev, keyManager.serialize());
78         return error::None;
79 }
80
81 int KeyServer::remove(const std::string& dev, const std::string& password)
82 {
83         if (dev.empty() || password.empty())
84                 return error::InvalidParameter;
85
86         KeyManager::data pwData(password.begin(), password.end());
87         KeyManager keyManager(FileFooter::read(dev));
88
89         if (!keyManager.verifyPassword(pwData)) {
90                 ERROR(SINK, "Wrong password passed.");
91                 return error::WrongPassword;
92         }
93
94         FileFooter::clear(dev);
95         return error::None;
96 }
97
98 int KeyServer::changePassword(const std::string& dev,
99                                                           const std::string& curPassword,
100                                                           const std::string& newPassword)
101 {
102         if (dev.empty() || curPassword.empty() || newPassword.empty())
103                 return error::InvalidParameter;
104
105         KeyManager::data curPwData(curPassword.begin(), curPassword.end());
106         KeyManager::data newPwData(newPassword.begin(), newPassword.end());
107         KeyManager keyManager(FileFooter::read(dev));
108
109         if (!keyManager.verifyPassword(curPwData)) {
110                 ERROR(SINK, "Wrong password passed.");
111                 return error::WrongPassword;
112         }
113
114         keyManager.changePassword(curPwData, newPwData);
115         FileFooter::write(dev, keyManager.serialize());
116         return error::None;
117 }
118
119 int KeyServer::verifyPassword(const std::string& dev,
120                                                           const std::string& password)
121 {
122         if (dev.empty() || password.empty())
123                 return error::InvalidParameter;
124
125         KeyManager::data pwData(password.begin(), password.end());
126         KeyManager keyManager(FileFooter::read(dev));
127
128         return keyManager.verifyPassword(pwData) ? error::None : error::WrongPassword;
129 }
130
131 } // namespace ode