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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "gtesttlsbackend.h"
25 static GType _g_test_tls_certificate_get_type (void);
26 static GType _g_test_tls_connection_get_type (void);
28 struct _GTestTlsBackend {
29 GObject parent_instance;
32 static void g_test_tls_backend_iface_init (GTlsBackendInterface *iface);
34 #define g_test_tls_backend_get_type _g_test_tls_backend_get_type
35 G_DEFINE_TYPE_WITH_CODE (GTestTlsBackend, g_test_tls_backend, G_TYPE_OBJECT,
36 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
37 g_test_tls_backend_iface_init)
38 g_io_extension_point_set_required_type (
39 g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME),
41 g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
47 g_test_tls_backend_init (GTestTlsBackend *backend)
52 g_test_tls_backend_class_init (GTestTlsBackendClass *backend_class)
57 g_test_tls_backend_iface_init (GTlsBackendInterface *iface)
59 iface->get_certificate_type = _g_test_tls_certificate_get_type;
60 iface->get_client_connection_type = _g_test_tls_connection_get_type;
61 iface->get_server_connection_type = _g_test_tls_connection_get_type;
64 /* Test certificate type */
66 typedef struct _GTestTlsCertificate GTestTlsCertificate;
67 typedef struct _GTestTlsCertificateClass GTestTlsCertificateClass;
69 struct _GTestTlsCertificate {
70 GTlsCertificate parent_instance;
75 struct _GTestTlsCertificateClass {
76 GTlsCertificateClass parent_class;
83 PROP_CERT_CERTIFICATE,
84 PROP_CERT_CERTIFICATE_BYTES,
85 PROP_CERT_CERTIFICATE_PEM,
86 PROP_CERT_PRIVATE_KEY,
87 PROP_CERT_PRIVATE_KEY_BYTES,
88 PROP_CERT_PRIVATE_KEY_PEM,
92 static void g_test_tls_certificate_initable_iface_init (GInitableIface *iface);
94 #define g_test_tls_certificate_get_type _g_test_tls_certificate_get_type
95 G_DEFINE_TYPE_WITH_CODE (GTestTlsCertificate, g_test_tls_certificate, G_TYPE_TLS_CERTIFICATE,
96 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
97 g_test_tls_certificate_initable_iface_init);)
100 g_test_tls_certificate_get_property (GObject *object,
105 GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
109 case PROP_CERT_CERTIFICATE_PEM:
110 g_value_set_string (value, cert->cert_pem);
112 case PROP_CERT_PRIVATE_KEY_PEM:
113 g_value_set_string (value, cert->key_pem);
116 g_assert_not_reached ();
122 g_test_tls_certificate_set_property (GObject *object,
127 GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
131 case PROP_CERT_CERTIFICATE_PEM:
132 cert->cert_pem = g_value_dup_string (value);
134 case PROP_CERT_PRIVATE_KEY_PEM:
135 cert->key_pem = g_value_dup_string (value);
137 case PROP_CERT_CERTIFICATE:
138 case PROP_CERT_CERTIFICATE_BYTES:
139 case PROP_CERT_PRIVATE_KEY:
140 case PROP_CERT_PRIVATE_KEY_BYTES:
141 case PROP_CERT_ISSUER:
145 g_assert_not_reached ();
151 g_test_tls_certificate_finalize (GObject *object)
153 GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
155 g_free (cert->cert_pem);
156 g_free (cert->key_pem);
160 g_test_tls_certificate_class_init (GTestTlsCertificateClass *certificate_class)
162 GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
164 gobject_class->get_property = g_test_tls_certificate_get_property;
165 gobject_class->set_property = g_test_tls_certificate_set_property;
166 gobject_class->finalize = g_test_tls_certificate_finalize;
168 g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
169 g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_BYTES, "certificate-bytes");
170 g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
171 g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
172 g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_BYTES, "private-key-bytes");
173 g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
174 g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
178 g_test_tls_certificate_init (GTestTlsCertificate *certificate)
183 g_test_tls_certificate_initable_init (GInitable *initable,
184 GCancellable *cancellable,
191 g_test_tls_certificate_initable_iface_init (GInitableIface *iface)
193 iface->init = g_test_tls_certificate_initable_init;
196 /* Dummy connection type; since GTlsClientConnection and
197 * GTlsServerConnection are just interfaces, we can implement them
198 * both on a single object.
201 typedef struct _GTestTlsConnection GTestTlsConnection;
202 typedef struct _GTestTlsConnectionClass GTestTlsConnectionClass;
204 struct _GTestTlsConnection {
205 GTlsConnection parent_instance;
208 struct _GTestTlsConnectionClass {
209 GTlsConnectionClass parent_class;
216 PROP_CONN_BASE_IO_STREAM,
217 PROP_CONN_USE_SYSTEM_CERTDB,
218 PROP_CONN_REQUIRE_CLOSE_NOTIFY,
219 PROP_CONN_REHANDSHAKE_MODE,
220 PROP_CONN_CERTIFICATE,
221 PROP_CONN_PEER_CERTIFICATE,
222 PROP_CONN_PEER_CERTIFICATE_ERRORS,
223 PROP_CONN_VALIDATION_FLAGS,
224 PROP_CONN_SERVER_IDENTITY,
226 PROP_CONN_ACCEPTED_CAS,
227 PROP_CONN_AUTHENTICATION_MODE
230 static void g_test_tls_connection_initable_iface_init (GInitableIface *iface);
232 #define g_test_tls_connection_get_type _g_test_tls_connection_get_type
233 G_DEFINE_TYPE_WITH_CODE (GTestTlsConnection, g_test_tls_connection, G_TYPE_TLS_CONNECTION,
234 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
235 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
236 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
237 g_test_tls_connection_initable_iface_init);)
240 g_test_tls_connection_get_property (GObject *object,
248 g_test_tls_connection_set_property (GObject *object,
256 g_test_tls_connection_close (GIOStream *stream,
257 GCancellable *cancellable,
264 g_test_tls_connection_class_init (GTestTlsConnectionClass *connection_class)
266 GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
267 GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
269 gobject_class->get_property = g_test_tls_connection_get_property;
270 gobject_class->set_property = g_test_tls_connection_set_property;
272 /* Need to override this because when initable_init fails it will
273 * dispose the connection, which will close it, which would
274 * otherwise try to close its input/output streams, which don't
277 io_stream_class->close_fn = g_test_tls_connection_close;
279 g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
280 g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
281 g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
282 g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
283 g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
284 g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
285 g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
286 g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
287 g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
288 g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
289 g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
290 g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
294 g_test_tls_connection_init (GTestTlsConnection *connection)
299 g_test_tls_connection_initable_init (GInitable *initable,
300 GCancellable *cancellable,
303 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
304 "TLS Connection support is not available");
309 g_test_tls_connection_initable_iface_init (GInitableIface *iface)
311 iface->init = g_test_tls_connection_initable_init;
315 g_test_tls_connection_get_private_key_pem (GTlsCertificate *cert)
317 return ((GTestTlsCertificate *)cert)->key_pem;