Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gtlsconnection.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
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "config.h"
22
#include "glib.h"
23
24
#include "gtlsconnection.h"
25
#include "gcancellable.h"
26
#include "gioenumtypes.h"
27
#include "gsocket.h"
28
#include "gtlsbackend.h"
29
#include "gtlscertificate.h"
30
#include "gtlsclientconnection.h"
31
#include "gtlsdatabase.h"
32
#include "gtlsinteraction.h"
33
#include "glibintl.h"
34
#include "gmarshal-internal.h"
35
36
/**
37
 * SECTION:gtlsconnection
38
 * @short_description: TLS connection type
39
 * @include: gio/gio.h
40
 *
41
 * #GTlsConnection is the base TLS connection class type, which wraps
42
 * a #GIOStream and provides TLS encryption on top of it. Its
43
 * subclasses, #GTlsClientConnection and #GTlsServerConnection,
44
 * implement client-side and server-side TLS, respectively.
45
 *
46
 * For DTLS (Datagram TLS) support, see #GDtlsConnection.
47
 *
48
 * Since: 2.28
49
 */
50
51
/**
52
 * GTlsConnection:
53
 *
54
 * Abstract base class for the backend-specific #GTlsClientConnection
55
 * and #GTlsServerConnection types.
56
 *
57
 * Since: 2.28
58
 */
59
60
G_DEFINE_ABSTRACT_TYPE (GTlsConnection, g_tls_connection, G_TYPE_IO_STREAM)
61
62
static void g_tls_connection_get_property (GObject    *object,
63
             guint       prop_id,
64
             GValue     *value,
65
             GParamSpec *pspec);
66
static void g_tls_connection_set_property (GObject      *object,
67
             guint         prop_id,
68
             const GValue *value,
69
             GParamSpec   *pspec);
70
71
enum {
72
  ACCEPT_CERTIFICATE,
73
74
  LAST_SIGNAL
75
};
76
77
static guint signals[LAST_SIGNAL] = { 0 };
78
79
enum {
80
  PROP_0,
81
  PROP_BASE_IO_STREAM,
82
  PROP_REQUIRE_CLOSE_NOTIFY,
83
  PROP_REHANDSHAKE_MODE,
84
  PROP_USE_SYSTEM_CERTDB,
85
  PROP_DATABASE,
86
  PROP_INTERACTION,
87
  PROP_CERTIFICATE,
88
  PROP_PEER_CERTIFICATE,
89
  PROP_PEER_CERTIFICATE_ERRORS,
90
  PROP_ADVERTISED_PROTOCOLS,
91
  PROP_NEGOTIATED_PROTOCOL,
92
  PROP_PROTOCOL_VERSION,
93
  PROP_CIPHERSUITE_NAME,
94
};
95
96
static void
97
g_tls_connection_class_init (GTlsConnectionClass *klass)
98
0
{
99
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100
101
0
  gobject_class->get_property = g_tls_connection_get_property;
102
0
  gobject_class->set_property = g_tls_connection_set_property;
103
104
  /**
105
   * GTlsConnection:base-io-stream:
106
   *
107
   * The #GIOStream that the connection wraps. The connection holds a reference
108
   * to this stream, and may run operations on the stream from other threads
109
   * throughout its lifetime. Consequently, after the #GIOStream has been
110
   * constructed, application code may only run its own operations on this
111
   * stream when no #GIOStream operations are running.
112
   *
113
   * Since: 2.28
114
   */
115
0
  g_object_class_install_property (gobject_class, PROP_BASE_IO_STREAM,
116
0
           g_param_spec_object ("base-io-stream",
117
0
              P_("Base IOStream"),
118
0
              P_("The GIOStream that the connection wraps"),
119
0
              G_TYPE_IO_STREAM,
120
0
              G_PARAM_READWRITE |
121
0
              G_PARAM_CONSTRUCT_ONLY |
122
0
              G_PARAM_STATIC_STRINGS));
123
  /**
124
   * GTlsConnection:use-system-certdb:
125
   *
126
   * Whether or not the system certificate database will be used to
127
   * verify peer certificates. See
128
   * g_tls_connection_set_use_system_certdb().
129
   *
130
   * Deprecated: 2.30: Use GTlsConnection:database instead
131
   */
132
0
  g_object_class_install_property (gobject_class, PROP_USE_SYSTEM_CERTDB,
133
0
           g_param_spec_boolean ("use-system-certdb",
134
0
               P_("Use system certificate database"),
135
0
               P_("Whether to verify peer certificates against the system certificate database"),
136
0
               TRUE,
137
0
               G_PARAM_READWRITE |
138
0
               G_PARAM_CONSTRUCT |
139
0
               G_PARAM_STATIC_STRINGS |
140
0
               G_PARAM_DEPRECATED));
141
  /**
142
   * GTlsConnection:database: (nullable)
143
   *
144
   * The certificate database to use when verifying this TLS connection.
145
   * If no certificate database is set, then the default database will be
146
   * used. See g_tls_backend_get_default_database().
147
   *
148
   * When using a non-default database, #GTlsConnection must fall back to using
149
   * the #GTlsDatabase to perform certificate verification using
150
   * g_tls_database_verify_chain(), which means certificate verification will
151
   * not be able to make use of TLS session context. This may be less secure.
152
   * For example, if you create your own #GTlsDatabase that just wraps the
153
   * default #GTlsDatabase, you might expect that you have not changed anything,
154
   * but this is not true because you may have altered the behavior of
155
   * #GTlsConnection by causing it to use g_tls_database_verify_chain(). See the
156
   * documentation of g_tls_database_verify_chain() for more details on specific
157
   * security checks that may not be performed. Accordingly, setting a
158
   * non-default database is discouraged except for specialty applications with
159
   * unusual security requirements.
160
   *
161
   * Since: 2.30
162
   */
163
0
  g_object_class_install_property (gobject_class, PROP_DATABASE,
164
0
           g_param_spec_object ("database",
165
0
               P_("Database"),
166
0
               P_("Certificate database to use for looking up or verifying certificates"),
167
0
               G_TYPE_TLS_DATABASE,
168
0
               G_PARAM_READWRITE |
169
0
               G_PARAM_STATIC_STRINGS));
170
  /**
171
   * GTlsConnection:interaction: (nullable)
172
   *
173
   * A #GTlsInteraction object to be used when the connection or certificate
174
   * database need to interact with the user. This will be used to prompt the
175
   * user for passwords where necessary.
176
   *
177
   * Since: 2.30
178
   */
179
0
  g_object_class_install_property (gobject_class, PROP_INTERACTION,
180
0
                                   g_param_spec_object ("interaction",
181
0
                                                        P_("Interaction"),
182
0
                                                        P_("Optional object for user interaction"),
183
0
                                                        G_TYPE_TLS_INTERACTION,
184
0
                                                        G_PARAM_READWRITE |
185
0
                                                        G_PARAM_STATIC_STRINGS));
186
  /**
187
   * GTlsConnection:require-close-notify:
188
   *
189
   * Whether or not proper TLS close notification is required.
190
   * See g_tls_connection_set_require_close_notify().
191
   *
192
   * Since: 2.28
193
   */
194
0
  g_object_class_install_property (gobject_class, PROP_REQUIRE_CLOSE_NOTIFY,
195
0
           g_param_spec_boolean ("require-close-notify",
196
0
               P_("Require close notify"),
197
0
               P_("Whether to require proper TLS close notification"),
198
0
               TRUE,
199
0
               G_PARAM_READWRITE |
200
0
               G_PARAM_CONSTRUCT |
201
0
               G_PARAM_STATIC_STRINGS));
202
  /**
203
   * GTlsConnection:rehandshake-mode:
204
   *
205
   * The rehandshaking mode. See
206
   * g_tls_connection_set_rehandshake_mode().
207
   *
208
   * Since: 2.28
209
   *
210
   * Deprecated: 2.60: The rehandshake mode is ignored.
211
   */
212
0
  g_object_class_install_property (gobject_class, PROP_REHANDSHAKE_MODE,
213
0
           g_param_spec_enum ("rehandshake-mode",
214
0
                  P_("Rehandshake mode"),
215
0
                  P_("When to allow rehandshaking"),
216
0
                  G_TYPE_TLS_REHANDSHAKE_MODE,
217
0
                  G_TLS_REHANDSHAKE_SAFELY,
218
0
                  G_PARAM_READWRITE |
219
0
                  G_PARAM_CONSTRUCT |
220
0
                  G_PARAM_STATIC_STRINGS |
221
0
                  G_PARAM_DEPRECATED));
222
  /**
223
   * GTlsConnection:certificate:
224
   *
225
   * The connection's certificate; see
226
   * g_tls_connection_set_certificate().
227
   *
228
   * Since: 2.28
229
   */
230
0
  g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
231
0
           g_param_spec_object ("certificate",
232
0
              P_("Certificate"),
233
0
              P_("The connection’s certificate"),
234
0
              G_TYPE_TLS_CERTIFICATE,
235
0
              G_PARAM_READWRITE |
236
0
              G_PARAM_STATIC_STRINGS));
