Coverage Report

Created: 2025-12-31 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pidgin/libpurple/certificate.c
Line
Count
Source
1
/**
2
 * @file certificate.c Public-Key Certificate API
3
 * @ingroup core
4
 */
5
6
/*
7
 *
8
 * purple
9
 *
10
 * Purple is the legal property of its developers, whose names are too numerous
11
 * to list here.  Please refer to the COPYRIGHT file distributed with this
12
 * source distribution.
13
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
27
 */
28
29
#include "internal.h"
30
#include "certificate.h"
31
#include "dbus-maybe.h"
32
#include "debug.h"
33
#include "glibcompat.h"
34
#include "request.h"
35
#include "signals.h"
36
#include "util.h"
37
38
/** List holding pointers to all registered certificate schemes */
39
static GList *cert_schemes = NULL;
40
/** List of registered Verifiers */
41
static GList *cert_verifiers = NULL;
42
/** List of registered Pools */
43
static GList *cert_pools = NULL;
44
45
46
static const gchar *
47
invalidity_reason_to_string(PurpleCertificateInvalidityFlags flag)
48
0
{
49
0
  switch (flag) {
50
0
    case PURPLE_CERTIFICATE_SELF_SIGNED:
51
0
      return _("The certificate is self-signed and cannot be "
52
0
               "automatically checked.");
53
0
      break;
54
0
    case PURPLE_CERTIFICATE_CA_UNKNOWN:
55
0
      return _("The certificate is not trusted because no certificate "
56
0
               "that can verify it is currently trusted.");
57
0
      break;
58
0
    case PURPLE_CERTIFICATE_NOT_ACTIVATED:
59
0
      return _("The certificate is not valid yet.  Check that your "
60
0
               "computer's date and time are accurate.");
61
0
      break;
62
0
    case PURPLE_CERTIFICATE_EXPIRED:
63
0
      return _("The certificate has expired and should not be "
64
0
               "considered valid.  Check that your computer's date "
65
0
               "and time are accurate.");
66
0
      break;
67
0
    case PURPLE_CERTIFICATE_NAME_MISMATCH:
68
      /* Translators: "domain" refers to a DNS domain (e.g. talk.google.com) */
69
0
      return _("The certificate presented is not issued to this domain.");
70
0
      break;
71
0
    case PURPLE_CERTIFICATE_NO_CA_POOL:
72
0
      return _("You have no database of root certificates, so "
73
0
               "this certificate cannot be validated.");
74
0
      break;
75
0
    case PURPLE_CERTIFICATE_INVALID_CHAIN:
76
0
      return _("The certificate chain presented is invalid.");
77
0
      break;
78
0
    case PURPLE_CERTIFICATE_REVOKED:
79
0
      return _("The certificate has been revoked.");
80
0
      break;
81
0
    case PURPLE_CERTIFICATE_UNKNOWN_ERROR:
82
0
    default:
83
0
      return _("An unknown certificate error occurred.");
84
0
      break;
85
0
  }
86
0
}
87
88
static void
89
get_ascii_fingerprints (PurpleCertificate *crt, gchar **sha1, gchar **sha256)
90
0
{
91
0
  GByteArray *sha_bin;
92
93
0
  if (sha1 != NULL) {
94
0
    sha_bin = purple_certificate_get_fingerprint_sha1(crt);
95
96
0
    *sha1 = purple_base16_encode_chunked(sha_bin->data, sha_bin->len);
97
98
0
    g_byte_array_free(sha_bin, TRUE);
99
0
  }
100
101
0
  if (sha256 != NULL) {
102
0
    sha_bin = purple_certificate_get_fingerprint_sha256(crt, FALSE);
103
104
0
    *sha256 = (sha_bin == NULL) ? g_strdup("(null)") :
105
0
      purple_base16_encode_chunked(sha_bin->data, sha_bin->len);
106
107
0
    g_byte_array_free(sha_bin, TRUE);
108
0
  }
109
0
}
110
111
void
112
purple_certificate_verify (PurpleCertificateVerifier *verifier,
113
         const gchar *subject_name, GList *cert_chain,
114
         PurpleCertificateVerifiedCallback cb,
115
         gpointer cb_data)
116
0
{
117
0
  PurpleCertificateVerificationRequest *vrq;
118
0
  PurpleCertificateScheme *scheme;
119
120
0
  g_return_if_fail(subject_name != NULL);
121
  /* If you don't have a cert to check, why are you requesting that it
122
     be verified? */
123
0
  g_return_if_fail(cert_chain != NULL);
124
0
  g_return_if_fail(cb != NULL);
125
126
  /* Look up the CertificateScheme */
127
0
  scheme = purple_certificate_find_scheme(verifier->scheme_name);
128
0
  g_return_if_fail(scheme);
129
130
  /* Check that at least the first cert in the chain matches the
131
     Verifier scheme */
132
0
  g_return_if_fail(scheme ==
133
0
       ((PurpleCertificate *) (cert_chain->data))->scheme);
134
135
  /* Construct and fill in the request fields */
136
0
  vrq = g_new0(PurpleCertificateVerificationRequest, 1);
137
0
  vrq->verifier = verifier;
138
0
  vrq->scheme = scheme;
139
0
  vrq->subject_name = g_strdup(subject_name);
140
0
  vrq->cert_chain = purple_certificate_copy_list(cert_chain);
141
0
  vrq->cb = cb;
142
0
  vrq->cb_data = cb_data;
143
144
  /* Initiate verification */
145
0
  (verifier->start_verification)(vrq);
146
0
}
147
148
void
149
purple_certificate_verify_complete(PurpleCertificateVerificationRequest *vrq,
150
           PurpleCertificateVerificationStatus st)
151
0
{
152
0
  PurpleCertificateVerifier *vr;
153
154
0
  g_return_if_fail(vrq);
155
156
0
  if (st == PURPLE_CERTIFICATE_VALID) {
157
0
    purple_debug_info("certificate",
158
0
          "Successfully verified certificate for %s\n",
159
0
          vrq->subject_name);
160
0
  } else {
161
0
    purple_debug_error("certificate",
162
0
          "Failed to verify certificate for %s\n",
163
0
          vrq->subject_name);
164
0
  }
165
166
  /* Pass the results on to the request's callback */
167
0
  (vrq->cb)(st, vrq->cb_data);
168
169
  /* And now to eliminate the request */
170
  /* Fetch the Verifier responsible... */
171
0
  vr = vrq->verifier;
172
  /* ...and order it to KILL */
173
0
  (vr->destroy_request)(vrq);
174
175
  /* Now the internals have been cleaned up, so clean up the libpurple-
176
     created elements */
177
0
  g_free(vrq->subject_name);
178
0
  purple_certificate_destroy_list(vrq->cert_chain);
179
180
  /*  A structure born
181
   *          to much ado
182
   *                   and with so much within.
183
   * It reaches now
184
   *             its quiet end. */
185
0
  g_free(vrq);
186
0
}
187
188
189
PurpleCertificate *
190
purple_certificate_copy(PurpleCertificate *crt)
191
0
{
192
0
  g_return_val_if_fail(crt, NULL);
193
0
  g_return_val_if_fail(crt->scheme, NULL);
194
0
  g_return_val_if_fail(crt->scheme->copy_certificate, NULL);
195
196
0
  return (crt->scheme->copy_certificate)(crt);
197
0
}
198
199
GList *
200
purple_certificate_copy_list(GList *crt_list)
201
0
{
202
0
  GList *new_l, *l;
203
204
  /* First, make a shallow copy of the list */
205
0
  new_l = g_list_copy(crt_list);
206
207
  /* Now go through and actually duplicate each certificate */
208
0
  for (l = new_l; l; l = l->next) {
209
0
    l->data = purple_certificate_copy(l->data);
210
0
  }
211
212
0
  return new_l;
213
0
}
214
215
void
216
purple_certificate_destroy (PurpleCertificate *crt)
217
0
{
218
0
  PurpleCertificateScheme *scheme;
219
220
0
  if (NULL == crt) return;
221
222
0
  scheme = crt->scheme;
223
224
0
  (scheme->destroy_certificate)(crt);
225
0
}
226
227
void
228
purple_certificate_destroy_list (GList * crt_list)
229
0
{
230
0
  PurpleCertificate *crt;
231
0
  GList *l;
232
233
0
  for (l=crt_list; l; l = l->next) {
234
0
    crt = (PurpleCertificate *) l->data;
235
0
    purple_certificate_destroy(crt);
236
0
  }
237
238
0
  g_list_free(crt_list);
239
0
}
240
241
gboolean
242
purple_certificate_signed_by(PurpleCertificate *crt, PurpleCertificate *issuer)
243
0
{
244
0
  PurpleCertificateScheme *scheme;
245
246
0
  g_return_val_if_fail(crt, FALSE);
247
0
  g_return_val_if_fail(issuer, FALSE);
248
249
0
  scheme = crt->scheme;
250
0
  g_return_val_if_fail(scheme, FALSE);
251
  /* We can't compare two certs of unrelated schemes, obviously */
252
0
  g_return_val_if_fail(issuer->scheme == scheme, FALSE);
253
254
0
  return (scheme->signed_by)(crt, issuer);
255
0
}
256
257
gboolean
258
purple_certificate_check_signature_chain_with_failing(GList *chain,
259
                                                      PurpleCertificate **failing)
