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("secretXXXXXX",
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");
81 static void test_secret_indirect_badfile(void)
83 Object
*sec
= object_new_with_props(
85 object_get_objects_root(),
88 "file", "does-not-exist",
91 g_assert(sec
== NULL
);
95 static void test_secret_indirect_emptyfile(void)
99 int fd
= g_file_open_tmp("secretXXXXXX",
104 g_assert_nonnull(fname
);
106 sec
= object_new_with_props(
108 object_get_objects_root(),
114 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
117 g_assert_cmpstr(pw
, ==, "");
119 object_unparent(sec
);
126 static void test_secret_noconv_base64_good(void)
128 Object
*sec
= object_new_with_props(
130 object_get_objects_root(),
137 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
140 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
142 object_unparent(sec
);
147 static void test_secret_noconv_base64_bad(void)
149 Object
*sec
= object_new_with_props(
151 object_get_objects_root(),
158 g_assert(sec
== NULL
);
162 static void test_secret_noconv_utf8(void)
164 Object
*sec
= object_new_with_props(
166 object_get_objects_root(),
173 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
176 g_assert_cmpstr(pw
, ==, "123456");
178 object_unparent(sec
);
183 static void test_secret_conv_base64_utf8valid(void)
185 Object
*sec
= object_new_with_props(
187 object_get_objects_root(),
194 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
197 g_assert_cmpstr(pw
, ==, "123456");
199 object_unparent(sec
);
204 static void test_secret_conv_base64_utf8invalid(void)
206 Object
*sec
= object_new_with_props(
208 object_get_objects_root(),
211 "data", "f0VMRgIBAQAAAA==",
215 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
217 g_assert(pw
== NULL
);
219 object_unparent(sec
);
223 static void test_secret_conv_utf8_base64(void)
225 Object
*sec
= object_new_with_props(
227 object_get_objects_root(),
233 char *pw
= qcrypto_secret_lookup_as_base64("sec0",
236 g_assert_cmpstr(pw
, ==, "MTIzNDU2");
238 object_unparent(sec
);
243 static void test_secret_crypt_raw(void)
245 Object
*master
= object_new_with_props(
247 object_get_objects_root(),
250 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
253 Object
*sec
= object_new_with_props(
255 object_get_objects_root(),
259 "\xCC\xBF\xF7\x09\x46\x19\x0B\x52\x2A\x3A\xB4\x6B\xCD\x7A\xB0\xB0",
262 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
265 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
268 g_assert_cmpstr(pw
, ==, "123456");
270 object_unparent(sec
);
271 object_unparent(master
);
276 static void test_secret_crypt_base64(void)
278 Object
*master
= object_new_with_props(
280 object_get_objects_root(),
283 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
286 Object
*sec
= object_new_with_props(
288 object_get_objects_root(),
291 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
294 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
297 char *pw
= qcrypto_secret_lookup_as_utf8("sec0",
300 g_assert_cmpstr(pw
, ==, "123456");
302 object_unparent(sec
);
303 object_unparent(master
);
308 static void test_secret_crypt_short_key(void)
310 Object
*master
= object_new_with_props(
312 object_get_objects_root(),
315 "data", "9miloPQCzGy+TL6aonfzVc",
318 Object
*sec
= object_new_with_props(
320 object_get_objects_root(),
323 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
326 "iv", "0I7Gw/TKuA+Old2W2apQ3g==",
329 g_assert(sec
== NULL
);
330 object_unparent(master
);
334 static void test_secret_crypt_short_iv(void)
336 Object
*master
= object_new_with_props(
338 object_get_objects_root(),
341 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
344 Object
*sec
= object_new_with_props(
346 object_get_objects_root(),
349 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
352 "iv", "0I7Gw/TKuA+Old2W2a",
355 g_assert(sec
== NULL
);
356 object_unparent(master
);
360 static void test_secret_crypt_missing_iv(void)
362 Object
*master
= object_new_with_props(
364 object_get_objects_root(),
367 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
370 Object
*sec
= object_new_with_props(
372 object_get_objects_root(),
375 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
380 g_assert(sec
== NULL
);
381 object_unparent(master
);
385 static void test_secret_crypt_bad_iv(void)
387 Object
*master
= object_new_with_props(
389 object_get_objects_root(),
392 "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=",
395 Object
*sec
= object_new_with_props(
397 object_get_objects_root(),
400 "data", "zL/3CUYZC1IqOrRrzXqwsA==",
403 "iv", "0I7Gw/TK$$uA+Old2W2a",
406 g_assert(sec
== NULL
);
407 object_unparent(master
);
411 int main(int argc
, char **argv
)
413 module_call_init(MODULE_INIT_QOM
);
414 g_test_init(&argc
, &argv
, NULL
);
416 g_assert(qcrypto_init(NULL
) == 0);
418 g_test_add_func("/crypto/secret/direct",
420 g_test_add_func("/crypto/secret/indirect/good",
421 test_secret_indirect_good
);
422 g_test_add_func("/crypto/secret/indirect/badfile",
423 test_secret_indirect_badfile
);
424 g_test_add_func("/crypto/secret/indirect/emptyfile",
425 test_secret_indirect_emptyfile
);
427 g_test_add_func("/crypto/secret/noconv/base64/good",
428 test_secret_noconv_base64_good
);
429 g_test_add_func("/crypto/secret/noconv/base64/bad",
430 test_secret_noconv_base64_bad
);
431 g_test_add_func("/crypto/secret/noconv/utf8",
432 test_secret_noconv_utf8
);
433 g_test_add_func("/crypto/secret/conv/base64/utf8valid",
434 test_secret_conv_base64_utf8valid
);
435 g_test_add_func("/crypto/secret/conv/base64/utf8invalid",
436 test_secret_conv_base64_utf8invalid
);
437 g_test_add_func("/crypto/secret/conv/utf8/base64",
438 test_secret_conv_utf8_base64
);
440 g_test_add_func("/crypto/secret/crypt/raw",
441 test_secret_crypt_raw
);
442 g_test_add_func("/crypto/secret/crypt/base64",
443 test_secret_crypt_base64
);
444 g_test_add_func("/crypto/secret/crypt/shortkey",
445 test_secret_crypt_short_key
);
446 g_test_add_func("/crypto/secret/crypt/shortiv",
447 test_secret_crypt_short_iv
);
448 g_test_add_func("/crypto/secret/crypt/missingiv",
449 test_secret_crypt_missing_iv
);
450 g_test_add_func("/crypto/secret/crypt/badiv",
451 test_secret_crypt_bad_iv
);