Fix build break with boost 1.71.0
[platform/core/security/key-manager.git] / tests / test_key-provider.cpp
1 /*
2  *  Copyright (c) 2016 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Kyungwook Tak <k.tak@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  *
18  * @file        test_key-provider.cpp
19  * @author      Kyungwook Tak (k.tak@samsung.com)
20  * @version
21  * @brief
22  */
23 #define BOOST_TEST_MODULE KEY_MANAGER_TEST
24 #include <boost/test/unit_test.hpp>
25 #include <exception.h>
26 #include <key-provider.h>
27 #include <test_common.h>
28 #include <iostream>
29
30 const CKM::Password PASSWORD = "12345TIZEN12345AAAAAAAAA";
31 const CKM::Password INCORRECT_PASSWORD = "AAAAAAAAAAAAAAAAAAAAA";
32 const CKM::Password NEW_PASSWORD = "NEW12345TIZEN12345NEW";
33
34 const std::string USERNAME_SHORT = "AB";
35 const std::string USERNAME_LONG = "SOFTWARE_CENTER_SYSTEM_SW_LAB";
36 const std::string CLIENT_ID_1 = "SAMPLE_CLIENT_ID_1";
37 const std::string CLIENT_ID_2 = "SAMPLE_CLIENT_ID_2";
38
39 extern bool isLibInitialized;
40
41 BOOST_AUTO_TEST_SUITE(KEY_PROVIDER_TEST)
42 BOOST_AUTO_TEST_CASE(KeyDomainKEK)
43 {
44         BOOST_REQUIRE_MESSAGE(isLibInitialized,
45                                                   "Library is not initialized!");
46         CKM::KeyProvider keyProvider;
47         CKM::RawBuffer rb_test;
48         BOOST_REQUIRE_NO_THROW(rb_test =
49                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
50         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
51         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
52                                                   "KeyProvider created, but uninitialized");
53 }
54
55 BOOST_AUTO_TEST_CASE(KeyDomainKekInvalidPassword)
56 {
57         BOOST_REQUIRE_MESSAGE(isLibInitialized,
58                                                   "Library is not initialized!");
59         CKM::KeyProvider keyProvider;
60         CKM::RawBuffer rb_test;
61         BOOST_REQUIRE_NO_THROW(rb_test =
62                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
63         BOOST_REQUIRE_THROW(keyProvider = CKM::KeyProvider(rb_test, INCORRECT_PASSWORD),
64                                                 CKM::Exc::AuthenticationFailed);
65         BOOST_REQUIRE_MESSAGE(!keyProvider.isInitialized(),
66                                                   "KeyProvider not created, but initialized");
67 }
68
69 BOOST_AUTO_TEST_CASE(KeygetPureDomainKEK)
70 {
71         BOOST_REQUIRE_MESSAGE(isLibInitialized,
72                                                   "Library is not initialized!");
73         CKM::KeyProvider keyProvider;
74         CKM::RawBuffer rb_test;
75         BOOST_REQUIRE_NO_THROW(rb_test =
76                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
77         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
78         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
79                                                   "KeyProvider created, but uninitialized");
80         BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getPureDomainKEK());
81 }
82
83 BOOST_AUTO_TEST_CASE(KeyGetWrappedDomainKEK)
84 {
85         BOOST_REQUIRE_MESSAGE(isLibInitialized,
86                                                   "Library is not initialized!");
87         CKM::KeyProvider keyProvider;
88         CKM::RawBuffer rb_test;
89         BOOST_REQUIRE_NO_THROW(rb_test =
90                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
91         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
92         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
93                                                   "KeyProvider created, but uninitialized");
94         BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getWrappedDomainKEK(PASSWORD));
95 }
96
97 BOOST_AUTO_TEST_CASE(KeyGenerateDEK)
98 {
99         BOOST_REQUIRE_MESSAGE(isLibInitialized,
100                                                   "Library is not initialized!");
101         CKM::KeyProvider keyProvider;
102         CKM::RawBuffer rb_test;
103         CKM::RawBuffer rb_DEK1;
104         BOOST_REQUIRE_NO_THROW(rb_test =
105                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
106         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
107         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
108                                                   "KeyProvider created, but uninitialized");
109         BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(CLIENT_ID_1));
110 }
111
112 BOOST_AUTO_TEST_CASE(KeyGetPureDEK)
113 {
114         BOOST_REQUIRE_MESSAGE(isLibInitialized,
115                                                   "Library is not initialized!");
116         CKM::KeyProvider keyProvider;
117         CKM::RawBuffer rb_pureDEK1;
118         CKM::RawBuffer rb_DEK1;
119         CKM::RawBuffer rb_test;
120         BOOST_REQUIRE_NO_THROW(rb_test =
121                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
122         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
123         BOOST_REQUIRE_MESSAGE(keyProvider.isInitialized(),
124                                                   "KeyProvider created, but uninitialized");
125         BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(CLIENT_ID_1));
126         BOOST_REQUIRE_NO_THROW(rb_pureDEK1 = keyProvider.getPureDEK(rb_DEK1));
127 }
128
129 BOOST_AUTO_TEST_CASE(KeyReencrypt)
130 {
131         BOOST_REQUIRE_MESSAGE(isLibInitialized,
132                                                   "Library is not initialized!");
133         CKM::RawBuffer rb_test;
134         BOOST_REQUIRE_NO_THROW(rb_test =
135                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
136         BOOST_REQUIRE_NO_THROW(CKM::KeyProvider::reencrypt(rb_test, PASSWORD,
137                                                    NEW_PASSWORD));
138 }
139
140 BOOST_AUTO_TEST_CASE(KeyReencrypt_incorrect_password)
141 {
142         BOOST_REQUIRE_MESSAGE(isLibInitialized,
143                                                   "Library is not initialized!");
144         CKM::RawBuffer rb_test;
145         BOOST_REQUIRE_NO_THROW(rb_test =
146                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
147         BOOST_REQUIRE_THROW((rb_test = CKM::KeyProvider::reencrypt(rb_test,
148                                                                    INCORRECT_PASSWORD,
149                                                                    NEW_PASSWORD)), CKM::Exc::AuthenticationFailed);
150 }
151
152 BOOST_AUTO_TEST_CASE(KeyGetPureDEK_after_reencrypt)
153 {
154         BOOST_REQUIRE_MESSAGE(isLibInitialized,
155                                                   "Library is not initialized!");
156         CKM::KeyProvider keyProvider;
157         CKM::RawBuffer rb_DEK1;
158         CKM::RawBuffer rb_test;
159         BOOST_REQUIRE_NO_THROW(rb_test =
160                                                            CKM::KeyProvider::generateDomainKEK(USERNAME_LONG, PASSWORD));
161         BOOST_REQUIRE_NO_THROW(keyProvider = CKM::KeyProvider(rb_test, PASSWORD));
162         BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(CLIENT_ID_1));
163         BOOST_REQUIRE_NO_THROW(keyProvider.getPureDEK(rb_DEK1));
164 }
165
166 BOOST_AUTO_TEST_CASE(wrapped_container)
167 {
168         CKM::WrappedKeyAndInfoContainer wrappedContainer;
169
170         auto salt = createRandom(20);
171         BOOST_REQUIRE_NO_THROW(wrappedContainer.setKeyInfoSalt(salt.data(), salt.size()));
172         BOOST_REQUIRE_NO_THROW(wrappedContainer.setKeyInfoClient("key_info_client"));
173
174         BOOST_REQUIRE_THROW(wrappedContainer.setKeyInfoClient("key_info_client_waaaaay_too_long"),
175                             CKM::Exc::InternalError);
176
177         CKM::WrappedKeyAndInfoContainer wrappedContainer2;
178         BOOST_REQUIRE_NO_THROW(
179                 wrappedContainer2.setKeyInfo(&wrappedContainer.getWrappedKeyAndInfo().keyInfo));
180
181         BOOST_REQUIRE(
182                 wrappedContainer.getWrappedKeyAndInfo().keyInfo.keyLength ==
183                         wrappedContainer2.getWrappedKeyAndInfo().keyInfo.keyLength);
184         BOOST_REQUIRE(memcmp(
185                 wrappedContainer.getWrappedKeyAndInfo().keyInfo.salt,
186                 wrappedContainer2.getWrappedKeyAndInfo().keyInfo.salt,
187                 sizeof(wrappedContainer.getWrappedKeyAndInfo().keyInfo.salt)) == 0);
188         BOOST_REQUIRE(memcmp(
189                 wrappedContainer.getWrappedKeyAndInfo().keyInfo.client,
190                 wrappedContainer2.getWrappedKeyAndInfo().keyInfo.client,
191                 sizeof(wrappedContainer.getWrappedKeyAndInfo().keyInfo.client)) == 0);
192
193         CKM::WrappedKeyAndInfo wrapped3;
194         wrapped3.keyInfo.keyLength = MAX_WRAPPED_KEY_SIZE;
195         BOOST_REQUIRE_NO_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3(
196                 reinterpret_cast<unsigned char*>(&wrapped3)));
197
198         wrapped3.keyInfo.keyLength++;
199         BOOST_REQUIRE_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3(
200                 reinterpret_cast<unsigned char*>(&wrapped3)),
201                             CKM::Exc::InternalError);
202
203         // missing NULL termination in wrapped4.keyInfo.client
204         CKM::WrappedKeyAndInfo wrapped4;
205         memset(&wrapped4, 0x01, sizeof(CKM::WrappedKeyAndInfo));
206         BOOST_REQUIRE_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3(
207                         reinterpret_cast<unsigned char*>(&wrapped4)),
208                                     CKM::Exc::InternalError);
209 }
210
211 BOOST_AUTO_TEST_CASE(container)
212 {
213         CKM::KeyAndInfoContainer container;
214         BOOST_REQUIRE_NO_THROW(container.setKeyInfoKeyLength(10));
215
216         CKM::KeyAndInfoContainer container2;
217         BOOST_REQUIRE_NO_THROW(container2.setKeyInfo(&container.getKeyAndInfo().keyInfo));
218
219         BOOST_REQUIRE(
220                 container.getKeyAndInfo().keyInfo.keyLength ==
221                         container2.getKeyAndInfo().keyInfo.keyLength);
222 }
223
224 BOOST_AUTO_TEST_CASE(moves)
225 {
226         CKM::KeyProvider provider;
227
228         try {
229                 CKM::KeyProvider provider2(std::move(provider));
230                 CKM::KeyProvider provider3 = std::move(provider2);
231         } catch (...) {
232                 BOOST_REQUIRE_MESSAGE(false, "Unknown exception on moving KeyProvider");
233         }
234 }
235
236 BOOST_AUTO_TEST_SUITE_END()