virstorageencryption.c 11.8 KB
Newer Older
1
/*
2
 * virstorageencryption.c: volume encryption information
3
 *
4
 * Copyright (C) 2009-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24
 *
 * Red Hat Author: Miloslav Trmač <mitr@redhat.com>
 */

#include <config.h>

25 26 27
#include <fcntl.h>
#include <unistd.h>

28 29
#include "internal.h"

30
#include "virbuffer.h"
31
#include "viralloc.h"
32
#include "virstorageencryption.h"
33
#include "virxml.h"
34
#include "virerror.h"
35
#include "viruuid.h"
E
Eric Blake 已提交
36
#include "virfile.h"
J
John Ferlan 已提交
37
#include "virsecret.h"
38
#include "virstring.h"
39 40 41

#define VIR_FROM_THIS VIR_FROM_STORAGE

42
VIR_ENUM_IMPL(virStorageEncryptionSecret,
43 44 45 46
              VIR_STORAGE_ENCRYPTION_SECRET_TYPE_LAST, "passphrase")

VIR_ENUM_IMPL(virStorageEncryptionFormat,
              VIR_STORAGE_ENCRYPTION_FORMAT_LAST,
47
              "default", "qcow", "luks")
48

49 50 51 52 53 54 55 56 57 58 59
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);
}


60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static void
virStorageEncryptionSecretFree(virStorageEncryptionSecretPtr secret)
{
    if (!secret)
        return;
    VIR_FREE(secret);
}

void
virStorageEncryptionFree(virStorageEncryptionPtr enc)
{
    size_t i;

    if (!enc)
        return;

    for (i = 0; i < enc->nsecrets; i++)
        virStorageEncryptionSecretFree(enc->secrets[i]);
78
    virStorageEncryptionInfoDefFree(&enc->encinfo);
79 80 81 82
    VIR_FREE(enc->secrets);
    VIR_FREE(enc);
}

83 84 85 86 87 88 89 90 91 92 93 94 95
static virStorageEncryptionSecretPtr
virStorageEncryptionSecretCopy(const virStorageEncryptionSecret *src)
{
    virStorageEncryptionSecretPtr ret;

    if (VIR_ALLOC(ret) < 0)
        return NULL;

    memcpy(ret, src, sizeof(*src));

    return ret;
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

static int
virStorageEncryptionInfoDefCopy(const virStorageEncryptionInfoDef *src,
                                virStorageEncryptionInfoDefPtr dst)
{
    dst->cipher_size = src->cipher_size;
    if (VIR_STRDUP(dst->cipher_name, src->cipher_name) < 0 ||
        VIR_STRDUP(dst->cipher_mode, src->cipher_mode) < 0 ||
        VIR_STRDUP(dst->cipher_hash, src->cipher_hash) < 0 ||
        VIR_STRDUP(dst->ivgen_name, src->ivgen_name) < 0 ||
        VIR_STRDUP(dst->ivgen_hash, src->ivgen_hash) < 0)
        return -1;

    return 0;
}


113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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;
    }

133 134 135
    if (virStorageEncryptionInfoDefCopy(&src->encinfo, &ret->encinfo) < 0)
        goto error;

136 137 138 139 140 141 142
    return ret;

 error:
    virStorageEncryptionFree(ret);
    return NULL;
}

143
static virStorageEncryptionSecretPtr
144
virStorageEncryptionSecretParse(xmlXPathContextPtr ctxt,
145 146 147 148
                                xmlNodePtr node)
{
    xmlNodePtr old_node;
    virStorageEncryptionSecretPtr ret;
149
    char *type_str = NULL;
150
    char *uuidstr = NULL;
J
John Ferlan 已提交
151
    char *usagestr = NULL;
152

153
    if (VIR_ALLOC(ret) < 0)
154 155 156 157 158
        return NULL;

    old_node = ctxt->node;
    ctxt->node = node;

159
    if (!(type_str = virXPathString("string(./@type)", ctxt))) {
160 161
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("unknown volume encryption secret type"));
162 163
        goto cleanup;
    }
164 165

    if ((ret->type = virStorageEncryptionSecretTypeFromString(type_str)) < 0) {
166
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
167 168
                       _("unknown volume encryption secret type %s"),
                       type_str);
169 170 171
        goto cleanup;
    }

J
John Ferlan 已提交
172
    if (virSecretLookupParseSecret(node, &ret->seclookupdef) < 0)
173
        goto cleanup;
J
John Ferlan 已提交
174 175 176

    VIR_FREE(type_str);

177 178 179
    ctxt->node = old_node;
    return ret;

180
 cleanup:
181
    VIR_FREE(type_str);
182
    virStorageEncryptionSecretFree(ret);
183
    VIR_FREE(uuidstr);
J
John Ferlan 已提交
184
    VIR_FREE(usagestr);
185 186 187 188
    ctxt->node = old_node;
    return NULL;
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

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;
}