237
  /**
238
   * GTlsConnection:peer-certificate: (nullable)
239
   *
240
   * The connection's peer's certificate, after the TLS handshake has
241
   * completed or failed. Note in particular that this is not yet set
242
   * during the emission of #GTlsConnection::accept-certificate.
243
   *
244
   * (You can watch for a #GObject::notify signal on this property to
245
   * detect when a handshake has occurred.)
246
   *
247
   * Since: 2.28
248
   */
249
0
  g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE,
250
0
           g_param_spec_object ("peer-certificate",
251
0
              P_("Peer Certificate"),
252
0
              P_("The connection’s peer’s certificate"),
253
0
              G_TYPE_TLS_CERTIFICATE,
254
0
              G_PARAM_READABLE |
255
0
              G_PARAM_STATIC_STRINGS));
256
  /**
257
   * GTlsConnection:peer-certificate-errors:
258
   *
259
   * The errors noticed while verifying
260
   * #GTlsConnection:peer-certificate. Normally this should be 0, but
261
   * it may not be if #GTlsClientConnection:validation-flags is not
262
   * %G_TLS_CERTIFICATE_VALIDATE_ALL, or if
263
   * #GTlsConnection::accept-certificate overrode the default
264
   * behavior.
265
   *
266
   * GLib guarantees that if certificate verification fails, at least
267
   * one error will be set, but it does not guarantee that all possible
268
   * errors will be set. Accordingly, you may not safely decide to
269
   * ignore any particular type of error. For example, it would be
270
   * incorrect to mask %G_TLS_CERTIFICATE_EXPIRED if you want to allow
271
   * expired certificates, because this could potentially be the only
272
   * error flag set even if other problems exist with the certificate.
273
   *
274
   * Since: 2.28
275
   */
276
0
  g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE_ERRORS,
277
0
           g_param_spec_flags ("peer-certificate-errors",
278
0
                   P_("Peer Certificate Errors"),
279
0
                   P_("Errors found with the peer’s certificate"),
280
0
                   G_TYPE_TLS_CERTIFICATE_FLAGS,
281
0
                   0,
282
0
                   G_PARAM_READABLE |
283
0
                   G_PARAM_STATIC_STRINGS));
284
  /**
285
   * GTlsConnection:advertised-protocols: (nullable)
286
   *
287
   * The list of application-layer protocols that the connection
288
   * advertises that it is willing to speak. See
289
   * g_tls_connection_set_advertised_protocols().
290
   *
291
   * Since: 2.60
292
   */
