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"
27 static void test_secret_direct(void)
29 Object
*sec
= object_new_with_props(
31 object_get_objects_root(),
37 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
40 g_assert_cmpstr(pw
, ==, "123456");
47 static void test_secret_indirect_good(void)
51 int fd
= g_file_open_tmp("secretXXXXXX",
56 g_assert_nonnull(fname
);
58 g_assert(write(fd
, "123456", 6) == 6);
60 sec
= object_new_with_props(
62 object_get_objects_root(),
68 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
71 g_assert_cmpstr(pw
, ==, "123456");
80 static void test_secret_indirect_badfile(void)
82 Object
*sec
= object_new_with_props(
84 object_get_objects_root(),
87 "file", "does-not-exist",
90 g_assert(sec
== NULL
);
94 static void test_secret_indirect_emptyfile(void)
98 int fd
= g_file_open_tmp("secretXXXXXX",
103 g_assert_nonnull(fname
);
105 sec
= object_new_with_props(
107 object_get_objects_root(),
113 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
116 g_assert_cmpstr(pw
, ==, "");
118 object_unparent(sec
);
125 static void test_secret_noconv_base64_good(void)
127 Object
*sec
= object_new_with_props(
129 object_get_objects_root(),
136 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
139 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
141 object_unparent(sec
);
146 static void test_secret_noconv_base64_bad(void)
148 Object
*sec
= object_new_with_props(
150 object_get_objects_root(),
157 g_assert(sec
== NULL
);
161 static void test_secret_noconv_utf8(void)
163 Object
*sec
= object_new_with_props(
165 object_get_objects_root(),
172 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
175 g_assert_cmpstr(pw
, ==, "123456");
177 object_unparent(sec
);
182 static void test_secret_conv_base64_utf8valid(void)
184 Object
*sec
= object_new_with_props(
186 object_get_objects_root(),
193 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
196 g_assert_cmpstr(pw
, ==, "123456");
198 object_unparent(sec
);
203 static void test_secret_conv_base64_utf8invalid(void)
205 Object
*sec
= object_new_with_props(
207 object_get_objects_root(),
210 "data", "f0VMRgIBAQAAAA==",
214 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
216 g_assert(pw
== NULL
);
218 object_unparent(sec
);
222 static void test_secret_conv_utf8_base64(void)
224 Object
*sec
= object_new_with_props(
226 object_get_objects_root(),
232 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
235 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
237 object_unparent(sec
);
242 static void test_secret_crypt_raw(void)
244 Object
*master
= object_new_with_props(
246 object_get_objects_root(),
249 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
252 Object
*sec
= object_new_with_props(
254 object_get_objects_root(),
258 "\xCC\xBF\xF7\x09\x46\x19\x0B\x52\x2A\x3A\xB4\x6B\xCD\x7A\xB0\xB0",
261 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
264 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
267 g_assert_cmpstr(pw
, ==, "123456");
269 object_unparent(sec
);
270 object_unparent(master
);
275 static void test_secret_crypt_base64(void)
277 Object
*master
= object_new_with_props(
279 object_get_objects_root(),
282 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
285 Object
*sec
= object_new_with_props(
287 object_get_objects_root(),
290 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
293 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
296 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
299 g_assert_cmpstr(pw
, ==, "123456");
301 object_unparent(sec
);
302 object_unparent(master
);
307 static void test_secret_crypt_short_key(void)
309 Object
*master
= object_new_with_props(
311 object_get_objects_root(),
314 "data", "9miloPQCzGy+TL6aonfzVc",
317 Object
*sec
= object_new_with_props(
319 object_get_objects_root(),
322 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
325 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
328 g_assert(sec
== NULL
);
329 object_unparent(master
);
333 static void test_secret_crypt_short_iv(void)
335 Object
*master
= object_new_with_props(
337 object_get_objects_root(),
340 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
343 Object
*sec
= object_new_with_props(
345 object_get_objects_root(),
348 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
351 "iv", "0I7Gw/TKuA+Old2W2a",
354 g_assert(sec
== NULL
);
355 object_unparent(master
);
359 static void test_secret_crypt_missing_iv(void)
361 Object
*master
= object_new_with_props(
363 object_get_objects_root(),
366 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
369 Object
*sec
= object_new_with_props(
371 object_get_objects_root(),
374 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
379 g_assert(sec
== NULL
);
380 object_unparent(master
);
384 static void test_secret_crypt_bad_iv(void)
386 Object
*master
= object_new_with_props(
388 object_get_objects_root(),
391 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
394 Object
*sec
= object_new_with_props(
396 object_get_objects_root(),
399 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
402 "iv", "0I7Gw/TK$$uA+Old2W2a",
405 g_assert(sec
== NULL
);
406 object_unparent(master
);
410 int main(int argc
, char **argv
)
412 module_call_init(MODULE_INIT_QOM
);
413 g_test_init(&argc
, &argv
, NULL
);
415 g_assert(qcrypto_init(NULL
) == 0);
417 g_test_add_func("/crypto/secret/direct",
419 g_test_add_func("/crypto/secret/indirect/good",
420 test_secret_indirect_good
);
421 g_test_add_func("/crypto/secret/indirect/badfile",
422 test_secret_indirect_badfile
);
423 g_test_add_func("/crypto/secret/indirect/emptyfile",
424 test_secret_indirect_emptyfile
);
426 g_test_add_func("/crypto/secret/noconv/base64/good",
427 test_secret_noconv_base64_good
);
428 g_test_add_func("/crypto/secret/noconv/base64/bad",
429 test_secret_noconv_base64_bad
);
430 g_test_add_func("/crypto/secret/noconv/utf8",
431 test_secret_noconv_utf8
);
432 g_test_add_func("/crypto/secret/conv/base64/utf8valid",
433 test_secret_conv_base64_utf8valid
);
434 g_test_add_func("/crypto/secret/conv/base64/utf8invalid",
435 test_secret_conv_base64_utf8invalid
);
436 g_test_add_func("/crypto/secret/conv/utf8/base64",
437 test_secret_conv_utf8_base64
);
439 g_test_add_func("/crypto/secret/crypt/raw",
440 test_secret_crypt_raw
);
441 g_test_add_func("/crypto/secret/crypt/base64",
442 test_secret_crypt_base64
);
443 g_test_add_func("/crypto/secret/crypt/shortkey",
444 test_secret_crypt_short_key
);
445 g_test_add_func("/crypto/secret/crypt/shortiv",
446 test_secret_crypt_short_iv
);
447 g_test_add_func("/crypto/secret/crypt/missingiv",
448 test_secret_crypt_missing_iv
);
449 g_test_add_func("/crypto/secret/crypt/badiv",
450 test_secret_crypt_bad_iv
);