~ubuntu-branches/ubuntu/trusty/telepathy-idle/trusty

« back to all changes in this revision

Viewing changes to .pc/0001-Don-t-use-telepathy-glib-dbus.h-with-older-telepathy.patch/src/tls-certificate.c

  • Committer: Package Import Robot
  • Author(s): Simon McVittie
  • Date: 2013-05-01 15:52:26 UTC
  • mfrom: (1.3.6)
  • Revision ID: package-import@ubuntu.com-20130501155226-ttpmql3jetet34iu
Tags: 0.1.16-1
* New upstream release
  - adds support for interactive TLS certificate verification
    (Closes: #706270)
* Add a patch to avoid use of a telepathy-glib 0.20 header, to make
  this easy to backport to wheezy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * tls-certificate.c - Source for IdleTLSCertificate
 
3
 * Copyright (C) 2010 Collabora Ltd.
 
4
 * @author Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
 
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 Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include "tls-certificate.h"
 
23
 
 
24
#include <telepathy-glib/telepathy-glib.h>
 
25
#include <telepathy-glib/telepathy-glib-dbus.h>
 
26
 
 
27
#define IDLE_DEBUG_FLAG IDLE_DEBUG_TLS
 
28
#include "idle-debug.h"
 
29
 
 
30
static void
 
31
tls_certificate_iface_init (gpointer g_iface, gpointer iface_data);
 
32
 
 
33
G_DEFINE_TYPE_WITH_CODE (IdleTLSCertificate,
 
34
    idle_tls_certificate,
 
35
    G_TYPE_OBJECT,
 
36
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_AUTHENTICATION_TLS_CERTIFICATE,
 
37
        tls_certificate_iface_init);
 
38
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
 
39
        tp_dbus_properties_mixin_iface_init);)
 
40
 
 
41
struct _IdleTLSCertificatePrivate {
 
42
  gchar *object_path;
 
43
 
 
44
  gchar *cert_type;
 
45
  TpTLSCertificateState cert_state;
 
46
 
 
47
  GPtrArray *rejections;
 
48
  GPtrArray *cert_data;
 
49
 
 
50
  TpDBusDaemon *daemon;
 
51
 
 
52
  gboolean dispose_has_run;
 
53
};
 
54
 
 
55
enum {
 
56
  PROP_OBJECT_PATH = 1,
 
57
  PROP_STATE,
 
58
  PROP_REJECTIONS,
 
59
  PROP_CERTIFICATE_TYPE,
 
60
  PROP_CERTIFICATE_CHAIN_DATA,
 
61
 
 
62
  /* not exported */
 
63
  PROP_DBUS_DAEMON,
 
64
 
 
65
  NUM_PROPERTIES
 
66
};
 
67
 
 
68
static void
 
69
idle_tls_certificate_get_property (GObject *object,
 
70
    guint property_id,
 
71
    GValue *value,
 
72
    GParamSpec *pspec)
 
73
{
 
74
  IdleTLSCertificate *self = IDLE_TLS_CERTIFICATE (object);
 
75
 
 
76
  switch (property_id)
 
77
    {
 
78
    case PROP_OBJECT_PATH:
 
79
      g_value_set_string (value, self->priv->object_path);
 
80
      break;
 
81
    case PROP_STATE:
 
82
      g_value_set_uint (value, self->priv->cert_state);
 
83
      break;
 
84
    case PROP_REJECTIONS:
 
85
      g_value_set_boxed (value, self->priv->rejections);
 
86
      break;
 
87
    case PROP_CERTIFICATE_TYPE:
 
88
      g_value_set_string (value, self->priv->cert_type);
 
89
      break;
 
90
    case PROP_CERTIFICATE_CHAIN_DATA:
 
91
      g_value_set_boxed (value, self->priv->cert_data);
 
92
      break;
 
93
    default:
 
94
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
95
      break;
 
96
  }
 
97
}
 
98
 
 
99
static void
 
100
idle_tls_certificate_set_property (GObject *object,
 
101
    guint property_id,
 
102
    const GValue *value,
 
103
    GParamSpec *pspec)
 
104
{
 
105
  IdleTLSCertificate *self = IDLE_TLS_CERTIFICATE (object);
 
106
 
 
107
  switch (property_id)
 
108
    {
 
109
    case PROP_OBJECT_PATH:
 
110
      self->priv->object_path = g_value_dup_string (value);
 
111
      break;
 
112
    case PROP_CERTIFICATE_TYPE:
 
113
      self->priv->cert_type = g_value_dup_string (value);
 
114
      break;
 
115
    case PROP_CERTIFICATE_CHAIN_DATA:
 
116
      self->priv->cert_data = g_value_dup_boxed (value);
 
117
      break;
 
118
    case PROP_DBUS_DAEMON:
 
119
      self->priv->daemon = g_value_dup_object (value);
 
120
      break;
 
121
    default:
 
122
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, value);
 
123
      break;
 
124
    }
 
125
}
 
126
 
 
127
static void
 
