Modify encryption scheme
[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/gobj.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::GObjShPtr& key);
210
211 protected:
212     int unlockSystemDB();
213
214 private:
215
216     // select private/system database depending on asking uid and owner label.
217     // output: database handler and effective label
218     UserData & selectDatabase(const Credentials &incoming_cred,
219                               const Label       &incoming_label);
220
221     int unlockDatabase(uid_t            user,
222                        const Password & password);
223
224     void loadDKEKFile(
225         uid_t user,
226         const Password &password);
227
228     void saveDKEKFile(
229         uid_t user,
230         const Password &password);
231
232     int verifyBinaryData(
233         DataType dataType,
234         RawBuffer &input_data) const;
235
236     int toBinaryData(
237         DataType dataType,
238         const RawBuffer &input_data,
239         RawBuffer &output_data) const;
240
241     int checkSaveConditions(
242         const Credentials &cred,
243         UserData &handler,
244         const Name &name,
245         const Label &label);
246
247     int saveDataHelper(
248         const Credentials &cred,
249         const Name &name,
250         const Label &label,
251         DataType dataType,
252         const RawBuffer &data,
253         const PolicySerializable &policy);
254
255     int saveDataHelper(
256         const Credentials &cred,
257         const Name &name,
258         const Label &label,
259         const PKCS12Serializable &pkcs,
260         const PolicySerializable &keyPolicy,
261         const PolicySerializable &certPolicy);
262
263     DB::Row createEncryptedRow(
264         CryptoLogic &crypto,
265         const Name &name,
266         const Label &label,
267         DataType dataType,
268         const RawBuffer &data,
269         const Policy &policy) const;
270
271     int getPKCS12Helper(
272         const Credentials &cred,
273         const Name &name,
274         const Label &label,
275         const Password &keyPassword,
276         const Password &certPassword,
277         KeyShPtr & privKey,
278         CertificateShPtr & cert,
279         CertificateShPtrVector & caChain);
280
281     int extractPKCS12Data(
282         CryptoLogic &crypto,
283         const Name &name,
284         const Label &ownerLabel,
285         const PKCS12Serializable &pkcs,
286         const PolicySerializable &keyPolicy,
287         const PolicySerializable &certPolicy,
288         DB::RowVector &output) const;
289
290     int removeDataHelper(
291         const Credentials &cred,
292         const Name &name,
293         const Label &ownerLabel);
294
295     int readSingleRow(
296         const Name &name,
297         const Label &ownerLabel,
298         DataType dataType,
299         DB::Crypto & database,
300         DB::Row &row);
301
302     int readMultiRow(const Name &name,
303         const Label &ownerLabel,
304         DataType dataType,
305         DB::Crypto & database,
306         DB::RowVector &output);
307
308     int checkDataPermissionsHelper(
309         const Credentials &cred,
310         const Name &name,
311         const Label &ownerLabel,
312         const Label &accessorLabel,
313         const DB::Row &row,
314         bool exportFlag,
315         DB::Crypto & database);
316
317     Crypto::GObjUPtr rowToObject(
318         UserData& handler,
319         DB::Row row,
320         const Password& password);
321
322     int readDataHelper(
323         bool exportFlag,
324         const Credentials &cred,
325         DataType dataType,
326         const Name &name,
327         const Label &label,
328         const Password &password,
329         Crypto::GObjUPtr &obj);
330
331     int readDataHelper(
332         bool exportFlag,
333         const Credentials &cred,
334         DataType dataType,
335         const Name &name,
336         const Label &label,
337         const Password &password,
338         Crypto::GObjUPtr &obj,
339         DataType& objDataType);
340
341     int readDataHelper(
342         bool exportFlag,
343         const Credentials &cred,
344         DataType dataType,
345         const Name &name,
346         const Label &label,
347         const Password &password,
348         Crypto::GObjUPtrVector &objs);
349
350     int createKeyAESHelper(
351         const Credentials &cred,
352         const int size,
353         const Name &name,
354         const Label &label,
355         const PolicySerializable &policy);
356
357     int createKeyPairHelper(
358         const Credentials &cred,
359         const CryptoAlgorithmSerializable & keyGenParams,
360         const Name &namePrivate,
361         const Label &labelPrivate,
362         const Name &namePublic,
363         const Label &labelPublic,
364         const PolicySerializable &policyPrivate,
365         const PolicySerializable &policyPublic);
366
367     int readCertificateHelper(
368         const Credentials &cred,
369         const LabelNameVector &labelNameVector,
370         CertificateImplVector &certVector);
371
372     int getCertificateChainHelper(
373         const CertificateImpl &cert,
374         const RawBufferVector &untrustedCertificates,
375         const RawBufferVector &trustedCertificates,
376         bool useTrustedSystemCertificates,
377         RawBufferVector &chainRawVector);
378
379     int getCertificateChainHelper(
380         const Credentials &cred,
381         const CertificateImpl &cert,
382         const LabelNameVector &untrusted,
383         const LabelNameVector &trusted,
384         bool useTrustedSystemCertificates,
385         RawBufferVector &chainRawVector);
386
387     int getDataListHelper(
388         const Credentials &cred,
389         const DataType dataType,
390         LabelNameVector &labelNameVector);
391
392     int changeUserPasswordHelper(uid_t user,
393                                  const Password &oldPassword,
394                                  const Password &newPassword);
395
396     int resetUserPasswordHelper(uid_t user, const Password &newPassword);
397
398     int loadAppKey(UserData& handle, const Label& appLabel);
399
400     AccessControl m_accessControl;
401     Crypto::Decider m_decider;
402     //FileLock m_lock;
403
404 protected:
405     std::map<uid_t, UserData> m_userDataMap;
406 };
407
408 } // namespace CKM
409