293
0
  g_object_class_install_property (gobject_class, PROP_ADVERTISED_PROTOCOLS,
294
0
                                   g_param_spec_boxed ("advertised-protocols",
295
0
                                                       P_("Advertised Protocols"),
296
0
                                                       P_("Application-layer protocols available on this connection"),
297
0
                                                       G_TYPE_STRV,
298
0
                                                       G_PARAM_READWRITE |
299
0
                                                       G_PARAM_STATIC_STRINGS));
300
  /**
301
   * GTlsConnection:negotiated-protocol:
302
   *
303
   * The application-layer protocol negotiated during the TLS
304
   * handshake. See g_tls_connection_get_negotiated_protocol().
305
   *
306
   * Since: 2.60
307
   */
308
0
  g_object_class_install_property (gobject_class, PROP_NEGOTIATED_PROTOCOL,
309
0
                                   g_param_spec_string ("negotiated-protocol",
310
0
                                                        P_("Negotiated Protocol"),
311
0
                                                        P_("Application-layer protocol negotiated for this connection"),
312
0
                                                        NULL,
313
0
                                                        G_PARAM_READABLE |
314
0
                                                        G_PARAM_STATIC_STRINGS));
315
316
  /**
317
   * GTlsConnection:protocol-version:
318
   *
319
   * The TLS protocol version in use. See g_tls_connection_get_protocol_version().
320
   *
321
   * Since: 2.70
322
   */
323
0
  g_object_class_install_property (gobject_class, PROP_PROTOCOL_VERSION,
324
0
                                   g_param_spec_enum ("protocol-version",
325
0
                                                      P_("Protocol Version"),
326
0
                                                      P_("TLS protocol version negotiated for this connection"),
327
0
                                                      G_TYPE_TLS_PROTOCOL_VERSION,
328
0
                                                      G_TLS_PROTOCOL_VERSION_UNKNOWN,
329
0
                                                      G_PARAM_READABLE |
330
0
                                                      G_PARAM_STATIC_STRINGS));
331
332
  /**
333
   * GTlsConnection:ciphersuite-name: (nullable)
334
   *
335
   * The name of the TLS ciphersuite in use. See g_tls_connection_get_ciphersuite_name().
336
   *
337
   * Since: 2.70
338
   */
339
0
  g_object_class_install_property (gobject_class, PROP_CIPHERSUITE_NAME,
340
0
                                   g_param_spec_string ("ciphersuite-name",
341
0
                                                        P_("Ciphersuite Name"),
342
0
                                                        P_("Name of ciphersuite negotiated for this connection"),
343
0
                                                        NULL,
344
0
                                                        G_PARAM_READABLE |
345
0
                                                        G_PARAM_STATIC_STRINGS));
346
347
  /**
348
   * GTlsConnection::accept-certificate:
349
   * @conn: a #GTlsConnection
350
   * @peer_cert: the peer's #GTlsCertificate
351
   * @errors: the problems with @peer_cert.
352
   *
353
   * Emitted during the TLS handshake after the peer certificate has
354
   * been received. You can examine @peer_cert's certification path by
355
   * calling g_tls_certificate_get_issuer() on it.
356
   *
357
   * For a client-side connection, @peer_cert is the server's
358
   * certificate, and the signal will only be emitted if the
359
   * certificate was not acceptable according to @conn's
360
   * #GTlsClientConnection:validation_flags. If you would like the
361
   * certificate to be accepted despite @errors, return %TRUE from the
362
   * signal handler. Otherwise, if no handler accepts the certificate,
363
   * the handshake will fail with %G_TLS_ERROR_BAD_CERTIFICATE.
364
   *
365
   * GLib guarantees that if certificate verification fails, this signal
366
   * will be emitted with at least one error will be set in @errors, but
367
   * it does not guarantee that all possible errors will be set.
368
   * Accordingly, you may not safely decide to ignore any particular
369
   * type of error. For example, it would be incorrect to ignore
370
   * %G_TLS_CERTIFICATE_EXPIRED if you want to allow expired
371
   * certificates, because this could potentially be the only error flag
372
   * set even if other problems exist with the certificate.
373
   *
374
   * For a server-side connection, @peer_cert is the certificate
375
   * presented by the client, if this was requested via the server's
376
   * #GTlsServerConnection:authentication_mode. On the server side,
377
   * the signal is always emitted when the client presents a
378
   * certificate, and the certificate will only be accepted if a
379
   * handler returns %TRUE.
380
   *
381
   * Note that if this signal is emitted as part of asynchronous I/O
382
   * in the main thread, then you should not attempt to interact with
383
   * the user before returning from the signal handler. If you want to
384
   * let the user decide whether or not to accept the certificate, you
385
   * would have to return %FALSE from the signal handler on the first
386
   * attempt, and then after the connection attempt returns a
387
   * %G_TLS_ERROR_BAD_CERTIFICATE, you can interact with the user, and
388
   * if the user decides to accept the certificate, remember that fact,
389
   * create a new connection, and return %TRUE from the signal handler
390
   * the next time.
391
   *
392
   * If you are doing I/O in another thread, you do not
393
   * need to worry about this, and can simply block in the signal
394
   * handler until the UI thread returns an answer.
395
   *
396
   * Returns: %TRUE to accept @peer_cert (which will also
397
   * immediately end the signal emission). %FALSE to allow the signal
398
   * emission to continue, which will cause the handshake to fail if
399
   * no one else overrides it.
400
   *
401
   * Since: 2.28
402
   */
403
0
  signals[ACCEPT_CERTIFICATE] =
404
0
    g_signal_new (I_("accept-certificate"),
405
0
      G_TYPE_TLS_CONNECTION,
406
0
      G_SIGNAL_RUN_LAST,
407
0
      G_STRUCT_OFFSET (GTlsConnectionClass, accept_certificate),
408
0
      g_signal_accumulator_true_handled, NULL,
409
0
      _g_cclosure_marshal_BOOLEAN__OBJECT_FLAGS,
410
0
      G_TYPE_BOOLEAN, 2,
411
0
      G_TYPE_TLS_CERTIFICATE,
412
0
      G_TYPE_TLS_CERTIFICATE_FLAGS);
