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