Add initial TLS (SSL) support to gio
[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 "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
44 struct _GDummyTlsBackend {
45   GObject parent_instance;
46 };
47
48 static void g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface);
49
50 #define g_dummy_tls_backend_get_type _g_dummy_tls_backend_get_type
51 G_DEFINE_TYPE_WITH_CODE (GDummyTlsBackend, g_dummy_tls_backend, G_TYPE_OBJECT,
52                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
53                                                 g_dummy_tls_backend_iface_init)
54                          _g_io_modules_ensure_extension_points_registered ();
55                          g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
56                                                          g_define_type_id,
57                                                          "dummy",
58                                                          -100))
59
60 static void
61 g_dummy_tls_backend_init (GDummyTlsBackend *backend)
62 {
63 }
64
65 static void
66 g_dummy_tls_backend_class_init (GDummyTlsBackendClass *backend_class)
67 {
68 }
69
70 static void
71 g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface)
72 {
73   iface->get_certificate_type = _g_dummy_tls_certificate_get_type;
74   iface->get_client_connection_type = _g_dummy_tls_connection_get_type;
75   iface->get_server_connection_type = _g_dummy_tls_connection_get_type;
76 }
77
78 /* Dummy certificate type */
79
80 typedef struct _GDummyTlsCertificate      GDummyTlsCertificate;
81 typedef struct _GDummyTlsCertificateClass GDummyTlsCertificateClass;
82
83 struct _GDummyTlsCertificate {
84   GTlsCertificate parent_instance;
85 };
86
87 struct _GDummyTlsCertificateClass {
88   GTlsCertificateClass parent_class;
89 };
90
91 enum
92 {
93   PROP_CERTIFICATE_0,
94
95   PROP_CERTIFICATE,
96   PROP_CERTIFICATE_PEM,
97   PROP_PRIVATE_KEY,
98   PROP_PRIVATE_KEY_PEM
99 };
100
101 static void g_dummy_tls_certificate_initable_iface_init (GInitableIface *iface);
102
103 #define g_dummy_tls_certificate_get_type _g_dummy_tls_certificate_get_type
104 G_DEFINE_TYPE_WITH_CODE (GDummyTlsCertificate, g_dummy_tls_certificate, G_TYPE_TLS_CERTIFICATE,
105                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
106                                                 g_dummy_tls_certificate_initable_iface_init);)
107
108 static void
109 g_dummy_tls_certificate_get_property (GObject    *object,
110                                       guint       prop_id,
111                                       GValue     *value,
112                                       GParamSpec *pspec)
113 {
114   /* We need to define this method to make GObject happy, but it will
115    * never be possible to construct a working GDummyTlsCertificate, so
116    * it doesn't have to do anything useful.
117    */
118 }
119
120 static void
121 g_dummy_tls_certificate_set_property (GObject      *object,
122                                       guint         prop_id,
123                                       const GValue *value,
124                                       GParamSpec   *pspec)
125 {
126   /* Just ignore all attempts to set properties. */
127 }
128
129 static void
130 g_dummy_tls_certificate_class_init (GDummyTlsCertificateClass *certificate_class)
131 {
132   GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
133
134   gobject_class->get_property = g_dummy_tls_certificate_get_property;
135   gobject_class->set_property = g_dummy_tls_certificate_set_property;
136
137   g_object_class_override_property (gobject_class, PROP_CERTIFICATE, "certificate");
138   g_object_class_override_property (gobject_class, PROP_CERTIFICATE_PEM, "certificate-pem");
139   g_object_class_override_property (gobject_class, PROP_PRIVATE_KEY, "private-key");
140   g_object_class_override_property (gobject_class, PROP_PRIVATE_KEY_PEM, "private-key-pem");
141 }
142
143 static void
144 g_dummy_tls_certificate_init (GDummyTlsCertificate *certificate)
145 {
146 }
147
148 static gboolean
149 g_dummy_tls_certificate_initable_init (GInitable       *initable,
150                                        GCancellable    *cancellable,
151                                        GError         **error)
152 {
153   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
154                        _("TLS support is not available"));
155   return FALSE;
156 }
157
158 static void
159 g_dummy_tls_certificate_initable_iface_init (GInitableIface  *iface)
160 {
161   iface->init = g_dummy_tls_certificate_initable_init;
162 }
163
164 /* Dummy connection type; since GTlsClientConnection and
165  * GTlsServerConnection are just interfaces, we can implement them
166  * both on a single object.
167  */
168
169 typedef struct _GDummyTlsConnection      GDummyTlsConnection;
170 typedef struct _GDummyTlsConnectionClass GDummyTlsConnectionClass;
171
172 struct _GDummyTlsConnection {
173   GTlsConnection parent_instance;
174 };
175
176 struct _GDummyTlsConnectionClass {
177   GTlsConnectionClass parent_class;
178 };
179
180 enum
181 {
182   PROP_CONNECTION_0,
183
184   PROP_BASE_IO_STREAM,
185   PROP_REQUIRE_CLOSE_NOTIFY,
186   PROP_REHANDSHAKE_MODE,
187   PROP_VALIDATION_FLAGS,
188   PROP_SERVER_IDENTITY,
189   PROP_USE_SSL3,
190   PROP_ACCEPTED_CAS,
191   PROP_AUTHENTICATION_MODE
192 };
193
194 static void g_dummy_tls_connection_initable_iface_init (GInitableIface *iface);
195
196 #define g_dummy_tls_connection_get_type _g_dummy_tls_connection_get_type
197 G_DEFINE_TYPE_WITH_CODE (GDummyTlsConnection, g_dummy_tls_connection, G_TYPE_TLS_CONNECTION,
198                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
199                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
200                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
201                                                 g_dummy_tls_connection_initable_iface_init);)
202
203 static void
204 g_dummy_tls_connection_get_property (GObject    *object,
205                                      guint       prop_id,
206                                      GValue     *value,
207                                      GParamSpec *pspec)
208 {
209 }
210
211 static void
212 g_dummy_tls_connection_set_property (GObject      *object,
213                                      guint         prop_id,
214                                      const GValue *value,
215                                      GParamSpec   *pspec)
216 {
217 }
218
219 static gboolean
220 g_dummy_tls_connection_close (GIOStream     *stream,
221                               GCancellable  *cancellable,
222                               GError       **error)
223 {
224   return TRUE;
225 }
226
227 static void
228 g_dummy_tls_connection_class_init (GDummyTlsConnectionClass *connection_class)
229 {
230   GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
231   GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
232
233   gobject_class->get_property = g_dummy_tls_connection_get_property;
234   gobject_class->set_property = g_dummy_tls_connection_set_property;
235
236   /* Need to override this because when initable_init fails it will
237    * dispose the connection, which will close it, which would
238    * otherwise try to close its input/output streams, which don't
239    * exist.
240    */
241   io_stream_class->close_fn = g_dummy_tls_connection_close;
242
243   g_object_class_override_property (gobject_class, PROP_BASE_IO_STREAM, "base-io-stream");
244   g_object_class_override_property (gobject_class, PROP_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
245   g_object_class_override_property (gobject_class, PROP_REHANDSHAKE_MODE, "rehandshake-mode");
246   g_object_class_override_property (gobject_class, PROP_VALIDATION_FLAGS, "validation-flags");
247   g_object_class_override_property (gobject_class, PROP_SERVER_IDENTITY, "server-identity");
248   g_object_class_override_property (gobject_class, PROP_USE_SSL3, "use-ssl3");
249   g_object_class_override_property (gobject_class, PROP_ACCEPTED_CAS, "accepted-cas");
250   g_object_class_override_property (gobject_class, PROP_AUTHENTICATION_MODE, "authentication-mode");
251
252 }
253
254 static void
255 g_dummy_tls_connection_init (GDummyTlsConnection *connection)
256 {
257 }
258
259 static gboolean
260 g_dummy_tls_connection_initable_init (GInitable       *initable,
261                                       GCancellable    *cancellable,
262                                       GError         **error)
263 {
264   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_MISC,
265                        _("TLS support is not available"));
266   return FALSE;
267 }
268
269 static void
270 g_dummy_tls_connection_initable_iface_init (GInitableIface  *iface)
271 {
272   iface->init = g_dummy_tls_connection_initable_init;
273 }
274