1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2011 Collabora Ltd.
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.
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.
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/>.
19 #include "gtesttlsbackend.h"
23 static GType _g_test_tls_certificate_get_type (void);
24 static GType _g_test_tls_connection_get_type (void);
26 struct _GTestTlsBackend {
27 GObject parent_instance;
30 static void g_test_tls_backend_iface_init (GTlsBackendInterface *iface);
32 #define g_test_tls_backend_get_type _g_test_tls_backend_get_type
33 G_DEFINE_TYPE_WITH_CODE (GTestTlsBackend, g_test_tls_backend, G_TYPE_OBJECT,
34 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
35 g_test_tls_backend_iface_init)
36 g_io_extension_point_set_required_type (
37 g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME),
39 g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
45 g_test_tls_backend_init (GTestTlsBackend *backend)
50 g_test_tls_backend_class_init (GTestTlsBackendClass *backend_class)
55 g_test_tls_backend_iface_init (GTlsBackendInterface *iface)
57 iface->get_certificate_type = _g_test_tls_certificate_get_type;
58 iface->get_client_connection_type = _g_test_tls_connection_get_type;
59 iface->get_server_connection_type = _g_test_tls_connection_get_type;
62 /* Test certificate type */
64 typedef struct _GTestTlsCertificate GTestTlsCertificate;
65 typedef struct _GTestTlsCertificateClass GTestTlsCertificateClass;
67 struct _GTestTlsCertificate {
68 GTlsCertificate parent_instance;
73 struct _GTestTlsCertificateClass {
74 GTlsCertificateClass parent_class;
81 PROP_CERT_CERTIFICATE,
82 PROP_CERT_CERTIFICATE_PEM,
83 PROP_CERT_PRIVATE_KEY,
84 PROP_CERT_PRIVATE_KEY_PEM,
88 static void g_test_tls_certificate_initable_iface_init (GInitableIface *iface);
90 #define g_test_tls_certificate_get_type _g_test_tls_certificate_get_type
91 G_DEFINE_TYPE_WITH_CODE (GTestTlsCertificate, g_test_tls_certificate, G_TYPE_TLS_CERTIFICATE,
92 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
93 g_test_tls_certificate_initable_iface_init);)
96 g_test_tls_certificate_get_property (GObject *object,
101 GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
105 case PROP_CERT_CERTIFICATE_PEM:
106 g_value_set_string (value, cert->cert_pem);
108 case PROP_CERT_PRIVATE_KEY_PEM:
109 g_value_set_string (value, cert->key_pem);
112 g_assert_not_reached ();
118 g_test_tls_certificate_set_property (GObject *object,
123 GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
127 case PROP_CERT_CERTIFICATE_PEM:
128 cert->cert_pem = g_value_dup_string (value);
130 case PROP_CERT_PRIVATE_KEY_PEM:
131 cert->key_pem = g_value_dup_string (value);
133 case PROP_CERT_CERTIFICATE:
134 case PROP_CERT_PRIVATE_KEY:
135 case PROP_CERT_ISSUER:
139 g_assert_not_reached ();
145 g_test_tls_certificate_finalize (GObject *object)
147 GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
149 g_free (cert->cert_pem);
150 g_free (cert->key_pem);
154 g_test_tls_certificate_class_init (GTestTlsCertificateClass *certificate_class)
156 GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
158 gobject_class->get_property = g_test_tls_certificate_get_property;
159 gobject_class->set_property = g_test_tls_certificate_set_property;
160 gobject_class->finalize = g_test_tls_certificate_finalize;
162 g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
163 g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
164 g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
165 g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
166 g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
170 g_test_tls_certificate_init (GTestTlsCertificate *certificate)
175 g_test_tls_certificate_initable_init (GInitable *initable,
176 GCancellable *cancellable,
183 g_test_tls_certificate_initable_iface_init (GInitableIface *iface)
185 iface->init = g_test_tls_certificate_initable_init;
188 /* Dummy connection type; since GTlsClientConnection and
189 * GTlsServerConnection are just interfaces, we can implement them
190 * both on a single object.
193 typedef struct _GTestTlsConnection GTestTlsConnection;
194 typedef struct _GTestTlsConnectionClass GTestTlsConnectionClass;
196 struct _GTestTlsConnection {
197 GTlsConnection parent_instance;
200 struct _GTestTlsConnectionClass {
201 GTlsConnectionClass parent_class;
208 PROP_CONN_BASE_IO_STREAM,
209 PROP_CONN_USE_SYSTEM_CERTDB,
210 PROP_CONN_REQUIRE_CLOSE_NOTIFY,
211 PROP_CONN_REHANDSHAKE_MODE,
212 PROP_CONN_CERTIFICATE,
213 PROP_CONN_PEER_CERTIFICATE,
214 PROP_CONN_PEER_CERTIFICATE_ERRORS,
215 PROP_CONN_VALIDATION_FLAGS,
216 PROP_CONN_SERVER_IDENTITY,
218 PROP_CONN_ACCEPTED_CAS,
219 PROP_CONN_AUTHENTICATION_MODE
222 static void g_test_tls_connection_initable_iface_init (GInitableIface *iface);
224 #define g_test_tls_connection_get_type _g_test_tls_connection_get_type
225 G_DEFINE_TYPE_WITH_CODE (GTestTlsConnection, g_test_tls_connection, G_TYPE_TLS_CONNECTION,
226 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
227 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
228 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
229 g_test_tls_connection_initable_iface_init);)
232 g_test_tls_connection_get_property (GObject *object,
240 g_test_tls_connection_set_property (GObject *object,
248 g_test_tls_connection_close (GIOStream *stream,
249 GCancellable *cancellable,
256 g_test_tls_connection_class_init (GTestTlsConnectionClass *connection_class)
258 GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
259 GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
261 gobject_class->get_property = g_test_tls_connection_get_property;
262 gobject_class->set_property = g_test_tls_connection_set_property;
264 /* Need to override this because when initable_init fails it will
265 * dispose the connection, which will close it, which would
266 * otherwise try to close its input/output streams, which don't
269 io_stream_class->close_fn = g_test_tls_connection_close;
271 g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
272 g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
273 g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
274 g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
275 g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
276 g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
277 g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
278 g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
279 g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
280 g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
281 g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
282 g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
286 g_test_tls_connection_init (GTestTlsConnection *connection)
291 g_test_tls_connection_initable_init (GInitable *initable,
292 GCancellable *cancellable,
295 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
296 "TLS Connection support is not available");
301 g_test_tls_connection_initable_iface_init (GInitableIface *iface)
303 iface->init = g_test_tls_connection_initable_init;
307 g_test_tls_connection_get_private_key_pem (GTlsCertificate *cert)
309 return ((GTestTlsCertificate *)cert)->key_pem;