isSKey mistake counter + a refactoring promise
[platform/core/security/key-manager.git] / src / manager / common / data-type.h
1 /*
2  *  Copyright (c) 2015 - 2020 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.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <ckm/ckm-type.h>
25 #include <exception.h>
26 #include <symbol-visibility.h>
27 #include <dpl/serialization.h>
28
29 namespace CKM {
30
31 class COMMON_API DataType final : public ISerializable {
32 public:
33         enum Type {
34                 KEY_RSA_PUBLIC,
35                 KEY_RSA_PRIVATE,
36                 KEY_ECDSA_PUBLIC,
37                 KEY_ECDSA_PRIVATE,
38                 KEY_DSA_PUBLIC,
39                 KEY_DSA_PRIVATE,
40                 KEY_AES,
41                 CERTIFICATE,
42                 BINARY_DATA,
43                 CHAIN_CERT_0,
44                 CHAIN_CERT_1,
45                 CHAIN_CERT_2,
46                 CHAIN_CERT_3,
47                 CHAIN_CERT_4,
48                 CHAIN_CERT_5,
49                 CHAIN_CERT_6,
50                 CHAIN_CERT_7,
51                 CHAIN_CERT_8,
52                 CHAIN_CERT_9,
53                 CHAIN_CERT_10,
54                 CHAIN_CERT_11,
55                 CHAIN_CERT_12,
56                 CHAIN_CERT_13,
57                 CHAIN_CERT_14,
58                 CHAIN_CERT_15,
59
60                 // Special types to support database,
61                 DB_KEY_FIRST = KEY_RSA_PUBLIC,
62                 DB_KEY_LAST  = KEY_AES,
63                 DB_CHAIN_FIRST = CHAIN_CERT_0,
64                 DB_CHAIN_LAST = CHAIN_CERT_15,
65                 DB_FIRST = KEY_RSA_PUBLIC,
66                 DB_LAST  = CHAIN_CERT_15,
67         };
68
69         DataType();
70         DataType(Type data);
71
72         explicit DataType(IStream &stream);
73         void Serialize(IStream &stream) const override;
74
75         explicit DataType(KeyType key);
76         DataType(const DataType &) = default;
77         DataType &operator=(const DataType &) = default;
78
79         operator int() const;
80
81         bool isKey() const;
82
83         /*
84          * Number of times someone mistook it for isKey() (or the opposite): 3
85          * Increase the counter if it happened to you.
86          * I will rename this function if the counter reaches 4.
87          */
88         bool isSKey() const;
89         bool isChainCert() const;
90         bool isKeyPrivate() const;
91         bool isKeyPublic() const;
92         bool isCertificate() const;
93         bool isBinaryData() const;
94         bool isEllipticCurve() const;
95
96         static DataType getChainDatatype(unsigned int index);
97
98 private:
99         void checkRange() const;
100
101         Type m_dataType;
102 };
103
104 } // namespace CKM