gdummytlsbackend.c 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include "gdummytlsbackend.h"

#include <glib.h>

#include "gasyncresult.h"
#include "gcancellable.h"
#include "ginitable.h"
#include "gtlsbackend.h"
#include "gtlscertificate.h"
#include "gtlsclientconnection.h"
33 34
#include "gtlsdatabase.h"
#include "gtlsfiledatabase.h"
35 36 37 38 39 40 41 42 43 44
#include "gtlsserverconnection.h"
#include "gsimpleasyncresult.h"

#include "giomodule.h"
#include "giomodule-priv.h"

#include "glibintl.h"

static GType _g_dummy_tls_certificate_get_type (void);
static GType _g_dummy_tls_connection_get_type (void);
45
static GType _g_dummy_tls_database_get_type (void);
46 47

struct _GDummyTlsBackend {
48 49
  GObject       parent_instance;
  GTlsDatabase *database;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
};

static void g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface);

#define g_dummy_tls_backend_get_type _g_dummy_tls_backend_get_type
G_DEFINE_TYPE_WITH_CODE (GDummyTlsBackend, g_dummy_tls_backend, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_BACKEND,
						g_dummy_tls_backend_iface_init)
			 _g_io_modules_ensure_extension_points_registered ();
			 g_io_extension_point_implement (G_TLS_BACKEND_EXTENSION_POINT_NAME,
							 g_define_type_id,
							 "dummy",
							 -100))

static void
g_dummy_tls_backend_init (GDummyTlsBackend *backend)
{
}

static void
g_dummy_tls_backend_class_init (GDummyTlsBackendClass *backend_class)
{
}

74 75 76 77 78 79
static GTlsDatabase*
g_dummy_tls_backend_get_default_database (GTlsBackend *backend)
{
  return g_object_new (_g_dummy_tls_database_get_type (), NULL);
}

80 81 82 83 84 85
static void
g_dummy_tls_backend_iface_init (GTlsBackendInterface *iface)
{
  iface->get_certificate_type = _g_dummy_tls_certificate_get_type;
  iface->get_client_connection_type = _g_dummy_tls_connection_get_type;
  iface->get_server_connection_type = _g_dummy_tls_connection_get_type;
86 87
  iface->get_file_database_type = _g_dummy_tls_database_get_type;
  iface->get_default_database = g_dummy_tls_backend_get_default_database;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

/* Dummy certificate type */

typedef struct _GDummyTlsCertificate      GDummyTlsCertificate;
typedef struct _GDummyTlsCertificateClass GDummyTlsCertificateClass;

struct _GDummyTlsCertificate {
  GTlsCertificate parent_instance;
};

struct _GDummyTlsCertificateClass {
  GTlsCertificateClass parent_class;
};

enum
{
  PROP_CERTIFICATE_0,

107 108 109 110 111
  PROP_CERT_CERTIFICATE,
  PROP_CERT_CERTIFICATE_PEM,
  PROP_CERT_PRIVATE_KEY,
  PROP_CERT_PRIVATE_KEY_PEM,
  PROP_CERT_ISSUER
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
};

static void g_dummy_tls_certificate_initable_iface_init (GInitableIface *iface);

#define g_dummy_tls_certificate_get_type _g_dummy_tls_certificate_get_type
G_DEFINE_TYPE_WITH_CODE (GDummyTlsCertificate, g_dummy_tls_certificate, G_TYPE_TLS_CERTIFICATE,
			 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
						g_dummy_tls_certificate_initable_iface_init);)

static void
g_dummy_tls_certificate_get_property (GObject    *object,
				      guint       prop_id,
				      GValue     *value,
				      GParamSpec *pspec)
{
  /* We need to define this method to make GObject happy, but it will
   * never be possible to construct a working GDummyTlsCertificate, so
   * it doesn't have to do anything useful.
   */
}

static void
g_dummy_tls_certificate_set_property (GObject      *object,
				      guint         prop_id,
				      const GValue *value,
				      GParamSpec   *pspec)
{
  /* Just ignore all attempts to set properties. */
}

