summaryrefslogtreecommitdiff
path: root/src/manager/common/data-type.cpp
blob: ba1a6b2aa48f5d2e3ca1656f191c80b055a0457c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file       data-type.cpp
 * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
 * @version    1.0
 */

#include <data-type.h>

namespace CKM {

DataType::DataType() :
	m_dataType(BINARY_DATA)
{
}

DataType::DataType(Type data)
	: m_dataType(data)
{
	if (!isInRange(data))
		ThrowMsg(Exception::OutOfRange,
				 "Invalid conversion from DataType=" << static_cast<int>(data) <<
				 " to DBDataType");
}

DataType::DataType(KeyType key)
{
	switch (key) {
	case KeyType::KEY_RSA_PUBLIC:
		m_dataType = DataType::KEY_RSA_PUBLIC;
		break;

	case KeyType::KEY_RSA_PRIVATE:
		m_dataType = DataType::KEY_RSA_PRIVATE;
		break;

	case KeyType::KEY_DSA_PUBLIC:
		m_dataType = DataType::KEY_DSA_PUBLIC;
		break;

	case KeyType::KEY_DSA_PRIVATE:
		m_dataType = DataType::KEY_DSA_PRIVATE;
		break;

	case KeyType::KEY_ECDSA_PUBLIC:
		m_dataType = DataType::KEY_ECDSA_PUBLIC;
		break;

	case KeyType::KEY_ECDSA_PRIVATE:
		m_dataType = DataType::KEY_ECDSA_PRIVATE;
		break;

	case KeyType::KEY_AES:
		m_dataType = DataType::KEY_AES;
		break;

	default:
		ThrowMsg(Exception::OutOfRange,
				 "Invalid conversion from KeyType=" << static_cast<int>(key) <<
				 " to DBDataType");
	}
}

DataType::DataType(AlgoType algorithmType)
{
	switch (algorithmType) {
	case AlgoType::AES_CTR:
	case AlgoType::AES_CBC:
	case AlgoType::AES_GCM:
	case AlgoType::AES_CFB:
	case AlgoType::AES_GEN:
		m_dataType = DataType::KEY_AES;
		break;

	case AlgoType::RSA_SV:
	case AlgoType::RSA_OAEP:
	case AlgoType::RSA_GEN:
		m_dataType = DataType::KEY_RSA_PUBLIC;
		break;

	case AlgoType::DSA_SV:
	case AlgoType::DSA_GEN:
		m_dataType = DataType::KEY_DSA_PUBLIC;
		break;

	case AlgoType::ECDSA_SV:
	case AlgoType::ECDSA_GEN:
		m_dataType = DataType::KEY_ECDSA_PUBLIC;
		break;

	default:
		ThrowMsg(Exception::OutOfRange,
				 "Invalid conversion from AlgoType=" << static_cast<int>(algorithmType) <<
				 " to DBDataType");
	}
}

DataType::DataType(int data) :
	m_dataType(static_cast<Type>(data))
{
	if (!isInRange(data))
		ThrowMsg(Exception::OutOfRange,
				 "Invalid conversion from int=" << data << " to DBDataType");
}

DataType::operator int () const
{
	return static_cast<int>(m_dataType);
}

DataType::operator KeyType() const
{
	switch (m_dataType) {
	case DataType::KEY_RSA_PUBLIC:
		return KeyType::KEY_RSA_PUBLIC;

	case DataType::KEY_RSA_PRIVATE:
		return KeyType::KEY_RSA_PRIVATE;

	case DataType::KEY_DSA_PUBLIC:
		return KeyType::KEY_DSA_PUBLIC;

	case DataType::KEY_DSA_PRIVATE:
		return KeyType::KEY_DSA_PRIVATE;

	case DataType::KEY_ECDSA_PRIVATE:
		return KeyType::KEY_ECDSA_PRIVATE;

	case DataType::KEY_ECDSA_PUBLIC:
		return KeyType::KEY_ECDSA_PUBLIC;

	case DataType::KEY_AES:
		return KeyType::KEY_AES;

	default:
		ThrowMsg(Exception::OutOfRange,
				 "Invalid conversion from DBDataType=" << static_cast<int>(m_dataType) <<
				 " to KeyType");
	}
}

bool DataType::operator==(const DataType &second) const
{
	return m_dataType == second.m_dataType;
}

bool DataType::isKey() const
{
	if (DB_KEY_FIRST <= m_dataType && DB_KEY_LAST >= m_dataType)
		return true;

	return false;
}

bool DataType::isSKey() const
{
	return (KEY_AES == m_dataType);
}

bool DataType::isChainCert() const
{
	if (DB_CHAIN_FIRST <= m_dataType && DB_CHAIN_LAST >= m_dataType)
		return true;

	return false;
}

bool DataType::isKeyPrivate() const
{
	switch (m_dataType) {
	case KEY_RSA_PRIVATE:
	case KEY_DSA_PRIVATE:
	case KEY_ECDSA_PRIVATE:
		return true;

	default:
		return false;
	}
}

bool DataType::isKeyPublic() const
{
	switch (m_dataType) {
	case KEY_RSA_PUBLIC:
	case KEY_DSA_PUBLIC:
	case KEY_ECDSA_PUBLIC:
		return true;

	default:
		return false;
	}
}

bool DataType::isCertificate() const
{
	return m_dataType == CERTIFICATE;
}

bool DataType::isBinaryData() const
{
	return m_dataType == BINARY_DATA;
}

bool DataType::isInRange(int data)
{
	if (data < static_cast<int>(DB_FIRST))
		return false;

	if (data > static_cast<int>(DB_LAST))
		return false;

	return true;
}

DataType DataType::getChainDatatype(unsigned int index)
{
	DataType result(static_cast<int>(index) + DB_CHAIN_FIRST);

	if (!result.isChainCert())
		ThrowMsg(Exception::OutOfRange, "Certificate number is out of range");

	return result;
}

} // namespace CKM