AES: add generation, save, get support.
[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         int commandId,
131         const CryptoAlgorithmSerializable & keyGenParams,
132         const Name &namePrivate,
133         const Label &labelPrivate,
134         const Name &namePublic,
135         const Label &labelPublic,
136         const PolicySerializable &policyPrivate,
137         const PolicySerializable &policyPublic);
138
139     RawBuffer createKeyAES(
140         const Credentials &cred,
141         int commandId,
142         const int size,
143         const Name &name,
144         const Label &label,
145         const PolicySerializable &policy);
146
147     RawBuffer getCertificateChain(
148         const Credentials &cred,
149         int commandId,
150         const RawBuffer &certificate,
151         const RawBufferVector &untrustedCertificates,
152         const RawBufferVector &trustedCertificates,
153         bool useTrustedSystemCertificates);
154
155     RawBuffer getCertificateChain(
156         const Credentials &cred,
157         int commandId,
158         const RawBuffer &certificate,
159         const LabelNameVector &untrustedCertificates,
160         const LabelNameVector &trustedCertificates,
161         bool useTrustedSystemCertificates);
162
163     RawBuffer  createSignature(
164         const Credentials &cred,
165         int commandId,
166         const Name &privateKeyName,
167         const Label & ownerLabel,
168         const Password &password,           // password for private_key
169         const RawBuffer &message,
170         const HashAlgorithm hash,
171         const RSAPaddingAlgorithm padding);
172
173     RawBuffer verifySignature(
174         const Credentials &cred,
175         int commandId,
176         const Name &publicKeyOrCertName,
177         const Label &label,
178         const Password &password,           // password for public_key (optional)
179         const RawBuffer &message,
180         const RawBuffer &signature,
181         const HashAlgorithm hash,
182         const RSAPaddingAlgorithm padding);
183
184     RawBuffer updateCCMode();
185
186     RawBuffer setPermission(
187         const Credentials &cred,
188         const int command,
189         const int msgID,
190         const Name &name,
191         const Label &label,
192         const Label &accessor_label,
193         const PermissionMask permissionMask);
194
195     int setPermissionHelper(
196             const Credentials &cred,
197             const Name &name,
198             const Label &ownerLabel,
199             const Label &accessorLabel,
200             const PermissionMask permissionMask);
201
202     int verifyAndSaveDataHelper(
203         const Credentials &cred,
204         const Name &name,
205         const Label &label,
206         const RawBuffer &data,
207         DataType dataType,
208         const PolicySerializable &policy);
209
210 private:
211
212     // select private/system database depending on asking uid and owner label.
213     // output: database handler and effective label
214     UserData & selectDatabase(const Credentials &incoming_cred,
215                               const Label       &incoming_label);
216
217     int unlockSystemDB();
218     int unlockDatabase(uid_t            user,
219                        const Password & password);
220
221     void loadDKEKFile(
222         uid_t user,
223         const Password &password);
224
225     void saveDKEKFile(
226         uid_t user,
227         const Password &password);
228
229     int verifyBinaryData(
230         DataType dataType,
231         RawBuffer &input_data) const;
232
233     int toBinaryData(
234         DataType dataType,
235         const RawBuffer &input_data,
236         RawBuffer &output_data) const;
237
238     int checkSaveConditions(
239         const Credentials &cred,
240         UserData &handler,
241         const Name &name,
242         const Label &label);
243
244     int saveDataHelper(
245         const Credentials &cred,
246         const Name &name,
247         const Label &label,
248         DataType dataType,
249         const RawBuffer &data,
250         const PolicySerializable &policy);
251
252     int saveDataHelper(
253         const Credentials &cred,
254         const Name &name,
255         const Label &label,
256         const PKCS12Serializable &pkcs,
257         const PolicySerializable &keyPolicy,
258         const PolicySerializable &certPolicy);
259
260     DB::Row createEncryptedRow(
261         CryptoLogic &crypto,
262         const Name &name,
263         const Label &label,
264         DataType dataType,
265         const RawBuffer &data,
266         const Policy &policy) const;
267
268     int getPKCS12Helper(
269         const Credentials &cred,
270         const Name &name,
271         const Label &label,
272         const Password &keyPassword,
273         const Password &certPassword,
274         KeyShPtr & privKey,
275         CertificateShPtr & cert,
276         CertificateShPtrVector & caChain);
277
278     int extractPKCS12Data(
279         CryptoLogic &crypto,
280         const Name &name,
281         const Label &ownerLabel,
282         const PKCS12Serializable &pkcs,
283         const PolicySerializable &keyPolicy,
284         const PolicySerializable &certPolicy,
285         DB::RowVector &output) const;
286
287     int removeDataHelper(
288         const Credentials &cred,
289         const Name &name,
290         const Label &ownerLabel);
291
292     int readSingleRow(
293         const Name &name,
294         const Label &ownerLabel,
295         DataType dataType,
296         DB::Crypto & database,
297         DB::Row &row);
298
299     int readMultiRow(const Name &name,
300         const Label &ownerLabel,
301         DataType dataType,
302         DB::Crypto & database,
303         DB::RowVector &output);
304
305     int checkDataPermissionsHelper(
306         const Credentials &cred,
307         const Name &name,
308         const Label &ownerLabel,
309         const Label &accessorLabel,
310         const DB::Row &row,
311         bool exportFlag,
312         DB::Crypto & database);
313
314     int readDataHelper(
315         bool exportFlag,
316         const Credentials &cred,
317         DataType dataType,
318         const Name &name,
319         const Label &label,
320         const Password &password,
321         DB::Row &row);
322
323     int readDataHelper(
324         bool exportFlag,
325         const Credentials &cred,
326         DataType dataType,
327         const Name &name,
328         const Label &label,
329         const Password &password,
330         DB::RowVector &rows);
331
332     int createKeyAESHelper(
333         const Credentials &cred,
334         const int size,
335         const Name &name,
336         const Label &label,
337         const PolicySerializable &policy);
338
339     int createKeyPairHelper(
340         const Credentials &cred,
341         const CryptoAlgorithmSerializable & keyGenParams,
342         const Name &namePrivate,
343         const Label &labelPrivate,
344         const Name &namePublic,
345         const Label &labelPublic,
346         const PolicySerializable &policyPrivate,
347         const PolicySerializable &policyPublic);
348
349     int readCertificateHelper(
350         const Credentials &cred,
351         const LabelNameVector &labelNameVector,
352         CertificateImplVector &certVector);
353
354     int getCertificateChainHelper(
355         const CertificateImpl &cert,
356         const RawBufferVector &untrustedCertificates,
357         const RawBufferVector &trustedCertificates,
358         bool useTrustedSystemCertificates,
359         RawBufferVector &chainRawVector);
360
361     int getCertificateChainHelper(
362         const Credentials &cred,
363         const CertificateImpl &cert,
364         const LabelNameVector &untrusted,
365         const LabelNameVector &trusted,
366         bool useTrustedSystemCertificates,
367         RawBufferVector &chainRawVector);
368
369     int getDataListHelper(
370         const Credentials &cred,
371         const DataType dataType,
372         LabelNameVector &labelNameVector);
373
374     int changeUserPasswordHelper(uid_t user,
375                                  const Password &oldPassword,
376                                  const Password &newPassword);
377
378     int resetUserPasswordHelper(uid_t user, const Password &newPassword);
379
380     std::map<uid_t, UserData> m_userDataMap;
381     AccessControl m_accessControl;
382     Crypto::Decider m_decider;
383     //FileLock m_lock;
384 };
385
386 } // namespace CKM
387