413
0
  g_signal_set_va_marshaller (signals[ACCEPT_CERTIFICATE],
414
0
                              G_TYPE_FROM_CLASS (klass),
415
0
                              _g_cclosure_marshal_BOOLEAN__OBJECT_FLAGSv);
416
0
}
417
418
static void
419
g_tls_connection_init (GTlsConnection *conn)
420
0
{
421
0
}
422
423
static void
424
g_tls_connection_get_property (GObject    *object,
425
             guint       prop_id,
426
             GValue     *value,
427
             GParamSpec *pspec)
428
0
{
429
0
  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
430
0
}
431
432
static void
433
g_tls_connection_set_property (GObject      *object,
434
             guint         prop_id,
435
             const GValue *value,
436
             GParamSpec   *pspec)
437
0
{
438
0
  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
439
0
}
440
441
/**
442
 * g_tls_connection_set_use_system_certdb:
443
 * @conn: a #GTlsConnection
444
 * @use_system_certdb: whether to use the system certificate database
445
 *
446
 * Sets whether @conn uses the system certificate database to verify
447
 * peer certificates. This is %TRUE by default. If set to %FALSE, then
448
 * peer certificate validation will always set the
449
 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
450
 * #GTlsConnection::accept-certificate will always be emitted on
451
 * client-side connections, unless that bit is not set in
452
 * #GTlsClientConnection:validation-flags).
453
 *
454
 * Deprecated: 2.30: Use g_tls_connection_set_database() instead
455
 */
456
void
457
g_tls_connection_set_use_system_certdb (GTlsConnection *conn,
458
          gboolean        use_system_certdb)
459
0
{
460
0
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
461
462
0
  g_object_set (G_OBJECT (conn),
463
0
    "use-system-certdb", use_system_certdb,
464
0
    NULL);
465
0
}
466
467
/**
468
 * g_tls_connection_get_use_system_certdb:
469
 * @conn: a #GTlsConnection
470
 *
471
 * Gets whether @conn uses the system certificate database to verify
472
 * peer certificates. See g_tls_connection_set_use_system_certdb().
473
 *
474
 * Returns: whether @conn uses the system certificate database
475
 *
476
 * Deprecated: 2.30: Use g_tls_connection_get_database() instead
477
 */
478
gboolean
479
g_tls_connection_get_use_system_certdb (GTlsConnection *conn)
480
0
{
481
0
  gboolean use_system_certdb;
482
483
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
484
485
0
  g_object_get (G_OBJECT (conn),
486
0
    "use-system-certdb", &use_system_certdb,
487
0
    NULL);
488
0
  return use_system_certdb;
489
0
}
490
491
/**
492
 * g_tls_connection_set_database:
493
 * @conn: a #GTlsConnection
494
 * @database: (nullable): a #GTlsDatabase
495
 *
496
 * Sets the certificate database that is used to verify peer certificates.
497
 * This is set to the default database by default. See
498
 * g_tls_backend_get_default_database(). If set to %NULL, then
499
 * peer certificate validation will always set the
500
 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
501
 * #GTlsConnection::accept-certificate will always be emitted on
502
 * client-side connections, unless that bit is not set in
503
 * #GTlsClientConnection:validation-flags).
504
 *
505
 * There are nonintuitive security implications when using a non-default
506
 * database. See #GTlsConnection:database for details.
507
 *
508
 * Since: 2.30
509
 */
510
void
511
g_tls_connection_set_database (GTlsConnection *conn,
512
                               GTlsDatabase   *database)
513
0
{
514
0
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
515
0
  g_return_if_fail (database == NULL || G_IS_TLS_DATABASE (database));
516
517
0
  g_object_set (G_OBJECT (conn),
518
0
    "database", database,
519
0
    NULL);
520
0
}
521
522
/**
523
 * g_tls_connection_get_database:
524
 * @conn: a #GTlsConnection
525
 *
526
 * Gets the certificate database that @conn uses to verify
527
 * peer certificates. See g_tls_connection_set_database().
528
 *
529
 * Returns: (transfer none) (nullable): the certificate database that @conn uses or %NULL
530
 *
531
 * Since: 2.30
532
 */
533
GTlsDatabase*
534
g_tls_connection_get_database (GTlsConnection *conn)
535
0
{
536
0
  GTlsDatabase *database = NULL;
537
538
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
539
540
0
  g_object_get (G_OBJECT (conn),
541
0
    "database", &database,
542
0
    NULL);
543
0
  if (database)
544
0
    g_object_unref (database);
545
0
  return database;
546
0
}
547
548
/**
549
 * g_tls_connection_set_certificate:
550
 * @conn: a #GTlsConnection
551
 * @certificate: the certificate to use for @conn
552
 *
553
 * This sets the certificate that @conn will present to its peer
554
 * during the TLS handshake. For a #GTlsServerConnection, it is
555
 * mandatory to set this, and that will normally be done at construct
556
 * time.
557
 *
558
 * For a #GTlsClientConnection, this is optional. If a handshake fails
559
 * with %G_TLS_ERROR_CERTIFICATE_REQUIRED, that means that the server
560
 * requires a certificate, and if you try connecting again, you should
561
 * call this method first. You can call
562
 * g_tls_client_connection_get_accepted_cas() on the failed connection
563
 * to get a list of Certificate Authorities that the server will
564
 * accept certificates from.
565
 *
566
 * (It is also possible that a server will allow the connection with
567
 * or without a certificate; in that case, if you don't provide a
568
 * certificate, you can tell that the server requested one by the fact
569
 * that g_tls_client_connection_get_accepted_cas() will return
570
 * non-%NULL.)
571
 *
572
 * Since: 2.28
573
 */
