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 "qom/object_interfaces.h"
25 #include "qemu/base64.h"
30 qcrypto_secret_load_data(QCryptoSecret
*secret
,
45 "'file' and 'data' are mutually exclusive");
48 if (!g_file_get_contents(secret
->file
, &data
, &length
, &gerr
)) {
50 "Unable to read %s: %s",
51 secret
->file
, gerr
->message
);
55 *output
= (uint8_t *)data
;
57 } else if (secret
->data
) {
58 *outputlen
= strlen(secret
->data
);
59 *output
= (uint8_t *)g_strdup(secret
->data
);
61 error_setg(errp
, "Either 'file' or 'data' must be provided");
66 static void qcrypto_secret_decrypt(QCryptoSecret
*secret
,
73 uint8_t *key
= NULL
, *ciphertext
= NULL
, *iv
= NULL
;
74 size_t keylen
, ciphertextlen
, ivlen
;
75 QCryptoCipher
*aes
= NULL
;
76 uint8_t *plaintext
= NULL
;
81 if (qcrypto_secret_lookup(secret
->keyid
,
88 error_setg(errp
, "Key should be 32 bytes in length");
93 error_setg(errp
, "IV is required to decrypt secret");
97 iv
= qbase64_decode(secret
->iv
, -1, &ivlen
, errp
);
102 error_setg(errp
, "IV should be 16 bytes in length not %zu",
107 aes
= qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_256
,
108 QCRYPTO_CIPHER_MODE_CBC
,
115 if (qcrypto_cipher_setiv(aes
, iv
, ivlen
, errp
) < 0) {
119 if (secret
->format
== QCRYPTO_SECRET_FORMAT_BASE64
) {
120 ciphertext
= qbase64_decode((const gchar
*)input
,
127 plaintext
= g_new0(uint8_t, ciphertextlen
+ 1);
129 ciphertextlen
= inputlen
;
130 plaintext
= g_new0(uint8_t, inputlen
+ 1);
132 if (qcrypto_cipher_decrypt(aes
,
133 ciphertext
? ciphertext
: input
,
141 if (plaintext
[ciphertextlen
- 1] > 16 ||
142 plaintext
[ciphertextlen
- 1] > ciphertextlen
) {
143 error_setg(errp
, "Incorrect number of padding bytes (%d) "
144 "found on decrypted data",
145 (int)plaintext
[ciphertextlen
- 1]);
151 /* Even though plaintext may contain arbitrary NUL
152 * ensure it is explicitly NUL terminated.
154 ciphertextlen
-= plaintext
[ciphertextlen
- 1];
155 plaintext
[ciphertextlen
] = '\0';
158 *outputlen
= ciphertextlen
;
164 qcrypto_cipher_free(aes
);
168 static void qcrypto_secret_decode(const uint8_t *input
,
174 *output
= qbase64_decode((const gchar
*)input
,
182 qcrypto_secret_prop_set_loaded(Object
*obj
,
186 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
189 Error
*local_err
= NULL
;
190 uint8_t *input
= NULL
;
192 uint8_t *output
= NULL
;
193 size_t outputlen
= 0;
195 qcrypto_secret_load_data(secret
, &input
, &inputlen
, &local_err
);
197 error_propagate(errp
, local_err
);
202 qcrypto_secret_decrypt(secret
, input
, inputlen
,
203 &output
, &outputlen
, &local_err
);
206 error_propagate(errp
, local_err
);
210 inputlen
= outputlen
;
212 if (secret
->format
!= QCRYPTO_SECRET_FORMAT_RAW
) {
213 qcrypto_secret_decode(input
, inputlen
,
214 &output
, &outputlen
, &local_err
);
217 error_propagate(errp
, local_err
);
221 inputlen
= outputlen
;
225 secret
->rawdata
= input
;
226 secret
->rawlen
= inputlen
;
228 g_free(secret
->rawdata
);
235 qcrypto_secret_prop_get_loaded(Object
*obj
,
236 Error
**errp G_GNUC_UNUSED
)
238 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
239 return secret
->data
!= NULL
;
244 qcrypto_secret_prop_set_format(Object
*obj
,
246 Error
**errp G_GNUC_UNUSED
)
248 QCryptoSecret
*creds
= QCRYPTO_SECRET(obj
);
250 creds
->format
= value
;
255 qcrypto_secret_prop_get_format(Object
*obj
,
256 Error
**errp G_GNUC_UNUSED
)
258 QCryptoSecret
*creds
= QCRYPTO_SECRET(obj
);
260 return creds
->format
;
265 qcrypto_secret_prop_set_data(Object
*obj
,
269 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
271 g_free(secret
->data
);
272 secret
->data
= g_strdup(value
);
277 qcrypto_secret_prop_get_data(Object
*obj
,
280 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
281 return g_strdup(secret
->data
);
286 qcrypto_secret_prop_set_file(Object
*obj
,
290 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
292 g_free(secret
->file
);
293 secret
->file
= g_strdup(value
);
298 qcrypto_secret_prop_get_file(Object
*obj
,
301 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
302 return g_strdup(secret
->file
);
307 qcrypto_secret_prop_set_iv(Object
*obj
,
311 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
314 secret
->iv
= g_strdup(value
);
319 qcrypto_secret_prop_get_iv(Object
*obj
,
322 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
323 return g_strdup(secret
->iv
);
328 qcrypto_secret_prop_set_keyid(Object
*obj
,
332 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
334 g_free(secret
->keyid
);
335 secret
->keyid
= g_strdup(value
);
340 qcrypto_secret_prop_get_keyid(Object
*obj
,
343 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
344 return g_strdup(secret
->keyid
);
349 qcrypto_secret_complete(UserCreatable
*uc
, Error
**errp
)
351 object_property_set_bool(OBJECT(uc
), true, "loaded", errp
);
356 qcrypto_secret_finalize(Object
*obj
)
358 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
361 g_free(secret
->file
);
362 g_free(secret
->keyid
);
363 g_free(secret
->rawdata
);
364 g_free(secret
->data
);
368 qcrypto_secret_class_init(ObjectClass
*oc
, void *data
)
370 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
372 ucc
->complete
= qcrypto_secret_complete
;
374 object_class_property_add_bool(oc
, "loaded",
375 qcrypto_secret_prop_get_loaded
,
376 qcrypto_secret_prop_set_loaded
,
378 object_class_property_add_enum(oc
, "format",
379 "QCryptoSecretFormat",
380 QCryptoSecretFormat_lookup
,
381 qcrypto_secret_prop_get_format
,
382 qcrypto_secret_prop_set_format
,
384 object_class_property_add_str(oc
, "data",
385 qcrypto_secret_prop_get_data
,
386 qcrypto_secret_prop_set_data
,
388 object_class_property_add_str(oc
, "file",
389 qcrypto_secret_prop_get_file
,
390 qcrypto_secret_prop_set_file
,
392 object_class_property_add_str(oc
, "keyid",
393 qcrypto_secret_prop_get_keyid
,
394 qcrypto_secret_prop_set_keyid
,
396 object_class_property_add_str(oc
, "iv",
397 qcrypto_secret_prop_get_iv
,
398 qcrypto_secret_prop_set_iv
,
403 int qcrypto_secret_lookup(const char *secretid
,
409 QCryptoSecret
*secret
;
411 obj
= object_resolve_path_component(
412 object_get_objects_root(), secretid
);
414 error_setg(errp
, "No secret with id '%s'", secretid
);
418 secret
= (QCryptoSecret
*)
419 object_dynamic_cast(obj
,
420 TYPE_QCRYPTO_SECRET
);
422 error_setg(errp
, "Object with id '%s' is not a secret",
427 if (!secret
->rawdata
) {
428 error_setg(errp
, "Secret with id '%s' has no data",
433 *data
= g_new0(uint8_t, secret
->rawlen
+ 1);
434 memcpy(*data
, secret
->rawdata
, secret
->rawlen
);
435 (*data
)[secret
->rawlen
] = '\0';
436 *datalen
= secret
->rawlen
;
442 char *qcrypto_secret_lookup_as_utf8(const char *secretid
,
448 if (qcrypto_secret_lookup(secretid
,
455 if (!g_utf8_validate((const gchar
*)data
, datalen
, NULL
)) {
457 "Data from secret %s is not valid UTF-8",
467 char *qcrypto_secret_lookup_as_base64(const char *secretid
,
474 if (qcrypto_secret_lookup(secretid
,
481 ret
= g_base64_encode(data
, datalen
);
487 static const TypeInfo qcrypto_secret_info
= {
488 .parent
= TYPE_OBJECT
,
489 .name
= TYPE_QCRYPTO_SECRET
,
490 .instance_size
= sizeof(QCryptoSecret
),
491 .instance_finalize
= qcrypto_secret_finalize
,
492 .class_size
= sizeof(QCryptoSecretClass
),
493 .class_init
= qcrypto_secret_class_init
,
494 .interfaces
= (InterfaceInfo
[]) {
495 { TYPE_USER_CREATABLE
},
502 qcrypto_secret_register_types(void)
504 type_register_static(&qcrypto_secret_info
);
508 type_init(qcrypto_secret_register_types
);