gresource: Make extract work better
[platform/upstream/glib.git] / gio / gtlsclientconnection.c
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 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
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, tells the connection to use SSL 3.0 rather than trying
107    * to negotiate the best version of TLS or SSL to use. This can be
108    * used when talking to servers that don't implement version
109    * negotiation correctly and therefore refuse to handshake at all with
110    * a "modern" TLS handshake.
111    *
112    * Since: 2.28
113    */
114   g_object_interface_install_property (iface,
115                                        g_param_spec_boolean ("use-ssl3",
116                                                              P_("Use SSL3"),
117                                                              P_("Use SSL 3.0 rather than trying to use TLS 1.x"),
118                                                              FALSE,
119                                                              G_PARAM_READWRITE |
120                                                              G_PARAM_CONSTRUCT |
121                                                              G_PARAM_STATIC_STRINGS));
122
123   /**
124    * GTlsClientConnection:accepted-cas: (type GLib.List) (element-type GLib.ByteArray):
125    *
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.
130    *
131    * Each item in the list is a #GByteArray which contains the complete
132    * subject DN of the certificate authority.
133    *
134    * Since: 2.28
135    */
136   g_object_interface_install_property (iface,
137                                        g_param_spec_pointer ("accepted-cas",
138                                                              P_("Accepted CAs"),
139                                                              P_("Distinguished names of the CAs the server accepts certificates from"),
140                                                              G_PARAM_READABLE |
141                                                              G_PARAM_STATIC_STRINGS));
142 }
143
144 /**
145  * g_tls_client_connection_new:
146  * @base_io_stream: the #GIOStream to wrap
147  * @server_identity: (allow-none): the expected identity of the server
148  * @error: #GError for error reporting, or %NULL to ignore.
149  *
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.
153  *
154  * Returns: (transfer full) (type GTlsClientConnection): the new
155  * #GTlsClientConnection, or %NULL on error
156  *
157  * Since: 2.28
158  */
159 GIOStream *
160 g_tls_client_connection_new (GIOStream           *base_io_stream,
161                              GSocketConnectable  *server_identity,
162                              GError             **error)
163 {
164   GObject *conn;
165   GTlsBackend *backend;
166
167   backend = g_tls_backend_get_default ();
168   conn = g_initable_new (g_tls_backend_get_client_connection_type (backend),
169                          NULL, error,
170                          "base-io-stream", base_io_stream,
171                          "server-identity", server_identity,
172                          NULL);
173   return G_IO_STREAM (conn);
174 }
175
176 /**
177  * g_tls_client_connection_get_validation_flags:
178  * @conn: the #GTlsClientConnection
179  *
180  * Gets @conn's validation flags
181  *
182  * Returns: the validation flags
183  *
184  * Since: 2.28
185  */
186 GTlsCertificateFlags
187 g_tls_client_connection_get_validation_flags (GTlsClientConnection *conn)
188 {
189   GTlsCertificateFlags flags = 0;
190
191   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
192
193   g_object_get (G_OBJECT (conn), "validation-flags", &flags, NULL);
194   return flags;
195 }
196
197 /**
198  * g_tls_client_connection_set_validation_flags:
199  * @conn: the #GTlsClientConnection
200  * @flags: the #GTlsCertificateFlags to use
201  *
202  * Sets @conn's validation flags, to override the default set of
203  * checks performed when validating a server certificate. By default,
204  * %G_TLS_CERTIFICATE_VALIDATE_ALL is used.
205  *
206  * Since: 2.28
207  */
208 void
209 g_tls_client_connection_set_validation_flags (GTlsClientConnection  *conn,
210                                               GTlsCertificateFlags   flags)
211 {
212   g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
213
214   g_object_set (G_OBJECT (conn), "validation-flags", flags, NULL);
215 }
216
217 /**
218  * g_tls_client_connection_get_server_identity:
219  * @conn: the #GTlsClientConnection
220  *
221  * Gets @conn's expected server identity
222  *
223  * Returns: (transfer none): a #GSocketConnectable describing the
224  * expected server identity, or %NULL if the expected identity is not
225  * known.
226  *
227  * Since: 2.28
228  */
229 GSocketConnectable *
230 g_tls_client_connection_get_server_identity (GTlsClientConnection *conn)
231 {
232   GSocketConnectable *identity = NULL;
233
234   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
235
236   g_object_get (G_OBJECT (conn), "server-identity", &identity, NULL);
237   if (identity)
238     g_object_unref (identity);
239   return identity;
240 }
241
242 /**
243  * g_tls_client_connection_set_server_identity:
244  * @conn: the #GTlsClientConnection
245  * @identity: a #GSocketConnectable describing the expected server identity
246  *
247  * Sets @conn's expected server identity, which is used both to tell
248  * servers on virtual hosts which certificate to present, and also
249  * to let @conn know what name to look for in the certificate when
250  * performing %G_TLS_CERTIFICATE_BAD_IDENTITY validation, if enabled.
251  *
252  * Since: 2.28
253  */
254 void
255 g_tls_client_connection_set_server_identity (GTlsClientConnection *conn,
256                                              GSocketConnectable   *identity)
257 {
258   g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
259
260   g_object_set (G_OBJECT (conn), "server-identity", identity, NULL);
261 }
262
263 /**
264  * g_tls_client_connection_get_use_ssl3:
265  * @conn: the #GTlsClientConnection
266  *
267  * Gets whether @conn will use SSL 3.0 rather than the
268  * highest-supported version of TLS; see
269  * g_tls_client_connection_set_use_ssl3().
270  *
271  * Returns: whether @conn will use SSL 3.0
272  *
273  * Since: 2.28
274  */
275 gboolean
276 g_tls_client_connection_get_use_ssl3 (GTlsClientConnection *conn)
277 {
278   gboolean use_ssl3 = FALSE;
279
280   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
281
282   g_object_get (G_OBJECT (conn), "use-ssl3", &use_ssl3, NULL);
283   return use_ssl3;
284 }
285
286 /**
287  * g_tls_client_connection_set_use_ssl3:
288  * @conn: the #GTlsClientConnection
289  * @use_ssl3: whether to use SSL 3.0
290  *
291  * If @use_ssl3 is %TRUE, this forces @conn to use SSL 3.0 rather than
292  * trying to properly negotiate the right version of TLS or SSL to use.
293  * This can be used when talking to servers that do not implement the
294  * fallbacks correctly and which will therefore fail to handshake with
295  * a "modern" TLS handshake attempt.
296  *
297  * Since: 2.28
298  */
299 void
300 g_tls_client_connection_set_use_ssl3 (GTlsClientConnection *conn,
301                                       gboolean              use_ssl3)
302 {
303   g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
304
305   g_object_set (G_OBJECT (conn), "use-ssl3", use_ssl3, NULL);
306 }
307
308 /**
309  * g_tls_client_connection_get_accepted_cas:
310  * @conn: the #GTlsClientConnection
311  *
312  * Gets the list of distinguished names of the Certificate Authorities
313  * that the server will accept certificates from. This will be set
314  * during the TLS handshake if the server requests a certificate.
315  * Otherwise, it will be %NULL.
316  *
317  * Each item in the list is a #GByteArray which contains the complete
318  * subject DN of the certificate authority.
319  *
320  * Returns: (element-type GByteArray) (transfer full): the list of
321  * CA DNs. You should unref each element with g_byte_array_unref() and then
322  * the free the list with g_list_free().
323  *
324  * Since: 2.28
325  */
326 GList *
327 g_tls_client_connection_get_accepted_cas (GTlsClientConnection *conn)
328 {
329   GList *accepted_cas = NULL;
330
331   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), NULL);
332
333   g_object_get (G_OBJECT (conn), "accepted-cas", &accepted_cas, NULL);
334   return accepted_cas;
335 }