128
idle_tls_certificate_finalize (GObject *object)
 
129
{
 
130
  IdleTLSCertificate *self = IDLE_TLS_CERTIFICATE (object);
 
131
 
 
132
  tp_clear_boxed (TP_ARRAY_TYPE_TLS_CERTIFICATE_REJECTION_LIST,
 
133
      &self->priv->rejections);
 
134
 
 
135
  g_free (self->priv->object_path);
 
136
  g_free (self->priv->cert_type);
 
137
  g_ptr_array_unref (self->priv->cert_data);
 
138
 
 
139
  G_OBJECT_CLASS (idle_tls_certificate_parent_class)->finalize (object);
 
140
}
 
141
 
 
142
static void
 
143
idle_tls_certificate_dispose (GObject *object)
 
144
{
 
145
  IdleTLSCertificate *self = IDLE_TLS_CERTIFICATE (object);
 
146
 
 
147
  if (self->priv->dispose_has_run)
 
148
    return;
 
149
 
 
150
  self->priv->dispose_has_run = TRUE;
 
151
 
 
152
  tp_clear_object (&self->priv->daemon);
 
153
 
 
154
  G_OBJECT_CLASS (idle_tls_certificate_parent_class)->dispose (object);
 
155
}
 
156
 
 
157
static void
 
158
idle_tls_certificate_constructed (GObject *object)
 
159
{
 
160
  IdleTLSCertificate *self = IDLE_TLS_CERTIFICATE (object);
 
161
  void (*chain_up) (GObject *) =
 
162
    G_OBJECT_CLASS (idle_tls_certificate_parent_class)->constructed;
 
163
 
 
164
  if (chain_up != NULL)
 
165
    chain_up (object);
 
166
 
 
167
  /* register the certificate on the bus */
 
168
  tp_dbus_daemon_register_object (self->priv->daemon,
 
169
      self->priv->object_path, self);
 
170
}
 
171
 
 
172
static void
 
173
idle_tls_certificate_init (IdleTLSCertificate *self)
 
174
{
 
175
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
 
176
      IDLE_TYPE_TLS_CERTIFICATE, IdleTLSCertificatePrivate);
 
177
  self->priv->rejections = g_ptr_array_new ();
 
178
}
 
179
 
 
180
static void
 
181
idle_tls_certificate_class_init (IdleTLSCertificateClass *klass)
 
182
{
 
183
  static TpDBusPropertiesMixinPropImpl object_props[] = {
 
184
    { "State", "state", NULL },
 
185
    { "Rejections", "rejections", NULL },
 
186
    { "CertificateType", "certificate-type", NULL },
 
187
    { "CertificateChainData", "certificate-chain-data", NULL },
 
188
    { NULL }
 
189
  };
 
190
  static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
 
191
    { TP_IFACE_AUTHENTICATION_TLS_CERTIFICATE,
 
192
      tp_dbus_properties_mixin_getter_gobject_properties,
 
193
      NULL,
 
194
      object_props,
 
195
    },
 
196
    { NULL }
 
197
  };
 
198
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
 
199
  GParamSpec *pspec;
 
200
 
 
201
  g_type_class_add_private (klass, sizeof (IdleTLSCertificatePrivate));
 
202
 
 
203
  oclass->finalize = idle_tls_certificate_finalize;
 
204
  oclass->dispose = idle_tls_certificate_dispose;
 
205
  oclass->set_property = idle_tls_certificate_set_property;
 
206
  oclass->get_property = idle_tls_certificate_get_property;
 
207
  oclass->constructed = idle_tls_certificate_constructed;
 
