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"
23 #include "crypto/init.h"
24 #include "crypto/secret.h"
25 #include "qapi/error.h"
26 #include "qemu/module.h"
28 static void test_secret_direct(void)
30 Object
*sec
= object_new_with_props(
32 object_get_objects_root(),
38 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
41 g_assert_cmpstr(pw
, ==, "123456");
48 static void test_secret_indirect_good(void)
52 int fd
= g_file_open_tmp("qemu-test-crypto-secret-XXXXXX",
57 g_assert_nonnull(fname
);
59 g_assert(write(fd
, "123456", 6) == 6);
61 sec
= object_new_with_props(
63 object_get_objects_root(),
69 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
72 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("qemu-test-crypto-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
);
128 static void test_secret_noconv_base64_good(void)
130 Object
*sec
= object_new_with_props(
132 object_get_objects_root(),
139 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
142 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
144 object_unparent(sec
);
149 static void test_secret_noconv_base64_bad(void)
151 Object
*sec
= object_new_with_props(
153 object_get_objects_root(),
160 g_assert(sec
== NULL
);
164 static void test_secret_noconv_utf8(void)
166 Object
*sec
= object_new_with_props(
168 object_get_objects_root(),
175 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
178 g_assert_cmpstr(pw
, ==, "123456");
180 object_unparent(sec
);
185 static void test_secret_conv_base64_utf8valid(void)
187 Object
*sec
= object_new_with_props(
189 object_get_objects_root(),
196 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
199 g_assert_cmpstr(pw
, ==, "123456");
201 object_unparent(sec
);
206 static void test_secret_conv_base64_utf8invalid(void)
208 Object
*sec
= object_new_with_props(
210 object_get_objects_root(),
213 "data", "f0VMRgIBAQAAAA==",
217 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
219 g_assert(pw
== NULL
);
221 object_unparent(sec
);
225 static void test_secret_conv_utf8_base64(void)
227 Object
*sec
= object_new_with_props(
229 object_get_objects_root(),
235 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
238 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
240 object_unparent(sec
);
245 static void test_secret_crypt_raw(void)
247 Object
*master
= object_new_with_props(
249 object_get_objects_root(),
252 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
255 Object
*sec
= object_new_with_props(
257 object_get_objects_root(),
261 "\xCC\xBF\xF7\x09\x46\x19\x0B\x52\x2A\x3A\xB4\x6B\xCD\x7A\xB0\xB0",
264 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
267 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
270 g_assert_cmpstr(pw
, ==, "123456");
272 object_unparent(sec
);
273 object_unparent(master
);
278 static void test_secret_crypt_base64(void)
280 Object
*master
= object_new_with_props(
282 object_get_objects_root(),
285 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
288 Object
*sec
= object_new_with_props(
290 object_get_objects_root(),
293 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
296 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
299 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
302 g_assert_cmpstr(pw
, ==, "123456");
304 object_unparent(sec
);
305 object_unparent(master
);
310 static void test_secret_crypt_short_key(void)
312 Object
*master
= object_new_with_props(
314 object_get_objects_root(),
317 "data", "9miloPQCzGy+TL6aonfzVc",
320 Object
*sec
= object_new_with_props(
322 object_get_objects_root(),
325 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
328 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
331 g_assert(sec
== NULL
);
332 object_unparent(master
);
336 static void test_secret_crypt_short_iv(void)
338 Object
*master
= object_new_with_props(
340 object_get_objects_root(),
343 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
346 Object
*sec
= object_new_with_props(
348 object_get_objects_root(),
351 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
354 "iv", "0I7Gw/TKuA+Old2W2a",
357 g_assert(sec
== NULL
);
358 object_unparent(master
);
362 static void test_secret_crypt_missing_iv(void)
364 Object
*master
= object_new_with_props(
366 object_get_objects_root(),
369 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
372 Object
*sec
= object_new_with_props(
374 object_get_objects_root(),
377 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
382 g_assert(sec
== NULL
);
383 object_unparent(master
);
387 static void test_secret_crypt_bad_iv(void)
389 Object
*master
= object_new_with_props(
391 object_get_objects_root(),
394 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
397 Object
*sec
= object_new_with_props(
399 object_get_objects_root(),
402 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
405 "iv", "0I7Gw/TK$$uA+Old2W2a",
408 g_assert(sec
== NULL
);
409 object_unparent(master
);
413 int main(int argc
, char **argv
)
415 module_call_init(MODULE_INIT_QOM
);
416 g_test_init(&argc
, &argv
, NULL
);
418 g_assert(qcrypto_init(NULL
) == 0);
420 g_test_add_func("/crypto/secret/direct",
422 g_test_add_func("/crypto/secret/indirect/good",
423 test_secret_indirect_good
);
424 g_test_add_func("/crypto/secret/indirect/badfile",
425 test_secret_indirect_badfile
);
426 g_test_add_func("/crypto/secret/indirect/emptyfile",
427 test_secret_indirect_emptyfile
);
429 g_test_add_func("/crypto/secret/noconv/base64/good",
430 test_secret_noconv_base64_good
);
431 g_test_add_func("/crypto/secret/noconv/base64/bad",
432 test_secret_noconv_base64_bad
);
433 g_test_add_func("/crypto/secret/noconv/utf8",
434 test_secret_noconv_utf8
);
435 g_test_add_func("/crypto/secret/conv/base64/utf8valid",
436 test_secret_conv_base64_utf8valid
);
437 g_test_add_func("/crypto/secret/conv/base64/utf8invalid",
438 test_secret_conv_base64_utf8invalid
);
439 g_test_add_func("/crypto/secret/conv/utf8/base64",
440 test_secret_conv_utf8_base64
);
442 g_test_add_func("/crypto/secret/crypt/raw",
443 test_secret_crypt_raw
);
444 g_test_add_func("/crypto/secret/crypt/base64",
445 test_secret_crypt_base64
);
446 g_test_add_func("/crypto/secret/crypt/shortkey",
447 test_secret_crypt_short_key
);
448 g_test_add_func("/crypto/secret/crypt/shortiv",
449 test_secret_crypt_short_iv
);
450 g_test_add_func("/crypto/secret/crypt/missingiv",
451 test_secret_crypt_missing_iv
);
452 g_test_add_func("/crypto/secret/crypt/badiv",
453 test_secret_crypt_bad_iv
);