2 * Copyright (c) 2000 - 2019 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
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
22 #include <data-type.h>
23 #include <exception.h>
27 DataType::DataType() :
28 m_dataType(BINARY_DATA)
32 DataType::DataType(Type data)
36 ThrowErr(Exc::InputParam,
37 "Invalid conversion from DataType=", static_cast<int>(data),
41 DataType::DataType(KeyType key)
44 case KeyType::KEY_RSA_PUBLIC:
45 m_dataType = DataType::KEY_RSA_PUBLIC;
48 case KeyType::KEY_RSA_PRIVATE:
49 m_dataType = DataType::KEY_RSA_PRIVATE;
52 case KeyType::KEY_DSA_PUBLIC:
53 m_dataType = DataType::KEY_DSA_PUBLIC;
56 case KeyType::KEY_DSA_PRIVATE:
57 m_dataType = DataType::KEY_DSA_PRIVATE;
60 case KeyType::KEY_ECDSA_PUBLIC:
61 m_dataType = DataType::KEY_ECDSA_PUBLIC;
64 case KeyType::KEY_ECDSA_PRIVATE:
65 m_dataType = DataType::KEY_ECDSA_PRIVATE;
68 case KeyType::KEY_AES:
69 m_dataType = DataType::KEY_AES;
73 ThrowErr(Exc::InputParam,
74 "Invalid conversion from KeyType=", static_cast<int>(key),
79 DataType::DataType(int data) :
80 m_dataType(static_cast<Type>(data))
83 ThrowErr(Exc::InputParam, "Invalid conversion from int=", data, " to DBDataType");
86 DataType::operator int () const
88 return static_cast<int>(m_dataType);
91 DataType::operator KeyType() const
94 case DataType::KEY_RSA_PUBLIC:
95 return KeyType::KEY_RSA_PUBLIC;
97 case DataType::KEY_RSA_PRIVATE:
98 return KeyType::KEY_RSA_PRIVATE;
100 case DataType::KEY_DSA_PUBLIC:
101 return KeyType::KEY_DSA_PUBLIC;
103 case DataType::KEY_DSA_PRIVATE:
104 return KeyType::KEY_DSA_PRIVATE;
106 case DataType::KEY_ECDSA_PRIVATE:
107 return KeyType::KEY_ECDSA_PRIVATE;
109 case DataType::KEY_ECDSA_PUBLIC:
110 return KeyType::KEY_ECDSA_PUBLIC;
112 case DataType::KEY_AES:
113 return KeyType::KEY_AES;
116 ThrowErr(Exc::InputParam,
117 "Invalid conversion from DBDataType=", static_cast<int>(m_dataType),
122 bool DataType::operator==(const DataType &second) const
124 return m_dataType == second.m_dataType;
127 bool DataType::isKey() const
129 if (DB_KEY_FIRST <= m_dataType && DB_KEY_LAST >= m_dataType)
135 bool DataType::isSKey() const
137 return (KEY_AES == m_dataType);
140 bool DataType::isChainCert() const
142 if (DB_CHAIN_FIRST <= m_dataType && DB_CHAIN_LAST >= m_dataType)
148 bool DataType::isKeyPrivate() const
150 switch (m_dataType) {
151 case KEY_RSA_PRIVATE:
152 case KEY_DSA_PRIVATE:
153 case KEY_ECDSA_PRIVATE:
161 bool DataType::isKeyPublic() const
163 switch (m_dataType) {
166 case KEY_ECDSA_PUBLIC:
174 bool DataType::isCertificate() const
176 return m_dataType == CERTIFICATE;
179 bool DataType::isBinaryData() const
181 return m_dataType == BINARY_DATA;
184 bool DataType::isEllipticCurve() const
186 return (m_dataType == KEY_ECDSA_PRIVATE) || (m_dataType == KEY_ECDSA_PUBLIC);
189 bool DataType::isInRange(int data)
191 if (data < static_cast<int>(DB_FIRST))
194 if (data > static_cast<int>(DB_LAST))
200 DataType DataType::getChainDatatype(unsigned int index)
202 DataType result(static_cast<int>(index) + DB_CHAIN_FIRST);
204 if (!result.isChainCert())
205 ThrowErr(Exc::InputParam, "Certificate number is out of range");