/src/glib/gio/gtlsclientconnection.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright © 2010 Red Hat, Inc |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "config.h" |
22 | | #include "glib.h" |
23 | | |
24 | | #include "gtlsclientconnection.h" |
25 | | #include "ginitable.h" |
26 | | #include "gioenumtypes.h" |
27 | | #include "gsocket.h" |
28 | | #include "gsocketconnectable.h" |
29 | | #include "gtlsbackend.h" |
30 | | #include "gtlscertificate.h" |
31 | | #include "glibintl.h" |
32 | | |
33 | | /** |
34 | | * SECTION:gtlsclientconnection |
35 | | * @short_description: TLS client-side connection |
36 | | * @include: gio/gio.h |
37 | | * |
38 | | * #GTlsClientConnection is the client-side subclass of |
39 | | * #GTlsConnection, representing a client-side TLS connection. |
40 | | */ |
41 | | |
42 | | /** |
43 | | * GTlsClientConnection: |
44 | | * |
45 | | * Abstract base class for the backend-specific client connection |
46 | | * type. |
47 | | * |
48 | | * Since: 2.28 |
49 | | */ |
50 | | |
51 | | G_DEFINE_INTERFACE (GTlsClientConnection, g_tls_client_connection, G_TYPE_TLS_CONNECTION) |
52 | | |
53 | | static void |
54 | | g_tls_client_connection_default_init (GTlsClientConnectionInterface *iface) |
55 | 0 | { |
56 | | /** |
57 | | * GTlsClientConnection:validation-flags: |
58 | | * |
59 | | * What steps to perform when validating a certificate received from |
60 | | * a server. Server certificates that fail to validate in any of the |
61 | | * ways indicated here will be rejected unless the application |
62 | | * overrides the default via #GTlsConnection::accept-certificate. |
63 | | * |
64 | | * GLib guarantees that if certificate verification fails, at least one |
65 | | * flag will be set, but it does not guarantee that all possible flags |
66 | | * will be set. Accordingly, you may not safely decide to ignore any |
67 | | * particular type of error. For example, it would be incorrect to mask |
68 | | * %G_TLS_CERTIFICATE_EXPIRED if you want to allow expired certificates, |
69 | | * because this could potentially be the only error flag set even if |
70 | | * other problems exist with the certificate. Therefore, there is no |
71 | | * safe way to use this property. This is not a horrible problem, |
72 | | * though, because you should not be attempting to ignore validation |
73 | | * errors anyway. If you really must ignore TLS certificate errors, |
74 | | * connect to #GTlsConnection::accept-certificate. |
75 | | * |
76 | | * Since: 2.28 |
77 | | * |
78 | | * Deprecated: 2.72: Do not attempt to ignore validation errors. |
79 | | */ |
80 | 0 | g_object_interface_install_property (iface, |
81 | 0 | g_param_spec_flags ("validation-flags", |
82 | 0 | P_("Validation flags"), |
83 | 0 | P_("What certificate validation to perform"), |
84 | 0 | G_TYPE_TLS_CERTIFICATE_FLAGS, |
85 | 0 | G_TLS_CERTIFICATE_VALIDATE_ALL, |
86 | 0 | G_PARAM_READWRITE | |
87 | 0 | G_PARAM_CONSTRUCT | |
88 | 0 | G_PARAM_STATIC_STRINGS | |
89 | 0 | G_PARAM_DEPRECATED)); |
90 | | |
91 | | /** |
92 | | * GTlsClientConnection:server-identity: |
93 | | * |
94 | | * A #GSocketConnectable describing the identity of the server that |
95 | | * is expected on the other end of the connection. |
96 | | * |
97 | | * If the %G_TLS_CERTIFICATE_BAD_IDENTITY flag is set in |
98 | | * #GTlsClientConnection:validation-flags, this object will be used |
99 | | * to determine the expected identify of the remote end of the |
100 | | * connection; if #GTlsClientConnection:server-identity is not set, |
101 | | * or does not match the identity presented by the server, then the |
102 | | * %G_TLS_CERTIFICATE_BAD_IDENTITY validation will fail. |
103 | | * |
104 | | * In addition to its use in verifying the server certificate, |
105 | | * this is also used to give a hint to the server about what |
106 | | * certificate we expect, which is useful for servers that serve |
107 | | * virtual hosts. |
108 | | * |
109 | | * Since: 2.28 |
110 | | */ |
111 | 0 | g_object_interface_install_property (iface, |
112 | 0 | g_param_spec_object ("server-identity", |
113 | 0 | P_("Server identity"), |
114 | 0 | P_("GSocketConnectable identifying the server"), |
115 | 0 | G_TYPE_SOCKET_CONNECTABLE, |
116 | 0 | G_PARAM_READWRITE | |
117 | 0 | G_PARAM_CONSTRUCT | |
118 | 0 | G_PARAM_STATIC_STRINGS)); |
119 | | |
120 | | /** |
121 | | * GTlsClientConnection:use-ssl3: |
122 | | * |
123 | | * SSL 3.0 is no longer supported. See |
124 | | * g_tls_client_connection_set_use_ssl3() for details. |
125 | | * |
126 | | * Since: 2.28 |
127 | | * |
128 | | * Deprecated: 2.56: SSL 3.0 is insecure. |
129 | | */ |
130 | 0 | g_object_interface_install_property (iface, |
131 | 0 | g_param_spec_boolean ("use-ssl3", |
132 | 0 | P_("Use fallback"), |
133 | 0 | P_("Use fallback version of SSL/TLS rather than most recent version"), |
134 | 0 | FALSE, |
135 | 0 | G_PARAM_READWRITE | |
136 | 0 | G_PARAM_CONSTRUCT | |
137 | 0 | G_PARAM_STATIC_STRINGS | |
138 | 0 | G_PARAM_DEPRECATED)); |
139 | | |
140 | | /** |
141 | | * GTlsClientConnection:accepted-cas: (type GLib.List) (element-type GLib.ByteArray) |
142 | | * |
143 | | * A list of the distinguished names of the Certificate Authorities |
144 | | * that the server will accept client certificates signed by. If the |
145 | | * server requests a client certificate during the handshake, then |
146 | | * this property will be set after the handshake completes. |
147 | | * |
148 | | * Each item in the list is a #GByteArray which contains the complete |
149 | | * subject DN of the certificate authority. |
150 | | * |
151 | | * Since: 2.28 |
152 | | */ |
153 | 0 | g_object_interface_install_property (iface, |
154 | 0 | g_param_spec_pointer ("accepted-cas", |
155 | 0 | P_("Accepted CAs"), |
156 | 0 | P_("Distinguished names of the CAs the server accepts certificates from"), |
157 | 0 | G_PARAM_READABLE | |
158 | 0 | G_PARAM_STATIC_STRINGS)); |
159 | 0 | } |
160 | | |
161 | | /** |
162 | | * g_tls_client_connection_new: |
163 | | * @base_io_stream: the #GIOStream to wrap |
164 | | * @server_identity: (nullable): the expected identity of the server |
165 | | * @error: #GError for error reporting, or %NULL to ignore. |
166 | | * |
167 | | * Creates a new #GTlsClientConnection wrapping @base_io_stream (which |
168 | | * must have pollable input and output streams) which is assumed to |
169 | | * communicate with the server identified by @server_identity. |
170 | | * |
171 | | * See the documentation for #GTlsConnection:base-io-stream for restrictions |
172 | | * on when application code can run operations on the @base_io_stream after |
173 | | * this function has returned. |
174 | | * |
175 | | * Returns: (transfer full) (type GTlsClientConnection): the new |
176 | | * #GTlsClientConnection, or %NULL on error |
177 | | * |
178 | | * Since: 2.28 |
179 | | */ |
180 | | GIOStream * |
181 | | g_tls_client_connection_new (GIOStream *base_io_stream, |
182 | | GSocketConnectable *server_identity, |
183 | | GError **error) |
184 | 0 | { |
185 | 0 | GObject *conn; |
186 | 0 | GTlsBackend *backend; |
187 | |
|
188 | 0 | backend = g_tls_backend_get_default (); |
189 | 0 | conn = g_initable_new (g_tls_backend_get_client_connection_type (backend), |
190 | 0 | NULL, error, |
191 | 0 | "base-io-stream", base_io_stream, |
192 | 0 | "server-identity", server_identity, |
193 | 0 | NULL); |
194 | 0 | return G_IO_STREAM (conn); |
195 | 0 | } |
196 | | |
197 | | /** |
198 | | * g_tls_client_connection_get_validation_flags: |
199 | | * @conn: the #GTlsClientConnection |
200 | | * |
201 | | * Gets @conn's validation flags |
202 | | * |
203 | | * This function does not work as originally designed and is impossible |
204 | | * to use correctly. See #GTlsClientConnection:validation-flags for more |
205 | | * information. |
206 | | * |
207 | | * Returns: the validation flags |
208 | | * |
209 | | * Since: 2.28 |
210 | | * |
211 | | * Deprecated: 2.72: Do not attempt to ignore validation errors. |
212 | | */ |
213 | | GTlsCertificateFlags |
214 | | g_tls_client_connection_get_validation_flags (GTlsClientConnection *conn) |
215 | 0 | { |
216 | 0 | GTlsCertificateFlags flags = G_TLS_CERTIFICATE_NO_FLAGS; |
217 | |
|
218 | 0 | g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0); |
219 | | |
220 | 0 | g_object_get (G_OBJECT (conn), "validation-flags", &flags, NULL); |
221 | 0 | return flags; |
222 | 0 | } |
223 | | |
224 | | /** |
225 | | * g_tls_client_connection_set_validation_flags: |
226 | | * @conn: the #GTlsClientConnection |
227 | | * @flags: the #GTlsCertificateFlags to use |
228 | | * |
229 | | * Sets @conn's validation flags, to override the default set of |
230 | | * checks performed when validating a server certificate. By default, |
231 | | * %G_TLS_CERTIFICATE_VALIDATE_ALL is used. |
232 | | * |
233 | | * This function does not work as originally designed and is impossible |
234 | | * to use correctly. See #GTlsClientConnection:validation-flags for more |
235 | | * information. |
236 | | * |
237 | | * Since: 2.28 |
238 | | * |
239 | | * Deprecated: 2.72: Do not attempt to ignore validation errors. |
240 | | */ |
241 | | void |
242 | | g_tls_client_connection_set_validation_flags (GTlsClientConnection *conn, |
243 | | GTlsCertificateFlags flags) |
244 | 0 | { |
245 | 0 | g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn)); |
246 | | |
247 | 0 | g_object_set (G_OBJECT (conn), "validation-flags", flags, NULL); |
248 | 0 | } |
249 | | |
250 | | /** |
251 | | * g_tls_client_connection_get_server_identity: |
252 | | * @conn: the #GTlsClientConnection |
253 | | * |
254 | | * Gets @conn's expected server identity |
255 | | * |
256 | | * Returns: (nullable) (transfer none): a #GSocketConnectable describing the |
257 | | * expected server identity, or %NULL if the expected identity is not |
258 | | * known. |
259 | | * |
260 | | * Since: 2.28 |
261 | | */ |
262 | | GSocketConnectable * |
263 | | g_tls_client_connection_get_server_identity (GTlsClientConnection *conn) |
264 | 0 | { |
265 | 0 | GSocketConnectable *identity = NULL; |
266 | |
|
267 | 0 | g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0); |
268 | | |
269 | 0 | g_object_get (G_OBJECT (conn), "server-identity", &identity, NULL); |
270 | 0 | if (identity) |
271 | 0 | g_object_unref (identity); |
272 | 0 | return identity; |
273 | 0 | } |
274 | | |
275 | | /** |
276 | | * g_tls_client_connection_set_server_identity: |
277 | | * @conn: the #GTlsClientConnection |
278 | | * @identity: a #GSocketConnectable describing the expected server identity |
279 | | * |
280 | | * Sets @conn's expected server identity, which is used both to tell |
281 | | * servers on virtual hosts which certificate to present, and also |
282 | | * to let @conn know what name to look for in the certificate when |
283 | | * performing %G_TLS_CERTIFICATE_BAD_IDENTITY validation, if enabled. |
284 | | * |
285 | | * Since: 2.28 |
286 | | */ |
287 | | void |
288 | | g_tls_client_connection_set_server_identity (GTlsClientConnection *conn, |
289 | | GSocketConnectable *identity) |
290 | 0 | { |
291 | 0 | g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn)); |
292 | | |
293 | 0 | g_object_set (G_OBJECT (conn), "server-identity", identity, NULL); |
294 | 0 | } |
295 | | |
296 | | /** |
297 | | * g_tls_client_connection_get_use_ssl3: |
298 | | * @conn: the #GTlsClientConnection |
299 | | * |
300 | | * SSL 3.0 is no longer supported. See |
301 | | * g_tls_client_connection_set_use_ssl3() for details. |
302 | | * |
303 | | * Returns: %FALSE |
304 | | * |
305 | | * Since: 2.28 |
306 | | * |
307 | | * Deprecated: 2.56: SSL 3.0 is insecure. |
308 | | */ |
309 | | gboolean |
310 | | g_tls_client_connection_get_use_ssl3 (GTlsClientConnection *conn) |
311 | 0 | { |
312 | 0 | gboolean use_ssl3 = FALSE; |
313 | |
|
314 | 0 | g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0); |
315 | | |
316 | 0 | g_object_get (G_OBJECT (conn), "use-ssl3", &use_ssl3, NULL); |
317 | 0 | return FALSE; |
318 | 0 | } |
319 | | |
320 | | /** |
321 | | * g_tls_client_connection_set_use_ssl3: |
322 | | * @conn: the #GTlsClientConnection |
323 | | * @use_ssl3: a #gboolean, ignored |
324 | | * |
325 | | * Since GLib 2.42.1, SSL 3.0 is no longer supported. |
326 | | * |
327 | | * From GLib 2.42.1 through GLib 2.62, this function could be used to |
328 | | * force use of TLS 1.0, the lowest-supported TLS protocol version at |
329 | | * the time. In the past, this was needed to connect to broken TLS |
330 | | * servers that exhibited protocol version intolerance. Such servers |
331 | | * are no longer common, and using TLS 1.0 is no longer considered |
332 | | * acceptable. |
333 | | * |
334 | | * Since GLib 2.64, this function does nothing. |
335 | | * |
336 | | * Since: 2.28 |
337 | | * |
338 | | * Deprecated: 2.56: SSL 3.0 is insecure. |
339 | | */ |
340 | | void |
341 | | g_tls_client_connection_set_use_ssl3 (GTlsClientConnection *conn, |
342 | | gboolean use_ssl3) |
343 | 0 | { |
344 | 0 | g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn)); |
345 | | |
346 | 0 | g_object_set (G_OBJECT (conn), "use-ssl3", FALSE, NULL); |
347 | 0 | } |
348 | | |
349 | | /** |
350 | | * g_tls_client_connection_get_accepted_cas: |
351 | | * @conn: the #GTlsClientConnection |
352 | | * |
353 | | * Gets the list of distinguished names of the Certificate Authorities |
354 | | * that the server will accept certificates from. This will be set |
355 | | * during the TLS handshake if the server requests a certificate. |
356 | | * Otherwise, it will be %NULL. |
357 | | * |
358 | | * Each item in the list is a #GByteArray which contains the complete |
359 | | * subject DN of the certificate authority. |
360 | | * |
361 | | * Returns: (element-type GByteArray) (transfer full): the list of |
362 | | * CA DNs. You should unref each element with g_byte_array_unref() and then |
363 | | * the free the list with g_list_free(). |
364 | | * |
365 | | * Since: 2.28 |
366 | | */ |
367 | | GList * |
368 | | g_tls_client_connection_get_accepted_cas (GTlsClientConnection *conn) |
369 | 0 | { |
370 | 0 | GList *accepted_cas = NULL; |
371 | |
|
372 | 0 | g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), NULL); |
373 | | |
374 | 0 | g_object_get (G_OBJECT (conn), "accepted-cas", &accepted_cas, NULL); |
375 | 0 | return accepted_cas; |
376 | 0 | } |
377 | | |
378 | | /** |
379 | | * g_tls_client_connection_copy_session_state: |
380 | | * @conn: a #GTlsClientConnection |
381 | | * @source: a #GTlsClientConnection |
382 | | * |
383 | | * Possibly copies session state from one connection to another, for use |
384 | | * in TLS session resumption. This is not normally needed, but may be |
385 | | * used when the same session needs to be used between different |
386 | | * endpoints, as is required by some protocols, such as FTP over TLS. |
387 | | * @source should have already completed a handshake and, since TLS 1.3, |
388 | | * it should have been used to read data at least once. @conn should not |
389 | | * have completed a handshake. |
390 | | * |
391 | | * It is not possible to know whether a call to this function will |
392 | | * actually do anything. Because session resumption is normally used |
393 | | * only for performance benefit, the TLS backend might not implement |
394 | | * this function. Even if implemented, it may not actually succeed in |
395 | | * allowing @conn to resume @source's TLS session, because the server |
396 | | * may not have sent a session resumption token to @source, or it may |
397 | | * refuse to accept the token from @conn. There is no way to know |
398 | | * whether a call to this function is actually successful. |
399 | | * |
400 | | * Using this function is not required to benefit from session |
401 | | * resumption. If the TLS backend supports session resumption, the |
402 | | * session will be resumed automatically if it is possible to do so |
403 | | * without weakening the privacy guarantees normally provided by TLS, |
404 | | * without need to call this function. For example, with TLS 1.3, |
405 | | * a session ticket will be automatically copied from any |
406 | | * #GTlsClientConnection that has previously received session tickets |
407 | | * from the server, provided a ticket is available that has not |
408 | | * previously been used for session resumption, since session ticket |
409 | | * reuse would be a privacy weakness. Using this function causes the |
410 | | * ticket to be copied without regard for privacy considerations. |
411 | | * |
412 | | * Since: 2.46 |
413 | | */ |
414 | | void |
415 | | g_tls_client_connection_copy_session_state (GTlsClientConnection *conn, |
416 | | GTlsClientConnection *source) |
417 | 0 | { |
418 | 0 | g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn)); |
419 | 0 | g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (source)); |
420 | 0 | g_return_if_fail (G_TLS_CLIENT_CONNECTION_GET_INTERFACE (conn)->copy_session_state != NULL); |
421 | | |
422 | 0 | G_TLS_CLIENT_CONNECTION_GET_INTERFACE (conn)->copy_session_state (conn, |
423 | 0 | source); |
424 | 0 | } |