Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/gdummytlsbackend.c
Line
Count
Source (jump to first uncovered line)
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
44
static GType _g_dummy_tls_certificate_get_type (void);
45
static GType _g_dummy_tls_connection_get_type (void);
46
static GType _g_dummy_dtls_connection_get_type (void);
47
static GType _g_dummy_tls_database_get_type (void);
48
49
struct _GDummyTlsBackend {
50
  GObject       parent_instance;
51
  GTlsDatabase *database;
52
};
53
54
static void g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface);
55
56
#define g_dummy_tls_backend_get_type _g_dummy_tls_backend_get_type
57
G_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
66
static void
67
g_dummy_tls_backend_init (GDummyTlsBackend *dummy)
68
0
{
69
0
}
70
71
static void
72
g_dummy_tls_backend_finalize (GObject *object)
73
0
{
74
0
  GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (object);
75
76
0
  g_clear_object (&dummy->database);
77
78
0
  G_OBJECT_CLASS (g_dummy_tls_backend_parent_class)->finalize (object);
79
0
}
80
81
static void
82
g_dummy_tls_backend_class_init (GDummyTlsBackendClass *backend_class)
83
0
{
84
0
  GObjectClass *object_class = G_OBJECT_CLASS (backend_class);
85
86
0
  object_class->finalize = g_dummy_tls_backend_finalize;
87
0
}
88
89
static GTlsDatabase *
90
g_dummy_tls_backend_get_default_database (GTlsBackend *backend)
91
0
{
92
0
  GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (backend);
93
94
0
  if (g_once_init_enter (&dummy->database))
95
0
    {
96
0
      GTlsDatabase *tlsdb;
97
98
0
      tlsdb = g_object_new (_g_dummy_tls_database_get_type (), NULL);
99
0
      g_once_init_leave (&dummy->database, tlsdb);
100
0
    }
101
102
0
  return g_object_ref (dummy->database);
103
0
}
104
105
static void
106
g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface)
107
0
{
108
0
  iface->get_certificate_type = _g_dummy_tls_certificate_get_type;
109
0
  iface->get_client_connection_type = _g_dummy_tls_connection_get_type;
110
0
  iface->get_server_connection_type = _g_dummy_tls_connection_get_type;
111
0
  iface->get_dtls_client_connection_type = _g_dummy_dtls_connection_get_type;
112
0
  iface->get_dtls_server_connection_type = _g_dummy_dtls_connection_get_type;
113
0
  iface->get_file_database_type = _g_dummy_tls_database_get_type;
114
0
  iface->get_default_database = g_dummy_tls_backend_get_default_database;
115
0
}
116
117
/* Dummy certificate type */
118
119
typedef struct _GDummyTlsCertificate      GDummyTlsCertificate;
120
typedef struct _GDummyTlsCertificateClass GDummyTlsCertificateClass;
121
122
struct _GDummyTlsCertificate {
123
  GTlsCertificate parent_instance;
124
};
125
126
struct _GDummyTlsCertificateClass {
127
  GTlsCertificateClass parent_class;
128
};
129
130
enum
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
141
static 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
144
G_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
148
static void
149
g_dummy_tls_certificate_get_property (GObject    *object,
150
              guint       prop_id,
151
              GValue     *value,
152
              GParamSpec *pspec)
153
0
{
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
0
}
159
160
static void
161
g_dummy_tls_certificate_set_property (GObject      *object,
162
              guint         prop_id,
163
              const GValue *value,
164
              GParamSpec   *pspec)
165
0
{
166
  /* Just ignore all attempts to set properties. */
167
0
}
168
169
static void
170
g_dummy_tls_certificate_class_init (GDummyTlsCertificateClass *certificate_class)
171
0
{
172
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
173
174
0
  gobject_class->get_property = g_dummy_tls_certificate_get_property;
175
0
  gobject_class->set_property = g_dummy_tls_certificate_set_property;
176
177
0
  g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
178
0
  g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
179
0
  g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
180
0
  g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
181
0
  g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
182
0
}
183
184
static void
185
g_dummy_tls_certificate_init (GDummyTlsCertificate *certificate)
186
0
{
187
0
}
188
189
static gboolean
190
g_dummy_tls_certificate_initable_init (GInitable       *initable,
191
               GCancellable    *cancellable,
192
               GError         **error)
