xref: /openbmc/qemu/crypto/tlscredsanon.c (revision ed75658a)
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 #ifdef CONFIG_GNUTLS
31 
32 #include <gnutls/gnutls.h>
33 
34 
35 static int
36 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds,
37                             Error **errp)
38 {
39     g_autofree char *dhparams = NULL;
40     int ret;
41 
42     trace_qcrypto_tls_creds_anon_load(creds,
43             creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>");
44 
45     if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
46         if (qcrypto_tls_creds_get_path(&creds->parent_obj,
47                                        QCRYPTO_TLS_CREDS_DH_PARAMS,
48                                        false, &dhparams, errp) < 0) {
49             return -1;
50         }
51 
52         ret = gnutls_anon_allocate_server_credentials(&creds->data.server);
53         if (ret < 0) {
54             error_setg(errp, "Cannot allocate credentials: %s",
55                        gnutls_strerror(ret));
56             return -1;
57         }
58 
59         if (qcrypto_tls_creds_get_dh_params_file(&creds->parent_obj, dhparams,
60                                                  &creds->parent_obj.dh_params,
61                                                  errp) < 0) {
62             return -1;
63         }
64 
65         gnutls_anon_set_server_dh_params(creds->data.server,
66                                          creds->parent_obj.dh_params);
67     } else {
68         ret = gnutls_anon_allocate_client_credentials(&creds->data.client);
69         if (ret < 0) {
70             error_setg(errp, "Cannot allocate credentials: %s",
71                        gnutls_strerror(ret));
72             return -1;
73         }
74     }
75 
76     return 0;
77 }
78 
79 
80 static void
81 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds)
82 {
83     if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
84         if (creds->data.client) {
85             gnutls_anon_free_client_credentials(creds->data.client);
86             creds->data.client = NULL;
87         }
88     } else {
89         if (creds->data.server) {
90             gnutls_anon_free_server_credentials(creds->data.server);
91             creds->data.server = NULL;
92         }
93     }
94     if (creds->parent_obj.dh_params) {
95         gnutls_dh_params_deinit(creds->parent_obj.dh_params);
96         creds->parent_obj.dh_params = NULL;
97     }
98 }
99 
100 #else /* ! CONFIG_GNUTLS */
101 
102 
103 static void
104 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED,
105                             Error **errp)
106 {
107     error_setg(errp, "TLS credentials support requires GNUTLS");
108 }
109 
110 
111 static void
112 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED)
113 {
114     /* nada */
115 }
116 
117 
118 #endif /* ! CONFIG_GNUTLS */
119 
120 
121 static void
122 qcrypto_tls_creds_anon_complete(UserCreatable *uc, Error **errp)
123 {
124     QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(uc);
125 
126     qcrypto_tls_creds_anon_load(creds, errp);
127 }
128 
129 
130 #ifdef CONFIG_GNUTLS
131 
132 
133 static bool
134 qcrypto_tls_creds_anon_prop_get_loaded(Object *obj,
135                                        Error **errp G_GNUC_UNUSED)
136 {
137     QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj);
138 
139     if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
140         return creds->data.server != NULL;
141     } else {
142         return creds->data.client != NULL;
143     }
144 }
145 
146 
147 #else /* ! CONFIG_GNUTLS */
148 
149 
150 static bool
151 qcrypto_tls_creds_anon_prop_get_loaded(Object *obj G_GNUC_UNUSED,
152                                        Error **errp G_GNUC_UNUSED)
153 {
154     return false;
155 }
156 
157 
158 #endif /* ! CONFIG_GNUTLS */
159 
160 
161 static void
162 qcrypto_tls_creds_anon_finalize(Object *obj)
163 {
164     QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj);
165 
166     qcrypto_tls_creds_anon_unload(creds);
167 }
168 
169 
170 static void
171 qcrypto_tls_creds_anon_class_init(ObjectClass *oc, void *data)
172 {
173     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
174 
175     ucc->complete = qcrypto_tls_creds_anon_complete;
176 
177     object_class_property_add_bool(oc, "loaded",
178                                    qcrypto_tls_creds_anon_prop_get_loaded,
179                                    NULL);
180 }
181 
182 
183 static const TypeInfo qcrypto_tls_creds_anon_info = {
184     .parent = TYPE_QCRYPTO_TLS_CREDS,
185     .name = TYPE_QCRYPTO_TLS_CREDS_ANON,
186     .instance_size = sizeof(QCryptoTLSCredsAnon),
187     .instance_finalize = qcrypto_tls_creds_anon_finalize,
188     .class_size = sizeof(QCryptoTLSCredsAnonClass),
189     .class_init = qcrypto_tls_creds_anon_class_init,
190     .interfaces = (InterfaceInfo[]) {
191         { TYPE_USER_CREATABLE },
192         { }
193     }
194 };
195 
196 
197 static void
198 qcrypto_tls_creds_anon_register_types(void)
199 {
200     type_register_static(&qcrypto_tls_creds_anon_info);
201 }
202 
203 
204 type_init(qcrypto_tls_creds_anon_register_types);
205