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