193
0
{
194
0
  g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
195
0
           _("TLS support is not available"));
196
0
  return FALSE;
197
0
}
198
199
static void
200
g_dummy_tls_certificate_initable_iface_init (GInitableIface  *iface)
201
0
{
202
0
  iface->init = g_dummy_tls_certificate_initable_init;
203
0
}
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
210
typedef struct _GDummyTlsConnection      GDummyTlsConnection;
211
typedef struct _GDummyTlsConnectionClass GDummyTlsConnectionClass;
212
213
struct _GDummyTlsConnection {
214
  GTlsConnection parent_instance;
215
};
216
217
struct _GDummyTlsConnectionClass {
218
  GTlsConnectionClass parent_class;
219
};
220
221
enum
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
243
static 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
246
G_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
252
static void
253
g_dummy_tls_connection_get_property (GObject    *object,
254
             guint       prop_id,
255
             GValue     *value,
256
             GParamSpec *pspec)
257
0
{
258
0
}
259
260
static void
261
g_dummy_tls_connection_set_property (GObject      *object,
262
             guint         prop_id,
263
             const GValue *value,
264
             GParamSpec   *pspec)
265
0
{
266
0
}
267
268
static gboolean
269
g_dummy_tls_connection_close (GIOStream     *stream,
270
            GCancellable  *cancellable,
271
            GError       **error)
272
0
{
273
0
  return TRUE;
274
0
}
275
276
static void
277
g_dummy_tls_connection_class_init (GDummyTlsConnectionClass *connection_class)
278
0
{
279
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
280
0
  GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
281
282
0
  gobject_class->get_property = g_dummy_tls_connection_get_property;
283
0
  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
0
  io_stream_class->close_fn = g_dummy_tls_connection_close;
291
292
0
  g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
293
0
  g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
294
0
  g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
295
0
  g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
296
0
  g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
297
0
  g_object_class_override_property (gobject_class, PROP_CONN_DATABASE, "database");
298
0
  g_object_class_override_property (gobject_class, PROP_CONN_INTERACTION, "interaction");
299
0
  g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
300
0
  g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
301
0
  g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
302
0
  g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
303
0
  g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
304
0
  g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
305
0
  g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
306
0
  g_object_class_override_property (gobject_class, PROP_CONN_ADVERTISED_PROTOCOLS, "advertised-protocols");
307
0
  g_object_class_override_property (gobject_class, PROP_CONN_NEGOTIATED_PROTOCOL, "negotiated-protocol");
308
0
}
309
310
static void
311
g_dummy_tls_connection_init (GDummyTlsConnection *connection)
312
0
{
313
0
}
314
315
static gboolean
316
g_dummy_tls_connection_initable_init (GInitable       *initable,
317
              GCancellable    *cancellable,
318
              GError         **error)
319
0
{
320
0
  g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
321
0
           _("TLS support is not available"));
322
0
  return FALSE;
323
0
}
324
325
static void
326
g_dummy_tls_connection_initable_iface_init (GInitableIface  *iface)
327
0
{
328
0
  iface->init = g_dummy_tls_connection_initable_init;
329
0
}
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
336
typedef struct _GDummyDtlsConnection      GDummyDtlsConnection;
337
typedef struct _GDummyDtlsConnectionClass GDummyDtlsConnectionClass;
338
339
struct _GDummyDtlsConnection {
340
  GObject parent_instance;
341
};
342
343
struct _GDummyDtlsConnectionClass {
344
  GObjectClass parent_class;
345
};
346
347
enum
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
364
static 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
367
G_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
374
static void
375
g_dummy_dtls_connection_get_property (GObject    *object,
376
                                      guint       prop_id,
377
                                      GValue     *value,
378
                                      GParamSpec *pspec)
