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
;
71 GTlsCertificate
*issuer
;
74 struct _GTestTlsCertificateClass
{
75 GTlsCertificateClass parent_class
;
82 PROP_CERT_CERTIFICATE
,
83 PROP_CERT_CERTIFICATE_PEM
,
84 PROP_CERT_PRIVATE_KEY
,
85 PROP_CERT_PRIVATE_KEY_PEM
,
89 static void g_test_tls_certificate_initable_iface_init (GInitableIface
*iface
);
91 #define g_test_tls_certificate_get_type _g_test_tls_certificate_get_type
92 G_DEFINE_TYPE_WITH_CODE (GTestTlsCertificate
, g_test_tls_certificate
, G_TYPE_TLS_CERTIFICATE
,
93 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE
,
94 g_test_tls_certificate_initable_iface_init
);)
96 static GTlsCertificateFlags
97 g_test_tls_certificate_verify (GTlsCertificate
*cert
,
98 GSocketConnectable
*identity
,
99 GTlsCertificate
*trusted_ca
)
101 /* For now, all of the tests expect the certificate to verify */
106 g_test_tls_certificate_get_property (GObject
*object
,
111 GTestTlsCertificate
*cert
= (GTestTlsCertificate
*) object
;
115 case PROP_CERT_CERTIFICATE_PEM
:
116 g_value_set_string (value
, cert
->cert_pem
);
118 case PROP_CERT_PRIVATE_KEY_PEM
:
119 g_value_set_string (value
, cert
->key_pem
);
121 case PROP_CERT_ISSUER
:
122 g_value_set_object (value
, cert
->issuer
);
125 g_assert_not_reached ();
131 g_test_tls_certificate_set_property (GObject
*object
,
136 GTestTlsCertificate
*cert
= (GTestTlsCertificate
*) object
;
140 case PROP_CERT_CERTIFICATE_PEM
:
141 cert
->cert_pem
= g_value_dup_string (value
);
143 case PROP_CERT_PRIVATE_KEY_PEM
:
144 cert
->key_pem
= g_value_dup_string (value
);
146 case PROP_CERT_ISSUER
:
147 cert
->issuer
= g_value_dup_object (value
);
149 case PROP_CERT_CERTIFICATE
:
150 case PROP_CERT_PRIVATE_KEY
:
154 g_assert_not_reached ();
160 g_test_tls_certificate_finalize (GObject
*object
)
162 GTestTlsCertificate
*cert
= (GTestTlsCertificate
*) object
;
164 g_free (cert
->cert_pem
);
165 g_free (cert
->key_pem
);
166 g_clear_object (&cert
->issuer
);
170 g_test_tls_certificate_class_init (GTestTlsCertificateClass
*test_class
)
172 GObjectClass
*gobject_class
= G_OBJECT_CLASS (test_class
);
173 GTlsCertificateClass
*certificate_class
= G_TLS_CERTIFICATE_CLASS (test_class
);
175 gobject_class
->get_property
= g_test_tls_certificate_get_property
;
176 gobject_class
->set_property
= g_test_tls_certificate_set_property
;
177 gobject_class
->finalize
= g_test_tls_certificate_finalize
;
179 certificate_class
->verify
= g_test_tls_certificate_verify
;
181 g_object_class_override_property (gobject_class
, PROP_CERT_CERTIFICATE
, "certificate");
182 g_object_class_override_property (gobject_class
, PROP_CERT_CERTIFICATE_PEM
, "certificate-pem");
183 g_object_class_override_property (gobject_class
, PROP_CERT_PRIVATE_KEY
, "private-key");
184 g_object_class_override_property (gobject_class
, PROP_CERT_PRIVATE_KEY_PEM
, "private-key-pem");
185 g_object_class_override_property (gobject_class
, PROP_CERT_ISSUER
, "issuer");
189 g_test_tls_certificate_init (GTestTlsCertificate
*certificate
)
194 g_test_tls_certificate_initable_init (GInitable
*initable
,
195 GCancellable
*cancellable
,
202 g_test_tls_certificate_initable_iface_init (GInitableIface
*iface
)
204 iface
->init
= g_test_tls_certificate_initable_init
;
207 /* Dummy connection type; since GTlsClientConnection and
208 * GTlsServerConnection are just interfaces, we can implement them
209 * both on a single object.
212 typedef struct _GTestTlsConnection GTestTlsConnection
;
213 typedef struct _GTestTlsConnectionClass GTestTlsConnectionClass
;
215 struct _GTestTlsConnection
{
216 GTlsConnection parent_instance
;
219 struct _GTestTlsConnectionClass
{
220 GTlsConnectionClass parent_class
;
227 PROP_CONN_BASE_IO_STREAM
,
228 PROP_CONN_USE_SYSTEM_CERTDB
,
229 PROP_CONN_REQUIRE_CLOSE_NOTIFY
,
230 PROP_CONN_REHANDSHAKE_MODE
,
231 PROP_CONN_CERTIFICATE
,
232 PROP_CONN_PEER_CERTIFICATE
,
233 PROP_CONN_PEER_CERTIFICATE_ERRORS
,
234 PROP_CONN_VALIDATION_FLAGS
,
235 PROP_CONN_SERVER_IDENTITY
,
237 PROP_CONN_ACCEPTED_CAS
,
238 PROP_CONN_AUTHENTICATION_MODE
241 static void g_test_tls_connection_initable_iface_init (GInitableIface
*iface
);
243 #define g_test_tls_connection_get_type _g_test_tls_connection_get_type
244 G_DEFINE_TYPE_WITH_CODE (GTestTlsConnection
, g_test_tls_connection
, G_TYPE_TLS_CONNECTION
,
245 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION
, NULL
);
246 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION
, NULL
);
247 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE
,
248 g_test_tls_connection_initable_iface_init
);)
251 g_test_tls_connection_get_property (GObject
*object
,
259 g_test_tls_connection_set_property (GObject
*object
,
267 g_test_tls_connection_close (GIOStream
*stream
,
268 GCancellable
*cancellable
,
275 g_test_tls_connection_class_init (GTestTlsConnectionClass
*connection_class
)
277 GObjectClass
*gobject_class
= G_OBJECT_CLASS (connection_class
);
278 GIOStreamClass
*io_stream_class
= G_IO_STREAM_CLASS (connection_class
);
280 gobject_class
->get_property
= g_test_tls_connection_get_property
;
281 gobject_class
->set_property
= g_test_tls_connection_set_property
;
283 /* Need to override this because when initable_init fails it will
284 * dispose the connection, which will close it, which would
285 * otherwise try to close its input/output streams, which don't
288 io_stream_class
->close_fn
= g_test_tls_connection_close
;
290 g_object_class_override_property (gobject_class
, PROP_CONN_BASE_IO_STREAM
, "base-io-stream");
291 g_object_class_override_property (gobject_class
, PROP_CONN_USE_SYSTEM_CERTDB
, "use-system-certdb");
292 g_object_class_override_property (gobject_class
, PROP_CONN_REQUIRE_CLOSE_NOTIFY
, "require-close-notify");
293 g_object_class_override_property (gobject_class
, PROP_CONN_REHANDSHAKE_MODE
, "rehandshake-mode");
294 g_object_class_override_property (gobject_class
, PROP_CONN_CERTIFICATE
, "certificate");
295 g_object_class_override_property (gobject_class
, PROP_CONN_PEER_CERTIFICATE
, "peer-certificate");
296 g_object_class_override_property (gobject_class
, PROP_CONN_PEER_CERTIFICATE_ERRORS
, "peer-certificate-errors");
297 g_object_class_override_property (gobject_class
, PROP_CONN_VALIDATION_FLAGS
, "validation-flags");
298 g_object_class_override_property (gobject_class
, PROP_CONN_SERVER_IDENTITY
, "server-identity");
299 g_object_class_override_property (gobject_class
, PROP_CONN_USE_SSL3
, "use-ssl3");
300 g_object_class_override_property (gobject_class
, PROP_CONN_ACCEPTED_CAS
, "accepted-cas");
301 g_object_class_override_property (gobject_class
, PROP_CONN_AUTHENTICATION_MODE
, "authentication-mode");
305 g_test_tls_connection_init (GTestTlsConnection
*connection
)
310 g_test_tls_connection_initable_init (GInitable
*initable
,
311 GCancellable
*cancellable
,
314 g_set_error_literal (error
, G_TLS_ERROR
, G_TLS_ERROR_UNAVAILABLE
,
315 "TLS Connection support is not available");
320 g_test_tls_connection_initable_iface_init (GInitableIface
*iface
)
322 iface
->init
= g_test_tls_connection_initable_init
;
326 g_test_tls_connection_get_private_key_pem (GTlsCertificate
*cert
)
328 return ((GTestTlsCertificate
*)cert
)->key_pem
;