d724001e2b5bb386a1ceb6a9d1d2d9cc6ad90059
[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(int data)
53   : m_dataType(static_cast<Type>(data))
54 {
55     if (!isInRange(data))
56         ThrowMsg(Exception::OutOfRange, "Invalid conversion from int to DBDataType");
57 }
58
59 DataType::operator int () const {
60     return static_cast<int>(m_dataType);
61 }
62
63 DataType::operator KeyType () const {
64     switch(m_dataType) {
65     case DataType::KEY_RSA_PUBLIC: return KeyType::KEY_RSA_PUBLIC;
66     case DataType::KEY_RSA_PRIVATE: return KeyType::KEY_RSA_PRIVATE;
67     case DataType::KEY_DSA_PUBLIC: return KeyType::KEY_DSA_PUBLIC;
68     case DataType::KEY_DSA_PRIVATE: return KeyType::KEY_DSA_PRIVATE;
69     case DataType::KEY_ECDSA_PRIVATE: return KeyType::KEY_ECDSA_PRIVATE;
70     case DataType::KEY_ECDSA_PUBLIC: return KeyType::KEY_ECDSA_PUBLIC;
71     case DataType::KEY_AES: return KeyType::KEY_AES;
72     default:
73         ThrowMsg(Exception::OutOfRange, "Invalid conversion from DBDataType to KeyType");
74     }
75 }
76
77 bool DataType::operator==(const DataType &second) const {
78     return m_dataType == second.m_dataType;
79 }
80
81 bool DataType::isKey() const {
82     if (DB_KEY_FIRST <= m_dataType && DB_KEY_LAST >= m_dataType)
83         return true;
84     return false;
85 }
86
87 bool DataType::isChainCert() const {
88     if (DB_CHAIN_FIRST <= m_dataType && DB_CHAIN_LAST >= m_dataType)
89         return true;
90     return false;
91 }
92
93 bool DataType::isKeyPrivate() const {
94     switch (m_dataType) {
95     case KEY_RSA_PRIVATE:
96     case KEY_DSA_PRIVATE:
97     case KEY_ECDSA_PRIVATE:
98           return true;
99     default:
100           return false;
101     }
102 }
103
104 bool DataType::isKeyPublic() const {
105     switch (m_dataType) {
106     case KEY_RSA_PUBLIC:
107     case KEY_DSA_PUBLIC:
108     case KEY_ECDSA_PUBLIC:
109           return true;
110     default:
111           return false;
112     }
113 }
114
115 bool DataType::isCertificate() const {
116     return m_dataType == CERTIFICATE;
117 }
118
119 bool DataType::isBinaryData() const {
120     return m_dataType == BINARY_DATA;
121 }
122
123 bool DataType::isInRange(int data) {
124     if (data < static_cast<int>(DB_FIRST))
125         return false;
126     if (data > static_cast<int>(DB_LAST))
127         return false;
128     return true;
129 }
130
131 DataType DataType::getChainDatatype(unsigned int index)
132 {
133     DataType result(static_cast<int>(index) + DB_CHAIN_FIRST);
134
135     if ( !result.isChainCert() )
136         ThrowMsg(Exception::OutOfRange, "Certificate number is out of range");
137
138     return result;
139 }
140
141 } // namespace CKM