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