Skip to content

Commit aecdfcc

Browse files
committed
Merge remote-tracking branch 'remotes/philmd-gitlab/tags/fw_cfg-20200704' into staging
firmware (and crypto) patches - add the tls-cipher-suites object, - add the ability to QOM objects to produce data consumable by the fw_cfg device, - let the tls-cipher-suites object implement the FW_CFG_DATA_GENERATOR interface. This is required by EDK2 'HTTPS Boot' feature of OVMF to tell the guest which TLS ciphers it can use. CI jobs results: https://travis-ci.org/github/philmd/qemu/builds/704724619 https://gitlab.com/philmd/qemu/-/pipelines/162938106 https://cirrus-ci.com/build/4682977303068672 # gpg: Signature made Sat 04 Jul 2020 17:37:08 BST # gpg: using RSA key FAABE75E12917221DCFD6BB2E3E32C2CDEADC0DE # gpg: Good signature from "Philippe Mathieu-Daudé (F4BUG) <[email protected]>" [full] # Primary key fingerprint: FAAB E75E 1291 7221 DCFD 6BB2 E3E3 2C2C DEAD C0DE * remotes/philmd-gitlab/tags/fw_cfg-20200704: crypto/tls-cipher-suites: Produce fw_cfg consumable blob softmmu/vl: Allow -fw_cfg 'gen_id' option to use the 'etc/' namespace softmmu/vl: Let -fw_cfg option take a 'gen_id' argument hw/nvram/fw_cfg: Add the FW_CFG_DATA_GENERATOR interface crypto: Add tls-cipher-suites object Signed-off-by: Peter Maydell <[email protected]>
2 parents aff2caf + 69699f3 commit aecdfcc

File tree

9 files changed

+326
-10
lines changed

9 files changed

+326
-10
lines changed

crypto/Makefile.objs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ crypto-obj-y += cipher.o
1313
crypto-obj-$(CONFIG_AF_ALG) += afalg.o
1414
crypto-obj-$(CONFIG_AF_ALG) += cipher-afalg.o
1515
crypto-obj-$(CONFIG_AF_ALG) += hash-afalg.o
16+
crypto-obj-$(CONFIG_GNUTLS) += tls-cipher-suites.o
1617
crypto-obj-y += tlscreds.o
1718
crypto-obj-y += tlscredsanon.o
1819
crypto-obj-y += tlscredspsk.o

crypto/tls-cipher-suites.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* QEMU TLS Cipher Suites
3+
*
4+
* Copyright (c) 2018-2020 Red Hat, Inc.
5+
*
6+
* Author: Philippe Mathieu-Daudé <[email protected]>
7+
*
8+
* SPDX-License-Identifier: GPL-2.0-or-later
9+
*/
10+
11+
#include "qemu/osdep.h"
12+
#include "qapi/error.h"
13+
#include "qom/object_interfaces.h"
14+
#include "crypto/tlscreds.h"
15+
#include "crypto/tls-cipher-suites.h"
16+
#include "hw/nvram/fw_cfg.h"
17+
#include "trace.h"
18+
19+
/*
20+
* IANA registered TLS ciphers:
21+
* https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4
22+
*/
23+
typedef struct {
24+
uint8_t data[2];
25+
} QEMU_PACKED IANA_TLS_CIPHER;
26+
27+
GByteArray *qcrypto_tls_cipher_suites_get_data(QCryptoTLSCipherSuites *obj,
28+
Error **errp)
29+
{
30+
QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
31+
gnutls_priority_t pcache;
32+
GByteArray *byte_array;
33+
const char *err;
34+
size_t i;
35+
int ret;
36+
37+
trace_qcrypto_tls_cipher_suite_priority(creds->priority);
38+
ret = gnutls_priority_init(&pcache, creds->priority, &err);
39+
if (ret < 0) {
40+
error_setg(errp, "Syntax error using priority '%s': %s",
41+
creds->priority, gnutls_strerror(ret));
42+
return NULL;
43+
}
44+
45+
byte_array = g_byte_array_new();
46+
47+
for (i = 0;; i++) {
48+
int ret;
49+
unsigned idx;
50+
const char *name;
51+
IANA_TLS_CIPHER cipher;
52+
gnutls_protocol_t protocol;
53+
const char *version;
54+
55+
ret = gnutls_priority_get_cipher_suite_index(pcache, i, &idx);
56+
if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
57+
break;
58+
}
59+
if (ret == GNUTLS_E_UNKNOWN_CIPHER_SUITE) {
60+
continue;
61+
}
62+
63+
name = gnutls_cipher_suite_info(idx, (unsigned char *)&cipher,
64+
NULL, NULL, NULL, &protocol);
65+
if (name == NULL) {
66+
continue;
67+
}
68+
69+
version = gnutls_protocol_get_name(protocol);
70+
g_byte_array_append(byte_array, cipher.data, 2);
71+
trace_qcrypto_tls_cipher_suite_info(cipher.data[0],
72+
cipher.data[1],
73+
version, name);
74+
}
75+
trace_qcrypto_tls_cipher_suite_count(byte_array->len);
76+
gnutls_priority_deinit(pcache);
77+
78+
return byte_array;
79+
}
80+
81+
static void qcrypto_tls_cipher_suites_complete(UserCreatable *uc,
82+
Error **errp)
83+
{
84+
QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(uc);
85+
86+
if (!creds->priority) {
87+
error_setg(errp, "'priority' property is not set");
88+
return;
89+
}
90+
}
91+
92+
static GByteArray *qcrypto_tls_cipher_suites_fw_cfg_gen_data(Object *obj,
93+
Error **errp)
94+
{
95+
return qcrypto_tls_cipher_suites_get_data(QCRYPTO_TLS_CIPHER_SUITES(obj),
96+
errp);
97+
}
98+
99+
static void qcrypto_tls_cipher_suites_class_init(ObjectClass *oc, void *data)
100+
{
101+
UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
102+
FWCfgDataGeneratorClass *fwgc = FW_CFG_DATA_GENERATOR_CLASS(oc);
103+
104+
ucc->complete = qcrypto_tls_cipher_suites_complete;
105+
fwgc->get_data = qcrypto_tls_cipher_suites_fw_cfg_gen_data;
106+
}
107+
108+
static const TypeInfo qcrypto_tls_cipher_suites_info = {
109+
.parent = TYPE_QCRYPTO_TLS_CREDS,
110+
.name = TYPE_QCRYPTO_TLS_CIPHER_SUITES,
111+
.instance_size = sizeof(QCryptoTLSCreds),
112+
.class_size = sizeof(QCryptoTLSCredsClass),
113+
.class_init = qcrypto_tls_cipher_suites_class_init,
114+
.interfaces = (InterfaceInfo[]) {
115+
{ TYPE_USER_CREATABLE },
116+
{ TYPE_FW_CFG_DATA_GENERATOR_INTERFACE },
117+
{ }
118+
}
119+
};
120+
121+
static void qcrypto_tls_cipher_suites_register_types(void)
122+
{
123+
type_register_static(&qcrypto_tls_cipher_suites_info);
124+
}
125+
126+
type_init(qcrypto_tls_cipher_suites_register_types);

