ea369d1ae4ba604b7a8bb5b41c2c41f241a54fdd
[platform/core/security/key-manager.git] / tests / test_key-provider.cpp
1 /*
2  *  Copyright (c) 2016 - 2020 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_macros_wrapper.h>
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 POSITIVE_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 NEGATIVE_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 POSITIVE_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 POSITIVE_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 POSITIVE_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 POSITIVE_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 POSITIVE_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 NEGATIVE_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 POSITIVE_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 POSITIVE_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         CKM::WrappedKeyAndInfoContainer wrappedContainer2;
175         BOOST_REQUIRE_NO_THROW(
176                 wrappedContainer2.setKeyInfo(&wrappedContainer.getWrappedKeyAndInfo().keyInfo));
177
178         BOOST_REQUIRE(
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);
189
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)));
194 }
195
196 NEGATIVE_TEST_CASE(wrapped_container)
197 {
198         CKM::WrappedKeyAndInfoContainer wrappedContainer;
199
200         BOOST_REQUIRE_THROW(wrappedContainer.setKeyInfoClient("key_info_client_waaaaay_too_long"),
201                             CKM::Exc::InternalError);
202
203         CKM::WrappedKeyAndInfo wrapped3;
204
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);
209
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);
216 }
217
218 POSITIVE_TEST_CASE(container)
219 {
220         CKM::KeyAndInfoContainer container;
221         BOOST_REQUIRE_NO_THROW(container.setKeyInfoKeyLength(10));
222
223         CKM::KeyAndInfoContainer container2;
224         BOOST_REQUIRE_NO_THROW(container2.setKeyInfo(&container.getKeyAndInfo().keyInfo));
225
226         BOOST_REQUIRE(
227                 container.getKeyAndInfo().keyInfo.keyLength ==
228                         container2.getKeyAndInfo().keyInfo.keyLength);
229 }
230
231 POSITIVE_TEST_CASE(moves)
232 {
233         CKM::KeyProvider provider;
234
235         try {
236                 CKM::KeyProvider provider2(std::move(provider));
237                 CKM::KeyProvider provider3 = std::move(provider2);
238         } catch (...) {
239                 BOOST_REQUIRE_MESSAGE(false, "Unknown exception on moving KeyProvider");
240         }
241 }
242
243 BOOST_AUTO_TEST_SUITE_END()