Release 0.1.53
[platform/core/security/key-manager.git] / misc / encryption_scheme / scheme-test.h
1 /*
2  *  Copyright (c) 2015 -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       scheme-test.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 #include <ckm/ckm-control.h>
29 #include <ckm/ckm-manager.h>
30
31 #include <data-type.h>
32
33 namespace CKM {
34 namespace DB {
35 class Crypto;
36 } // DB
37 } // CKM
38
39 struct Item {
40         Item() : type(CKM::DataType::DB_LAST)
41         {
42         }
43
44         Item(const CKM::Alias &alias,
45                  const CKM::DataType::Type type,
46                  const CKM::Policy &policy)
47                 : alias(alias), type(type), policy(policy)
48         {
49         }
50
51         CKM::Alias alias;
52         CKM::DataType::Type type;
53         CKM::Policy policy;
54 };
55
56 typedef std::vector<Item> Items;
57
58 struct ItemFilter {
59         ItemFilter() :
60                 typeFrom(CKM::DataType::DB_FIRST),
61                 typeTo(CKM::DataType::DB_LAST),
62                 exportableOnly(false),
63                 noPassword(false)
64         {
65         }
66
67         explicit ItemFilter(CKM::DataType::Type type) :
68                 typeFrom(type),
69                 typeTo(type),
70                 exportableOnly(false),
71                 noPassword(false)
72         {
73         }
74
75         ItemFilter(CKM::DataType::Type typeFrom, CKM::DataType::Type typeTo) :
76                 typeFrom(typeFrom),
77                 typeTo(typeTo),
78                 exportableOnly(false),
79                 noPassword(false)
80         {
81         }
82
83         bool Matches(const Item &item) const
84         {
85                 if (item.type < typeFrom || item.type > typeTo)
86                         return false;
87
88                 if (exportableOnly && !item.policy.extractable)
89                         return false;
90
91                 if (noPassword && !item.policy.password.empty())
92                         return false;
93
94                 return true;
95         }
96
97         CKM::DataType::Type typeFrom;
98         CKM::DataType::Type typeTo;
99         bool exportableOnly;
100         bool noPassword;
101 };
102
103 class SchemeTest {
104 public:
105         SchemeTest();
106         ~SchemeTest();
107
108         void CheckAliasInfo();
109         void RemoveUserData();
110         void FillDb();
111         void ReadAll(bool useWrongPass = false);
112         void SignVerify();
113         void EncryptDecrypt();
114         void CreateChain();
115         void RemoveAll();
116         size_t CountObjects();
117         void RestoreDb();
118         void CheckSchemeVersion(const ItemFilter &filter, int version);
119
120 private:
121         void SwitchToUser();
122         void SwitchToRoot();
123         void EnableDirectDbAccess();
124         void SignVerifyItem(const Item &itemPrv, const Item &itemPub);
125         void EncryptDecryptItem(const Item &item);
126         void EncryptDecryptItem(const Item &itemPrv, const Item &itemPub);
127         void CreateChainItem(const Item &leaf, const Items &certs);
128
129         CKM::ControlShPtr m_control;
130         CKM::ManagerShPtr m_mgr;
131         std::string m_origLabel;
132         bool m_userChanged;
133
134         std::unique_ptr<CKM::DB::Crypto> m_db;
135         bool m_directAccessEnabled;
136 };