Imported Upstream version 2.66.6
[platform/upstream/glib.git] / gio / gdummytlsbackend.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
4  * Copyright © 2015 Collabora, Ltd.
5  *
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.1 of the License, or (at your option) any later version.
10  *
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.
15  *
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/>.
18  */
19
20 #include "config.h"
21
22 #include "gdummytlsbackend.h"
23
24 #include <glib.h>
25
26 #include "gasyncresult.h"
27 #include "gcancellable.h"
28 #include "ginitable.h"
29 #include "gdtlsclientconnection.h"
30 #include "gdtlsconnection.h"
31 #include "gdtlsserverconnection.h"
32 #include "gtlsbackend.h"
33 #include "gtlscertificate.h"
34 #include "gtlsclientconnection.h"
35 #include "gtlsdatabase.h"
36 #include "gtlsfiledatabase.h"
37 #include "gtlsserverconnection.h"
38
39 #include "giomodule.h"
40 #include "giomodule-priv.h"
41
42 #include "glibintl.h"
43
44 static GType _g_dummy_tls_certificate_get_type (void);
45 static GType _g_dummy_tls_connection_get_type (void);
46 static GType _g_dummy_dtls_connection_get_type (void);
47 static GType _g_dummy_tls_database_get_type (void);
48
49 struct _GDummyTlsBackend {
50   GObject       parent_instance;
51   GTlsDatabase *database;
52 };
53
54 static void g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface);
55
56 #define g_dummy_tls_backend_get_type _g_dummy_tls_backend_get_type
57 G_DEFINE_TYPE_WITH_CODE (GDummyTlsBackend, g_dummy_tls_backend, G_TYPE_OBJECT,
58                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
59                                                 g_dummy_tls_backend_iface_init)
60                          _g_io_modules_ensure_extension_points_registered ();
61                          g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
62                                                          g_define_type_id,
63                                                          "dummy",
64                                                          -100);)
65
66 static void
67 g_dummy_tls_backend_init (GDummyTlsBackend *dummy)
68 {
69 }
70
71 static void
72 g_dummy_tls_backend_finalize (GObject *object)
73 {
74   GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (object);
75
76   g_clear_object (&dummy->database);
77
78   G_OBJECT_CLASS (g_dummy_tls_backend_parent_class)->finalize (object);
79 }
80
81 static void
82 g_dummy_tls_backend_class_init (GDummyTlsBackendClass *backend_class)
83 {
84   GObjectClass *object_class = G_OBJECT_CLASS (backend_class);
85
86   object_class->finalize = g_dummy_tls_backend_finalize;
87 }
88
89 static GTlsDatabase *
90 g_dummy_tls_backend_get_default_database (GTlsBackend *backend)
91 {
92   GDummyTlsBackend *dummy = G_DUMMY_TLS_BACKEND (backend);
93
94   if (g_once_init_enter (&dummy->database))
95     {
96       GTlsDatabase *tlsdb;
97
98       tlsdb = g_object_new (_g_dummy_tls_database_get_type (), NULL);
99       g_once_init_leave (&dummy->database, tlsdb);
100     }
101
102   return g_object_ref (dummy->database);
103 }
104
105 static void
106 g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface)
107 {
108   iface->get_certificate_type = _g_dummy_tls_certificate_get_type;
109   iface->get_client_connection_type = _g_dummy_tls_connection_get_type;
110   iface->get_server_connection_type = _g_dummy_tls_connection_get_type;
111   iface->get_dtls_client_connection_type = _g_dummy_dtls_connection_get_type;
112   iface->get_dtls_server_connection_type = _g_dummy_dtls_connection_get_type;
113   iface->get_file_database_type = _g_dummy_tls_database_get_type;
114   iface->get_default_database = g_dummy_tls_backend_get_default_database;
115 }
116
117 /* Dummy certificate type */
118
119 typedef struct _GDummyTlsCertificate      GDummyTlsCertificate;
120 typedef struct _GDummyTlsCertificateClass GDummyTlsCertificateClass;
121
122 struct _GDummyTlsCertificate {
123   GTlsCertificate parent_instance;
124 };
125
126 struct _GDummyTlsCertificateClass {
127   GTlsCertificateClass parent_class;
128 };
129
130 enum
131 {
132   PROP_CERTIFICATE_0,
133
134   PROP_CERT_CERTIFICATE,
135   PROP_CERT_CERTIFICATE_PEM,
136   PROP_CERT_PRIVATE_KEY,
137   PROP_CERT_PRIVATE_KEY_PEM,
138   PROP_CERT_ISSUER
139 };
140
141 static void g_dummy_tls_certificate_initable_iface_init (GInitableIface *iface);
142
143 #define g_dummy_tls_certificate_get_type _g_dummy_tls_certificate_get_type
144 G_DEFINE_TYPE_WITH_CODE (GDummyTlsCertificate, g_dummy_tls_certificate, G_TYPE_TLS_CERTIFICATE,
145                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
146                                                 g_dummy_tls_certificate_initable_iface_init))
147
148 static void
149 g_dummy_tls_certificate_get_property (GObject    *object,
150                                       guint       prop_id,
151                                       GValue     *value,
152                                       GParamSpec *pspec)
153 {
154   /* We need to define this method to make GObject happy, but it will
155    * never be possible to construct a working GDummyTlsCertificate, so
156    * it doesn't have to do anything useful.
157    */
158 }
159
160 static void
161 g_dummy_tls_certificate_set_property (GObject      *object,
162                                       guint         prop_id,
163                                       const GValue *value,
164                                       GParamSpec   *pspec)
165 {
166   /* Just ignore all attempts to set properties. */
167 }
168
169 static void
170 g_dummy_tls_certificate_class_init (GDummyTlsCertificateClass *certificate_class)
171 {
172   GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
173
174   gobject_class->get_property = g_dummy_tls_certificate_get_property;
175   gobject_class->set_property = g_dummy_tls_certificate_set_property;
176
177   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
178   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
179   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
180   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
181   g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
182 }
183
184 static void
185 g_dummy_tls_certificate_init (GDummyTlsCertificate *certificate)
186 {
187 }
188
189 static gboolean
190 g_dummy_tls_certificate_initable_init (GInitable       *initable,
191                                        GCancellable    *cancellable,
192                                        GError         **error)
193 {
194   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
195                        _("TLS support is not available"));
196   return FALSE;
197 }
198
199 static void
200 g_dummy_tls_certificate_initable_iface_init (GInitableIface  *iface)
201 {
202   iface->init = g_dummy_tls_certificate_initable_init;
203 }
204
205 /* Dummy connection type; since GTlsClientConnection and
206  * GTlsServerConnection are just interfaces, we can implement them
207  * both on a single object.
208  */
209
210 typedef struct _GDummyTlsConnection      GDummyTlsConnection;
211 typedef struct _GDummyTlsConnectionClass GDummyTlsConnectionClass;
212
213 struct _GDummyTlsConnection {
214   GTlsConnection parent_instance;
215 };
216
217 struct _GDummyTlsConnectionClass {
218   GTlsConnectionClass parent_class;
219 };
220
221 enum
222 {
223   PROP_CONNECTION_0,
224
225   PROP_CONN_BASE_IO_STREAM,
226   PROP_CONN_USE_SYSTEM_CERTDB,
227   PROP_CONN_REQUIRE_CLOSE_NOTIFY,
228   PROP_CONN_REHANDSHAKE_MODE,
229   PROP_CONN_CERTIFICATE,
230   PROP_CONN_DATABASE,
231   PROP_CONN_INTERACTION,
232   PROP_CONN_PEER_CERTIFICATE,
233   PROP_CONN_PEER_CERTIFICATE_ERRORS,
234   PROP_CONN_VALIDATION_FLAGS,
235   PROP_CONN_SERVER_IDENTITY,
236   PROP_CONN_USE_SSL3,
237   PROP_CONN_ACCEPTED_CAS,
238   PROP_CONN_AUTHENTICATION_MODE,
239   PROP_CONN_ADVERTISED_PROTOCOLS,
240   PROP_CONN_NEGOTIATED_PROTOCOL,
241 };
242
243 static void g_dummy_tls_connection_initable_iface_init (GInitableIface *iface);
244
245 #define g_dummy_tls_connection_get_type _g_dummy_tls_connection_get_type
246 G_DEFINE_TYPE_WITH_CODE (GDummyTlsConnection, g_dummy_tls_connection, G_TYPE_TLS_CONNECTION,
247                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL)
248                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL)
249                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
250                                                 g_dummy_tls_connection_initable_iface_init))
251
252 static void
253 g_dummy_tls_connection_get_property (GObject    *object,
254                                      guint       prop_id,
255                                      GValue     *value,
256                                      GParamSpec *pspec)
257 {
258 }
259
260 static void
261 g_dummy_tls_connection_set_property (GObject      *object,
262                                      guint         prop_id,
263                                      const GValue *value,
264                                      GParamSpec   *pspec)
265 {
266 }
267
268 static gboolean
269 g_dummy_tls_connection_close (GIOStream     *stream,
270                               GCancellable  *cancellable,
271                               GError       **error)
272 {
273   return TRUE;
274 }
275
276 static void
277 g_dummy_tls_connection_class_init (GDummyTlsConnectionClass *connection_class)
278 {
279   GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
280   GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
281
282   gobject_class->get_property = g_dummy_tls_connection_get_property;
283   gobject_class->set_property = g_dummy_tls_connection_set_property;
284
285   /* Need to override this because when initable_init fails it will
286    * dispose the connection, which will close it, which would
287    * otherwise try to close its input/output streams, which don't
288    * exist.
289    */
290   io_stream_class->close_fn = g_dummy_tls_connection_close;
291
292   g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
293   g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
294   g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
295   g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
296   g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
297   g_object_class_override_property (gobject_class, PROP_CONN_DATABASE, "database");
298   g_object_class_override_property (gobject_class, PROP_CONN_INTERACTION, "interaction");
299   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
300   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
301   g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
302   g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
303   g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
304   g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
305   g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
306   g_object_class_override_property (gobject_class, PROP_CONN_ADVERTISED_PROTOCOLS, "advertised-protocols");
307   g_object_class_override_property (gobject_class, PROP_CONN_NEGOTIATED_PROTOCOL, "negotiated-protocol");
308 }
309
310 static void
311 g_dummy_tls_connection_init (GDummyTlsConnection *connection)
312 {
313 }
314
315 static gboolean
316 g_dummy_tls_connection_initable_init (GInitable       *initable,
317                                       GCancellable    *cancellable,
318                                       GError         **error)
319 {
320   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
321                        _("TLS support is not available"));
322   return FALSE;
323 }
324
325 static void
326 g_dummy_tls_connection_initable_iface_init (GInitableIface  *iface)
327 {
328   iface->init = g_dummy_tls_connection_initable_init;
329 }
330
331 /* Dummy DTLS connection type; since GDtlsClientConnection and
332  * GDtlsServerConnection are just interfaces, we can implement them
333  * both on a single object.
334  */
335
336 typedef struct _GDummyDtlsConnection      GDummyDtlsConnection;
337 typedef struct _GDummyDtlsConnectionClass GDummyDtlsConnectionClass;
338
339 struct _GDummyDtlsConnection {
340   GObject parent_instance;
341 };
342
343 struct _GDummyDtlsConnectionClass {
344   GObjectClass parent_class;
345 };
346
347 enum
348 {
349   PROP_DTLS_CONN_BASE_SOCKET = 1,
350   PROP_DTLS_CONN_REQUIRE_CLOSE_NOTIFY,
351   PROP_DTLS_CONN_REHANDSHAKE_MODE,
352   PROP_DTLS_CONN_CERTIFICATE,
353   PROP_DTLS_CONN_DATABASE,
354   PROP_DTLS_CONN_INTERACTION,
355   PROP_DTLS_CONN_PEER_CERTIFICATE,
356   PROP_DTLS_CONN_PEER_CERTIFICATE_ERRORS,
357   PROP_DTLS_CONN_VALIDATION_FLAGS,
358   PROP_DTLS_CONN_SERVER_IDENTITY,
359   PROP_DTLS_CONN_ENABLE_NEGOTIATION,
360   PROP_DTLS_CONN_ACCEPTED_CAS,
361   PROP_DTLS_CONN_AUTHENTICATION_MODE,
362 };
363
364 static void g_dummy_dtls_connection_initable_iface_init (GInitableIface *iface);
365
366 #define g_dummy_dtls_connection_get_type _g_dummy_dtls_connection_get_type
367 G_DEFINE_TYPE_WITH_CODE (GDummyDtlsConnection, g_dummy_dtls_connection, G_TYPE_OBJECT,
368                          G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_CONNECTION, NULL);
369                          G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_CLIENT_CONNECTION, NULL);
370                          G_IMPLEMENT_INTERFACE (G_TYPE_DTLS_SERVER_CONNECTION, NULL);
371                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
372                                                 g_dummy_dtls_connection_initable_iface_init);)
373
374 static void
375 g_dummy_dtls_connection_get_property (GObject    *object,
376                                       guint       prop_id,
377                                       GValue     *value,
378                                       GParamSpec *pspec)
379 {
380 }
381
382 static void
383 g_dummy_dtls_connection_set_property (GObject      *object,
384                                       guint         prop_id,
385                                       const GValue *value,
386                                       GParamSpec   *pspec)
387 {
388 }
389
390 static void
391 g_dummy_dtls_connection_class_init (GDummyDtlsConnectionClass *connection_class)
392 {
393   GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
394
395   gobject_class->get_property = g_dummy_dtls_connection_get_property;
396   gobject_class->set_property = g_dummy_dtls_connection_set_property;
397
398   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_BASE_SOCKET, "base-socket");
399   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
400   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
401   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_CERTIFICATE, "certificate");
402   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_DATABASE, "database");
403   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_INTERACTION, "interaction");
404   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_PEER_CERTIFICATE, "peer-certificate");
405   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
406   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_VALIDATION_FLAGS, "validation-flags");
407   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_SERVER_IDENTITY, "server-identity");
408   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_ACCEPTED_CAS, "accepted-cas");
409   g_object_class_override_property (gobject_class, PROP_DTLS_CONN_AUTHENTICATION_MODE, "authentication-mode");
410 }
411
412 static void
413 g_dummy_dtls_connection_init (GDummyDtlsConnection *connection)
414 {
415 }
416
417 static gboolean
418 g_dummy_dtls_connection_initable_init (GInitable       *initable,
419                                        GCancellable    *cancellable,
420                                        GError         **error)
421 {
422   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
423                        _("DTLS support is not available"));
424   return FALSE;
425 }
426
427 static void
428 g_dummy_dtls_connection_initable_iface_init (GInitableIface  *iface)
429 {
430   iface->init = g_dummy_dtls_connection_initable_init;
431 }
432
433 /* Dummy database type.
434  */
435
436 typedef struct _GDummyTlsDatabase      GDummyTlsDatabase;
437 typedef struct _GDummyTlsDatabaseClass GDummyTlsDatabaseClass;
438
439 struct _GDummyTlsDatabase {
440   GTlsDatabase parent_instance;
441 };
442
443 struct _GDummyTlsDatabaseClass {
444   GTlsDatabaseClass parent_class;
445 };
446
447 enum
448 {
449   PROP_DATABASE_0,
450
451   PROP_ANCHORS,
452 };
453
454 static void g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface *iface);
455 static void g_dummy_tls_database_initable_iface_init (GInitableIface *iface);
456
457 #define g_dummy_tls_database_get_type _g_dummy_tls_database_get_type
458 G_DEFINE_TYPE_WITH_CODE (GDummyTlsDatabase, g_dummy_tls_database, G_TYPE_TLS_DATABASE,
459                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_FILE_DATABASE,
460                                                 g_dummy_tls_database_file_database_iface_init)
461                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
462                                                 g_dummy_tls_database_initable_iface_init))
463
464
465 static void
466 g_dummy_tls_database_get_property (GObject    *object,
467                                    guint       prop_id,
468                                    GValue     *value,
469                                    GParamSpec *pspec)
470 {
471   /* We need to define this method to make GObject happy, but it will
472    * never be possible to construct a working GDummyTlsDatabase, so
473    * it doesn't have to do anything useful.
474    */
475 }
476
477 static void
478 g_dummy_tls_database_set_property (GObject      *object,
479                                    guint         prop_id,
480                                    const GValue *value,
481                                    GParamSpec   *pspec)
482 {
483   /* Just ignore all attempts to set properties. */
484 }
485
486 static void
487 g_dummy_tls_database_class_init (GDummyTlsDatabaseClass *database_class)
488 {
489   GObjectClass *gobject_class = G_OBJECT_CLASS (database_class);
490
491   gobject_class->get_property = g_dummy_tls_database_get_property;
492   gobject_class->set_property = g_dummy_tls_database_set_property;
493
494   g_object_class_override_property (gobject_class, PROP_ANCHORS, "anchors");
495 }
496
497 static void
498 g_dummy_tls_database_init (GDummyTlsDatabase *database)
499 {
500 }
501
502 static void
503 g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface  *iface)
504 {
505 }
506
507 static gboolean
508 g_dummy_tls_database_initable_init (GInitable       *initable,
509                                     GCancellable    *cancellable,
510                                     GError         **error)
511 {
512   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
513                        _("TLS support is not available"));
514   return FALSE;
515 }
516
517 static void
518 g_dummy_tls_database_initable_iface_init (GInitableIface  *iface)
519 {
520   iface->init = g_dummy_tls_database_initable_init;
521 }