Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 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" |
Philippe Mathieu-Daudé | 69699f3 | 2020-05-14 15:15:47 +0200 | [diff] [blame] | 16 | #include "hw/nvram/fw_cfg.h" |
Philippe Mathieu-Daudé | 678bcc3 | 2021-06-28 18:09:14 +0200 | [diff] [blame] | 17 | #include "tlscredspriv.h" |
Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 18 | #include "trace.h" |
| 19 | |
Philippe Mathieu-Daudé | 678bcc3 | 2021-06-28 18:09:14 +0200 | [diff] [blame] | 20 | struct QCryptoTLSCipherSuites { |
| 21 | /* <private> */ |
| 22 | QCryptoTLSCreds parent_obj; |
| 23 | /* <public> */ |
| 24 | }; |
| 25 | |
Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 26 | /* |
| 27 | * IANA registered TLS ciphers: |
| 28 | * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4 |
| 29 | */ |
| 30 | typedef struct { |
| 31 | uint8_t data[2]; |
| 32 | } QEMU_PACKED IANA_TLS_CIPHER; |
| 33 | |
| 34 | GByteArray *qcrypto_tls_cipher_suites_get_data(QCryptoTLSCipherSuites *obj, |
| 35 | Error **errp) |
| 36 | { |
| 37 | QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj); |
| 38 | gnutls_priority_t pcache; |
| 39 | GByteArray *byte_array; |
| 40 | const char *err; |
| 41 | size_t i; |
| 42 | int ret; |
| 43 | |
| 44 | trace_qcrypto_tls_cipher_suite_priority(creds->priority); |
| 45 | ret = gnutls_priority_init(&pcache, creds->priority, &err); |
| 46 | if (ret < 0) { |
| 47 | error_setg(errp, "Syntax error using priority '%s': %s", |
| 48 | creds->priority, gnutls_strerror(ret)); |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | byte_array = g_byte_array_new(); |
| 53 | |
| 54 | for (i = 0;; i++) { |
Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 55 | unsigned idx; |
| 56 | const char *name; |
| 57 | IANA_TLS_CIPHER cipher; |
| 58 | gnutls_protocol_t protocol; |
| 59 | const char *version; |
| 60 | |
| 61 | ret = gnutls_priority_get_cipher_suite_index(pcache, i, &idx); |
| 62 | if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { |
| 63 | break; |
| 64 | } |
| 65 | if (ret == GNUTLS_E_UNKNOWN_CIPHER_SUITE) { |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | name = gnutls_cipher_suite_info(idx, (unsigned char *)&cipher, |
| 70 | NULL, NULL, NULL, &protocol); |
| 71 | if (name == NULL) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | version = gnutls_protocol_get_name(protocol); |
| 76 | g_byte_array_append(byte_array, cipher.data, 2); |
| 77 | trace_qcrypto_tls_cipher_suite_info(cipher.data[0], |
| 78 | cipher.data[1], |
| 79 | version, name); |
| 80 | } |
| 81 | trace_qcrypto_tls_cipher_suite_count(byte_array->len); |
| 82 | gnutls_priority_deinit(pcache); |
| 83 | |
| 84 | return byte_array; |
| 85 | } |
| 86 | |
| 87 | static void qcrypto_tls_cipher_suites_complete(UserCreatable *uc, |
| 88 | Error **errp) |
| 89 | { |
| 90 | QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(uc); |
| 91 | |
| 92 | if (!creds->priority) { |
| 93 | error_setg(errp, "'priority' property is not set"); |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | |
Philippe Mathieu-Daudé | 69699f3 | 2020-05-14 15:15:47 +0200 | [diff] [blame] | 98 | static GByteArray *qcrypto_tls_cipher_suites_fw_cfg_gen_data(Object *obj, |
| 99 | Error **errp) |
| 100 | { |
| 101 | return qcrypto_tls_cipher_suites_get_data(QCRYPTO_TLS_CIPHER_SUITES(obj), |
| 102 | errp); |
| 103 | } |
| 104 | |
Philippe Mathieu-Daudé | 12d1a76 | 2025-02-09 23:47:35 +0100 | [diff] [blame] | 105 | static void qcrypto_tls_cipher_suites_class_init(ObjectClass *oc, |
| 106 | const void *data) |
Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 107 | { |
| 108 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); |
Philippe Mathieu-Daudé | 69699f3 | 2020-05-14 15:15:47 +0200 | [diff] [blame] | 109 | FWCfgDataGeneratorClass *fwgc = FW_CFG_DATA_GENERATOR_CLASS(oc); |
Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 110 | |
| 111 | ucc->complete = qcrypto_tls_cipher_suites_complete; |
Philippe Mathieu-Daudé | 69699f3 | 2020-05-14 15:15:47 +0200 | [diff] [blame] | 112 | fwgc->get_data = qcrypto_tls_cipher_suites_fw_cfg_gen_data; |
Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static const TypeInfo qcrypto_tls_cipher_suites_info = { |
| 116 | .parent = TYPE_QCRYPTO_TLS_CREDS, |
| 117 | .name = TYPE_QCRYPTO_TLS_CIPHER_SUITES, |
Eduardo Habkost | a7c893a | 2020-08-26 13:10:05 -0400 | [diff] [blame] | 118 | .instance_size = sizeof(QCryptoTLSCipherSuites), |
Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 119 | .class_size = sizeof(QCryptoTLSCredsClass), |
| 120 | .class_init = qcrypto_tls_cipher_suites_class_init, |
Philippe Mathieu-Daudé | 2cd09e4 | 2025-04-23 18:46:19 +0200 | [diff] [blame] | 121 | .interfaces = (const InterfaceInfo[]) { |
Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 122 | { TYPE_USER_CREATABLE }, |
Philippe Mathieu-Daudé | 69699f3 | 2020-05-14 15:15:47 +0200 | [diff] [blame] | 123 | { TYPE_FW_CFG_DATA_GENERATOR_INTERFACE }, |
Philippe Mathieu-Daudé | 993aec2 | 2018-10-11 20:21:11 +0200 | [diff] [blame] | 124 | { } |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | static void qcrypto_tls_cipher_suites_register_types(void) |
| 129 | { |
| 130 | type_register_static(&qcrypto_tls_cipher_suites_info); |
| 131 | } |
| 132 | |
| 133 | type_init(qcrypto_tls_cipher_suites_register_types); |