gtlscertificate: Add certificate-bytes and private-key-bytes props
[platform/upstream/glib.git] / gio / tests / gtesttlsbackend.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2011 Collabora Ltd.
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 "gtesttlsbackend.h"
22
23 #include <glib.h>
24
25 static GType _g_test_tls_certificate_get_type (void);
26 static GType _g_test_tls_connection_get_type (void);
27
28 struct _GTestTlsBackend {
29   GObject parent_instance;
30 };
31
32 static void g_test_tls_backend_iface_init (GTlsBackendInterface *iface);
33
34 #define g_test_tls_backend_get_type _g_test_tls_backend_get_type
35 G_DEFINE_TYPE_WITH_CODE (GTestTlsBackend, g_test_tls_backend, G_TYPE_OBJECT,
36                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
37                                                 g_test_tls_backend_iface_init)
38                          g_io_extension_point_set_required_type (
39                            g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME),
40                            G_TYPE_TLS_BACKEND);
41                          g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
42                                                          g_define_type_id,
43                                                          "test",
44                                                          999))
45
46 static void
47 g_test_tls_backend_init (GTestTlsBackend *backend)
48 {
49 }
50
51 static void
52 g_test_tls_backend_class_init (GTestTlsBackendClass *backend_class)
53 {
54 }
55
56 static void
57 g_test_tls_backend_iface_init (GTlsBackendInterface *iface)
58 {
59   iface->get_certificate_type = _g_test_tls_certificate_get_type;
60   iface->get_client_connection_type = _g_test_tls_connection_get_type;
61   iface->get_server_connection_type = _g_test_tls_connection_get_type;
62 }
63
64 /* Test certificate type */
65
66 typedef struct _GTestTlsCertificate      GTestTlsCertificate;
67 typedef struct _GTestTlsCertificateClass GTestTlsCertificateClass;
68
69 struct _GTestTlsCertificate {
70   GTlsCertificate parent_instance;
71   gchar *key_pem;
72   gchar *cert_pem;
73 };
74
75 struct _GTestTlsCertificateClass {
76   GTlsCertificateClass parent_class;
77 };
78
79 enum
80 {
81   PROP_CERTIFICATE_0,
82
83   PROP_CERT_CERTIFICATE,
84   PROP_CERT_CERTIFICATE_BYTES,
85   PROP_CERT_CERTIFICATE_PEM,
86   PROP_CERT_PRIVATE_KEY,
87   PROP_CERT_PRIVATE_KEY_BYTES,
88   PROP_CERT_PRIVATE_KEY_PEM,
89   PROP_CERT_ISSUER
90 };
91
92 static void g_test_tls_certificate_initable_iface_init (GInitableIface *iface);
93
94 #define g_test_tls_certificate_get_type _g_test_tls_certificate_get_type
95 G_DEFINE_TYPE_WITH_CODE (GTestTlsCertificate, g_test_tls_certificate, G_TYPE_TLS_CERTIFICATE,
96                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
97                                                 g_test_tls_certificate_initable_iface_init);)
98
99 static void
100 g_test_tls_certificate_get_property (GObject    *object,
101                                       guint       prop_id,
102                                       GValue     *value,
103                                       GParamSpec *pspec)
104 {
105   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
106
107   switch (prop_id)
108     {
109     case PROP_CERT_CERTIFICATE_PEM:
110       g_value_set_string (value, cert->cert_pem);
111       break;
112     case PROP_CERT_PRIVATE_KEY_PEM:
113       g_value_set_string (value, cert->key_pem);
114       break;
115     default:
116       g_assert_not_reached ();
117       break;
118     }
119 }
120
121 static void
122 g_test_tls_certificate_set_property (GObject      *object,
123                                       guint         prop_id,
124                                       const GValue *value,
125                                       GParamSpec   *pspec)
126 {
127   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
128
129   switch (prop_id)
130     {
131     case PROP_CERT_CERTIFICATE_PEM:
132       cert->cert_pem = g_value_dup_string (value);
133       break;
134     case PROP_CERT_PRIVATE_KEY_PEM:
135       cert->key_pem = g_value_dup_string (value);
136       break;
137     case PROP_CERT_CERTIFICATE:
138     case PROP_CERT_CERTIFICATE_BYTES:
139     case PROP_CERT_PRIVATE_KEY:
140     case PROP_CERT_PRIVATE_KEY_BYTES:
141     case PROP_CERT_ISSUER:
142       /* ignore */
143       break;
144     default:
145       g_assert_not_reached ();
146       break;
147     }
148 }
149
150 static void
151 g_test_tls_certificate_finalize (GObject *object)
152 {
153   GTestTlsCertificate *cert = (GTestTlsCertificate *) object;
154
155   g_free (cert->cert_pem);
156   g_free (cert->key_pem);
157 }
158
159 static void
160 g_test_tls_certificate_class_init (GTestTlsCertificateClass *certificate_class)
161 {
162   GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);
163
164   gobject_class->get_property = g_test_tls_certificate_get_property;
165   gobject_class->set_property = g_test_tls_certificate_set_property;
166   gobject_class->finalize = g_test_tls_certificate_finalize;
167
168   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
169   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_BYTES, "certificate-bytes");
170   g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
171   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
172   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_BYTES, "private-key-bytes");
173   g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
174   g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
175 }
176
177 static void
178 g_test_tls_certificate_init (GTestTlsCertificate *certificate)
179 {
180 }
181
182 static gboolean
183 g_test_tls_certificate_initable_init (GInitable       *initable,
184                                        GCancellable    *cancellable,
185                                        GError         **error)
186 {
187   return TRUE;
188 }
189
190 static void
191 g_test_tls_certificate_initable_iface_init (GInitableIface  *iface)
192 {
193   iface->init = g_test_tls_certificate_initable_init;
194 }
195
196 /* Dummy connection type; since GTlsClientConnection and
197  * GTlsServerConnection are just interfaces, we can implement them
198  * both on a single object.
199  */
200
201 typedef struct _GTestTlsConnection      GTestTlsConnection;
202 typedef struct _GTestTlsConnectionClass GTestTlsConnectionClass;
203
204 struct _GTestTlsConnection {
205   GTlsConnection parent_instance;
206 };
207
208 struct _GTestTlsConnectionClass {
209   GTlsConnectionClass parent_class;
210 };
211
212 enum
213 {
214   PROP_CONNECTION_0,
215
216   PROP_CONN_BASE_IO_STREAM,
217   PROP_CONN_USE_SYSTEM_CERTDB,
218   PROP_CONN_REQUIRE_CLOSE_NOTIFY,
219   PROP_CONN_REHANDSHAKE_MODE,
220   PROP_CONN_CERTIFICATE,
221   PROP_CONN_PEER_CERTIFICATE,
222   PROP_CONN_PEER_CERTIFICATE_ERRORS,
223   PROP_CONN_VALIDATION_FLAGS,
224   PROP_CONN_SERVER_IDENTITY,
225   PROP_CONN_USE_SSL3,
226   PROP_CONN_ACCEPTED_CAS,
227   PROP_CONN_AUTHENTICATION_MODE
228 };
229
230 static void g_test_tls_connection_initable_iface_init (GInitableIface *iface);
231
232 #define g_test_tls_connection_get_type _g_test_tls_connection_get_type
233 G_DEFINE_TYPE_WITH_CODE (GTestTlsConnection, g_test_tls_connection, G_TYPE_TLS_CONNECTION,
234                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
235                          G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
236                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
237                                                 g_test_tls_connection_initable_iface_init);)
238
239 static void
240 g_test_tls_connection_get_property (GObject    *object,
241                                      guint       prop_id,
242                                      GValue     *value,
243                                      GParamSpec *pspec)
244 {
245 }
246
247 static void
248 g_test_tls_connection_set_property (GObject      *object,
249                                      guint         prop_id,
250                                      const GValue *value,
251                                      GParamSpec   *pspec)
252 {
253 }
254
255 static gboolean
256 g_test_tls_connection_close (GIOStream     *stream,
257                               GCancellable  *cancellable,
258                               GError       **error)
259 {
260   return TRUE;
261 }
262
263 static void
264 g_test_tls_connection_class_init (GTestTlsConnectionClass *connection_class)
265 {
266   GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
267   GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);
268
269   gobject_class->get_property = g_test_tls_connection_get_property;
270   gobject_class->set_property = g_test_tls_connection_set_property;
271
272   /* Need to override this because when initable_init fails it will
273    * dispose the connection, which will close it, which would
274    * otherwise try to close its input/output streams, which don't
275    * exist.
276    */
277   io_stream_class->close_fn = g_test_tls_connection_close;
278
279   g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
280   g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
281   g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
282   g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
283   g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
284   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
285   g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
286   g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
287   g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
288   g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
289   g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
290   g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
291 }
292
293 static void
294 g_test_tls_connection_init (GTestTlsConnection *connection)
295 {
296 }
297
298 static gboolean
299 g_test_tls_connection_initable_init (GInitable       *initable,
300                                       GCancellable    *cancellable,
301                                       GError         **error)
302 {
303   g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
304                        "TLS Connection support is not available");
305   return FALSE;
306 }
307
308 static void
309 g_test_tls_connection_initable_iface_init (GInitableIface  *iface)
310 {
311   iface->init = g_test_tls_connection_initable_init;
312 }
313
314 const gchar *
315 g_test_tls_connection_get_private_key_pem (GTlsCertificate *cert)
316 {
317   return ((GTestTlsCertificate *)cert)->key_pem;
318 }