379
0
{
380
0
}
381
382
static void
383
g_dummy_dtls_connection_set_property (GObject      *object,
384
                                      guint         prop_id,
385
                                      const GValue *value,
386
                                      GParamSpec   *pspec)
387
0
{
388
0
}
389
390
static void
391
g_dummy_dtls_connection_class_init (GDummyDtlsConnectionClass *connection_class)
392
0
{
393
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
394
395
0
  gobject_class->get_property = g_dummy_dtls_connection_get_property;
396
0
  gobject_class->set_property = g_dummy_dtls_connection_set_property;
397
398
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_BASE_SOCKET, "base-socket");
399
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
400
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
401
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_CERTIFICATE, "certificate");
402
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_DATABASE, "database");
403
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_INTERACTION, "interaction");
404
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_PEER_CERTIFICATE, "peer-certificate");
405
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
406
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_VALIDATION_FLAGS, "validation-flags");
407
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_SERVER_IDENTITY, "server-identity");
408
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_ACCEPTED_CAS, "accepted-cas");
409
0
  g_object_class_override_property (gobject_class, PROP_DTLS_CONN_AUTHENTICATION_MODE, "authentication-mode");
410
0
}
411
412
static void
413
g_dummy_dtls_connection_init (GDummyDtlsConnection *connection)
414
0
{
415
0
}
416
417
static gboolean
418
g_dummy_dtls_connection_initable_init (GInitable       *initable,
419
                                       GCancellable    *cancellable,
420
                                       GError         **error)
421
0
{
422
0
  g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
423
0
                       _("DTLS support is not available"));
424
0
  return FALSE;
425
0
}
426
427
static void
428
g_dummy_dtls_connection_initable_iface_init (GInitableIface  *iface)
429
0
{
430
0
  iface->init = g_dummy_dtls_connection_initable_init;
431
0
}
432
433
/* Dummy database type.
434
 */
435
436
typedef struct _GDummyTlsDatabase      GDummyTlsDatabase;
437
typedef struct _GDummyTlsDatabaseClass GDummyTlsDatabaseClass;
438
439
struct _GDummyTlsDatabase {
440
  GTlsDatabase parent_instance;
441
};
442
443
struct _GDummyTlsDatabaseClass {
444
  GTlsDatabaseClass parent_class;
445
};
446
447
enum
448
{
449
  PROP_DATABASE_0,
450
451
  PROP_ANCHORS,
452
};
453
454
static void g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface *iface);
455
static 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
458
G_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
465
static void
466
g_dummy_tls_database_get_property (GObject    *object,
467
                                   guint       prop_id,
468
                                   GValue     *value,
469
                                   GParamSpec *pspec)
470
0
{
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
0
}
476
477
static void
478
g_dummy_tls_database_set_property (GObject      *object,
479
                                   guint         prop_id,
480
                                   const GValue *value,
481
                                   GParamSpec   *pspec)
482
0
{
483
  /* Just ignore all attempts to set properties. */
484
0
}
485
486
static void
487
g_dummy_tls_database_class_init (GDummyTlsDatabaseClass *database_class)
488
0
{
489
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (database_class);
490
491
0
  gobject_class->get_property = g_dummy_tls_database_get_property;
492
0
  gobject_class->set_property = g_dummy_tls_database_set_property;
493
494
0
  g_object_class_override_property (gobject_class, PROP_ANCHORS, "anchors");
495
0
}
496
497
static void
498
g_dummy_tls_database_init (GDummyTlsDatabase *database)
499
0
{
500
0
}
501
502
static void
503
g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface  *iface)
504
0
{
505
0
}
506
507
static gboolean
508
g_dummy_tls_database_initable_init (GInitable       *initable,
509
                                    GCancellable    *cancellable,
510
                                    GError         **error)
511
0
{
512
0
  g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
513
0
                       _("TLS support is not available"));
514
0
  return FALSE;
515
0
}
516
517
static void
518
g_dummy_tls_database_initable_iface_init (GInitableIface  *iface)
519
0
{
520
0
  iface->init = g_dummy_tls_database_initable_init;
521
0
}