afc90be16c79bc95af5dfc6ba00c74e8ad3e8996
[platform/core/security/key-manager.git] / src / manager / service / ckm-logic.h
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.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Sample service implementation.
21  */
22 #pragma once
23
24 #include <string>
25 #include <vector>
26
27 #include <message-buffer.h>
28 #include <protocols.h>
29 #include <ckm/ckm-type.h>
30 #include <connection-info.h>
31 #include <db-crypto.h>
32 #include <key-provider.h>
33 #include <crypto-logic.h>
34 #include <file-lock.h>
35 #include <access-control.h>
36 #include <certificate-impl.h>
37 #include <sys/types.h>
38
39 #include <platform/decider.h>
40
41 namespace CKM {
42
43 struct UserData {
44     KeyProvider    keyProvider;
45     DB::Crypto     database;
46     CryptoLogic    crypto;
47 };
48
49 class CKMLogic {
50 public:
51     static const uid_t SYSTEM_DB_UID;
52
53     CKMLogic();
54     CKMLogic(const CKMLogic &) = delete;
55     CKMLogic(CKMLogic &&) = delete;
56     CKMLogic& operator=(const CKMLogic &) = delete;
57     CKMLogic& operator=(CKMLogic &&) = delete;
58     virtual ~CKMLogic();
59
60     RawBuffer unlockUserKey(uid_t user, const Password &password);
61     RawBuffer lockUserKey(uid_t user);
62
63     RawBuffer removeUserData(uid_t user);
64
65     RawBuffer changeUserPassword(
66         uid_t user,
67         const Password &oldPassword,
68         const Password &newPassword);
69
70     RawBuffer resetUserPassword(
71         uid_t user,
72         const Password &newPassword);
73
74     RawBuffer removeApplicationData(
75         const Label &smackLabel);
76
77     RawBuffer saveData(
78         const Credentials &cred,
79         int commandId,
80         const Name &name,
81         const Label &label,
82         const RawBuffer &data,
83         DataType dataType,
84         const PolicySerializable &policy);
85
86     RawBuffer savePKCS12(
87         const Credentials &cred,
88         int commandId,
89         const Name &name,
90         const Label &label,
91         const PKCS12Serializable &pkcs,
92         const PolicySerializable &keyPolicy,
93         const PolicySerializable &certPolicy);
94
95     RawBuffer removeData(
96         const Credentials &cred,
97         int commandId,
98         const Name &name,
99         const Label &label);
100
101     RawBuffer getData(
102         const Credentials &cred,
103         int commandId,
104         DataType dataType,
105         const Name &name,
106         const Label &label,
107         const Password &password);
108
109     RawBuffer getPKCS12(
110         const Credentials &cred,
111         int commandId,
112         const Name &name,
113         const Label &label,
114         const Password &keyPassword,
115         const Password &certPassword);
116
117     RawBuffer getDataList(
118         const Credentials &cred,
119         int commandId,
120         DataType dataType);
121
122     RawBuffer createKeyPair(
123         const Credentials &cred,
124         int commandId,
125         const CryptoAlgorithmSerializable & keyGenParams,
126         const Name &namePrivate,
127         const Label &labelPrivate,
128         const Name &namePublic,
129         const Label &labelPublic,
130         const PolicySerializable &policyPrivate,
131         const PolicySerializable &policyPublic);
132
133     RawBuffer createKeyAES(
134         const Credentials &cred,
135         int commandId,
136         const int size,
137         const Name &name,
138         const Label &label,
139         const PolicySerializable &policy);
140
141     RawBuffer getCertificateChain(
142         const Credentials &cred,
143         int commandId,
144         const RawBuffer &certificate,
145         const RawBufferVector &untrustedCertificates,
146         const RawBufferVector &trustedCertificates,
147         bool useTrustedSystemCertificates);
148
149     RawBuffer getCertificateChain(
150         const Credentials &cred,
151         int commandId,
152         const RawBuffer &certificate,
153         const LabelNameVector &untrustedCertificates,
154         const LabelNameVector &trustedCertificates,
155         bool useTrustedSystemCertificates);
156
157     RawBuffer  createSignature(
158         const Credentials &cred,
159         int commandId,
160         const Name &privateKeyName,
161         const Label & ownerLabel,
162         const Password &password,           // password for private_key
163         const RawBuffer &message,
164         const HashAlgorithm hash,
165         const RSAPaddingAlgorithm padding);
166
167     RawBuffer verifySignature(
168         const Credentials &cred,
169         int commandId,
170         const Name &publicKeyOrCertName,
171         const Label &label,
172         const Password &password,           // password for public_key (optional)
173         const RawBuffer &message,
174         const RawBuffer &signature,
175         const HashAlgorithm hash,
176         const RSAPaddingAlgorithm padding);
177
178     RawBuffer updateCCMode();
179
180     RawBuffer setPermission(
181         const Credentials &cred,
182         const int command,
183         const int msgID,
184         const Name &name,
185         const Label &label,
186         const Label &accessor_label,
187         const PermissionMask permissionMask);
188
189     int setPermissionHelper(
190             const Credentials &cred,
191             const Name &name,
192             const Label &ownerLabel,
193             const Label &accessorLabel,
194             const PermissionMask permissionMask);
195
196     int verifyAndSaveDataHelper(
197         const Credentials &cred,
198         const Name &name,
199         const Label &label,
200         const RawBuffer &data,
201         DataType dataType,
202         const PolicySerializable &policy);
203
204 private:
205
206     // select private/system database depending on asking uid and owner label.
207     // output: database handler and effective label
208     UserData & selectDatabase(const Credentials &incoming_cred,
209                               const Label       &incoming_label);
210
211     int unlockSystemDB();
212     int unlockDatabase(uid_t            user,
213                        const Password & password);
214
215     void loadDKEKFile(
216         uid_t user,
217         const Password &password);
218
219     void saveDKEKFile(
220         uid_t user,
221         const Password &password);
222
223     int verifyBinaryData(
224         DataType dataType,
225         RawBuffer &input_data) const;
226
227     int toBinaryData(
228         DataType dataType,
229         const RawBuffer &input_data,
230         RawBuffer &output_data) const;
231
232     int checkSaveConditions(
233         const Credentials &cred,
234         UserData &handler,
235         const Name &name,
236         const Label &label);
237
238     int saveDataHelper(
239         const Credentials &cred,
240         const Name &name,
241         const Label &label,
242         DataType dataType,
243         const RawBuffer &data,
244         const PolicySerializable &policy);
245
246     int saveDataHelper(
247         const Credentials &cred,
248         const Name &name,
249         const Label &label,
250         const PKCS12Serializable &pkcs,
251         const PolicySerializable &keyPolicy,
252         const PolicySerializable &certPolicy);
253
254     DB::Row createEncryptedRow(
255         CryptoLogic &crypto,
256         const Name &name,
257         const Label &label,
258         DataType dataType,
259         const RawBuffer &data,
260         const Policy &policy) const;
261
262     int getPKCS12Helper(
263         const Credentials &cred,
264         const Name &name,
265         const Label &label,
266         const Password &keyPassword,
267         const Password &certPassword,
268         KeyShPtr & privKey,
269         CertificateShPtr & cert,
270         CertificateShPtrVector & caChain);
271
272     int extractPKCS12Data(
273         CryptoLogic &crypto,
274         const Name &name,
275         const Label &ownerLabel,
276         const PKCS12Serializable &pkcs,
277         const PolicySerializable &keyPolicy,
278         const PolicySerializable &certPolicy,
279         DB::RowVector &output) const;
280
281     int removeDataHelper(
282         const Credentials &cred,
283         const Name &name,
284         const Label &ownerLabel);
285
286     int readSingleRow(
287         const Name &name,
288         const Label &ownerLabel,
289         DataType dataType,
290         DB::Crypto & database,
291         DB::Row &row);
292
293     int readMultiRow(const Name &name,
294         const Label &ownerLabel,
295         DataType dataType,
296         DB::Crypto & database,
297         DB::RowVector &output);
298
299     int checkDataPermissionsHelper(
300         const Credentials &cred,
301         const Name &name,
302         const Label &ownerLabel,
303         const Label &accessorLabel,
304         const DB::Row &row,
305         bool exportFlag,
306         DB::Crypto & database);
307
308     int readDataHelper(
309         bool exportFlag,
310         const Credentials &cred,
311         DataType dataType,
312         const Name &name,
313         const Label &label,
314         const Password &password,
315         DB::Row &row);
316
317     int readDataHelper(
318         bool exportFlag,
319         const Credentials &cred,
320         DataType dataType,
321         const Name &name,
322         const Label &label,
323         const Password &password,
324         DB::RowVector &rows);
325
326     int createKeyAESHelper(
327         const Credentials &cred,
328         const int size,
329         const Name &name,
330         const Label &label,
331         const PolicySerializable &policy);
332
333     int createKeyPairHelper(
334         const Credentials &cred,
335         const CryptoAlgorithmSerializable & keyGenParams,
336         const Name &namePrivate,
337         const Label &labelPrivate,
338         const Name &namePublic,
339         const Label &labelPublic,
340         const PolicySerializable &policyPrivate,
341         const PolicySerializable &policyPublic);
342
343     int readCertificateHelper(
344         const Credentials &cred,
345         const LabelNameVector &labelNameVector,
346         CertificateImplVector &certVector);
347
348     int getCertificateChainHelper(
349         const CertificateImpl &cert,
350         const RawBufferVector &untrustedCertificates,
351         const RawBufferVector &trustedCertificates,
352         bool useTrustedSystemCertificates,
353         RawBufferVector &chainRawVector);
354
355     int getCertificateChainHelper(
356         const Credentials &cred,
357         const CertificateImpl &cert,
358         const LabelNameVector &untrusted,
359         const LabelNameVector &trusted,
360         bool useTrustedSystemCertificates,
361         RawBufferVector &chainRawVector);
362
363     int getDataListHelper(
364         const Credentials &cred,
365         const DataType dataType,
366         LabelNameVector &labelNameVector);
367
368     int changeUserPasswordHelper(uid_t user,
369                                  const Password &oldPassword,
370                                  const Password &newPassword);
371
372     int resetUserPasswordHelper(uid_t user, const Password &newPassword);
373
374     std::map<uid_t, UserData> m_userDataMap;
375     AccessControl m_accessControl;
376     Crypto::Decider m_decider;
377     //FileLock m_lock;
378 };
379
380 } // namespace CKM
381