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.1 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, see <http://www.gnu.org/licenses/>.
22 #include "gtlsclientconnection.h"
23 #include "ginitable.h"
24 #include "gioenumtypes.h"
26 #include "gsocketconnectable.h"
27 #include "gtlsbackend.h"
28 #include "gtlscertificate.h"
32 * SECTION:gtlsclientconnection
33 * @short_description: TLS client-side connection
36 * #GTlsClientConnection is the client-side subclass of
37 * #GTlsConnection, representing a client-side TLS connection.
41 * GTlsClientConnection:
43 * Abstract base class for the backend-specific client connection
49 G_DEFINE_INTERFACE (GTlsClientConnection, g_tls_client_connection, G_TYPE_TLS_CONNECTION)
52 g_tls_client_connection_default_init (GTlsClientConnectionInterface *iface)
55 * GTlsClientConnection:validation-flags:
57 * What steps to perform when validating a certificate received from
58 * a server. Server certificates that fail to validate in any of the
59 * ways indicated here will be rejected unless the application
60 * overrides the default via #GTlsConnection::accept-certificate.
64 g_object_interface_install_property (iface,
65 g_param_spec_flags ("validation-flags",
66 P_("Validation flags"),
67 P_("What certificate validation to perform"),
68 G_TYPE_TLS_CERTIFICATE_FLAGS,
69 G_TLS_CERTIFICATE_VALIDATE_ALL,
72 G_PARAM_STATIC_STRINGS));
75 * GTlsClientConnection:server-identity:
77 * A #GSocketConnectable describing the identity of the server that
78 * is expected on the other end of the connection.
80 * If the %G_TLS_CERTIFICATE_BAD_IDENTITY flag is set in
81 * #GTlsClientConnection:validation-flags, this object will be used
82 * to determine the expected identify of the remote end of the
83 * connection; if #GTlsClientConnection:server-identity is not set,
84 * or does not match the identity presented by the server, then the
85 * %G_TLS_CERTIFICATE_BAD_IDENTITY validation will fail.
87 * In addition to its use in verifying the server certificate,
88 * this is also used to give a hint to the server about what
89 * certificate we expect, which is useful for servers that serve
94 g_object_interface_install_property (iface,
95 g_param_spec_object ("server-identity",
96 P_("Server identity"),
97 P_("GSocketConnectable identifying the server"),
98 G_TYPE_SOCKET_CONNECTABLE,
101 G_PARAM_STATIC_STRINGS));
104 * GTlsClientConnection:use-ssl3:
106 * SSL 3.0 is no longer supported. See
107 * g_tls_client_connection_set_use_ssl3() for details.
111 * Deprecated: 2.56: SSL 3.0 is insecure.
113 g_object_interface_install_property (iface,
114 g_param_spec_boolean ("use-ssl3",
116 P_("Use fallback version of SSL/TLS rather than most recent version"),
120 G_PARAM_STATIC_STRINGS |
121 G_PARAM_DEPRECATED));
124 * GTlsClientConnection:accepted-cas: (type GLib.List) (element-type GLib.ByteArray)
126 * A list of the distinguished names of the Certificate Authorities
127 * that the server will accept client certificates signed by. If the
128 * server requests a client certificate during the handshake, then
129 * this property will be set after the handshake completes.
131 * Each item in the list is a #GByteArray which contains the complete
132 * subject DN of the certificate authority.
136 g_object_interface_install_property (iface,
137 g_param_spec_pointer ("accepted-cas",
139 P_("Distinguished names of the CAs the server accepts certificates from"),
141 G_PARAM_STATIC_STRINGS));
145 * g_tls_client_connection_new:
146 * @base_io_stream: the #GIOStream to wrap
147 * @server_identity: (nullable): the expected identity of the server
148 * @error: #GError for error reporting, or %NULL to ignore.
150 * Creates a new #GTlsClientConnection wrapping @base_io_stream (which
151 * must have pollable input and output streams) which is assumed to
152 * communicate with the server identified by @server_identity.
154 * See the documentation for #GTlsConnection:base-io-stream for restrictions
155 * on when application code can run operations on the @base_io_stream after
156 * this function has returned.
158 * Returns: (transfer full) (type GTlsClientConnection): the new
159 * #GTlsClientConnection, or %NULL on error
164 g_tls_client_connection_new (GIOStream *base_io_stream,
165 GSocketConnectable *server_identity,
169 GTlsBackend *backend;
171 backend = g_tls_backend_get_default ();
172 conn = g_initable_new (g_tls_backend_get_client_connection_type (backend),
174 "base-io-stream", base_io_stream,
175 "server-identity", server_identity,
177 return G_IO_STREAM (conn);
181 * g_tls_client_connection_get_validation_flags:
182 * @conn: the #GTlsClientConnection
184 * Gets @conn's validation flags
186 * Returns: the validation flags
191 g_tls_client_connection_get_validation_flags (GTlsClientConnection *conn)
193 GTlsCertificateFlags flags = 0;
195 g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
197 g_object_get (G_OBJECT (conn), "validation-flags", &flags, NULL);
202 * g_tls_client_connection_set_validation_flags:
203 * @conn: the #GTlsClientConnection
204 * @flags: the #GTlsCertificateFlags to use
206 * Sets @conn's validation flags, to override the default set of
207 * checks performed when validating a server certificate. By default,
208 * %G_TLS_CERTIFICATE_VALIDATE_ALL is used.
213 g_tls_client_connection_set_validation_flags (GTlsClientConnection *conn,
214 GTlsCertificateFlags flags)
216 g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
218 g_object_set (G_OBJECT (conn), "validation-flags", flags, NULL);
222 * g_tls_client_connection_get_server_identity:
223 * @conn: the #GTlsClientConnection
225 * Gets @conn's expected server identity
227 * Returns: (nullable) (transfer none): a #GSocketConnectable describing the
228 * expected server identity, or %NULL if the expected identity is not
234 g_tls_client_connection_get_server_identity (GTlsClientConnection *conn)
236 GSocketConnectable *identity = NULL;
238 g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
240 g_object_get (G_OBJECT (conn), "server-identity", &identity, NULL);
242 g_object_unref (identity);
247 * g_tls_client_connection_set_server_identity:
248 * @conn: the #GTlsClientConnection
249 * @identity: a #GSocketConnectable describing the expected server identity
251 * Sets @conn's expected server identity, which is used both to tell
252 * servers on virtual hosts which certificate to present, and also
253 * to let @conn know what name to look for in the certificate when
254 * performing %G_TLS_CERTIFICATE_BAD_IDENTITY validation, if enabled.
259 g_tls_client_connection_set_server_identity (GTlsClientConnection *conn,
260 GSocketConnectable *identity)
262 g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
264 g_object_set (G_OBJECT (conn), "server-identity", identity, NULL);
268 * g_tls_client_connection_get_use_ssl3:
269 * @conn: the #GTlsClientConnection
271 * SSL 3.0 is no longer supported. See
272 * g_tls_client_connection_set_use_ssl3() for details.
278 * Deprecated: 2.56: SSL 3.0 is insecure.
281 g_tls_client_connection_get_use_ssl3 (GTlsClientConnection *conn)
283 gboolean use_ssl3 = FALSE;
285 g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
287 g_object_get (G_OBJECT (conn), "use-ssl3", &use_ssl3, NULL);
292 * g_tls_client_connection_set_use_ssl3:
293 * @conn: the #GTlsClientConnection
294 * @use_ssl3: a #gboolean, ignored
296 * Since GLib 2.42.1, SSL 3.0 is no longer supported.
298 * From GLib 2.42.1 through GLib 2.62, this function could be used to
299 * force use of TLS 1.0, the lowest-supported TLS protocol version at
300 * the time. In the past, this was needed to connect to broken TLS
301 * servers that exhibited protocol version intolerance. Such servers
302 * are no longer common, and using TLS 1.0 is no longer considered
305 * Since GLib 2.64, this function does nothing.
309 * Deprecated: 2.56: SSL 3.0 is insecure.
312 g_tls_client_connection_set_use_ssl3 (GTlsClientConnection *conn,
315 g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
317 g_object_set (G_OBJECT (conn), "use-ssl3", FALSE, NULL);
321 * g_tls_client_connection_get_accepted_cas:
322 * @conn: the #GTlsClientConnection
324 * Gets the list of distinguished names of the Certificate Authorities
325 * that the server will accept certificates from. This will be set
326 * during the TLS handshake if the server requests a certificate.
327 * Otherwise, it will be %NULL.
329 * Each item in the list is a #GByteArray which contains the complete
330 * subject DN of the certificate authority.
332 * Returns: (element-type GByteArray) (transfer full): the list of
333 * CA DNs. You should unref each element with g_byte_array_unref() and then
334 * the free the list with g_list_free().
339 g_tls_client_connection_get_accepted_cas (GTlsClientConnection *conn)
341 GList *accepted_cas = NULL;
343 g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), NULL);
345 g_object_get (G_OBJECT (conn), "accepted-cas", &accepted_cas, NULL);
350 * g_tls_client_connection_copy_session_state:
351 * @conn: a #GTlsClientConnection
352 * @source: a #GTlsClientConnection
354 * Possibly copies session state from one connection to another, for use
355 * in TLS session resumption. This is not normally needed, but may be
356 * used when the same session needs to be used between different
357 * endpoints, as is required by some protocols, such as FTP over TLS.
358 * @source should have already completed a handshake and, since TLS 1.3,
359 * it should have been used to read data at least once. @conn should not
360 * have completed a handshake.
362 * It is not possible to know whether a call to this function will
363 * actually do anything. Because session resumption is normally used
364 * only for performance benefit, the TLS backend might not implement
365 * this function. Even if implemented, it may not actually succeed in
366 * allowing @conn to resume @source's TLS session, because the server
367 * may not have sent a session resumption token to @source, or it may
368 * refuse to accept the token from @conn. There is no way to know
369 * whether a call to this function is actually successful.
371 * Using this function is not required to benefit from session
372 * resumption. If the TLS backend supports session resumption, the
373 * session will be resumed automatically if it is possible to do so
374 * without weakening the privacy guarantees normally provided by TLS,
375 * without need to call this function. For example, with TLS 1.3,
376 * a session ticket will be automatically copied from any
377 * #GTlsClientConnection that has previously received session tickets
378 * from the server, provided a ticket is available that has not
379 * previously been used for session resumption, since session ticket
380 * reuse would be a privacy weakness. Using this function causes the
381 * ticket to be copied without regard for privacy considerations.
386 g_tls_client_connection_copy_session_state (GTlsClientConnection *conn,
387 GTlsClientConnection *source)
389 g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
390 g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (source));
391 g_return_if_fail (G_TLS_CLIENT_CONNECTION_GET_INTERFACE (conn)->copy_session_state != NULL);
393 G_TLS_CLIENT_CONNECTION_GET_INTERFACE (conn)->copy_session_state (conn,