Add PKCS12 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 <certificate-store.h>
35 #include <file-lock.h>
36 #include <access-control.h>
37
38 namespace CKM {
39
40 struct UserData {
41     KeyProvider    keyProvider;
42     DBCrypto       database;
43     CryptoLogic    crypto;
44 };
45
46 class CKMLogic {
47 public:
48     CKMLogic();
49     CKMLogic(const CKMLogic &) = delete;
50     CKMLogic(CKMLogic &&) = delete;
51     CKMLogic& operator=(const CKMLogic &) = delete;
52     CKMLogic& operator=(CKMLogic &&) = delete;
53     virtual ~CKMLogic();
54
55     RawBuffer unlockUserKey(uid_t user, const Password &password);
56
57     RawBuffer lockUserKey(uid_t user);
58
59     RawBuffer removeUserData(uid_t user);
60
61     RawBuffer changeUserPassword(
62         uid_t user,
63         const Password &oldPassword,
64         const Password &newPassword);
65
66     RawBuffer resetUserPassword(
67         uid_t user,
68         const Password &newPassword);
69
70     RawBuffer removeApplicationData(
71         const Label &smackLabel);
72
73     RawBuffer saveData(
74         const Credentials &cred,
75         int commandId,
76         const Name &name,
77         const Label &label,
78         const RawBuffer &data,
79         DBDataType dataType,
80         const PolicySerializable &policy);
81
82     RawBuffer savePKCS12(
83         const Credentials &cred,
84         int commandId,
85         const Name &name,
86         const Label &label,
87         const PKCS12Serializable &pkcs,
88         const PolicySerializable &keyPolicy,
89         const PolicySerializable &certPolicy);
90
91     RawBuffer removeData(
92         const Credentials &cred,
93         int commandId,
94         const Name &name,
95         const Label &label);
96
97     RawBuffer getData(
98         const Credentials &cred,
99         int commandId,
100         DBDataType dataType,
101         const Name &name,
102         const Label &label,
103         const Password &password);
104
105     RawBuffer getPKCS12(
106         const Credentials &cred,
107         int commandId,
108         const Name &name,
109         const Label &label);
110
111     RawBuffer getDataList(
112         const Credentials &cred,
113         int commandId,
114         DBDataType dataType);
115
116     RawBuffer createKeyPair(
117         const Credentials &cred,
118         LogicCommand protocol_cmd,
119         int commandId,
120         const int additional_param,
121         const Name &namePrivate,
122         const Label &labelPrivate,
123         const Name &namePublic,
124         const Label &labelPublic,
125         const PolicySerializable &policyPrivate,
126         const PolicySerializable &policyPublic);
127
128     RawBuffer getCertificateChain(
129         const Credentials &cred,
130         int commandId,
131         const RawBuffer &certificate,
132         const RawBufferVector &untrustedCertificates);
133
134     RawBuffer getCertificateChain(
135         const Credentials &cred,
136         int commandId,
137         const RawBuffer &certificate,
138         const LabelNameVector &labelNameVector);
139
140     RawBuffer  createSignature(
141         const Credentials &cred,
142         int commandId,
143         const Name &privateKeyName,
144         const Label & ownerLabel,
145         const Password &password,           // password for private_key
146         const RawBuffer &message,
147         const HashAlgorithm hash,
148         const RSAPaddingAlgorithm padding);
149
150     RawBuffer verifySignature(
151         const Credentials &cred,
152         int commandId,
153         const Name &publicKeyOrCertName,
154         const Label &label,
155         const Password &password,           // password for public_key (optional)
156         const RawBuffer &message,
157         const RawBuffer &signature,
158         const HashAlgorithm hash,
159         const RSAPaddingAlgorithm padding);
160
161     RawBuffer updateCCMode();
162
163     RawBuffer setPermission(
164         const Credentials &cred,
165         int command,
166         int msgID,
167         const Name &name,
168         const Label &label,
169         const Label &accessor_label,
170         const Permission newPermission);
171
172 private:
173
174     int verifyBinaryData(
175         DBDataType dataType,
176         const RawBuffer &input_data) const;
177
178     int checkSaveConditions(
179         const Credentials &cred,
180         UserData &handler,
181         const Name &name,
182         const Label &label);
183
184     int saveDataHelper(
185         const Credentials &cred,
186         const Name &name,
187         const Label &label,
188         DBDataType dataType,
189         const RawBuffer &data,
190         const PolicySerializable &policy);
191
192     int saveDataHelper(
193         const Credentials &cred,
194         const Name &name,
195         const Label &label,
196         const PKCS12Serializable &pkcs,
197         const PolicySerializable &keyPolicy,
198         const PolicySerializable &certPolicy);
199
200     DBRow createEncryptedDBRow(
201         CryptoLogic &crypto,
202         const Name &name,
203         const Label &label,
204         DBDataType dataType,
205         const RawBuffer &data,
206         const Policy &policy) const;
207
208     int getPKCS12Helper(
209         const Credentials &cred,
210         const Name &name,
211         const Label &label,
212         KeyShPtr & privKey,
213         CertificateShPtr & cert,
214         CertificateShPtrVector & caChain);
215
216     int extractPKCS12Data(
217         CryptoLogic &crypto,
218         const Name &name,
219         const Label &ownerLabel,
220         const PKCS12Serializable &pkcs,
221         const PolicySerializable &keyPolicy,
222         const PolicySerializable &certPolicy,
223         DBRowVector &output) const;
224
225     int removeDataHelper(
226         const Credentials &cred,
227         const Name &name,
228         const Label &ownerLabel);
229
230     int readSingleRow(
231         const Name &name,
232         const Label &ownerLabel,
233         DBDataType dataType,
234         DBCrypto & database,
235         DBRow &row);
236
237     int readMultiRow(const Name &name,
238         const Label &ownerLabel,
239         DBDataType dataType,
240         DBCrypto & database,
241         DBRowVector &output);
242
243     int checkDataPermissionsHelper(
244         const Name &name,
245         const Label &ownerLabel,
246         const Label &accessorLabel,
247         const DBRow &row,
248         bool exportFlag,
249         DBCrypto & database);
250
251     int readDataHelper(
252         bool exportFlag,
253         const Credentials &cred,
254         DBDataType dataType,
255         const Name &name,
256         const Label &label,
257         const Password &password,
258         DBRow &row);
259
260     int readDataHelper(
261         bool exportFlag,
262         const Credentials &cred,
263         DBDataType dataType,
264         const Name &name,
265         const Label &label,
266         const Password &password,
267         DBRowVector &rows);
268
269     int createKeyPairHelper(
270         const Credentials &cred,
271         const KeyType key_type,
272         const int additional_param,
273         const Name &namePrivate,
274         const Label &labelPrivate,
275         const Name &namePublic,
276         const Label &labelPublic,
277         const PolicySerializable &policyPrivate,
278         const PolicySerializable &policyPublic);
279
280     int getCertificateChainHelper(
281         const Credentials &cred,
282         const RawBuffer &certificate,
283         const LabelNameVector &labelNameVector,
284         RawBufferVector & chainRawVector);
285
286     int setPermissionHelper(
287         const Credentials &cred,
288         const Name &name,
289         const Label &ownerLabel,
290         const Label &accessorLabel,
291         const Permission newPermission);
292
293
294     std::map<uid_t, UserData> m_userDataMap;
295     CertificateStore m_certStore;
296     AccessControl m_accessControl;
297     //FileLock m_lock;
298 };
299
300 } // namespace CKM
301