secret_util.c 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * secret_util.c: secret related utility functions
 *
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "secret_util.h"
#include "viralloc.h"
#include "virerror.h"
#include "virlog.h"
#include "virobject.h"
#include "viruuid.h"
#include "datatypes.h"

#define VIR_FROM_THIS VIR_FROM_SECRET

VIR_LOG_INIT("secret.secret_util");


/* virSecretGetSecretString:
 * @conn: Pointer to the connection driver to make secret driver call
 * @authdef: Pointer to the disk storage authentication
 * @secretUsageType: Type of secret usage for authdef lookup
41 42
 * @secret: returned secret as a sized stream of unsigned chars
 * @secret_size: Return size of the secret - either raw text or base64
43
 *
44 45
 * Lookup the secret for the authdef usage type and return it as raw text.
 * It is up to the caller to encode the secret further.
46
 *
47 48
 * Returns 0 on success, -1 on failure.  On success the memory in secret
 * needs to be cleared and free'd after usage.
49
 */
50
int
51 52
virSecretGetSecretString(virConnectPtr conn,
                         virStorageAuthDefPtr authdef,
53 54 55
                         virSecretUsageType secretUsageType,
                         uint8_t **secret,
                         size_t *secret_size)
56 57
{
    virSecretPtr sec = NULL;
58
    int ret = -1;
59 60 61

    switch (authdef->secretType) {
    case VIR_STORAGE_SECRET_TYPE_UUID:
62
        sec = conn->secretDriver->secretLookupByUUID(conn, authdef->secret.uuid);
63
        break;
64

65
    case VIR_STORAGE_SECRET_TYPE_USAGE:
66 67
        sec = conn->secretDriver->secretLookupByUsage(conn, secretUsageType,
                                                      authdef->secret.usage);
68 69 70
        break;
    }

71
    if (!sec)
72 73
        goto cleanup;

74 75
    *secret = conn->secretDriver->secretGetValue(sec, secret_size, 0,
                                                 VIR_SECRET_GET_VALUE_INTERNAL_CALL);
76

77
    if (!*secret)
78 79
        goto cleanup;

80
    ret = 0;
81 82 83

 cleanup:
    virObjectUnref(sec);
84
    return ret;
85
}