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