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