Add PKCS12 support.
[platform/core/security/key-manager.git] / src / manager / common / protocols.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  * @file        protocols.h
17  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18  * @author      Zofia Abramowska (z.abramowska@samsung.com)
19  * @version     1.0
20  * @brief       This file contains list of all protocols suported by Central
21  *              Key Manager.
22  */
23 #pragma once
24
25 #include <stdexcept>
26 #include <string>
27
28 #include <ckm/ckm-type.h>
29 #include <pkcs12-impl.h>
30
31 #include <dpl/exception.h>
32 #include <dpl/serialization.h>
33
34 namespace CKM {
35
36 extern char const * const SERVICE_SOCKET_ECHO;
37 extern char const * const SERVICE_SOCKET_CKM_CONTROL;
38 extern char const * const SERVICE_SOCKET_CKM_STORAGE;
39 extern char const * const SERVICE_SOCKET_OCSP;
40
41 enum class ControlCommand : int {
42     UNLOCK_USER_KEY,
43     LOCK_USER_KEY,
44     REMOVE_USER_DATA,
45     CHANGE_USER_PASSWORD,
46     RESET_USER_PASSWORD,
47     REMOVE_APP_DATA,
48     UPDATE_CC_MODE,
49     SET_PERMISSION
50     // for backward compatibility append new at the end
51 };
52
53 enum class LogicCommand : int {
54     GET,
55     GET_LIST,
56     SAVE,
57     REMOVE,
58     CREATE_KEY_PAIR_RSA,
59     CREATE_KEY_PAIR_ECDSA,
60     GET_CHAIN_CERT,
61     GET_CHAIN_ALIAS,
62     CREATE_SIGNATURE,
63     VERIFY_SIGNATURE,
64     CREATE_KEY_PAIR_DSA,
65     SET_PERMISSION,
66     SAVE_PKCS12,
67     GET_PKCS12
68     // for backward compatibility append new at the end
69 };
70
71 class DBDataType {
72 public:
73     class Exception {
74     public:
75         DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
76         DECLARE_EXCEPTION_TYPE(Base, OutOfRange)
77     };
78
79     enum DataType {
80         KEY_RSA_PUBLIC,
81         KEY_RSA_PRIVATE,
82         KEY_ECDSA_PUBLIC,
83         KEY_ECDSA_PRIVATE,
84         KEY_DSA_PUBLIC,
85         KEY_DSA_PRIVATE,
86         KEY_AES,
87         CERTIFICATE,
88         BINARY_DATA,
89
90         CHAIN_CERT_0,
91         CHAIN_CERT_1,
92         CHAIN_CERT_2,
93         CHAIN_CERT_3,
94         CHAIN_CERT_4,
95         CHAIN_CERT_5,
96         CHAIN_CERT_6,
97         CHAIN_CERT_7,
98         CHAIN_CERT_8,
99         CHAIN_CERT_9,
100         CHAIN_CERT_10,
101         CHAIN_CERT_11,
102         CHAIN_CERT_12,
103         CHAIN_CERT_13,
104         CHAIN_CERT_14,
105         CHAIN_CERT_15,
106
107         // Special types to support database,
108         DB_KEY_FIRST = KEY_RSA_PUBLIC,
109         DB_KEY_LAST  = KEY_AES,
110         DB_CHAIN_FIRST = CHAIN_CERT_0,
111         DB_CHAIN_LAST = CHAIN_CERT_15,
112         DB_FIRST = KEY_RSA_PUBLIC,
113         DB_LAST  = CHAIN_CERT_15,
114     };
115
116     DBDataType()
117       : m_dataType(BINARY_DATA)
118     {}
119
120     DBDataType(DataType data)
121       : m_dataType(data)
122     {
123         if (!isInRange(data))
124             ThrowMsg(Exception::OutOfRange, "Invalid conversion from DataType to DBDataType");
125     }
126
127     explicit DBDataType(KeyType key) {
128         switch(key) {
129         case KeyType::KEY_RSA_PUBLIC:    m_dataType = DBDataType::KEY_RSA_PUBLIC;    break;
130         case KeyType::KEY_RSA_PRIVATE:   m_dataType = DBDataType::KEY_RSA_PRIVATE;   break;
131         case KeyType::KEY_DSA_PUBLIC:    m_dataType = DBDataType::KEY_DSA_PUBLIC;    break;
132         case KeyType::KEY_DSA_PRIVATE:   m_dataType = DBDataType::KEY_DSA_PRIVATE;   break;
133         case KeyType::KEY_ECDSA_PUBLIC:  m_dataType = DBDataType::KEY_ECDSA_PUBLIC;  break;
134         case KeyType::KEY_ECDSA_PRIVATE: m_dataType = DBDataType::KEY_ECDSA_PRIVATE; break;
135         case KeyType::KEY_AES:           m_dataType = DBDataType::KEY_AES;           break;
136         default:
137             ThrowMsg(Exception::OutOfRange, "Invalid conversion from KeyType to DBDataType");
138         }
139     }
140
141     explicit DBDataType(int data)
142       : m_dataType(static_cast<DataType>(data))
143     {
144         if (!isInRange(data))
145             ThrowMsg(Exception::OutOfRange, "Invalid conversion from int to DBDataType");
146     }
147
148     DBDataType(const DBDataType &) = default;
149     DBDataType& operator=(const DBDataType &) = default;
150
151     operator int () const {
152         return static_cast<int>(m_dataType);
153     }
154
155     operator KeyType () const {
156         switch(m_dataType) {
157         case DBDataType::KEY_RSA_PUBLIC: return KeyType::KEY_RSA_PUBLIC;
158         case DBDataType::KEY_RSA_PRIVATE: return KeyType::KEY_RSA_PRIVATE;
159         case DBDataType::KEY_DSA_PUBLIC: return KeyType::KEY_DSA_PUBLIC;
160         case DBDataType::KEY_DSA_PRIVATE: return KeyType::KEY_DSA_PRIVATE;
161         case DBDataType::KEY_ECDSA_PRIVATE: return KeyType::KEY_ECDSA_PRIVATE;
162         case DBDataType::KEY_ECDSA_PUBLIC: return KeyType::KEY_ECDSA_PUBLIC;
163         case DBDataType::KEY_AES: return KeyType::KEY_AES;
164         default:
165             ThrowMsg(Exception::OutOfRange, "Invalid conversion from DBDataType to KeyType");
166         }
167     }
168
169     bool operator==(const DBDataType &second) const {
170         return m_dataType == second.m_dataType;
171     }
172
173     bool isKey() const {
174         if (DB_KEY_FIRST <= m_dataType && DB_KEY_LAST >= m_dataType)
175             return true;
176         return false;
177     }
178
179     bool isChainCert() const {
180         if (DB_CHAIN_FIRST <= m_dataType && DB_CHAIN_LAST >= m_dataType)
181             return true;
182         return false;
183     }
184
185     static DBDataType getChainDatatype(unsigned int index)
186     {
187         DBDataType result(static_cast<int>(index) + DB_CHAIN_FIRST);
188
189         if ( !result.isChainCert() )
190             ThrowMsg(Exception::OutOfRange, "Certificate number is out of range");
191
192         return result;
193     }
194
195     bool isKeyPrivate() const {
196         switch (m_dataType) {
197         case KEY_RSA_PRIVATE:
198         case KEY_DSA_PRIVATE:
199         case KEY_ECDSA_PRIVATE:
200               return true;
201         default:
202               return false;
203         }
204     }
205
206     bool isKeyPublic() const {
207         switch (m_dataType) {
208         case KEY_RSA_PUBLIC:
209         case KEY_DSA_PUBLIC:
210         case KEY_ECDSA_PUBLIC:
211               return true;
212         default:
213               return false;
214         }
215     }
216
217     bool isCertificate() const {
218         return m_dataType == CERTIFICATE;
219     }
220
221     bool isBinaryData() const {
222         return m_dataType == BINARY_DATA;
223     }
224
225     static bool isInRange(int data) {
226         if (data < static_cast<int>(DB_FIRST))
227             return false;
228         if (data > static_cast<int>(DB_LAST))
229             return false;
230         return true;
231     }
232
233     // it's not virtual with a reason!
234     ~DBDataType(){}
235
236 private:
237     DataType m_dataType;
238 };
239
240 // (client side) Alias = (service side) Label::Name
241 extern char const * const LABEL_NAME_SEPARATOR;
242 typedef std::string Name;
243 typedef std::vector<std::pair<Label, Name> > LabelNameVector;
244
245
246 const char* toDBPermission(Permission access_right_type);
247 Permission toPermission(const std::string &input_DB_data);
248
249 class IStream;
250
251 struct PolicySerializable : public Policy, ISerializable {
252     PolicySerializable() {};
253     explicit PolicySerializable(const Policy &policy) : Policy(policy) {}
254     explicit PolicySerializable(IStream &stream) {
255         Deserialization::Deserialize(stream, password);
256         Deserialization::Deserialize(stream, extractable);
257     }
258     void Serialize(IStream &stream) const {
259         Serialization::Serialize(stream, password);
260         Serialization::Serialize(stream, extractable);
261     }
262 };
263
264 struct PKCS12Serializable : public PKCS12Impl, ISerializable {
265     PKCS12Serializable();
266     explicit PKCS12Serializable(const PKCS12 &);
267     explicit PKCS12Serializable(IStream &);
268     PKCS12Serializable(
269             const KeyShPtr &privKey,
270             const CertificateShPtr &cert,
271             const CertificateShPtrVector &chainCerts);
272     void Serialize(IStream &) const;
273 };
274
275 } // namespace CKM
276