574
void
575
g_tls_connection_set_certificate (GTlsConnection  *conn,
576
          GTlsCertificate *certificate)
577
0
{
578
0
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
579
0
  g_return_if_fail (G_IS_TLS_CERTIFICATE (certificate));
580
581
0
  g_object_set (G_OBJECT (conn), "certificate", certificate, NULL);
582
0
}
583
584
/**
585
 * g_tls_connection_get_certificate:
586
 * @conn: a #GTlsConnection
587
 *
588
 * Gets @conn's certificate, as set by
589
 * g_tls_connection_set_certificate().
590
 *
591
 * Returns: (transfer none) (nullable): @conn's certificate, or %NULL
592
 *
593
 * Since: 2.28
594
 */
595
GTlsCertificate *
596
g_tls_connection_get_certificate (GTlsConnection *conn)
597
0
{
598
0
  GTlsCertificate *certificate;
599
600
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
601
602
0
  g_object_get (G_OBJECT (conn), "certificate", &certificate, NULL);
603
0
  if (certificate)
604
0
    g_object_unref (certificate);
605
606
0
  return certificate;
607
0
}
608
609
/**
610
 * g_tls_connection_set_interaction:
611
 * @conn: a connection
612
 * @interaction: (nullable): an interaction object, or %NULL
613
 *
614
 * Set the object that will be used to interact with the user. It will be used
615
 * for things like prompting the user for passwords.
616
 *
617
 * The @interaction argument will normally be a derived subclass of
618
 * #GTlsInteraction. %NULL can also be provided if no user interaction
619
 * should occur for this connection.
620
 *
621
 * Since: 2.30
622
 */
623
void
624
g_tls_connection_set_interaction (GTlsConnection       *conn,
625
                                  GTlsInteraction      *interaction)
626
0
{
627
0
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
628
0
  g_return_if_fail (interaction == NULL || G_IS_TLS_INTERACTION (interaction));
629
630
0
  g_object_set (G_OBJECT (conn), "interaction", interaction, NULL);
631
0
}
632
633
/**
634
 * g_tls_connection_get_interaction:
635
 * @conn: a connection
636
 *
637
 * Get the object that will be used to interact with the user. It will be used
638
 * for things like prompting the user for passwords. If %NULL is returned, then
639
 * no user interaction will occur for this connection.
640
 *
641
 * Returns: (transfer none) (nullable): The interaction object.
642
 *
643
 * Since: 2.30
644
 */
645
GTlsInteraction *
646
g_tls_connection_get_interaction (GTlsConnection       *conn)
647
0
{
648
0
  GTlsInteraction *interaction = NULL;
649
650
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
651
652
0
  g_object_get (G_OBJECT (conn), "interaction", &interaction, NULL);
653
0
  if (interaction)
654
0
    g_object_unref (interaction);
655
656
0
  return interaction;
657
0
}
658
659
/**
660
 * g_tls_connection_get_peer_certificate:
661
 * @conn: a #GTlsConnection
662
 *
663
 * Gets @conn's peer's certificate after the handshake has completed
664
 * or failed. (It is not set during the emission of
665
 * #GTlsConnection::accept-certificate.)
666
 *
667
 * Returns: (transfer none) (nullable): @conn's peer's certificate, or %NULL
668
 *
669
 * Since: 2.28
670
 */
671
GTlsCertificate *
672
g_tls_connection_get_peer_certificate (GTlsConnection *conn)
673
0
{
674
0
  GTlsCertificate *peer_certificate;
675
676
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
677
678
0
  g_object_get (G_OBJECT (conn), "peer-certificate", &peer_certificate, NULL);
679
0
  if (peer_certificate)
680
0
    g_object_unref (peer_certificate);
681
682
0
  return peer_certificate;
683
0
}
684
685
/**
686
 * g_tls_connection_get_peer_certificate_errors:
687
 * @conn: a #GTlsConnection
688
 *
689
 * Gets the errors associated with validating @conn's peer's
690
 * certificate, after the handshake has completed or failed. (It is
691
 * not set during the emission of #GTlsConnection::accept-certificate.)
692
 *
693
 * See #GTlsConnection:peer-certificate-errors for more information.
694
 *
695
 * Returns: @conn's peer's certificate errors
696
 *
697
 * Since: 2.28
698
 */
699
GTlsCertificateFlags
700
g_tls_connection_get_peer_certificate_errors (GTlsConnection *conn)
701
0
{
702
0
  GTlsCertificateFlags errors;
703
704
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), 0);
705
706
0
  g_object_get (G_OBJECT (conn), "peer-certificate-errors", &errors, NULL);
707
0
  return errors;
708
0
}
709
710
/**
711
 * g_tls_connection_set_require_close_notify:
712
 * @conn: a #GTlsConnection
713
 * @require_close_notify: whether or not to require close notification
714
 *
715
 * Sets whether or not @conn expects a proper TLS close notification
716
 * before the connection is closed. If this is %TRUE (the default),
717
 * then @conn will expect to receive a TLS close notification from its
718
 * peer before the connection is closed, and will return a
719
 * %G_TLS_ERROR_EOF error if the connection is closed without proper
720
 * notification (since this may indicate a network error, or
721
 * man-in-the-middle attack).
722
 *
723
 * In some protocols, the application will know whether or not the
724
 * connection was closed cleanly based on application-level data
725
 * (because the application-level data includes a length field, or is
726
 * somehow self-delimiting); in this case, the close notify is
727
 * redundant and sometimes omitted. (TLS 1.1 explicitly allows this;
728
 * in TLS 1.0 it is technically an error, but often done anyway.) You
729
 * can use g_tls_connection_set_require_close_notify() to tell @conn
730
 * to allow an "unannounced" connection close, in which case the close
731
 * will show up as a 0-length read, as in a non-TLS
732
 * #GSocketConnection, and it is up to the application to check that
733
 * the data has been fully received.
734
 *
735
 * Note that this only affects the behavior when the peer closes the
736
 * connection; when the application calls g_io_stream_close() itself
737
 * on @conn, this will send a close notification regardless of the
738
 * setting of this property. If you explicitly want to do an unclean
739
 * close, you can close @conn's #GTlsConnection:base-io-stream rather
740
 * than closing @conn itself, but note that this may only be done when no other
741
 * operations are pending on @conn or the base I/O stream.
742
 *
743
 * Since: 2.28
744
 */
