e8cfb84b65f2cb1440424df67548bb2377936879
[platform/core/security/key-manager.git] / src / manager / common / data-type.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 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 /*
17  * @file       data-type.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <data-type.h>
23
24 namespace CKM
25 {
26
27 DataType::DataType()
28   : m_dataType(BINARY_DATA)
29 {}
30
31 DataType::DataType(Type data)
32   : m_dataType(data)
33 {
34     if (!isInRange(data))
35         ThrowMsg(Exception::OutOfRange, "Invalid conversion from DataType to DBDataType");
36 }
37
38 DataType::DataType(KeyType key) {
39     switch(key) {
40     case KeyType::KEY_RSA_PUBLIC:    m_dataType = DataType::KEY_RSA_PUBLIC;    break;
41     case KeyType::KEY_RSA_PRIVATE:   m_dataType = DataType::KEY_RSA_PRIVATE;   break;
42     case KeyType::KEY_DSA_PUBLIC:    m_dataType = DataType::KEY_DSA_PUBLIC;    break;
43     case KeyType::KEY_DSA_PRIVATE:   m_dataType = DataType::KEY_DSA_PRIVATE;   break;
44     case KeyType::KEY_ECDSA_PUBLIC:  m_dataType = DataType::KEY_ECDSA_PUBLIC;  break;
45     case KeyType::KEY_ECDSA_PRIVATE: m_dataType = DataType::KEY_ECDSA_PRIVATE; break;
46     case KeyType::KEY_AES:           m_dataType = DataType::KEY_AES;           break;
47     default:
48         ThrowMsg(Exception::OutOfRange, "Invalid conversion from KeyType to DBDataType");
49     }
50 }
51
52 DataType::DataType(AlgoType algorithmType) {
53     switch(algorithmType) {
54     case AlgoType::AES_CTR:
55     case AlgoType::AES_CBC:
56     case AlgoType::AES_GCM:
57     case AlgoType::AES_CFB:         m_dataType = DataType::KEY_AES;             break;
58     case AlgoType::RSA_GEN:         m_dataType = DataType::KEY_RSA_PUBLIC;      break;
59     case AlgoType::DSA_GEN:         m_dataType = DataType::KEY_DSA_PUBLIC;      break;
60     case AlgoType::ECDSA_GEN:       m_dataType = DataType::KEY_ECDSA_PUBLIC;    break;
61     default:
62         ThrowMsg(Exception::OutOfRange, "Invalid conversion from AlgoType to DBDataType");
63     }
64 }
65
66 DataType::DataType(int data)
67   : m_dataType(static_cast<Type>(data))
68 {
69     if (!isInRange(data))
70         ThrowMsg(Exception::OutOfRange, "Invalid conversion from int to DBDataType");
71 }
72
73 DataType::operator int () const {
74     return static_cast<int>(m_dataType);
75 }
76
77 DataType::operator KeyType () const {
78     switch(m_dataType) {
79     case DataType::KEY_RSA_PUBLIC: return KeyType::KEY_RSA_PUBLIC;
80     case DataType::KEY_RSA_PRIVATE: return KeyType::KEY_RSA_PRIVATE;
81     case DataType::KEY_DSA_PUBLIC: return KeyType::KEY_DSA_PUBLIC;
82     case DataType::KEY_DSA_PRIVATE: return KeyType::KEY_DSA_PRIVATE;
83     case DataType::KEY_ECDSA_PRIVATE: return KeyType::KEY_ECDSA_PRIVATE;
84     case DataType::KEY_ECDSA_PUBLIC: return KeyType::KEY_ECDSA_PUBLIC;
85     case DataType::KEY_AES: return KeyType::KEY_AES;
86     default:
87         ThrowMsg(Exception::OutOfRange, "Invalid conversion from DBDataType to KeyType");
88     }
89 }
90
91 bool DataType::operator==(const DataType &second) const {
92     return m_dataType == second.m_dataType;
93 }
94
95 bool DataType::isKey() const {
96     if (DB_KEY_FIRST <= m_dataType && DB_KEY_LAST >= m_dataType)
97         return true;
98     return false;
99 }
100
101 bool DataType::isSKey() const {
102     return (KEY_AES == m_dataType);
103 }
104
105 bool DataType::isChainCert() const {
106     if (DB_CHAIN_FIRST <= m_dataType && DB_CHAIN_LAST >= m_dataType)
107         return true;
108     return false;
109 }
110
111 bool DataType::isKeyPrivate() const {
112     switch (m_dataType) {
113     case KEY_RSA_PRIVATE:
114     case KEY_DSA_PRIVATE:
115     case KEY_ECDSA_PRIVATE:
116           return true;
117     default:
118           return false;
119     }
120 }
121
122 bool DataType::isKeyPublic() const {
123     switch (m_dataType) {
124     case KEY_RSA_PUBLIC:
125     case KEY_DSA_PUBLIC:
126     case KEY_ECDSA_PUBLIC:
127           return true;
128     default:
129           return false;
130     }
131 }
132
133 bool DataType::isCertificate() const {
134     return m_dataType == CERTIFICATE;
135 }
136
137 bool DataType::isBinaryData() const {
138     return m_dataType == BINARY_DATA;
139 }
140
141 bool DataType::isInRange(int data) {
142     if (data < static_cast<int>(DB_FIRST))
143         return false;
144     if (data > static_cast<int>(DB_LAST))
145         return false;
146     return true;
147 }
148
149 DataType DataType::getChainDatatype(unsigned int index)
150 {
151     DataType result(static_cast<int>(index) + DB_CHAIN_FIRST);
152
153     if ( !result.isChainCert() )
154         ThrowMsg(Exception::OutOfRange, "Certificate number is out of range");
155
156     return result;
157 }
158
159 } // namespace CKM