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.1 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"
27 #include "qemu/module.h"
32 qcrypto_secret_load_data(QCryptoSecret *secret,
47 "'file' and 'data' are mutually exclusive");
50 if (!g_file_get_contents(secret->file, &data, &length, &gerr)) {
52 "Unable to read %s: %s",
53 secret->file, gerr->message);
57 *output = (uint8_t *)data;
59 } else if (secret->data) {
60 *outputlen = strlen(secret->data);
61 *output = (uint8_t *)g_strdup(secret->data);
63 error_setg(errp, "Either 'file' or 'data' must be provided");
68 static void qcrypto_secret_decrypt(QCryptoSecret *secret,
75 g_autofree uint8_t *key = NULL;
76 g_autofree uint8_t *ciphertext = NULL;
77 g_autofree uint8_t *iv = NULL;
78 size_t keylen, ciphertextlen, ivlen;
79 g_autoptr(QCryptoCipher) aes = NULL;
80 g_autofree uint8_t *plaintext = NULL;
85 if (qcrypto_secret_lookup(secret->keyid,
92 error_setg(errp, "Key should be 32 bytes in length");
97 error_setg(errp, "IV is required to decrypt secret");
101 iv = qbase64_decode(secret->iv, -1, &ivlen, errp);
106 error_setg(errp, "IV should be 16 bytes in length not %zu",
111 aes = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_256,
112 QCRYPTO_CIPHER_MODE_CBC,
119 if (qcrypto_cipher_setiv(aes, iv, ivlen, errp) < 0) {
123 if (secret->format == QCRYPTO_SECRET_FORMAT_BASE64) {
124 ciphertext = qbase64_decode((const gchar*)input,
131 plaintext = g_new0(uint8_t, ciphertextlen + 1);
133 ciphertextlen = inputlen;
134 plaintext = g_new0(uint8_t, inputlen + 1);
136 if (qcrypto_cipher_decrypt(aes,
137 ciphertext ? ciphertext : input,
144 if (plaintext[ciphertextlen - 1] > 16 ||
145 plaintext[ciphertextlen - 1] > ciphertextlen) {
146 error_setg(errp, "Incorrect number of padding bytes (%d) "
147 "found on decrypted data",
148 (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';
158 *output = g_steal_pointer(&plaintext);
159 *outputlen = ciphertextlen;
163 static void qcrypto_secret_decode(const uint8_t *input,
169 *output = qbase64_decode((const gchar*)input,
177 qcrypto_secret_prop_set_loaded(Object *obj,
181 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
184 Error *local_err = NULL;
185 uint8_t *input = NULL;
187 uint8_t *output = NULL;
188 size_t outputlen = 0;
190 qcrypto_secret_load_data(secret, &input, &inputlen, &local_err);
192 error_propagate(errp, local_err);
197 qcrypto_secret_decrypt(secret, input, inputlen,
198 &output, &outputlen, &local_err);
201 error_propagate(errp, local_err);
205 inputlen = outputlen;
207 if (secret->format == QCRYPTO_SECRET_FORMAT_BASE64) {
208 qcrypto_secret_decode(input, inputlen,
209 &output, &outputlen, &local_err);
212 error_propagate(errp, local_err);
216 inputlen = outputlen;
220 secret->rawdata = input;
221 secret->rawlen = inputlen;
223 g_free(secret->rawdata);
224 secret->rawdata = NULL;
231 qcrypto_secret_prop_get_loaded(Object *obj,
232 Error **errp G_GNUC_UNUSED)
234 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
235 return secret->rawdata != NULL;
240 qcrypto_secret_prop_set_format(Object *obj,
242 Error **errp G_GNUC_UNUSED)
244 QCryptoSecret *creds = QCRYPTO_SECRET(obj);
246 creds->format = value;
251 qcrypto_secret_prop_get_format(Object *obj,
252 Error **errp G_GNUC_UNUSED)
254 QCryptoSecret *creds = QCRYPTO_SECRET(obj);
256 return creds->format;
261 qcrypto_secret_prop_set_data(Object *obj,
265 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
267 g_free(secret->data);
268 secret->data = g_strdup(value);
273 qcrypto_secret_prop_get_data(Object *obj,
276 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
277 return g_strdup(secret->data);
282 qcrypto_secret_prop_set_file(Object *obj,
286 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
288 g_free(secret->file);
289 secret->file = g_strdup(value);
294 qcrypto_secret_prop_get_file(Object *obj,
297 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
298 return g_strdup(secret->file);
303 qcrypto_secret_prop_set_iv(Object *obj,
307 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
310 secret->iv = g_strdup(value);
315 qcrypto_secret_prop_get_iv(Object *obj,
318 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
319 return g_strdup(secret->iv);
324 qcrypto_secret_prop_set_keyid(Object *obj,
328 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
330 g_free(secret->keyid);
331 secret->keyid = g_strdup(value);
336 qcrypto_secret_prop_get_keyid(Object *obj,
339 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
340 return g_strdup(secret->keyid);
345 qcrypto_secret_complete(UserCreatable *uc, Error **errp)
347 object_property_set_bool(OBJECT(uc), true, "loaded", errp);
352 qcrypto_secret_finalize(Object *obj)
354 QCryptoSecret *secret = QCRYPTO_SECRET(obj);
357 g_free(secret->file);
358 g_free(secret->keyid);
359 g_free(secret->rawdata);
360 g_free(secret->data);
364 qcrypto_secret_class_init(ObjectClass *oc, void *data)
366 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
368 ucc->complete = qcrypto_secret_complete;
370 object_class_property_add_bool(oc, "loaded",
371 qcrypto_secret_prop_get_loaded,
372 qcrypto_secret_prop_set_loaded);
373 object_class_property_add_enum(oc, "format",
374 "QCryptoSecretFormat",
375 &QCryptoSecretFormat_lookup,
376 qcrypto_secret_prop_get_format,
377 qcrypto_secret_prop_set_format);
378 object_class_property_add_str(oc, "data",
379 qcrypto_secret_prop_get_data,
380 qcrypto_secret_prop_set_data);
381 object_class_property_add_str(oc, "file",
382 qcrypto_secret_prop_get_file,
383 qcrypto_secret_prop_set_file);
384 object_class_property_add_str(oc, "keyid",
385 qcrypto_secret_prop_get_keyid,
386 qcrypto_secret_prop_set_keyid);
387 object_class_property_add_str(oc, "iv",
388 qcrypto_secret_prop_get_iv,
389 qcrypto_secret_prop_set_iv);
393 int qcrypto_secret_lookup(const char *secretid,
399 QCryptoSecret *secret;
401 obj = object_resolve_path_component(
402 object_get_objects_root(), secretid);
404 error_setg(errp, "No secret with id '%s'", secretid);
408 secret = (QCryptoSecret *)
409 object_dynamic_cast(obj,
410 TYPE_QCRYPTO_SECRET);
412 error_setg(errp, "Object with id '%s' is not a secret",
417 if (!secret->rawdata) {
418 error_setg(errp, "Secret with id '%s' has no data",
423 *data = g_new0(uint8_t, secret->rawlen + 1);
424 memcpy(*data, secret->rawdata, secret->rawlen);
425 (*data)[secret->rawlen] = '\0';
426 *datalen = secret->rawlen;
432 char *qcrypto_secret_lookup_as_utf8(const char *secretid,
438 if (qcrypto_secret_lookup(secretid,
445 if (!g_utf8_validate((const gchar*)data, datalen, NULL)) {
447 "Data from secret %s is not valid UTF-8",
457 char *qcrypto_secret_lookup_as_base64(const char *secretid,
464 if (qcrypto_secret_lookup(secretid,
471 ret = g_base64_encode(data, datalen);
477 static const TypeInfo qcrypto_secret_info = {
478 .parent = TYPE_OBJECT,
479 .name = TYPE_QCRYPTO_SECRET,
480 .instance_size = sizeof(QCryptoSecret),
481 .instance_finalize = qcrypto_secret_finalize,
482 .class_size = sizeof(QCryptoSecretClass),
483 .class_init = qcrypto_secret_class_init,
484 .interfaces = (InterfaceInfo[]) {
485 { TYPE_USER_CREATABLE },
492 qcrypto_secret_register_types(void)
494 type_register_static(&qcrypto_secret_info);
498 type_init(qcrypto_secret_register_types);