New DB layout: CKM_TABLE split into NAME_TABLE and OBJECT_TABLE.
[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     class Exception
49     {
50         public:
51             DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
52             DECLARE_EXCEPTION_TYPE(Base, InputDataInvalid);
53     };
54
55     CKMLogic();
56     CKMLogic(const CKMLogic &) = delete;
57     CKMLogic(CKMLogic &&) = delete;
58     CKMLogic& operator=(const CKMLogic &) = delete;
59     CKMLogic& operator=(CKMLogic &&) = delete;
60     virtual ~CKMLogic();
61
62     RawBuffer unlockUserKey(uid_t user, const Password &password);
63
64     RawBuffer lockUserKey(uid_t user);
65
66     RawBuffer removeUserData(uid_t user);
67
68     RawBuffer changeUserPassword(
69         uid_t user,
70         const Password &oldPassword,
71         const Password &newPassword);
72
73     RawBuffer resetUserPassword(
74         uid_t user,
75         const Password &newPassword);
76
77     RawBuffer removeApplicationData(
78         const Label &smackLabel);
79
80     RawBuffer saveData(
81         const Credentials &cred,
82         int commandId,
83         DBDataType dataType,
84         const Name &name,
85         const Label &label,
86         const RawBuffer &key,
87         const PolicySerializable &policy);
88
89     RawBuffer removeData(
90         const Credentials &cred,
91         int commandId,
92         DBDataType dataType,
93         const Name &name,
94         const Label &label);
95
96     RawBuffer getData(
97         const Credentials &cred,
98         int commandId,
99         DBDataType dataType,
100         const Name &name,
101         const Label &label,
102         const Password &password);
103
104     RawBuffer getDataList(
105         const Credentials &cred,
106         int commandId,
107         DBDataType dataType);
108
109     RawBuffer createKeyPair(
110         const Credentials &cred,
111         LogicCommand protocol_cmd,
112         int commandId,
113         const int additional_param,
114         const Name &namePrivate,
115         const Label &labelPrivate,
116         const Name &namePublic,
117         const Label &labelPublic,
118         const PolicySerializable &policyPrivate,
119         const PolicySerializable &policyPublic);
120
121     RawBuffer getCertificateChain(
122         const Credentials &cred,
123         int commandId,
124         const RawBuffer &certificate,
125         const RawBufferVector &untrustedCertificates);
126
127     RawBuffer getCertificateChain(
128         const Credentials &cred,
129         int commandId,
130         const RawBuffer &certificate,
131         const LabelNameVector &labelNameVector);
132
133     RawBuffer  createSignature(
134         const Credentials &cred,
135         int commandId,
136         const Name &privateKeyName,
137         const Label & ownerLabel,
138         const Password &password,           // password for private_key
139         const RawBuffer &message,
140         const HashAlgorithm hash,
141         const RSAPaddingAlgorithm padding);
142
143     RawBuffer verifySignature(
144         const Credentials &cred,
145         int commandId,
146         const Name &publicKeyOrCertName,
147         const Label &label,
148         const Password &password,           // password for public_key (optional)
149         const RawBuffer &message,
150         const RawBuffer &signature,
151         const HashAlgorithm hash,
152         const RSAPaddingAlgorithm padding);
153
154     RawBuffer updateCCMode();
155
156     RawBuffer setPermission(
157         const Credentials &cred,
158         int command,
159         int msgID,
160         const Name &name,
161         const Label &label,
162         const Label &accessor_label,
163         const Permission newPermission);
164
165 private:
166
167     void verifyBinaryData(
168         DBDataType dataType,
169         const RawBuffer &input_data) const;
170
171     int saveDataHelper(
172         const Credentials &cred,
173         DBDataType dataType,
174         const Name &name,
175         const Label &label,
176         const RawBuffer &key,
177         const PolicySerializable &policy);
178
179     int removeDataHelper(
180         const Credentials &cred,
181         const Name &name,
182         const Label &ownerLabel);
183
184     int readDataRowHelper(
185         const Name &name,
186         const Label &ownerLabel,
187         DBDataType dataType,
188         DBCrypto & database,
189         DBRow &row);
190
191     int checkDataPermissionsHelper(
192         const Name &name,
193         const Label &ownerLabel,
194         const Label &accessorLabel,
195         const DBRow &row,
196         bool exportFlag,
197         DBCrypto & database);
198
199     int readDataHelper(
200         bool exportFlag,
201         const Credentials &cred,
202         DBDataType dataType,
203         const Name &name,
204         const Label &label,
205         const Password &password,
206         DBRow &row);
207
208     int createKeyPairHelper(
209         const Credentials &cred,
210         const KeyType key_type,
211         const int additional_param,
212         const Name &namePrivate,
213         const Label &labelPrivate,
214         const Name &namePublic,
215         const Label &labelPublic,
216         const PolicySerializable &policyPrivate,
217         const PolicySerializable &policyPublic);
218
219     int getCertificateChainHelper(
220         const Credentials &cred,
221         const RawBuffer &certificate,
222         const LabelNameVector &labelNameVector,
223         RawBufferVector & chainRawVector);
224
225     int setPermissionHelper(
226         const Credentials &cred,
227         const Name &name,
228         const Label &ownerLabel,
229         const Label &accessorLabel,
230         const Permission newPermission);
231
232     std::map<uid_t, UserData> m_userDataMap;
233     CertificateStore m_certStore;
234     AccessControl m_accessControl;
235     //FileLock m_lock;
236 };
237
238 } // namespace CKM
239