-
Notifications
You must be signed in to change notification settings - Fork 2
/
certificate.c
2219 lines (1789 loc) · 58.7 KB
/
certificate.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
198
199
200
201
202
203
204
205
206
207
208
209
210
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file certificate.c Public-Key Certificate API
* @ingroup core
*/
/*
*
* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "internal.h"
#include "certificate.h"
#include "dbus-maybe.h"
#include "debug.h"
#include "request.h"
#include "signals.h"
#include "util.h"
/** List holding pointers to all registered certificate schemes */
static GList *cert_schemes = NULL;
/** List of registered Verifiers */
static GList *cert_verifiers = NULL;
/** List of registered Pools */
static GList *cert_pools = NULL;
/*
* TODO: Merge this with PurpleCertificateVerificationStatus for 3.0.0 */
typedef enum {
PURPLE_CERTIFICATE_UNKNOWN_ERROR = -1,
/* Not an error */
PURPLE_CERTIFICATE_NO_PROBLEMS = 0,
/* Non-fatal */
PURPLE_CERTIFICATE_NON_FATALS_MASK = 0x0000FFFF,
/* The certificate is self-signed. */
PURPLE_CERTIFICATE_SELF_SIGNED = 0x01,
/* The CA is not in libpurple's pool of certificates. */
PURPLE_CERTIFICATE_CA_UNKNOWN = 0x02,
/* The current time is before the certificate's specified
* activation time.
*/
PURPLE_CERTIFICATE_NOT_ACTIVATED = 0x04,
/* The current time is after the certificate's specified expiration time */
PURPLE_CERTIFICATE_EXPIRED = 0x08,
/* The certificate's subject name doesn't match the expected */
PURPLE_CERTIFICATE_NAME_MISMATCH = 0x10,
/* No CA pool was found. This shouldn't happen... */
PURPLE_CERTIFICATE_NO_CA_POOL = 0x20,
/* Fatal */
PURPLE_CERTIFICATE_FATALS_MASK = 0xFFFF0000,
/* The signature chain could not be validated. Due to limitations in the
* the current API, this also indicates one of the CA certificates in the
* chain is expired (or not yet activated). FIXME 3.0.0 */
PURPLE_CERTIFICATE_INVALID_CHAIN = 0x10000,
/* The signature has been revoked. */
PURPLE_CERTIFICATE_REVOKED = 0x20000,
PURPLE_CERTIFICATE_LAST = 0x40000,
} PurpleCertificateInvalidityFlags;
static const gchar *
invalidity_reason_to_string(PurpleCertificateInvalidityFlags flag)
{
switch (flag) {
case PURPLE_CERTIFICATE_SELF_SIGNED:
return _("The certificate is self-signed and cannot be "
"automatically checked.");
break;
case PURPLE_CERTIFICATE_CA_UNKNOWN:
return _("The certificate is not trusted because no certificate "
"that can verify it is currently trusted.");
break;
case PURPLE_CERTIFICATE_NOT_ACTIVATED:
return _("The certificate is not valid yet. Check that your "
"computer's date and time are accurate.");
break;
case PURPLE_CERTIFICATE_EXPIRED:
return _("The certificate has expired and should not be "
"considered valid. Check that your computer's date "
"and time are accurate.");
break;
case PURPLE_CERTIFICATE_NAME_MISMATCH:
/* Translators: "domain" refers to a DNS domain (e.g. talk.google.com) */
return _("The certificate presented is not issued to this domain.");
break;
case PURPLE_CERTIFICATE_NO_CA_POOL:
return _("You have no database of root certificates, so "
"this certificate cannot be validated.");
break;
case PURPLE_CERTIFICATE_INVALID_CHAIN:
return _("The certificate chain presented is invalid.");
break;
case PURPLE_CERTIFICATE_REVOKED:
return _("The certificate has been revoked.");
break;
case PURPLE_CERTIFICATE_UNKNOWN_ERROR:
default:
return _("An unknown certificate error occurred.");
break;
}
}
void
purple_certificate_verify (PurpleCertificateVerifier *verifier,
const gchar *subject_name, GList *cert_chain,
PurpleCertificateVerifiedCallback cb,
gpointer cb_data)
{
PurpleCertificateVerificationRequest *vrq;
PurpleCertificateScheme *scheme;
g_return_if_fail(subject_name != NULL);
/* If you don't have a cert to check, why are you requesting that it
be verified? */
g_return_if_fail(cert_chain != NULL);
g_return_if_fail(cb != NULL);
/* Look up the CertificateScheme */
scheme = purple_certificate_find_scheme(verifier->scheme_name);
g_return_if_fail(scheme);
/* Check that at least the first cert in the chain matches the
Verifier scheme */
g_return_if_fail(scheme ==
((PurpleCertificate *) (cert_chain->data))->scheme);
/* Construct and fill in the request fields */
vrq = g_new0(PurpleCertificateVerificationRequest, 1);
vrq->verifier = verifier;
vrq->scheme = scheme;
vrq->subject_name = g_strdup(subject_name);
vrq->cert_chain = purple_certificate_copy_list(cert_chain);
vrq->cb = cb;
vrq->cb_data = cb_data;
/* Initiate verification */
(verifier->start_verification)(vrq);
}
void
purple_certificate_verify_complete(PurpleCertificateVerificationRequest *vrq,
PurpleCertificateVerificationStatus st)
{
PurpleCertificateVerifier *vr;
g_return_if_fail(vrq);
if (st == PURPLE_CERTIFICATE_VALID) {
purple_debug_info("certificate",
"Successfully verified certificate for %s\n",
vrq->subject_name);
} else {
purple_debug_error("certificate",
"Failed to verify certificate for %s\n",
vrq->subject_name);
}
/* Pass the results on to the request's callback */
(vrq->cb)(st, vrq->cb_data);
/* And now to eliminate the request */
/* Fetch the Verifier responsible... */
vr = vrq->verifier;
/* ...and order it to KILL */
(vr->destroy_request)(vrq);
/* Now the internals have been cleaned up, so clean up the libpurple-
created elements */
g_free(vrq->subject_name);
purple_certificate_destroy_list(vrq->cert_chain);
/* A structure born
* to much ado
* and with so much within.
* It reaches now
* its quiet end. */
g_free(vrq);
}
PurpleCertificate *
purple_certificate_copy(PurpleCertificate *crt)
{
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
g_return_val_if_fail(crt->scheme->copy_certificate, NULL);
return (crt->scheme->copy_certificate)(crt);
}
GList *
purple_certificate_copy_list(GList *crt_list)
{
GList *new_l, *l;
/* First, make a shallow copy of the list */
new_l = g_list_copy(crt_list);
/* Now go through and actually duplicate each certificate */
for (l = new_l; l; l = l->next) {
l->data = purple_certificate_copy(l->data);
}
return new_l;
}
void
purple_certificate_destroy (PurpleCertificate *crt)
{
PurpleCertificateScheme *scheme;
if (NULL == crt) return;
scheme = crt->scheme;
(scheme->destroy_certificate)(crt);
}
void
purple_certificate_destroy_list (GList * crt_list)
{
PurpleCertificate *crt;
GList *l;
for (l=crt_list; l; l = l->next) {
crt = (PurpleCertificate *) l->data;
purple_certificate_destroy(crt);
}
g_list_free(crt_list);
}
gboolean
purple_certificate_signed_by(PurpleCertificate *crt, PurpleCertificate *issuer)
{
PurpleCertificateScheme *scheme;
g_return_val_if_fail(crt, FALSE);
g_return_val_if_fail(issuer, FALSE);
scheme = crt->scheme;
g_return_val_if_fail(scheme, FALSE);
/* We can't compare two certs of unrelated schemes, obviously */
g_return_val_if_fail(issuer->scheme == scheme, FALSE);
return (scheme->signed_by)(crt, issuer);
}
gboolean
purple_certificate_check_signature_chain_with_failing(GList *chain,
PurpleCertificate **failing)
{
GList *cur;
PurpleCertificate *crt, *issuer;
gchar *uid;
time_t now, activation, expiration;
gboolean ret;
g_return_val_if_fail(chain, FALSE);
if (failing)
*failing = NULL;
uid = purple_certificate_get_unique_id((PurpleCertificate *) chain->data);
purple_debug_info("certificate",
"Checking signature chain for uid=%s\n",
uid);
g_free(uid);
/* If this is a single-certificate chain, say that it is valid */
if (chain->next == NULL) {
purple_debug_info("certificate",
"...Singleton. We'll say it's valid.\n");
return TRUE;
}
now = time(NULL);
/* Load crt with the first certificate */
crt = (PurpleCertificate *)(chain->data);
/* And start with the second certificate in the chain */
for ( cur = chain->next; cur; cur = cur->next ) {
issuer = (PurpleCertificate *)(cur->data);
uid = purple_certificate_get_unique_id(issuer);
ret = purple_certificate_get_times(issuer, &activation, &expiration);
if (!ret || now < activation || now > expiration) {
if (!ret)
purple_debug_error("certificate",
"...Failed to get validity times for certificate %s\n"
"Chain is INVALID\n", uid);
else if (now > expiration)
purple_debug_error("certificate",
"...Issuer %s expired at %s\nChain is INVALID\n",
uid, ctime(&expiration));
else
purple_debug_error("certificate",
"...Not-yet-activated issuer %s will be valid at %s\n"
"Chain is INVALID\n", uid, ctime(&activation));
if (failing)
*failing = crt;
g_free(uid);
return FALSE;
}
/* Check the signature for this link */
if (! purple_certificate_signed_by(crt, issuer) ) {
purple_debug_error("certificate",
"...Bad or missing signature by %s\nChain is INVALID\n",
uid);
g_free(uid);
if (failing)
*failing = crt;
return FALSE;
}
purple_debug_info("certificate",
"...Good signature by %s\n",
uid);
g_free(uid);
/* The issuer is now the next crt whose signature is to be
checked */
crt = issuer;
}
/* If control reaches this point, the chain is valid */
purple_debug_info("certificate", "Chain is VALID\n");
return TRUE;
}
gboolean
purple_certificate_check_signature_chain(GList *chain)
{
return purple_certificate_check_signature_chain_with_failing(chain, NULL);
}
PurpleCertificate *
purple_certificate_import(PurpleCertificateScheme *scheme, const gchar *filename)
{
g_return_val_if_fail(scheme, NULL);
g_return_val_if_fail(scheme->import_certificate, NULL);
g_return_val_if_fail(filename, NULL);
return (scheme->import_certificate)(filename);
}
GSList *
purple_certificates_import(PurpleCertificateScheme *scheme, const gchar *filename)
{
g_return_val_if_fail(scheme, NULL);
g_return_val_if_fail(scheme->import_certificates, NULL);
g_return_val_if_fail(filename, NULL);
return (scheme->import_certificates)(filename);
}
gboolean
purple_certificate_export(const gchar *filename, PurpleCertificate *crt)
{
PurpleCertificateScheme *scheme;
g_return_val_if_fail(filename, FALSE);
g_return_val_if_fail(crt, FALSE);
g_return_val_if_fail(crt->scheme, FALSE);
scheme = crt->scheme;
g_return_val_if_fail(scheme->export_certificate, FALSE);
return (scheme->export_certificate)(filename, crt);
}
static gboolean
byte_arrays_equal(const GByteArray *array1, const GByteArray *array2)
{
g_return_val_if_fail(array1 != NULL, FALSE);
g_return_val_if_fail(array2 != NULL, FALSE);
return (array1->len == array2->len) &&
(0 == memcmp(array1->data, array2->data, array1->len));
}
GByteArray *
purple_certificate_get_fingerprint_sha1(PurpleCertificate *crt)
{
PurpleCertificateScheme *scheme;
GByteArray *fpr;
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
scheme = crt->scheme;
g_return_val_if_fail(scheme->get_fingerprint_sha1, NULL);
fpr = (scheme->get_fingerprint_sha1)(crt);
return fpr;
}
gchar *
purple_certificate_get_unique_id(PurpleCertificate *crt)
{
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
g_return_val_if_fail(crt->scheme->get_unique_id, NULL);
return (crt->scheme->get_unique_id)(crt);
}
gchar *
purple_certificate_get_issuer_unique_id(PurpleCertificate *crt)
{
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
g_return_val_if_fail(crt->scheme->get_issuer_unique_id, NULL);
return (crt->scheme->get_issuer_unique_id)(crt);
}
gchar *
purple_certificate_get_subject_name(PurpleCertificate *crt)
{
PurpleCertificateScheme *scheme;
gchar *subject_name;
g_return_val_if_fail(crt, NULL);
g_return_val_if_fail(crt->scheme, NULL);
scheme = crt->scheme;
g_return_val_if_fail(scheme->get_subject_name, NULL);
subject_name = (scheme->get_subject_name)(crt);
return subject_name;
}
gboolean
purple_certificate_check_subject_name(PurpleCertificate *crt, const gchar *name)
{
PurpleCertificateScheme *scheme;
g_return_val_if_fail(crt, FALSE);
g_return_val_if_fail(crt->scheme, FALSE);
g_return_val_if_fail(name, FALSE);
scheme = crt->scheme;
g_return_val_if_fail(scheme->check_subject_name, FALSE);
return (scheme->check_subject_name)(crt, name);
}
gboolean
purple_certificate_get_times(PurpleCertificate *crt, time_t *activation, time_t *expiration)
{
PurpleCertificateScheme *scheme;
g_return_val_if_fail(crt, FALSE);
scheme = crt->scheme;
g_return_val_if_fail(scheme, FALSE);
/* If both provided references are NULL, what are you doing calling
this? */
g_return_val_if_fail( (activation != NULL) || (expiration != NULL), FALSE);
/* Throw the request on down to the certscheme */
return (scheme->get_times)(crt, activation, expiration);
}
gchar *
purple_certificate_pool_mkpath(PurpleCertificatePool *pool, const gchar *id)
{
gchar *path;
gchar *esc_scheme_name, *esc_name, *esc_id;
g_return_val_if_fail(pool, NULL);
g_return_val_if_fail(pool->scheme_name, NULL);
g_return_val_if_fail(pool->name, NULL);
/* Escape all the elements for filesystem-friendliness */
esc_scheme_name = g_strdup(purple_escape_filename(pool->scheme_name));
esc_name = g_strdup(purple_escape_filename(pool->name));
esc_id = id ? g_strdup(purple_escape_filename(id)) : NULL;
path = g_build_filename(purple_user_dir(),
"certificates", /* TODO: constantize this? */
esc_scheme_name,
esc_name,
esc_id,
NULL);
g_free(esc_scheme_name);
g_free(esc_name);
g_free(esc_id);
return path;
}
gboolean
purple_certificate_pool_usable(PurpleCertificatePool *pool)
{
g_return_val_if_fail(pool, FALSE);
g_return_val_if_fail(pool->scheme_name, FALSE);
/* Check that the pool's scheme is loaded */
if (purple_certificate_find_scheme(pool->scheme_name) == NULL) {
return FALSE;
}
return TRUE;
}
PurpleCertificateScheme *
purple_certificate_pool_get_scheme(PurpleCertificatePool *pool)
{
g_return_val_if_fail(pool, NULL);
g_return_val_if_fail(pool->scheme_name, NULL);
return purple_certificate_find_scheme(pool->scheme_name);
}
gboolean
purple_certificate_pool_contains(PurpleCertificatePool *pool, const gchar *id)
{
g_return_val_if_fail(pool, FALSE);
g_return_val_if_fail(id, FALSE);
g_return_val_if_fail(pool->cert_in_pool, FALSE);
return (pool->cert_in_pool)(id);
}
PurpleCertificate *
purple_certificate_pool_retrieve(PurpleCertificatePool *pool, const gchar *id)
{
g_return_val_if_fail(pool, NULL);
g_return_val_if_fail(id, NULL);
g_return_val_if_fail(pool->get_cert, NULL);
return (pool->get_cert)(id);
}
gboolean
purple_certificate_pool_store(PurpleCertificatePool *pool, const gchar *id, PurpleCertificate *crt)
{
gboolean ret = FALSE;
g_return_val_if_fail(pool, FALSE);
g_return_val_if_fail(id, FALSE);
g_return_val_if_fail(pool->put_cert, FALSE);
/* Whether crt->scheme matches find_scheme(pool->scheme_name) is not
relevant... I think... */
g_return_val_if_fail(
g_ascii_strcasecmp(pool->scheme_name, crt->scheme->name) == 0,
FALSE);
ret = (pool->put_cert)(id, crt);
/* Signal that the certificate was stored if success*/
if (ret) {
purple_signal_emit(pool, "certificate-stored",
pool, id);
}
return ret;
}
gboolean
purple_certificate_pool_delete(PurpleCertificatePool *pool, const gchar *id)
{
gboolean ret = FALSE;
g_return_val_if_fail(pool, FALSE);
g_return_val_if_fail(id, FALSE);
g_return_val_if_fail(pool->delete_cert, FALSE);
ret = (pool->delete_cert)(id);
/* Signal that the certificate was deleted if success */
if (ret) {
purple_signal_emit(pool, "certificate-deleted",
pool, id);
}
return ret;
}
GList *
purple_certificate_pool_get_idlist(PurpleCertificatePool *pool)
{
g_return_val_if_fail(pool, NULL);
g_return_val_if_fail(pool->get_idlist, NULL);
return (pool->get_idlist)();
}
void
purple_certificate_pool_destroy_idlist(GList *idlist)
{
GList *l;
/* Iterate through and free them strings */
for ( l = idlist; l; l = l->next ) {
g_free(l->data);
}
g_list_free(idlist);
}
/****************************************************************************/
/* Builtin Verifiers, Pools, etc. */
/****************************************************************************/
static void
x509_singleuse_verify_cb (PurpleCertificateVerificationRequest *vrq, gint id)
{
g_return_if_fail(vrq);
purple_debug_info("certificate/x509_singleuse",
"VRQ on cert from %s gave %d\n",
vrq->subject_name, id);
/* Signal what happened back to the caller */
if (1 == id) {
/* Accepted! */
purple_certificate_verify_complete(vrq,
PURPLE_CERTIFICATE_VALID);
} else {
/* Not accepted */
purple_certificate_verify_complete(vrq,
PURPLE_CERTIFICATE_INVALID);
}
}
static void
x509_singleuse_start_verify (PurpleCertificateVerificationRequest *vrq)
{
gchar *sha_asc;
GByteArray *sha_bin;
gchar *cn;
const gchar *cn_match;
gchar *primary, *secondary;
PurpleCertificate *crt = (PurpleCertificate *) vrq->cert_chain->data;
/* Pull out the SHA1 checksum */
sha_bin = purple_certificate_get_fingerprint_sha1(crt);
/* Now decode it for display */
sha_asc = purple_base16_encode_chunked(sha_bin->data,
sha_bin->len);
/* Get the cert Common Name */
cn = purple_certificate_get_subject_name(crt);
/* Determine whether the name matches */
if (purple_certificate_check_subject_name(crt, vrq->subject_name)) {
cn_match = "";
} else {
cn_match = _("(DOES NOT MATCH)");
}
/* Make messages */
primary = g_strdup_printf(_("%s has presented the following certificate for just-this-once use:"), vrq->subject_name);
secondary = g_strdup_printf(_("Common name: %s %s\nFingerprint (SHA1): %s"), cn, cn_match, sha_asc);
/* Make a semi-pretty display */
purple_request_accept_cancel(
vrq->cb_data, /* TODO: Find what the handle ought to be */
_("Single-use Certificate Verification"),
primary,
secondary,
0, /* Accept by default */
NULL, /* No account */
NULL, /* No other user */
NULL, /* No associated conversation */
vrq,
x509_singleuse_verify_cb,
x509_singleuse_verify_cb );
/* Cleanup */
g_free(cn);
g_free(primary);
g_free(secondary);
g_free(sha_asc);
g_byte_array_free(sha_bin, TRUE);
}
static void
x509_singleuse_destroy_request (PurpleCertificateVerificationRequest *vrq)
{
/* I don't do anything! */
}
static PurpleCertificateVerifier x509_singleuse = {
"x509", /* Scheme name */
"singleuse", /* Verifier name */
x509_singleuse_start_verify, /* start_verification function */
x509_singleuse_destroy_request, /* Request cleanup operation */
NULL,
NULL,
NULL,
NULL
};
/***** X.509 Certificate Authority pool, keyed by Distinguished Name *****/
/* This is implemented in what may be the most inefficient and bugprone way
possible; however, future optimizations should not be difficult. */
static PurpleCertificatePool x509_ca;
/** Holds a key-value pair for quickish certificate lookup */
typedef struct {
gchar *dn;
PurpleCertificate *crt;
} x509_ca_element;
static void
x509_ca_element_free(x509_ca_element *el)
{
if (NULL == el) return;
g_free(el->dn);
purple_certificate_destroy(el->crt);
g_free(el);
}
/** System directory to probe for CA certificates */
/* This is set in the lazy_init function */
static GList *x509_ca_paths = NULL;
/** A list of loaded CAs, populated from the above path whenever the lazy_init
happens. Contains pointers to x509_ca_elements */
static GList *x509_ca_certs = NULL;
/** Used for lazy initialization purposes. */
static gboolean x509_ca_initialized = FALSE;
/** Adds a certificate to the in-memory cache, doing nothing else */
static gboolean
x509_ca_quiet_put_cert(PurpleCertificate *crt)
{
x509_ca_element *el;
/* lazy_init calls this function, so calling lazy_init here is a
Bad Thing */
g_return_val_if_fail(crt, FALSE);
g_return_val_if_fail(crt->scheme, FALSE);
/* Make sure that this is some kind of X.509 certificate */
/* TODO: Perhaps just check crt->scheme->name instead? */
g_return_val_if_fail(crt->scheme == purple_certificate_find_scheme(x509_ca.scheme_name), FALSE);
el = g_new0(x509_ca_element, 1);
el->dn = purple_certificate_get_unique_id(crt);
el->crt = purple_certificate_copy(crt);
x509_ca_certs = g_list_prepend(x509_ca_certs, el);
return TRUE;
}
/* Since the libpurple CertificatePools get registered before plugins are
loaded, an X.509 Scheme is generally not available when x509_ca_init is
called, but x509_ca requires X.509 operations in order to properly load.
To solve this, I present the lazy_init function. It attempts to finish
initialization of the Pool, but it usually fails when it is called from
x509_ca_init. However, this is OK; initialization is then simply deferred
until someone tries to use functions from the pool. */
static gboolean
x509_ca_lazy_init(void)
{
PurpleCertificateScheme *x509;
GDir *certdir;
const gchar *entry;
GPatternSpec *pempat, *crtpat;
GList *iter = NULL;
GSList *crts = NULL;
if (x509_ca_initialized) return TRUE;
/* Check that X.509 is registered */
x509 = purple_certificate_find_scheme(x509_ca.scheme_name);
if ( !x509 ) {
purple_debug_warning("certificate/x509/ca",
"Lazy init failed because an X.509 Scheme "
"is not yet registered. Maybe it will be "
"better later.\n");
return FALSE;
}
/* Use a glob to only read .pem files */
pempat = g_pattern_spec_new("*.pem");
crtpat = g_pattern_spec_new("*.crt");
/* Populate the certificates pool from the search path(s) */
for (iter = x509_ca_paths; iter; iter = iter->next) {
certdir = g_dir_open(iter->data, 0, NULL);
if (!certdir) {
purple_debug_error("certificate/x509/ca", "Couldn't open location '%s'\n", (const char *)iter->data);
continue;
}
while ( (entry = g_dir_read_name(certdir)) ) {
gchar *fullpath;
PurpleCertificate *crt;
if (!g_pattern_match_string(pempat, entry) && !g_pattern_match_string(crtpat, entry)) {
continue;
}
fullpath = g_build_filename(iter->data, entry, NULL);
/* TODO: Respond to a failure in the following? */
crts = purple_certificates_import(x509, fullpath);
while (crts && crts->data) {
crt = crts->data;
if (x509_ca_quiet_put_cert(crt)) {
gchar *name;
name = purple_certificate_get_subject_name(crt);
purple_debug_info("certificate/x509/ca",
"Loaded %s from %s\n",
name ? name : "(unknown)", fullpath);
g_free(name);
} else {
purple_debug_error("certificate/x509/ca",
"Failed to load certificate from %s\n",
fullpath);
}
purple_certificate_destroy(crt);
crts = g_slist_delete_link(crts, crts);
}
g_free(fullpath);
}
g_dir_close(certdir);
}
g_pattern_spec_free(pempat);
g_pattern_spec_free(crtpat);
purple_debug_info("certificate/x509/ca",
"Lazy init completed.\n");
x509_ca_initialized = TRUE;
return TRUE;
}
static gboolean
x509_ca_init(void)
{
/* Attempt to point at the appropriate system path */
if (NULL == x509_ca_paths) {
#ifdef _WIN32
x509_ca_paths = g_list_append(NULL, g_build_filename(DATADIR,
"ca-certs", NULL));
#else
# ifdef SSL_CERTIFICATES_DIR
x509_ca_paths = g_list_append(NULL, g_strdup(SSL_CERTIFICATES_DIR));
# endif
x509_ca_paths = g_list_append(x509_ca_paths,
g_build_filename(DATADIR, "purple", "ca-certs", NULL));
#endif
}
/* Attempt to initialize now, but if it doesn't work, that's OK;
it will get done later */
if ( ! x509_ca_lazy_init()) {
purple_debug_info("certificate/x509/ca",
"Init failed, probably because a "
"dependency is not yet registered. "
"It has been deferred to later.\n");
}
return TRUE;
}
static void
x509_ca_uninit(void)
{
GList *l;
for (l = x509_ca_certs; l; l = l->next) {
x509_ca_element *el = l->data;
x509_ca_element_free(el);
}
g_list_free(x509_ca_certs);
x509_ca_certs = NULL;
x509_ca_initialized = FALSE;
g_list_foreach(x509_ca_paths, (GFunc)g_free, NULL);
g_list_free(x509_ca_paths);
x509_ca_paths = NULL;
}
/** Look up a ca_element by dn */
static x509_ca_element *
x509_ca_locate_cert(GList *lst, const gchar *dn)
{
GList *cur;
for (cur = lst; cur; cur = cur->next) {
x509_ca_element *el = cur->data;
if (purple_strequal(dn, el->dn)) {
return el;
}
}
return NULL;
}
static GSList *
x509_ca_locate_certs(GList *lst, const gchar *dn)
{
GList *cur;
GSList *crts = NULL;
for (cur = lst; cur; cur = cur->next) {
x509_ca_element *el = cur->data;
if (purple_strequal(dn, el->dn)) {
crts = g_slist_prepend(crts, el);
}
}
return crts;
}
static gboolean
x509_ca_cert_in_pool(const gchar *id)
{
g_return_val_if_fail(x509_ca_lazy_init(), FALSE);
g_return_val_if_fail(id, FALSE);
if (x509_ca_locate_cert(x509_ca_certs, id) != NULL) {
return TRUE;
} else {
return FALSE;
}
return FALSE;
}
static PurpleCertificate *
x509_ca_get_cert(const gchar *id)
{
PurpleCertificate *crt = NULL;
x509_ca_element *el;
g_return_val_if_fail(x509_ca_lazy_init(), NULL);
g_return_val_if_fail(id, NULL);
/* Search the memory-cached pool */
el = x509_ca_locate_cert(x509_ca_certs, id);
if (el != NULL) {
/* Make a copy of the memcached one for the function caller
to play with */
crt = purple_certificate_copy(el->crt);
} else {
crt = NULL;
}