crypto/trace-events

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ qcrypto_tls_creds_x509_load_cert_list(void *creds, const char *file) "TLS creds
2121
# tlssession.c
2222
qcrypto_tls_session_new(void *session, void *creds, const char *hostname, const char *authzid, int endpoint) "TLS session new session=%p creds=%p hostname=%s authzid=%s endpoint=%d"
2323
qcrypto_tls_session_check_creds(void *session, const char *status) "TLS session check creds session=%p status=%s"
24+
25+
# tls-cipher-suites.c
26+
qcrypto_tls_cipher_suite_priority(const char *name) "priority: %s"
27+
qcrypto_tls_cipher_suite_info(uint8_t data0, uint8_t data1, const char *version, const char *name) "data=[0x%02x,0x%02x] version=%s name=%s"
28+
qcrypto_tls_cipher_suite_count(unsigned count) "count: %u"

docs/specs/fw_cfg.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ To check the result, read the "control" field:
219219

220220
= Externally Provided Items =
221221

222-
As of v2.4, "file" fw_cfg items (i.e., items with selector keys above
222+
Since v2.4, "file" fw_cfg items (i.e., items with selector keys above
223223
FW_CFG_FILE_FIRST, and with a corresponding entry in the fw_cfg file
224224
directory structure) may be inserted via the QEMU command line, using
225225
the following syntax:
@@ -230,6 +230,13 @@ Or
230230

231231
-fw_cfg [name=]<item_name>,string=<string>
232232

233+
Since v5.1, QEMU allows some objects to generate fw_cfg-specific content,
234+
the content is then associated with a "file" item using the 'gen_id' option
235+
in the command line, using the following syntax:
236+
237+
-object <generator-type>,id=<generated_id>,[generator-specific-options] \
238+
-fw_cfg [name=]<item_name>,gen_id=<generated_id>
239+
233240
See QEMU man page for more documentation.
234241

235242
Using item_name with plain ASCII characters only is recommended.
@@ -251,4 +258,8 @@ Prefix "opt/org.qemu/" is reserved for QEMU itself.
251258
Use of names not beginning with "opt/" is potentially dangerous and
252259
entirely unsupported. QEMU will warn if you try.
253260

261+
Use of names not beginning with "opt/" is tolerated with 'gen_id' (that
262+
is, the warning is suppressed), but you must know exactly what you're
263+
doing.
264+
254265
All externally provided fw_cfg items are read-only to the guest.

hw/nvram/fw_cfg.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,35 @@ void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
10321032
return NULL;
10331033
}
10341034

