Add support for binary data to GStore
[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     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::Row &row);
325
326     int readDataHelper(
327         bool exportFlag,
328         const Credentials &cred,
329         DataType dataType,
330         const Name &name,
331         const Label &label,
332         const Password &password,
333         DB::RowVector &rows);
334
335     int createKeyAESHelper(
336         const Credentials &cred,
337         const int size,
338         const Name &name,
339         const Label &label,
340         const PolicySerializable &policy);
341
342     int createKeyPairHelper(
343         const Credentials &cred,
344         const CryptoAlgorithmSerializable & keyGenParams,
345         const Name &namePrivate,
346         const Label &labelPrivate,
347         const Name &namePublic,
348         const Label &labelPublic,
349         const PolicySerializable &policyPrivate,
350         const PolicySerializable &policyPublic);
351
352     int readCertificateHelper(
353         const Credentials &cred,
354         const LabelNameVector &labelNameVector,
355         CertificateImplVector &certVector);
356
357     int getCertificateChainHelper(
358         const CertificateImpl &cert,
359         const RawBufferVector &untrustedCertificates,
360         const RawBufferVector &trustedCertificates,
361         bool useTrustedSystemCertificates,
362         RawBufferVector &chainRawVector);
363
364     int getCertificateChainHelper(
365         const Credentials &cred,
366         const CertificateImpl &cert,
367         const LabelNameVector &untrusted,
368         const LabelNameVector &trusted,
369         bool useTrustedSystemCertificates,
370         RawBufferVector &chainRawVector);
371
372     int getDataListHelper(
373         const Credentials &cred,
374         const DataType dataType,
375         LabelNameVector &labelNameVector);
376
377     int changeUserPasswordHelper(uid_t user,
378                                  const Password &oldPassword,
379                                  const Password &newPassword);
380
381     int resetUserPasswordHelper(uid_t user, const Password &newPassword);
382
383     int loadAppKey(UserData& handle, const Label& appLabel);
384
385     AccessControl m_accessControl;
386     Crypto::Decider m_decider;
387     //FileLock m_lock;
388
389 protected:
390     std::map<uid_t, UserData> m_userDataMap;
391 };
392
393 } // namespace CKM
394