260
0
{
261
0
  GList *cur;
262
0
  PurpleCertificate *crt, *issuer;
263
0
  gchar *uid;
264
0
  time_t now, activation, expiration;
265
0
  gboolean ret;
266
267
0
  g_return_val_if_fail(chain, FALSE);
268
269
0
  if (failing)
270
0
    *failing = NULL;
271
272
0
  uid = purple_certificate_get_unique_id((PurpleCertificate *) chain->data);
273
0
  purple_debug_info("certificate",
274
0
        "Checking signature chain for uid=%s\n",
275
0
        uid);
276
0
  g_free(uid);
277
278
  /* If this is a single-certificate chain, say that it is valid */
279
0
  if (chain->next == NULL) {
280
0
    purple_debug_info("certificate",
281
0
          "...Singleton. We'll say it's valid.\n");
282
0
    return TRUE;
283
0
  }
284
285
0
  now = time(NULL);
286
287
  /* Load crt with the first certificate */
288
0
  crt = (PurpleCertificate *)(chain->data);
289
  /* And start with the second certificate in the chain */
290
0
  for ( cur = chain->next; cur; cur = cur->next ) {
291
292
0
    issuer = (PurpleCertificate *)(cur->data);
293
294
0
    uid = purple_certificate_get_unique_id(issuer);
295
296
0
    ret = purple_certificate_get_times(issuer, &activation, &expiration);
297
0
    if (!ret || now < activation || now > expiration) {
298
0
      if (!ret)
299
0
        purple_debug_error("certificate",
300
0
            "...Failed to get validity times for certificate %s\n"
301
0
            "Chain is INVALID\n", uid);
302
0
      else if (now > expiration)
303
0
        purple_debug_error("certificate",
304
0
            "...Issuer %s expired at %s\nChain is INVALID\n",
305
0
            uid, ctime(&expiration));
306
0
      else
307
0
        purple_debug_error("certificate",
308
0
            "...Not-yet-activated issuer %s will be valid at %s\n"
309
0
            "Chain is INVALID\n", uid, ctime(&activation));
310
311
0
      if (failing)
312
0
        *failing = crt;
313
314
0
      g_free(uid);
315
0
      return FALSE;
316
0
    }
317
318
    /* Check the signature for this link */
319
0
    if (! purple_certificate_signed_by(crt, issuer) ) {
320
0
      purple_debug_error("certificate",
321
0
            "...Bad or missing signature by %s\nChain is INVALID\n",
322
0
            uid);
323
0
      g_free(uid);
324
325
0
      if (failing)
326
0
        *failing = crt;
327
328
0
      return FALSE;
329
0
    }
330
331
0
    purple_debug_info("certificate",
332
0
          "...Good signature by %s\n",
333
0
          uid);
334
0
    g_free(uid);
335
336
    /* The issuer is now the next crt whose signature is to be
337
       checked */
338
0
    crt = issuer;
339
0
  }
340
341
  /* If control reaches this point, the chain is valid */
342
0
  purple_debug_info("certificate", "Chain is VALID\n");
343
0
  return TRUE;
344
0
}
345
346
gboolean
347
purple_certificate_check_signature_chain(GList *chain)
348
0
{
349
0
  return purple_certificate_check_signature_chain_with_failing(chain, NULL);
350
0
}
351
352
PurpleCertificate *
353
purple_certificate_import(PurpleCertificateScheme *scheme, const gchar *filename)
354
0
{
355
0
  g_return_val_if_fail(scheme, NULL);
356
0
  g_return_val_if_fail(scheme->import_certificate, NULL);
357
0
  g_return_val_if_fail(filename, NULL);
358
359
0
  return (scheme->import_certificate)(filename);
360
0
}
361
362
GSList *
363
purple_certificates_import(PurpleCertificateScheme *scheme, const gchar *filename)
364
0
{
365
0
  g_return_val_if_fail(scheme, NULL);
366
0
  g_return_val_if_fail(scheme->import_certificates, NULL);
367
0
  g_return_val_if_fail(filename, NULL);
368
369
0
  return (scheme->import_certificates)(filename);
370
0
}
371
372
gboolean
373
purple_certificate_export(const gchar *filename, PurpleCertificate *crt)
374
0
{
375
0
  PurpleCertificateScheme *scheme;
376
377
0
  g_return_val_if_fail(filename, FALSE);
378
0
  g_return_val_if_fail(crt, FALSE);
379
0
  g_return_val_if_fail(crt->scheme, FALSE);
380
381
0
  scheme = crt->scheme;
382
0
  g_return_val_if_fail(scheme->export_certificate, FALSE);
383
384
0
  return (scheme->export_certificate)(filename, crt);
385
0
}
386
387
static gboolean
388
byte_arrays_equal(const GByteArray *array1, const GByteArray *array2)
389
0
{
390
0
  g_return_val_if_fail(array1 != NULL, FALSE);
391
0
  g_return_val_if_fail(array2 != NULL, FALSE);
392
393
0
  return (array1->len == array2->len) &&
394
0
    (0 == memcmp(array1->data, array2->data, array1->len));
395
0
}
396
397
GByteArray *
398
purple_certificate_get_fingerprint_sha1(PurpleCertificate *crt)
399
0
{
400
0
  PurpleCertificateScheme *scheme;
401
0
  GByteArray *fpr;
402
403
0
  g_return_val_if_fail(crt, NULL);
404
0
  g_return_val_if_fail(crt->scheme, NULL);
405
406
0
  scheme = crt->scheme;
407
408
0
  g_return_val_if_fail(scheme->get_fingerprint_sha1, NULL);
409
410
0
  fpr = (scheme->get_fingerprint_sha1)(crt);
411
412
0
  return fpr;
413
0
}
414
415
GByteArray *
416
purple_certificate_get_fingerprint_sha256(PurpleCertificate *crt, gboolean sha1_fallback)
417
0
{
418
0
  PurpleCertificateScheme *scheme;
419
0
  GByteArray *fpr = NULL;
420
421
0
  g_return_val_if_fail(crt, NULL);
422
0
  g_return_val_if_fail(crt->scheme, NULL);
423
424
0
  scheme = crt->scheme;
425
426
0
  if (!PURPLE_CERTIFICATE_SCHEME_HAS_FUNC(scheme, get_fingerprint_sha256)) {
427
    /* outdated ssl module? fallback to sha1 and print a warning */
428
0
    if (sha1_fallback) {
429
0
      fpr = purple_certificate_get_fingerprint_sha1(crt);
430
0
    }
431
0
    g_return_val_if_reached(fpr);
432
0
  }
433
434
0
  fpr = (scheme->get_fingerprint_sha256)(crt);
435
436
0
  return fpr;
437
0
}
438
439
gchar *
440
purple_certificate_get_unique_id(PurpleCertificate *crt)
441
0
{
442
0
  g_return_val_if_fail(crt, NULL);
443
0
  g_return_val_if_fail(crt->scheme, NULL);
444
0
  g_return_val_if_fail(crt->scheme->get_unique_id, NULL);
445
446
0
  return (crt->scheme->get_unique_id)(crt);
447
0
}
448
449
gchar *
450
purple_certificate_get_issuer_unique_id(PurpleCertificate *crt)
451
0
{
452
0
  g_return_val_if_fail(crt, NULL);
453
0
  g_return_val_if_fail(crt->scheme, NULL);
454
0
  g_return_val_if_fail(crt->scheme->get_issuer_unique_id, NULL);
455
456
0
  return (crt->scheme->get_issuer_unique_id)(crt);
457
0
}
458
459
gchar *
460
purple_certificate_get_subject_name(PurpleCertificate *crt)
461
0
{
462
0
  PurpleCertificateScheme *scheme;
463
0
  gchar *subject_name;
464
465
0
  g_return_val_if_fail(crt, NULL);
466
0
  g_return_val_if_fail(crt->scheme, NULL);
467
468
0
  scheme = crt->scheme;
469
470
0
  g_return_val_if_fail(scheme->get_subject_name, NULL);
471
472
0
  subject_name = (scheme->get_subject_name)(crt);
473
474
0
  return subject_name;
475
0
}
476
477
gboolean
478
purple_certificate_check_subject_name(PurpleCertificate *crt, const gchar *name)
479
0
{
480
0
  PurpleCertificateScheme *scheme;
481
482
0
  g_return_val_if_fail(crt, FALSE);
483
0
  g_return_val_if_fail(crt->scheme, FALSE);
484
0
  g_return_val_if_fail(name, FALSE);
485
486
0
  scheme = crt->scheme;
487
488
0
  g_return_val_if_fail(scheme->check_subject_name, FALSE);
489
490
0
  return (scheme->check_subject_name)(crt, name);
491
0
}
492
493
gboolean
494
purple_certificate_get_times(PurpleCertificate *crt, time_t *activation, time_t *expiration)
495
0
{
496
0
  PurpleCertificateScheme *scheme;
497
498
0
  g_return_val_if_fail(crt, FALSE);
499
500
0
  scheme = crt->scheme;
501
502
0
  g_return_val_if_fail(scheme, FALSE);
503
504
  /* If both provided references are NULL, what are you doing calling
505
     this? */
506
0
  g_return_val_if_fail( (activation != NULL) || (expiration != NULL), FALSE);
507
508
  /* Throw the request on down to the certscheme */
509
0
  return (scheme->get_times)(crt, activation, expiration);
510
0
}
511
512
gboolean
513
purple_certificate_compare_pubkeys(PurpleCertificate *crt1, PurpleCertificate *crt2)
514
0
{
515
0
  PurpleCertificateScheme *scheme;
516
517
0
  g_return_val_if_fail(crt1 && crt2, FALSE);
518
0
  g_return_val_if_fail(crt1->scheme && crt2->scheme, FALSE);
519
0
  g_return_val_if_fail(crt1->scheme == crt2->scheme, FALSE);
520
521
0
  scheme = crt1->scheme;
522
523
0
  if (!(PURPLE_CERTIFICATE_SCHEME_HAS_FUNC(scheme, compare_pubkeys))) {
524
0
    return FALSE;
525
0
  }
526
527
0
  return (scheme->compare_pubkeys)(crt1, crt2);
528
0
}
529
530
gchar *
531
purple_certificate_pool_mkpath(PurpleCertificatePool *pool, const gchar *id)
532
0
{
533
0
  gchar *path;
534
0
  gchar *esc_scheme_name, *esc_name, *esc_id;
535
536
0
  g_return_val_if_fail(pool, NULL);
537
0
  g_return_val_if_fail(pool->scheme_name, NULL);
538
0
  g_return_val_if_fail(pool->name, NULL);
539
540
  /* Escape all the elements for filesystem-friendliness */
541
0
  esc_scheme_name = g_strdup(purple_escape_filename(pool->scheme_name));
542
0
  esc_name = g_strdup(purple_escape_filename(pool->name));
543
0
  esc_id = id ? g_strdup(purple_escape_filename(id)) : NULL;
544
545
0
  path = g_build_filename(purple_user_dir(),
546
0
        "certificates", /* TODO: constantize this? */
547
0
        esc_scheme_name,
548
0
        esc_name,
549
0
        esc_id,
550
0
        NULL);
551
552
0
  g_free(esc_scheme_name);
553
0
  g_free(esc_name);
554
0
  g_free(esc_id);
555
0
  return path;
556
0
}
557
558
gboolean
559
purple_certificate_pool_usable(PurpleCertificatePool *pool)
560
0
{
561
0
  g_return_val_if_fail(pool, FALSE);
562
0
  g_return_val_if_fail(pool->scheme_name, FALSE);
563
564
  /* Check that the pool's scheme is loaded */
565
0
  if (purple_certificate_find_scheme(pool->scheme_name) == NULL) {
566
0
    return FALSE;
567
0
  }
568
569
0
  return TRUE;
570
0
}
571
572
PurpleCertificateScheme *
573
purple_certificate_pool_get_scheme(PurpleCertificatePool *pool)
574
0
{
575
0
  g_return_val_if_fail(pool, NULL);
576
0
  g_return_val_if_fail(pool->scheme_name, NULL);
577
578
0
  return purple_certificate_find_scheme(pool->scheme_name);
579
0
}
580
581
gboolean
582
purple_certificate_pool_contains(PurpleCertificatePool *pool, const gchar *id)
583
0
{
584
0
  g_return_val_if_fail(pool, FALSE);
585
0
  g_return_val_if_fail(id, FALSE);
586
0
  g_return_val_if_fail(pool->cert_in_pool, FALSE);
587
588
0
  return (pool->cert_in_pool)(id);
589
0
}
590
591
PurpleCertificate *
592
purple_certificate_pool_retrieve(PurpleCertificatePool *pool, const gchar *id)
593
0
{
594
0
  g_return_val_if_fail(pool, NULL);
595
0
  g_return_val_if_fail(id, NULL);
596
0
  g_return_val_if_fail(pool->get_cert, NULL);
597
598
0
  return (pool->get_cert)(id);
599
0
}
600
601
gboolean
602
purple_certificate_pool_store(PurpleCertificatePool *pool, const gchar *id, PurpleCertificate *crt)
603
0
{
604
0
  gboolean ret = FALSE;
605
606
0
  g_return_val_if_fail(pool, FALSE);
607
0
  g_return_val_if_fail(id, FALSE);
608
0
  g_return_val_if_fail(pool->put_cert, FALSE);
609
610
  /* Whether crt->scheme matches find_scheme(pool->scheme_name) is not
611
     relevant... I think... */
612
0
  g_return_val_if_fail(
613
0
    g_ascii_strcasecmp(pool->scheme_name, crt->scheme->name) == 0,
614
0
    FALSE);
615
616
0
  ret = (pool->put_cert)(id, crt);
617
618
  /* Signal that the certificate was stored if success*/
619
0
  if (ret) {
620
0
    purple_signal_emit(pool, "certificate-stored",
621
0
           pool, id);
622
0
  }
623
624
0
  return ret;
625
0
}
626
627
gboolean
628
purple_certificate_pool_delete(PurpleCertificatePool *pool, const gchar *id)
629
0
{
630
0
  gboolean ret = FALSE;
631
632
0
  g_return_val_if_fail(pool, FALSE);
633
0
  g_return_val_if_fail(id, FALSE);
634
0
  g_return_val_if_fail(pool->delete_cert, FALSE);
635
636
0
  ret = (pool->delete_cert)(id);
637
638
  /* Signal that the certificate was deleted if success */
639
0
  if (ret) {
640
0
    purple_signal_emit(pool, "certificate-deleted",
641
0
           pool, id);
642
0
  }
643
644
0
  return ret;
645
0
}
646
647
GList *
648
purple_certificate_pool_get_idlist(PurpleCertificatePool *pool)
649
0
{
650
0
  g_return_val_if_fail(pool, NULL);
651
0
  g_return_val_if_fail(pool->get_idlist, NULL);
652
653
0
  return (pool->get_idlist)();
654
0
}
655
656
void
657
purple_certificate_pool_destroy_idlist(GList *idlist)
658
0
{
659
0
  GList *l;
660
661
  /* Iterate through and free them strings */
662
0
  for ( l = idlist; l; l = l->next ) {
663
0
    g_free(l->data);
664
0
  }
665
666
0
  g_list_free(idlist);
667
0
}
668
669
670
/****************************************************************************/
671
/* Builtin Verifiers, Pools, etc.                                           */
672
/****************************************************************************/
673
674
static void
675
x509_singleuse_verify_cb (PurpleCertificateVerificationRequest *vrq, gint id)
676
0
{
677
0
  g_return_if_fail(vrq);
678
679
0
  purple_debug_info("certificate/x509_singleuse",
680
0
        "VRQ on cert from %s gave %d\n",
681
0
        vrq->subject_name, id);
682
683
  /* Signal what happened back to the caller */
684
0
  if (1 == id) {
685
    /* Accepted! */
686
0
    purple_certificate_verify_complete(vrq,
687
0
               PURPLE_CERTIFICATE_VALID);
688
0
  } else {
689
    /* Not accepted */
690
0
    purple_certificate_verify_complete(vrq,
691
0
               PURPLE_CERTIFICATE_INVALID);
692
693
0
  }
694
0
}
695
696
static void
697
x509_singleuse_start_verify (PurpleCertificateVerificationRequest *vrq)
698
0
{
699
0
  gchar *sha1_asc, *sha256_asc;
700
0
  gchar *cn;
701
0
  const gchar *cn_match;
702
0
  gchar *primary, *secondary, *secondary_extra;
703
0
  PurpleCertificate *crt = (PurpleCertificate *) vrq->cert_chain->data;
704
705
0
  get_ascii_fingerprints(crt, &sha1_asc, &sha256_asc);
706
707
  /* Get the cert Common Name */
708
0
  cn = purple_certificate_get_subject_name(crt);
709
710
  /* Determine whether the name matches */
711
0
  if (purple_certificate_check_subject_name(crt, vrq->subject_name)) {
712
0
    cn_match = "";
713
0
  } else {
714
0
    cn_match = _("(DOES NOT MATCH)");
715
0
  }
716
717
  /* Make messages */
718
0
  primary = g_strdup_printf(_("%s has presented the following certificate for just-this-once use:"), vrq->subject_name);
719
0
  secondary = g_strdup_printf(_("Common name: %s %s\nFingerprint (SHA1): %s"), cn, cn_match, sha1_asc);
720
721
  /* TODO: make this part of the translatable string above */
722
0
  secondary_extra = g_strdup_printf("%s\nSHA256: %s", secondary, sha256_asc);
723
724
  /* Make a semi-pretty display */
725
0
  purple_request_accept_cancel(
726
0
    vrq->cb_data, /* TODO: Find what the handle ought to be */
727
0
    _("Single-use Certificate Verification"),
728
0
    primary,
729
0
    secondary_extra,
730
0
    0,            /* Accept by default */
731
0
    NULL,         /* No account */
732
0
    NULL,         /* No other user */
733
0
    NULL,         /* No associated conversation */
734
0
    vrq,
735
0
    x509_singleuse_verify_cb,
736
0
    x509_singleuse_verify_cb );
737
738
  /* Cleanup */
739
0
  g_free(cn);
740
0
  g_free(primary);
741
0
  g_free(secondary);
742
0
  g_free(secondary_extra);
743
0
  g_free(sha1_asc);
744
0
  g_free(sha256_asc);
745
0
}
746
747
static void
748
x509_singleuse_destroy_request (PurpleCertificateVerificationRequest *vrq)
749
0
{
750
  /* I don't do anything! */
751
0
}
752
753
static PurpleCertificateVerifier x509_singleuse = {
754
  "x509",                         /* Scheme name */
755
  "singleuse",                    /* Verifier name */
756
  x509_singleuse_start_verify,    /* start_verification function */
757
  x509_singleuse_destroy_request, /* Request cleanup operation */
758
759
  NULL,
760
  NULL,
761
  NULL,
762
  NULL
763
};
764
765
766
767
/***** X.509 Certificate Authority pool, keyed by Distinguished Name *****/
768
/* This is implemented in what may be the most inefficient and bugprone way
769
   possible; however, future optimizations should not be difficult. */