1035+
void fw_cfg_add_from_generator(FWCfgState *s, const char *filename,
1036+
const char *gen_id, Error **errp)
1037+
{
1038+
FWCfgDataGeneratorClass *klass;
1039+
Error *local_err = NULL;
1040+
GByteArray *array;
1041+
Object *obj;
1042+
gsize size;
1043+
1044+
obj = object_resolve_path_component(object_get_objects_root(), gen_id);
1045+
if (!obj) {
1046+
error_setg(errp, "Cannot find object ID '%s'", gen_id);
1047+
return;
1048+
}
1049+
if (!object_dynamic_cast(obj, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE)) {
1050+
error_setg(errp, "Object ID '%s' is not a '%s' subclass",
1051+
gen_id, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE);
1052+
return;
1053+
}
1054+
klass = FW_CFG_DATA_GENERATOR_GET_CLASS(obj);
1055+
array = klass->get_data(obj, &local_err);
1056+
if (local_err) {
1057+
error_propagate(errp, local_err);
1058+
return;
1059+
}
1060+
size = array->len;
1061+
fw_cfg_add_file(s, filename, g_byte_array_free(array, TRUE), size);
1062+
}
1063+
10351064
static void fw_cfg_machine_reset(void *opaque)
10361065
{
10371066
MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
@@ -1333,12 +1362,18 @@ static const TypeInfo fw_cfg_mem_info = {
13331362
.class_init = fw_cfg_mem_class_init,
13341363
};
13351364

1365+
static const TypeInfo fw_cfg_data_generator_interface_info = {
1366+
.parent = TYPE_INTERFACE,
1367+
.name = TYPE_FW_CFG_DATA_GENERATOR_INTERFACE,
1368+
.class_size = sizeof(FWCfgDataGeneratorClass),
1369+
};
13361370

13371371
static void fw_cfg_register_types(void)
13381372
{
13391373
type_register_static(&fw_cfg_info);
13401374
type_register_static(&fw_cfg_io_info);
13411375
type_register_static(&fw_cfg_mem_info);
1376+
type_register_static(&fw_cfg_data_generator_interface_info);
13421377
}
13431378

13441379
type_init(fw_cfg_register_types)

include/crypto/tls-cipher-suites.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* QEMU TLS Cipher Suites Registry (RFC8447)
3+
*
4+
* Copyright (c) 2018-2020 Red Hat, Inc.
5+
*
6+
* Author: Philippe Mathieu-Daudé <[email protected]>
7+
*
8+
* SPDX-License-Identifier: GPL-2.0-or-later
9+
*/
10+
11+
#ifndef QCRYPTO_TLSCIPHERSUITES_H
12+
#define QCRYPTO_TLSCIPHERSUITES_H
13+
14+
#include "qom/object.h"
15+
#include "crypto/tlscreds.h"
16+
17+
#define TYPE_QCRYPTO_TLS_CIPHER_SUITES "tls-cipher-suites"
18+
#define QCRYPTO_TLS_CIPHER_SUITES(obj) \
19+
OBJECT_CHECK(QCryptoTLSCipherSuites, (obj), TYPE_QCRYPTO_TLS_CIPHER_SUITES)
20+
21+
typedef struct QCryptoTLSCipherSuites {
22+
/* <private> */
23+
QCryptoTLSCreds parent_obj;
24+
/* <public> */
25+
} QCryptoTLSCipherSuites;
26+
27+
/**
28+
* qcrypto_tls_cipher_suites_get_data:
29+
* @obj: pointer to a TLS cipher suites object
30+
* @errp: pointer to a NULL-initialized error object
31+
*
32+
* Returns: reference to a byte array containing the data.
33+
* The caller should release the reference when no longer
34+
* required.
35+
*/
36+
GByteArray *qcrypto_tls_cipher_suites_get_data(QCryptoTLSCipherSuites *obj,
37+
Error **errp);
38+
39+
#endif /* QCRYPTO_TLSCIPHERSUITES_H */

include/hw/nvram/fw_cfg.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,36 @@
99
#define TYPE_FW_CFG "fw_cfg"
1010
#define TYPE_FW_CFG_IO "fw_cfg_io"
1111
#define TYPE_FW_CFG_MEM "fw_cfg_mem"
12+
#define TYPE_FW_CFG_DATA_GENERATOR_INTERFACE "fw_cfg-data-generator"
1213

1314
#define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
1415
#define FW_CFG_IO(obj) OBJECT_CHECK(FWCfgIoState, (obj), TYPE_FW_CFG_IO)
1516
#define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
1617

18+
#define FW_CFG_DATA_GENERATOR_CLASS(class) \
19+
OBJECT_CLASS_CHECK(FWCfgDataGeneratorClass, (class), \
20+
TYPE_FW_CFG_DATA_GENERATOR_INTERFACE)
21+
#define FW_CFG_DATA_GENERATOR_GET_CLASS(obj) \
22+
OBJECT_GET_CLASS(FWCfgDataGeneratorClass, (obj), \
23+
TYPE_FW_CFG_DATA_GENERATOR_INTERFACE)
24+
25+
typedef struct FWCfgDataGeneratorClass {
26+
/*< private >*/
27+
InterfaceClass parent_class;
28+
/*< public >*/
29+
30+
/**
31+
* get_data:
32+
* @obj: the object implementing this interface
33+
* @errp: pointer to a NULL-initialized error object
34+
*
35+
* Returns: reference to a byte array containing the data.
36+
* The caller should release the reference when no longer
37+
* required.
38+
*/
39+
GByteArray *(*get_data)(Object *obj, Error **errp);
40+
} FWCfgDataGeneratorClass;
41+
1742
typedef struct fw_cfg_file FWCfgFile;
1843

1944
#define FW_CFG_ORDER_OVERRIDE_VGA 70
@@ -263,6 +288,24 @@ void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
263288
void *fw_cfg_modify_file(FWCfgState *s, const char *filename, void *data,
264289
size_t len);
265290

291+
/**
292+
* fw_cfg_add_from_generator:
293+
* @s: fw_cfg device being modified
294+
* @filename: name of new fw_cfg file item
295+
* @gen_id: name of object implementing FW_CFG_DATA_GENERATOR interface
296+
* @errp: pointer to a NULL initialized error object
297+
*
298+
* Add a new NAMED fw_cfg item with the content generated from the
299+
* @gen_id object. The data generated by the @gen_id object is copied
300+
* into the data structure of the fw_cfg device.
301+
* The next available (unused) selector key starting at FW_CFG_FILE_FIRST
302+
* will be used; also, a new entry will be added to the file directory
303+
* structure residing at key value FW_CFG_FILE_DIR, containing the item name,
304+
* data size, and assigned selector key value.
305+
*/
306+
void fw_cfg_add_from_generator(FWCfgState *s, const char *filename,
307+
const char *gen_id, Error **errp);
308+
266309
FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
267310
AddressSpace *dma_as);
268311
FWCfgState *fw_cfg_init_io(uint32_t iobase);

