xref: /openbmc/qemu/crypto/tls-cipher-suites.c (revision 2d7ac0af)
1 /*
2  * QEMU TLS Cipher Suites
3  *
4  * Copyright (c) 2018-2020 Red Hat, Inc.
5  *
6  * Author: Philippe Mathieu-Daudé <philmd@redhat.com>
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);
127