745
void
746
g_tls_connection_set_require_close_notify (GTlsConnection *conn,
747
             gboolean        require_close_notify)
748
0
{
749
0
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
750
751
0
  g_object_set (G_OBJECT (conn),
752
0
    "require-close-notify", require_close_notify,
753
0
    NULL);
754
0
}
755
756
/**
757
 * g_tls_connection_get_require_close_notify:
758
 * @conn: a #GTlsConnection
759
 *
760
 * Tests whether or not @conn expects a proper TLS close notification
761
 * when the connection is closed. See
762
 * g_tls_connection_set_require_close_notify() for details.
763
 *
764
 * Returns: %TRUE if @conn requires a proper TLS close
765
 * notification.
766
 *
767
 * Since: 2.28
768
 */
769
gboolean
770
g_tls_connection_get_require_close_notify (GTlsConnection *conn)
771
0
{
772
0
  gboolean require_close_notify;
773
774
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
775
776
0
  g_object_get (G_OBJECT (conn),
777
0
    "require-close-notify", &require_close_notify,
778
0
    NULL);
779
0
  return require_close_notify;
780
0
}
781
782
/**
783
 * g_tls_connection_set_rehandshake_mode:
784
 * @conn: a #GTlsConnection
785
 * @mode: the rehandshaking mode
786
 *
787
 * Since GLib 2.64, changing the rehandshake mode is no longer supported
788
 * and will have no effect. With TLS 1.3, rehandshaking has been removed from
789
 * the TLS protocol, replaced by separate post-handshake authentication and
790
 * rekey operations.
791
 *
792
 * Since: 2.28
793
 *
794
 * Deprecated: 2.60. Changing the rehandshake mode is no longer
795
 *   required for compatibility. Also, rehandshaking has been removed
796
 *   from the TLS protocol in TLS 1.3.
797
 */
798
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
799
void
800
g_tls_connection_set_rehandshake_mode (GTlsConnection       *conn,
801
               GTlsRehandshakeMode   mode)
802
0
{
803
0
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
804
805
0
  g_object_set (G_OBJECT (conn),
806
0
    "rehandshake-mode", G_TLS_REHANDSHAKE_SAFELY,
807
0
    NULL);
808
0
}
809
G_GNUC_END_IGNORE_DEPRECATIONS
810
811
/**
812
 * g_tls_connection_get_rehandshake_mode:
813
 * @conn: a #GTlsConnection
814
 *
815
 * Gets @conn rehandshaking mode. See
816
 * g_tls_connection_set_rehandshake_mode() for details.
817
 *
818
 * Returns: %G_TLS_REHANDSHAKE_SAFELY
819
 *
820
 * Since: 2.28
821
 *
822
 * Deprecated: 2.60. Changing the rehandshake mode is no longer
823
 *   required for compatibility. Also, rehandshaking has been removed
824
 *   from the TLS protocol in TLS 1.3.
825
 */
826
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
827
GTlsRehandshakeMode
828
g_tls_connection_get_rehandshake_mode (GTlsConnection       *conn)
829
0
{
830
0
  GTlsRehandshakeMode mode;
831
832
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), G_TLS_REHANDSHAKE_SAFELY);
833
834
  /* Continue to call g_object_get(), even though the return value is
835
   * ignored, so that behavior doesn’t change for derived classes.
836
   */
837
0
  g_object_get (G_OBJECT (conn),
838
0
    "rehandshake-mode", &mode,
839
0
    NULL);
840
0
  return G_TLS_REHANDSHAKE_SAFELY;
841
0
}
842
G_GNUC_END_IGNORE_DEPRECATIONS
843
844
/**
845
 * g_tls_connection_set_advertised_protocols:
846
 * @conn: a #GTlsConnection
847
 * @protocols: (array zero-terminated=1) (nullable): a %NULL-terminated
848
 *   array of ALPN protocol names (eg, "http/1.1", "h2"), or %NULL
849
 *
850
 * Sets the list of application-layer protocols to advertise that the
851
 * caller is willing to speak on this connection. The
852
 * Application-Layer Protocol Negotiation (ALPN) extension will be
853
 * used to negotiate a compatible protocol with the peer; use
854
 * g_tls_connection_get_negotiated_protocol() to find the negotiated
855
 * protocol after the handshake.  Specifying %NULL for the the value
856
 * of @protocols will disable ALPN negotiation.
857
 *
858
 * See [IANA TLS ALPN Protocol IDs](https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids)
859
 * for a list of registered protocol IDs.
860
 *
861
 * Since: 2.60
862
 */
863
void
864
g_tls_connection_set_advertised_protocols (GTlsConnection      *conn,
865
                                           const gchar * const *protocols)
866
0
{
867
0
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
868
869
0
  g_object_set (G_OBJECT (conn),
870
0
                "advertised-protocols", protocols,
871
0
                NULL);
872
0
}
873
874
/**
875
 * g_tls_connection_get_negotiated_protocol:
876
 * @conn: a #GTlsConnection
877
 *
878
 * Gets the name of the application-layer protocol negotiated during
879
 * the handshake.
880
 *
881
 * If the peer did not use the ALPN extension, or did not advertise a
882
 * protocol that matched one of @conn's protocols, or the TLS backend
883
 * does not support ALPN, then this will be %NULL. See
884
 * g_tls_connection_set_advertised_protocols().
885
 *
886
 * Returns: (nullable): the negotiated protocol, or %NULL
887
 *
888
 * Since: 2.60
889
 */
