Merge branch 'fix-gdbus-unix-address' into 'master'
[glib.git] / gio / gdtlsclientconnection.c
blob1ce97d3623c8801b6fdec581d5091d331aedb6b1
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2010 Red Hat, Inc
4 * Copyright © 2015 Collabora, Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "config.h"
21 #include "glib.h"
23 #include "gdtlsclientconnection.h"
24 #include "ginitable.h"
25 #include "gioenumtypes.h"
26 #include "gsocket.h"
27 #include "gsocketconnectable.h"
28 #include "gtlsbackend.h"
29 #include "gtlscertificate.h"
30 #include "glibintl.h"
32 /**
33 * SECTION:gdtlsclientconnection
34 * @short_description: DTLS client-side connection
35 * @include: gio/gio.h
37 * #GDtlsClientConnection is the client-side subclass of
38 * #GDtlsConnection, representing a client-side DTLS connection.
40 * Since: 2.48
43 /**
44 * GDtlsClientConnection:
46 * Abstract base class for the backend-specific client connection
47 * type.
49 * Since: 2.48
52 G_DEFINE_INTERFACE (GDtlsClientConnection, g_dtls_client_connection,
53 G_TYPE_DTLS_CONNECTION)
55 static void
56 g_dtls_client_connection_default_init (GDtlsClientConnectionInterface *iface)
58 /**
59 * GDtlsClientConnection:validation-flags:
61 * What steps to perform when validating a certificate received from
62 * a server. Server certificates that fail to validate in all of the
63 * ways indicated here will be rejected unless the application
64 * overrides the default via #GDtlsConnection::accept-certificate.
66 * Since: 2.48
68 g_object_interface_install_property (iface,
69 g_param_spec_flags ("validation-flags",
70 P_("Validation flags"),
71 P_("What certificate validation to perform"),
72 G_TYPE_TLS_CERTIFICATE_FLAGS,
73 G_TLS_CERTIFICATE_VALIDATE_ALL,
74 G_PARAM_READWRITE |
75 G_PARAM_CONSTRUCT |
76 G_PARAM_STATIC_STRINGS));
78 /**
79 * GDtlsClientConnection:server-identity:
81 * A #GSocketConnectable describing the identity of the server that
82 * is expected on the other end of the connection.
84 * If the %G_TLS_CERTIFICATE_BAD_IDENTITY flag is set in
85 * #GDtlsClientConnection:validation-flags, this object will be used
86 * to determine the expected identify of the remote end of the
87 * connection; if #GDtlsClientConnection:server-identity is not set,
88 * or does not match the identity presented by the server, then the
89 * %G_TLS_CERTIFICATE_BAD_IDENTITY validation will fail.
91 * In addition to its use in verifying the server certificate,
92 * this is also used to give a hint to the server about what
93 * certificate we expect, which is useful for servers that serve
94 * virtual hosts.
96 * Since: 2.48
98 g_object_interface_install_property (iface,
99 g_param_spec_object ("server-identity",
100 P_("Server identity"),
101 P_("GSocketConnectable identifying the server"),
102 G_TYPE_SOCKET_CONNECTABLE,
103 G_PARAM_READWRITE |
104 G_PARAM_CONSTRUCT |
105 G_PARAM_STATIC_STRINGS));
108 * GDtlsClientConnection:accepted-cas: (type GLib.List) (element-type GLib.ByteArray)
110 * A list of the distinguished names of the Certificate Authorities
111 * that the server will accept client certificates signed by. If the
112 * server requests a client certificate during the handshake, then
113 * this property will be set after the handshake completes.
115 * Each item in the list is a #GByteArray which contains the complete
116 * subject DN of the certificate authority.
118 * Since: 2.48
120 g_object_interface_install_property (iface,
121 g_param_spec_pointer ("accepted-cas",
122 P_("Accepted CAs"),
123 P_("Distinguished names of the CAs the server accepts certificates from"),
124 G_PARAM_READABLE |
125 G_PARAM_STATIC_STRINGS));
129 * g_dtls_client_connection_new:
130 * @base_socket: the #GDatagramBased to wrap
131 * @server_identity: (nullable): the expected identity of the server
132 * @error: #GError for error reporting, or %NULL to ignore.
134 * Creates a new #GDtlsClientConnection wrapping @base_socket which is
135 * assumed to communicate with the server identified by @server_identity.
137 * Returns: (transfer full) (type GDtlsClientConnection): the new
138 * #GDtlsClientConnection, or %NULL on error
140 * Since: 2.48
142 GDatagramBased *
143 g_dtls_client_connection_new (GDatagramBased *base_socket,
144 GSocketConnectable *server_identity,
145 GError **error)
147 GObject *conn;
148 GTlsBackend *backend;
150 backend = g_tls_backend_get_default ();
151 conn = g_initable_new (g_tls_backend_get_dtls_client_connection_type (backend),
152 NULL, error,
153 "base-socket", base_socket,
154 "server-identity", server_identity,
155 NULL);
156 return G_DATAGRAM_BASED (conn);
160 * g_dtls_client_connection_get_validation_flags:
161 * @conn: the #GDtlsClientConnection
163 * Gets @conn's validation flags
165 * Returns: the validation flags
167 * Since: 2.48
169 GTlsCertificateFlags
170 g_dtls_client_connection_get_validation_flags (GDtlsClientConnection *conn)
172 GTlsCertificateFlags flags = 0;
174 g_return_val_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn), 0);
176 g_object_get (G_OBJECT (conn), "validation-flags", &flags, NULL);
177 return flags;
181 * g_dtls_client_connection_set_validation_flags:
182 * @conn: the #GDtlsClientConnection
183 * @flags: the #GTlsCertificateFlags to use
185 * Sets @conn's validation flags, to override the default set of
186 * checks performed when validating a server certificate. By default,
187 * %G_TLS_CERTIFICATE_VALIDATE_ALL is used.
189 * Since: 2.48
191 void
192 g_dtls_client_connection_set_validation_flags (GDtlsClientConnection *conn,
193 GTlsCertificateFlags flags)
195 g_return_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn));
197 g_object_set (G_OBJECT (conn), "validation-flags", flags, NULL);
201 * g_dtls_client_connection_get_server_identity:
202 * @conn: the #GDtlsClientConnection
204 * Gets @conn's expected server identity
206 * Returns: (transfer none): a #GSocketConnectable describing the
207 * expected server identity, or %NULL if the expected identity is not
208 * known.
210 * Since: 2.48
212 GSocketConnectable *
213 g_dtls_client_connection_get_server_identity (GDtlsClientConnection *conn)
215 GSocketConnectable *identity = NULL;
217 g_return_val_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn), 0);
219 g_object_get (G_OBJECT (conn), "server-identity", &identity, NULL);
220 if (identity)
221 g_object_unref (identity);
222 return identity;
226 * g_dtls_client_connection_set_server_identity:
227 * @conn: the #GDtlsClientConnection
228 * @identity: a #GSocketConnectable describing the expected server identity
230 * Sets @conn's expected server identity, which is used both to tell
231 * servers on virtual hosts which certificate to present, and also
232 * to let @conn know what name to look for in the certificate when
233 * performing %G_TLS_CERTIFICATE_BAD_IDENTITY validation, if enabled.
235 * Since: 2.48
237 void
238 g_dtls_client_connection_set_server_identity (GDtlsClientConnection *conn,
239 GSocketConnectable *identity)
241 g_return_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn));
243 g_object_set (G_OBJECT (conn), "server-identity", identity, NULL);
247 * g_dtls_client_connection_get_accepted_cas:
248 * @conn: the #GDtlsClientConnection
250 * Gets the list of distinguished names of the Certificate Authorities
251 * that the server will accept certificates from. This will be set
252 * during the TLS handshake if the server requests a certificate.
253 * Otherwise, it will be %NULL.
255 * Each item in the list is a #GByteArray which contains the complete
256 * subject DN of the certificate authority.
258 * Returns: (element-type GByteArray) (transfer full): the list of
259 * CA DNs. You should unref each element with g_byte_array_unref() and then
260 * the free the list with g_list_free().
262 * Since: 2.48
264 GList *
265 g_dtls_client_connection_get_accepted_cas (GDtlsClientConnection *conn)
267 GList *accepted_cas = NULL;
269 g_return_val_if_fail (G_IS_DTLS_CLIENT_CONNECTION (conn), NULL);
271 g_object_get (G_OBJECT (conn), "accepted-cas", &accepted_cas, NULL);
272 return accepted_cas;