Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gdtlsclientconnection.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 "gdtlsclientconnection.h"
26
#include "ginitable.h"
27
#include "gioenumtypes.h"
28
#include "gsocket.h"
29
#include "gsocketconnectable.h"
30
#include "gtlsbackend.h"
31
#include "gtlscertificate.h"
32
#include "glibintl.h"
33
34
/**
35
 * SECTION:gdtlsclientconnection
36
 * @short_description: DTLS client-side connection
37
 * @include: gio/gio.h
38
 *
39
 * #GDtlsClientConnection is the client-side subclass of
40
 * #GDtlsConnection, representing a client-side DTLS connection.
41
 *
42
 * Since: 2.48
43
 */
44
45
/**
46
 * GDtlsClientConnection:
47
 *
48
 * Abstract base class for the backend-specific client connection
49
 * type.
50
 *
51
 * Since: 2.48
52
 */
53
54
G_DEFINE_INTERFACE (GDtlsClientConnection, g_dtls_client_connection,
55
                    G_TYPE_DTLS_CONNECTION)
56
57
static void
58
g_dtls_client_connection_default_init (GDtlsClientConnectionInterface *iface)
59
0
{
60
  /**
61
   * GDtlsClientConnection:validation-flags:
62
   *
63
   * What steps to perform when validating a certificate received from
64
   * a server. Server certificates that fail to validate in any of the
65
   * ways indicated here will be rejected unless the application
66
   * overrides the default via #GDtlsConnection::accept-certificate.
67
   *
68
   * GLib guarantees that if certificate verification fails, at least one
69
   * flag will be set, but it does not guarantee that all possible flags
70
   * will be set. Accordingly, you may not safely decide to ignore any
71
   * particular type of error. For example, it would be incorrect to mask
72
   * %G_TLS_CERTIFICATE_EXPIRED if you want to allow expired certificates,
73
   * because this could potentially be the only error flag set even if
74
   * other problems exist with the certificate. Therefore, there is no
75
   * safe way to use this property. This is not a horrible problem,
76
   * though, because you should not be attempting to ignore validation
77
   * errors anyway. If you really must ignore TLS certificate errors,
78
   * connect to #GDtlsConnection::accept-certificate.
79
   *
80
   * Since: 2.48
81
   *
82
   * Deprecated: 2.74: Do not attempt to ignore validation errors.
83
   */
84
0
  g_object_interface_install_property (iface,
85
0
                                       g_param_spec_flags ("validation-flags",
86
0
                                                           P_("Validation flags"),
87
0
                                                           P_("What certificate validation to perform"),
88
0
                                                           G_TYPE_TLS_CERTIFICATE_FLAGS,
89
0
                                                           G_TLS_CERTIFICATE_VALIDATE_ALL,
90
0
                                                           G_PARAM_READWRITE |
91
0
                                                           G_PARAM_CONSTRUCT |
92
0
                                                           G_PARAM_STATIC_STRINGS));
93
94
  /**
95
   * GDtlsClientConnection:server-identity:
96
   *
97
   * A #GSocketConnectable describing the identity of the server that
98
   * is expected on the other end of the connection.
99
   *
100
   * If the %G_TLS_CERTIFICATE_BAD_IDENTITY flag is set in
101
   * #GDtlsClientConnection:validation-flags, this object will be used
102
   * to determine the expected identify of the remote end of the
103
   * connection; if #GDtlsClientConnection:server-identity is not set,
104
   * or does not match the identity presented by the server, then the
105
   * %G_TLS_CERTIFICATE_BAD_IDENTITY validation will fail.
106
   *
107
   * In addition to its use in verifying the server certificate,
108
   * this is also used to give a hint to the server about what
109
   * certificate we expect, which is useful for servers that serve
110
   * virtual hosts.
111
   *
112
   * Since: 2.48
113
   */
114
0
  g_object_interface_install_property (iface,
115
0
                                       g_param_spec_object ("server-identity",
116
0
                                                            P_("Server identity"),
117
0
                                                            P_("GSocketConnectable identifying the server"),
118
0
                                                            G_TYPE_SOCKET_CONNECTABLE,
119
0
                                                            G_PARAM_READWRITE |
120
0
                                                            G_PARAM_CONSTRUCT |
121
0
                                                            G_PARAM_STATIC_STRINGS));
122
123
  /**
124
   * GDtlsClientConnection:accepted-cas: (type GLib.List) (element-type GLib.ByteArray)
125
   *
126
   * A list of the distinguished names of the Certificate Authorities
127
   * that the server will accept client certificates signed by. If the
128
   * server requests a client certificate during the handshake, then
129
   * this property will be set after the handshake completes.
130
   *
131
   * Each item in the list is a #GByteArray which contains the complete
132
   * subject DN of the certificate authority.
133
   *
134
   * Since: 2.48
135
   */
136
0
  g_object_interface_install_property (iface,
137
0
                                       g_param_spec_pointer ("accepted-cas",
138
0
                                                             P_("Accepted CAs"),
139
0
                                                             P_("Distinguished names of the CAs the server accepts certificates from"),
140
0
                                                             G_PARAM_READABLE |
141
0
                                                             G_PARAM_STATIC_STRINGS));
142
0
}
143
144
/**
145
 * g_dtls_client_connection_new:
146
 * @base_socket: the #GDatagramBased to wrap
147
 * @server_identity: (nullable): the expected identity of the server
148
 * @error: #GError for error reporting, or %NULL to ignore.
149
 *
150
 * Creates a new #GDtlsClientConnection wrapping @base_socket which is
151
 * assumed to communicate with the server identified by @server_identity.
152
 *
153
 * Returns: (transfer full) (type GDtlsClientConnection): the new
154
 *   #GDtlsClientConnection, or %NULL on error
155
 *
156
 * Since: 2.48
157
 */
