Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/gio/gtlsbackend.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright © 2010 Red Hat, Inc
4
 * Copyright © 2015 Collabora, Ltd.
5
 *
6
 * SPDX-License-Identifier: LGPL-2.1-or-later
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General
19
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "config.h"
23
#include "glib.h"
24
25
#include "gtlsbackend.h"
26
#include "gtlsdatabase.h"
27
#include "gdummytlsbackend.h"
28
#include "gioenumtypes.h"
29
#include "giomodule-priv.h"
30
31
/**
32
 * GTlsBackend:
33
 *
34
 * TLS (Transport Layer Security, aka SSL) and DTLS backend. This is an
35
 * internal type used to coordinate the different classes implemented
36
 * by a TLS backend.
37
 *
38
 * Since: 2.28
39
 */
40
41
G_DEFINE_INTERFACE (GTlsBackend, g_tls_backend, G_TYPE_OBJECT)
42
43
static GTlsDatabase *default_database;
44
G_LOCK_DEFINE_STATIC (default_database_lock);
45
46
static void
47
g_tls_backend_default_init (GTlsBackendInterface *iface)
48
0
{
49
0
}
50
51
static GTlsBackend *tls_backend_default_singleton = NULL;  /* (owned) (atomic) */
52
53
/**
54
 * g_tls_backend_get_default:
55
 *
56
 * Gets the default #GTlsBackend for the system.
57
 *
58
 * Returns: (not nullable) (transfer none): a #GTlsBackend, which will be a
59
 *     dummy object if no TLS backend is available
60
 *
61
 * Since: 2.28
62
 */
63
GTlsBackend *
64
g_tls_backend_get_default (void)
65
0
{
66
0
  if (g_once_init_enter_pointer (&tls_backend_default_singleton))
67
0
    {
68
0
      GTlsBackend *singleton;
69
70
0
      singleton = _g_io_module_get_default (G_TLS_BACKEND_EXTENSION_POINT_NAME,
71
0
                                            "GIO_USE_TLS",
72
0
                                            NULL);
73
74
0
      g_once_init_leave_pointer (&tls_backend_default_singleton, singleton);
75
0
    }
76
77
0
  return tls_backend_default_singleton;
78
0
}
79
80
/**
81
 * g_tls_backend_supports_tls:
82
 * @backend: the #GTlsBackend
83
 *
84
 * Checks if TLS is supported; if this returns %FALSE for the default
85
 * #GTlsBackend, it means no "real" TLS backend is available.
86
 *
87
 * Returns: whether or not TLS is supported
88
 *
89
 * Since: 2.28
90
 */
91
gboolean
92
g_tls_backend_supports_tls (GTlsBackend *backend)
93
0
{
94
0
  if (G_TLS_BACKEND_GET_INTERFACE (backend)->supports_tls)
95
0
    return G_TLS_BACKEND_GET_INTERFACE (backend)->supports_tls (backend);
96
0
  else if (G_IS_DUMMY_TLS_BACKEND (backend))
97
0
    return FALSE;
98
0
  else
99
0
    return TRUE;
100
0
}
101
102
/**
103
 * g_tls_backend_supports_dtls:
104
 * @backend: the #GTlsBackend
105
 *
106
 * Checks if DTLS is supported. DTLS support may not be available even if TLS
107
 * support is available, and vice-versa.
108
 *
109
 * Returns: whether DTLS is supported
110
 *
111
 * Since: 2.48
112
 */
113
gboolean
114
g_tls_backend_supports_dtls (GTlsBackend *backend)
115
0
{
116
0
  if (G_TLS_BACKEND_GET_INTERFACE (backend)->supports_dtls)
117
0
    return G_TLS_BACKEND_GET_INTERFACE (backend)->supports_dtls (backend);
118
119
0
  return FALSE;
120
0
}
121
122
/**
123
 * g_tls_backend_get_default_database:
124
 * @backend: the #GTlsBackend
125
 *
126
 * Gets the default #GTlsDatabase used to verify TLS connections.
127
 *
128
 * Returns: (transfer full): the default database, which should be
129
 *               unreffed when done.
130
 *
131
 * Since: 2.30
132
 */
133
GTlsDatabase *
134
g_tls_backend_get_default_database (GTlsBackend *backend)
135
0
{
136
0
  GTlsDatabase *db;
137
138
0
  g_return_val_if_fail (G_IS_TLS_BACKEND (backend), NULL);
139
140
  /* This method was added later, so accept the (remote) possibility it can be NULL */
141
0
  if (!G_TLS_BACKEND_GET_INTERFACE (backend)->get_default_database)
142
0
    return NULL;
143
144
0
  G_LOCK (default_database_lock);
145
146
0
  if (!default_database)
147
0
    default_database = G_TLS_BACKEND_GET_INTERFACE (backend)->get_default_database (backend);
148
0
  db = default_database ? g_object_ref (default_database) : NULL;
149
0
  G_UNLOCK (default_database_lock);
150
151
0
  return db;
152
0
}
153
154
/**
155
 * g_tls_backend_set_default_database:
156
 * @backend: the #GTlsBackend
157
 * @database: (nullable): the #GTlsDatabase
158
 *
159
 * Set the default #GTlsDatabase used to verify TLS connections
160
 *
161
 * Any subsequent call to g_tls_backend_get_default_database() will return
162
 * the database set in this call.  Existing databases and connections are not
163
 * modified.
164
 *
165
 * Setting a %NULL default database will reset to using the system default
166
 * database as if g_tls_backend_set_default_database() had never been called.
167
 *
168
 * Since: 2.60
169
 */
