2009-08-19 19:50:10 +00:00
|
|
|
/*
|
2014-03-28 03:26:44 +00:00
|
|
|
* virstorageencryption.c: volume encryption information
|
2009-08-19 19:50:10 +00:00
|
|
|
*
|
2014-03-06 14:57:47 +00:00
|
|
|
* Copyright (C) 2009-2014 Red Hat, Inc.
|
2009-08-19 19:50:10 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-08-19 19:50:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2009-08-14 18:06:59 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2009-08-19 19:50:10 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2012-12-04 12:04:07 +00:00
|
|
|
#include "virbuffer.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2014-03-28 03:26:44 +00:00
|
|
|
#include "virstorageencryption.h"
|
2012-12-13 18:13:21 +00:00
|
|
|
#include "virxml.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-13 18:01:25 +00:00
|
|
|
#include "viruuid.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2016-05-30 11:47:46 +00:00
|
|
|
#include "virsecret.h"
|
2016-06-01 23:21:26 +00:00
|
|
|
#include "virstring.h"
|
2009-08-19 19:50:10 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_STORAGE
|
|
|
|
|
2014-05-14 19:48:15 +00:00
|
|
|
VIR_ENUM_IMPL(virStorageEncryptionSecret,
|
2019-01-20 16:30:15 +00:00
|
|
|
VIR_STORAGE_ENCRYPTION_SECRET_TYPE_LAST,
|
|
|
|
"passphrase",
|
|
|
|
);
|
2009-08-19 19:50:10 +00:00
|
|
|
|
|
|
|
VIR_ENUM_IMPL(virStorageEncryptionFormat,
|
|
|
|
VIR_STORAGE_ENCRYPTION_FORMAT_LAST,
|
2019-01-20 16:30:15 +00:00
|
|
|
"default", "qcow", "luks",
|
|
|
|
);
|
2009-08-19 19:50:10 +00:00
|
|
|
|
2016-06-01 23:21:26 +00:00
|
|
|
static void
|
|
|
|
virStorageEncryptionInfoDefFree(virStorageEncryptionInfoDefPtr def)
|
|
|
|
{
|
|
|
|
VIR_FREE(def->cipher_name);
|
|
|
|
VIR_FREE(def->cipher_mode);
|
|
|
|
VIR_FREE(def->cipher_hash);
|
|
|
|
VIR_FREE(def->ivgen_name);
|
|
|
|
VIR_FREE(def->ivgen_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 19:50:10 +00:00
|
|
|
static void
|
|
|
|
virStorageEncryptionSecretFree(virStorageEncryptionSecretPtr secret)
|
|
|
|
{
|
|
|
|
if (!secret)
|
|
|
|
return;
|
2016-07-08 16:09:40 +00:00
|
|
|
virSecretLookupDefClear(&secret->seclookupdef);
|
2009-08-19 19:50:10 +00:00
|
|
|
VIR_FREE(secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
virStorageEncryptionFree(virStorageEncryptionPtr enc)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!enc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < enc->nsecrets; i++)
|
|
|
|
virStorageEncryptionSecretFree(enc->secrets[i]);
|
2016-06-01 23:21:26 +00:00
|
|
|
virStorageEncryptionInfoDefFree(&enc->encinfo);
|
2009-08-19 19:50:10 +00:00
|
|
|
VIR_FREE(enc->secrets);
|
|
|
|
VIR_FREE(enc);
|
|
|
|
}
|
|
|
|
|
2014-06-12 13:26:18 +00:00
|
|
|
static virStorageEncryptionSecretPtr
|
|
|
|
virStorageEncryptionSecretCopy(const virStorageEncryptionSecret *src)
|
|
|
|
{
|
2020-03-19 14:38:06 +00:00
|
|
|
virStorageEncryptionSecretPtr ret = g_new0(virStorageEncryptionSecret, 1);
|
2014-06-12 13:26:18 +00:00
|
|
|
|
2020-03-19 14:38:06 +00:00
|
|
|
ret->type = src->type;
|
|
|
|
virSecretLookupDefCopy(&ret->seclookupdef, &src->seclookupdef);
|
2014-06-12 13:26:18 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:21:26 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageEncryptionInfoDefCopy(const virStorageEncryptionInfoDef *src,
|
|
|
|
virStorageEncryptionInfoDefPtr dst)
|
|
|
|
{
|
|
|
|
dst->cipher_size = src->cipher_size;
|
2019-10-20 11:49:46 +00:00
|
|
|
dst->cipher_name = g_strdup(src->cipher_name);
|
|
|
|
dst->cipher_mode = g_strdup(src->cipher_mode);
|
|
|
|
dst->cipher_hash = g_strdup(src->cipher_hash);
|
|
|
|
dst->ivgen_name = g_strdup(src->ivgen_name);
|
|
|
|
dst->ivgen_hash = g_strdup(src->ivgen_hash);
|
2016-06-01 23:21:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-12 13:26:18 +00:00
|
|
|
virStorageEncryptionPtr
|
|
|
|
virStorageEncryptionCopy(const virStorageEncryption *src)
|
|
|
|
{
|
|
|
|
virStorageEncryptionPtr ret;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(ret) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(ret->secrets, src->nsecrets) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
ret->nsecrets = src->nsecrets;
|
|
|
|
ret->format = src->format;
|
|
|
|
|
|
|
|
for (i = 0; i < src->nsecrets; i++) {
|
|
|
|
if (!(ret->secrets[i] = virStorageEncryptionSecretCopy(src->secrets[i])))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:21:26 +00:00
|
|
|
if (virStorageEncryptionInfoDefCopy(&src->encinfo, &ret->encinfo) < 0)
|
|
|
|
goto error;
|
|
|
|
|
2014-06-12 13:26:18 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
error:
|
|
|
|
virStorageEncryptionFree(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-19 19:50:10 +00:00
|
|
|
static virStorageEncryptionSecretPtr
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageEncryptionSecretParse(xmlXPathContextPtr ctxt,
|
2009-08-19 19:50:10 +00:00
|
|
|
xmlNodePtr node)
|
|
|
|
{
|
2020-07-28 19:47:48 +00:00
|
|
|
VIR_XPATH_NODE_AUTORESTORE(ctxt)
|
2009-08-19 19:50:10 +00:00
|
|
|
virStorageEncryptionSecretPtr ret;
|
2016-05-25 10:51:30 +00:00
|
|
|
char *type_str = NULL;
|
2009-08-19 19:50:10 +00:00
|
|
|
|
2013-07-04 10:02:00 +00:00
|
|
|
if (VIR_ALLOC(ret) < 0)
|
2009-08-19 19:50:10 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ctxt->node = node;
|
|
|
|
|
2016-05-25 10:51:30 +00:00
|
|
|
if (!(type_str = virXPathString("string(./@type)", ctxt))) {
|
2012-07-18 10:50:44 +00:00
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("unknown volume encryption secret type"));
|
2009-08-19 19:50:10 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-05-25 10:51:30 +00:00
|
|
|
|
|
|
|
if ((ret->type = virStorageEncryptionSecretTypeFromString(type_str)) < 0) {
|
2014-01-10 16:41:33 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
2012-07-18 10:50:44 +00:00
|
|
|
_("unknown volume encryption secret type %s"),
|
|
|
|
type_str);
|
2009-08-19 19:50:10 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2016-05-30 11:47:46 +00:00
|
|
|
if (virSecretLookupParseSecret(node, &ret->seclookupdef) < 0)
|
Fix UUID handling in secrets/storage encryption APIs
Convert all the secret/storage encryption APIs / wire format to
handle UUIDs in raw format instead of non-canonical printable
format. Guarentees data format correctness.
* docs/schemas/storageencryption.rng: Make UUID mandatory for a secret
and validate fully
* docs/schemas/secret.rng: Fully validate UUID
* include/libvirt/libvirt.h, include/libvirt/libvirt.h.in, Add
virSecretLookupByUUID and virSecretGetUUID. Make
virSecretGetUUIDString follow normal API design pattern
* python/generator.py: Skip generation of virSecretGetUUID,
virSecretGetUUIDString and virSecretLookupByUUID
* python/libvir.c, python/libvirt-python-api.xml: Manual impl
of virSecretGetUUID,virSecretGetUUIDString and virSecretLookupByUUID
* qemud/remote.c: s/virSecretLookupByUUIDString/virSecretLookupByUUID/
Fix get_nonnull_secret/make_nonnull_secret to use unsigned char
* qemud/remote_protocol.x: Fix remote_nonnull_secret to use a
remote_uuid instead of remote_nonnull_string for UUID field.
Rename REMOTE_PROC_SECRET_LOOKUP_BY_UUID_STRING to
REMOTE_PROC_SECRET_LOOKUP_BY_UUID_STRING and make it take an
remote_uuid value
* qemud/remote_dispatch_args.h, qemud/remote_dispatch_prototypes.h,
qemud/remote_dispatch_ret.h, qemud/remote_dispatch_table.h,
qemud/remote_protocol.c, qemud/remote_protocol.h: Re-generate
* src/datatypes.h, src/datatypes.c: Store UUID in raw format instead
of printable. Change virGetSecret to use raw format UUID
* src/driver.h: Rename virDrvSecretLookupByUUIDString to
virDrvSecretLookupByUUID and use raw format UUID
* src/libvirt.c: Add virSecretLookupByUUID and virSecretGetUUID
and re-implement virSecretLookupByUUIDString and
virSecretGetUUIDString in terms of those
* src/libvirt_public.syms: Add virSecretLookupByUUID and
virSecretGetUUID
* src/remote_internal.c: Rename remoteSecretLookupByUUIDString
to remoteSecretLookupByUUID. Fix typo in args for
remoteSecretDefineXML impl. Use raw UUID format for
get_nonnull_secret and make_nonnull_secret
* src/storage_encryption_conf.c, src/storage_encryption_conf.h:
Storage UUID in raw format, and require it to be present in
XML. Use UUID parser to validate.
* secret_conf.h, secret_conf.c: Generate a UUID if none is provided.
Storage UUID in raw format.
* src/secret_driver.c: Adjust to deal with raw UUIDs. Save secrets
in a filed with printable UUID, instead of base64 UUID.
* src/virsh.c: Adjust for changed public API contract of
virSecretGetUUIDString.
* src/storage_Backend.c: DOn't undefine secret we just generated
upon successful volume creation. Fix to handle raw UUIDs. Generate
a non-clashing UUID
* src/qemu_driver.c: Change to use lookupByUUID instead of
lookupByUUIDString
2009-09-10 16:44:12 +00:00
|
|
|
goto cleanup;
|
2016-05-30 11:47:46 +00:00
|
|
|
|
|
|
|
VIR_FREE(type_str);
|
|
|
|
|
2009-08-19 19:50:10 +00:00
|
|
|
return ret;
|
|
|
|
|
2014-03-25 06:48:31 +00:00
|
|
|
cleanup:
|
2016-05-25 10:51:30 +00:00
|
|
|
VIR_FREE(type_str);
|
2009-08-19 19:50:10 +00:00
|
|
|
virStorageEncryptionSecretFree(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:21:26 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageEncryptionInfoParseCipher(xmlNodePtr info_node,
|
|
|
|
virStorageEncryptionInfoDefPtr info)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
char *size_str = NULL;
|
|
|
|
|
|
|
|
if (!(info->cipher_name = virXMLPropString(info_node, "name"))) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("cipher info missing 'name' attribute"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((size_str = virXMLPropString(info_node, "size")) &&
|
|
|
|
virStrToLong_uip(size_str, NULL, 10, &info->cipher_size) < 0) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("cannot parse cipher size: '%s'"),
|
|
|
|
size_str);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!size_str) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("cipher info missing 'size' attribute"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->cipher_mode = virXMLPropString(info_node, "mode");
|
|
|
|
info->cipher_hash = virXMLPropString(info_node, "hash");
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(size_str);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virStorageEncryptionInfoParseIvgen(xmlNodePtr info_node,
|
|
|
|
virStorageEncryptionInfoDefPtr info)
|
|
|
|
{
|
|
|
|
if (!(info->ivgen_name = virXMLPropString(info_node, "name"))) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("missing ivgen info name string"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->ivgen_hash = virXMLPropString(info_node, "hash");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-06 13:17:59 +00:00
|
|
|
virStorageEncryptionPtr
|
|
|
|
virStorageEncryptionParseNode(xmlNodePtr node,
|
|
|
|
xmlXPathContextPtr ctxt)
|
2009-08-19 19:50:10 +00:00
|
|
|
{
|
2020-07-28 19:47:48 +00:00
|
|
|
VIR_XPATH_NODE_AUTORESTORE(ctxt)
|
2009-08-19 19:50:10 +00:00
|
|
|
xmlNodePtr *nodes = NULL;
|
2018-03-06 13:57:17 +00:00
|
|
|
virStorageEncryptionPtr encdef = NULL;
|
|
|
|
virStorageEncryptionPtr ret = NULL;
|
2016-05-25 10:51:30 +00:00
|
|
|
char *format_str = NULL;
|
|
|
|
int n;
|
Convert 'int i' to 'size_t i' in src/conf/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2009-08-19 19:50:10 +00:00
|
|
|
|
2018-03-06 13:17:59 +00:00
|
|
|
ctxt->node = node;
|
|
|
|
|
2018-03-06 13:57:17 +00:00
|
|
|
if (VIR_ALLOC(encdef) < 0)
|
|
|
|
goto cleanup;
|
2009-08-19 19:50:10 +00:00
|
|
|
|
2016-05-25 10:51:30 +00:00
|
|
|
if (!(format_str = virXPathString("string(./@format)", ctxt))) {
|
2012-07-18 10:50:44 +00:00
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("unknown volume encryption format"));
|
2009-08-19 19:50:10 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-05-25 10:51:30 +00:00
|
|
|
|
2018-03-06 13:57:17 +00:00
|
|
|
if ((encdef->format =
|
2016-05-25 10:51:30 +00:00
|
|
|
virStorageEncryptionFormatTypeFromString(format_str)) < 0) {
|
2014-01-10 16:41:33 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
2012-07-18 10:50:44 +00:00
|
|
|
_("unknown volume encryption format type %s"),
|
|
|
|
format_str);
|
2009-08-19 19:50:10 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2016-05-25 10:51:30 +00:00
|
|
|
if ((n = virXPathNodeSet("./secret", ctxt, &nodes)) < 0)
|
2009-08-19 19:50:10 +00:00
|
|
|
goto cleanup;
|
2016-05-25 10:51:30 +00:00
|
|
|
|
|
|
|
if (n > 0) {
|
2018-03-06 13:57:17 +00:00
|
|
|
if (VIR_ALLOC_N(encdef->secrets, n) < 0)
|
2009-08-19 19:50:10 +00:00
|
|
|
goto cleanup;
|
2018-03-06 13:57:17 +00:00
|
|
|
encdef->nsecrets = n;
|
2016-05-25 10:51:30 +00:00
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2018-03-06 13:57:17 +00:00
|
|
|
if (!(encdef->secrets[i] =
|
2016-05-25 10:51:30 +00:00
|
|
|
virStorageEncryptionSecretParse(ctxt, nodes[i])))
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2009-08-19 19:50:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 13:57:17 +00:00
|
|
|
if (encdef->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS) {
|
2016-06-01 23:21:26 +00:00
|
|
|
xmlNodePtr tmpnode;
|
|
|
|
|
|
|
|
if ((tmpnode = virXPathNode("./cipher[1]", ctxt))) {
|
2018-03-06 13:57:17 +00:00
|
|
|
if (virStorageEncryptionInfoParseCipher(tmpnode, &encdef->encinfo) < 0)
|
2016-06-01 23:21:26 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tmpnode = virXPathNode("./ivgen[1]", ctxt))) {
|
|
|
|
/* If no cipher node, then fail */
|
2018-03-06 13:57:17 +00:00
|
|
|
if (!encdef->encinfo.cipher_name) {
|
2016-06-01 23:21:26 +00:00
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
2018-09-19 08:38:14 +00:00
|
|
|
_("ivgen element found, but cipher is missing"));
|
2016-06-01 23:21:26 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:57:17 +00:00
|
|
|
if (virStorageEncryptionInfoParseIvgen(tmpnode, &encdef->encinfo) < 0)
|
2016-06-01 23:21:26 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
ret = g_steal_pointer(&encdef);
|
2009-08-19 19:50:10 +00:00
|
|
|
|
2014-03-25 06:48:31 +00:00
|
|
|
cleanup:
|
2016-05-25 10:51:30 +00:00
|
|
|
VIR_FREE(format_str);
|
2009-08-19 19:50:10 +00:00
|
|
|
VIR_FREE(nodes);
|
2018-03-06 13:57:17 +00:00
|
|
|
virStorageEncryptionFree(encdef);
|
2009-08-19 19:50:10 +00:00
|
|
|
|
2018-03-06 13:17:59 +00:00
|
|
|
return ret;
|
2009-08-19 19:50:10 +00:00
|
|
|
}
|
2009-09-01 20:37:42 +00:00
|
|
|
|
2009-08-19 19:50:10 +00:00
|
|
|
|
|
|
|
static int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageEncryptionSecretFormat(virBufferPtr buf,
|
2011-09-22 18:16:26 +00:00
|
|
|
virStorageEncryptionSecretPtr secret)
|
2009-08-19 19:50:10 +00:00
|
|
|
{
|
|
|
|
const char *type;
|
|
|
|
|
2016-05-25 10:51:30 +00:00
|
|
|
if (!(type = virStorageEncryptionSecretTypeToString(secret->type))) {
|
2012-07-18 10:50:44 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("unexpected volume encryption secret type"));
|
2009-08-19 19:50:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-30 11:47:46 +00:00
|
|
|
virSecretLookupFormatSecret(buf, type, &secret->seclookupdef);
|
|
|
|
|
2009-08-19 19:50:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:21:26 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
virStorageEncryptionInfoDefFormat(virBufferPtr buf,
|
|
|
|
const virStorageEncryptionInfoDef *enc)
|
|
|
|
{
|
|
|
|
virBufferEscapeString(buf, "<cipher name='%s'", enc->cipher_name);
|
|
|
|
virBufferAsprintf(buf, " size='%u'", enc->cipher_size);
|
|
|
|
if (enc->cipher_mode)
|
|
|
|
virBufferEscapeString(buf, " mode='%s'", enc->cipher_mode);
|
|
|
|
if (enc->cipher_hash)
|
|
|
|
virBufferEscapeString(buf, " hash='%s'", enc->cipher_hash);
|
|
|
|
virBufferAddLit(buf, "/>\n");
|
|
|
|
|
|
|
|
if (enc->ivgen_name) {
|
|
|
|
virBufferEscapeString(buf, "<ivgen name='%s'", enc->ivgen_name);
|
|
|
|
if (enc->ivgen_hash)
|
|
|
|
virBufferEscapeString(buf, " hash='%s'", enc->ivgen_hash);
|
|
|
|
virBufferAddLit(buf, "/>\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 19:50:10 +00:00
|
|
|
int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageEncryptionFormat(virBufferPtr buf,
|
2011-09-22 18:16:26 +00:00
|
|
|
virStorageEncryptionPtr enc)
|
2009-08-19 19:50:10 +00:00
|
|
|
{
|
|
|
|
const char *format;
|
|
|
|
size_t i;
|
|
|
|
|
2016-05-25 10:51:30 +00:00
|
|
|
if (!(format = virStorageEncryptionFormatTypeToString(enc->format))) {
|
2012-07-18 10:50:44 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("unexpected encryption format"));
|
2009-08-19 19:50:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-09-22 18:16:26 +00:00
|
|
|
virBufferAsprintf(buf, "<encryption format='%s'>\n", format);
|
2014-03-06 14:57:47 +00:00
|
|
|
virBufferAdjustIndent(buf, 2);
|
2009-08-19 19:50:10 +00:00
|
|
|
|
|
|
|
for (i = 0; i < enc->nsecrets; i++) {
|
2011-09-22 18:16:26 +00:00
|
|
|
if (virStorageEncryptionSecretFormat(buf, enc->secrets[i]) < 0)
|
2009-08-19 19:50:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:21:26 +00:00
|
|
|
if (enc->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS &&
|
|
|
|
enc->encinfo.cipher_name)
|
|
|
|
virStorageEncryptionInfoDefFormat(buf, &enc->encinfo);
|
|
|
|
|
2014-03-06 14:57:47 +00:00
|
|
|
virBufferAdjustIndent(buf, -2);
|
2011-09-22 18:16:26 +00:00
|
|
|
virBufferAddLit(buf, "</encryption>\n");
|
2009-08-19 19:50:10 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-14 18:06:59 +00:00
|
|
|
|
|
|
|
int
|
2010-02-10 11:42:56 +00:00
|
|
|
virStorageGenerateQcowPassphrase(unsigned char *dest)
|
2009-08-14 18:06:59 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* A qcow passphrase is up to 16 bytes, with any data following a NUL
|
|
|
|
ignored. Prohibit control and non-ASCII characters to avoid possible
|
|
|
|
unpleasant surprises with the qemu monitor input mechanism. */
|
|
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2012-07-18 10:50:44 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot open /dev/urandom"));
|
2009-08-14 18:06:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while (i < VIR_STORAGE_QCOW_PASSPHRASE_SIZE) {
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
while ((r = read(fd, dest + i, 1)) == -1 && errno == EINTR)
|
|
|
|
;
|
|
|
|
if (r <= 0) {
|
2012-07-18 10:50:44 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot read from /dev/urandom"));
|
2010-10-19 14:23:51 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2009-08-14 18:06:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (dest[i] >= 0x20 && dest[i] <= 0x7E)
|
|
|
|
i++; /* Got an acceptable character */
|
|
|
|
}
|
2010-10-19 14:23:51 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2009-08-14 18:06:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|