Add algorithm param validation
[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,
36                  "Invalid conversion from DataType=" << static_cast<int>(data) << " to DBDataType");
37 }
38
39 DataType::DataType(KeyType key) {
40     switch(key) {
41     case KeyType::KEY_RSA_PUBLIC:    m_dataType = DataType::KEY_RSA_PUBLIC;    break;
42     case KeyType::KEY_RSA_PRIVATE:   m_dataType = DataType::KEY_RSA_PRIVATE;   break;
43     case KeyType::KEY_DSA_PUBLIC:    m_dataType = DataType::KEY_DSA_PUBLIC;    break;
44     case KeyType::KEY_DSA_PRIVATE:   m_dataType = DataType::KEY_DSA_PRIVATE;   break;
45     case KeyType::KEY_ECDSA_PUBLIC:  m_dataType = DataType::KEY_ECDSA_PUBLIC;  break;
46     case KeyType::KEY_ECDSA_PRIVATE: m_dataType = DataType::KEY_ECDSA_PRIVATE; break;
47     case KeyType::KEY_AES:           m_dataType = DataType::KEY_AES;           break;
48     default:
49         ThrowMsg(Exception::OutOfRange,
50                  "Invalid conversion from KeyType=" << static_cast<int>(key) << " to DBDataType");
51     }
52 }
53
54 DataType::DataType(AlgoType algorithmType) {
55     switch(algorithmType) {
56     case AlgoType::AES_CTR:
57     case AlgoType::AES_CBC:
58     case AlgoType::AES_GCM:
59     case AlgoType::AES_CFB:
60     case AlgoType::AES_GEN:         m_dataType = DataType::KEY_AES;             break;
61     case AlgoType::RSA_SV:
62     case AlgoType::RSA_OAEP:
63     case AlgoType::RSA_GEN:         m_dataType = DataType::KEY_RSA_PUBLIC;      break;
64     case AlgoType::DSA_SV:
65     case AlgoType::DSA_GEN:         m_dataType = DataType::KEY_DSA_PUBLIC;      break;
66     case AlgoType::ECDSA_SV:
67     case AlgoType::ECDSA_GEN:       m_dataType = DataType::KEY_ECDSA_PUBLIC;    break;
68     default:
69         ThrowMsg(Exception::OutOfRange,
70                  "Invalid conversion from AlgoType=" << static_cast<int>(algorithmType) <<
71                  " to DBDataType");
72     }
73 }
74
75 DataType::DataType(int data)
76   : m_dataType(static_cast<Type>(data))
77 {
78     if (!isInRange(data))
79         ThrowMsg(Exception::OutOfRange, "Invalid conversion from int=" << data << " to DBDataType");
80 }
81
82 DataType::operator int () const {
83     return static_cast<int>(m_dataType);
84 }
85
86 DataType::operator KeyType () const {
87     switch(m_dataType) {
88     case DataType::KEY_RSA_PUBLIC: return KeyType::KEY_RSA_PUBLIC;
89     case DataType::KEY_RSA_PRIVATE: return KeyType::KEY_RSA_PRIVATE;
90     case DataType::KEY_DSA_PUBLIC: return KeyType::KEY_DSA_PUBLIC;
91     case DataType::KEY_DSA_PRIVATE: return KeyType::KEY_DSA_PRIVATE;
92     case DataType::KEY_ECDSA_PRIVATE: return KeyType::KEY_ECDSA_PRIVATE;
93     case DataType::KEY_ECDSA_PUBLIC: return KeyType::KEY_ECDSA_PUBLIC;
94     case DataType::KEY_AES: return KeyType::KEY_AES;
95     default:
96         ThrowMsg(Exception::OutOfRange,
97                  "Invalid conversion from DBDataType=" << static_cast<int>(m_dataType) <<
98                  " to KeyType" );
99     }
100 }
101
102 bool DataType::operator==(const DataType &second) const {
103     return m_dataType == second.m_dataType;
104 }
105
106 bool DataType::isKey() const {
107     if (DB_KEY_FIRST <= m_dataType && DB_KEY_LAST >= m_dataType)
108         return true;
109     return false;
110 }
111
112 bool DataType::isSKey() const {
113     return (KEY_AES == m_dataType);
114 }
115
116 bool DataType::isChainCert() const {
117     if (DB_CHAIN_FIRST <= m_dataType && DB_CHAIN_LAST >= m_dataType)
118         return true;
119     return false;
120 }
121
122 bool DataType::isKeyPrivate() const {
123     switch (m_dataType) {
124     case KEY_RSA_PRIVATE:
125     case KEY_DSA_PRIVATE:
126     case KEY_ECDSA_PRIVATE:
127           return true;
128     default:
129           return false;
130     }
131 }
132
133 bool DataType::isKeyPublic() const {
134     switch (m_dataType) {
135     case KEY_RSA_PUBLIC:
136     case KEY_DSA_PUBLIC:
137     case KEY_ECDSA_PUBLIC:
138           return true;
139     default:
140           return false;
141     }
142 }
143
144 bool DataType::isCertificate() const {
145     return m_dataType == CERTIFICATE;
146 }
147
148 bool DataType::isBinaryData() const {
149     return m_dataType == BINARY_DATA;
150 }
151
152 bool DataType::isInRange(int data) {
153     if (data < static_cast<int>(DB_FIRST))
154         return false;
155     if (data > static_cast<int>(DB_LAST))
156         return false;
157     return true;
158 }
159
160 DataType DataType::getChainDatatype(unsigned int index)
161 {
162     DataType result(static_cast<int>(index) + DB_CHAIN_FIRST);
163
164     if ( !result.isChainCert() )
165         ThrowMsg(Exception::OutOfRange, "Certificate number is out of range");
166
167     return result;
168 }
169
170 } // namespace CKM