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"
27 #include "gio-marshal.h"
29 #include "gtlsbackend.h"
30 #include "gtlscertificate.h"
31 #include "gtlsclientconnection.h"
35 * SECTION:gtlsconnection
36 * @short_description: TLS connection type
39 * #GTlsConnection is the base TLS connection class type, which wraps
40 * a #GIOStream and provides TLS encryption on top of it. Its
41 * subclasses, #GTlsClientConnection and #GTlsServerConnection,
42 * implement client-side and server-side TLS, respectively.
50 * Abstract base class for the backend-specific #GTlsClientConnection
51 * and #GTlsServerConnection types.
56 G_DEFINE_ABSTRACT_TYPE (GTlsConnection, g_tls_connection, G_TYPE_IO_STREAM)
58 static void g_tls_connection_get_property (GObject *object,
62 static void g_tls_connection_set_property (GObject *object,
73 static guint signals[LAST_SIGNAL] = { 0 };
78 PROP_REQUIRE_CLOSE_NOTIFY,
79 PROP_REHANDSHAKE_MODE,
80 PROP_USE_SYSTEM_CERTDB,
82 PROP_PEER_CERTIFICATE,
83 PROP_PEER_CERTIFICATE_ERRORS
87 g_tls_connection_class_init (GTlsConnectionClass *klass)
89 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
91 gobject_class->get_property = g_tls_connection_get_property;
92 gobject_class->set_property = g_tls_connection_set_property;
95 * GTlsConnection:base-io-stream:
97 * The #GIOStream that the connection wraps
101 g_object_class_install_property (gobject_class, PROP_BASE_IO_STREAM,
102 g_param_spec_object ("base-io-stream",
104 P_("The GIOStream that the connection wraps"),
107 G_PARAM_CONSTRUCT_ONLY |
108 G_PARAM_STATIC_STRINGS));
110 * GTlsConnection:use-system-certdb:
112 * Whether or not the system certificate database will be used to
113 * verify peer certificates. See
114 * g_tls_connection_set_use_system_certdb().
118 g_object_class_install_property (gobject_class, PROP_USE_SYSTEM_CERTDB,
119 g_param_spec_boolean ("use-system-certdb",
120 P_("Use system certificate database"),
121 P_("Whether to verify peer certificates against the system certificate database"),
125 G_PARAM_STATIC_STRINGS));
127 * GTlsConnection:require-close-notify:
129 * Whether or not proper TLS close notification is required.
130 * See g_tls_connection_set_require_close_notify().
134 g_object_class_install_property (gobject_class, PROP_REQUIRE_CLOSE_NOTIFY,
135 g_param_spec_boolean ("require-close-notify",
136 P_("Require close notify"),
137 P_("Whether to require proper TLS close notification"),
141 G_PARAM_STATIC_STRINGS));
143 * GTlsConnection:rehandshake-mode:
145 * The rehandshaking mode. See
146 * g_tls_connection_set_rehandshake_mode().
150 g_object_class_install_property (gobject_class, PROP_REHANDSHAKE_MODE,
151 g_param_spec_enum ("rehandshake-mode",
152 P_("Rehandshake mode"),
153 P_("When to allow rehandshaking"),
154 G_TYPE_TLS_REHANDSHAKE_MODE,
155 G_TLS_REHANDSHAKE_SAFELY,
158 G_PARAM_STATIC_STRINGS));
160 * GTlsConnection:certificate:
162 * The connection's certificate; see
163 * g_tls_connection_set_certificate().
167 g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
168 g_param_spec_object ("certificate",
170 P_("The connection's certificate"),
171 G_TYPE_TLS_CERTIFICATE,
173 G_PARAM_STATIC_STRINGS));
175 * GTlsConnection:peer-certificate:
177 * The connection's peer's certificate, after the TLS handshake has
178 * completed and the certificate has been accepted. Note in
179 * particular that this is not yet set during the emission of
180 * #GTlsConnection::accept-certificate.
182 * (You can watch for a #GObject::notify signal on this property to
183 * detect when a handshake has occurred.)
187 g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE,
188 g_param_spec_object ("peer-certificate",
189 P_("Peer Certificate"),
190 P_("The connection's peer's certificate"),
191 G_TYPE_TLS_CERTIFICATE,
193 G_PARAM_STATIC_STRINGS));
195 * GTlsConnection:peer-certificate-errors:
197 * The errors noticed-and-ignored while verifying
198 * #GTlsConnection:peer-certificate. Normally this should be 0, but
199 * it may not be if #GTlsClientConnection:validation-flags is not
200 * %G_TLS_CERTIFICATE_VALIDATE_ALL, or if
201 * #GTlsConnection::accept-certificate overrode the default
206 g_object_class_install_property (gobject_class, PROP_PEER_CERTIFICATE_ERRORS,
207 g_param_spec_flags ("peer-certificate-errors",
208 P_("Peer Certificate Errors"),
209 P_("Errors found with the peer's certificate"),
210 G_TYPE_TLS_CERTIFICATE_FLAGS,
213 G_PARAM_STATIC_STRINGS));
216 * GTlsConnection::accept-certificate:
217 * @conn: a #GTlsConnection
218 * @peer_cert: the peer's #GTlsCertificate
219 * @errors: the problems with @peer_cert.
221 * Emitted during the TLS handshake after the peer certificate has
222 * been received. You can examine @peer_cert's certification path by
223 * calling g_tls_certificate_get_issuer() on it.
225 * For a client-side connection, @peer_cert is the server's
226 * certificate, and the signal will only be emitted if the
227 * certificate was not acceptable according to @conn's
228 * #GTlsClientConnection:validation_flags. If you would like the
229 * certificate to be accepted despite @errors, return %TRUE from the
230 * signal handler. Otherwise, if no handler accepts the certificate,
231 * the handshake will fail with %G_TLS_ERROR_BAD_CERTIFICATE.
233 * For a server-side connection, @peer_cert is the certificate
234 * presented by the client, if this was requested via the server's
235 * #GTlsServerConnection:authentication_mode. On the server side,
236 * the signal is always emitted when the client presents a
237 * certificate, and the certificate will only be accepted if a
238 * handler returns %TRUE.
240 * Note that if this signal is emitted as part of asynchronous I/O
241 * in the main thread, then you should not attempt to interact with
242 * the user before returning from the signal handler. If you want to
243 * let the user decide whether or not to accept the certificate, you
244 * would have to return %FALSE from the signal handler on the first
245 * attempt, and then after the connection attempt returns a
246 * %G_TLS_ERROR_HANDSHAKE, you can interact with the user, and if
247 * the user decides to accept the certificate, remember that fact,
248 * create a new connection, and return %TRUE from the signal handler
251 * If you are doing I/O in another thread, you do not
252 * need to worry about this, and can simply block in the signal
253 * handler until the UI thread returns an answer.
255 * Return value: %TRUE to accept @peer_cert (which will also
256 * immediately end the signal emission). %FALSE to allow the signal
257 * emission to continue, which will cause the handshake to fail if
258 * no one else overrides it.
262 signals[ACCEPT_CERTIFICATE] =
263 g_signal_new (I_("accept-certificate"),
264 G_TYPE_TLS_CONNECTION,
266 G_STRUCT_OFFSET (GTlsConnectionClass, accept_certificate),
267 g_signal_accumulator_true_handled, NULL,
268 _gio_marshal_BOOLEAN__OBJECT_FLAGS,
270 G_TYPE_TLS_CERTIFICATE,
271 G_TYPE_TLS_CERTIFICATE_FLAGS);
275 g_tls_connection_init (GTlsConnection *conn)
280 g_tls_connection_get_property (GObject *object,
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 g_tls_connection_set_property (GObject *object,
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298 * g_tls_connection_set_use_system_certdb:
299 * @conn: a #GTlsConnection
300 * @use_system_certdb: whether to use the system certificate database
302 * Sets whether @conn uses the system certificate database to verify
303 * peer certificates. This is %TRUE by default. If set to %FALSE, then
304 * peer certificate validation will always set the
305 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning
306 * #GTlsConnection::accept-certificate will always be emitted on
307 * client-side connections, unless that bit is not set in
308 * #GTlsClientConnection:validation-flags).
313 g_tls_connection_set_use_system_certdb (GTlsConnection *conn,
314 gboolean use_system_certdb)
316 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
318 g_object_set (G_OBJECT (conn),
319 "use-system-certdb", use_system_certdb,
324 * g_tls_connection_get_use_system_certdb:
325 * @conn: a #GTlsConnection
327 * Gets whether @conn uses the system certificate database to verify
328 * peer certificates. See g_tls_connection_set_use_system_certdb().
330 * Return value: whether @conn uses the system certificate database
335 g_tls_connection_get_use_system_certdb (GTlsConnection *conn)
337 gboolean use_system_certdb;
339 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
341 g_object_get (G_OBJECT (conn),
342 "use-system-certdb", &use_system_certdb,
344 return use_system_certdb;
348 * g_tls_connection_set_certificate:
349 * @conn: a #GTlsConnection
350 * @certificate: the certificate to use for @conn
352 * This sets the certificate that @conn will present to its peer
353 * during the TLS handshake. For a #GTlsServerConnection, it is
354 * mandatory to set this, and that will normally be done at construct
357 * For a #GTlsClientConnection, this is optional. If a handshake fails
358 * with %G_TLS_ERROR_CERTIFICATE_REQUIRED, that means that the server
359 * requires a certificate, and if you try connecting again, you should
360 * call this method first. You can call
361 * g_tls_client_connection_get_accepted_cas() on the failed connection
362 * to get a list of Certificate Authorities that the server will
363 * accept certificates from.
365 * (It is also possible that a server will allow the connection with
366 * or without a certificate; in that case, if you don't provide a
367 * certificate, you can tell that the server requested one by the fact
368 * that g_tls_client_connection_get_accepted_cas() will return
374 g_tls_connection_set_certificate (GTlsConnection *conn,
375 GTlsCertificate *certificate)
377 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
378 g_return_if_fail (G_IS_TLS_CERTIFICATE (certificate));
380 g_object_set (G_OBJECT (conn), "certificate", certificate, NULL);
384 * g_tls_connection_get_certificate:
385 * @conn: a #GTlsConnection
387 * Gets @conn's certificate, as set by
388 * g_tls_connection_set_certificate().
390 * Return value: (transfer none): @conn's certificate, or %NULL
395 g_tls_connection_get_certificate (GTlsConnection *conn)
397 GTlsCertificate *certificate;
399 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
401 g_object_get (G_OBJECT (conn), "certificate", &certificate, NULL);
403 g_object_unref (certificate);
409 * g_tls_connection_get_peer_certificate:
410 * @conn: a #GTlsConnection
412 * Gets @conn's peer's certificate after the handshake has completed.
413 * (It is not set during the emission of
414 * #GTlsConnection::accept-certificate.)
416 * Return value: (transfer none): @conn's peer's certificate, or %NULL
421 g_tls_connection_get_peer_certificate (GTlsConnection *conn)
423 GTlsCertificate *peer_certificate;
425 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), NULL);
427 g_object_get (G_OBJECT (conn), "peer-certificate", &peer_certificate, NULL);
428 if (peer_certificate)
429 g_object_unref (peer_certificate);
431 return peer_certificate;
435 * g_tls_connection_get_peer_certificate_errors:
436 * @conn: a #GTlsConnection
438 * Gets the errors associated with validating @conn's peer's
439 * certificate, after the handshake has completed. (It is not set
440 * during the emission of #GTlsConnection::accept-certificate.)
442 * Return value: @conn's peer's certificate errors
447 g_tls_connection_get_peer_certificate_errors (GTlsConnection *conn)
449 GTlsCertificateFlags errors;
451 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), 0);
453 g_object_get (G_OBJECT (conn), "peer-certificate-errors", &errors, NULL);
458 * g_tls_connection_set_require_close_notify:
459 * @conn: a #GTlsConnection
460 * @require_close_notify: whether or not to require close notification
462 * Sets whether or not @conn expects a proper TLS close notification
463 * before the connection is closed. If this is %TRUE (the default),
464 * then @conn will expect to receive a TLS close notification from its
465 * peer before the connection is closed, and will return a
466 * %G_TLS_ERROR_EOF error if the connection is closed without proper
467 * notification (since this may indicate a network error, or
468 * man-in-the-middle attack).
470 * In some protocols, the application will know whether or not the
471 * connection was closed cleanly based on application-level data
472 * (because the application-level data includes a length field, or is
473 * somehow self-delimiting); in this case, the close notify is
474 * redundant and sometimes omitted. (TLS 1.1 explicitly allows this;
475 * in TLS 1.0 it is technically an error, but often done anyway.) You
476 * can use g_tls_connection_set_require_close_notify() to tell @conn
477 * to allow an "unannounced" connection close, in which case the close
478 * will show up as a 0-length read, as in a non-TLS
479 * #GSocketConnection, and it is up to the application to check that
480 * the data has been fully received.
482 * Note that this only affects the behavior when the peer closes the
483 * connection; when the application calls g_io_stream_close() itself
484 * on @conn, this will send a close notification regardless of the
485 * setting of this property. If you explicitly want to do an unclean
486 * close, you can close @conn's #GTlsConnection:base-io-stream rather
487 * than closing @conn itself.
492 g_tls_connection_set_require_close_notify (GTlsConnection *conn,
493 gboolean require_close_notify)
495 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
497 g_object_set (G_OBJECT (conn),
498 "require-close-notify", require_close_notify,
503 * g_tls_connection_get_require_close_notify:
504 * @conn: a #GTlsConnection
506 * Tests whether or not @conn expects a proper TLS close notification
507 * when the connection is closed. See
508 * g_tls_connection_set_require_close_notify() for details.
510 * Return value: %TRUE if @conn requires a proper TLS close
516 g_tls_connection_get_require_close_notify (GTlsConnection *conn)
518 gboolean require_close_notify;
520 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), TRUE);
522 g_object_get (G_OBJECT (conn),
523 "require-close-notify", &require_close_notify,
525 return require_close_notify;
529 * g_tls_connection_set_rehandshake_mode:
530 * @conn: a #GTlsConnection
531 * @mode: the rehandshaking mode
533 * Sets how @conn behaves with respect to rehandshaking requests.
535 * %G_TLS_REHANDSHAKE_NEVER means that it will never agree to
536 * rehandshake after the initial handshake is complete. (For a client,
537 * this means it will refuse rehandshake requests from the server, and
538 * for a server, this means it will close the connection with an error
539 * if the client attempts to rehandshake.)
541 * %G_TLS_REHANDSHAKE_SAFELY means that the connection will allow a
542 * rehandshake only if the other end of the connection supports the
543 * TLS <literal>renegotiation_info</literal> extension. This is the
544 * default behavior, but means that rehandshaking will not work
545 * against older implementations that do not support that extension.
547 * %G_TLS_REHANDSHAKE_UNSAFELY means that the connection will allow
548 * rehandshaking even without the
549 * <literal>renegotiation_info</literal> extension. On the server side
550 * in particular, this is not recommended, since it leaves the server
551 * open to certain attacks. However, this mode is necessary if you
552 * need to allow renegotiation with older client software.
557 g_tls_connection_set_rehandshake_mode (GTlsConnection *conn,
558 GTlsRehandshakeMode mode)
560 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
562 g_object_set (G_OBJECT (conn),
563 "rehandshake-mode", mode,
568 * g_tls_connection_get_rehandshake_mode:
569 * @conn: a #GTlsConnection
571 * Gets @conn rehandshaking mode. See
572 * g_tls_connection_set_rehandshake_mode() for details.
574 * Return value: @conn's rehandshaking mode
579 g_tls_connection_get_rehandshake_mode (GTlsConnection *conn)
581 GTlsRehandshakeMode mode;
583 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), G_TLS_REHANDSHAKE_NEVER);
585 g_object_get (G_OBJECT (conn),
586 "rehandshake-mode", &mode,
592 * g_tls_connection_handshake:
593 * @conn: a #GTlsConnection
594 * @cancellable: a #GCancellable, or %NULL
595 * @error: a #GError, or %NULL
597 * Attempts a TLS handshake on @conn.
599 * On the client side, it is never necessary to call this method;
600 * although the connection needs to perform a handshake after
601 * connecting (or after sending a "STARTTLS"-type command) and may
602 * need to rehandshake later if the server requests it,
603 * #GTlsConnection will handle this for you automatically when you try
604 * to send or receive data on the connection. However, you can call
605 * g_tls_connection_handshake() manually if you want to know for sure
606 * whether the initial handshake succeeded or failed (as opposed to
607 * just immediately trying to write to @conn's output stream, in which
608 * case if it fails, it may not be possible to tell if it failed
609 * before or after completing the handshake).
611 * Likewise, on the server side, although a handshake is necessary at
612 * the beginning of the communication, you do not need to call this
613 * function explicitly unless you want clearer error reporting.
614 * However, you may call g_tls_connection_handshake() later on to
615 * renegotiate parameters (encryption methods, etc) with the client.
617 * #GTlsConnection::accept_certificate may be emitted during the
620 * Return value: success or failure
625 g_tls_connection_handshake (GTlsConnection *conn,
626 GCancellable *cancellable,
629 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
631 return G_TLS_CONNECTION_GET_CLASS (conn)->handshake (conn, cancellable, error);
635 * g_tls_connection_handshake_async:
636 * @conn: a #GTlsConnection
637 * @io_priority: the <link linkend="io-priority">I/O priority</link>
639 * @cancellable: a #GCancellable, or %NULL
640 * @callback: callback to call when the handshake is complete
641 * @user_data: the data to pass to the callback function
643 * Asynchronously performs a TLS handshake on @conn. See
644 * g_tls_connection_handshake() for more information.
649 g_tls_connection_handshake_async (GTlsConnection *conn,
651 GCancellable *cancellable,
652 GAsyncReadyCallback callback,
655 g_return_if_fail (G_IS_TLS_CONNECTION (conn));
657 G_TLS_CONNECTION_GET_CLASS (conn)->handshake_async (conn, io_priority,
659 callback, user_data);
663 * g_tls_connection_handshake_finish:
664 * @conn: a #GTlsConnection
665 * @result: a #GAsyncResult.
666 * @error: a #GError pointer, or %NULL
668 * Finish an asynchronous TLS handshake operation. See
669 * g_tls_connection_handshake() for more information.
671 * Return value: %TRUE on success, %FALSE on failure, in which
672 * case @error will be set.
677 g_tls_connection_handshake_finish (GTlsConnection *conn,
678 GAsyncResult *result,
681 g_return_val_if_fail (G_IS_TLS_CONNECTION (conn), FALSE);
683 return G_TLS_CONNECTION_GET_CLASS (conn)->handshake_finish (conn, result, error);
689 * Gets the TLS error quark.
691 * Return value: a #GQuark.
696 g_tls_error_quark (void)
698 return g_quark_from_static_string ("g-tls-error-quark");
703 * g_tls_connection_emit_accept_certificate:
704 * @conn: a #GTlsConnection
705 * @peer_cert: the peer's #GTlsCertificate
706 * @errors: the problems with @peer_cert
708 * Used by #GTlsConnection implementations to emit the
709 * #GTlsConnection::accept-certificate signal.
711 * Return value: %TRUE if one of the signal handlers has returned
712 * %TRUE to accept @peer_cert
717 g_tls_connection_emit_accept_certificate (GTlsConnection *conn,
718 GTlsCertificate *peer_cert,
719 GTlsCertificateFlags errors)
721 gboolean accept = FALSE;
723 g_signal_emit (conn, signals[ACCEPT_CERTIFICATE], 0,
724 peer_cert, errors, &accept);