Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / gtlsconnection.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2010 Red Hat, Inc
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
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
35 /**
36  * SECTION:gtlsconnection
37  * @short_description: TLS connection type
38  * @include: gio/gio.h
39  *
40  * #GTlsConnection is the base TLS connection class type, which wraps
41  * a #GIOStream and provides TLS encryption on top of it. Its
42  * subclasses, #GTlsClientConnection and #GTlsServerConnection,
43  * implement client-side and server-side TLS, respectively.
44  *
45  * Since: 2.28
46  */
47
48 /**
49  * GTlsConnection:
50  *
51  * Abstract base class for the backend-specific #GTlsClientConnection
52  * and #GTlsServerConnection types.
53  *
54  * Since: 2.28
55  */
56
57 G_DEFINE_ABSTRACT_TYPE (GTlsConnection, g_tls_connection, G_TYPE_IO_STREAM)
58
59 static void g_tls_connection_get_property (GObject    *object,
60                                            guint       prop_id,
61                                            GValue     *value,
62                                            GParamSpec *pspec);
63 static void g_tls_connection_set_property (GObject      *object,
64                                            guint         prop_id,
65                                            const GValue *value,
66                                            GParamSpec   *pspec);
67
68 enum {
69   ACCEPT_CERTIFICATE,
70
71   LAST_SIGNAL
72 };
73
74 static guint signals[LAST_SIGNAL] = { 0 };
75
76 enum {
77   PROP_0,
78   PROP_BASE_IO_STREAM,
79   PROP_REQUIRE_CLOSE_NOTIFY,
80   PROP_REHANDSHAKE_MODE,
81   PROP_USE_SYSTEM_CERTDB,
82   PROP_DATABASE,
83   PROP_INTERACTION,
84   PROP_CERTIFICATE,
85   PROP_PEER_CERTIFICATE,
86   PROP_PEER_CERTIFICATE_ERRORS
87 };
88
89 static void
90 g_tls_connection_class_init (GTlsConnectionClass *klass)
91 {
92   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
93
94   gobject_class->get_property = g_tls_connection_get_property;
95   gobject_class->set_property = g_tls_connection_set_property;
96
97   /**
98    * GTlsConnection:base-io-stream:
99    *
100    * The #GIOStream that the connection wraps
101    *
102    * Since: 2.28
103    */
104   g_object_class_install_property (gobject_class, PROP_BASE_IO_STREAM,
105                                    g_param_spec_object ("base-io-stream",
106                                                         P_("Base IOStream"),
107                                                         P_("The GIOStream that the connection wraps"),
108                                                         G_TYPE_IO_STREAM,
109                                                         G_PARAM_READWRITE |
110                                                         G_PARAM_CONSTRUCT_ONLY |
111                                                         G_PARAM_STATIC_STRINGS));
112   /**
113    * GTlsConnection:use-system-certdb:
114    *
115    * Whether or not the system certificate database will be used to
116    * verify peer certificates. See
117    * g_tls_connection_set_use_system_certdb().
118    *
119    * Deprecated: 2.30: Use GTlsConnection:database instead
120    */
121   g_object_class_install_property (gobject_class, PROP_USE_SYSTEM_CERTDB,
122                                    g_param_spec_boolean ("use-system-certdb",
123                                                          P_("Use system certificate database"),
124                                                          P_("Whether to verify peer certificates against the system certificate database"),
125                                                          TRUE,
126                                                          G_PARAM_READWRITE |
127                                                          G_PARAM_CONSTRUCT |
128                                                          G_PARAM_STATIC_STRINGS));
129   /**
130    * GTlsConnection:database:
131    *
132    * The certificate database to use when verifying this TLS connection.
133    * If no cerificate database is set, then the default database will be
134    * used. See g_tls_backend_get_default_database().
135    *
136    * Since: 2.30
137    */
138   g_object_class_install_property (gobject_class, PROP_DATABASE,
139                                    g_param_spec_object ("database",
140                                                          P_("Database"),
141                                                          P_("Certificate database to use for looking up or verifying certificates"),
142                                                          G_TYPE_TLS_DATABASE,
143                                                          G_PARAM_READWRITE |
144                                                          G_PARAM_STATIC_STRINGS));
145   /**
146    * GTlsConnection:interaction:
147    *
148    * A #GTlsInteraction object to be used when the connection or certificate
149    * database need to interact with the user. This will be used to prompt the
150    * user for passwords where necessary.
151    *
152    * Since: 2.30
153    */
154   g_object_class_install_property (gobject_class, PROP_INTERACTION,
155                                    g_param_spec_object ("interaction",
156                                                         P_("Interaction"),
157                                                         P_("Optional object for user interaction"),
158                                                         G_TYPE_TLS_INTERACTION,
159                                                         G_PARAM_READWRITE |
160                                                         G_PARAM_STATIC_STRINGS));
161   /**
162    * GTlsConnection:require-close-notify:
163    *
164    * Whether or not proper TLS close notification is required.
165    * See g_tls_connection_set_require_close_notify().
166    *
167    * Since: 2.28
168    */
169   g_object_class_install_property (gobject_class, PROP_REQUIRE_CLOSE_NOTIFY,
170                                    g_param_spec_boolean ("require-close-notify",
171                                                          P_("Require close notify"),
172                                                          P_("Whether to require proper TLS close notification"),
173                                                          TRUE,
174                                                          G_PARAM_READWRITE |
175                                                          G_PARAM_CONSTRUCT |
176                                                          G_PARAM_STATIC_STRINGS));
177   /**
178    * GTlsConnection:rehandshake-mode:
179    *
180    * The rehandshaking mode. See
181    * g_tls_connection_set_rehandshake_mode().
182    *
183    * Since: 2.28
184    */
185   g_object_class_install_property (gobject_class, PROP_REHANDSHAKE_MODE,
186                                    g_param_spec_enum ("rehandshake-mode",
187                                                       P_("Rehandshake mode"),
188                                                       P_("When to allow rehandshaking"),
189                                                       G_TYPE_TLS_REHANDSHAKE_MODE,
190                                                       G_TLS_REHANDSHAKE_SAFELY,
191                                                       G_PARAM_READWRITE |
192                                                       G_PARAM_CONSTRUCT |
193                                                       G_PARAM_STATIC_STRINGS));
194   /**
195    * GTlsConnection:certificate:
196    *
197    * The connection's certificate; see
198    * g_tls_connection_set_certificate().
199    *
200    * Since: 2.28
201    */
202   g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
203                                    g_param_spec_object ("certificate",
204                                                         P_("Certificate"),
205                                                         P_("The connection's certificate"),
206                                                         G_TYPE_TLS_CERTIFICATE,
207                                                         G_PARAM_READWRITE |
208                                                         G_PARAM_STATIC_STRINGS));
209   /**
210    * GTlsConnection:peer-certificate:
211    *
212    * The connection's peer's certificate, after the TLS handshake has
213    * completed and the certificate has been accepted. Note in
214    * particular that this is not yet set during the emission of
215    * #GTlsConnection::accept-certificate.
216    *
217    * (You can watch for a #GObject::notify signal on this property to
218    * detect when a handshake has occurred.)
219    *
220    * Since: 2.28
221    */
222   g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE,
223                                    g_param_spec_object ("peer-certificate",
224                                                         P_("Peer Certificate"),
225                                                         P_("The connection's peer's certificate"),
226                                                         G_TYPE_TLS_CERTIFICATE,
227                                                         G_PARAM_READABLE |
228                                                         G_PARAM_STATIC_STRINGS));
229   /**
230    * GTlsConnection:peer-certificate-errors:
231    *
232    * The errors noticed-and-ignored while verifying
233    * #GTlsConnection:peer-certificate. Normally this should be 0, but
234    * it may not be if #GTlsClientConnection:validation-flags is not
235    * %G_TLS_CERTIFICATE_VALIDATE_ALL, or if
236    * #GTlsConnection::accept-certificate overrode the default
237    * behavior.
238    *
239    * Since: 2.28
240    */
241   g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE_ERRORS,
242                                    g_param_spec_flags ("peer-certificate-errors",
243                                                        P_("Peer Certificate Errors"),
244                                                        P_("Errors found with the peer's certificate"),
245                                                        G_TYPE_TLS_CERTIFICATE_FLAGS,
246                                                        0,
247                                                        G_PARAM_READABLE |
248                                                        G_PARAM_STATIC_STRINGS));
249
250   /**
251    * GTlsConnection::accept-certificate:
252    * @conn: a #GTlsConnection
253    * @peer_cert: the peer's #GTlsCertificate
254    * @errors: the problems with @peer_cert.
255    *
256    * Emitted during the TLS handshake after the peer certificate has
257    * been received. You can examine @peer_cert's certification path by
258    * calling g_tls_certificate_get_issuer() on it.
259    *
260    * For a client-side connection, @peer_cert is the server's
261    * certificate, and the signal will only be emitted if the
262    * certificate was not acceptable according to @conn's
263    * #GTlsClientConnection:validation_flags. If you would like the
264    * certificate to be accepted despite @errors, return %TRUE from the
265    * signal handler. Otherwise, if no handler accepts the certificate,
266    * the handshake will fail with %G_TLS_ERROR_BAD_CERTIFICATE.
267    *
268    * For a server-side connection, @peer_cert is the certificate
269    * presented by the client, if this was requested via the server's
270    * #GTlsServerConnection:authentication_mode. On the server side,
271    * the signal is always emitted when the client presents a
272    * certificate, and the certificate will only be accepted if a
273    * handler returns %TRUE.
274    *
275    * Note that if this signal is emitted as part of asynchronous I/O
276    * in the main thread, then you should not attempt to interact with
277    * the user before returning from the signal handler. If you want to
278    * let the user decide whether or not to accept the certificate, you
279    * would have to return %FALSE from the signal handler on the first
280    * attempt, and then after the connection attempt returns a
281    * %G_TLS_ERROR_HANDSHAKE, you can interact with the user, and if
282    * the user decides to accept the certificate, remember that fact,
283    * create a new connection, and return %TRUE from the signal handler
284    * the next time.
285    *
286    * If you are doing I/O in another thread, you do not
287    * need to worry about this, and can simply block in the signal
288    * handler until the UI thread returns an answer.
289    *
290    * Return value: %TRUE to accept @peer_cert (which will also
291    * immediately end the signal emission). %FALSE to allow the signal
292    * emission to continue, which will cause the handshake to fail if
293    * no one else overrides it.
294    *
295    * Since: 2.28
296    */
297   signals[ACCEPT_CERTIFICATE] =
298     g_signal_new (I_("accept-certificate"),
299                   G_TYPE_TLS_CONNECTION,
300                   G_SIGNAL_RUN_LAST,
301                   G_STRUCT_OFFSET (GTlsConnectionClass, accept_certificate),
302                   g_signal_accumulator_true_handled, NULL,
303                   NULL,
304                   G_TYPE_BOOLEAN, 2,
305                   G_TYPE_TLS_CERTIFICATE,
306                   G_TYPE_TLS_CERTIFICATE_FLAGS);
307 }
308
309 static void
310 g_tls_connection_init (GTlsConnection *conn)
311 {
312 }
313
314 static void
315 g_tls_connection_get_property (GObject    *object,
316                                guint       prop_id,
317                                GValue     *value,
318                                GParamSpec *pspec)
319 {
320   G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321 }
322
323 static void
324 g_tls_connection_set_property (GObject      *object,
325                                guint         prop_id,
326                                const GValue *value,
327                                GParamSpec   *pspec)
328 {
329   G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 }
331
332 /**
333  * g_tls_connection_set_use_system_certdb:
334  * @conn: a #GTlsConnection
335  * @use_system_certdb: whether to use the system certificate database
336  *
337  * Sets whether @conn uses the system certificate database to verify
338  * peer certificates. This is %TRUE by default. If set to %FALSE, then
339  * peer certificate validation will always set the
340  * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
341  * #GTlsConnection::accept-certificate will always be emitted on
342  * client-side connections, unless that bit is not set in
343  * #GTlsClientConnection:validation-flags).
344  *
345  * Deprecated: 2.30: Use g_tls_connection_set_database() instead
346  */
347 void
348 g_tls_connection_set_use_system_certdb (GTlsConnection *conn,
349                                         gboolean        use_system_certdb)
350 {
351   g_return_if_fail (G_IS_TLS_CONNECTION (conn));
352
353   g_object_set (G_OBJECT (conn),
354                 "use-system-certdb", use_system_certdb,
355                 NULL);
356 }
357
358 /**
359  * g_tls_connection_get_use_system_certdb:
360  * @conn: a #GTlsConnection
361  *
362  * Gets whether @conn uses the system certificate database to verify
363  * peer certificates. See g_tls_connection_set_use_system_certdb().
364  *
365  * Return value: whether @conn uses the system certificate database
366  *
367  * Deprecated: 2.30: Use g_tls_connection_get_database() instead
368  */
369 gboolean
370 g_tls_connection_get_use_system_certdb (GTlsConnection *conn)
371 {
372   gboolean use_system_certdb;
373
374   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
375
376   g_object_get (G_OBJECT (conn),
377                 "use-system-certdb", &use_system_certdb,
378                 NULL);
379   return use_system_certdb;
380 }
381
382 /**
383  * g_tls_connection_set_database:
384  * @conn: a #GTlsConnection
385  * @database: a #GTlsDatabase
386  *
387  * Sets the certificate database that is used to verify peer certificates.
388  * This is set to the default database by default. See
389  * g_tls_backend_get_default_database(). If set to %NULL, then
390  * peer certificate validation will always set the
391  * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
392  * #GTlsConnection::accept-certificate will always be emitted on
393  * client-side connections, unless that bit is not set in
394  * #GTlsClientConnection:validation-flags).
395  *
396  * Since: 2.30
397  */
398 void
399 g_tls_connection_set_database (GTlsConnection *conn,
400                                GTlsDatabase   *database)
401 {
402   g_return_if_fail (G_IS_TLS_CONNECTION (conn));
403   g_return_if_fail (database == NULL || G_IS_TLS_DATABASE (database));
404
405   g_object_set (G_OBJECT (conn),
406                 "database", database,
407                 NULL);
408 }
409
410 /**
411  * g_tls_connection_get_database:
412  * @conn: a #GTlsConnection
413  *
414  * Gets the certificate database that @conn uses to verify
415  * peer certificates. See g_tls_connection_set_database().
416  *
417  * Return value: (transfer none): the certificate database that @conn uses or %NULL
418  *
419  * Since: 2.30
420  */
421 GTlsDatabase*
422 g_tls_connection_get_database (GTlsConnection *conn)
423 {
424   GTlsDatabase *database = NULL;
425
426   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
427
428   g_object_get (G_OBJECT (conn),
429                 "database", &database,
430                 NULL);
431   if (database)
432     g_object_unref (database);
433   return database;
434 }
435
436 /**
437  * g_tls_connection_set_certificate:
438  * @conn: a #GTlsConnection
439  * @certificate: the certificate to use for @conn
440  *
441  * This sets the certificate that @conn will present to its peer
442  * during the TLS handshake. For a #GTlsServerConnection, it is
443  * mandatory to set this, and that will normally be done at construct
444  * time.
445  *
446  * For a #GTlsClientConnection, this is optional. If a handshake fails
447  * with %G_TLS_ERROR_CERTIFICATE_REQUIRED, that means that the server
448  * requires a certificate, and if you try connecting again, you should
449  * call this method first. You can call
450  * g_tls_client_connection_get_accepted_cas() on the failed connection
451  * to get a list of Certificate Authorities that the server will
452  * accept certificates from.
453  *
454  * (It is also possible that a server will allow the connection with
455  * or without a certificate; in that case, if you don't provide a
456  * certificate, you can tell that the server requested one by the fact
457  * that g_tls_client_connection_get_accepted_cas() will return
458  * non-%NULL.)
459  *
460  * Since: 2.28
461  */
462 void
463 g_tls_connection_set_certificate (GTlsConnection  *conn,
464                                   GTlsCertificate *certificate)
465 {
466   g_return_if_fail (G_IS_TLS_CONNECTION (conn));
467   g_return_if_fail (G_IS_TLS_CERTIFICATE (certificate));
468
469   g_object_set (G_OBJECT (conn), "certificate", certificate, NULL);
470 }
471
472 /**
473  * g_tls_connection_get_certificate:
474  * @conn: a #GTlsConnection
475  *
476  * Gets @conn's certificate, as set by
477  * g_tls_connection_set_certificate().
478  *
479  * Return value: (transfer none): @conn's certificate, or %NULL
480  *
481  * Since: 2.28
482  */
483 GTlsCertificate *
484 g_tls_connection_get_certificate (GTlsConnection *conn)
485 {
486   GTlsCertificate *certificate;
487
488   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
489
490   g_object_get (G_OBJECT (conn), "certificate", &certificate, NULL);
491   if (certificate)
492     g_object_unref (certificate);
493
494   return certificate;
495 }
496
497 /**
498  * g_tls_connection_set_interaction:
499  * @conn: a connection
500  * @interaction: (allow-none): an interaction object, or %NULL
501  *
502  * Set the object that will be used to interact with the user. It will be used
503  * for things like prompting the user for passwords.
504  *
505  * The @interaction argument will normally be a derived subclass of
506  * #GTlsInteraction. %NULL can also be provided if no user interaction
507  * should occur for this connection.
508  *
509  * Since: 2.30
510  */
511 void
512 g_tls_connection_set_interaction (GTlsConnection       *conn,
513                                   GTlsInteraction      *interaction)
514 {
515   g_return_if_fail (G_IS_TLS_CONNECTION (conn));
516   g_return_if_fail (interaction == NULL || G_IS_TLS_INTERACTION (interaction));
517
518   g_object_set (G_OBJECT (conn), "interaction", interaction, NULL);
519 }
520
521 /**
522  * g_tls_connection_get_interaction:
523  * @conn: a connection
524  *
525  * Get the object that will be used to interact with the user. It will be used
526  * for things like prompting the user for passwords. If %NULL is returned, then
527  * no user interaction will occur for this connection.
528  *
529  * Returns: (transfer none): The interaction object.
530  *
531  * Since: 2.30
532  */
533 GTlsInteraction *
534 g_tls_connection_get_interaction (GTlsConnection       *conn)
535 {
536   GTlsInteraction *interaction = NULL;
537
538   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
539
540   g_object_get (G_OBJECT (conn), "interaction", &interaction, NULL);
541   if (interaction)
542     g_object_unref (interaction);
543
544   return interaction;
545 }
546
547 /**
548  * g_tls_connection_get_peer_certificate:
549  * @conn: a #GTlsConnection
550  *
551  * Gets @conn's peer's certificate after the handshake has completed.
552  * (It is not set during the emission of
553  * #GTlsConnection::accept-certificate.)
554  *
555  * Return value: (transfer none): @conn's peer's certificate, or %NULL
556  *
557  * Since: 2.28
558  */
559 GTlsCertificate *
560 g_tls_connection_get_peer_certificate (GTlsConnection *conn)
561 {
562   GTlsCertificate *peer_certificate;
563
564   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
565
566   g_object_get (G_OBJECT (conn), "peer-certificate", &peer_certificate, NULL);
567   if (peer_certificate)
568     g_object_unref (peer_certificate);
569
570   return peer_certificate;
571 }
572
573 /**
574  * g_tls_connection_get_peer_certificate_errors:
575  * @conn: a #GTlsConnection
576  *
577  * Gets the errors associated with validating @conn's peer's
578  * certificate, after the handshake has completed. (It is not set
579  * during the emission of #GTlsConnection::accept-certificate.)
580  *
581  * Return value: @conn's peer's certificate errors
582  *
583  * Since: 2.28
584  */
585 GTlsCertificateFlags
586 g_tls_connection_get_peer_certificate_errors (GTlsConnection *conn)
587 {
588   GTlsCertificateFlags errors;
589
590   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), 0);
591
592   g_object_get (G_OBJECT (conn), "peer-certificate-errors", &errors, NULL);
593   return errors;
594 }
595
596 /**
597  * g_tls_connection_set_require_close_notify:
598  * @conn: a #GTlsConnection
599  * @require_close_notify: whether or not to require close notification
600  *
601  * Sets whether or not @conn expects a proper TLS close notification
602  * before the connection is closed. If this is %TRUE (the default),
603  * then @conn will expect to receive a TLS close notification from its
604  * peer before the connection is closed, and will return a
605  * %G_TLS_ERROR_EOF error if the connection is closed without proper
606  * notification (since this may indicate a network error, or
607  * man-in-the-middle attack).
608  *
609  * In some protocols, the application will know whether or not the
610  * connection was closed cleanly based on application-level data
611  * (because the application-level data includes a length field, or is
612  * somehow self-delimiting); in this case, the close notify is
613  * redundant and sometimes omitted. (TLS 1.1 explicitly allows this;
614  * in TLS 1.0 it is technically an error, but often done anyway.) You
615  * can use g_tls_connection_set_require_close_notify() to tell @conn
616  * to allow an "unannounced" connection close, in which case the close
617  * will show up as a 0-length read, as in a non-TLS
618  * #GSocketConnection, and it is up to the application to check that
619  * the data has been fully received.
620  *
621  * Note that this only affects the behavior when the peer closes the
622  * connection; when the application calls g_io_stream_close() itself
623  * on @conn, this will send a close notification regardless of the
624  * setting of this property. If you explicitly want to do an unclean
625  * close, you can close @conn's #GTlsConnection:base-io-stream rather
626  * than closing @conn itself.
627  *
628  * Since: 2.28
629  */
630 void
631 g_tls_connection_set_require_close_notify (GTlsConnection *conn,
632                                            gboolean        require_close_notify)
633 {
634   g_return_if_fail (G_IS_TLS_CONNECTION (conn));
635
636   g_object_set (G_OBJECT (conn),
637                 "require-close-notify", require_close_notify,
638                 NULL);
639 }
640
641 /**
642  * g_tls_connection_get_require_close_notify:
643  * @conn: a #GTlsConnection
644  *
645  * Tests whether or not @conn expects a proper TLS close notification
646  * when the connection is closed. See
647  * g_tls_connection_set_require_close_notify() for details.
648  *
649  * Return value: %TRUE if @conn requires a proper TLS close
650  * notification.
651  *
652  * Since: 2.28
653  */
654 gboolean
655 g_tls_connection_get_require_close_notify (GTlsConnection *conn)
656 {
657   gboolean require_close_notify;
658
659   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
660
661   g_object_get (G_OBJECT (conn),
662                 "require-close-notify", &require_close_notify,
663                 NULL);
664   return require_close_notify;
665 }
666
667 /**
668  * g_tls_connection_set_rehandshake_mode:
669  * @conn: a #GTlsConnection
670  * @mode: the rehandshaking mode
671  *
672  * Sets how @conn behaves with respect to rehandshaking requests.
673  *
674  * %G_TLS_REHANDSHAKE_NEVER means that it will never agree to
675  * rehandshake after the initial handshake is complete. (For a client,
676  * this means it will refuse rehandshake requests from the server, and
677  * for a server, this means it will close the connection with an error
678  * if the client attempts to rehandshake.)
679  *
680  * %G_TLS_REHANDSHAKE_SAFELY means that the connection will allow a
681  * rehandshake only if the other end of the connection supports the
682  * TLS <literal>renegotiation_info</literal> extension. This is the
683  * default behavior, but means that rehandshaking will not work
684  * against older implementations that do not support that extension.
685  *
686  * %G_TLS_REHANDSHAKE_UNSAFELY means that the connection will allow
687  * rehandshaking even without the
688  * <literal>renegotiation_info</literal> extension. On the server side
689  * in particular, this is not recommended, since it leaves the server
690  * open to certain attacks. However, this mode is necessary if you
691  * need to allow renegotiation with older client software.
692  *
693  * Since: 2.28
694  */
695 void
696 g_tls_connection_set_rehandshake_mode (GTlsConnection       *conn,
697                                        GTlsRehandshakeMode   mode)
698 {
699   g_return_if_fail (G_IS_TLS_CONNECTION (conn));
700
701   g_object_set (G_OBJECT (conn),
702                 "rehandshake-mode", mode,
703                 NULL);
704 }
705
706 /**
707  * g_tls_connection_get_rehandshake_mode:
708  * @conn: a #GTlsConnection
709  *
710  * Gets @conn rehandshaking mode. See
711  * g_tls_connection_set_rehandshake_mode() for details.
712  *
713  * Return value: @conn's rehandshaking mode
714  *
715  * Since: 2.28
716  */
717 GTlsRehandshakeMode
718 g_tls_connection_get_rehandshake_mode (GTlsConnection       *conn)
719 {
720   GTlsRehandshakeMode mode;
721
722   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), G_TLS_REHANDSHAKE_NEVER);
723
724   g_object_get (G_OBJECT (conn),
725                 "rehandshake-mode", &mode,
726                 NULL);
727   return mode;
728 }
729
730 /**
731  * g_tls_connection_handshake:
732  * @conn: a #GTlsConnection
733  * @cancellable: (allow-none): a #GCancellable, or %NULL
734  * @error: a #GError, or %NULL
735  *
736  * Attempts a TLS handshake on @conn.
737  *
738  * On the client side, it is never necessary to call this method;
739  * although the connection needs to perform a handshake after
740  * connecting (or after sending a "STARTTLS"-type command) and may
741  * need to rehandshake later if the server requests it,
742  * #GTlsConnection will handle this for you automatically when you try
743  * to send or receive data on the connection. However, you can call
744  * g_tls_connection_handshake() manually if you want to know for sure
745  * whether the initial handshake succeeded or failed (as opposed to
746  * just immediately trying to write to @conn's output stream, in which
747  * case if it fails, it may not be possible to tell if it failed
748  * before or after completing the handshake).
749  *
750  * Likewise, on the server side, although a handshake is necessary at
751  * the beginning of the communication, you do not need to call this
752  * function explicitly unless you want clearer error reporting.
753  * However, you may call g_tls_connection_handshake() later on to
754  * renegotiate parameters (encryption methods, etc) with the client.
755  *
756  * #GTlsConnection::accept_certificate may be emitted during the
757  * handshake.
758  *
759  * Return value: success or failure
760  *
761  * Since: 2.28
762  */
763 gboolean
764 g_tls_connection_handshake (GTlsConnection   *conn,
765                             GCancellable     *cancellable,
766                             GError          **error)
767 {
768   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
769
770   return G_TLS_CONNECTION_GET_CLASS (conn)->handshake (conn, cancellable, error);
771 }
772
773 /**
774  * g_tls_connection_handshake_async:
775  * @conn: a #GTlsConnection
776  * @io_priority: the <link linkend="io-priority">I/O priority</link>
777  * of the request.
778  * @cancellable: (allow-none): a #GCancellable, or %NULL
779  * @callback: callback to call when the handshake is complete
780  * @user_data: the data to pass to the callback function
781  *
782  * Asynchronously performs a TLS handshake on @conn. See
783  * g_tls_connection_handshake() for more information.
784  *
785  * Since: 2.28
786  */
787 void
788 g_tls_connection_handshake_async (GTlsConnection       *conn,
789                                   int                   io_priority,
790                                   GCancellable         *cancellable,
791                                   GAsyncReadyCallback   callback,
792                                   gpointer              user_data)
793 {
794   g_return_if_fail (G_IS_TLS_CONNECTION (conn));
795
796   G_TLS_CONNECTION_GET_CLASS (conn)->handshake_async (conn, io_priority,
797                                                       cancellable,
798                                                       callback, user_data);
799 }
800
801 /**
802  * g_tls_connection_handshake_finish:
803  * @conn: a #GTlsConnection
804  * @result: a #GAsyncResult.
805  * @error: a #GError pointer, or %NULL
806  *
807  * Finish an asynchronous TLS handshake operation. See
808  * g_tls_connection_handshake() for more information.
809  *
810  * Return value: %TRUE on success, %FALSE on failure, in which
811  * case @error will be set.
812  *
813  * Since: 2.28
814  */
815 gboolean
816 g_tls_connection_handshake_finish (GTlsConnection  *conn,
817                                    GAsyncResult    *result,
818                                    GError         **error)
819 {
820   g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
821
822   return G_TLS_CONNECTION_GET_CLASS (conn)->handshake_finish (conn, result, error);
823 }
824
825 /**
826  * g_tls_error_quark:
827  *
828  * Gets the TLS error quark.
829  *
830  * Return value: a #GQuark.
831  *
832  * Since: 2.28
833  */
834 GQuark
835 g_tls_error_quark (void)
836 {
837   return g_quark_from_static_string ("g-tls-error-quark");
838 }
839
840
841 /**
842  * g_tls_connection_emit_accept_certificate:
843  * @conn: a #GTlsConnection
844  * @peer_cert: the peer's #GTlsCertificate
845  * @errors: the problems with @peer_cert
846  *
847  * Used by #GTlsConnection implementations to emit the
848  * #GTlsConnection::accept-certificate signal.
849  *
850  * Return value: %TRUE if one of the signal handlers has returned
851  *     %TRUE to accept @peer_cert
852  *
853  * Since: 2.28
854  */
855 gboolean
856 g_tls_connection_emit_accept_certificate (GTlsConnection       *conn,
857                                           GTlsCertificate      *peer_cert,
858                                           GTlsCertificateFlags  errors)
859 {
860   gboolean accept = FALSE;
861
862   g_signal_emit (conn, signals[ACCEPT_CERTIFICATE], 0,
863                  peer_cert, errors, &accept);
864   return accept;
865 }