2 * Copyright (c) 2016 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Kyungwook Tak <k.tak@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @file test_key-provider.cpp
19 * @author Kyungwook Tak (k.tak@samsung.com)
23 #define BOOST_TEST_MODULE KEY_MANAGER_TEST
24 #include <boost_macros_wrapper.h>
25 #include <exception.h>
26 #include <key-provider.h>
27 #include <test_common.h>
30 const CKM::Password PASSWORD = "12345TIZEN12345AAAAAAAAA";
31 const CKM::Password INCORRECT_PASSWORD = "AAAAAAAAAAAAAAAAAAAAA";
32 const CKM::Password NEW_PASSWORD = "NEW12345TIZEN12345NEW";
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";
39 extern bool isLibInitialized;
41 BOOST_AUTO_TEST_SUITE(KEY_PROVIDER_TEST)
42 POSITIVE_TEST_CASE(KeyDomainKEK)
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");
55 NEGATIVE_TEST_CASE(KeyDomainKekInvalidPassword)
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");
69 POSITIVE_TEST_CASE(KeygetPureDomainKEK)
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());
83 POSITIVE_TEST_CASE(KeyGetWrappedDomainKEK)
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));
97 POSITIVE_TEST_CASE(KeyGenerateDEK)
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));
112 POSITIVE_TEST_CASE(KeyGetPureDEK)
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));
129 POSITIVE_TEST_CASE(KeyReencrypt)
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,
140 NEGATIVE_TEST_CASE(KeyReencrypt_incorrect_password)
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,
149 NEW_PASSWORD)), CKM::Exc::AuthenticationFailed);
152 POSITIVE_TEST_CASE(KeyGetPureDEK_after_reencrypt)
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));
166 POSITIVE_TEST_CASE(wrapped_container)
168 CKM::WrappedKeyAndInfoContainer wrappedContainer;
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"));
174 CKM::WrappedKeyAndInfoContainer wrappedContainer2;
175 BOOST_REQUIRE_NO_THROW(
176 wrappedContainer2.setKeyInfo(&wrappedContainer.getWrappedKeyAndInfo().keyInfo));
179 wrappedContainer.getWrappedKeyAndInfo().keyInfo.keyLength ==
180 wrappedContainer2.getWrappedKeyAndInfo().keyInfo.keyLength);
181 BOOST_REQUIRE(memcmp(
182 wrappedContainer.getWrappedKeyAndInfo().keyInfo.salt,
183 wrappedContainer2.getWrappedKeyAndInfo().keyInfo.salt,
184 sizeof(wrappedContainer.getWrappedKeyAndInfo().keyInfo.salt)) == 0);
185 BOOST_REQUIRE(memcmp(
186 wrappedContainer.getWrappedKeyAndInfo().keyInfo.client,
187 wrappedContainer2.getWrappedKeyAndInfo().keyInfo.client,
188 sizeof(wrappedContainer.getWrappedKeyAndInfo().keyInfo.client)) == 0);
190 CKM::WrappedKeyAndInfo wrapped3;
191 wrapped3.keyInfo.keyLength = MAX_WRAPPED_KEY_SIZE;
192 BOOST_REQUIRE_NO_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3(
193 reinterpret_cast<unsigned char*>(&wrapped3)));
196 NEGATIVE_TEST_CASE(wrapped_container)
198 CKM::WrappedKeyAndInfoContainer wrappedContainer;
200 BOOST_REQUIRE_THROW(wrappedContainer.setKeyInfoClient("key_info_client_waaaaay_too_long"),
201 CKM::Exc::InternalError);
203 CKM::WrappedKeyAndInfo wrapped3;
205 wrapped3.keyInfo.keyLength = MAX_WRAPPED_KEY_SIZE + 1;
206 BOOST_REQUIRE_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3(
207 reinterpret_cast<unsigned char*>(&wrapped3)),
208 CKM::Exc::InternalError);
210 // missing NULL termination in wrapped4.keyInfo.client
211 CKM::WrappedKeyAndInfo wrapped4;
212 memset(&wrapped4, 0x01, sizeof(CKM::WrappedKeyAndInfo));
213 BOOST_REQUIRE_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3(
214 reinterpret_cast<unsigned char*>(&wrapped4)),
215 CKM::Exc::InternalError);
218 POSITIVE_TEST_CASE(container)
220 CKM::KeyAndInfoContainer container;
221 BOOST_REQUIRE_NO_THROW(container.setKeyInfoKeyLength(10));
223 CKM::KeyAndInfoContainer container2;
224 BOOST_REQUIRE_NO_THROW(container2.setKeyInfo(&container.getKeyAndInfo().keyInfo));
227 container.getKeyAndInfo().keyInfo.keyLength ==
228 container2.getKeyAndInfo().keyInfo.keyLength);
231 POSITIVE_TEST_CASE(moves)
233 CKM::KeyProvider provider;
236 CKM::KeyProvider provider2(std::move(provider));
237 CKM::KeyProvider provider3 = std::move(provider2);
239 BOOST_REQUIRE_MESSAGE(false, "Unknown exception on moving KeyProvider");
243 BOOST_AUTO_TEST_SUITE_END()