770
771
static PurpleCertificatePool x509_ca;
772
773
/** Holds a key-value pair for quickish certificate lookup */
774
typedef struct {
775
  gchar *dn;
776
  PurpleCertificate *crt;
777
} x509_ca_element;
778
779
static void
780
x509_ca_element_free(x509_ca_element *el)
781
0
{
782
0
  if (NULL == el) return;
783
784
0
  g_free(el->dn);
785
0
  purple_certificate_destroy(el->crt);
786
0
  g_free(el);
787
0
}
788
789
/** System directory to probe for CA certificates */
790
/* This is set in the lazy_init function */
791
static GList *x509_ca_paths = NULL;
792
793
/** A list of loaded CAs, populated from the above path whenever the lazy_init
794
    happens. Contains pointers to x509_ca_elements */
795
static GList *x509_ca_certs = NULL;
796
797
/** Used for lazy initialization purposes. */
798
static gboolean x509_ca_initialized = FALSE;
799
800
/** Adds a certificate to the in-memory cache, and mark it as trusted */
801
static gboolean
802
x509_ca_quiet_put_cert(PurpleCertificate *crt)
803
0
{
804
0
  gboolean ret;
805
0
  x509_ca_element *el;
806
807
  /* lazy_init calls this function, so calling lazy_init here is a
808
     Bad Thing */
809
810
0
  g_return_val_if_fail(crt, FALSE);
811
0
  g_return_val_if_fail(crt->scheme, FALSE);
812
  /* Make sure that this is some kind of X.509 certificate */
813
  /* TODO: Perhaps just check crt->scheme->name instead? */
814
0
  g_return_val_if_fail(crt->scheme == purple_certificate_find_scheme(x509_ca.scheme_name), FALSE);
815
816
0
  ret = TRUE;
817
818
0
  if (crt->scheme->register_trusted_tls_cert) {
819
0
    ret = (crt->scheme->register_trusted_tls_cert)(crt, TRUE);
820
0
  }
821
822
0
  if (ret) {
823
0
    el = g_new0(x509_ca_element, 1);
824
0
    el->dn = purple_certificate_get_unique_id(crt);
825
0
    el->crt = purple_certificate_copy(crt);
826
0
    x509_ca_certs = g_list_prepend(x509_ca_certs, el);
827
0
  }
828
829
0
  return ret;
830
0
}
831
832
/* Since the libpurple CertificatePools get registered before plugins are
833
   loaded, an X.509 Scheme is generally not available when x509_ca_init is
834
   called, but x509_ca requires X.509 operations in order to properly load.
835
836
   To solve this, I present the lazy_init function. It attempts to finish
837
   initialization of the Pool, but it usually fails when it is called from
838
   x509_ca_init. However, this is OK; initialization is then simply deferred
839
   until someone tries to use functions from the pool. */