170
void
171
g_tls_backend_set_default_database (GTlsBackend  *backend,
172
                                    GTlsDatabase *database)
173
0
{
174
0
  g_return_if_fail (G_IS_TLS_BACKEND (backend));
175
0
  g_return_if_fail (database == NULL || G_IS_TLS_DATABASE (database));
176
177
0
  G_LOCK (default_database_lock);
178
0
  g_set_object (&default_database, database);
179
0
  G_UNLOCK (default_database_lock);
180
0
}
181
182
/**
183
 * g_tls_backend_get_certificate_type:
184
 * @backend: the #GTlsBackend
185
 *
186
 * Gets the #GType of @backend's #GTlsCertificate implementation.
187
 *
188
 * Returns: the #GType of @backend's #GTlsCertificate
189
 *   implementation.
190
 *
191
 * Since: 2.28
192
 */
193
GType
194
g_tls_backend_get_certificate_type (GTlsBackend *backend)
195
0
{
196
0
  return G_TLS_BACKEND_GET_INTERFACE (backend)->get_certificate_type ();
197
0
}
198
199
/**
200
 * g_tls_backend_get_client_connection_type:
201
 * @backend: the #GTlsBackend
202
 *
203
 * Gets the #GType of @backend's #GTlsClientConnection implementation.
204
 *
205
 * Returns: the #GType of @backend's #GTlsClientConnection
206
 *   implementation.
207
 *
208
 * Since: 2.28
209
 */
210
GType
211
g_tls_backend_get_client_connection_type (GTlsBackend *backend)
212
0
{
213
0
  return G_TLS_BACKEND_GET_INTERFACE (backend)->get_client_connection_type ();
214
0
}
215
216
/**
217
 * g_tls_backend_get_server_connection_type:
218
 * @backend: the #GTlsBackend
219
 *
220
 * Gets the #GType of @backend's #GTlsServerConnection implementation.
221
 *
222
 * Returns: the #GType of @backend's #GTlsServerConnection
223
 *   implementation.
224
 *
225
 * Since: 2.28
226
 */
227
GType
228
g_tls_backend_get_server_connection_type (GTlsBackend *backend)
229
0
{
230
0
  return G_TLS_BACKEND_GET_INTERFACE (backend)->get_server_connection_type ();
231
0
}
232
233
/**
234
 * g_tls_backend_get_dtls_client_connection_type:
235
 * @backend: the #GTlsBackend
236
 *
237
 * Gets the #GType of @backend’s #GDtlsClientConnection implementation.
238
 *
239
 * Returns: the #GType of @backend’s #GDtlsClientConnection
240
 *   implementation, or %G_TYPE_INVALID if this backend doesn’t support DTLS.
241
 *
242
 * Since: 2.48
243
 */
244
GType
245
g_tls_backend_get_dtls_client_connection_type (GTlsBackend *backend)
246
0
{
247
0
  GTlsBackendInterface *iface;
248
249
0
  g_return_val_if_fail (G_IS_TLS_BACKEND (backend), G_TYPE_INVALID);
250
251
0
  iface = G_TLS_BACKEND_GET_INTERFACE (backend);
252
0
  if (iface->get_dtls_client_connection_type == NULL)
253
0
    return G_TYPE_INVALID;
254
255
0
  return iface->get_dtls_client_connection_type ();
256
0
}
257
258
/**
259
 * g_tls_backend_get_dtls_server_connection_type:
260
 * @backend: the #GTlsBackend
261
 *
262
 * Gets the #GType of @backend’s #GDtlsServerConnection implementation.
263
 *
264
 * Returns: the #GType of @backend’s #GDtlsServerConnection
265
 *   implementation, or %G_TYPE_INVALID if this backend doesn’t support DTLS.
266
 *
267
 * Since: 2.48
268
 */
269
GType
270
g_tls_backend_get_dtls_server_connection_type (GTlsBackend *backend)
271
0
{
272
0
  GTlsBackendInterface *iface;
273
274
0
  g_return_val_if_fail (G_IS_TLS_BACKEND (backend), G_TYPE_INVALID);
275
276
0
  iface = G_TLS_BACKEND_GET_INTERFACE (backend);
277
0
  if (iface->get_dtls_server_connection_type == NULL)
278
0
    return G_TYPE_INVALID;
279
280
0
  return iface->get_dtls_server_connection_type ();
281
0
}
282
283
/**
284
 * g_tls_backend_get_file_database_type:
285
 * @backend: the #GTlsBackend
286
 *
287
 * Gets the #GType of @backend's #GTlsFileDatabase implementation.
288
 *
289
 * Returns: the #GType of backend's #GTlsFileDatabase implementation.
290
 *
291
 * Since: 2.30
292
 */
293
GType
294
g_tls_backend_get_file_database_type (GTlsBackend *backend)
295
0
{
296
0
  g_return_val_if_fail (G_IS_TLS_BACKEND (backend), 0);
297
298
  /* This method was added later, so accept the (remote) possibility it can be NULL */
299
0
  if (!G_TLS_BACKEND_GET_INTERFACE (backend)->get_file_database_type)
300
0
    return 0;
301
302
0
  return G_TLS_BACKEND_GET_INTERFACE (backend)->get_file_database_type ();
303
0
}