890
const gchar *
891
g_tls_connection_get_negotiated_protocol (GTlsConnection *conn)
892
0
{
893
0
  GTlsConnectionClass *class;
894
895
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
896
897
0
  class = G_TLS_CONNECTION_GET_CLASS (conn);
898
0
  if (class->get_negotiated_protocol == NULL)
899
0
    return NULL;
900
901
0
  return class->get_negotiated_protocol (conn);
902
0
}
903
904
/**
905
 * g_tls_channel_binding_error_quark:
906
 *
907
 * Gets the TLS channel binding error quark.
908
 *
909
 * Returns: a #GQuark.
910
 *
911
 * Since: 2.66
912
 */
913
G_DEFINE_QUARK (g-tls-channel-binding-error-quark, g_tls_channel_binding_error)
914
915
/**
916
 * g_tls_connection_get_channel_binding_data:
917
 * @conn: a #GTlsConnection
918
 * @type: #GTlsChannelBindingType type of data to fetch
919
 * @data: (out callee-allocates)(optional)(transfer none): #GByteArray is
920
 *        filled with the binding data, or %NULL
921
 * @error: a #GError pointer, or %NULL
922
 *
923
 * Query the TLS backend for TLS channel binding data of @type for @conn.
924
 *
925
 * This call retrieves TLS channel binding data as specified in RFC
926
 * [5056](https://tools.ietf.org/html/rfc5056), RFC
927
 * [5929](https://tools.ietf.org/html/rfc5929), and related RFCs.  The
928
 * binding data is returned in @data.  The @data is resized by the callee
929
 * using #GByteArray buffer management and will be freed when the @data
930
 * is destroyed by g_byte_array_unref(). If @data is %NULL, it will only
931
 * check whether TLS backend is able to fetch the data (e.g. whether @type
932
 * is supported by the TLS backend). It does not guarantee that the data
933
 * will be available though.  That could happen if TLS connection does not
934
 * support @type or the binding data is not available yet due to additional
935
 * negotiation or input required.
936
 *
937
 * Returns: %TRUE on success, %FALSE otherwise
938
 *
939
 * Since: 2.66
940
 */
941
gboolean
942
g_tls_connection_get_channel_binding_data (GTlsConnection          *conn,
943
                                           GTlsChannelBindingType   type,
944
                                           GByteArray              *data,
945
                                           GError                 **error)
946
0
{
947
0
  GTlsConnectionClass *class;
948
949
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
950
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
951
952
0
  class = G_TLS_CONNECTION_GET_CLASS (conn);
953
0
  if (class->get_binding_data == NULL)
954
0
    {
955
0
      g_set_error_literal (error, G_TLS_CHANNEL_BINDING_ERROR,
956
0
          G_TLS_CHANNEL_BINDING_ERROR_NOT_IMPLEMENTED,
957
0
          _("TLS backend does not implement TLS binding retrieval"));
958
0
      return FALSE;
959
0
    }
960
961
0
  return class->get_binding_data (conn, type, data, error);
962
0
}
963
964
/**
965
 * g_tls_connection_handshake:
966
 * @conn: a #GTlsConnection
967
 * @cancellable: (nullable): a #GCancellable, or %NULL
968
 * @error: a #GError, or %NULL
969
 *
970
 * Attempts a TLS handshake on @conn.
971
 *
972
 * On the client side, it is never necessary to call this method;
973
 * although the connection needs to perform a handshake after
974
 * connecting (or after sending a "STARTTLS"-type command),
975
 * #GTlsConnection will handle this for you automatically when you try
976
 * to send or receive data on the connection. You can call
977
 * g_tls_connection_handshake() manually if you want to know whether
978
 * the initial handshake succeeded or failed (as opposed to just
979
 * immediately trying to use @conn to read or write, in which case,
980
 * if it fails, it may not be possible to tell if it failed before or
981
 * after completing the handshake), but beware that servers may reject
982
 * client authentication after the handshake has completed, so a
983
 * successful handshake does not indicate the connection will be usable.
984
 *
985
 * Likewise, on the server side, although a handshake is necessary at
986
 * the beginning of the communication, you do not need to call this
987
 * function explicitly unless you want clearer error reporting.
988
 *
989
 * Previously, calling g_tls_connection_handshake() after the initial
990
 * handshake would trigger a rehandshake; however, this usage was
991
 * deprecated in GLib 2.60 because rehandshaking was removed from the
992
 * TLS protocol in TLS 1.3. Since GLib 2.64, calling this function after
993
 * the initial handshake will no longer do anything.
994
 *
995
 * When using a #GTlsConnection created by #GSocketClient, the
996
 * #GSocketClient performs the initial handshake, so calling this
997
 * function manually is not recommended.
998
 *
999
 * #GTlsConnection::accept_certificate may be emitted during the
1000
 * handshake.
1001
 *
1002
 * Returns: success or failure
1003
 *
1004
 * Since: 2.28
1005
 */
1006
gboolean
1007
g_tls_connection_handshake (GTlsConnection   *conn,
1008
          GCancellable     *cancellable,
1009
          GError          **error)
1010
0
{
1011
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
1012
1013
0
  return G_TLS_CONNECTION_GET_CLASS (conn)->handshake (conn, cancellable, error);
1014
0
}
1015
1016
/**
1017
 * g_tls_connection_handshake_async:
1018
 * @conn: a #GTlsConnection
1019
 * @io_priority: the [I/O priority][io-priority] of the request
1020
 * @cancellable: (nullable): a #GCancellable, or %NULL
1021
 * @callback: callback to call when the handshake is complete
1022
 * @user_data: the data to pass to the callback function
1023
 *
1024
 * Asynchronously performs a TLS handshake on @conn. See
1025
 * g_tls_connection_handshake() for more information.
1026
 *
1027
 * Since: 2.28
1028
 */