840
static gboolean
841
x509_ca_lazy_init(void)
842
0
{
843
0
  PurpleCertificateScheme *x509;
844
0
  GDir *certdir;
845
0
  const gchar *entry;
846
0
  GPatternSpec *pempat, *crtpat;
847
0
  GList *iter = NULL;
848
0
  GSList *crts = NULL;
849
850
0
  if (x509_ca_initialized) return TRUE;
851
852
  /* Check that X.509 is registered */
853
0
  x509 = purple_certificate_find_scheme(x509_ca.scheme_name);
854
0
  if ( !x509 ) {
855
0
    purple_debug_warning("certificate/x509/ca",
856
0
          "Lazy init failed because an X.509 Scheme "
857
0
          "is not yet registered. Maybe it will be "
858
0
          "better later.\n");
859
0
    return FALSE;
860
0
  }
861
862
  /* Use a glob to only read .pem files */
863
0
  pempat = g_pattern_spec_new("*.pem");
864
0
  crtpat = g_pattern_spec_new("*.crt");
865
866
  /* Populate the certificates pool from the search path(s) */
867
0
  for (iter = x509_ca_paths; iter; iter = iter->next) {
868
0
    certdir = g_dir_open(iter->data, 0, NULL);
869
0
    if (!certdir) {
870
0
      purple_debug_error("certificate/x509/ca", "Couldn't open location '%s'\n", (const char *)iter->data);
871
0
      continue;
872
0
    }
873
874
0
    while ( (entry = g_dir_read_name(certdir)) ) {
875
0
      gchar *fullpath;
876
0
      PurpleCertificate *crt;
877
878
0
      if (!g_pattern_match_string(pempat, entry) && !g_pattern_match_string(crtpat, entry)) {
879
0
        continue;
880
0
      }
881
882
0
      fullpath = g_build_filename(iter->data, entry, NULL);
883
884
      /* TODO: Respond to a failure in the following? */
885
0
      crts = purple_certificates_import(x509, fullpath);
886
887
0
      while (crts && crts->data) {
888
0
        crt = crts->data;
889
0
        if (x509_ca_quiet_put_cert(crt)) {
890
0
          gchar *name;
891
0
          name = purple_certificate_get_subject_name(crt);
892
0
          purple_debug_info("certificate/x509/ca",
893
0
                "Loaded %s from %s\n",
894
0
                name ? name : "(unknown)", fullpath);
895
0
          g_free(name);
896
0
        } else {
897
0
          purple_debug_error("certificate/x509/ca",
898
0
                "Failed to load certificate from %s\n",
899
0
                fullpath);
900
0
        }
901
0
        purple_certificate_destroy(crt);
902
0
        crts = g_slist_delete_link(crts, crts);
903
0
      }
904
905
0
      g_free(fullpath);
906
0
    }
907
0
    g_dir_close(certdir);
908
0
  }
909
910
0
  g_pattern_spec_free(pempat);
911
0
  g_pattern_spec_free(crtpat);
912
913
0
  purple_debug_info("certificate/x509/ca",
914
0
        "Lazy init completed.\n");
915
0
  x509_ca_initialized = TRUE;
916
0
  return TRUE;
917
0
}
918
919
static gboolean
920
x509_ca_init(void)
921
0
{
922
  /* Attempt to point at the appropriate system path */
923
0
  if (NULL == x509_ca_paths) {
924
#ifdef _WIN32
925
    x509_ca_paths = g_list_append(NULL, g_build_filename(DATADIR,
926
               "ca-certs", NULL));
927
#else
928
# ifdef SSL_CERTIFICATES_DIR
929
    x509_ca_paths = g_list_append(NULL, g_strdup(SSL_CERTIFICATES_DIR));
930
# endif
931
0
    x509_ca_paths = g_list_append(x509_ca_paths,
932
0
      g_build_filename(DATADIR, "purple", "ca-certs", NULL));
933
0
#endif
934
0
  }
935
936
  /* Attempt to initialize now, but if it doesn't work, that's OK;
937
     it will get done later */
938
0
  if ( ! x509_ca_lazy_init()) {
939
0
    purple_debug_info("certificate/x509/ca",
940
0
          "Init failed, probably because a "
941
0
          "dependency is not yet registered. "
942
0
          "It has been deferred to later.\n");
943
0
  }
944
945
0
  return TRUE;
946
0
}
947
948
static void
949
x509_ca_uninit(void)
950
0
{
951
0
  GList *l;
952
953
0
  for (l = x509_ca_certs; l; l = l->next) {
954
0
    x509_ca_element *el = l->data;
955
0
    x509_ca_element_free(el);
956
0
  }
957
0
  g_list_free(x509_ca_certs);
958
0
  x509_ca_certs = NULL;
959
0
  x509_ca_initialized = FALSE;
960
  /** TODO: the cert store in the SSL implementation wouldn't be cleared by this */
961
0
  g_list_free_full(x509_ca_paths, (GDestroyNotify)g_free);
962
0
  x509_ca_paths = NULL;
963
0
}
964
965
/** Look up a ca_element by dn */
966
static x509_ca_element *
967
x509_ca_locate_cert(GList *lst, const gchar *dn)
968
0
{
969
0
  GList *cur;
970
971
0
  for (cur = lst; cur; cur = cur->next) {
972
0
    x509_ca_element *el = cur->data;
973
0
    if (purple_strequal(dn, el->dn)) {
974
0
      return el;
975
0
    }
976
0
  }
977
0
  return NULL;
978
0
}
979
980
static GSList *
981
x509_ca_locate_certs(GList *lst, const gchar *dn)
982
0
{
983
0
  GList *cur;
984
0
  GSList *crts = NULL;
985
986
0
  for (cur = lst; cur; cur = cur->next) {
987
0
    x509_ca_element *el = cur->data;
988
0
    if (purple_strequal(dn, el->dn)) {
989
0
      crts = g_slist_prepend(crts, el);
990
0
    }
991
0
  }
992
0
  return crts;
993
0
}
994
995
996
static gboolean
997
x509_ca_cert_in_pool(const gchar *id)
998
0
{
999
0
  g_return_val_if_fail(x509_ca_lazy_init(), FALSE);
1000
0
  g_return_val_if_fail(id, FALSE);
1001
1002
0
  if (x509_ca_locate_cert(x509_ca_certs, id) != NULL) {
1003
0
    return TRUE;
1004
0
  } else {
1005
0
    return FALSE;
1006
0
  }
1007
1008
0
  return FALSE;
1009
0
}
1010
1011
static PurpleCertificate *
1012
x509_ca_get_cert(const gchar *id)
1013
0
{
1014
0
  PurpleCertificate *crt = NULL;
1015
0
  x509_ca_element *el;
1016
1017
0
  g_return_val_if_fail(x509_ca_lazy_init(), NULL);
1018
0
  g_return_val_if_fail(id, NULL);
1019
1020
  /* Search the memory-cached pool */
1021
0
  el = x509_ca_locate_cert(x509_ca_certs, id);
1022
1023
0
  if (el != NULL) {
1024
    /* Make a copy of the memcached one for the function caller
1025
       to play with */
1026
0
    crt = purple_certificate_copy(el->crt);
1027
0
  } else {
1028
0
    crt = NULL;
1029
0
  }
1030
1031
0
  return crt;
1032
0
}
1033
1034
static GSList *
1035
x509_ca_get_certs(const gchar *id)
1036
0
{
1037
0
  GSList *crts = NULL, *els = NULL;
1038
1039
0
  g_return_val_if_fail(x509_ca_lazy_init(), NULL);
1040
0
  g_return_val_if_fail(id, NULL);
1041
1042
  /* Search the memory-cached pool */
1043
0
  els = x509_ca_locate_certs(x509_ca_certs, id);
1044
1045
0
  if (els != NULL) {
1046
0
    GSList *cur;
1047
    /* Make a copy of the memcached ones for the function caller
1048
       to play with */
1049
0
    for (cur = els; cur; cur = cur->next) {
1050
0
      x509_ca_element *el = cur->data;
1051
0
      crts = g_slist_prepend(crts, purple_certificate_copy(el->crt));
1052
0
    }
1053
0
    g_slist_free(els);
1054
0
  }
1055
1056
0
  return crts;
1057
0
}
1058
1059
static gboolean
1060
x509_ca_put_cert(const gchar *id, PurpleCertificate *crt)
1061
0
{
1062
0
  gboolean ret = FALSE;
1063
1064
0
  g_return_val_if_fail(x509_ca_lazy_init(), FALSE);
1065
1066
  /* TODO: This is a quick way of doing this. At some point the change
1067
     ought to be flushed to disk somehow. */
1068
0
  ret = x509_ca_quiet_put_cert(crt);
1069
1070
0
  return ret;
1071
0
}
1072
1073
static gboolean
1074
x509_ca_delete_cert(const gchar *id)
1075
0
{
1076
0
  x509_ca_element *el;
1077
1078
0
  g_return_val_if_fail(x509_ca_lazy_init(), FALSE);
1079
0
  g_return_val_if_fail(id, FALSE);
1080
1081
  /* Is the id even in the pool? */
1082
0
  el = x509_ca_locate_cert(x509_ca_certs, id);
1083
0
  if ( el == NULL ) {
1084
0
    purple_debug_warning("certificate/x509/ca",
1085
0
             "Id %s wasn't in the pool\n",
1086
0
             id);
1087
0
    return FALSE;
1088
0
  }
1089
1090
  /* Unlink it from the memory cache and destroy it */
1091
0
  x509_ca_certs = g_list_remove(x509_ca_certs, el);
1092
0
  x509_ca_element_free(el);
1093
1094
0
  return TRUE;
1095
0
}
1096
1097
static GList *
1098
x509_ca_get_idlist(void)
1099
0
{
1100
0
  GList *l, *idlist;
1101
1102
0
  g_return_val_if_fail(x509_ca_lazy_init(), NULL);
1103
1104
0
  idlist = NULL;
1105
0
  for (l = x509_ca_certs; l; l = l->next) {
1106
0
    x509_ca_element *el = l->data;
1107
0
    idlist = g_list_prepend(idlist, g_strdup(el->dn));
1108
0
  }
1109
1110
0
  return idlist;
1111
0
}
1112
1113
1114
static PurpleCertificatePool x509_ca = {
1115
  "x509",                       /* Scheme name */
1116
  "ca",                         /* Pool name */
1117
  N_("Certificate Authorities"),/* User-friendly name */
1118
  NULL,                         /* Internal data */
1119
  x509_ca_init,                 /* init */
1120
  x509_ca_uninit,               /* uninit */
1121
  x509_ca_cert_in_pool,         /* Certificate exists? */
1122
  x509_ca_get_cert,             /* Cert retriever */
1123
  x509_ca_put_cert,             /* Cert writer */
1124
  x509_ca_delete_cert,          /* Cert remover */
1125
  x509_ca_get_idlist,           /* idlist retriever */
1126
1127
  NULL,
1128
  NULL,
1129
  NULL,
1130
  NULL
1131
1132
};
1133
1134
1135
1136
/***** Cache of certificates given by TLS/SSL peers *****/
1137
static PurpleCertificatePool x509_tls_peers;
1138
1139
static gboolean
1140
x509_tls_peers_init(void)
1141
0
{
1142
0
  gchar *poolpath;
1143
0
  int ret;
1144
1145
  /* Set up key cache here if it isn't already done */
1146
0
  poolpath = purple_certificate_pool_mkpath(&x509_tls_peers, NULL);
1147
0
  ret = purple_build_dir(poolpath, 0700); /* Make it this user only */
1148
1149
0
  if (ret != 0)
1150
0
    purple_debug_info("certificate/tls_peers",
1151
0
        "Could not create %s.  Certificates will not be cached.\n",
1152
0
        poolpath);
1153
1154
0
  g_free(poolpath);
1155
1156
0
  return TRUE;
1157
0
}
1158
1159
static gboolean
1160
x509_tls_peers_cert_in_pool(const gchar *id)
1161
0
{
1162
0
  gchar *keypath;
1163
0
  gboolean ret = FALSE;
1164
1165
0
  g_return_val_if_fail(id, FALSE);
1166
1167
0
  keypath = purple_certificate_pool_mkpath(&x509_tls_peers, id);
1168
1169
0
  ret = g_file_test(keypath, G_FILE_TEST_IS_REGULAR);
1170
1171
0
  g_free(keypath);
1172
0
  return ret;
1173
0
}
1174
1175
static PurpleCertificate *
1176
x509_tls_peers_get_cert(const gchar *id)
1177
0
{
1178
0
  PurpleCertificateScheme *x509;
1179
0
  PurpleCertificate *crt;
1180
0
  gchar *keypath;
1181
1182
0
  g_return_val_if_fail(id, NULL);
1183
1184
  /* Is it in the pool? */
1185
0
  if ( !x509_tls_peers_cert_in_pool(id) ) {
1186
0
    return NULL;
1187
0
  }
1188
1189
  /* Look up the X.509 scheme */
1190
0
  x509 = purple_certificate_find_scheme("x509");
1191
0
  g_return_val_if_fail(x509, NULL);
1192
1193
  /* Okay, now find and load that key */
1194
0
  keypath = purple_certificate_pool_mkpath(&x509_tls_peers, id);
1195
0
  crt = purple_certificate_import(x509, keypath);
1196
1197
0
  g_free(keypath);
1198
1199
0
  return crt;
1200
0
}
1201
1202
static gboolean
1203
x509_tls_peers_put_cert(const gchar *id, PurpleCertificate *crt)
1204
0
{
1205
0
  gboolean ret = FALSE;
1206
0
  gchar *keypath;
1207
1208
0
  g_return_val_if_fail(crt, FALSE);
1209
0
  g_return_val_if_fail(crt->scheme, FALSE);
1210
  /* Make sure that this is some kind of X.509 certificate */
1211
  /* TODO: Perhaps just check crt->scheme->name instead? */
1212
0
  g_return_val_if_fail(crt->scheme == purple_certificate_find_scheme(x509_tls_peers.scheme_name), FALSE);
1213
1214
  /* Work out the filename and export */
1215
0
  keypath = purple_certificate_pool_mkpath(&x509_tls_peers, id);
1216
0
  ret = purple_certificate_export(keypath, crt);
1217
1218
0
  if (crt->scheme->register_trusted_tls_cert) {
1219
0
    ret = (crt->scheme->register_trusted_tls_cert)(crt, FALSE);
1220
0
  }
1221
1222
0
  g_free(keypath);
1223
0
  return ret;
1224
0
}
1225
1226
static gboolean
1227
x509_tls_peers_delete_cert(const gchar *id)
1228
0
{
1229
0
  gboolean ret = FALSE;
1230
0
  gchar *keypath;
1231
1232
0
  g_return_val_if_fail(id, FALSE);
1233
1234
  /* Is the id even in the pool? */
1235
0
  if (!x509_tls_peers_cert_in_pool(id)) {
1236
0
    purple_debug_warning("certificate/tls_peers",
1237
0
             "Id %s wasn't in the pool\n",
1238
0
             id);
1239
0
    return FALSE;
1240
0
  }
1241
1242
  /* OK, so work out the keypath and delete the thing */
1243
0
  keypath = purple_certificate_pool_mkpath(&x509_tls_peers, id);
1244
0
  if ( unlink(keypath) != 0 ) {
1245
0
    purple_debug_error("certificate/tls_peers",
1246
0
           "Unlink of %s failed!\n",
1247
0
           keypath);
1248
0
    ret = FALSE;
1249
0
  } else {
1250
0
    ret = TRUE;
1251
0
  }
1252
1253
0
  g_free(keypath);
1254
0
  return ret;
1255
0
}
1256
1257
static GList *
1258
x509_tls_peers_get_idlist(void)
1259
0
{
1260
0
  GList *idlist = NULL;
1261
0
  GDir *dir;
1262
0
  const gchar *entry;
1263
0
  gchar *poolpath;
1264
1265
  /* Get a handle on the pool directory */
1266
0
  poolpath = purple_certificate_pool_mkpath(&x509_tls_peers, NULL);
1267
0
  dir = g_dir_open(poolpath,
1268
0
       0,     /* No flags */
1269
0
       NULL); /* Not interested in what the error is */
1270
0
  g_free(poolpath);
1271
1272
0
  g_return_val_if_fail(dir, NULL);
1273
1274
  /* Traverse the directory listing and create an idlist */
1275
0
  while ( (entry = g_dir_read_name(dir)) != NULL ) {
1276
    /* Unescape the filename */
1277
0
    const char *unescaped = purple_unescape_filename(entry);
1278
1279
    /* Copy the entry name into our list (GLib owns the original
1280
       string) */
1281
0
    idlist = g_list_prepend(idlist, g_strdup(unescaped));
1282
0
  }
1283
1284
  /* Release the directory */
1285
0
  g_dir_close(dir);
1286
1287
0
  return idlist;
1288
0
}
1289
1290
static PurpleCertificatePool x509_tls_peers = {
1291
  "x509",                       /* Scheme name */
1292
  "tls_peers",                  /* Pool name */
1293
  N_("SSL Peers Cache"),        /* User-friendly name */
1294
  NULL,                         /* Internal data */
1295
  x509_tls_peers_init,          /* init */
1296
  NULL,                         /* uninit not required */
1297
  x509_tls_peers_cert_in_pool,  /* Certificate exists? */
1298
  x509_tls_peers_get_cert,      /* Cert retriever */
1299
  x509_tls_peers_put_cert,      /* Cert writer */
1300
  x509_tls_peers_delete_cert,   /* Cert remover */
1301
  x509_tls_peers_get_idlist,    /* idlist retriever */
1302
1303
  NULL,
1304
  NULL,
1305
  NULL,
1306
  NULL
1307
};
1308
1309
1310
/***** A Verifier that uses the tls_peers cache and the CA pool to validate certificates *****/
1311
static PurpleCertificateVerifier x509_tls_cached;
1312
1313
1314
/* The following is several hacks piled together and needs to be fixed.
1315
 * It exists because show_cert (see its comments) needs the original reason
1316
 * given to user_auth in order to rebuild the dialog.
1317
 */