static void
g_dummy_tls_certificate_class_init (GDummyTlsCertificateClass *certificate_class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (certificate_class);

  gobject_class->get_property = g_dummy_tls_certificate_get_property;
  gobject_class->set_property = g_dummy_tls_certificate_set_property;

150 151 152 153 154
  g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE, "certificate");
  g_object_class_override_property (gobject_class, PROP_CERT_CERTIFICATE_PEM, "certificate-pem");
  g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY, "private-key");
  g_object_class_override_property (gobject_class, PROP_CERT_PRIVATE_KEY_PEM, "private-key-pem");
  g_object_class_override_property (gobject_class, PROP_CERT_ISSUER, "issuer");
155 156 157 158 159 160 161 162 163 164 165 166
}

static void
g_dummy_tls_certificate_init (GDummyTlsCertificate *certificate)
{
}

static gboolean
g_dummy_tls_certificate_initable_init (GInitable       *initable,
				       GCancellable    *cancellable,
				       GError         **error)
{
167
  g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
		       _("TLS support is not available"));
  return FALSE;
}

static void
g_dummy_tls_certificate_initable_iface_init (GInitableIface  *iface)
{
  iface->init = g_dummy_tls_certificate_initable_init;
}

/* Dummy connection type; since GTlsClientConnection and
 * GTlsServerConnection are just interfaces, we can implement them
 * both on a single object.
 */

typedef struct _GDummyTlsConnection      GDummyTlsConnection;
typedef struct _GDummyTlsConnectionClass GDummyTlsConnectionClass;

struct _GDummyTlsConnection {
  GTlsConnection parent_instance;
};

struct _GDummyTlsConnectionClass {
  GTlsConnectionClass parent_class;
};

enum
{
  PROP_CONNECTION_0,

198 199 200 201 202
  PROP_CONN_BASE_IO_STREAM,
  PROP_CONN_USE_SYSTEM_CERTDB,
  PROP_CONN_REQUIRE_CLOSE_NOTIFY,
  PROP_CONN_REHANDSHAKE_MODE,
  PROP_CONN_CERTIFICATE,
203
  PROP_CONN_DATABASE,
204 205 206 207 208 209 210
  PROP_CONN_PEER_CERTIFICATE,
  PROP_CONN_PEER_CERTIFICATE_ERRORS,
  PROP_CONN_VALIDATION_FLAGS,
  PROP_CONN_SERVER_IDENTITY,
  PROP_CONN_USE_SSL3,
  PROP_CONN_ACCEPTED_CAS,
  PROP_CONN_AUTHENTICATION_MODE
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
};

static void g_dummy_tls_connection_initable_iface_init (GInitableIface *iface);

#define g_dummy_tls_connection_get_type _g_dummy_tls_connection_get_type
G_DEFINE_TYPE_WITH_CODE (GDummyTlsConnection, g_dummy_tls_connection, G_TYPE_TLS_CONNECTION,
			 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_CLIENT_CONNECTION, NULL);
			 G_IMPLEMENT_INTERFACE (G_TYPE_TLS_SERVER_CONNECTION, NULL);
			 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
						g_dummy_tls_connection_initable_iface_init);)

static void
g_dummy_tls_connection_get_property (GObject    *object,
				     guint       prop_id,
				     GValue     *value,
				     GParamSpec *pspec)
{
}

static void
g_dummy_tls_connection_set_property (GObject      *object,
				     guint         prop_id,
				     const GValue *value,
				     GParamSpec   *pspec)
{
}

static gboolean
g_dummy_tls_connection_close (GIOStream     *stream,
			      GCancellable  *cancellable,
			      GError       **error)
{
  return TRUE;
}

