1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2010 Red Hat, Inc
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.
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.
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.
24 #include "gtlsconnection.h"
25 #include "gcancellable.h"
26 #include "gioenumtypes.h"
28 #include "gtlsbackend.h"
29 #include "gtlscertificate.h"
30 #include "gtlsclientconnection.h"
34 * SECTION:gtlsconnection
35 * @short_description: TLS connection type
38 * #GTlsConnection is the base TLS connection class type, which wraps
39 * a #GIOStream and provides TLS encryption on top of it. Its
40 * subclasses, #GTlsClientConnection and #GTlsServerConnection,
41 * implement client-side and server-side TLS, respectively.
49 * Abstract base class for the backend-specific #GTlsClientConnection
50 * and #GTlsServerConnection types.
55 G_DEFINE_ABSTRACT_TYPE (GTlsConnection, g_tls_connection, G_TYPE_IO_STREAM)
57 static void g_tls_connection_get_property (GObject *object,
61 static void g_tls_connection_set_property (GObject *object,
72 static guint signals[LAST_SIGNAL] = { 0 };
77 PROP_REQUIRE_CLOSE_NOTIFY,
78 PROP_REHANDSHAKE_MODE,
79 PROP_USE_SYSTEM_CERTDB,
81 PROP_PEER_CERTIFICATE,
82 PROP_PEER_CERTIFICATE_ERRORS
86 g_tls_connection_class_init (GTlsConnectionClass *klass)
88 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90 gobject_class->get_property = g_tls_connection_get_property;
91 gobject_class->set_property = g_tls_connection_set_property;
94 * GTlsConnection:base-io-stream:
96 * The #GIOStream that the connection wraps
100 g_object_class_install_property (gobject_class, PROP_BASE_IO_STREAM,
101 g_param_spec_object ("base-io-stream",
103 P_("The GIOStream that the connection wraps"),
106 G_PARAM_CONSTRUCT_ONLY |
107 G_PARAM_STATIC_STRINGS));
109 * GTlsConnection:use-system-certdb:
111 * Whether or not the system certificate database will be used to
112 * verify peer certificates. See
113 * g_tls_connection_set_use_system_certdb().
117 g_object_class_install_property (gobject_class, PROP_USE_SYSTEM_CERTDB,
118 g_param_spec_boolean ("use-system-certdb",
119 P_("Use system certificate database"),
120 P_("Whether to verify peer certificates against the system certificate database"),
124 G_PARAM_STATIC_STRINGS));
126 * GTlsConnection:require-close-notify:
128 * Whether or not proper TLS close notification is required.
129 * See g_tls_connection_set_require_close_notify().
133 g_object_class_install_property (gobject_class, PROP_REQUIRE_CLOSE_NOTIFY,
134 g_param_spec_boolean ("require-close-notify",
135 P_("Require close notify"),
136 P_("Whether to require proper TLS close notification"),
140 G_PARAM_STATIC_STRINGS));
142 * GTlsConnection:rehandshake-mode:
144 * The rehandshaking mode. See
145 * g_tls_connection_set_rehandshake_mode().
149 g_object_class_install_property (gobject_class, PROP_REHANDSHAKE_MODE,
150 g_param_spec_enum ("rehandshake-mode",
151 P_("Rehandshake mode"),
152 P_("When to allow rehandshaking"),
153 G_TYPE_TLS_REHANDSHAKE_MODE,
154 G_TLS_REHANDSHAKE_SAFELY,
157 G_PARAM_STATIC_STRINGS));
159 * GTlsConnection:certificate:
161 * The connection's certificate; see
162 * g_tls_connection_set_certificate().
166 g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
167 g_param_spec_object ("certificate",
169 P_("The connection's certificate"),
170 G_TYPE_TLS_CERTIFICATE,
172 G_PARAM_STATIC_STRINGS));
174 * GTlsConnection:peer-certificate:
176 * The connection's peer's certificate, after the TLS handshake has
177 * completed and the certificate has been accepted. Note in
178 * particular that this is not yet set during the emission of
179 * #GTlsConnection::accept-certificate.
181 * (You can watch for a #GObject::notify signal on this property to
182 * detect when a handshake has occurred.)
186 g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE,
187 g_param_spec_object ("peer-certificate",
188 P_("Peer Certificate"),
189 P_("The connection's peer's certificate"),
190 G_TYPE_TLS_CERTIFICATE,
192 G_PARAM_STATIC_STRINGS));
194 * GTlsConnection:peer-certificate-errors:
196 * The errors noticed-and-ignored while verifying
197 * #GTlsConnection:peer-certificate. Normally this should be 0, but
198 * it may not be if #GTlsClientConnection:validation-flags is not
199 * %G_TLS_CERTIFICATE_VALIDATE_ALL, or if
200 * #GTlsConnection::accept-certificate overrode the default
205 g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE_ERRORS,
206 g_param_spec_flags ("peer-certificate-errors",
207 P_("Peer Certificate Errors"),
208 P_("Errors found with the peer's certificate"),
209 G_TYPE_TLS_CERTIFICATE_FLAGS,
212 G_PARAM_STATIC_STRINGS));
215 * GTlsConnection::accept-certificate:
216 * @conn: a #GTlsConnection
217 * @peer_cert: the peer's #GTlsCertificate
218 * @errors: the problems with @peer_cert.
220 * Emitted during the TLS handshake after the peer certificate has
221 * been received. You can examine @peer_cert's certification path by
222 * calling g_tls_certificate_get_issuer() on it.
224 * For a client-side connection, @peer_cert is the server's
225 * certificate, and the signal will only be emitted if the
226 * certificate was not acceptable according to @conn's
227 * #GTlsClientConnection:validation_flags. If you would like the
228 * certificate to be accepted despite @errors, return %TRUE from the
229 * signal handler. Otherwise, if no handler accepts the certificate,
230 * the handshake will fail with %G_TLS_ERROR_BAD_CERTIFICATE.
232 * For a server-side connection, @peer_cert is the certificate
233 * presented by the client, if this was requested via the server's
234 * #GTlsServerConnection:authentication_mode. On the server side,
235 * the signal is always emitted when the client presents a
236 * certificate, and the certificate will only be accepted if a
237 * handler returns %TRUE.
239 * Note that if this signal is emitted as part of asynchronous I/O
240 * in the main thread, then you should not attempt to interact with
241 * the user before returning from the signal handler. If you want to
242 * let the user decide whether or not to accept the certificate, you
243 * would have to return %FALSE from the signal handler on the first
244 * attempt, and then after the connection attempt returns a
245 * %G_TLS_ERROR_HANDSHAKE, you can interact with the user, and if
246 * the user decides to accept the certificate, remember that fact,
247 * create a new connection, and return %TRUE from the signal handler
250 * If you are doing I/O in another thread, you do not
251 * need to worry about this, and can simply block in the signal
252 * handler until the UI thread returns an answer.
254 * Return value: %TRUE to accept @peer_cert (which will also
255 * immediately end the signal emission). %FALSE to allow the signal
256 * emission to continue, which will cause the handshake to fail if
257 * no one else overrides it.
261 signals[ACCEPT_CERTIFICATE] =
262 g_signal_new (I_("accept-certificate"),
263 G_TYPE_TLS_CONNECTION,
265 G_STRUCT_OFFSET (GTlsConnectionClass, accept_certificate),
266 g_signal_accumulator_true_handled, NULL,
269 G_TYPE_TLS_CERTIFICATE,
270 G_TYPE_TLS_CERTIFICATE_FLAGS);
274 g_tls_connection_init (GTlsConnection *conn)
279 g_tls_connection_get_property (GObject *object,
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 g_tls_connection_set_property (GObject *object,
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297 * g_tls_connection_set_use_system_certdb:
298 * @conn: a #GTlsConnection
299 * @use_system_certdb: whether to use the system certificate database
301 * Sets whether @conn uses the system certificate database to verify
302 * peer certificates. This is %TRUE by default. If set to %FALSE, then
303 * peer certificate validation will always set the
304 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
305 * #GTlsConnection::accept-certificate will always be emitted on
306 * client-side connections, unless that bit is not set in
307 * #GTlsClientConnection:validation-flags).
312 g_tls_connection_set_use_system_certdb (GTlsConnection *conn,
313 gboolean use_system_certdb)
315 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
317 g_object_set (G_OBJECT (conn),
318 "use-system-certdb", use_system_certdb,
323 * g_tls_connection_get_use_system_certdb:
324 * @conn: a #GTlsConnection
326 * Gets whether @conn uses the system certificate database to verify
327 * peer certificates. See g_tls_connection_set_use_system_certdb().
329 * Return value: whether @conn uses the system certificate database
334 g_tls_connection_get_use_system_certdb (GTlsConnection *conn)
336 gboolean use_system_certdb;
338 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
340 g_object_get (G_OBJECT (conn),
341 "use-system-certdb", &use_system_certdb,
343 return use_system_certdb;
347 * g_tls_connection_set_certificate:
348 * @conn: a #GTlsConnection
349 * @certificate: the certificate to use for @conn
351 * This sets the certificate that @conn will present to its peer
352 * during the TLS handshake. For a #GTlsServerConnection, it is
353 * mandatory to set this, and that will normally be done at construct
356 * For a #GTlsClientConnection, this is optional. If a handshake fails
357 * with %G_TLS_ERROR_CERTIFICATE_REQUIRED, that means that the server
358 * requires a certificate, and if you try connecting again, you should
359 * call this method first. You can call
360 * g_tls_client_connection_get_accepted_cas() on the failed connection
361 * to get a list of Certificate Authorities that the server will
362 * accept certificates from.
364 * (It is also possible that a server will allow the connection with
365 * or without a certificate; in that case, if you don't provide a
366 * certificate, you can tell that the server requested one by the fact
367 * that g_tls_client_connection_get_accepted_cas() will return
373 g_tls_connection_set_certificate (GTlsConnection *conn,
374 GTlsCertificate *certificate)
376 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
377 g_return_if_fail (G_IS_TLS_CERTIFICATE (certificate));
379 g_object_set (G_OBJECT (conn), "certificate", certificate, NULL);
383 * g_tls_connection_get_certificate:
384 * @conn: a #GTlsConnection
386 * Gets @conn's certificate, as set by
387 * g_tls_connection_set_certificate().
389 * Return value: (transfer none): @conn's certificate, or %NULL
394 g_tls_connection_get_certificate (GTlsConnection *conn)
396 GTlsCertificate *certificate;
398 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
400 g_object_get (G_OBJECT (conn), "certificate", &certificate, NULL);
402 g_object_unref (certificate);
408 * g_tls_connection_get_peer_certificate:
409 * @conn: a #GTlsConnection
411 * Gets @conn's peer's certificate after the handshake has completed.
412 * (It is not set during the emission of
413 * #GTlsConnection::accept-certificate.)
415 * Return value: (transfer none): @conn's peer's certificate, or %NULL
420 g_tls_connection_get_peer_certificate (GTlsConnection *conn)
422 GTlsCertificate *peer_certificate;
424 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
426 g_object_get (G_OBJECT (conn), "peer-certificate", &peer_certificate, NULL);
427 if (peer_certificate)
428 g_object_unref (peer_certificate);
430 return peer_certificate;
434 * g_tls_connection_get_peer_certificate_errors:
435 * @conn: a #GTlsConnection
437 * Gets the errors associated with validating @conn's peer's
438 * certificate, after the handshake has completed. (It is not set
439 * during the emission of #GTlsConnection::accept-certificate.)
441 * Return value: @conn's peer's certificate errors
446 g_tls_connection_get_peer_certificate_errors (GTlsConnection *conn)
448 GTlsCertificateFlags errors;
450 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), 0);
452 g_object_get (G_OBJECT (conn), "peer-certificate-errors", &errors, NULL);
457 * g_tls_connection_set_require_close_notify:
458 * @conn: a #GTlsConnection
459 * @require_close_notify: whether or not to require close notification
461 * Sets whether or not @conn expects a proper TLS close notification
462 * before the connection is closed. If this is %TRUE (the default),
463 * then @conn will expect to receive a TLS close notification from its
464 * peer before the connection is closed, and will return a
465 * %G_TLS_ERROR_EOF error if the connection is closed without proper
466 * notification (since this may indicate a network error, or
467 * man-in-the-middle attack).
469 * In some protocols, the application will know whether or not the
470 * connection was closed cleanly based on application-level data
471 * (because the application-level data includes a length field, or is
472 * somehow self-delimiting); in this case, the close notify is
473 * redundant and sometimes omitted. (TLS 1.1 explicitly allows this;
474 * in TLS 1.0 it is technically an error, but often done anyway.) You
475 * can use g_tls_connection_set_require_close_notify() to tell @conn
476 * to allow an "unannounced" connection close, in which case the close
477 * will show up as a 0-length read, as in a non-TLS
478 * #GSocketConnection, and it is up to the application to check that
479 * the data has been fully received.
481 * Note that this only affects the behavior when the peer closes the
482 * connection; when the application calls g_io_stream_close() itself
483 * on @conn, this will send a close notification regardless of the
484 * setting of this property. If you explicitly want to do an unclean
485 * close, you can close @conn's #GTlsConnection:base-io-stream rather
486 * than closing @conn itself.
491 g_tls_connection_set_require_close_notify (GTlsConnection *conn,
492 gboolean require_close_notify)
494 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
496 g_object_set (G_OBJECT (conn),
497 "require-close-notify", require_close_notify,
502 * g_tls_connection_get_require_close_notify:
503 * @conn: a #GTlsConnection
505 * Tests whether or not @conn expects a proper TLS close notification
506 * when the connection is closed. See
507 * g_tls_connection_set_require_close_notify() for details.
509 * Return value: %TRUE if @conn requires a proper TLS close
515 g_tls_connection_get_require_close_notify (GTlsConnection *conn)
517 gboolean require_close_notify;
519 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
521 g_object_get (G_OBJECT (conn),
522 "require-close-notify", &require_close_notify,
524 return require_close_notify;
528 * g_tls_connection_set_rehandshake_mode:
529 * @conn: a #GTlsConnection
530 * @mode: the rehandshaking mode
532 * Sets how @conn behaves with respect to rehandshaking requests.
534 * %G_TLS_REHANDSHAKE_NEVER means that it will never agree to
535 * rehandshake after the initial handshake is complete. (For a client,
536 * this means it will refuse rehandshake requests from the server, and
537 * for a server, this means it will close the connection with an error
538 * if the client attempts to rehandshake.)
540 * %G_TLS_REHANDSHAKE_SAFELY means that the connection will allow a
541 * rehandshake only if the other end of the connection supports the
542 * TLS <literal>renegotiation_info</literal> extension. This is the
543 * default behavior, but means that rehandshaking will not work
544 * against older implementations that do not support that extension.
546 * %G_TLS_REHANDSHAKE_UNSAFELY means that the connection will allow
547 * rehandshaking even without the
548 * <literal>renegotiation_info</literal> extension. On the server side
549 * in particular, this is not recommended, since it leaves the server
550 * open to certain attacks. However, this mode is necessary if you
551 * need to allow renegotiation with older client software.
556 g_tls_connection_set_rehandshake_mode (GTlsConnection *conn,
557 GTlsRehandshakeMode mode)
559 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
561 g_object_set (G_OBJECT (conn),
562 "rehandshake-mode", mode,
567 * g_tls_connection_get_rehandshake_mode:
568 * @conn: a #GTlsConnection
570 * Gets @conn rehandshaking mode. See
571 * g_tls_connection_set_rehandshake_mode() for details.
573 * Return value: @conn's rehandshaking mode
578 g_tls_connection_get_rehandshake_mode (GTlsConnection *conn)
580 GTlsRehandshakeMode mode;
582 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), G_TLS_REHANDSHAKE_NEVER);
584 g_object_get (G_OBJECT (conn),
585 "rehandshake-mode", &mode,
591 * g_tls_connection_handshake:
592 * @conn: a #GTlsConnection
593 * @cancellable: a #GCancellable, or %NULL
594 * @error: a #GError, or %NULL
596 * Attempts a TLS handshake on @conn.
598 * On the client side, it is never necessary to call this method;
599 * although the connection needs to perform a handshake after
600 * connecting (or after sending a "STARTTLS"-type command) and may
601 * need to rehandshake later if the server requests it,
602 * #GTlsConnection will handle this for you automatically when you try
603 * to send or receive data on the connection. However, you can call
604 * g_tls_connection_handshake() manually if you want to know for sure
605 * whether the initial handshake succeeded or failed (as opposed to
606 * just immediately trying to write to @conn's output stream, in which
607 * case if it fails, it may not be possible to tell if it failed
608 * before or after completing the handshake).
610 * Likewise, on the server side, although a handshake is necessary at
611 * the beginning of the communication, you do not need to call this
612 * function explicitly unless you want clearer error reporting.
613 * However, you may call g_tls_connection_handshake() later on to
614 * renegotiate parameters (encryption methods, etc) with the client.
616 * #GTlsConnection::accept_certificate may be emitted during the
619 * Return value: success or failure
624 g_tls_connection_handshake (GTlsConnection *conn,
625 GCancellable *cancellable,
628 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
630 return G_TLS_CONNECTION_GET_CLASS (conn)->handshake (conn, cancellable, error);
634 * g_tls_connection_handshake_async:
635 * @conn: a #GTlsConnection
636 * @io_priority: the <link linkend="io-priority">I/O priority</link>
638 * @cancellable: a #GCancellable, or %NULL
639 * @callback: callback to call when the handshake is complete
640 * @user_data: the data to pass to the callback function
642 * Asynchronously performs a TLS handshake on @conn. See
643 * g_tls_connection_handshake() for more information.
648 g_tls_connection_handshake_async (GTlsConnection *conn,
650 GCancellable *cancellable,
651 GAsyncReadyCallback callback,
654 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
656 G_TLS_CONNECTION_GET_CLASS (conn)->handshake_async (conn, io_priority,
658 callback, user_data);
662 * g_tls_connection_handshake_finish:
663 * @conn: a #GTlsConnection
664 * @result: a #GAsyncResult.
665 * @error: a #GError pointer, or %NULL
667 * Finish an asynchronous TLS handshake operation. See
668 * g_tls_connection_handshake() for more information.
670 * Return value: %TRUE on success, %FALSE on failure, in which
671 * case @error will be set.
676 g_tls_connection_handshake_finish (GTlsConnection *conn,
677 GAsyncResult *result,
680 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
682 return G_TLS_CONNECTION_GET_CLASS (conn)->handshake_finish (conn, result, error);
688 * Gets the TLS error quark.
690 * Return value: a #GQuark.
695 g_tls_error_quark (void)
697 return g_quark_from_static_string ("g-tls-error-quark");
702 * g_tls_connection_emit_accept_certificate:
703 * @conn: a #GTlsConnection
704 * @peer_cert: the peer's #GTlsCertificate
705 * @errors: the problems with @peer_cert
707 * Used by #GTlsConnection implementations to emit the
708 * #GTlsConnection::accept-certificate signal.
710 * Return value: %TRUE if one of the signal handlers has returned
711 * %TRUE to accept @peer_cert
716 g_tls_connection_emit_accept_certificate (GTlsConnection *conn,
717 GTlsCertificate *peer_cert,
718 GTlsCertificateFlags errors)
720 gboolean accept = FALSE;
722 g_signal_emit (conn, signals[ACCEPT_CERTIFICATE], 0,
723 peer_cert, errors, &accept);