MAINTAINERS: Cover docs/igd-assign.txt in VFIO section
[qemu/ar7.git] / crypto / secret_keyring.c
blob1b7edec84a0a1d79ec8f0eaac2ccd0e9e998a8e0
1 /*
2 * QEMU crypto secret support
4 * Copyright 2020 Yandex N.V.
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 <asm/unistd.h>
23 #include <linux/keyctl.h>
24 #include "qapi/error.h"
25 #include "qom/object_interfaces.h"
26 #include "trace.h"
27 #include "crypto/secret_keyring.h"
30 static inline
31 long keyctl_read(int32_t key, uint8_t *buffer, size_t buflen)
33 return syscall(__NR_keyctl, KEYCTL_READ, key, buffer, buflen, 0);
37 static void
38 qcrypto_secret_keyring_load_data(QCryptoSecretCommon *sec_common,
39 uint8_t **output,
40 size_t *outputlen,
41 Error **errp)
43 QCryptoSecretKeyring *secret = QCRYPTO_SECRET_KEYRING(sec_common);
44 uint8_t *buffer = NULL;
45 long retcode;
47 *output = NULL;
48 *outputlen = 0;
50 if (!secret->serial) {
51 error_setg(errp, "'serial' parameter must be provided");
52 return;
55 retcode = keyctl_read(secret->serial, NULL, 0);
56 if (retcode <= 0) {
57 goto keyctl_error;
60 buffer = g_new0(uint8_t, retcode);
62 retcode = keyctl_read(secret->serial, buffer, retcode);
63 if (retcode < 0) {
64 g_free(buffer);
65 goto keyctl_error;
68 *outputlen = retcode;
69 *output = buffer;
70 return;
72 keyctl_error:
73 error_setg_errno(errp, errno,
74 "Unable to read serial key %08x",
75 secret->serial);
79 static void
80 qcrypto_secret_prop_set_key(Object *obj, Visitor *v,
81 const char *name, void *opaque,
82 Error **errp)
84 QCryptoSecretKeyring *secret = QCRYPTO_SECRET_KEYRING(obj);
85 int32_t value;
86 visit_type_int32(v, name, &value, errp);
87 if (!value) {
88 error_setg(errp, "'serial' should not be equal to 0");
90 secret->serial = value;
94 static void
95 qcrypto_secret_prop_get_key(Object *obj, Visitor *v,
96 const char *name, void *opaque,
97 Error **errp)
99 QCryptoSecretKeyring *secret = QCRYPTO_SECRET_KEYRING(obj);
100 int32_t value = secret->serial;
101 visit_type_int32(v, name, &value, errp);
105 static void
106 qcrypto_secret_keyring_class_init(ObjectClass *oc, void *data)
108 QCryptoSecretCommonClass *sic = QCRYPTO_SECRET_COMMON_CLASS(oc);
109 sic->load_data = qcrypto_secret_keyring_load_data;
111 object_class_property_add(oc, "serial", "int32_t",
112 qcrypto_secret_prop_get_key,
113 qcrypto_secret_prop_set_key,
114 NULL, NULL);
118 static const TypeInfo qcrypto_secret_info = {
119 .parent = TYPE_QCRYPTO_SECRET_COMMON,
120 .name = TYPE_QCRYPTO_SECRET_KEYRING,
121 .instance_size = sizeof(QCryptoSecretKeyring),
122 .class_init = qcrypto_secret_keyring_class_init,
126 static void
127 qcrypto_secret_register_types(void)
129 type_register_static(&qcrypto_secret_info);
133 type_init(qcrypto_secret_register_types);