tz-backend: Implement asymmetric operations
[platform/core/security/key-manager.git] / src / manager / common / data-type.cpp
1 /*
2  *  Copyright (c) 2000 - 2019 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 #include <exception.h>
24
25 namespace CKM {
26
27 DataType::DataType() :
28         m_dataType(BINARY_DATA)
29 {
30 }
31
32 DataType::DataType(Type data)
33         : m_dataType(data)
34 {
35         if (!isInRange(data))
36                 ThrowErr(Exc::InputParam,
37                                  "Invalid conversion from DataType=", static_cast<int>(data),
38                                  " to DBDataType");
39 }
40
41 DataType::DataType(KeyType key)
42 {
43         switch (key) {
44         case KeyType::KEY_RSA_PUBLIC:
45                 m_dataType = DataType::KEY_RSA_PUBLIC;
46                 break;
47
48         case KeyType::KEY_RSA_PRIVATE:
49                 m_dataType = DataType::KEY_RSA_PRIVATE;
50                 break;
51
52         case KeyType::KEY_DSA_PUBLIC:
53                 m_dataType = DataType::KEY_DSA_PUBLIC;
54                 break;
55
56         case KeyType::KEY_DSA_PRIVATE:
57                 m_dataType = DataType::KEY_DSA_PRIVATE;
58                 break;
59
60         case KeyType::KEY_ECDSA_PUBLIC:
61                 m_dataType = DataType::KEY_ECDSA_PUBLIC;
62                 break;
63
64         case KeyType::KEY_ECDSA_PRIVATE:
65                 m_dataType = DataType::KEY_ECDSA_PRIVATE;
66                 break;
67
68         case KeyType::KEY_AES:
69                 m_dataType = DataType::KEY_AES;
70                 break;
71
72         default:
73                 ThrowErr(Exc::InputParam,
74                                  "Invalid conversion from KeyType=", static_cast<int>(key),
75                                  " to DBDataType");
76         }
77 }
78
79 DataType::DataType(int data) :
80         m_dataType(static_cast<Type>(data))
81 {
82         if (!isInRange(data))
83                 ThrowErr(Exc::InputParam, "Invalid conversion from int=", data, " to DBDataType");
84 }
85
86 DataType::operator int () const
87 {
88         return static_cast<int>(m_dataType);
89 }
90
91 DataType::operator KeyType() const
92 {
93         switch (m_dataType) {
94         case DataType::KEY_RSA_PUBLIC:
95                 return KeyType::KEY_RSA_PUBLIC;
96
97         case DataType::KEY_RSA_PRIVATE:
98                 return KeyType::KEY_RSA_PRIVATE;
99
100         case DataType::KEY_DSA_PUBLIC:
101                 return KeyType::KEY_DSA_PUBLIC;
102
103         case DataType::KEY_DSA_PRIVATE:
104                 return KeyType::KEY_DSA_PRIVATE;
105
106         case DataType::KEY_ECDSA_PRIVATE:
107                 return KeyType::KEY_ECDSA_PRIVATE;
108
109         case DataType::KEY_ECDSA_PUBLIC:
110                 return KeyType::KEY_ECDSA_PUBLIC;
111
112         case DataType::KEY_AES:
113                 return KeyType::KEY_AES;
114
115         default:
116                 ThrowErr(Exc::InputParam,
117                                  "Invalid conversion from DBDataType=", static_cast<int>(m_dataType),
118                                  " to KeyType");
119         }
120 }
121
122 bool DataType::operator==(const DataType &second) const
123 {
124         return m_dataType == second.m_dataType;
125 }
126
127 bool DataType::isKey() const
128 {
129         if (DB_KEY_FIRST <= m_dataType && DB_KEY_LAST >= m_dataType)
130                 return true;
131
132         return false;
133 }
134
135 bool DataType::isSKey() const
136 {
137         return (KEY_AES == m_dataType);
138 }
139
140 bool DataType::isChainCert() const
141 {
142         if (DB_CHAIN_FIRST <= m_dataType && DB_CHAIN_LAST >= m_dataType)
143                 return true;
144
145         return false;
146 }
147
148 bool DataType::isKeyPrivate() const
149 {
150         switch (m_dataType) {
151         case KEY_RSA_PRIVATE:
152         case KEY_DSA_PRIVATE:
153         case KEY_ECDSA_PRIVATE:
154                 return true;
155
156         default:
157                 return false;
158         }
159 }
160
161 bool DataType::isKeyPublic() const
162 {
163         switch (m_dataType) {
164         case KEY_RSA_PUBLIC:
165         case KEY_DSA_PUBLIC:
166         case KEY_ECDSA_PUBLIC:
167                 return true;
168
169         default:
170                 return false;
171         }
172 }
173
174 bool DataType::isCertificate() const
175 {
176         return m_dataType == CERTIFICATE;
177 }
178
179 bool DataType::isBinaryData() const
180 {
181         return m_dataType == BINARY_DATA;
182 }
183
184 bool DataType::isEllipticCurve() const
185 {
186         return (m_dataType == KEY_ECDSA_PRIVATE) || (m_dataType == KEY_ECDSA_PUBLIC);
187 }
188
189 bool DataType::isInRange(int data)
190 {
191         if (data < static_cast<int>(DB_FIRST))
192                 return false;
193
194         if (data > static_cast<int>(DB_LAST))
195                 return false;
196
197         return true;
198 }
199
200 DataType DataType::getChainDatatype(unsigned int index)
201 {
202         DataType result(static_cast<int>(index) + DB_CHAIN_FIRST);
203
204         if (!result.isChainCert())
205                 ThrowErr(Exc::InputParam, "Certificate number is out of range");
206
207         return result;
208 }
209
210 } // namespace CKM