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 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/>.
23 #include "gdtlsclientconnection.h"
24 #include "ginitable.h"
25 #include "gioenumtypes.h"
27 #include "gsocketconnectable.h"
28 #include "gtlsbackend.h"
29 #include "gtlscertificate.h"
33 * SECTION:gdtlsclientconnection
34 * @short_description: DTLS client-side connection
37 * #GDtlsClientConnection is the client-side subclass of
38 * #GDtlsConnection, representing a client-side DTLS connection.
44 * GDtlsClientConnection:
46 * Abstract base class for the backend-specific client connection
52 G_DEFINE_INTERFACE (GDtlsClientConnection
, g_dtls_client_connection
,
53 G_TYPE_DTLS_CONNECTION
)
56 g_dtls_client_connection_default_init (GDtlsClientConnectionInterface
*iface
)
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.
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
,
76 G_PARAM_STATIC_STRINGS
));
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
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
,
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.
120 g_object_interface_install_property (iface
,
121 g_param_spec_pointer ("accepted-cas",
123 P_("Distinguished names of the CAs the server accepts certificates from"),
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
143 g_dtls_client_connection_new (GDatagramBased
*base_socket
,
144 GSocketConnectable
*server_identity
,
148 GTlsBackend
*backend
;
150 backend
= g_tls_backend_get_default ();
151 conn
= g_initable_new (g_tls_backend_get_dtls_client_connection_type (backend
),
153 "base-socket", base_socket
,
154 "server-identity", server_identity
,
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
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
);
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.
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
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
);
221 g_object_unref (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.
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().
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
);