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/>.
21 #include "qemu/osdep.h"
24 #include "crypto/init.h"
25 #include "crypto/secret.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
29 static void test_secret_direct(void)
31 Object
*sec
= object_new_with_props(
33 object_get_objects_root(),
39 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
42 g_assert_cmpstr(pw
, ==, "123456");
49 static void test_secret_indirect_good(void)
53 int fd
= g_file_open_tmp("secretXXXXXX",
58 g_assert_nonnull(fname
);
60 g_assert(write(fd
, "123456", 6) == 6);
62 sec
= object_new_with_props(
64 object_get_objects_root(),
70 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
73 g_assert_cmpstr(pw
, ==, "123456");
82 static void test_secret_indirect_badfile(void)
84 Object
*sec
= object_new_with_props(
86 object_get_objects_root(),
89 "file", "does-not-exist",
92 g_assert(sec
== NULL
);
96 static void test_secret_indirect_emptyfile(void)
100 int fd
= g_file_open_tmp("secretXXXXXX",
105 g_assert_nonnull(fname
);
107 sec
= object_new_with_props(
109 object_get_objects_root(),
115 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
118 g_assert_cmpstr(pw
, ==, "");
120 object_unparent(sec
);
127 static void test_secret_noconv_base64_good(void)
129 Object
*sec
= object_new_with_props(
131 object_get_objects_root(),
138 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
141 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
143 object_unparent(sec
);
148 static void test_secret_noconv_base64_bad(void)
150 Object
*sec
= object_new_with_props(
152 object_get_objects_root(),
159 g_assert(sec
== NULL
);
163 static void test_secret_noconv_utf8(void)
165 Object
*sec
= object_new_with_props(
167 object_get_objects_root(),
174 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
177 g_assert_cmpstr(pw
, ==, "123456");
179 object_unparent(sec
);
184 static void test_secret_conv_base64_utf8valid(void)
186 Object
*sec
= object_new_with_props(
188 object_get_objects_root(),
195 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
198 g_assert_cmpstr(pw
, ==, "123456");
200 object_unparent(sec
);
205 static void test_secret_conv_base64_utf8invalid(void)
207 Object
*sec
= object_new_with_props(
209 object_get_objects_root(),
212 "data", "f0VMRgIBAQAAAA==",
216 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
218 g_assert(pw
== NULL
);
220 object_unparent(sec
);
224 static void test_secret_conv_utf8_base64(void)
226 Object
*sec
= object_new_with_props(
228 object_get_objects_root(),
234 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
237 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
239 object_unparent(sec
);
244 static void test_secret_crypt_raw(void)
246 Object
*master
= object_new_with_props(
248 object_get_objects_root(),
251 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
254 Object
*sec
= object_new_with_props(
256 object_get_objects_root(),
260 "\xCC\xBF\xF7\x09\x46\x19\x0B\x52\x2A\x3A\xB4\x6B\xCD\x7A\xB0\xB0",
263 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
266 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
269 g_assert_cmpstr(pw
, ==, "123456");
271 object_unparent(sec
);
272 object_unparent(master
);
277 static void test_secret_crypt_base64(void)
279 Object
*master
= object_new_with_props(
281 object_get_objects_root(),
284 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
287 Object
*sec
= object_new_with_props(
289 object_get_objects_root(),
292 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
295 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
298 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
301 g_assert_cmpstr(pw
, ==, "123456");
303 object_unparent(sec
);
304 object_unparent(master
);
309 static void test_secret_crypt_short_key(void)
311 Object
*master
= object_new_with_props(
313 object_get_objects_root(),
316 "data", "9miloPQCzGy+TL6aonfzVc",
319 Object
*sec
= object_new_with_props(
321 object_get_objects_root(),
324 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
327 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
330 g_assert(sec
== NULL
);
331 object_unparent(master
);
335 static void test_secret_crypt_short_iv(void)
337 Object
*master
= object_new_with_props(
339 object_get_objects_root(),
342 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
345 Object
*sec
= object_new_with_props(
347 object_get_objects_root(),
350 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
353 "iv", "0I7Gw/TKuA+Old2W2a",
356 g_assert(sec
== NULL
);
357 object_unparent(master
);
361 static void test_secret_crypt_missing_iv(void)
363 Object
*master
= object_new_with_props(
365 object_get_objects_root(),
368 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
371 Object
*sec
= object_new_with_props(
373 object_get_objects_root(),
376 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
381 g_assert(sec
== NULL
);
382 object_unparent(master
);
386 static void test_secret_crypt_bad_iv(void)
388 Object
*master
= object_new_with_props(
390 object_get_objects_root(),
393 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
396 Object
*sec
= object_new_with_props(
398 object_get_objects_root(),
401 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
404 "iv", "0I7Gw/TK$$uA+Old2W2a",
407 g_assert(sec
== NULL
);
408 object_unparent(master
);
412 int main(int argc
, char **argv
)
414 module_call_init(MODULE_INIT_QOM
);
415 g_test_init(&argc
, &argv
, NULL
);
417 g_assert(qcrypto_init(NULL
) == 0);
419 g_test_add_func("/crypto/secret/direct",
421 g_test_add_func("/crypto/secret/indirect/good",
422 test_secret_indirect_good
);
423 g_test_add_func("/crypto/secret/indirect/badfile",
424 test_secret_indirect_badfile
);
425 g_test_add_func("/crypto/secret/indirect/emptyfile",
426 test_secret_indirect_emptyfile
);
428 g_test_add_func("/crypto/secret/noconv/base64/good",
429 test_secret_noconv_base64_good
);
430 g_test_add_func("/crypto/secret/noconv/base64/bad",
431 test_secret_noconv_base64_bad
);
432 g_test_add_func("/crypto/secret/noconv/utf8",
433 test_secret_noconv_utf8
);
434 g_test_add_func("/crypto/secret/conv/base64/utf8valid",
435 test_secret_conv_base64_utf8valid
);
436 g_test_add_func("/crypto/secret/conv/base64/utf8invalid",
437 test_secret_conv_base64_utf8invalid
);
438 g_test_add_func("/crypto/secret/conv/utf8/base64",
439 test_secret_conv_utf8_base64
);
441 g_test_add_func("/crypto/secret/crypt/raw",
442 test_secret_crypt_raw
);
443 g_test_add_func("/crypto/secret/crypt/base64",
444 test_secret_crypt_base64
);
445 g_test_add_func("/crypto/secret/crypt/shortkey",
446 test_secret_crypt_short_key
);
447 g_test_add_func("/crypto/secret/crypt/shortiv",
448 test_secret_crypt_short_iv
);
449 g_test_add_func("/crypto/secret/crypt/missingiv",
450 test_secret_crypt_missing_iv
);
451 g_test_add_func("/crypto/secret/crypt/badiv",
452 test_secret_crypt_bad_iv
);