Daniel P. Berrange | ac1d887 | 2015-10-14 09:58:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Crypto secret handling |
| 3 | * |
| 4 | * Copyright (c) 2015 Red Hat, Inc. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <glib.h> |
| 22 | |
| 23 | #include "crypto/init.h" |
| 24 | #include "crypto/secret.h" |
| 25 | |
| 26 | static void test_secret_direct(void) |
| 27 | { |
| 28 | Object *sec = object_new_with_props( |
| 29 | TYPE_QCRYPTO_SECRET, |
| 30 | object_get_objects_root(), |
| 31 | "sec0", |
| 32 | &error_abort, |
| 33 | "data", "123456", |
| 34 | NULL); |
| 35 | |
| 36 | char *pw = qcrypto_secret_lookup_as_utf8("sec0", |
| 37 | &error_abort); |
| 38 | |
| 39 | g_assert_cmpstr(pw, ==, "123456"); |
| 40 | |
| 41 | object_unparent(sec); |
| 42 | g_free(pw); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | static void test_secret_indirect_good(void) |
| 47 | { |
| 48 | Object *sec; |
| 49 | char *fname = NULL; |
| 50 | int fd = g_file_open_tmp("secretXXXXXX", |
| 51 | &fname, |
| 52 | NULL); |
| 53 | |
| 54 | g_assert(fd >= 0); |
| 55 | g_assert_nonnull(fname); |
| 56 | |
| 57 | g_assert(write(fd, "123456", 6) == 6); |
| 58 | |
| 59 | sec = object_new_with_props( |
| 60 | TYPE_QCRYPTO_SECRET, |
| 61 | object_get_objects_root(), |
| 62 | "sec0", |
| 63 | &error_abort, |
| 64 | "file", fname, |
| 65 | NULL); |
| 66 | |
| 67 | char *pw = qcrypto_secret_lookup_as_utf8("sec0", |
| 68 | &error_abort); |
| 69 | |
| 70 | g_assert_cmpstr(pw, ==, "123456"); |
| 71 | |
| 72 | object_unparent(sec); |
| 73 | g_free(pw); |
| 74 | close(fd); |
| 75 | g_free(fname); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static void test_secret_indirect_badfile(void) |
| 80 | { |
| 81 | Object *sec = object_new_with_props( |
| 82 | TYPE_QCRYPTO_SECRET, |
| 83 | object_get_objects_root(), |
| 84 | "sec0", |
| 85 | NULL, |
| 86 | "file", "does-not-exist", |
| 87 | NULL); |
| 88 | |
| 89 | g_assert(sec == NULL); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static void test_secret_indirect_emptyfile(void) |
| 94 | { |
| 95 | Object *sec; |
| 96 | char *fname = NULL; |
| 97 | int fd = g_file_open_tmp("secretXXXXXX", |
| 98 | &fname, |
| 99 | NULL); |
| 100 | |
| 101 | g_assert(fd >= 0); |
| 102 | g_assert_nonnull(fname); |
| 103 | |
| 104 | sec = object_new_with_props( |
| 105 | TYPE_QCRYPTO_SECRET, |
| 106 | object_get_objects_root(), |
| 107 | "sec0", |
| 108 | &error_abort, |
| 109 | "file", fname, |
| 110 | NULL); |
| 111 | |
| 112 | char *pw = qcrypto_secret_lookup_as_utf8("sec0", |
| 113 | &error_abort); |
| 114 | |
| 115 | g_assert_cmpstr(pw, ==, ""); |
| 116 | |
| 117 | object_unparent(sec); |
| 118 | g_free(pw); |
| 119 | close(fd); |
| 120 | g_free(fname); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static void test_secret_noconv_base64_good(void) |
| 125 | { |
| 126 | Object *sec = object_new_with_props( |
| 127 | TYPE_QCRYPTO_SECRET, |
| 128 | object_get_objects_root(), |
| 129 | "sec0", |
| 130 | &error_abort, |
| 131 | "data", "MTIzNDU2", |
| 132 | "format", "base64", |
| 133 | NULL); |
| 134 | |
| 135 | char *pw = qcrypto_secret_lookup_as_base64("sec0", |
| 136 | &error_abort); |
| 137 | |
| 138 | g_assert_cmpstr(pw, ==, "MTIzNDU2"); |
| 139 | |
| 140 | object_unparent(sec); |
| 141 | g_free(pw); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | static void test_secret_noconv_base64_bad(void) |
| 146 | { |
| 147 | Object *sec = object_new_with_props( |
| 148 | TYPE_QCRYPTO_SECRET, |
| 149 | object_get_objects_root(), |
| 150 | "sec0", |
| 151 | NULL, |
| 152 | "data", "MTI$NDU2", |
| 153 | "format", "base64", |
| 154 | NULL); |
| 155 | |
| 156 | g_assert(sec == NULL); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | static void test_secret_noconv_utf8(void) |
| 161 | { |
| 162 | Object *sec = object_new_with_props( |
| 163 | TYPE_QCRYPTO_SECRET, |
| 164 | object_get_objects_root(), |
| 165 | "sec0", |
| 166 | &error_abort, |
| 167 | "data", "123456", |
| 168 | "format", "raw", |
| 169 | NULL); |
| 170 | |
| 171 | char *pw = qcrypto_secret_lookup_as_utf8("sec0", |
| 172 | &error_abort); |
| 173 | |
| 174 | g_assert_cmpstr(pw, ==, "123456"); |
| 175 | |
| 176 | object_unparent(sec); |
| 177 | g_free(pw); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | static void test_secret_conv_base64_utf8valid(void) |
| 182 | { |
| 183 | Object *sec = object_new_with_props( |
| 184 | TYPE_QCRYPTO_SECRET, |
| 185 | object_get_objects_root(), |
| 186 | "sec0", |
| 187 | &error_abort, |
| 188 | "data", "MTIzNDU2", |
| 189 | "format", "base64", |
| 190 | NULL); |
| 191 | |
| 192 | char *pw = qcrypto_secret_lookup_as_utf8("sec0", |
| 193 | &error_abort); |
| 194 | |
| 195 | g_assert_cmpstr(pw, ==, "123456"); |
| 196 | |
| 197 | object_unparent(sec); |
| 198 | g_free(pw); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | static void test_secret_conv_base64_utf8invalid(void) |
| 203 | { |
| 204 | Object *sec = object_new_with_props( |
| 205 | TYPE_QCRYPTO_SECRET, |
| 206 | object_get_objects_root(), |
| 207 | "sec0", |
| 208 | &error_abort, |
| 209 | "data", "f0VMRgIBAQAAAA==", |
| 210 | "format", "base64", |
| 211 | NULL); |
| 212 | |
| 213 | char *pw = qcrypto_secret_lookup_as_utf8("sec0", |
| 214 | NULL); |
| 215 | g_assert(pw == NULL); |
| 216 | |
| 217 | object_unparent(sec); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | static void test_secret_conv_utf8_base64(void) |
| 222 | { |
| 223 | Object *sec = object_new_with_props( |
| 224 | TYPE_QCRYPTO_SECRET, |
| 225 | object_get_objects_root(), |
| 226 | "sec0", |
| 227 | &error_abort, |
| 228 | "data", "123456", |
| 229 | NULL); |
| 230 | |
| 231 | char *pw = qcrypto_secret_lookup_as_base64("sec0", |
| 232 | &error_abort); |
| 233 | |
| 234 | g_assert_cmpstr(pw, ==, "MTIzNDU2"); |
| 235 | |
| 236 | object_unparent(sec); |
| 237 | g_free(pw); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | static void test_secret_crypt_raw(void) |
| 242 | { |
| 243 | Object *master = object_new_with_props( |
| 244 | TYPE_QCRYPTO_SECRET, |
| 245 | object_get_objects_root(), |
| 246 | "master", |
| 247 | &error_abort, |
| 248 | "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=", |
| 249 | "format", "base64", |
| 250 | NULL); |
| 251 | Object *sec = object_new_with_props( |
| 252 | TYPE_QCRYPTO_SECRET, |
| 253 | object_get_objects_root(), |
| 254 | "sec0", |
| 255 | &error_abort, |
| 256 | "data", |
| 257 | "\xCC\xBF\xF7\x09\x46\x19\x0B\x52\x2A\x3A\xB4\x6B\xCD\x7A\xB0\xB0", |
| 258 | "format", "raw", |
| 259 | "keyid", "master", |
| 260 | "iv", "0I7Gw/TKuA+Old2W2apQ3g==", |
| 261 | NULL); |
| 262 | |
| 263 | char *pw = qcrypto_secret_lookup_as_utf8("sec0", |
| 264 | &error_abort); |
| 265 | |
| 266 | g_assert_cmpstr(pw, ==, "123456"); |
| 267 | |
| 268 | object_unparent(sec); |
| 269 | object_unparent(master); |
| 270 | g_free(pw); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | static void test_secret_crypt_base64(void) |
| 275 | { |
| 276 | Object *master = object_new_with_props( |
| 277 | TYPE_QCRYPTO_SECRET, |
| 278 | object_get_objects_root(), |
| 279 | "master", |
| 280 | &error_abort, |
| 281 | "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=", |
| 282 | "format", "base64", |
| 283 | NULL); |
| 284 | Object *sec = object_new_with_props( |
| 285 | TYPE_QCRYPTO_SECRET, |
| 286 | object_get_objects_root(), |
| 287 | "sec0", |
| 288 | &error_abort, |
| 289 | "data", "zL/3CUYZC1IqOrRrzXqwsA==", |
| 290 | "format", "base64", |
| 291 | "keyid", "master", |
| 292 | "iv", "0I7Gw/TKuA+Old2W2apQ3g==", |
| 293 | NULL); |
| 294 | |
| 295 | char *pw = qcrypto_secret_lookup_as_utf8("sec0", |
| 296 | &error_abort); |
| 297 | |
| 298 | g_assert_cmpstr(pw, ==, "123456"); |
| 299 | |
| 300 | object_unparent(sec); |
| 301 | object_unparent(master); |
| 302 | g_free(pw); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | static void test_secret_crypt_short_key(void) |
| 307 | { |
| 308 | Object *master = object_new_with_props( |
| 309 | TYPE_QCRYPTO_SECRET, |
| 310 | object_get_objects_root(), |
| 311 | "master", |
| 312 | &error_abort, |
| 313 | "data", "9miloPQCzGy+TL6aonfzVc", |
| 314 | "format", "base64", |
| 315 | NULL); |
| 316 | Object *sec = object_new_with_props( |
| 317 | TYPE_QCRYPTO_SECRET, |
| 318 | object_get_objects_root(), |
| 319 | "sec0", |
| 320 | NULL, |
| 321 | "data", "zL/3CUYZC1IqOrRrzXqwsA==", |
| 322 | "format", "raw", |
| 323 | "keyid", "master", |
| 324 | "iv", "0I7Gw/TKuA+Old2W2apQ3g==", |
| 325 | NULL); |
| 326 | |
| 327 | g_assert(sec == NULL); |
| 328 | object_unparent(master); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | static void test_secret_crypt_short_iv(void) |
| 333 | { |
| 334 | Object *master = object_new_with_props( |
| 335 | TYPE_QCRYPTO_SECRET, |
| 336 | object_get_objects_root(), |
| 337 | "master", |
| 338 | &error_abort, |
| 339 | "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=", |
| 340 | "format", "base64", |
| 341 | NULL); |
| 342 | Object *sec = object_new_with_props( |
| 343 | TYPE_QCRYPTO_SECRET, |
| 344 | object_get_objects_root(), |
| 345 | "sec0", |
| 346 | NULL, |
| 347 | "data", "zL/3CUYZC1IqOrRrzXqwsA==", |
| 348 | "format", "raw", |
| 349 | "keyid", "master", |
| 350 | "iv", "0I7Gw/TKuA+Old2W2a", |
| 351 | NULL); |
| 352 | |
| 353 | g_assert(sec == NULL); |
| 354 | object_unparent(master); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | static void test_secret_crypt_missing_iv(void) |
| 359 | { |
| 360 | Object *master = object_new_with_props( |
| 361 | TYPE_QCRYPTO_SECRET, |
| 362 | object_get_objects_root(), |
| 363 | "master", |
| 364 | &error_abort, |
| 365 | "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=", |
| 366 | "format", "base64", |
| 367 | NULL); |
| 368 | Object *sec = object_new_with_props( |
| 369 | TYPE_QCRYPTO_SECRET, |
| 370 | object_get_objects_root(), |
| 371 | "sec0", |
| 372 | NULL, |
| 373 | "data", "zL/3CUYZC1IqOrRrzXqwsA==", |
| 374 | "format", "raw", |
| 375 | "keyid", "master", |
| 376 | NULL); |
| 377 | |
| 378 | g_assert(sec == NULL); |
| 379 | object_unparent(master); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | static void test_secret_crypt_bad_iv(void) |
| 384 | { |
| 385 | Object *master = object_new_with_props( |
| 386 | TYPE_QCRYPTO_SECRET, |
| 387 | object_get_objects_root(), |
| 388 | "master", |
| 389 | &error_abort, |
| 390 | "data", "9miloPQCzGy+TL6aonfzVcptibCmCIhKzrnlfwiWivk=", |
| 391 | "format", "base64", |
| 392 | NULL); |
| 393 | Object *sec = object_new_with_props( |
| 394 | TYPE_QCRYPTO_SECRET, |
| 395 | object_get_objects_root(), |
| 396 | "sec0", |
| 397 | NULL, |
| 398 | "data", "zL/3CUYZC1IqOrRrzXqwsA==", |
| 399 | "format", "raw", |
| 400 | "keyid", "master", |
| 401 | "iv", "0I7Gw/TK$$uA+Old2W2a", |
| 402 | NULL); |
| 403 | |
| 404 | g_assert(sec == NULL); |
| 405 | object_unparent(master); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | int main(int argc, char **argv) |
| 410 | { |
| 411 | module_call_init(MODULE_INIT_QOM); |
| 412 | g_test_init(&argc, &argv, NULL); |
| 413 | |
| 414 | g_assert(qcrypto_init(NULL) == 0); |
| 415 | |
| 416 | g_test_add_func("/crypto/secret/direct", |
| 417 | test_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); |
| 424 | |
| 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); |
| 437 | |
| 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); |
| 450 | |
| 451 | return g_test_run(); |
| 452 | } |