Add file lock to key-manager server
[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
37 namespace CKM {
38
39 struct UserData {
40     KeyProvider    keyProvider;
41     DBCrypto       database;
42     CryptoLogic    crypto;
43 };
44
45 class CKMLogic {
46 public:
47     class Exception
48     {
49         public:
50             DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
51             DECLARE_EXCEPTION_TYPE(Base, InputDataInvalid);
52     };
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
63     RawBuffer lockUserKey(uid_t user);
64
65     RawBuffer removeUserData(uid_t user);
66
67     RawBuffer changeUserPassword(
68         uid_t user,
69         const Password &oldPassword,
70         const Password &newPassword);
71
72     RawBuffer resetUserPassword(
73         uid_t user,
74         const Password &newPassword);
75
76     RawBuffer removeApplicationData(const Label &smackLabel);
77
78     RawBuffer saveData(
79         Credentials &cred,
80         int commandId,
81         DBDataType dataType,
82         const Name &name,
83         const RawBuffer &key,
84         const PolicySerializable &policy);
85
86     RawBuffer removeData(
87         Credentials &cred,
88         int commandId,
89         DBDataType dataType,
90         const Name &name,
91         const Label &label);
92
93     RawBuffer getData(
94         Credentials &cred,
95         int commandId,
96         DBDataType dataType,
97         const Name &name,
98         const Label &label,
99         const Password &password);
100
101     RawBuffer getDataList(
102         Credentials &cred,
103         int commandId,
104         DBDataType dataType);
105
106     RawBuffer createKeyPair(
107         Credentials &cred,
108         LogicCommand protocol_cmd,
109         int commandId,
110         const int additional_param,
111         const Name &namePrivate,
112         const Name &namePublic,
113         const PolicySerializable &policyPrivate,
114         const PolicySerializable &policyPublic);
115
116     RawBuffer getCertificateChain(
117         Credentials &cred,
118         int commandId,
119         const RawBuffer &certificate,
120         const RawBufferVector &untrustedCertificates);
121
122     RawBuffer getCertificateChain(
123         Credentials &cred,
124         int commandId,
125         const RawBuffer &certificate,
126         const AliasVector &aliasVector);
127
128     RawBuffer  createSignature(
129         Credentials &cred,
130         int commandId,
131         const Name &privateKeyName,
132         const Label & ownerLabel,
133         const Password &password,           // password for private_key
134         const RawBuffer &message,
135         const HashAlgorithm hash,
136         const RSAPaddingAlgorithm padding);
137
138     RawBuffer verifySignature(
139         Credentials &cred,
140         int commandId,
141         const Name &publicKeyOrCertName,
142         const Label & ownerLabel,
143         const Password &password,           // password for public_key (optional)
144         const RawBuffer &message,
145         const RawBuffer &signature,
146         const HashAlgorithm hash,
147         const RSAPaddingAlgorithm padding);
148
149     RawBuffer updateCCMode();
150
151     RawBuffer allowAccess(
152         Credentials &cred,
153         int command,
154         int msgID,
155         const Name &name,
156         const Label &accessor_label,
157         const AccessRight req_rights);
158
159     RawBuffer denyAccess(
160         Credentials &cred,
161         int command,
162         int msgID,
163         const Name &name,
164         const Label &accessor_label);
165
166 private:
167
168     void verifyBinaryData(
169         DBDataType dataType,
170         const RawBuffer &input_data) const;
171
172     int saveDataHelper(
173         Credentials &cred,
174         DBDataType dataType,
175         const Name &name,
176         const RawBuffer &key,
177         const PolicySerializable &policy);
178
179     int getDataHelper(
180         Credentials &cred,
181         DBDataType dataType,
182         const Name &name,
183         const Label &label,
184         const Password &password,
185         DBRow &row);
186
187     int createKeyPairHelper(
188         Credentials &cred,
189         const KeyType key_type,
190         const int additional_param,
191         const Name &namePrivate,
192         const Name &namePublic,
193         const PolicySerializable &policyPrivate,
194         const PolicySerializable &policyPublic);
195
196     int getKeyHelper(
197         Credentials &cred,
198         const Name &publicKeyOrCertName,
199         const Password &password,           // password for public_key (optional)
200         const KeyImpl &genericKey);
201
202
203     // @return true if name & label are proper, false otherwise
204     static bool checkNameAndLabelValid(
205         const Name &name,
206         const Label &label);
207     void updateCCMode_internal();
208
209     std::map<uid_t, UserData> m_userDataMap;
210     CertificateStore m_certStore;
211     bool m_ccMode;
212     //FileLock m_lock;
213 };
214
215 } // namespace CKM
216