Fix the docs build
[platform/upstream/glib.git] / gio / gdummytlsbackend.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "config.h"
20
21 #include "gdummytlsbackend.h"
22
23 #include <glib.h>
24
25 #include "gasyncresult.h"
26 #include "gcancellable.h"
27 #include "ginitable.h"
28 #include "gtlsbackend.h"
29 #include "gtlscertificate.h"
30 #include "gtlsclientconnection.h"
31 #include "gtlsdatabase.h"
32 #include "gtlsfiledatabase.h"
33 #include "gtlsserverconnection.h"
34 #include "gsimpleasyncresult.h"
35
36 #include "giomodule.h"
37 #include "giomodule-priv.h"
38
39 #include "glibintl.h"
40
41 static GType _g_dummy_tls_certificate_get_type (void);
42 static GType _g_dummy_tls_connection_get_type (void);
43 static GType _g_dummy_tls_database_get_type (void);
44
45 struct _GDummyTlsBackend {
46   GObject       parent_instance;
47   GTlsDatabase *database;
48 };
49
50 static void g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface);
51
52 #define g_dummy_tls_backend_get_type _g_dummy_tls_backend_get_type
53 G_DEFINE_TYPE_WITH_CODE (GDummyTlsBackend, g_dummy_tls_backend, G_TYPE_OBJECT,
54                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
55                                                 g_dummy_tls_backend_iface_init)
56                          _g_io_modules_ensure_extension_points_registered ();
57                          g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
58                                                          g_define_type_id,
59                                                          "dummy",
60                                                          -100))
61
62 static void
63 g_dummy_tls_backend_init (GDummyTlsBackend *dummy)
64 {
65 }
66
67 static void
68 g_dummy_tls_backend_finalize (GObject *object)
69 {
70   GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (object);
71
72   g_clear_object (&dummy->database);
73
74   G_OBJECT_CLASS (g_dummy_tls_backend_parent_class)->finalize (object);
75 }
76
77 static void
78 g_dummy_tls_backend_class_init (GDummyTlsBackendClass *backend_class)
79 {
80   GObjectClass *object_class = G_OBJECT_CLASS (backend_class);
81
82   object_class->finalize = g_dummy_tls_backend_finalize;
83 }
84
85 static GTlsDatabase *
86 g_dummy_tls_backend_get_default_database (GTlsBackend *backend)
87 {
88   GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (backend);
89
90   if (g_once_init_enter (&dummy->database))
91     {
92       GTlsDatabase *tlsdb;
93
94       tlsdb = g_object_new (_g_dummy_tls_database_get_type (), NULL);
95       g_once_init_leave (&dummy->database, tlsdb);
96     }
97
98   return g_object_ref (dummy->database);
99 }
100
101 static void
102 g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface)
103 {
104   iface->get_certificate_type = _g_dummy_tls_certificate_get_type;
105   iface->get_client_connection_type = _g_dummy_tls_connection_get_type;
106   iface->get_server_connection_type = _g_dummy_tls_connection_get_type;
107   iface->get_file_database_type = _g_dummy_tls_database_get_type;
108   iface->get_default_database = g_dummy_tls_backend_get_default_database;
109 }
110
111 /* Dummy certificate type */
112
113 typedef struct _GDummyTlsCertificate      GDummyTlsCertificate;
114 typedef struct _GDummyTlsCertificateClass GDummyTlsCertificateClass;
115
116 struct _GDummyTlsCertificate {
117   GTlsCertificate parent_instance;
118 };
119
120 struct _GDummyTlsCertificateClass {
121   GTlsCertificateClass parent_class;
122 };
123
124 enum
125 {
126   PROP_CERTIFICATE_0,
127
128   PROP_CERT_CERTIFICATE,
129   PROP_CERT_CERTIFICATE_PEM,
130   PROP_CERT_PRIVATE_KEY,
131   PROP_CERT_PRIVATE_KEY_PEM,
132   PROP_CERT_ISSUER
133 };
134
135 static void g_dummy_tls_certificate_initable_iface_init (GInitableIface *iface);
136
137 #define g_dummy_tls_certificate_get_type _g_dummy_tls_certificate_get_type
138 G_DEFINE_TYPE_WITH_CODE (GDummyTlsCertificate, g_dummy_tls_certificate, G_TYPE_TLS_CERTIFICATE,
139                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
140                                                 g_dummy_tls_certificate_initable_iface_init);)
141
142 static void
143 g_dummy_tls_certificate_get_property (GObject    *object,
144                                       guint       prop_id,
145                                       GValue     *value,
146                                       GParamSpec *pspec)
147 {
148   /* We need to define this method to make GObject happy, but it will
149    * never be possible to construct a working GDummyTlsCertificate, so
150    * it doesn't have to do anything useful.
151    */
152 }
153
154 static void
155 g_dummy_tls_certificate_set_property (GObject      *object,
156                                       guint         prop_id,
157                                       const GValue *value,
158                                       GParamSpec   *pspec)
159 {
160   /* Just ignore all attempts to set properties. */
161 }
162
163 static void
164 g_dummy_tls_certificate_class_init (GDummyTlsCertificateClass *certificate_class)
165 {
166   GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
167
168   gobject_class->get_property = g_dummy_tls_certificate_get_property;
169   gobject_class->set_property = g_dummy_tls_certificate_set_property;
170
171   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
172   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
173   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
174   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
175   g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
176 }
177
178 static void
179 g_dummy_tls_certificate_init (GDummyTlsCertificate *certificate)
180 {
181 }
182
183 static gboolean
184 g_dummy_tls_certificate_initable_init (GInitable       *initable,
185                                        GCancellable    *cancellable,
186                                        GError         **error)
187 {
188   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
189                        _("TLS support is not available"));
190   return FALSE;
191 }
192
193 static void
194 g_dummy_tls_certificate_initable_iface_init (GInitableIface  *iface)
195 {
196   iface->init = g_dummy_tls_certificate_initable_init;
197 }
198
199 /* Dummy connection type; since GTlsClientConnection and
200  * GTlsServerConnection are just interfaces, we can implement them
201  * both on a single object.
202  */
203
204 typedef struct _GDummyTlsConnection      GDummyTlsConnection;
205 typedef struct _GDummyTlsConnectionClass GDummyTlsConnectionClass;
206
207 struct _GDummyTlsConnection {
208   GTlsConnection parent_instance;
209 };
210
211 struct _GDummyTlsConnectionClass {
212   GTlsConnectionClass parent_class;
213 };
214
215 enum
216 {
217   PROP_CONNECTION_0,
218
219   PROP_CONN_BASE_IO_STREAM,
220   PROP_CONN_USE_SYSTEM_CERTDB,
221   PROP_CONN_REQUIRE_CLOSE_NOTIFY,
222   PROP_CONN_REHANDSHAKE_MODE,
223   PROP_CONN_CERTIFICATE,
224   PROP_CONN_DATABASE,
225   PROP_CONN_PEER_CERTIFICATE,
226   PROP_CONN_PEER_CERTIFICATE_ERRORS,
227   PROP_CONN_VALIDATION_FLAGS,
228   PROP_CONN_SERVER_IDENTITY,
229   PROP_CONN_USE_SSL3,
230   PROP_CONN_ACCEPTED_CAS,
231   PROP_CONN_AUTHENTICATION_MODE
232 };
233
234 static void g_dummy_tls_connection_initable_iface_init (GInitableIface *iface);
235
236 #define g_dummy_tls_connection_get_type _g_dummy_tls_connection_get_type
237 G_DEFINE_TYPE_WITH_CODE (GDummyTlsConnection, g_dummy_tls_connection, G_TYPE_TLS_CONNECTION,
238                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
239                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
240                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
241                                                 g_dummy_tls_connection_initable_iface_init);)
242
243 static void
244 g_dummy_tls_connection_get_property (GObject    *object,
245                                      guint       prop_id,
246                                      GValue     *value,
247                                      GParamSpec *pspec)
248 {
249 }
250
251 static void
252 g_dummy_tls_connection_set_property (GObject      *object,
253                                      guint         prop_id,
254                                      const GValue *value,
255                                      GParamSpec   *pspec)
256 {
257 }
258
259 static gboolean
260 g_dummy_tls_connection_close (GIOStream     *stream,
261                               GCancellable  *cancellable,
262                               GError       **error)
263 {
264   return TRUE;
265 }
266
267 static void
268 g_dummy_tls_connection_class_init (GDummyTlsConnectionClass *connection_class)
269 {
270   GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
271   GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
272
273   gobject_class->get_property = g_dummy_tls_connection_get_property;
274   gobject_class->set_property = g_dummy_tls_connection_set_property;
275
276   /* Need to override this because when initable_init fails it will
277    * dispose the connection, which will close it, which would
278    * otherwise try to close its input/output streams, which don't
279    * exist.
280    */
281   io_stream_class->close_fn = g_dummy_tls_connection_close;
282
283   g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
284   g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
285   g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
286   g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
287   g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
288   g_object_class_override_property (gobject_class, PROP_CONN_DATABASE, "database");
289   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
290   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
291   g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
292   g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
293   g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
294   g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
295   g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
296 }
297
298 static void
299 g_dummy_tls_connection_init (GDummyTlsConnection *connection)
300 {
301 }
302
303 static gboolean
304 g_dummy_tls_connection_initable_init (GInitable       *initable,
305                                       GCancellable    *cancellable,
306                                       GError         **error)
307 {
308   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
309                        _("TLS support is not available"));
310   return FALSE;
311 }
312
313 static void
314 g_dummy_tls_connection_initable_iface_init (GInitableIface  *iface)
315 {
316   iface->init = g_dummy_tls_connection_initable_init;
317 }
318
319 /* Dummy database type.
320  */
321
322 typedef struct _GDummyTlsDatabase      GDummyTlsDatabase;
323 typedef struct _GDummyTlsDatabaseClass GDummyTlsDatabaseClass;
324
325 struct _GDummyTlsDatabase {
326   GTlsDatabase parent_instance;
327 };
328
329 struct _GDummyTlsDatabaseClass {
330   GTlsDatabaseClass parent_class;
331 };
332
333 enum
334 {
335   PROP_DATABASE_0,
336
337   PROP_ANCHORS,
338 };
339
340 static void g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface *iface);
341 static void g_dummy_tls_database_initable_iface_init (GInitableIface *iface);
342
343 #define g_dummy_tls_database_get_type _g_dummy_tls_database_get_type
344 G_DEFINE_TYPE_WITH_CODE (GDummyTlsDatabase, g_dummy_tls_database, G_TYPE_TLS_DATABASE,
345                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_FILE_DATABASE,
346                                                 g_dummy_tls_database_file_database_iface_init);
347                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
348                                                 g_dummy_tls_database_initable_iface_init);)
349
350
351 static void
352 g_dummy_tls_database_get_property (GObject    *object,
353                                    guint       prop_id,
354                                    GValue     *value,
355                                    GParamSpec *pspec)
356 {
357   /* We need to define this method to make GObject happy, but it will
358    * never be possible to construct a working GDummyTlsDatabase, so
359    * it doesn't have to do anything useful.
360    */
361 }
362
363 static void
364 g_dummy_tls_database_set_property (GObject      *object,
365                                    guint         prop_id,
366                                    const GValue *value,
367                                    GParamSpec   *pspec)
368 {
369   /* Just ignore all attempts to set properties. */
370 }
371
372 static void
373 g_dummy_tls_database_class_init (GDummyTlsDatabaseClass *database_class)
374 {
375   GObjectClass *gobject_class = G_OBJECT_CLASS (database_class);
376
377   gobject_class->get_property = g_dummy_tls_database_get_property;
378   gobject_class->set_property = g_dummy_tls_database_set_property;
379
380   g_object_class_override_property (gobject_class, PROP_ANCHORS, "anchors");
381 }
382
383 static void
384 g_dummy_tls_database_init (GDummyTlsDatabase *database)
385 {
386 }
387
388 static void
389 g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface  *iface)
390 {
391 }
392
393 static gboolean
394 g_dummy_tls_database_initable_init (GInitable       *initable,
395                                     GCancellable    *cancellable,
396                                     GError         **error)
397 {
398   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
399                        _("TLS support is not available"));
400   return FALSE;
401 }
402
403 static void
404 g_dummy_tls_database_initable_iface_init (GInitableIface  *iface)
405 {
406   iface->init = g_dummy_tls_database_initable_init;
407 }