]>
Commit | Line | Data |
---|---|---|
e00adf6c DB |
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 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 | ||
42f7a448 | 21 | #include "qemu/osdep.h" |
e00adf6c DB |
22 | #include "crypto/tlscredsanon.h" |
23 | #include "crypto/tlscredspriv.h" | |
24 | #include "qom/object_interfaces.h" | |
25 | #include "trace.h" | |
26 | ||
27 | ||
28 | #ifdef CONFIG_GNUTLS | |
29 | ||
30 | ||
31 | static int | |
32 | qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds, | |
33 | Error **errp) | |
34 | { | |
35 | char *dhparams = NULL; | |
36 | int ret; | |
37 | int rv = -1; | |
38 | ||
39 | trace_qcrypto_tls_creds_anon_load(creds, | |
40 | creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>"); | |
41 | ||
42 | if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { | |
43 | if (qcrypto_tls_creds_get_path(&creds->parent_obj, | |
44 | QCRYPTO_TLS_CREDS_DH_PARAMS, | |
45 | false, &dhparams, errp) < 0) { | |
46 | goto cleanup; | |
47 | } | |
48 | ||
49 | ret = gnutls_anon_allocate_server_credentials(&creds->data.server); | |
50 | if (ret < 0) { | |
51 | error_setg(errp, "Cannot allocate credentials: %s", | |
52 | gnutls_strerror(ret)); | |
53 | goto cleanup; | |
54 | } | |
55 | ||
56 | if (qcrypto_tls_creds_get_dh_params_file(&creds->parent_obj, dhparams, | |
57 | &creds->parent_obj.dh_params, | |
58 | errp) < 0) { | |
59 | goto cleanup; | |
60 | } | |
61 | ||
62 | gnutls_anon_set_server_dh_params(creds->data.server, | |
63 | creds->parent_obj.dh_params); | |
64 | } else { | |
65 | ret = gnutls_anon_allocate_client_credentials(&creds->data.client); | |
66 | if (ret < 0) { | |
67 | error_setg(errp, "Cannot allocate credentials: %s", | |
68 | gnutls_strerror(ret)); | |
69 | goto cleanup; | |
70 | } | |
71 | } | |
72 | ||
73 | rv = 0; | |
74 | cleanup: | |
75 | g_free(dhparams); | |
76 | return rv; | |
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_prop_set_loaded(Object *obj, | |
123 | bool value, | |
124 | Error **errp) | |
125 | { | |
126 | QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj); | |
127 | ||
128 | if (value) { | |
129 | qcrypto_tls_creds_anon_load(creds, errp); | |
130 | } else { | |
131 | qcrypto_tls_creds_anon_unload(creds); | |
132 | } | |
133 | } | |
134 | ||
135 | ||
136 | #ifdef CONFIG_GNUTLS | |
137 | ||
138 | ||
139 | static bool | |
140 | qcrypto_tls_creds_anon_prop_get_loaded(Object *obj, | |
141 | Error **errp G_GNUC_UNUSED) | |
142 | { | |
143 | QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj); | |
144 | ||
145 | if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { | |
146 | return creds->data.server != NULL; | |
147 | } else { | |
148 | return creds->data.client != NULL; | |
149 | } | |
150 | } | |
151 | ||
152 | ||
153 | #else /* ! CONFIG_GNUTLS */ | |
154 | ||
155 | ||
156 | static bool | |
157 | qcrypto_tls_creds_anon_prop_get_loaded(Object *obj G_GNUC_UNUSED, | |
158 | Error **errp G_GNUC_UNUSED) | |
159 | { | |
160 | return false; | |
161 | } | |
162 | ||
163 | ||
164 | #endif /* ! CONFIG_GNUTLS */ | |
165 | ||
166 | ||
167 | static void | |
168 | qcrypto_tls_creds_anon_complete(UserCreatable *uc, Error **errp) | |
169 | { | |
170 | object_property_set_bool(OBJECT(uc), true, "loaded", errp); | |
171 | } | |
172 | ||
173 | ||
174 | static void | |
175 | qcrypto_tls_creds_anon_init(Object *obj) | |
176 | { | |
177 | object_property_add_bool(obj, "loaded", | |
178 | qcrypto_tls_creds_anon_prop_get_loaded, | |
179 | qcrypto_tls_creds_anon_prop_set_loaded, | |
180 | NULL); | |
181 | } | |
182 | ||
183 | ||
184 | static void | |
185 | qcrypto_tls_creds_anon_finalize(Object *obj) | |
186 | { | |
187 | QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj); | |
188 | ||
189 | qcrypto_tls_creds_anon_unload(creds); | |
190 | } | |
191 | ||
192 | ||
193 | static void | |
194 | qcrypto_tls_creds_anon_class_init(ObjectClass *oc, void *data) | |
195 | { | |
196 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); | |
197 | ||
198 | ucc->complete = qcrypto_tls_creds_anon_complete; | |
199 | } | |
200 | ||
201 | ||
202 | static const TypeInfo qcrypto_tls_creds_anon_info = { | |
203 | .parent = TYPE_QCRYPTO_TLS_CREDS, | |
204 | .name = TYPE_QCRYPTO_TLS_CREDS_ANON, | |
205 | .instance_size = sizeof(QCryptoTLSCredsAnon), | |
206 | .instance_init = qcrypto_tls_creds_anon_init, | |
207 | .instance_finalize = qcrypto_tls_creds_anon_finalize, | |
208 | .class_size = sizeof(QCryptoTLSCredsAnonClass), | |
209 | .class_init = qcrypto_tls_creds_anon_class_init, | |
210 | .interfaces = (InterfaceInfo[]) { | |
211 | { TYPE_USER_CREATABLE }, | |
212 | { } | |
213 | } | |
214 | }; | |
215 | ||
216 | ||
217 | static void | |
218 | qcrypto_tls_creds_anon_register_types(void) | |
219 | { | |
220 | type_register_static(&qcrypto_tls_creds_anon_info); | |
221 | } | |
222 | ||
223 | ||
224 | type_init(qcrypto_tls_creds_anon_register_types); |