1318
/* TODO: This will cause a ua_ctx to become memleaked if the request(s) get
1319
   closed by handle or otherwise abnormally. */
1320
typedef struct {
1321
  PurpleCertificateVerificationRequest *vrq;
1322
  gchar *reason;
1323
} x509_tls_cached_ua_ctx;
1324
1325
static x509_tls_cached_ua_ctx *
1326
x509_tls_cached_ua_ctx_new(PurpleCertificateVerificationRequest *vrq,
1327
         const gchar *reason)
1328
0
{
1329
0
  x509_tls_cached_ua_ctx *c;
1330
1331
0
  c = g_new0(x509_tls_cached_ua_ctx, 1);
1332
0
  c->vrq = vrq;
1333
0
  c->reason = g_strdup(reason);
1334
1335
0
  return c;
1336
0
}
1337
1338
1339
static void
1340
x509_tls_cached_ua_ctx_free(x509_tls_cached_ua_ctx *c)
1341
0
{
1342
0
  g_return_if_fail(c);
1343
0
  g_free(c->reason);
1344
0
  g_free(c);
1345
0
}
1346
1347
static void
1348
x509_tls_cached_user_auth(PurpleCertificateVerificationRequest *vrq,
1349
        const gchar *reason);
1350
1351
static void
1352
x509_tls_cached_show_cert(x509_tls_cached_ua_ctx *c, gint id)
1353
0
{
1354
0
  PurpleCertificate *disp_crt = c->vrq->cert_chain->data;
1355
1356
  /* Since clicking a button closes the request, show it again */
1357
0
  x509_tls_cached_user_auth(c->vrq, c->reason);
1358
1359
  /* Show the certificate AFTER re-opening the dialog so that this
1360
     appears above the other */
1361
0
  purple_certificate_display_x509(disp_crt);
1362
1363
0
  x509_tls_cached_ua_ctx_free(c);
1364
0
}
1365
1366
static void
1367
x509_tls_cached_user_auth_cb (x509_tls_cached_ua_ctx *c, gint id)
1368
0
{
1369
0
  PurpleCertificateVerificationRequest *vrq;
1370
0
  PurpleCertificatePool *tls_peers;
1371
1372
0
  g_return_if_fail(c);
1373
0
  g_return_if_fail(c->vrq);
1374
1375
0
  vrq = c->vrq;
1376
1377
0
  x509_tls_cached_ua_ctx_free(c);
1378
1379
0
  tls_peers = purple_certificate_find_pool("x509","tls_peers");
1380
1381
0
  if (2 == id) {
1382
0
    gchar *cache_id = vrq->subject_name;
1383
0
    purple_debug_info("certificate/x509/tls_cached",
1384
0
          "User ACCEPTED cert\nCaching first in chain for future use as %s...\n",
1385
0
          cache_id);
1386
1387
0
    purple_certificate_pool_store(tls_peers, cache_id,
1388
0
                vrq->cert_chain->data);
1389
1390
0
    purple_certificate_verify_complete(vrq,
1391
0
               PURPLE_CERTIFICATE_VALID);
1392
0
  } else {
1393
0
    purple_debug_warning("certificate/x509/tls_cached",
1394
0
          "User REJECTED cert\n");
1395
0
    purple_certificate_verify_complete(vrq,
1396
0
               PURPLE_CERTIFICATE_INVALID);
1397
0
  }
1398
0
}
1399
1400
static void
1401
x509_tls_cached_user_auth_accept_cb(x509_tls_cached_ua_ctx *c, gint ignore)
1402
0
{
1403
0
  x509_tls_cached_user_auth_cb(c, 2);
1404
0
}
1405
1406
static void
1407
x509_tls_cached_user_auth_reject_cb(x509_tls_cached_ua_ctx *c, gint ignore)
1408
0
{
1409
0
  x509_tls_cached_user_auth_cb(c, 1);
1410
0
}
1411
1412
/** Validates a certificate by asking the user
1413
 * @param reason    String to explain why the user needs to accept/refuse the
1414
 *                  certificate.
1415
 * @todo Needs a handle argument
1416
 */
1417
static void
1418
x509_tls_cached_user_auth(PurpleCertificateVerificationRequest *vrq,
1419
        const gchar *reason)
1420
0
{
1421
0
  gchar *primary;
1422
1423
  /* Make messages */
1424
0
  primary = g_strdup_printf(_("Accept certificate for %s?"),
1425
0
          vrq->subject_name);
1426
1427
  /* Make a semi-pretty display */
1428
0
  purple_request_action(
1429
0
    vrq->cb_data, /* TODO: Find what the handle ought to be */
1430
0
    _("SSL Certificate Verification"),
1431
0
    primary,
1432
0
    reason,
1433
0
    0,            /* Accept by default */
1434
0
    NULL,         /* No account */
1435
0
    NULL,         /* No other user */
1436
0
    NULL,         /* No associated conversation */
1437
0
    x509_tls_cached_ua_ctx_new(vrq, reason),
1438
0
    3,            /* Number of actions */
1439
0
    _("Accept"), x509_tls_cached_user_auth_accept_cb,
1440
0
    _("Reject"),  x509_tls_cached_user_auth_reject_cb,
1441
0
    _("_View Certificate..."), x509_tls_cached_show_cert);
1442
1443
  /* Cleanup */
1444
0
  g_free(primary);
1445
0
}
1446
1447
static void
1448
x509_tls_cached_unknown_peer(PurpleCertificateVerificationRequest *vrq,
1449
                             PurpleCertificateInvalidityFlags flags);
1450
1451
static void
1452
x509_tls_cached_complete(PurpleCertificateVerificationRequest *vrq,
1453
                         PurpleCertificateInvalidityFlags flags)
1454
0
{
1455
0
  PurpleCertificatePool *tls_peers;
1456
0
  PurpleCertificate *peer_crt = vrq->cert_chain->data;
1457
1458
0
  if (flags & PURPLE_CERTIFICATE_FATALS_MASK) {
1459
    /* TODO: Also print any other warnings? */
1460
0
    const gchar *error;
1461
0
    gchar *tmp, *secondary;
1462
1463
0
    if (flags & PURPLE_CERTIFICATE_INVALID_CHAIN)
1464
0
      error = invalidity_reason_to_string(PURPLE_CERTIFICATE_INVALID_CHAIN);
1465
0
    else if (flags & PURPLE_CERTIFICATE_REVOKED)
1466
0
      error = invalidity_reason_to_string(PURPLE_CERTIFICATE_REVOKED);
1467
0
    else
1468
0
      error = invalidity_reason_to_string(PURPLE_CERTIFICATE_UNKNOWN_ERROR);
1469
1470
0
    tmp = g_strdup_printf(_("The certificate for %s could not be validated."),
1471
0
          vrq->subject_name);
1472
0
    secondary = g_strconcat(tmp, " ", error, NULL);
1473
0
    g_free(tmp);
1474
1475
0
    purple_notify_error(NULL, /* TODO: Probably wrong. */
1476
0
          _("SSL Certificate Error"),
1477
0
          _("Unable to validate certificate"),
1478
0
          secondary);
1479
0
    g_free(secondary);
1480
1481
0
    purple_certificate_verify_complete(vrq, PURPLE_CERTIFICATE_INVALID);
1482
0
    return;
1483
0
  } else if (flags & PURPLE_CERTIFICATE_NON_FATALS_MASK) {
1484
    /* Non-fatal error. Prompt the user. */
1485
0
    gchar *tmp;
1486
0
    GString *errors;
1487
0
    guint32 i = 1;
1488
1489
0
    tmp = g_strdup_printf(_("The certificate for %s could not be validated."),
1490
0
          vrq->subject_name);
1491
0
    errors = g_string_new(tmp);
1492
0
    g_free(tmp);
1493
1494
0
    errors = g_string_append_c(errors, '\n');
1495
1496
    /* Special case a name mismatch because we want to display the two names... */
1497
0
    if (flags & PURPLE_CERTIFICATE_NAME_MISMATCH) {
1498
0
      gchar *sn = purple_certificate_get_subject_name(peer_crt);
1499
1500
0
      if (sn) {
1501
0
        g_string_append_printf(errors, _("The certificate claims to be "
1502
0
              "from \"%s\" instead. This could mean that you are "
1503
0
              "not connecting to the service you believe you are."),
1504
0
              sn);
1505
0
        g_free(sn);
1506
1507
0
        flags &= ~PURPLE_CERTIFICATE_NAME_MISMATCH;
1508
0
      }
1509
0
    }
1510
1511
0
    while (i != PURPLE_CERTIFICATE_LAST) {
1512
0
      if (flags & i) {
1513
0
        errors = g_string_append_c(errors, '\n');
1514
0
        g_string_append(errors, invalidity_reason_to_string(i));
1515
0
      }
1516
1517
0
      i <<= 1;
1518
0
    }
1519
1520
0
    x509_tls_cached_user_auth(vrq, errors->str);
1521
0
    g_string_free(errors, TRUE);
1522
0
    return;
1523
0
  }
1524
1525
  /* If we reach this point, the certificate is good. */
1526
1527
  /* Look up the local cache and store it there for future use */
1528
0
  tls_peers = purple_certificate_find_pool(x509_tls_cached.scheme_name,
1529
0
             "tls_peers");
1530
0
  if (tls_peers) {
1531
0
    if (!purple_certificate_pool_store(tls_peers,vrq->subject_name,
1532
0
                                       peer_crt)) {
1533
0
      purple_debug_error("certificate/x509/tls_cached",
1534
0
                         "FAILED to cache peer certificate\n");
1535
0
    }
1536
0
  } else {
1537
0
    purple_debug_error("certificate/x509/tls_cached",
1538
0
                       "Unable to locate tls_peers certificate cache.\n");
1539
0
  }
1540
1541
0
  purple_certificate_verify_complete(vrq, PURPLE_CERTIFICATE_VALID);
1542
0
}
1543
1544
static void
1545
x509_tls_cached_cert_in_cache(PurpleCertificateVerificationRequest *vrq,
1546
                              PurpleCertificateInvalidityFlags flags)
