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 "crypto/secret.h"
22 #include "crypto/cipher.h"
23 #include "qom/object_interfaces.h"
24 #include "qemu/base64.h"
29 qcrypto_secret_load_data(QCryptoSecret
*secret
,
44 "'file' and 'data' are mutually exclusive");
47 if (!g_file_get_contents(secret
->file
, &data
, &length
, &gerr
)) {
49 "Unable to read %s: %s",
50 secret
->file
, gerr
->message
);
54 *output
= (uint8_t *)data
;
56 } else if (secret
->data
) {
57 *outputlen
= strlen(secret
->data
);
58 *output
= (uint8_t *)g_strdup(secret
->data
);
60 error_setg(errp
, "Either 'file' or 'data' must be provided");
65 static void qcrypto_secret_decrypt(QCryptoSecret
*secret
,
72 uint8_t *key
= NULL
, *ciphertext
= NULL
, *iv
= NULL
;
73 size_t keylen
, ciphertextlen
, ivlen
;
74 QCryptoCipher
*aes
= NULL
;
75 uint8_t *plaintext
= NULL
;
80 if (qcrypto_secret_lookup(secret
->keyid
,
87 error_setg(errp
, "Key should be 32 bytes in length");
92 error_setg(errp
, "IV is required to decrypt secret");
96 iv
= qbase64_decode(secret
->iv
, -1, &ivlen
, errp
);
101 error_setg(errp
, "IV should be 16 bytes in length not %zu",
106 aes
= qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_256
,
107 QCRYPTO_CIPHER_MODE_CBC
,
114 if (qcrypto_cipher_setiv(aes
, iv
, ivlen
, errp
) < 0) {
118 if (secret
->format
== QCRYPTO_SECRET_FORMAT_BASE64
) {
119 ciphertext
= qbase64_decode((const gchar
*)input
,
126 plaintext
= g_new0(uint8_t, ciphertextlen
+ 1);
128 ciphertextlen
= inputlen
;
129 plaintext
= g_new0(uint8_t, inputlen
+ 1);
131 if (qcrypto_cipher_decrypt(aes
,
132 ciphertext
? ciphertext
: input
,
140 if (plaintext
[ciphertextlen
- 1] > 16 ||
141 plaintext
[ciphertextlen
- 1] > ciphertextlen
) {
142 error_setg(errp
, "Incorrect number of padding bytes (%d) "
143 "found on decrypted data",
144 (int)plaintext
[ciphertextlen
- 1]);
150 /* Even though plaintext may contain arbitrary NUL
151 * ensure it is explicitly NUL terminated.
153 ciphertextlen
-= plaintext
[ciphertextlen
- 1];
154 plaintext
[ciphertextlen
] = '\0';
157 *outputlen
= ciphertextlen
;
163 qcrypto_cipher_free(aes
);
167 static void qcrypto_secret_decode(const uint8_t *input
,
173 *output
= qbase64_decode((const gchar
*)input
,
181 qcrypto_secret_prop_set_loaded(Object
*obj
,
185 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
188 Error
*local_err
= NULL
;
189 uint8_t *input
= NULL
;
191 uint8_t *output
= NULL
;
192 size_t outputlen
= 0;
194 qcrypto_secret_load_data(secret
, &input
, &inputlen
, &local_err
);
196 error_propagate(errp
, local_err
);
201 qcrypto_secret_decrypt(secret
, input
, inputlen
,
202 &output
, &outputlen
, &local_err
);
205 error_propagate(errp
, local_err
);
209 inputlen
= outputlen
;
211 if (secret
->format
!= QCRYPTO_SECRET_FORMAT_RAW
) {
212 qcrypto_secret_decode(input
, inputlen
,
213 &output
, &outputlen
, &local_err
);
216 error_propagate(errp
, local_err
);
220 inputlen
= outputlen
;
224 secret
->rawdata
= input
;
225 secret
->rawlen
= inputlen
;
227 g_free(secret
->rawdata
);
234 qcrypto_secret_prop_get_loaded(Object
*obj
,
235 Error
**errp G_GNUC_UNUSED
)
237 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
238 return secret
->data
!= NULL
;
243 qcrypto_secret_prop_set_format(Object
*obj
,
245 Error
**errp G_GNUC_UNUSED
)
247 QCryptoSecret
*creds
= QCRYPTO_SECRET(obj
);
249 creds
->format
= value
;
254 qcrypto_secret_prop_get_format(Object
*obj
,
255 Error
**errp G_GNUC_UNUSED
)
257 QCryptoSecret
*creds
= QCRYPTO_SECRET(obj
);
259 return creds
->format
;
264 qcrypto_secret_prop_set_data(Object
*obj
,
268 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
270 g_free(secret
->data
);
271 secret
->data
= g_strdup(value
);
276 qcrypto_secret_prop_get_data(Object
*obj
,
279 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
280 return g_strdup(secret
->data
);
285 qcrypto_secret_prop_set_file(Object
*obj
,
289 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
291 g_free(secret
->file
);
292 secret
->file
= g_strdup(value
);
297 qcrypto_secret_prop_get_file(Object
*obj
,
300 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
301 return g_strdup(secret
->file
);
306 qcrypto_secret_prop_set_iv(Object
*obj
,
310 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
313 secret
->iv
= g_strdup(value
);
318 qcrypto_secret_prop_get_iv(Object
*obj
,
321 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
322 return g_strdup(secret
->iv
);
327 qcrypto_secret_prop_set_keyid(Object
*obj
,
331 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
333 g_free(secret
->keyid
);
334 secret
->keyid
= g_strdup(value
);
339 qcrypto_secret_prop_get_keyid(Object
*obj
,
342 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
343 return g_strdup(secret
->keyid
);
348 qcrypto_secret_complete(UserCreatable
*uc
, Error
**errp
)
350 object_property_set_bool(OBJECT(uc
), true, "loaded", errp
);
355 qcrypto_secret_init(Object
*obj
)
357 object_property_add_bool(obj
, "loaded",
358 qcrypto_secret_prop_get_loaded
,
359 qcrypto_secret_prop_set_loaded
,
361 object_property_add_enum(obj
, "format",
362 "QCryptoSecretFormat",
363 QCryptoSecretFormat_lookup
,
364 qcrypto_secret_prop_get_format
,
365 qcrypto_secret_prop_set_format
,
367 object_property_add_str(obj
, "data",
368 qcrypto_secret_prop_get_data
,
369 qcrypto_secret_prop_set_data
,
371 object_property_add_str(obj
, "file",
372 qcrypto_secret_prop_get_file
,
373 qcrypto_secret_prop_set_file
,
375 object_property_add_str(obj
, "keyid",
376 qcrypto_secret_prop_get_keyid
,
377 qcrypto_secret_prop_set_keyid
,
379 object_property_add_str(obj
, "iv",
380 qcrypto_secret_prop_get_iv
,
381 qcrypto_secret_prop_set_iv
,
387 qcrypto_secret_finalize(Object
*obj
)
389 QCryptoSecret
*secret
= QCRYPTO_SECRET(obj
);
392 g_free(secret
->file
);
393 g_free(secret
->keyid
);
394 g_free(secret
->rawdata
);
395 g_free(secret
->data
);
399 qcrypto_secret_class_init(ObjectClass
*oc
, void *data
)
401 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
403 ucc
->complete
= qcrypto_secret_complete
;
407 int qcrypto_secret_lookup(const char *secretid
,
413 QCryptoSecret
*secret
;
415 obj
= object_resolve_path_component(
416 object_get_objects_root(), secretid
);
418 error_setg(errp
, "No secret with id '%s'", secretid
);
422 secret
= (QCryptoSecret
*)
423 object_dynamic_cast(obj
,
424 TYPE_QCRYPTO_SECRET
);
426 error_setg(errp
, "Object with id '%s' is not a secret",
431 if (!secret
->rawdata
) {
432 error_setg(errp
, "Secret with id '%s' has no data",
437 *data
= g_new0(uint8
, secret
->rawlen
+ 1);
438 memcpy(*data
, secret
->rawdata
, secret
->rawlen
);
439 (*data
)[secret
->rawlen
] = '\0';
440 *datalen
= secret
->rawlen
;
446 char *qcrypto_secret_lookup_as_utf8(const char *secretid
,
452 if (qcrypto_secret_lookup(secretid
,
459 if (!g_utf8_validate((const gchar
*)data
, datalen
, NULL
)) {
461 "Data from secret %s is not valid UTF-8",
471 char *qcrypto_secret_lookup_as_base64(const char *secretid
,
478 if (qcrypto_secret_lookup(secretid
,
485 ret
= g_base64_encode(data
, datalen
);
491 static const TypeInfo qcrypto_secret_info
= {
492 .parent
= TYPE_OBJECT
,
493 .name
= TYPE_QCRYPTO_SECRET
,
494 .instance_size
= sizeof(QCryptoSecret
),
495 .instance_init
= qcrypto_secret_init
,
496 .instance_finalize
= qcrypto_secret_finalize
,
497 .class_size
= sizeof(QCryptoSecretClass
),
498 .class_init
= qcrypto_secret_class_init
,
499 .interfaces
= (InterfaceInfo
[]) {
500 { TYPE_USER_CREATABLE
},
507 qcrypto_secret_register_types(void)
509 type_register_static(&qcrypto_secret_info
);
513 type_init(qcrypto_secret_register_types
);