1029
void
1030
g_tls_connection_handshake_async (GTlsConnection       *conn,
1031
          int                   io_priority,
1032
          GCancellable         *cancellable,
1033
          GAsyncReadyCallback   callback,
1034
          gpointer              user_data)
1035
0
{
1036
0
  g_return_if_fail (G_IS_TLS_CONNECTION (conn));
1037
1038
0
  G_TLS_CONNECTION_GET_CLASS (conn)->handshake_async (conn, io_priority,
1039
0
                  cancellable,
1040
0
                  callback, user_data);
1041
0
}
1042
1043
/**
1044
 * g_tls_connection_handshake_finish:
1045
 * @conn: a #GTlsConnection
1046
 * @result: a #GAsyncResult.
1047
 * @error: a #GError pointer, or %NULL
1048
 *
1049
 * Finish an asynchronous TLS handshake operation. See
1050
 * g_tls_connection_handshake() for more information.
1051
 *
1052
 * Returns: %TRUE on success, %FALSE on failure, in which
1053
 * case @error will be set.
1054
 *
1055
 * Since: 2.28
1056
 */
1057
gboolean
1058
g_tls_connection_handshake_finish (GTlsConnection  *conn,
1059
           GAsyncResult    *result,
1060
           GError         **error)
1061
0
{
1062
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
1063
1064
0
  return G_TLS_CONNECTION_GET_CLASS (conn)->handshake_finish (conn, result, error);
1065
0
}
1066
1067
/**
1068
 * g_tls_connection_get_protocol_version:
1069
 * @conn: a #GTlsConnection
1070
 *
1071
 * Returns the current TLS protocol version, which may be
1072
 * %G_TLS_PROTOCOL_VERSION_UNKNOWN if the connection has not handshaked, or
1073
 * has been closed, or if the TLS backend has implemented a protocol version
1074
 * that is not a recognized #GTlsProtocolVersion.
1075
 *
1076
 * Returns: The current TLS protocol version
1077
 *
1078
 * Since: 2.70
1079
 */
1080
GTlsProtocolVersion
1081
g_tls_connection_get_protocol_version (GTlsConnection *conn)
1082
0
{
1083
0
  GTlsProtocolVersion protocol_version;
1084
0
  GEnumClass *enum_class;
1085
0
  GEnumValue *enum_value;
1086
1087
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), G_TLS_PROTOCOL_VERSION_UNKNOWN);
1088
1089
0
  g_object_get (G_OBJECT (conn),
1090
0
                "protocol-version", &protocol_version,
1091
0
                NULL);
1092
1093
  /* Convert unknown values to G_TLS_PROTOCOL_VERSION_UNKNOWN. */
1094
0
  enum_class = g_type_class_peek_static (G_TYPE_TLS_PROTOCOL_VERSION);
1095
0
  enum_value = g_enum_get_value (enum_class, protocol_version);
1096
0
  return enum_value ? protocol_version : G_TLS_PROTOCOL_VERSION_UNKNOWN;
1097
0
}
1098
1099
/**
1100
 * g_tls_connection_get_ciphersuite_name:
1101
 * @conn: a #GTlsConnection
1102
 *
1103
 * Returns the name of the current TLS ciphersuite, or %NULL if the
1104
 * connection has not handshaked or has been closed. Beware that the TLS
1105
 * backend may use any of multiple different naming conventions, because
1106
 * OpenSSL and GnuTLS have their own ciphersuite naming conventions that
1107
 * are different from each other and different from the standard, IANA-
1108
 * registered ciphersuite names. The ciphersuite name is intended to be
1109
 * displayed to the user for informative purposes only, and parsing it
1110
 * is not recommended.
1111
 *
1112
 * Returns: (nullable): The name of the current TLS ciphersuite, or %NULL
1113
 *
1114
 * Since: 2.70
1115
 */
1116
gchar *
1117
g_tls_connection_get_ciphersuite_name (GTlsConnection *conn)
1118
0
{
1119
0
  gchar *ciphersuite_name;
1120
1121
0
  g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
1122
1123
0
  g_object_get (G_OBJECT (conn),
1124
0
                "ciphersuite-name", &ciphersuite_name,
1125
0
                NULL);
1126
1127
0
  return g_steal_pointer (&ciphersuite_name);
1128
0
}
1129
1130
/**
1131
 * g_tls_error_quark:
1132
 *
1133
 * Gets the TLS error quark.
1134
 *
1135
 * Returns: a #GQuark.
1136
 *
1137
 * Since: 2.28
1138
 */
1139
G_DEFINE_QUARK (g-tls-error-quark, g_tls_error)
1140
1141
/**
1142
 * g_tls_connection_emit_accept_certificate:
1143
 * @conn: a #GTlsConnection
1144
 * @peer_cert: the peer's #GTlsCertificate
1145
 * @errors: the problems with @peer_cert
1146
 *
1147
 * Used by #GTlsConnection implementations to emit the
1148
 * #GTlsConnection::accept-certificate signal.
1149
 *
1150
 * Returns: %TRUE if one of the signal handlers has returned
1151
 *     %TRUE to accept @peer_cert
1152
 *
1153
 * Since: 2.28
1154
 */
1155
gboolean
1156
g_tls_connection_emit_accept_certificate (GTlsConnection       *conn,
1157
            GTlsCertificate      *peer_cert,
1158
            GTlsCertificateFlags  errors)
1159
0
{
1160
0
  gboolean accept = FALSE;
1161
1162
0
  g_signal_emit (conn, signals[ACCEPT_CERTIFICATE], 0,
1163
0
     peer_cert, errors, &accept);
1164
0
  return accept;
1165
0
}