244
static virStorageEncryptionPtr
245
virStorageEncryptionParseXML(xmlXPathContextPtr ctxt)
246 247 248
{
    xmlNodePtr *nodes = NULL;
    virStorageEncryptionPtr ret;
249 250
    char *format_str = NULL;
    int n;
251
    size_t i;
252

253
    if (VIR_ALLOC(ret) < 0)
254 255
        return NULL;

256
    if (!(format_str = virXPathString("string(./@format)", ctxt))) {
257 258
        virReportError(VIR_ERR_XML_ERROR, "%s",
                       _("unknown volume encryption format"));
259 260
        goto cleanup;
    }
261 262 263

    if ((ret->format =
         virStorageEncryptionFormatTypeFromString(format_str)) < 0) {
264
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
265 266
                       _("unknown volume encryption format type %s"),
                       format_str);
267 268 269 270
        goto cleanup;
    }
    VIR_FREE(format_str);

271
    if ((n = virXPathNodeSet("./secret", ctxt, &nodes)) < 0)
272
        goto cleanup;
273 274 275

    if (n > 0) {
        if (VIR_ALLOC_N(ret->secrets, n) < 0)
276
            goto cleanup;
277 278 279 280 281 282 283 284
        ret->nsecrets = n;

        for (i = 0; i < n; i++) {
            if (!(ret->secrets[i] =
                  virStorageEncryptionSecretParse(ctxt, nodes[i])))
                goto cleanup;
        }
        VIR_FREE(nodes);
285 286
    }

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    if (ret->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS) {
        xmlNodePtr tmpnode;

        if ((tmpnode = virXPathNode("./cipher[1]", ctxt))) {
            if (virStorageEncryptionInfoParseCipher(tmpnode, &ret->encinfo) < 0)
                goto cleanup;
        }

        if ((tmpnode = virXPathNode("./ivgen[1]", ctxt))) {
            /* If no cipher node, then fail */
            if (!ret->encinfo.cipher_name) {
                virReportError(VIR_ERR_XML_ERROR, "%s",
                                _("ivgen element found, but cipher is missing"));
                goto cleanup;
            }

            if (virStorageEncryptionInfoParseIvgen(tmpnode, &ret->encinfo) < 0)
                goto cleanup;
        }
    }


309 310
    return ret;

311
 cleanup:
312
    VIR_FREE(format_str);
313 314 315 316 317 318
    VIR_FREE(nodes);
    virStorageEncryptionFree(ret);
    return NULL;
}

virStorageEncryptionPtr
319
virStorageEncryptionParseNode(xmlDocPtr xml, xmlNodePtr root)
320 321 322 323 324
{
    xmlXPathContextPtr ctxt = NULL;
    virStorageEncryptionPtr enc = NULL;

    if (STRNEQ((const char *) root->name, "encryption")) {
325 326 327
        virReportError(VIR_ERR_XML_ERROR,
                       "%s", _("unknown root element for volume "
                               "encryption information"));
328 329 330 331 332
        goto cleanup;
    }

    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
333
        virReportOOMError();
334 335 336 337
        goto cleanup;
    }

    ctxt->node = root;
338
    enc = virStorageEncryptionParseXML(ctxt);
339

340
 cleanup:
341 342 343
    xmlXPathFreeContext(ctxt);
    return enc;
}
344

345 346

static int
347
virStorageEncryptionSecretFormat(virBufferPtr buf,
348
                                 virStorageEncryptionSecretPtr secret)
349 350 351
{
    const char *type;

352
    if (!(type = virStorageEncryptionSecretTypeToString(secret->type))) {
353 354
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("unexpected volume encryption secret type"));
355 356 357
        return -1;
    }

J
John Ferlan 已提交
358 359
    virSecretLookupFormatSecret(buf, type, &secret->seclookupdef);

360 361 362
    return 0;
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

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");
    }
}


385
int
386
virStorageEncryptionFormat(virBufferPtr buf,
387
                           virStorageEncryptionPtr enc)
388 389 390 391
{
    const char *format;
    size_t i;

392
    if (!(format = virStorageEncryptionFormatTypeToString(enc->format))) {
393 394
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unexpected encryption format"));
395 396
        return -1;
    }
397
    virBufferAsprintf(buf, "<encryption format='%s'>\n", format);
398
    virBufferAdjustIndent(buf, 2);
399 400

    for (i = 0; i < enc->nsecrets; i++) {
401
        if (virStorageEncryptionSecretFormat(buf, enc->secrets[i]) < 0)
402 403 404
            return -1;
    }

405 406 407 408
    if (enc->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS &&
        enc->encinfo.cipher_name)
        virStorageEncryptionInfoDefFormat(buf, &enc->encinfo);

409
    virBufferAdjustIndent(buf, -2);
410
    virBufferAddLit(buf, "</encryption>\n");
411 412 413

    return 0;
}
414 415

int
416
virStorageGenerateQcowPassphrase(unsigned char *dest)
417 418 419 420 421 422 423 424 425
{
    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) {
426 427
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot open /dev/urandom"));
428 429 430 431 432 433 434 435 436
        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) {
437 438
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("Cannot read from /dev/urandom"));
439
            VIR_FORCE_CLOSE(fd);
440 441 442 443 444
            return -1;
        }
        if (dest[i] >= 0x20 && dest[i] <= 0x7E)
            i++; /* Got an acceptable character */
    }
445
    VIR_FORCE_CLOSE(fd);
446 447
    return 0;
}