1547
0
{
1548
  /* TODO: Looking this up by name over and over is expensive.
1549
     Fix, please! */
1550
0
  PurpleCertificatePool *tls_peers =
1551
0
    purple_certificate_find_pool(x509_tls_cached.scheme_name,
1552
0
               "tls_peers");
1553
1554
  /* The peer's certificate should be the first in the list */
1555
0
  PurpleCertificate *peer_crt =
1556
0
    (PurpleCertificate *) vrq->cert_chain->data;
1557
1558
0
  PurpleCertificate *cached_crt;
1559
0
  GByteArray *peer_fpr, *cached_fpr;
1560
1561
  /* Load up the cached certificate */
1562
0
  cached_crt = purple_certificate_pool_retrieve(
1563
0
    tls_peers, vrq->subject_name);
1564
0
  if ( !cached_crt ) {
1565
0
    purple_debug_warning("certificate/x509/tls_cached",
1566
0
           "Lookup failed on cached certificate!\n"
1567
0
           "Falling back to full verification.\n");
1568
    /* vrq now becomes the problem of unknown_peer */
1569
0
    x509_tls_cached_unknown_peer(vrq, flags);
1570
0
    return;
1571
0
  }
1572
1573
  /* Now get SHA256 sums for both and compare them */
1574
  /* TODO: This is not an elegant way to compare certs */
1575
0
  peer_fpr = purple_certificate_get_fingerprint_sha256(peer_crt, TRUE);
1576
0
  cached_fpr = purple_certificate_get_fingerprint_sha256(cached_crt, TRUE);
1577
0
  if (!memcmp(peer_fpr->data, cached_fpr->data, peer_fpr->len)) {
1578
0
    purple_debug_info("certificate/x509/tls_cached",
1579
0
          "Peer cert matched cached\n");
1580
0
    x509_tls_cached_complete(vrq, flags);
1581
0
  } else {
1582
0
    purple_debug_error("certificate/x509/tls_cached",
1583
0
          "Peer cert did NOT match cached\n");
1584
    /* vrq now becomes the problem of the user */
1585
0
    x509_tls_cached_unknown_peer(vrq, flags);
1586
0
  }
1587
1588
0
  purple_certificate_destroy(cached_crt);
1589
0
  g_byte_array_free(peer_fpr, TRUE);
1590
0
  g_byte_array_free(cached_fpr, TRUE);
1591
0
}
1592
1593
/*
1594
 * This is called from two points in x509_tls_cached_unknown_peer below
1595
 * once we've verified the signature chain is valid. Now we need to verify
1596
 * the subject name of the certificate.
1597
 */
1598
static void
1599
x509_tls_cached_check_subject_name(PurpleCertificateVerificationRequest *vrq,
1600
                                   PurpleCertificateInvalidityFlags flags)
1601
0
{
1602
0
  PurpleCertificate *peer_crt;
1603
0
  GList *chain = vrq->cert_chain;
1604
1605
0
  peer_crt = (PurpleCertificate *) chain->data;
1606
1607
  /* Last, check that the hostname matches */
1608
0
  if ( ! purple_certificate_check_subject_name(peer_crt,
1609
0
                 vrq->subject_name) ) {
1610
0
    gchar *sn = purple_certificate_get_subject_name(peer_crt);
1611
1612
0
    flags |= PURPLE_CERTIFICATE_NAME_MISMATCH;
1613
0
    purple_debug_error("certificate/x509/tls_cached",
1614
0
          "Name mismatch: Certificate given for %s "
1615
0
          "has a name of %s\n",
1616
0
          vrq->subject_name, sn);
1617
0
    g_free(sn);
1618
0
  }
1619
1620
0
  x509_tls_cached_complete(vrq, flags);
1621
0
}
1622
1623
/* For when we've never communicated with this party before */
1624
/* TODO: Need ways to specify possibly multiple problems with a cert, or at
1625
   least  reprioritize them.
1626
 */
1627
static void
1628
x509_tls_cached_unknown_peer(PurpleCertificateVerificationRequest *vrq,
1629
                             PurpleCertificateInvalidityFlags flags)
