• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.1 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, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "config.h"
20 #include "glib.h"
21 
22 #include "gtlsclientconnection.h"
23 #include "ginitable.h"
24 #include "gioenumtypes.h"
25 #include "gsocket.h"
26 #include "gsocketconnectable.h"
27 #include "gtlsbackend.h"
28 #include "gtlscertificate.h"
29 #include "glibintl.h"
30 
31 /**
32  * SECTION:gtlsclientconnection
33  * @short_description: TLS client-side connection
34  * @include: gio/gio.h
35  *
36  * #GTlsClientConnection is the client-side subclass of
37  * #GTlsConnection, representing a client-side TLS connection.
38  */
39 
40 /**
41  * GTlsClientConnection:
42  *
43  * Abstract base class for the backend-specific client connection
44  * type.
45  *
46  * Since: 2.28
47  */
48 
G_DEFINE_INTERFACE(GTlsClientConnection,g_tls_client_connection,G_TYPE_TLS_CONNECTION)49 G_DEFINE_INTERFACE (GTlsClientConnection, g_tls_client_connection, G_TYPE_TLS_CONNECTION)
50 
51 static void
52 g_tls_client_connection_default_init (GTlsClientConnectionInterface *iface)
53 {
54   /**
55    * GTlsClientConnection:validation-flags:
56    *
57    * What steps to perform when validating a certificate received from
58    * a server. Server certificates that fail to validate in all of the
59    * ways indicated here will be rejected unless the application
60    * overrides the default via #GTlsConnection::accept-certificate.
61    *
62    * Since: 2.28
63    */
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,
70 							   G_PARAM_READWRITE |
71 							   G_PARAM_CONSTRUCT |
72 							   G_PARAM_STATIC_STRINGS));
73 
74   /**
75    * GTlsClientConnection:server-identity:
76    *
77    * A #GSocketConnectable describing the identity of the server that
78    * is expected on the other end of the connection.
79    *
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.
86    *
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
90    * virtual hosts.
91    *
92    * Since: 2.28
93    */
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,
99 							    G_PARAM_READWRITE |
100 							    G_PARAM_CONSTRUCT |
101 							    G_PARAM_STATIC_STRINGS));
102 
103   /**
104    * GTlsClientConnection:use-ssl3:
105    *
106    * If %TRUE, forces the connection to use a fallback version of TLS
107    * or SSL, rather than trying to negotiate the best version of TLS
108    * to use. See g_tls_client_connection_set_use_ssl3().
109    *
110    * Since: 2.28
111    *
112    * Deprecated: 2.56: SSL 3.0 is insecure, and this property does not
113    * generally enable or disable it, despite its name.
114    */
115   g_object_interface_install_property (iface,
116 				       g_param_spec_boolean ("use-ssl3",
117 							     P_("Use fallback"),
118 							     P_("Use fallback version of SSL/TLS rather than most recent version"),
119 							     FALSE,
120 							     G_PARAM_READWRITE |
121 							     G_PARAM_CONSTRUCT |
122 							     G_PARAM_STATIC_STRINGS |
123 							     G_PARAM_DEPRECATED));
124 
125   /**
126    * GTlsClientConnection:accepted-cas: (type GLib.List) (element-type GLib.ByteArray)
127    *
128    * A list of the distinguished names of the Certificate Authorities
129    * that the server will accept client certificates signed by. If the
130    * server requests a client certificate during the handshake, then
131    * this property will be set after the handshake completes.
132    *
133    * Each item in the list is a #GByteArray which contains the complete
134    * subject DN of the certificate authority.
135    *
136    * Since: 2.28
137    */
138   g_object_interface_install_property (iface,
139 				       g_param_spec_pointer ("accepted-cas",
140 							     P_("Accepted CAs"),
141 							     P_("Distinguished names of the CAs the server accepts certificates from"),
142 							     G_PARAM_READABLE |
143 							     G_PARAM_STATIC_STRINGS));
144 }
145 
146 /**
147  * g_tls_client_connection_new:
148  * @base_io_stream: the #GIOStream to wrap
149  * @server_identity: (nullable): the expected identity of the server
150  * @error: #GError for error reporting, or %NULL to ignore.
151  *
152  * Creates a new #GTlsClientConnection wrapping @base_io_stream (which
153  * must have pollable input and output streams) which is assumed to
154  * communicate with the server identified by @server_identity.
155  *
156  * See the documentation for #GTlsConnection:base-io-stream for restrictions
157  * on when application code can run operations on the @base_io_stream after
158  * this function has returned.
159  *
160  * Returns: (transfer full) (type GTlsClientConnection): the new
161  * #GTlsClientConnection, or %NULL on error
162  *
163  * Since: 2.28
164  */
165 GIOStream *
g_tls_client_connection_new(GIOStream * base_io_stream,GSocketConnectable * server_identity,GError ** error)166 g_tls_client_connection_new (GIOStream           *base_io_stream,
167 			     GSocketConnectable  *server_identity,
168 			     GError             **error)
169 {
170   GObject *conn;
171   GTlsBackend *backend;
172 
173   backend = g_tls_backend_get_default ();
174   conn = g_initable_new (g_tls_backend_get_client_connection_type (backend),
175 			 NULL, error,
176 			 "base-io-stream", base_io_stream,
177 			 "server-identity", server_identity,
178 			 NULL);
179   return G_IO_STREAM (conn);
180 }
181 
182 /**
183  * g_tls_client_connection_get_validation_flags:
184  * @conn: the #GTlsClientConnection
185  *
186  * Gets @conn's validation flags
187  *
188  * Returns: the validation flags
189  *
190  * Since: 2.28
191  */
192 GTlsCertificateFlags
g_tls_client_connection_get_validation_flags(GTlsClientConnection * conn)193 g_tls_client_connection_get_validation_flags (GTlsClientConnection *conn)
194 {
195   GTlsCertificateFlags flags = 0;
196 
197   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
198 
199   g_object_get (G_OBJECT (conn), "validation-flags", &flags, NULL);
200   return flags;
201 }
202 
203 /**
204  * g_tls_client_connection_set_validation_flags:
205  * @conn: the #GTlsClientConnection
206  * @flags: the #GTlsCertificateFlags to use
207  *
208  * Sets @conn's validation flags, to override the default set of
209  * checks performed when validating a server certificate. By default,
210  * %G_TLS_CERTIFICATE_VALIDATE_ALL is used.
211  *
212  * Since: 2.28
213  */
214 void
g_tls_client_connection_set_validation_flags(GTlsClientConnection * conn,GTlsCertificateFlags flags)215 g_tls_client_connection_set_validation_flags (GTlsClientConnection  *conn,
216 					      GTlsCertificateFlags   flags)
217 {
218   g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
219 
220   g_object_set (G_OBJECT (conn), "validation-flags", flags, NULL);
221 }
222 
223 /**
224  * g_tls_client_connection_get_server_identity:
225  * @conn: the #GTlsClientConnection
226  *
227  * Gets @conn's expected server identity
228  *
229  * Returns: (transfer none): a #GSocketConnectable describing the
230  * expected server identity, or %NULL if the expected identity is not
231  * known.
232  *
233  * Since: 2.28
234  */
235 GSocketConnectable *
g_tls_client_connection_get_server_identity(GTlsClientConnection * conn)236 g_tls_client_connection_get_server_identity (GTlsClientConnection *conn)
237 {
238   GSocketConnectable *identity = NULL;
239 
240   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
241 
242   g_object_get (G_OBJECT (conn), "server-identity", &identity, NULL);
243   if (identity)
244     g_object_unref (identity);
245   return identity;
246 }
247 
248 /**
249  * g_tls_client_connection_set_server_identity:
250  * @conn: the #GTlsClientConnection
251  * @identity: a #GSocketConnectable describing the expected server identity
252  *
253  * Sets @conn's expected server identity, which is used both to tell
254  * servers on virtual hosts which certificate to present, and also
255  * to let @conn know what name to look for in the certificate when
256  * performing %G_TLS_CERTIFICATE_BAD_IDENTITY validation, if enabled.
257  *
258  * Since: 2.28
259  */
260 void
g_tls_client_connection_set_server_identity(GTlsClientConnection * conn,GSocketConnectable * identity)261 g_tls_client_connection_set_server_identity (GTlsClientConnection *conn,
262 					     GSocketConnectable   *identity)
263 {
264   g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
265 
266   g_object_set (G_OBJECT (conn), "server-identity", identity, NULL);
267 }
268 
269 /**
270  * g_tls_client_connection_get_use_ssl3:
271  * @conn: the #GTlsClientConnection
272  *
273  * Gets whether @conn will force the lowest-supported TLS protocol
274  * version rather than attempt to negotiate the highest mutually-
275  * supported version of TLS; see g_tls_client_connection_set_use_ssl3().
276  *
277  * Returns: whether @conn will use the lowest-supported TLS protocol version
278  *
279  * Since: 2.28
280  *
281  * Deprecated: 2.56: SSL 3.0 is insecure, and this function does not
282  * actually indicate whether it is enabled.
283  */
284 gboolean
g_tls_client_connection_get_use_ssl3(GTlsClientConnection * conn)285 g_tls_client_connection_get_use_ssl3 (GTlsClientConnection *conn)
286 {
287   gboolean use_ssl3 = FALSE;
288 
289   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
290 
291   g_object_get (G_OBJECT (conn), "use-ssl3", &use_ssl3, NULL);
292   return use_ssl3;
293 }
294 
295 /**
296  * g_tls_client_connection_set_use_ssl3:
297  * @conn: the #GTlsClientConnection
298  * @use_ssl3: whether to use the lowest-supported protocol version
299  *
300  * Since 2.42.1, if @use_ssl3 is %TRUE, this forces @conn to use the
301  * lowest-supported TLS protocol version rather than trying to properly
302  * negotiate the highest mutually-supported protocol version with the
303  * peer. Be aware that SSL 3.0 is generally disabled by the
304  * #GTlsBackend, so the lowest-supported protocol version is probably
305  * not SSL 3.0.
306  *
307  * Since 2.58, this may additionally cause an RFC 7507 fallback SCSV to
308  * be sent to the server, causing modern TLS servers to immediately
309  * terminate the connection. You should generally only use this function
310  * if you need to connect to broken servers that exhibit TLS protocol
311  * version intolerance, and when an initial attempt to connect to a
312  * server normally has already failed.
313  *
314  * Since: 2.28
315  *
316  * Deprecated: 2.56: SSL 3.0 is insecure, and this function does not
317  * generally enable or disable it, despite its name.
318  */
319 void
g_tls_client_connection_set_use_ssl3(GTlsClientConnection * conn,gboolean use_ssl3)320 g_tls_client_connection_set_use_ssl3 (GTlsClientConnection *conn,
321 				      gboolean              use_ssl3)
322 {
323   g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
324 
325   g_object_set (G_OBJECT (conn), "use-ssl3", use_ssl3, NULL);
326 }
327 
328 /**
329  * g_tls_client_connection_get_accepted_cas:
330  * @conn: the #GTlsClientConnection
331  *
332  * Gets the list of distinguished names of the Certificate Authorities
333  * that the server will accept certificates from. This will be set
334  * during the TLS handshake if the server requests a certificate.
335  * Otherwise, it will be %NULL.
336  *
337  * Each item in the list is a #GByteArray which contains the complete
338  * subject DN of the certificate authority.
339  *
340  * Returns: (element-type GByteArray) (transfer full): the list of
341  * CA DNs. You should unref each element with g_byte_array_unref() and then
342  * the free the list with g_list_free().
343  *
344  * Since: 2.28
345  */
346 GList *
g_tls_client_connection_get_accepted_cas(GTlsClientConnection * conn)347 g_tls_client_connection_get_accepted_cas (GTlsClientConnection *conn)
348 {
349   GList *accepted_cas = NULL;
350 
351   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), NULL);
352 
353   g_object_get (G_OBJECT (conn), "accepted-cas", &accepted_cas, NULL);
354   return accepted_cas;
355 }
356 
357 /**
358  * g_tls_client_connection_copy_session_state:
359  * @conn: a #GTlsClientConnection
360  * @source: a #GTlsClientConnection
361  *
362  * Copies session state from one connection to another. This is
363  * not normally needed, but may be used when the same session
364  * needs to be used between different endpoints as is required
365  * by some protocols such as FTP over TLS. @source should have
366  * already completed a handshake, and @conn should not have
367  * completed a handshake.
368  *
369  * Since: 2.46
370  */
371 void
g_tls_client_connection_copy_session_state(GTlsClientConnection * conn,GTlsClientConnection * source)372 g_tls_client_connection_copy_session_state (GTlsClientConnection *conn,
373                                             GTlsClientConnection *source)
374 {
375   g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
376   g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (source));
377   g_return_if_fail (G_TLS_CLIENT_CONNECTION_GET_INTERFACE (conn)->copy_session_state != NULL);
378 
379   G_TLS_CLIENT_CONNECTION_GET_INTERFACE (conn)->copy_session_state (conn,
380                                                                     source);
381 }
382