2 * QEMU crypto secret support
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"
22 #include "crypto/secret.h"
23 #include "crypto/cipher.h"
24 #include "qapi/error.h"
25 #include "qom/object_interfaces.h"
26 #include "qemu/base64.h"
31 qcrypto_secret_load_data(QCryptoSecret
*secret
,
46 "'file' and 'data' are mutually exclusive");
49 if (!g_file_get_contents(secret
->file
, &data
, &length
, &gerr
)) {
51 "Unable to read %s: %s",
52 secret
->file
, gerr
->message
);
56 *output
= (uint8_t *)data
;
58 } else if (secret
->data
) {
59 *outputlen
= strlen(secret
->data
);
60 *output
= (uint8_t *)g_strdup(secret
->data
);
62 error_setg(errp
, "Either 'file' or 'data' must be provided");
67 static void qcrypto_secret_decrypt(QCryptoSecret
*secret
,
74 uint8_t *key
= NULL
, *ciphertext
= NULL
, *iv
= NULL
;
75 size_t keylen
, ciphertextlen
, ivlen
;
76 QCryptoCipher
*aes
= NULL
;
77 uint8_t *plaintext
= NULL
;
82 if (qcrypto_secret_lookup(secret
->keyid
,
89 error_setg(errp
, "Key should be 32 bytes in length");
94 error_setg(errp
, "IV is required to decrypt secret");
98 iv
= qbase64_decode(secret
->iv
, -1, &ivlen
, errp
);
103 error_setg(errp
, "IV should be 16 bytes in length not %zu",
108 aes
= qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_256
,
109 QCRYPTO_CIPHER_MODE_CBC
,
116 if (qcrypto_cipher_setiv(aes
, iv
, ivlen
, errp
) < 0) {
120 if (secret
->format
== QCRYPTO_SECRET_FORMAT_BASE64
) {
121 ciphertext
= qbase64_decode((const gchar
*)input
,
128 plaintext
= g_new0(uint8_t, ciphertextlen
+ 1);
130 ciphertextlen
= inputlen
;
131 plaintext
= g_new0(uint8_t, inputlen
+ 1);
133 if (qcrypto_cipher_decrypt(aes
,
134 ciphertext
? ciphertext
: input
,
142 if (plaintext
[ciphertextlen
- 1] > 16 ||
143 plaintext
[ciphertextlen
- 1] > ciphertextlen
) {
144 error_setg(errp
, "Incorrect number of padding bytes (%d) "
145 "found on decrypted data",
146 (int)plaintext
[ciphertextlen
- 1]);
152 /* Even though plaintext may contain arbitrary NUL
153 * ensure it is explicitly NUL terminated.
155 ciphertextlen
-= plaintext
[ciphertextlen
- 1];
156 plaintext
[ciphertextlen
] = '\0';
159 *outputlen
= ciphertextlen
;
165 qcrypto_cipher_free(aes
);
169 static void qcrypto_secret_decode(const uint8_t *input
,
175 *output
= qbase64_decode((const gchar
*)input
,
183 qcrypto_secret_prop_set_loaded(Object
*obj
,
187 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
190 Error
*local_err
= NULL
;
191 uint8_t *input
= NULL
;
193 uint8_t *output
= NULL
;
194 size_t outputlen
= 0;
196 qcrypto_secret_load_data(secret
, &input
, &inputlen
, &local_err
);
198 error_propagate(errp
, local_err
);
203 qcrypto_secret_decrypt(secret
, input
, inputlen
,
204 &output
, &outputlen
, &local_err
);
207 error_propagate(errp
, local_err
);
211 inputlen
= outputlen
;
213 if (secret
->format
!= QCRYPTO_SECRET_FORMAT_RAW
) {
214 qcrypto_secret_decode(input
, inputlen
,
215 &output
, &outputlen
, &local_err
);
218 error_propagate(errp
, local_err
);
222 inputlen
= outputlen
;
226 secret
->rawdata
= input
;
227 secret
->rawlen
= inputlen
;
229 g_free(secret
->rawdata
);
236 qcrypto_secret_prop_get_loaded(Object
*obj
,
237 Error
**errp G_GNUC_UNUSED
)
239 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
240 return secret
->data
!= NULL
;
245 qcrypto_secret_prop_set_format(Object
*obj
,
247 Error
**errp G_GNUC_UNUSED
)
249 QCryptoSecret
*creds
= QCRYPTO_SECRET(obj
);
251 creds
->format
= value
;
256 qcrypto_secret_prop_get_format(Object
*obj
,
257 Error
**errp G_GNUC_UNUSED
)
259 QCryptoSecret
*creds
= QCRYPTO_SECRET(obj
);
261 return creds
->format
;
266 qcrypto_secret_prop_set_data(Object
*obj
,
270 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
272 g_free(secret
->data
);
273 secret
->data
= g_strdup(value
);
278 qcrypto_secret_prop_get_data(Object
*obj
,
281 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
282 return g_strdup(secret
->data
);
287 qcrypto_secret_prop_set_file(Object
*obj
,
291 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
293 g_free(secret
->file
);
294 secret
->file
= g_strdup(value
);
299 qcrypto_secret_prop_get_file(Object
*obj
,
302 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
303 return g_strdup(secret
->file
);
308 qcrypto_secret_prop_set_iv(Object
*obj
,
312 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
315 secret
->iv
= g_strdup(value
);
320 qcrypto_secret_prop_get_iv(Object
*obj
,
323 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
324 return g_strdup(secret
->iv
);
329 qcrypto_secret_prop_set_keyid(Object
*obj
,
333 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
335 g_free(secret
->keyid
);
336 secret
->keyid
= g_strdup(value
);
341 qcrypto_secret_prop_get_keyid(Object
*obj
,
344 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
345 return g_strdup(secret
->keyid
);
350 qcrypto_secret_complete(UserCreatable
*uc
, Error
**errp
)
352 object_property_set_bool(OBJECT(uc
), true, "loaded", errp
);
357 qcrypto_secret_finalize(Object
*obj
)
359 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
362 g_free(secret
->file
);
363 g_free(secret
->keyid
);
364 g_free(secret
->rawdata
);
365 g_free(secret
->data
);
369 qcrypto_secret_class_init(ObjectClass
*oc
, void *data
)
371 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
373 ucc
->complete
= qcrypto_secret_complete
;
375 object_class_property_add_bool(oc
, "loaded",
376 qcrypto_secret_prop_get_loaded
,
377 qcrypto_secret_prop_set_loaded
,
379 object_class_property_add_enum(oc
, "format",
380 "QCryptoSecretFormat",
381 QCryptoSecretFormat_lookup
,
382 qcrypto_secret_prop_get_format
,
383 qcrypto_secret_prop_set_format
,
385 object_class_property_add_str(oc
, "data",
386 qcrypto_secret_prop_get_data
,
387 qcrypto_secret_prop_set_data
,
389 object_class_property_add_str(oc
, "file",
390 qcrypto_secret_prop_get_file
,
391 qcrypto_secret_prop_set_file
,
393 object_class_property_add_str(oc
, "keyid",
394 qcrypto_secret_prop_get_keyid
,
395 qcrypto_secret_prop_set_keyid
,
397 object_class_property_add_str(oc
, "iv",
398 qcrypto_secret_prop_get_iv
,
399 qcrypto_secret_prop_set_iv
,
404 int qcrypto_secret_lookup(const char *secretid
,
410 QCryptoSecret
*secret
;
412 obj
= object_resolve_path_component(
413 object_get_objects_root(), secretid
);
415 error_setg(errp
, "No secret with id '%s'", secretid
);
419 secret
= (QCryptoSecret
*)
420 object_dynamic_cast(obj
,
421 TYPE_QCRYPTO_SECRET
);
423 error_setg(errp
, "Object with id '%s' is not a secret",
428 if (!secret
->rawdata
) {
429 error_setg(errp
, "Secret with id '%s' has no data",
434 *data
= g_new0(uint8_t, secret
->rawlen
+ 1);
435 memcpy(*data
, secret
->rawdata
, secret
->rawlen
);
436 (*data
)[secret
->rawlen
] = '\0';
437 *datalen
= secret
->rawlen
;
443 char *qcrypto_secret_lookup_as_utf8(const char *secretid
,
449 if (qcrypto_secret_lookup(secretid
,
456 if (!g_utf8_validate((const gchar
*)data
, datalen
, NULL
)) {
458 "Data from secret %s is not valid UTF-8",
468 char *qcrypto_secret_lookup_as_base64(const char *secretid
,
475 if (qcrypto_secret_lookup(secretid
,
482 ret
= g_base64_encode(data
, datalen
);
488 static const TypeInfo qcrypto_secret_info
= {
489 .parent
= TYPE_OBJECT
,
490 .name
= TYPE_QCRYPTO_SECRET
,
491 .instance_size
= sizeof(QCryptoSecret
),
492 .instance_finalize
= qcrypto_secret_finalize
,
493 .class_size
= sizeof(QCryptoSecretClass
),
494 .class_init
= qcrypto_secret_class_init
,
495 .interfaces
= (InterfaceInfo
[]) {
496 { TYPE_USER_CREATABLE
},
503 qcrypto_secret_register_types(void)
505 type_register_static(&qcrypto_secret_info
);
509 type_init(qcrypto_secret_register_types
);