1630
0
{
1631
0
  PurpleCertificatePool *ca;
1632
0
  PurpleCertificate *peer_crt;
1633
0
  PurpleCertificate *ca_crt, *end_crt;
1634
0
  PurpleCertificate *failing_crt;
1635
0
  GList *chain = vrq->cert_chain;
1636
0
  GSList *ca_crts, *cur;
1637
0
  GByteArray *last_fpr, *ca_fpr;
1638
0
  gboolean valid = FALSE;
1639
0
  gchar *ca_id, *ca2_id;
1640
1641
0
  peer_crt = (PurpleCertificate *) chain->data;
1642
1643
0
  if (peer_crt->scheme->verify_cert) {
1644
    /** Make sure we've loaded the CA certs (which causes NSS to trust them) */
1645
0
    g_return_if_fail(x509_ca_lazy_init());
1646
0
    peer_crt->scheme->verify_cert(vrq, &flags);
1647
0
    x509_tls_cached_complete(vrq, flags);
1648
0
    return;
1649
0
  }
1650
1651
  /* TODO: Figure out a way to check for a bad signature, as opposed to
1652
     "not self-signed" */
1653
0
  if ( purple_certificate_signed_by(peer_crt, peer_crt) ) {
1654
0
    flags |= PURPLE_CERTIFICATE_SELF_SIGNED;
1655
1656
0
    purple_debug_info("certificate/x509/tls_cached",
1657
0
          "Certificate for %s is self-signed.\n",
1658
0
          vrq->subject_name);
1659
1660
0
    x509_tls_cached_check_subject_name(vrq, flags);
1661
0
    return;
1662
0
  } /* if (self signed) */
1663
1664
0
  ca = purple_certificate_find_pool(x509_tls_cached.scheme_name, "ca");
1665
1666
  /* Next, check that the certificate chain is valid */
1667
0
  if (!purple_certificate_check_signature_chain_with_failing(chain,
1668
0
        &failing_crt))
1669
0
  {
1670
0
    gboolean chain_validated = FALSE;
1671
    /*
1672
     * Check if the failing certificate is in the CA store. If it is, then
1673
     * consider this fully validated. This works around issues with some
1674
     * prominent intermediate CAs whose signature is md5WithRSAEncryption.
1675
     * I'm looking at CACert Class 3 here. See #4458 for details.
1676
     */
1677
0
    if (ca) {
1678
0
      gchar *uid = purple_certificate_get_unique_id(failing_crt);
1679
0
      PurpleCertificate *ca_crt = purple_certificate_pool_retrieve(ca, uid);
1680
0
      if (ca_crt != NULL) {
1681
0
        GByteArray *failing_fpr;
1682
0
        GByteArray *ca_fpr;
1683
0
        failing_fpr = purple_certificate_get_fingerprint_sha256(failing_crt, TRUE);
1684
0
        ca_fpr = purple_certificate_get_fingerprint_sha256(ca_crt, TRUE);
1685
0
        if (byte_arrays_equal(failing_fpr, ca_fpr)) {
1686
0
          purple_debug_info("certificate/x509/tls_cached",
1687
0
              "Full chain verification failed (probably a bad "
1688
0
              "signature algorithm), but found the last "
1689
0
              "certificate %s in the CA pool.\n", uid);
1690
0
          chain_validated = TRUE;
1691
0
        }
1692
1693
0
        g_byte_array_free(failing_fpr, TRUE);
1694
0
        g_byte_array_free(ca_fpr, TRUE);
1695
0
      }
1696
1697
0
      purple_certificate_destroy(ca_crt);
1698
0
      g_free(uid);
1699
0
    }
1700
1701
    /*
1702
     * If we get here, either the cert matched the stuff right above
1703
     * or it didn't, in which case we give up and complain to the user.
1704
     */
1705
0
    if (!chain_validated)
1706
      /* TODO: Tell the user where the chain broke? */
1707
0
      flags |= PURPLE_CERTIFICATE_INVALID_CHAIN;
1708
1709
0
    x509_tls_cached_check_subject_name(vrq, flags);
1710
0
    return;
1711
0
  } /* if (signature chain not good) */
1712
1713
  /* Next, attempt to verify the last certificate is signed by a trusted
1714
   * CA, or is a trusted CA (based on fingerprint).
1715
   */
1716
  /* If, for whatever reason, there is no Certificate Authority pool
1717
     loaded, we'll verify the subject name and then warn about thsi. */
1718
0
  if ( !ca ) {
1719
0
    purple_debug_error("certificate/x509/tls_cached",
1720
0
           "No X.509 Certificate Authority pool "
1721
0
           "could be found!\n");
1722
1723
0
    flags |= PURPLE_CERTIFICATE_NO_CA_POOL;
1724
1725
0
    x509_tls_cached_check_subject_name(vrq, flags);
1726
0
    return;
1727
0
  }
1728
1729
0
  end_crt = g_list_last(chain)->data;
1730
1731
  /* Attempt to look up the last certificate, and the last certificate's
1732
   * issuer. 
1733
   */
1734
0
  ca_id  = purple_certificate_get_issuer_unique_id(end_crt);
1735
0
  ca2_id = purple_certificate_get_unique_id(end_crt);
1736
0
  purple_debug_info("certificate/x509/tls_cached",
1737
0
        "Checking for a CA with DN=%s\n",
1738
0
        ca_id);
1739
0
  purple_debug_info("certificate/x509/tls_cached",
1740
0
        "Also checking for a CA with DN=%s\n",
1741
0
        ca2_id);
1742
0
  ca_crts = g_slist_concat(x509_ca_get_certs(ca_id), x509_ca_get_certs(ca2_id));
1743
0
  g_free(ca_id);
1744
0
  g_free(ca2_id);
1745
0
  if ( NULL == ca_crts ) {
1746
0
    flags |= PURPLE_CERTIFICATE_CA_UNKNOWN;
1747
1748
0
    purple_debug_warning("certificate/x509/tls_cached",
1749
0
          "No Certificate Authorities with either DN found "
1750
0
          "found. I'll prompt the user, I guess.\n");
1751
1752
0
    x509_tls_cached_check_subject_name(vrq, flags);
1753
0
    return;
1754
0
  }
1755
1756
  /*
1757
   * Check the fingerprints; if they match, then this certificate *is* one
1758
   * of the designated "trusted roots", and we don't need to verify the
1759
   * signature. This is good because some of the older roots are self-signed
1760
   * with bad hash algorithms that we don't want to allow in any other
1761
   * circumstances (one of Verisign's root CAs is self-signed with MD2).
1762
   *
1763
   * If the fingerprints don't match, we'll fall back to checking the
1764
   * signature.
1765
   */
1766
0
  last_fpr = purple_certificate_get_fingerprint_sha256(end_crt, TRUE);
1767
1768
0
  ca_id = purple_certificate_get_unique_id(end_crt);
1769
1770
0
  for (cur = ca_crts; cur; cur = cur->next) {
1771
0
    ca_crt = cur->data;
1772
0
    ca_fpr = purple_certificate_get_fingerprint_sha256(ca_crt, TRUE);
1773
0
    ca2_id = purple_certificate_get_unique_id(ca_crt);
1774
1775
0
    if ( byte_arrays_equal(last_fpr, ca_fpr) ||
1776
0
        (purple_strequal(ca_id, ca2_id) &&
1777
0
         purple_certificate_compare_pubkeys(end_crt, ca_crt)) ||
1778
0
        purple_certificate_signed_by(end_crt, ca_crt) )
1779
0
    {
1780
      /* TODO: If signed_by ever returns a reason, maybe mention
1781
         that, too. */
1782
      /* TODO: Also mention the CA involved. While I could do this
1783
         now, a full DN is a little much with which to assault the
1784
         user's poor, leaky eyes. */
1785
0
      valid = TRUE;
1786
0
      g_byte_array_free(ca_fpr, TRUE);
1787
0
      g_free(ca2_id);
1788
0
      break;
1789
0
    }
1790
1791
0
    g_byte_array_free(ca_fpr, TRUE);
1792
0
    g_free(ca2_id);
1793
0
  }
1794
0
  g_free(ca_id);
1795
1796
0
  if (valid == FALSE)
1797
0
    flags |= PURPLE_CERTIFICATE_INVALID_CHAIN;
1798
1799
0
  g_slist_free_full(ca_crts, (GDestroyNotify)purple_certificate_destroy);
1800
0
  g_byte_array_free(last_fpr, TRUE);
1801
1802
0
  x509_tls_cached_check_subject_name(vrq, flags);
1803
0
}
1804
1805
static void
1806
x509_tls_cached_start_verify(PurpleCertificateVerificationRequest *vrq)
1807
0
{
1808
0
  const gchar *tls_peers_name = "tls_peers"; /* Name of local cache */
1809
0
  PurpleCertificatePool *tls_peers;
1810
0
  time_t now, activation, expiration;
1811
0
  PurpleCertificateInvalidityFlags flags = PURPLE_CERTIFICATE_NO_PROBLEMS;
1812
0
  gboolean ret;
1813
1814
0
  g_return_if_fail(vrq);
1815
1816
0
  purple_debug_info("certificate/x509/tls_cached",
1817
0
        "Starting verify for %s\n",
1818
0
        vrq->subject_name);
1819
1820
  /*
1821
   * Verify the first certificate (the main one) has been activated and
1822
   * isn't expired, i.e. activation < now < expiration.
1823
   */
1824
0
  now = time(NULL);
1825
0
  ret = purple_certificate_get_times(vrq->cert_chain->data, &activation,
1826
0
                                     &expiration);
1827
0
  if (!ret) {
1828
0
    flags |= PURPLE_CERTIFICATE_EXPIRED | PURPLE_CERTIFICATE_NOT_ACTIVATED;
1829
0
    purple_debug_error("certificate/x509/tls_cached",
1830
0
        "Failed to get validity times for certificate %s\n",
1831
0
        vrq->subject_name);
1832
0
  } else if (now > expiration) {
1833
0
    flags |= PURPLE_CERTIFICATE_EXPIRED;
1834
0
    purple_debug_error("certificate/x509/tls_cached",
1835
0
        "Certificate %s expired at %s\n",
1836
0
        vrq->subject_name, ctime(&expiration));
1837
0
  } else if (now < activation) {
1838
0
    flags |= PURPLE_CERTIFICATE_NOT_ACTIVATED;
1839
0
    purple_debug_error("certificate/x509/tls_cached",
1840
0
        "Certificate %s is not yet valid, will be at %s\n",
1841
0
        vrq->subject_name, ctime(&activation));
1842
0
  }
1843
1844
0
  tls_peers = purple_certificate_find_pool(x509_tls_cached.scheme_name,tls_peers_name);
1845
1846
0
  if (!tls_peers) {
1847
0
    purple_debug_error("certificate/x509/tls_cached",
1848
0
           "Couldn't find local peers cache %s\n",
1849
0
           tls_peers_name);
1850
1851
    /* vrq now becomes the problem of unknown_peer */
1852
0
    x509_tls_cached_unknown_peer(vrq, flags);
1853
0
    return;
1854
0
  }
1855
1856
  /* Check if the peer has a certificate cached already */
1857
0
  purple_debug_info("certificate/x509/tls_cached",
1858
0
        "Checking for cached cert...\n");
1859
0
  if (purple_certificate_pool_contains(tls_peers, vrq->subject_name)) {
1860
0
    purple_debug_info("certificate/x509/tls_cached",
1861
0
          "...Found cached cert\n");
1862
    /* vrq is now the responsibility of cert_in_cache */
1863
0
    x509_tls_cached_cert_in_cache(vrq, flags);
1864
0
  } else {
1865
0
    purple_debug_warning("certificate/x509/tls_cached",
1866
0
          "...Not in cache\n");
1867
    /* vrq now becomes the problem of unknown_peer */
1868
0
    x509_tls_cached_unknown_peer(vrq, flags);
1869
0
  }
1870
0
}
1871
1872
static void
1873
x509_tls_cached_destroy_request(PurpleCertificateVerificationRequest *vrq)
1874
0
{
1875
0
  g_return_if_fail(vrq);
1876
0
}
1877
1878
static PurpleCertificateVerifier x509_tls_cached = {
1879
  "x509",                         /* Scheme name */
1880
  "tls_cached",                   /* Verifier name */
1881
  x509_tls_cached_start_verify,   /* Verification begin */
1882
  x509_tls_cached_destroy_request,/* Request cleanup */
1883
1884
  NULL,
1885
  NULL,
1886
  NULL,
1887
  NULL
1888
1889
};
1890
1891
/****************************************************************************/
1892
/* Subsystem                                                                */
1893
/****************************************************************************/
1894
void
1895
purple_certificate_init(void)
1896
0
{
1897
  /* Register builtins */
1898
0
  purple_certificate_register_verifier(&x509_singleuse);
1899
0
  purple_certificate_register_pool(&x509_ca);
1900
0
  purple_certificate_register_pool(&x509_tls_peers);
1901
0
  purple_certificate_register_verifier(&x509_tls_cached);
1902
0
}
1903
1904
void
1905
purple_certificate_uninit(void)
1906
0
{
1907
  /* Unregister all Verifiers */
1908
0
  g_list_foreach(cert_verifiers, (GFunc)purple_certificate_unregister_verifier, NULL);
1909
1910
  /* Unregister all Pools */
1911
0
  g_list_foreach(cert_pools, (GFunc)purple_certificate_unregister_pool, NULL);
1912
0
}
1913
1914
gpointer
1915
purple_certificate_get_handle(void)
1916
0
{
1917
0
  static gint handle;
1918
0
  return &handle;
1919
0
}
1920
1921
PurpleCertificateScheme *
1922
purple_certificate_find_scheme(const gchar *name)
1923
0
{
1924
0
  PurpleCertificateScheme *scheme = NULL;
1925
0
  GList *l;
1926
1927
0
  g_return_val_if_fail(name, NULL);
1928
1929
  /* Traverse the list of registered schemes and locate the
1930
     one whose name matches */
1931
0
  for(l = cert_schemes; l; l = l->next) {
1932
0
    scheme = (PurpleCertificateScheme *)(l->data);
1933
1934
    /* Name matches? that's our man */
1935
0
    if(!g_ascii_strcasecmp(scheme->name, name))
1936
0
      return scheme;
1937
0
  }
1938
1939
0
  purple_debug_warning("certificate",
1940
0
           "CertificateScheme %s requested but not found.\n",
1941
0
           name);
1942
1943
  /* TODO: Signalling and such? */
1944
1945
0
  return NULL;
1946
0
}
1947
1948
GList *
1949
purple_certificate_get_schemes(void)
1950
0
{
1951
0
  return cert_schemes;
1952
0
}
1953
1954
gboolean
1955
purple_certificate_register_scheme(PurpleCertificateScheme *scheme)
1956
0
{
1957
0
  g_return_val_if_fail(scheme != NULL, FALSE);
1958
1959
  /* Make sure no scheme is registered with the same name */
1960
0
  if (purple_certificate_find_scheme(scheme->name) != NULL) {
1961
0
    return FALSE;
1962
0
  }
1963
1964
  /* Okay, we're golden. Register it. */
1965
0
  cert_schemes = g_list_prepend(cert_schemes, scheme);
1966
1967
  /* TODO: Signalling and such? */
1968
1969
0
  purple_debug_info("certificate",
1970
0
        "CertificateScheme %s registered\n",
1971
0
        scheme->name);
1972
1973
0
  return TRUE;
1974
0
}
1975
1976
gboolean
1977
purple_certificate_unregister_scheme(PurpleCertificateScheme *scheme)
1978
0
{
1979
0
  if (NULL == scheme) {
1980
0
    purple_debug_warning("certificate",
1981
0
             "Attempting to unregister NULL scheme\n");
1982
0
    return FALSE;
1983
0
  }
1984
1985
  /* TODO: signalling? */
1986
1987
  /* TODO: unregister all CertificateVerifiers for this scheme?*/
1988
  /* TODO: unregister all CertificatePools for this scheme? */
1989
  /* Neither of the above should be necessary, though */
1990
0
  cert_schemes = g_list_remove(cert_schemes, scheme);
1991
1992
0
  purple_debug_info("certificate",
1993
0
        "CertificateScheme %s unregistered\n",
1994
0
        scheme->name);
1995
1996
1997
0
  return TRUE;
1998
0
}
1999
2000
PurpleCertificateVerifier *
2001
purple_certificate_find_verifier(const gchar *scheme_name, const gchar *ver_name)
2002
0
{
2003
0
  PurpleCertificateVerifier *vr = NULL;
2004
0
  GList *l;
2005
2006
0
  g_return_val_if_fail(scheme_name, NULL);
2007
0
  g_return_val_if_fail(ver_name, NULL);
2008
2009
  /* Traverse the list of registered verifiers and locate the
2010
     one whose name matches */
2011
0
  for(l = cert_verifiers; l; l = l->next) {
2012
0
    vr = (PurpleCertificateVerifier *)(l->data);
2013
2014
    /* Scheme and name match? */
2015
0
    if(!g_ascii_strcasecmp(vr->scheme_name, scheme_name) &&
2016
0
       !g_ascii_strcasecmp(vr->name, ver_name))
2017
0
      return vr;
2018
0
  }
2019
2020
0
  purple_debug_warning("certificate",
2021
0
           "CertificateVerifier %s, %s requested but not found.\n",
2022
0
           scheme_name, ver_name);
2023
2024
  /* TODO: Signalling and such? */
2025
2026
0
  return NULL;
2027
0
}
2028
2029
2030
GList *
2031
purple_certificate_get_verifiers(void)
2032
0
{
2033
0
  return cert_verifiers;
2034
0
}
2035
2036
gboolean
2037
purple_certificate_register_verifier(PurpleCertificateVerifier *vr)
2038
0
{
2039
0
  g_return_val_if_fail(vr != NULL, FALSE);
2040
2041
  /* Make sure no verifier is registered with the same scheme/name */
2042
0
  if (purple_certificate_find_verifier(vr->scheme_name, vr->name) != NULL) {
2043
0
    return FALSE;
2044
0
  }
2045
2046
  /* Okay, we're golden. Register it. */
2047
0
  cert_verifiers = g_list_prepend(cert_verifiers, vr);
2048
2049
  /* TODO: Signalling and such? */
2050
2051
0
  purple_debug_info("certificate",
2052
0
        "CertificateVerifier %s registered\n",
2053
0
        vr->name);
2054
0
  return TRUE;
2055
0
}
2056
2057
gboolean
2058
purple_certificate_unregister_verifier(PurpleCertificateVerifier *vr)
2059
0
{
2060
0
  if (NULL == vr) {
2061
0
    purple_debug_warning("certificate",
2062
0
             "Attempting to unregister NULL verifier\n");
2063
0
    return FALSE;
2064
0
  }
2065
2066
  /* TODO: signalling? */
2067
2068
0
  cert_verifiers = g_list_remove(cert_verifiers, vr);
2069
2070
2071
0
  purple_debug_info("certificate",
2072
0
        "CertificateVerifier %s unregistered\n",
2073
0
        vr->name);
2074
2075
0
  return TRUE;
2076
0
}
2077
2078
PurpleCertificatePool *
2079
purple_certificate_find_pool(const gchar *scheme_name, const gchar *pool_name)
2080
0
{
2081
0
  PurpleCertificatePool *pool = NULL;
2082
0
  GList *l;
2083
2084
0
  g_return_val_if_fail(scheme_name, NULL);
2085
0
  g_return_val_if_fail(pool_name, NULL);
2086
2087
  /* Traverse the list of registered pools and locate the
2088
     one whose name matches */
2089
0
  for(l = cert_pools; l; l = l->next) {
2090
0
    pool = (PurpleCertificatePool *)(l->data);
2091
2092
    /* Scheme and name match? */
2093
0
    if(!g_ascii_strcasecmp(pool->scheme_name, scheme_name) &&
2094
0
       !g_ascii_strcasecmp(pool->name, pool_name))
2095
0
      return pool;
2096
0
  }
2097
2098
0
  purple_debug_warning("certificate",
2099
0
           "CertificatePool %s, %s requested but not found.\n",
2100
0
           scheme_name, pool_name);
2101
2102
  /* TODO: Signalling and such? */
2103
2104
0
  return NULL;
2105
2106
0
}
2107
2108
GList *
2109
purple_certificate_get_pools(void)
2110
0
{
2111
0
  return cert_pools;
2112
0
}
2113
2114
gboolean
2115
purple_certificate_register_pool(PurpleCertificatePool *pool)
2116
0
{
2117
0
  g_return_val_if_fail(pool, FALSE);
2118
0
  g_return_val_if_fail(pool->scheme_name, FALSE);
2119
0
  g_return_val_if_fail(pool->name, FALSE);
2120
0
  g_return_val_if_fail(pool->fullname, FALSE);
2121
2122
  /* Make sure no pools are registered under this name */
2123
0
  if (purple_certificate_find_pool(pool->scheme_name, pool->name)) {
2124
0
    return FALSE;
2125
0
  }
2126
2127
  /* Initialize the pool if needed */
2128
0
  if (pool->init) {
2129
0
    gboolean success;
2130
2131
0
    success = pool->init();
2132
0
    if (!success)
2133
0
      return FALSE;
2134
0
  }
2135
2136
  /* Register the Pool */
2137
0
  cert_pools = g_list_prepend(cert_pools, pool);
2138
2139
  /* TODO: Emit a signal that the pool got registered */
2140
2141
0
  PURPLE_DBUS_REGISTER_POINTER(pool, PurpleCertificatePool);
2142
0
  purple_signal_register(pool, /* Signals emitted from pool */
2143
0
             "certificate-stored",
2144
0
             purple_marshal_VOID__POINTER_POINTER,
2145
0
             NULL, /* No callback return value */
2146
0
             2,    /* Two non-data arguments */
2147
0
             purple_value_new(PURPLE_TYPE_SUBTYPE,
2148
0
            PURPLE_SUBTYPE_CERTIFICATEPOOL),
2149
0
             purple_value_new(PURPLE_TYPE_STRING));
2150
2151
0
  purple_signal_register(pool, /* Signals emitted from pool */
2152
0
             "certificate-deleted",
2153
0
             purple_marshal_VOID__POINTER_POINTER,
2154
0
             NULL, /* No callback return value */
2155
0
             2,    /* Two non-data arguments */
2156
0
             purple_value_new(PURPLE_TYPE_SUBTYPE,
2157
0
            PURPLE_SUBTYPE_CERTIFICATEPOOL),
2158
0
             purple_value_new(PURPLE_TYPE_STRING));
2159
2160
0
  purple_debug_info("certificate",
2161
0
      "CertificatePool %s registered\n",
2162
0
      pool->name);
2163
2164
0
  return TRUE;
2165
0
}
2166
2167
gboolean
2168
purple_certificate_unregister_pool(PurpleCertificatePool *pool)
2169
0
{
2170
0
  if (NULL == pool) {
2171
0
    purple_debug_warning("certificate",
2172
0
             "Attempting to unregister NULL pool\n");
2173
0
    return FALSE;
2174
0
  }
2175
2176
  /* Check that the pool is registered */
2177
0
  if (!g_list_find(cert_pools, pool)) {
2178
0
    purple_debug_warning("certificate",
2179
0
             "Pool to unregister isn't registered!\n");
2180
2181
0
    return FALSE;
2182
0
  }
2183
2184
  /* Uninit the pool if needed */
2185
0
  PURPLE_DBUS_UNREGISTER_POINTER(pool);
2186
0
  if (pool->uninit) {
2187
0
    pool->uninit();
2188
0
  }
2189
2190
0
  cert_pools = g_list_remove(cert_pools, pool);
2191
2192
  /* TODO: Signalling? */
2193
0
  purple_signal_unregister(pool, "certificate-stored");
2194
0
  purple_signal_unregister(pool, "certificate-deleted");
2195
2196
0
  purple_debug_info("certificate",
2197
0
        "CertificatePool %s unregistered\n",
2198
0
        pool->name);
2199
0
  return TRUE;
2200
0
}
2201
2202
/****************************************************************************/
2203
/* Scheme-specific functions                                                */
2204
/****************************************************************************/
2205
2206
0
static void display_x509_issuer(gchar *issuer_id) {
2207
0
  PurpleCertificate *issuer_crt;
2208
2209
0
  issuer_crt = x509_ca_get_cert(issuer_id);
2210
2211
0
  if (issuer_crt) {
2212
0
    purple_certificate_display_x509(issuer_crt);
2213
0
    purple_certificate_destroy(issuer_crt);
2214
0
  } else {
2215
0
    purple_notify_info(NULL, /* TODO: Find what the handle ought to be */
2216
0
      _("Certificate Information"),
2217
0
      "",
2218
0
      _("Unable to find Issuer Certificate"));
2219
0
  }
2220
2221
0
  g_free(issuer_id);
2222
0
}
2223
2224
void
2225
purple_certificate_display_x509(PurpleCertificate *crt)
2226
0
{
2227
0
  gchar *sha1_asc, *sha256_asc;
2228
0
  gchar *cn, *issuer_id;
2229
0
  time_t activation, expiration;
2230
0
  gchar *activ_str, *expir_str;
2231
0
  gchar *secondary, *secondary_extra;
2232
0
  gboolean self_signed;
2233
2234
0
  get_ascii_fingerprints(crt, &sha1_asc, &sha256_asc);
2235
2236
  /* Get the cert Common Name */
2237
  /* TODO: Will break on CA certs */
2238
0
  cn = purple_certificate_get_subject_name(crt);
2239
2240
0
  issuer_id = purple_certificate_get_issuer_unique_id(crt);
2241
2242
  /* Get the certificate times */
2243
  /* TODO: Check the times against localtime */
2244
  /* TODO: errorcheck? */
2245
0
  if (!purple_certificate_get_times(crt, &activation, &expiration)) {
2246
0
    purple_debug_error("certificate",
2247
0
           "Failed to get certificate times!\n");
2248
0
    activation = expiration = 0;
2249
0
  }
2250
0
  activ_str = g_strdup(ctime(&activation));
2251
0
  expir_str = g_strdup(ctime(&expiration));
2252
2253
0
  self_signed = purple_certificate_signed_by(crt, crt);
2254
2255
  /* Make messages */
2256
0
  secondary = g_strdup_printf(_("Common name: %s\n\n"
2257
0
          "Issued By: %s\n\n"
2258
0
          "Fingerprint (SHA1): %s\n\n"
2259
0
          "Activation date: %s\n"
2260
0
          "Expiration date: %s\n"),
2261
0
        cn ? cn : "(null)",
2262
0
        self_signed ? _("(self-signed)") : (issuer_id ? issuer_id : "(null)"),
2263
0
        sha1_asc ? sha1_asc : "(null)",
2264
0
        activ_str ? activ_str : "(null)",
2265
0
        expir_str ? expir_str : "(null)");
2266
2267
  /* TODO: make this part of the translatable string above */
2268
0
  secondary_extra = g_strdup_printf("%sSHA256: %s", secondary, sha256_asc);
2269
2270
  /* Make a semi-pretty display */
2271
0
  if (self_signed) {
2272
0
    purple_notify_info(NULL, /* TODO: Find what the handle ought to be */
2273
0
      _("Certificate Information"),
2274
0
      "",
2275
0
      secondary_extra);
2276
0
  } else {
2277
0
    purple_request_action(NULL, /* TODO: Find what the handle ought to be */
2278
0
      _("Certificate Information"), _("Certificate Information"),
2279
0
      secondary_extra, 2, NULL, NULL, NULL,
2280
0
      issuer_id, 2,
2281
0
      _("View Issuer Certificate"), PURPLE_CALLBACK(display_x509_issuer),
2282
0
      _("Close"), PURPLE_CALLBACK(g_free));
2283
2284
    /* purple_request_action has taken ownership of issuer_id */
2285
0
    issuer_id = NULL;
2286
0
  }
2287
2288
  /* Cleanup */
2289
0
  g_free(cn);
2290
0
  g_free(issuer_id);
2291
0
  g_free(secondary);
2292
0
  g_free(secondary_extra);
2293
0
  g_free(sha1_asc);
2294
0
  g_free(sha256_asc);
2295
0
  g_free(activ_str);
2296
0
  g_free(expir_str);
2297
0
}
2298
2299
void purple_certificate_add_ca_search_path(const char *path)
2300
0
{
2301
0
  if (g_list_find_custom(x509_ca_paths, path, (GCompareFunc)strcmp))
2302
0
    return;
2303
0
  x509_ca_paths = g_list_append(x509_ca_paths, g_strdup(path));
2304
0
}
2305