2 * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
23 #include "key-server.h"
24 #include "file-footer.h"
26 #include "key-manager/key-manager.h"
28 #include "rmi/common.h"
34 const char *PRIVILEGE_PLATFORM = "http://tizen.org/privilege/internal/default/platform";
36 const std::map<int, size_t> KEY_SIZE = {
37 { Key::DEFAULT_256BIT, 32 },
38 { Key::DEFAULT_512BIT, 64 }
41 } // anonymous namespace
43 KeyServer::KeyServer(ServerContext& srv) :
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));
53 KeyServer::~KeyServer()
57 int KeyServer::isInitialized(const std::string& dev)
60 return error::InvalidParameter;
62 return FileFooter::exist(dev) ? error::None : error::NoSuchFile;
65 int KeyServer::init(const std::string& dev,
66 const std::string& password,
69 if (dev.empty() || password.empty() || KEY_SIZE.find(params) == KEY_SIZE.end())
70 return error::InvalidParameter;
72 KeyManager::data pwData(password.begin(), password.end());
73 KeyManager keyManager;
75 keyManager.initPassword(pwData, KEY_SIZE.at(params));
77 FileFooter::write(dev, keyManager.serialize());
81 int KeyServer::remove(const std::string& dev, const std::string& password)
83 if (dev.empty() || password.empty())
84 return error::InvalidParameter;
86 KeyManager::data pwData(password.begin(), password.end());
87 KeyManager keyManager(FileFooter::read(dev));
89 if (!keyManager.verifyPassword(pwData)) {
90 ERROR(SINK, "Wrong password passed.");
91 return error::WrongPassword;
94 FileFooter::clear(dev);
98 int KeyServer::changePassword(const std::string& dev,
99 const std::string& curPassword,
100 const std::string& newPassword)
102 if (dev.empty() || curPassword.empty() || newPassword.empty())
103 return error::InvalidParameter;
105 KeyManager::data curPwData(curPassword.begin(), curPassword.end());
106 KeyManager::data newPwData(newPassword.begin(), newPassword.end());
107 KeyManager keyManager(FileFooter::read(dev));
109 if (!keyManager.verifyPassword(curPwData)) {
110 ERROR(SINK, "Wrong password passed.");
111 return error::WrongPassword;
114 keyManager.changePassword(curPwData, newPwData);
115 FileFooter::write(dev, keyManager.serialize());
119 int KeyServer::verifyPassword(const std::string& dev,
120 const std::string& password)
122 if (dev.empty() || password.empty())
123 return error::InvalidParameter;
125 KeyManager::data pwData(password.begin(), password.end());
126 KeyManager keyManager(FileFooter::read(dev));
128 return keyManager.verifyPassword(pwData) ? error::None : error::WrongPassword;