qemu-options.hx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4579,6 +4579,43 @@ SRST
45794579
string as described at
45804580
https://gnutls.org/manual/html_node/Priority-Strings.html.
45814581

4582+
``-object tls-cipher-suites,id=id,priority=priority``
4583+
Creates a TLS cipher suites object, which can be used to control
4584+
the TLS cipher/protocol algorithms that applications are permitted
4585+
to use.
4586+
4587+
The ``id`` parameter is a unique ID which frontends will use to
4588+
access the ordered list of permitted TLS cipher suites from the
4589+
host.
4590+
4591+
The ``priority`` parameter allows to override the global default
4592+
priority used by gnutls. This can be useful if the system
4593+
administrator needs to use a weaker set of crypto priorities for
4594+
QEMU without potentially forcing the weakness onto all
4595+
applications. Or conversely if one wants wants a stronger
4596+
default for QEMU than for all other applications, they can do
4597+
this through this parameter. Its format is a gnutls priority
4598+
string as described at
4599+
https://gnutls.org/manual/html_node/Priority-Strings.html.
4600+
4601+
An example of use of this object is to control UEFI HTTPS Boot.
4602+
The tls-cipher-suites object exposes the ordered list of permitted
4603+
TLS cipher suites from the host side to the guest firmware, via
4604+
fw_cfg. The list is represented as an array of IANA_TLS_CIPHER
4605+
objects. The firmware uses the IANA_TLS_CIPHER array for configuring
4606+
guest-side TLS.
4607+
4608+
In the following example, the priority at which the host-side policy
4609+
is retrieved is given by the ``priority`` property.
4610+
Given that QEMU uses GNUTLS, ``priority=@SYSTEM`` may be used to
4611+
refer to /etc/crypto-policies/back-ends/gnutls.config.
4612+
4613+
.. parsed-literal::
4614+
4615+
# |qemu_system| \
4616+
-object tls-cipher-suites,id=mysuite0,priority=@SYSTEM \
4617+
-fw_cfg name=etc/edk2/https/ciphers,gen_id=mysuite0
4618+
45824619
``-object filter-buffer,id=id,netdev=netdevid,interval=t[,queue=all|rx|tx][,status=on|off][,position=head|tail|id=<id>][,insert=behind|before]``
45834620
Interval t can't be 0, this filter batches the packet delivery:
45844621
all packets arriving in a given interval on netdev netdevid are

0 commit comments

Comments
 (0)