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