2 * QEMU Crypto secret handling
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "crypto/init.h"
24 #include "crypto/secret.h"
26 static void test_secret_direct(void)
28 Object
*sec
= object_new_with_props(
30 object_get_objects_root(),
36 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
39 g_assert_cmpstr(pw
, ==, "123456");
46 static void test_secret_indirect_good(void)
50 int fd
= g_file_open_tmp("secretXXXXXX",
55 g_assert_nonnull(fname
);
57 g_assert(write(fd
, "123456", 6) == 6);
59 sec
= object_new_with_props(
61 object_get_objects_root(),
67 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
70 g_assert_cmpstr(pw
, ==, "123456");
79 static void test_secret_indirect_badfile(void)
81 Object
*sec
= object_new_with_props(
83 object_get_objects_root(),
86 "file", "does-not-exist",
89 g_assert(sec
== NULL
);
93 static void test_secret_indirect_emptyfile(void)
97 int fd
= g_file_open_tmp("secretXXXXXX",
102 g_assert_nonnull(fname
);
104 sec
= object_new_with_props(
106 object_get_objects_root(),
112 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
115 g_assert_cmpstr(pw
, ==, "");
117 object_unparent(sec
);
124 static void test_secret_noconv_base64_good(void)
126 Object
*sec
= object_new_with_props(
128 object_get_objects_root(),
135 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
138 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
140 object_unparent(sec
);
145 static void test_secret_noconv_base64_bad(void)
147 Object
*sec
= object_new_with_props(
149 object_get_objects_root(),
156 g_assert(sec
== NULL
);
160 static void test_secret_noconv_utf8(void)
162 Object
*sec
= object_new_with_props(
164 object_get_objects_root(),
171 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
174 g_assert_cmpstr(pw
, ==, "123456");
176 object_unparent(sec
);
181 static void test_secret_conv_base64_utf8valid(void)
183 Object
*sec
= object_new_with_props(
185 object_get_objects_root(),
192 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
195 g_assert_cmpstr(pw
, ==, "123456");
197 object_unparent(sec
);
202 static void test_secret_conv_base64_utf8invalid(void)
204 Object
*sec
= object_new_with_props(
206 object_get_objects_root(),
209 "data", "f0VMRgIBAQAAAA==",
213 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
215 g_assert(pw
== NULL
);
217 object_unparent(sec
);
221 static void test_secret_conv_utf8_base64(void)
223 Object
*sec
= object_new_with_props(
225 object_get_objects_root(),
231 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
234 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
236 object_unparent(sec
);
241 static void test_secret_crypt_raw(void)
243 Object
*master
= object_new_with_props(
245 object_get_objects_root(),
248 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
251 Object
*sec
= object_new_with_props(
253 object_get_objects_root(),
257 "\xCC\xBF\xF7\x09\x46\x19\x0B\x52\x2A\x3A\xB4\x6B\xCD\x7A\xB0\xB0",
260 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
263 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
266 g_assert_cmpstr(pw
, ==, "123456");
268 object_unparent(sec
);
269 object_unparent(master
);
274 static void test_secret_crypt_base64(void)
276 Object
*master
= object_new_with_props(
278 object_get_objects_root(),
281 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
284 Object
*sec
= object_new_with_props(
286 object_get_objects_root(),
289 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
292 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
295 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
298 g_assert_cmpstr(pw
, ==, "123456");
300 object_unparent(sec
);
301 object_unparent(master
);
306 static void test_secret_crypt_short_key(void)
308 Object
*master
= object_new_with_props(
310 object_get_objects_root(),
313 "data", "9miloPQCzGy+TL6aonfzVc",
316 Object
*sec
= object_new_with_props(
318 object_get_objects_root(),
321 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
324 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
327 g_assert(sec
== NULL
);
328 object_unparent(master
);
332 static void test_secret_crypt_short_iv(void)
334 Object
*master
= object_new_with_props(
336 object_get_objects_root(),
339 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
342 Object
*sec
= object_new_with_props(
344 object_get_objects_root(),
347 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
350 "iv", "0I7Gw/TKuA+Old2W2a",
353 g_assert(sec
== NULL
);
354 object_unparent(master
);
358 static void test_secret_crypt_missing_iv(void)
360 Object
*master
= object_new_with_props(
362 object_get_objects_root(),
365 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
368 Object
*sec
= object_new_with_props(
370 object_get_objects_root(),
373 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
378 g_assert(sec
== NULL
);
379 object_unparent(master
);
383 static void test_secret_crypt_bad_iv(void)
385 Object
*master
= object_new_with_props(
387 object_get_objects_root(),
390 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
393 Object
*sec
= object_new_with_props(
395 object_get_objects_root(),
398 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
401 "iv", "0I7Gw/TK$$uA+Old2W2a",
404 g_assert(sec
== NULL
);
405 object_unparent(master
);
409 int main(int argc
, char **argv
)
411 module_call_init(MODULE_INIT_QOM
);
412 g_test_init(&argc
, &argv
, NULL
);
414 g_assert(qcrypto_init(NULL
) == 0);
416 g_test_add_func("/crypto/secret/direct",
418 g_test_add_func("/crypto/secret/indirect/good",
419 test_secret_indirect_good
);
420 g_test_add_func("/crypto/secret/indirect/badfile",
421 test_secret_indirect_badfile
);
422 g_test_add_func("/crypto/secret/indirect/emptyfile",
423 test_secret_indirect_emptyfile
);
425 g_test_add_func("/crypto/secret/noconv/base64/good",
426 test_secret_noconv_base64_good
);
427 g_test_add_func("/crypto/secret/noconv/base64/bad",
428 test_secret_noconv_base64_bad
);
429 g_test_add_func("/crypto/secret/noconv/utf8",
430 test_secret_noconv_utf8
);
431 g_test_add_func("/crypto/secret/conv/base64/utf8valid",
432 test_secret_conv_base64_utf8valid
);
433 g_test_add_func("/crypto/secret/conv/base64/utf8invalid",
434 test_secret_conv_base64_utf8invalid
);
435 g_test_add_func("/crypto/secret/conv/utf8/base64",
436 test_secret_conv_utf8_base64
);
438 g_test_add_func("/crypto/secret/crypt/raw",
439 test_secret_crypt_raw
);
440 g_test_add_func("/crypto/secret/crypt/base64",
441 test_secret_crypt_base64
);
442 g_test_add_func("/crypto/secret/crypt/shortkey",
443 test_secret_crypt_short_key
);
444 g_test_add_func("/crypto/secret/crypt/shortiv",
445 test_secret_crypt_short_iv
);
446 g_test_add_func("/crypto/secret/crypt/missingiv",
447 test_secret_crypt_missing_iv
);
448 g_test_add_func("/crypto/secret/crypt/badiv",
449 test_secret_crypt_bad_iv
);