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