Change GTlsClientConnection::accepted-cas to contain DER DNs
[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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
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 "gio-marshal.h"
28 #include "gsocket.h"
29 #include "gsocketconnectable.h"
30 #include "gtlsbackend.h"
31 #include "gtlscertificate.h"
32 #include "glibintl.h"
33
34 /**
35  * SECTION:gtlsclientconnection
36  * @short_description: TLS client-side connection
37  * @include: gio/gio.h
38  *
39  * #GTlsClientConnection is the client-side subclass of
40  * #GTlsConnection, representing a client-side TLS connection.
41  *
42  * Since: 2.28
43  */
44
45 /**
46  * GTlsClientConnection:
47  *
48  * Abstract base class for the backend-specific client connection
49  * type.
50  *
51  * Since: 2.28
52  */
53
54 G_DEFINE_INTERFACE (GTlsClientConnection, g_tls_client_connection, G_TYPE_TLS_CONNECTION)
55
56 static void
57 g_tls_client_connection_default_init (GTlsClientConnectionInterface *iface)
58 {
59   /**
60    * GTlsClientConnection:validation-flags:
61    *
62    * What steps to perform when validating a certificate received from
63    * a server. Server certificates that fail to validate in all of the
64    * ways indicated here will be rejected unless the application
65    * overrides the default via #GTlsConnection::accept-certificate.
66    *
67    * Since: 2.28
68    */
69   g_object_interface_install_property (iface,
70                                        g_param_spec_flags ("validation-flags",
71                                                            P_("Validation flags"),
72                                                            P_("What certificate validation to perform"),
73                                                            G_TYPE_TLS_CERTIFICATE_FLAGS,
74                                                            G_TLS_CERTIFICATE_VALIDATE_ALL,
75                                                            G_PARAM_READWRITE |
76                                                            G_PARAM_CONSTRUCT |
77                                                            G_PARAM_STATIC_STRINGS));
78
79   /**
80    * GTlsClientConnection:server-identity:
81    *
82    * A #GSocketConnectable describing the identity of the server that
83    * is expected on the other end of the connection.
84    *
85    * If the %G_TLS_CERTIFICATE_BAD_IDENTITY flag is set in
86    * #GTlsClientConnection:validation-flags, this object will be used
87    * to determine the expected identify of the remote end of the
88    * connection; if #GTlsClientConnection:server-identity is not set,
89    * or does not match the identity presented by the server, then the
90    * %G_TLS_CERTIFICATE_BAD_IDENTITY validation will fail.
91    *
92    * In addition to its use in verifying the server certificate,
93    * this is also used to give a hint to the server about what
94    * certificate we expect, which is useful for servers that serve
95    * virtual hosts.
96    *
97    * Since: 2.28
98    */
99   g_object_interface_install_property (iface,
100                                        g_param_spec_object ("server-identity",
101                                                             P_("Server identity"),
102                                                             P_("GSocketConnectable identifying the server"),
103                                                             G_TYPE_SOCKET_CONNECTABLE,
104                                                             G_PARAM_READWRITE |
105                                                             G_PARAM_CONSTRUCT |
106                                                             G_PARAM_STATIC_STRINGS));
107
108   /**
109    * GTlsClientConnection:use-ssl3:
110    *
111    * If %TRUE, tells the connection to use SSL 3.0 rather than trying
112    * to negotiate the best version of TLS or SSL to use. This can be
113    * used when talking to servers that don't implement version
114    * negotiation correctly and therefore refuse to handshake at all with
115    * a "modern" TLS handshake.
116    *
117    * Since: 2.28
118    */
119   g_object_interface_install_property (iface,
120                                        g_param_spec_boolean ("use-ssl3",
121                                                              P_("Use SSL3"),
122                                                              P_("Use SSL 3.0 rather than trying to use TLS 1.x"),
123                                                              FALSE,
124                                                              G_PARAM_READWRITE |
125                                                              G_PARAM_CONSTRUCT |
126                                                              G_PARAM_STATIC_STRINGS));
127
128   /**
129    * GTlsClientConnection:accepted-cas:
130    *
131    * A list of the distinguished names of the Certificate Authorities
132    * that the server will accept client certificates signed by. If the
133    * server requests a client certificate during the handshake, then
134    * this property will be set after the handshake completes.
135    *
136    * Each item in the list is a #GByteArray which contains the complete
137    * subject DN of the certificate authority.
138    *
139    * Type: GList<GByteArray>
140    * Transfer: full
141    * Since: 2.28
142    */
143   g_object_interface_install_property (iface,
144                                        g_param_spec_pointer ("accepted-cas",
145                                                              P_("Accepted CAs"),
146                                                              P_("Distinguished names of the CAs the server accepts certificates from"),
147                                                              G_PARAM_READABLE |
148                                                              G_PARAM_STATIC_STRINGS));
149 }
150
151 /**
152  * g_tls_client_connection_new:
153  * @base_io_stream: the #GIOStream to wrap
154  * @server_identity: (allow-none): the expected identity of the server
155  * @error: #GError for error reporting, or %NULL to ignore.
156  *
157  * Creates a new #GTlsClientConnection wrapping @base_io_stream (which
158  * must have pollable input and output streams) which is assumed to
159  * communicate with the server identified by @server_identity.
160  *
161  * Return value: the new #GTlsClientConnection, or %NULL on error
162  *
163  * Since: 2.28
164  */
165 GIOStream *
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  * Return value: the validation flags
189  *
190  * Since: 2.28
191  */
192 GTlsCertificateFlags
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
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  * Return value: 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 *
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
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 use SSL 3.0 rather than the
274  * highest-supported version of TLS; see
275  * g_tls_client_connection_set_use_ssl3().
276  *
277  * Return value: whether @conn will use SSL 3.0
278  *
279  * Since: 2.28
280  */
281 gboolean
282 g_tls_client_connection_get_use_ssl3 (GTlsClientConnection *conn)
283 {
284   gboolean use_ssl3 = FALSE;
285
286   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), 0);
287
288   g_object_get (G_OBJECT (conn), "use-ssl3", &use_ssl3, NULL);
289   return use_ssl3;
290 }
291
292 /**
293  * g_tls_client_connection_set_use_ssl3:
294  * @conn: the #GTlsClientConnection
295  * @use_ssl3: whether to use SSL 3.0
296  *
297  * If @use_ssl3 is %TRUE, this forces @conn to use SSL 3.0 rather than
298  * trying to properly negotiate the right version of TLS or SSL to use.
299  * This can be used when talking to servers that do not implement the
300  * fallbacks correctly and which will therefore fail to handshake with
301  * a "modern" TLS handshake attempt.
302  *
303  * Since: 2.28
304  */
305 void
306 g_tls_client_connection_set_use_ssl3 (GTlsClientConnection *conn,
307                                       gboolean              use_ssl3)
308 {
309   g_return_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn));
310
311   g_object_set (G_OBJECT (conn), "use-ssl3", use_ssl3, NULL);
312 }
313
314 /**
315  * g_tls_client_connection_get_accepted_cas:
316  * @conn: the #GTlsClientConnection
317  *
318  * Gets the list of distinguished names of the Certificate Authorities
319  * that the server will accept certificates from. This will be set
320  * during the TLS handshake if the server requests a certificate.
321  * Otherwise, it will be %NULL.
322  *
323  * Each item in the list is a #GByteArray which contains the complete
324  * subject DN of the certificate authority.
325  *
326  * Return value: (element-type GByteArray) (transfer full): the list of
327  * CA DNs. You should unref each element with g_byte_array_unref() and then
328  * the free the list with g_list_free().
329  *
330  * Since: 2.28
331  */
332 GList *
333 g_tls_client_connection_get_accepted_cas (GTlsClientConnection *conn)
334 {
335   GList *accepted_cas = NULL;
336
337   g_return_val_if_fail (G_IS_TLS_CLIENT_CONNECTION (conn), NULL);
338
339   g_object_get (G_OBJECT (conn), "accepted-cas", &accepted_cas, NULL);
340   return accepted_cas;
341 }