158
GDatagramBased *
159
g_dtls_client_connection_new (GDatagramBased      *base_socket,
160
                              GSocketConnectable  *server_identity,
161
                              GError             **error)
162
0
{
163
0
  GObject *conn;
164
0
  GTlsBackend *backend;
165
166
0
  backend = g_tls_backend_get_default ();
167
0
  conn = g_initable_new (g_tls_backend_get_dtls_client_connection_type (backend),
168
0
                         NULL, error,
169
0
                         "base-socket", base_socket,
170
0
                         "server-identity", server_identity,
171
0
                         NULL);
172
0
  return G_DATAGRAM_BASED (conn);
173
0
}
174
175
/**
176
 * g_dtls_client_connection_get_validation_flags:
177
 * @conn: the #GDtlsClientConnection
178
 *
179
 * Gets @conn's validation flags
180
 *
181
 * This function does not work as originally designed and is impossible
182
 * to use correctly. See #GDtlsClientConnection:validation-flags for more
183
 * information.
184
 *
185
 * Returns: the validation flags
186
 *
187
 * Since: 2.48
188
 *
189
 * Deprecated: 2.74: Do not attempt to ignore validation errors.
190
 */
191
GTlsCertificateFlags
192
g_dtls_client_connection_get_validation_flags (GDtlsClientConnection *conn)
193
0
{
194
0
  GTlsCertificateFlags flags = G_TLS_CERTIFICATE_NO_FLAGS;
195
196
0
  g_return_val_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn), 0);
197
198
0
  g_object_get (G_OBJECT (conn), "validation-flags", &flags, NULL);
199
0
  return flags;
200
0
}
201
202
/**
203
 * g_dtls_client_connection_set_validation_flags:
204
 * @conn: the #GDtlsClientConnection
205
 * @flags: the #GTlsCertificateFlags to use
206
 *
207
 * Sets @conn's validation flags, to override the default set of
208
 * checks performed when validating a server certificate. By default,
209
 * %G_TLS_CERTIFICATE_VALIDATE_ALL is used.
210
 *
211
 * This function does not work as originally designed and is impossible
212
 * to use correctly. See #GDtlsClientConnection:validation-flags for more
213
 * information.
214
 *
215
 * Since: 2.48
216
 *
217
 * Deprecated: 2.74: Do not attempt to ignore validation errors.
218
 */
219
void
220
g_dtls_client_connection_set_validation_flags (GDtlsClientConnection  *conn,
221
                                               GTlsCertificateFlags   flags)
222
0
{
223
0
  g_return_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn));
224
225
0
  g_object_set (G_OBJECT (conn), "validation-flags", flags, NULL);
226
0
}
227
228
/**
229
 * g_dtls_client_connection_get_server_identity:
230
 * @conn: the #GDtlsClientConnection
231
 *
232
 * Gets @conn's expected server identity
233
 *
234
 * Returns: (transfer none): a #GSocketConnectable describing the
235
 * expected server identity, or %NULL if the expected identity is not
236
 * known.
237
 *
238
 * Since: 2.48
239
 */
240
GSocketConnectable *
241
g_dtls_client_connection_get_server_identity (GDtlsClientConnection *conn)
242
0
{
243
0
  GSocketConnectable *identity = NULL;
244
245
0
  g_return_val_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn), 0);
246
247
0
  g_object_get (G_OBJECT (conn), "server-identity", &identity, NULL);
248
0
  if (identity)
249
0
    g_object_unref (identity);
250
0
  return identity;
251
0
}
252
253
/**
254
 * g_dtls_client_connection_set_server_identity:
255
 * @conn: the #GDtlsClientConnection
256
 * @identity: a #GSocketConnectable describing the expected server identity
257
 *
258
 * Sets @conn's expected server identity, which is used both to tell
259
 * servers on virtual hosts which certificate to present, and also
260
 * to let @conn know what name to look for in the certificate when
261
 * performing %G_TLS_CERTIFICATE_BAD_IDENTITY validation, if enabled.
262
 *
263
 * Since: 2.48
264
 */
265
void
266
g_dtls_client_connection_set_server_identity (GDtlsClientConnection *conn,
267
                                              GSocketConnectable    *identity)
268
0
{
269
0
  g_return_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn));
270
271
0
  g_object_set (G_OBJECT (conn), "server-identity", identity, NULL);
272
0
}
273
274
/**
275
 * g_dtls_client_connection_get_accepted_cas:
276
 * @conn: the #GDtlsClientConnection
277
 *
278
 * Gets the list of distinguished names of the Certificate Authorities
279
 * that the server will accept certificates from. This will be set
280
 * during the TLS handshake if the server requests a certificate.
281
 * Otherwise, it will be %NULL.
282
 *
283
 * Each item in the list is a #GByteArray which contains the complete
284
 * subject DN of the certificate authority.
285
 *
286
 * Returns: (element-type GByteArray) (transfer full): the list of
287
 * CA DNs. You should unref each element with g_byte_array_unref() and then
288
 * the free the list with g_list_free().
289
 *
290
 * Since: 2.48
291
 */
292
GList *
293
g_dtls_client_connection_get_accepted_cas (GDtlsClientConnection *conn)
294
0
{
295
0
  GList *accepted_cas = NULL;
296
297
0
  g_return_val_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn), NULL);
298
299
0
  g_object_get (G_OBJECT (conn), "accepted-cas", &accepted_cas, NULL);
300
0
  return accepted_cas;
301
0
}