static void
g_dummy_tls_connection_class_init (GDummyTlsConnectionClass *connection_class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (connection_class);
  GIOStreamClass *io_stream_class = G_IO_STREAM_CLASS (connection_class);

  gobject_class->get_property = g_dummy_tls_connection_get_property;
  gobject_class->set_property = g_dummy_tls_connection_set_property;

  /* Need to override this because when initable_init fails it will
   * dispose the connection, which will close it, which would
   * otherwise try to close its input/output streams, which don't
   * exist.
   */
  io_stream_class->close_fn = g_dummy_tls_connection_close;

262 263 264 265 266
  g_object_class_override_property (gobject_class, PROP_CONN_BASE_IO_STREAM, "base-io-stream");
  g_object_class_override_property (gobject_class, PROP_CONN_USE_SYSTEM_CERTDB, "use-system-certdb");
  g_object_class_override_property (gobject_class, PROP_CONN_REQUIRE_CLOSE_NOTIFY, "require-close-notify");
  g_object_class_override_property (gobject_class, PROP_CONN_REHANDSHAKE_MODE, "rehandshake-mode");
  g_object_class_override_property (gobject_class, PROP_CONN_CERTIFICATE, "certificate");
267
  g_object_class_override_property (gobject_class, PROP_CONN_DATABASE, "database");
268 269 270 271 272 273 274
  g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE, "peer-certificate");
  g_object_class_override_property (gobject_class, PROP_CONN_PEER_CERTIFICATE_ERRORS, "peer-certificate-errors");
  g_object_class_override_property (gobject_class, PROP_CONN_VALIDATION_FLAGS, "validation-flags");
  g_object_class_override_property (gobject_class, PROP_CONN_SERVER_IDENTITY, "server-identity");
  g_object_class_override_property (gobject_class, PROP_CONN_USE_SSL3, "use-ssl3");
  g_object_class_override_property (gobject_class, PROP_CONN_ACCEPTED_CAS, "accepted-cas");
  g_object_class_override_property (gobject_class, PROP_CONN_AUTHENTICATION_MODE, "authentication-mode");
275 276 277 278 279 280 281 282 283 284 285 286
}

static void
g_dummy_tls_connection_init (GDummyTlsConnection *connection)
{
}

static gboolean
g_dummy_tls_connection_initable_init (GInitable       *initable,
				      GCancellable    *cancellable,
				      GError         **error)
{
287
  g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
288 289 290 291 292 293 294 295 296 297
		       _("TLS support is not available"));
  return FALSE;
}

static void
g_dummy_tls_connection_initable_iface_init (GInitableIface  *iface)
{
  iface->init = g_dummy_tls_connection_initable_init;
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
/* Dummy database type.
 */

typedef struct _GDummyTlsDatabase      GDummyTlsDatabase;
typedef struct _GDummyTlsDatabaseClass GDummyTlsDatabaseClass;

struct _GDummyTlsDatabase {
  GTlsDatabase parent_instance;
};

struct _GDummyTlsDatabaseClass {
  GTlsDatabaseClass parent_class;
};

enum
{
  PROP_DATABASE_0,

  PROP_ANCHORS,
};

static void g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface *iface);
static void g_dummy_tls_database_initable_iface_init (GInitableIface *iface);

#define g_dummy_tls_database_get_type _g_dummy_tls_database_get_type
G_DEFINE_TYPE_WITH_CODE (GDummyTlsDatabase, g_dummy_tls_database, G_TYPE_TLS_DATABASE,
                         G_IMPLEMENT_INTERFACE (G_TYPE_TLS_FILE_DATABASE,
                                                g_dummy_tls_database_file_database_iface_init);
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                g_dummy_tls_database_initable_iface_init);)


static void
g_dummy_tls_database_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  /* We need to define this method to make GObject happy, but it will
   * never be possible to construct a working GDummyTlsDatabase, so
   * it doesn't have to do anything useful.
   */
}

static void
g_dummy_tls_database_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  /* Just ignore all attempts to set properties. */
}

static void
g_dummy_tls_database_class_init (GDummyTlsDatabaseClass *database_class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (database_class);

  gobject_class->get_property = g_dummy_tls_database_get_property;
  gobject_class->set_property = g_dummy_tls_database_set_property;

  g_object_class_override_property (gobject_class, PROP_ANCHORS, "anchors");
}

static void
g_dummy_tls_database_init (GDummyTlsDatabase *database)
{
}

static void
g_dummy_tls_database_file_database_iface_init (GTlsFileDatabaseInterface  *iface)
{
}

static gboolean
g_dummy_tls_database_initable_init (GInitable       *initable,
                                    GCancellable    *cancellable,
                                    GError         **error)
{
  g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_UNAVAILABLE,
                       _("TLS support is not available"));
  return FALSE;
}

static void
g_dummy_tls_database_initable_iface_init (GInitableIface  *iface)
{
  iface->init = g_dummy_tls_database_initable_init;
}