| 1 | /* |
| 2 | * QEMU crypto TLS anonymous credential support |
| 3 | * |
| 4 | * Copyright (c) 2015 Red Hat, Inc. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "qemu/osdep.h" |
| 22 | #include "crypto/tlscredsanon.h" |
| 23 | #include "tlscredspriv.h" |
| 24 | #include "qapi/error.h" |
| 25 | #include "qemu/module.h" |
| 26 | #include "qom/object_interfaces.h" |
| 27 | #include "trace.h" |
| 28 | |
| 29 | |
| 30 | struct QCryptoTLSCredsAnon { |
| 31 | QCryptoTLSCreds parent_obj; |
| 32 | }; |
| 33 | |
| 34 | #ifdef CONFIG_GNUTLS |
| 35 | |
| 36 | #include <gnutls/gnutls.h> |
| 37 | |
| 38 | static int |
| 39 | qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds, |
| 40 | Error **errp) |
| 41 | { |
| 42 | g_autoptr(QCryptoTLSCredsBox) box = NULL; |
| 43 | g_autofree char *dhparams = NULL; |
| 44 | int ret; |
| 45 | |
| 46 | trace_qcrypto_tls_creds_anon_load(creds, |
| 47 | creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>"); |
| 48 | |
| 49 | if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { |
| 50 | box = qcrypto_tls_creds_box_new_server(GNUTLS_CRD_ANON); |
| 51 | |
| 52 | if (creds->parent_obj.dir && |
| 53 | qcrypto_tls_creds_get_path(&creds->parent_obj, |
| 54 | QCRYPTO_TLS_CREDS_DH_PARAMS, |
| 55 | false, &dhparams, errp) < 0) { |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | ret = gnutls_anon_allocate_server_credentials(&box->data.anonserver); |
| 60 | if (ret < 0) { |
| 61 | error_setg(errp, "Cannot allocate credentials: %s", |
| 62 | gnutls_strerror(ret)); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | if (qcrypto_tls_creds_get_dh_params_file(&creds->parent_obj, dhparams, |
| 67 | &box->dh_params, errp) < 0) { |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | if (box->dh_params) { |
| 72 | gnutls_anon_set_server_dh_params(box->data.anonserver, |
| 73 | box->dh_params); |
| 74 | } |
| 75 | } else { |
| 76 | box = qcrypto_tls_creds_box_new_client(GNUTLS_CRD_ANON); |
| 77 | |
| 78 | ret = gnutls_anon_allocate_client_credentials(&box->data.anonclient); |
| 79 | if (ret < 0) { |
| 80 | error_setg(errp, "Cannot allocate credentials: %s", |
| 81 | gnutls_strerror(ret)); |
| 82 | return -1; |
| 83 | } |
| 84 | } |
| 85 | creds->parent_obj.box = g_steal_pointer(&box); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | #else /* ! CONFIG_GNUTLS */ |
| 92 | |
| 93 | |
| 94 | static void |
| 95 | qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED, |
| 96 | Error **errp) |
| 97 | { |
| 98 | error_setg(errp, "TLS credentials support requires GNUTLS"); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | #endif /* ! CONFIG_GNUTLS */ |
| 103 | |
| 104 | |
| 105 | static void |
| 106 | qcrypto_tls_creds_anon_complete(UserCreatable *uc, Error **errp) |
| 107 | { |
| 108 | QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(uc); |
| 109 | |
| 110 | qcrypto_tls_creds_anon_load(creds, errp); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static void |
| 115 | qcrypto_tls_creds_anon_class_init(ObjectClass *oc, const void *data) |
| 116 | { |
| 117 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); |
| 118 | QCryptoTLSCredsClass *tcc = QCRYPTO_TLS_CREDS_CLASS(oc); |
| 119 | |
| 120 | ucc->complete = qcrypto_tls_creds_anon_complete; |
| 121 | tcc->prioritySuffix = "+ANON-DH"; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | static const TypeInfo qcrypto_tls_creds_anon_info = { |
| 126 | .parent = TYPE_QCRYPTO_TLS_CREDS, |
| 127 | .name = TYPE_QCRYPTO_TLS_CREDS_ANON, |
| 128 | .instance_size = sizeof(QCryptoTLSCredsAnon), |
| 129 | .class_size = sizeof(QCryptoTLSCredsAnonClass), |
| 130 | .class_init = qcrypto_tls_creds_anon_class_init, |
| 131 | .interfaces = (const InterfaceInfo[]) { |
| 132 | { TYPE_USER_CREATABLE }, |
| 133 | { } |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | static void |
| 139 | qcrypto_tls_creds_anon_register_types(void) |
| 140 | { |
| 141 | type_register_static(&qcrypto_tls_creds_anon_info); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | type_init(qcrypto_tls_creds_anon_register_types); |