2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18 * @author Zofia Abramowska (z.abramowska@samsung.com)
20 * @brief This file contains list of all protocols suported by Central
28 #include <ckm/ckm-type.h>
29 #include <pkcs12-impl.h>
31 #include <dpl/exception.h>
32 #include <dpl/serialization.h>
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;
41 enum class ControlCommand : int {
50 // for backward compatibility append new at the end
53 enum class LogicCommand : int {
59 CREATE_KEY_PAIR_ECDSA,
68 // for backward compatibility append new at the end
75 DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
76 DECLARE_EXCEPTION_TYPE(Base, OutOfRange)
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,
117 : m_dataType(BINARY_DATA)
120 DBDataType(DataType data)
123 if (!isInRange(data))
124 ThrowMsg(Exception::OutOfRange, "Invalid conversion from DataType to DBDataType");
127 explicit DBDataType(KeyType 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;
137 ThrowMsg(Exception::OutOfRange, "Invalid conversion from KeyType to DBDataType");
141 explicit DBDataType(int data)
142 : m_dataType(static_cast<DataType>(data))
144 if (!isInRange(data))
145 ThrowMsg(Exception::OutOfRange, "Invalid conversion from int to DBDataType");
148 DBDataType(const DBDataType &) = default;
149 DBDataType& operator=(const DBDataType &) = default;
151 operator int () const {
152 return static_cast<int>(m_dataType);
155 operator KeyType () const {
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;
165 ThrowMsg(Exception::OutOfRange, "Invalid conversion from DBDataType to KeyType");
169 bool operator==(const DBDataType &second) const {
170 return m_dataType == second.m_dataType;
174 if (DB_KEY_FIRST <= m_dataType && DB_KEY_LAST >= m_dataType)
179 bool isChainCert() const {
180 if (DB_CHAIN_FIRST <= m_dataType && DB_CHAIN_LAST >= m_dataType)
185 static DBDataType getChainDatatype(unsigned int index)
187 DBDataType result(static_cast<int>(index) + DB_CHAIN_FIRST);
189 if ( !result.isChainCert() )
190 ThrowMsg(Exception::OutOfRange, "Certificate number is out of range");
195 bool isKeyPrivate() const {
196 switch (m_dataType) {
197 case KEY_RSA_PRIVATE:
198 case KEY_DSA_PRIVATE:
199 case KEY_ECDSA_PRIVATE:
206 bool isKeyPublic() const {
207 switch (m_dataType) {
210 case KEY_ECDSA_PUBLIC:
217 bool isCertificate() const {
218 return m_dataType == CERTIFICATE;
221 bool isBinaryData() const {
222 return m_dataType == BINARY_DATA;
225 static bool isInRange(int data) {
226 if (data < static_cast<int>(DB_FIRST))
228 if (data > static_cast<int>(DB_LAST))
233 // it's not virtual with a reason!
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;
246 const char* toDBPermission(Permission access_right_type);
247 Permission toPermission(const std::string &input_DB_data);
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);
258 void Serialize(IStream &stream) const {
259 Serialization::Serialize(stream, password);
260 Serialization::Serialize(stream, extractable);
264 struct PKCS12Serializable : public PKCS12Impl, ISerializable {
265 PKCS12Serializable();
266 explicit PKCS12Serializable(const PKCS12 &);
267 explicit PKCS12Serializable(IStream &);
269 const KeyShPtr &privKey,
270 const CertificateShPtr &cert,
271 const CertificateShPtrVector &chainCerts);
272 void Serialize(IStream &) const;