208
 
 
209
  pspec = g_param_spec_string ("object-path",
 
210
      "D-Bus object path",
 
211
      "The D-Bus object path used for this object on the bus.",
 
212
      NULL,
 
213
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
 
214
  g_object_class_install_property (oclass, PROP_OBJECT_PATH, pspec);
 
215
 
 
216
  pspec = g_param_spec_uint ("state",
 
217
      "State of this certificate",
 
218
      "The state of this TLS certificate.",
 
219
      0, NUM_TP_TLS_CERTIFICATE_STATES - 1,
 
220
      TP_TLS_CERTIFICATE_STATE_PENDING,
 
221
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
 
222
  g_object_class_install_property (oclass, PROP_STATE, pspec);
 
223
 
 
224
  pspec = g_param_spec_boxed ("rejections",
 
225
      "The reject reasons",
 
226
      "The reasons why this TLS certificate has been rejected",
 
227
      TP_ARRAY_TYPE_TLS_CERTIFICATE_REJECTION_LIST,
 
228
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
 
229
  g_object_class_install_property (oclass, PROP_REJECTIONS, pspec);
 
230
 
 
231
  pspec = g_param_spec_string ("certificate-type",
 
232
      "The certificate type",
 
233
      "The type of this certificate.",
 
234
      NULL,
 
235
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
 
236
  g_object_class_install_property (oclass, PROP_CERTIFICATE_TYPE, pspec);
 
237
 
 
238
  pspec = g_param_spec_boxed ("certificate-chain-data",
 
239
      "The certificate chain data",
 
240
      "The raw PEM-encoded trust chain of this certificate.",
 
241
      TP_ARRAY_TYPE_UCHAR_ARRAY_LIST,
 
242
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
 
243
  g_object_class_install_property (oclass, PROP_CERTIFICATE_CHAIN_DATA, pspec);
 
244
 
 
245
  pspec = g_param_spec_object ("dbus-daemon",
 
246
      "The DBus daemon connection",
 
247
      "The connection to the DBus daemon owning the CM",
 
248
      TP_TYPE_DBUS_DAEMON,
 
249
      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
 
250
  g_object_class_install_property (oclass, PROP_DBUS_DAEMON, pspec);
 
251
 
 
252
  klass->dbus_props_class.interfaces = prop_interfaces;
 
253
  tp_dbus_properties_mixin_class_init (oclass,
 
254
      G_STRUCT_OFFSET (IdleTLSCertificateClass, dbus_props_class));
 
255
}
 
256
 
 
257
static void
 
258
idle_tls_certificate_accept (TpSvcAuthenticationTLSCertificate *cert,
 
259
    DBusGMethodInvocation *context)
 
260
{
 
261
  IdleTLSCertificate *self = IDLE_TLS_CERTIFICATE (cert);
 
262
 
 
263
  IDLE_DEBUG ("Accept() called on the TLS certificate; current state %u",
 
264
      self->priv->cert_state);
 
265
 
 
266
  if (self->priv->cert_state != TP_TLS_CERTIFICATE_STATE_PENDING)
 
267
    {
 
268
      GError error =
 
269
        { TP_ERROR,
 
270
          TP_ERROR_INVALID_ARGUMENT,
 
271
          "Calling Accept() on a certificate with state != PENDING "
 
272
          "doesn't make sense."
 
273
        };
 
274
 
 
275
      dbus_g_method_return_error (context, &error);
 
276
      return;
 
277
    }
 
278
 
 
279
  self->priv->cert_state = TP_TLS_CERTIFICATE_STATE_ACCEPTED;
 
280
  tp_svc_authentication_tls_certificate_emit_accepted (self);
 
281
 
 
282
  tp_svc_authentication_tls_certificate_return_from_accept (context);
 
283
}
 
284
 
 
285
static void
 
286
idle_tls_certificate_reject (TpSvcAuthenticationTLSCertificate *cert,
 
287
    const GPtrArray *rejections,
 
288
    DBusGMethodInvocation *context)
 
289
{
 
290
  IdleTLSCertificate *self = IDLE_TLS_CERTIFICATE (cert);
 
291
 
 
292
  IDLE_DEBUG ("Reject() called on the TLS certificate with rejections %p, "
 
293
      "length %u; current state %u", rejections, rejections->len,
 
294
      self->priv->cert_state);
 
295
 
 
296
  if (rejections->len < 1)
 
297
    {
 
298
      GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
 
299
          "Calling Reject() with a zero-length rejection list." };
 
300
 
 
301
      dbus_g_method_return_error (context, &error);
 
302
      return;
 
303
    }
 
304
 
 
305
  if (self->priv->cert_state != TP_TLS_CERTIFICATE_STATE_PENDING)
 
306
    {
 
307
      GError error =
 
308
        { TP_ERROR,
 
309
          TP_ERROR_INVALID_ARGUMENT,
 
310
          "Calling Reject() on a certificate with state != PENDING "
 
311
          "doesn't make sense."
 
312
        };
 
313
 
 
314
      dbus_g_method_return_error (context, &error);
 
315
      return;
 
316
    }
 
317
 
 
318
  tp_clear_boxed (TP_ARRAY_TYPE_TLS_CERTIFICATE_REJECTION_LIST,
 
319
      &self->priv->rejections);
 
320
 
 
321
  self->priv->rejections =
 
322
    g_boxed_copy (TP_ARRAY_TYPE_TLS_CERTIFICATE_REJECTION_LIST,
 
323
        rejections);
 
324
  self->priv->cert_state = TP_TLS_CERTIFICATE_STATE_REJECTED;
 
325
 
 
326
  tp_svc_authentication_tls_certificate_emit_rejected (
 
327
      self, self->priv->rejections);
 
328
 
 
329
  tp_svc_authentication_tls_certificate_return_from_reject (context);
 
330
}
 
331
 
 
332
static void
 
333
tls_certificate_iface_init (gpointer g_iface,
 
334
    gpointer iface_data)
 
335
{
 
336
  TpSvcAuthenticationTLSCertificateClass *klass = g_iface;
 
337
 
 
338
#define IMPLEMENT(x) \
 
339
  tp_svc_authentication_tls_certificate_implement_##x ( \
 
340
      klass, idle_tls_certificate_##x)
 
341
  IMPLEMENT (accept);
 
342
  IMPLEMENT (reject);
 
343
#undef IMPLEMENT
 
344
}