1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright © 2015 Collabora, Ltd.
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
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "config.h"
21
22#include "gdummytlsbackend.h"
23
24#include <glib.h>
25
26#include "gasyncresult.h"
27#include "gcancellable.h"
28#include "ginitable.h"
29#include "gdtlsclientconnection.h"
30#include "gdtlsconnection.h"
31#include "gdtlsserverconnection.h"
32#include "gtlsbackend.h"
33#include "gtlscertificate.h"
34#include "gtlsclientconnection.h"
35#include "gtlsdatabase.h"
36#include "gtlsfiledatabase.h"
37#include "gtlsserverconnection.h"
38
39#include "giomodule.h"
40#include "giomodule-priv.h"
41
42#include "glibintl.h"
43
44static GType _g_dummy_tls_certificate_get_type (void);
45static GType _g_dummy_tls_connection_get_type (void);
46static GType _g_dummy_dtls_connection_get_type (void);
47static GType _g_dummy_tls_database_get_type (void);
48
49struct _GDummyTlsBackend {
50 GObject parent_instance;
51 GTlsDatabase *database;
52};
53
54static void g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface);
55
56#define g_dummy_tls_backend_get_type _g_dummy_tls_backend_get_type
57G_DEFINE_TYPE_WITH_CODE (GDummyTlsBackend, g_dummy_tls_backend, G_TYPE_OBJECT,
58 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
59 g_dummy_tls_backend_iface_init)
60 _g_io_modules_ensure_extension_points_registered ();
61 g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
62 g_define_type_id,
63 "dummy",
64 -100);)
65
66static void
67g_dummy_tls_backend_init (GDummyTlsBackend *dummy)
68{
69}
70
71static void
72g_dummy_tls_backend_finalize (GObject *object)
73{
74 GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (object);
75
76 g_clear_object (&dummy->database);
77
78 G_OBJECT_CLASS (g_dummy_tls_backend_parent_class)->finalize (object);
79}
80
81static void
82g_dummy_tls_backend_class_init (GDummyTlsBackendClass *backend_class)
83{
84 GObjectClass *object_class = G_OBJECT_CLASS (backend_class);
85
86 object_class->finalize = g_dummy_tls_backend_finalize;
87}
88
89static GTlsDatabase *
90g_dummy_tls_backend_get_default_database (GTlsBackend *backend)
91{
92 GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (backend);
93
94 if (g_once_init_enter (&dummy->database))
95 {
96 GTlsDatabase *tlsdb;
97
98 tlsdb = g_object_new (object_type: _g_dummy_tls_database_get_type (), NULL);
99 g_once_init_leave (&dummy->database, tlsdb);
100 }
101
102 return g_object_ref (dummy->database);
103}
104
105static void
106g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface)
107{
108 iface->get_certificate_type = _g_dummy_tls_certificate_get_type;
109 iface->get_client_connection_type = _g_dummy_tls_connection_get_type;
110 iface->get_server_connection_type = _g_dummy_tls_connection_get_type;
111 iface->get_dtls_client_connection_type = _g_dummy_dtls_connection_get_type;
112 iface->get_dtls_server_connection_type = _g_dummy_dtls_connection_get_type;
113 iface->get_file_database_type = _g_dummy_tls_database_get_type;
114 iface->get_default_database = g_dummy_tls_backend_get_default_database;
115}
116
117/* Dummy certificate type */
118
119typedef struct _GDummyTlsCertificate GDummyTlsCertificate;
120typedef struct _GDummyTlsCertificateClass GDummyTlsCertificateClass;
121
122struct _GDummyTlsCertificate {
123 GTlsCertificate parent_instance;
124};
125
126struct _GDummyTlsCertificateClass {
127 GTlsCertificateClass parent_class;
128};
129
130enum
131{
132 PROP_CERTIFICATE_0,
133
134 PROP_CERT_CERTIFICATE,
135 PROP_CERT_CERTIFICATE_PEM,
136 PROP_CERT_PRIVATE_KEY,
137 PROP_CERT_PRIVATE_KEY_PEM,
138 PROP_CERT_ISSUER
139};
140
141static void g_dummy_tls_certificate_initable_iface_init (GInitableIface *iface);
142
143#define g_dummy_tls_certificate_get_type _g_dummy_tls_certificate_get_type
144G_DEFINE_TYPE_WITH_CODE (GDummyTlsCertificate, g_dummy_tls_certificate, G_TYPE_TLS_CERTIFICATE,
145 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
146 g_dummy_tls_certificate_initable_iface_init))
147
148static void
149g_dummy_tls_certificate_get_property (GObject *object,
150 guint prop_id,
151 GValue *value,
152 GParamSpec *pspec)
153{
154 /* We need to define this method to make GObject happy, but it will
155 * never be possible to construct a working GDummyTlsCertificate, so
156 * it doesn't have to do anything useful.
157 */
158}
159
160static void
161g_dummy_tls_certificate_set_property (GObject *object,
162 guint prop_id,
163 const GValue *value,
164 GParamSpec *pspec)
165{
166 /* Just ignore all attempts to set properties. */
167}
168
169static void
170g_dummy_tls_certificate_class_init (GDummyTlsCertificateClass *certificate_class)
171{
172 GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
173
174 gobject_class->get_property = g_dummy_tls_certificate_get_property;
175 gobject_class->set_property = g_dummy_tls_certificate_set_property;
176
177 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CERT_CERTIFICATE, name: "certificate");
178 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CERT_CERTIFICATE_PEM, name: "certificate-pem");
179 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CERT_PRIVATE_KEY, name: "private-key");
180 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CERT_PRIVATE_KEY_PEM, name: "private-key-pem");
181 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CERT_ISSUER, name: "issuer");
182}
183
184static void
185g_dummy_tls_certificate_init (GDummyTlsCertificate *certificate)
186{
187}
188
189static gboolean
190g_dummy_tls_certificate_initable_init (GInitable *initable,
191 GCancellable *cancellable,
192 GError **error)
193{
194 g_set_error_literal (err: error, G_TLS_ERROR, code: G_TLS_ERROR_UNAVAILABLE,
195 _("TLS support is not available"));
196 return FALSE;
197}
198
199static void
200g_dummy_tls_certificate_initable_iface_init (GInitableIface *iface)
201{
202 iface->init = g_dummy_tls_certificate_initable_init;
203}
204
205/* Dummy connection type; since GTlsClientConnection and
206 * GTlsServerConnection are just interfaces, we can implement them
207 * both on a single object.
208 */
209
210typedef struct _GDummyTlsConnection GDummyTlsConnection;
211typedef struct _GDummyTlsConnectionClass GDummyTlsConnectionClass;
212
213struct _GDummyTlsConnection {
214 GTlsConnection parent_instance;
215};
216
217struct _GDummyTlsConnectionClass {
218 GTlsConnectionClass parent_class;
219};
220
221enum
222{
223 PROP_CONNECTION_0,
224
225 PROP_CONN_BASE_IO_STREAM,
226 PROP_CONN_USE_SYSTEM_CERTDB,
227 PROP_CONN_REQUIRE_CLOSE_NOTIFY,
228 PROP_CONN_REHANDSHAKE_MODE,
229 PROP_CONN_CERTIFICATE,
230 PROP_CONN_DATABASE,
231 PROP_CONN_INTERACTION,
232 PROP_CONN_PEER_CERTIFICATE,
233 PROP_CONN_PEER_CERTIFICATE_ERRORS,
234 PROP_CONN_VALIDATION_FLAGS,
235 PROP_CONN_SERVER_IDENTITY,
236 PROP_CONN_USE_SSL3,
237 PROP_CONN_ACCEPTED_CAS,
238 PROP_CONN_AUTHENTICATION_MODE,
239 PROP_CONN_ADVERTISED_PROTOCOLS,
240 PROP_CONN_NEGOTIATED_PROTOCOL,
241};
242
243static void g_dummy_tls_connection_initable_iface_init (GInitableIface *iface);
244
245#define g_dummy_tls_connection_get_type _g_dummy_tls_connection_get_type
246G_DEFINE_TYPE_WITH_CODE (GDummyTlsConnection, g_dummy_tls_connection, G_TYPE_TLS_CONNECTION,
247 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL)
248 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL)
249 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
250 g_dummy_tls_connection_initable_iface_init))
251
252static void
253g_dummy_tls_connection_get_property (GObject *object,
254 guint prop_id,
255 GValue *value,
256 GParamSpec *pspec)
257{
258}
259
260static void
261g_dummy_tls_connection_set_property (GObject *object,
262 guint prop_id,
263 const GValue *value,
264 GParamSpec *pspec)
265{
266}
267
268static gboolean
269g_dummy_tls_connection_close (GIOStream *stream,
270 GCancellable *cancellable,
271 GError **error)
272{
273 return TRUE;
274}
275
276static void
277g_dummy_tls_connection_class_init (GDummyTlsConnectionClass *connection_class)
278{
279 GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
280 GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
281
282 gobject_class->get_property = g_dummy_tls_connection_get_property;
283 gobject_class->set_property = g_dummy_tls_connection_set_property;
284
285 /* Need to override this because when initable_init fails it will
286 * dispose the connection, which will close it, which would
287 * otherwise try to close its input/output streams, which don't
288 * exist.
289 */
290 io_stream_class->close_fn = g_dummy_tls_connection_close;
291
292 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_BASE_IO_STREAM, name: "base-io-stream");
293 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_USE_SYSTEM_CERTDB, name: "use-system-certdb");
294 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_REQUIRE_CLOSE_NOTIFY, name: "require-close-notify");
295 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_REHANDSHAKE_MODE, name: "rehandshake-mode");
296 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_CERTIFICATE, name: "certificate");
297 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_DATABASE, name: "database");
298 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_INTERACTION, name: "interaction");
299 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_PEER_CERTIFICATE, name: "peer-certificate");
300 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_PEER_CERTIFICATE_ERRORS, name: "peer-certificate-errors");
301 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_VALIDATION_FLAGS, name: "validation-flags");
302 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_SERVER_IDENTITY, name: "server-identity");
303 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_USE_SSL3, name: "use-ssl3");
304 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_ACCEPTED_CAS, name: "accepted-cas");
305 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_AUTHENTICATION_MODE, name: "authentication-mode");
306 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_ADVERTISED_PROTOCOLS, name: "advertised-protocols");
307 g_object_class_override_property (oclass: gobject_class, property_id: PROP_CONN_NEGOTIATED_PROTOCOL, name: "negotiated-protocol");
308}
309
310static void
311g_dummy_tls_connection_init (GDummyTlsConnection *connection)
312{
313}
314
315static gboolean
316g_dummy_tls_connection_initable_init (GInitable *initable,
317 GCancellable *cancellable,
318 GError **error)
319{
320 g_set_error_literal (err: error, G_TLS_ERROR, code: G_TLS_ERROR_UNAVAILABLE,
321 _("TLS support is not available"));
322 return FALSE;
323}
324
325static void
326g_dummy_tls_connection_initable_iface_init (GInitableIface *iface)
327{
328 iface->init = g_dummy_tls_connection_initable_init;
329}
330
331/* Dummy DTLS connection type; since GDtlsClientConnection and
332 * GDtlsServerConnection are just interfaces, we can implement them
333 * both on a single object.
334 */
335
336typedef struct _GDummyDtlsConnection GDummyDtlsConnection;
337typedef struct _GDummyDtlsConnectionClass GDummyDtlsConnectionClass;
338
339struct _GDummyDtlsConnection {
340 GObject parent_instance;
341};
342
343struct _GDummyDtlsConnectionClass {
344 GObjectClass parent_class;
345};
346
347enum
348{
349 PROP_DTLS_CONN_BASE_SOCKET = 1,
350 PROP_DTLS_CONN_REQUIRE_CLOSE_NOTIFY,
351 PROP_DTLS_CONN_REHANDSHAKE_MODE,
352 PROP_DTLS_CONN_CERTIFICATE,
353 PROP_DTLS_CONN_DATABASE,
354 PROP_DTLS_CONN_INTERACTION,
355 PROP_DTLS_CONN_PEER_CERTIFICATE,
356 PROP_DTLS_CONN_PEER_CERTIFICATE_ERRORS,
357 PROP_DTLS_CONN_VALIDATION_FLAGS,
358 PROP_DTLS_CONN_SERVER_IDENTITY,
359 PROP_DTLS_CONN_ENABLE_NEGOTIATION,
360 PROP_DTLS_CONN_ACCEPTED_CAS,
361 PROP_DTLS_CONN_AUTHENTICATION_MODE,
362};
363
364static void g_dummy_dtls_connection_initable_iface_init (GInitableIface *iface);
365
366#define g_dummy_dtls_connection_get_type _g_dummy_dtls_connection_get_type
367G_DEFINE_TYPE_WITH_CODE (GDummyDtlsConnection, g_dummy_dtls_connection, G_TYPE_OBJECT,
368 G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_CONNECTION, NULL);
369 G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_CLIENT_CONNECTION, NULL);
370 G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_SERVER_CONNECTION, NULL);
371 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
372 g_dummy_dtls_connection_initable_iface_init);)
373
374static void
375g_dummy_dtls_connection_get_property (GObject *object,
376 guint prop_id,
377 GValue *value,
378 GParamSpec *pspec)
379{
380}
381
382static void
383g_dummy_dtls_connection_set_property (GObject *object,
384 guint prop_id,
385 const GValue *value,
386 GParamSpec *pspec)
387{
388}
389
390static void
391g_dummy_dtls_connection_class_init (GDummyDtlsConnectionClass *connection_class)
392{
393 GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
394
395 gobject_class->get_property = g_dummy_dtls_connection_get_property;
396 gobject_class->set_property = g_dummy_dtls_connection_set_property;
397
398 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_BASE_SOCKET, name: "base-socket");
399 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_REQUIRE_CLOSE_NOTIFY, name: "require-close-notify");
400 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_REHANDSHAKE_MODE, name: "rehandshake-mode");
401 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_CERTIFICATE, name: "certificate");
402 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_DATABASE, name: "database");
403 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_INTERACTION, name: "interaction");
404 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_PEER_CERTIFICATE, name: "peer-certificate");
405 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_PEER_CERTIFICATE_ERRORS, name: "peer-certificate-errors");
406 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_VALIDATION_FLAGS, name: "validation-flags");
407 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_SERVER_IDENTITY, name: "server-identity");
408 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_ACCEPTED_CAS, name: "accepted-cas");
409 g_object_class_override_property (oclass: gobject_class, property_id: PROP_DTLS_CONN_AUTHENTICATION_MODE, name: "authentication-mode");
410}
411
412static void
413g_dummy_dtls_connection_init (GDummyDtlsConnection *connection)
414{
415}
416
417static gboolean
418g_dummy_dtls_connection_initable_init (GInitable *initable,
419 GCancellable *cancellable,
420 GError **error)
421{
422 g_set_error_literal (err: error, G_TLS_ERROR, code: G_TLS_ERROR_UNAVAILABLE,
423 _("DTLS support is not available"));
424 return FALSE;
425}
426
427static void
428g_dummy_dtls_connection_initable_iface_init (GInitableIface *iface)
429{
430 iface->init = g_dummy_dtls_connection_initable_init;
431}
432
433/* Dummy database type.
434 */
435
436typedef struct _GDummyTlsDatabase GDummyTlsDatabase;
437typedef struct _GDummyTlsDatabaseClass GDummyTlsDatabaseClass;
438
439struct _GDummyTlsDatabase {
440 GTlsDatabase parent_instance;
441};
442
443struct _GDummyTlsDatabaseClass {
444 GTlsDatabaseClass parent_class;
445};
446
447enum
448{
449 PROP_DATABASE_0,
450
451 PROP_ANCHORS,
452};
453
454static void g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface *iface);
455static void g_dummy_tls_database_initable_iface_init (GInitableIface *iface);
456
457#define g_dummy_tls_database_get_type _g_dummy_tls_database_get_type
458G_DEFINE_TYPE_WITH_CODE (GDummyTlsDatabase, g_dummy_tls_database, G_TYPE_TLS_DATABASE,
459 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_FILE_DATABASE,
460 g_dummy_tls_database_file_database_iface_init)
461 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
462 g_dummy_tls_database_initable_iface_init))
463
464
465static void
466g_dummy_tls_database_get_property (GObject *object,
467 guint prop_id,
468 GValue *value,
469 GParamSpec *pspec)
470{
471 /* We need to define this method to make GObject happy, but it will
472 * never be possible to construct a working GDummyTlsDatabase, so
473 * it doesn't have to do anything useful.
474 */
475}
476
477static void
478g_dummy_tls_database_set_property (GObject *object,
479 guint prop_id,
480 const GValue *value,
481 GParamSpec *pspec)
482{
483 /* Just ignore all attempts to set properties. */
484}
485
486static void
487g_dummy_tls_database_class_init (GDummyTlsDatabaseClass *database_class)
488{
489 GObjectClass *gobject_class = G_OBJECT_CLASS (database_class);
490
491 gobject_class->get_property = g_dummy_tls_database_get_property;
492 gobject_class->set_property = g_dummy_tls_database_set_property;
493
494 g_object_class_override_property (oclass: gobject_class, property_id: PROP_ANCHORS, name: "anchors");
495}
496
497static void
498g_dummy_tls_database_init (GDummyTlsDatabase *database)
499{
500}
501
502static void
503g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface *iface)
504{
505}
506
507static gboolean
508g_dummy_tls_database_initable_init (GInitable *initable,
509 GCancellable *cancellable,
510 GError **error)
511{
512 g_set_error_literal (err: error, G_TLS_ERROR, code: G_TLS_ERROR_UNAVAILABLE,
513 _("TLS support is not available"));
514 return FALSE;
515}
516
517static void
518g_dummy_tls_database_initable_iface_init (GInitableIface *iface)
519{
520 iface->init = g_dummy_tls_database_initable_init;
521}
522

source code of gtk/subprojects/glib/gio/gdummytlsbackend.c