Implementation of Control::unlockUserKey
[platform/core/security/key-manager.git] / src / manager / service / ckm-logic.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        ckm-logic.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Sample service implementation.
21  */
22 #include <dpl/serialization.h>
23
24 #include <ckm/ckm-error.h>
25 #include <ckm/ckm-type.h>
26 #include <key-provider.h>
27 #include <file-system.h>
28
29 #include <ckm-logic.h>
30 namespace CKM {
31
32 CKMLogic::CKMLogic(){}
33 CKMLogic::~CKMLogic(){}
34
35 RawBuffer CKMLogic::unlockUserKey(uid_t user, const std::string &password) {
36     // TODO try catch for all errors that should be supported by error code
37     int retCode = KEY_MANAGER_API_SUCCESS;
38
39     UserData &handle = m_userDataMap[user];
40
41     if (!(handle.keyProvider.isInitialized())) {
42         auto &handle = m_userDataMap[user];
43
44         FileSystem fs(user);
45         auto wrappedDomainKEK = fs.getDomainKEK();
46
47         if (wrappedDomainKEK.empty()) {
48             wrappedDomainKEK = KeyProvider::generateDomainKEK(std::to_string(user), password);
49             fs.saveDomainKEK(wrappedDomainKEK);
50         }
51
52         handle.keyProvider = KeyProvider(wrappedDomainKEK, password);
53
54         // TODO Now create database!
55     }
56
57     MessageBuffer response;
58     Serialization::Serialize(response, retCode);
59     return response.Pop();
60 }
61
62 RawBuffer CKMLogic::lockUserKey(uid_t user) {
63     // TODO try catch for all errors that should be supported by error code
64     m_userDataMap.erase(user);
65
66     MessageBuffer response;
67     Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
68     return response.Pop();
69 }
70
71 RawBuffer CKMLogic::removeUserData(uid_t user) {
72     // TODO try catch for all errors that should be supported by error code
73     m_userDataMap.erase(user);
74
75     FileSystem fs(user);
76 //    fs.removeUserData(); // remove DB also
77
78     MessageBuffer response;
79     Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
80     return response.Pop();
81 }
82
83 RawBuffer CKMLogic::changeUserPassword(
84     uid_t user,
85     const std::string &oldPassword,
86     const std::string &newPassword)
87 {
88     int retCode = KEY_MANAGER_API_SUCCESS;
89     // TODO try-catch
90     FileSystem fs(user);
91     auto wrappedDomainKEK = fs.getDomainKEK();
92     if (wrappedDomainKEK.empty()) {
93         retCode = KEY_MANAGER_API_ERROR_BAD_REQUEST;
94     } else {
95         wrappedDomainKEK = KeyProvider::reencrypt(wrappedDomainKEK, oldPassword, newPassword);
96         fs.saveDomainKEK(wrappedDomainKEK);
97     }
98     MessageBuffer response;
99     Serialization::Serialize(response, retCode);
100     return response.Pop();
101 }
102
103 RawBuffer CKMLogic::resetUserPassword(
104     uid_t user,
105     const std::string &newPassword)
106 {
107     int retCode = KEY_MANAGER_API_SUCCESS;
108     // TODO try-catch
109     if (m_userDataMap.count(user) <= 0) {
110         retCode = KEY_MANAGER_API_ERROR_BAD_REQUEST;
111     } else {
112         auto &handler = m_userDataMap[user];
113         auto wrappedDomainKEK = handler.keyProvider.getDomainKEK(newPassword);
114         FileSystem fs(user);
115         fs.saveDomainKEK(wrappedDomainKEK);
116     }
117
118     MessageBuffer response;
119     Serialization::Serialize(response, retCode);
120     return response.Pop();
121 }
122
123 RawBuffer CKMLogic::saveData(
124     Credentials &cred,
125     int commandId,
126     DBDataType dataType,
127     const Alias &alias,
128     const RawBuffer &key,
129     const PolicySerializable &policy)
130 {
131     (void)cred;
132     (void)alias;
133     (void)key;
134     (void)policy;
135
136     MessageBuffer response;
137     Serialization::Serialize(response, static_cast<int>(LogicCommand::SAVE));
138     Serialization::Serialize(response, commandId);
139     Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
140     Serialization::Serialize(response, static_cast<int>(dataType));
141
142     return response.Pop();
143 }
144
145 RawBuffer CKMLogic::removeData(
146     Credentials &cred,
147     int commandId,
148     DBDataType dataType,
149     const Alias &alias)
150 {
151     (void)cred;
152     (void)alias;
153
154     MessageBuffer response;
155     Serialization::Serialize(response, static_cast<int>(LogicCommand::REMOVE));
156     Serialization::Serialize(response, commandId);
157     Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
158     Serialization::Serialize(response, static_cast<int>(dataType));
159
160     return response.Pop();
161 }
162
163 RawBuffer CKMLogic::getData(
164     Credentials &cred,
165     int commandId,
166     DBDataType dataType,
167     const Alias &alias,
168     const std::string &password)
169 {
170     (void)cred;
171     (void)alias;
172     (void)password;
173
174     MessageBuffer response;
175     Serialization::Serialize(response, static_cast<int>(LogicCommand::GET));
176     Serialization::Serialize(response, commandId);
177     Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
178     Serialization::Serialize(response, static_cast<int>(dataType));
179     Serialization::Serialize(response, RawBuffer());
180     return response.Pop();
181 }
182
183 RawBuffer CKMLogic::getDataList(
184     Credentials &cred,
185     int commandId,
186     DBDataType dataType)
187 {
188     (void)cred;
189
190     MessageBuffer response;
191     Serialization::Serialize(response, static_cast<int>(LogicCommand::GET_LIST));
192     Serialization::Serialize(response, commandId);
193     Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
194     Serialization::Serialize(response, static_cast<int>(dataType));
195     Serialization::Serialize(response, AliasVector());
196     return response.Pop();
197 }
198
199 RawBuffer CKMLogic::createKeyPairRSA(
200     Credentials &cred,
201     int commandId,
202     int size,
203     const Alias &privateKeyAlias,
204     const Alias &publicKeyAlias,
205     PolicySerializable policyPrivateKey,
206     PolicySerializable policyPublicKey)
207
208     (void)cred;
209     (void)size;
210     (void)privateKeyAlias;
211     (void)publicKeyAlias,
212     (void)policyPrivateKey;
213     (void)policyPublicKey;
214     MessageBuffer response;
215     Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
216     Serialization::Serialize(response, commandId);
217     Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
218  
219     return response.Pop();
220 }
221
222 RawBuffer CKMLogic::createKeyPairECDSA(
223     Credentials &cred,
224     int commandId,
225     int type,
226     const Alias &privateKeyAlias,
227     const Alias &publicKeyAlias,
228     PolicySerializable policyPrivateKey,
229     PolicySerializable policyPublicKey)
230 {
231     (void)cred;
232     (void)type;
233     (void)privateKeyAlias;
234     (void)publicKeyAlias,
235     (void)policyPrivateKey;
236     (void)policyPublicKey;
237     
238     MessageBuffer response;
239     Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
240     Serialization::Serialize(response, commandId);
241     Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
242  
243     return response.Pop();
244 }
245
246 } // namespace CKM
247