2007-10-18 [colin] 3.0.2cvs89
[claws.git] / src / common / ssl_certificate.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> 
4  * and the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
26 #if USE_OPENSSL
27 #include <openssl/ssl.h>
28 #else
29 #include <gnutls/gnutls.h>
30 #include <gnutls/x509.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <string.h>
36 #endif
37 #include <glib.h>
38 #include <glib/gi18n.h>
39
40 #include "ssl_certificate.h"
41 #include "utils.h"
42 #include "log.h"
43 #include "socket.h"
44 #include "hooks.h"
45 #include "defs.h"
46
47 static GHashTable *warned_expired = NULL;
48
49 gboolean prefs_common_unsafe_ssl_certs(void);
50
51 static gchar *get_certificate_path(const gchar *host, const gchar *port, const gchar *fp)
52 {
53         if (fp != NULL && prefs_common_unsafe_ssl_certs())
54                 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
55                           "certs", G_DIR_SEPARATOR_S,
56                           host, ".", port, ".", fp, ".cert", NULL);
57         else 
58                 return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
59                           "certs", G_DIR_SEPARATOR_S,
60                           host, ".", port, ".cert", NULL);
61 }
62
63 #if USE_OPENSSL
64 static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup);
65 #else
66 static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup);
67 #endif
68 #if USE_OPENSSL
69 /* from Courier */
70 time_t asn1toTime(ASN1_TIME *asn1Time)
71 {
72         struct tm tm;
73         int offset;
74
75         if (asn1Time == NULL || asn1Time->length < 13)
76                 return 0;
77
78         memset(&tm, 0, sizeof(tm));
79
80 #define N2(n)   ((asn1Time->data[n]-'0')*10 + asn1Time->data[(n)+1]-'0')
81
82 #define CPY(f,n) (tm.f=N2(n))
83
84         CPY(tm_year,0);
85
86         if(tm.tm_year < 50)
87                 tm.tm_year += 100; /* Sux */
88
89         CPY(tm_mon, 2);
90         --tm.tm_mon;
91         CPY(tm_mday, 4);
92         CPY(tm_hour, 6);
93         CPY(tm_min, 8);
94         CPY(tm_sec, 10);
95
96         offset=0;
97
98         if (asn1Time->data[12] != 'Z')
99         {
100                 if (asn1Time->length < 17)
101                         return 0;
102
103                 offset=N2(13)*3600+N2(15)*60;
104
105                 if (asn1Time->data[12] == '-')
106                         offset= -offset;
107         }
108
109 #undef N2
110 #undef CPY
111
112         return mktime(&tm)-offset;
113 }
114 #endif
115
116 static char * get_fqdn(char *host)
117 {
118         struct hostent *hp;
119
120         if (host == NULL || strlen(host) == 0)
121                 return g_strdup("");
122
123         hp = my_gethostbyname(host);
124         if (hp == NULL)
125                 return g_strdup(host); /*caller should free*/
126         else 
127                 return g_strdup(hp->h_name);
128 }
129
130 char * readable_fingerprint(unsigned char *src, int len) 
131 {
132         int i=0;
133         char * ret;
134         
135         if (src == NULL)
136                 return NULL;
137         ret = g_strdup("");
138         while (i < len) {
139                 char *tmp2;
140                 if(i>0)
141                         tmp2 = g_strdup_printf("%s:%02X", ret, src[i]);
142                 else
143                         tmp2 = g_strdup_printf("%02X", src[i]);
144                 g_free(ret);
145                 ret = g_strdup(tmp2);
146                 g_free(tmp2);
147                 i++;
148         }
149         return ret;
150 }
151
152 #if USE_GNUTLS
153 static gnutls_x509_crt x509_crt_copy(gnutls_x509_crt src)
154 {
155     int ret;
156     size_t size;
157     gnutls_datum tmp;
158     gnutls_x509_crt dest;
159     
160     if (gnutls_x509_crt_init(&dest) != 0) {
161         g_warning("couldn't gnutls_x509_crt_init\n");
162         return NULL;
163     }
164
165     if (gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, NULL, &size) 
166         != GNUTLS_E_SHORT_MEMORY_BUFFER) {
167         g_warning("couldn't gnutls_x509_crt_export to get size\n");
168         gnutls_x509_crt_deinit(dest);
169         return NULL;
170     }
171
172     tmp.data = malloc(size);
173     memset(tmp.data, 0, size);
174     ret = gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, tmp.data, &size);
175     if (ret == 0) {
176         tmp.size = size;
177         ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
178         if (ret) {
179                 g_warning("couldn't gnutls_x509_crt_import for real (%d %s)\n", ret, gnutls_strerror(ret));
180                 gnutls_x509_crt_deinit(dest);
181                 dest = NULL;
182         }
183     } else {
184         g_warning("couldn't gnutls_x509_crt_export for real (%d %s)\n", ret, gnutls_strerror(ret));
185         gnutls_x509_crt_deinit(dest);
186         dest = NULL;
187     }
188
189     free(tmp.data);
190     return dest;
191 }
192 #endif
193
194 #if USE_OPENSSL
195 static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup)
196 #else
197 static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup)
198 #endif
199 {
200         SSLCertificate *cert = g_new0(SSLCertificate, 1);
201         unsigned int n;
202         unsigned char md[128];  
203
204         if (host == NULL || x509_cert == NULL) {
205                 ssl_certificate_destroy(cert);
206                 return NULL;
207         }
208 #if USE_OPENSSL
209         cert->x509_cert = X509_dup(x509_cert);
210 #else
211         cert->x509_cert = x509_crt_copy(x509_cert);
212         cert->status = (guint)-1;
213 #endif
214         if (lookup)
215                 cert->host = get_fqdn(host);
216         else
217                 cert->host = g_strdup(host);
218         cert->port = port;
219         
220         /* fingerprint */
221 #if USE_OPENSSL
222         X509_digest(cert->x509_cert, EVP_md5(), md, &n);
223         cert->fingerprint = readable_fingerprint(md, (int)n);
224 #else
225         gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
226         cert->fingerprint = readable_fingerprint(md, (int)n);
227 #endif
228         return cert;
229 }
230
231 #ifdef USE_GNUTLS
232 static void i2d_X509_fp(FILE *fp, gnutls_x509_crt x509_cert)
233 {
234         char output[10*1024];
235         size_t cert_size = 10*1024;
236         int r;
237         
238         if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, output, &cert_size)) < 0) {
239                 g_warning("couldn't export cert %s (%d)\n", gnutls_strerror(r), cert_size);
240                 return;
241         }
242         debug_print("writing %zd bytes\n",cert_size);
243         if (fwrite(&output, 1, cert_size, fp) < cert_size) {
244                 g_warning("failed to write cert\n");
245         }
246 }
247 static gnutls_x509_crt d2i_X509_fp(FILE *fp, int unused)
248 {
249         gnutls_x509_crt cert = NULL;
250         gnutls_datum tmp;
251         struct stat s;
252         int r;
253         if (fstat(fileno(fp), &s) < 0) {
254                 perror("fstat");
255                 return NULL;
256         }
257         tmp.data = malloc(s.st_size);
258         memset(tmp.data, 0, s.st_size);
259         tmp.size = s.st_size;
260         if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
261                 perror("fread");
262                 return NULL;
263         }
264
265         gnutls_x509_crt_init(&cert);
266         if ((r = gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER)) < 0) {
267                 g_warning("import failed: %s\n", gnutls_strerror(r));
268                 gnutls_x509_crt_deinit(cert);
269                 cert = NULL;
270         }
271         debug_print("got cert! %p\n", cert);
272         return cert;
273 }
274 #endif
275
276 static void ssl_certificate_save (SSLCertificate *cert)
277 {
278         gchar *file, *port;
279         FILE *fp;
280
281         file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
282                           "certs", G_DIR_SEPARATOR_S, NULL);
283         
284         if (!is_dir_exist(file))
285                 make_dir_hier(file);
286         g_free(file);
287
288         port = g_strdup_printf("%d", cert->port);
289         file = get_certificate_path(cert->host, port, cert->fingerprint);
290
291         g_free(port);
292         fp = g_fopen(file, "wb");
293         if (fp == NULL) {
294                 g_free(file);
295                 debug_print("Can't save certificate !\n");
296                 return;
297         }
298         i2d_X509_fp(fp, cert->x509_cert);
299         g_free(file);
300         fclose(fp);
301
302 }
303
304 void ssl_certificate_destroy(SSLCertificate *cert) 
305 {
306         if (cert == NULL)
307                 return;
308
309         if (cert->x509_cert)
310 #if USE_OPENSSL
311                 X509_free(cert->x509_cert);
312 #else
313                 gnutls_x509_crt_deinit(cert->x509_cert);
314 #endif
315         g_free(cert->host);
316         g_free(cert->fingerprint);
317         g_free(cert);
318         cert = NULL;
319 }
320
321 void ssl_certificate_delete_from_disk(SSLCertificate *cert)
322 {
323         gchar *buf;
324         gchar *file;
325         buf = g_strdup_printf("%d", cert->port);
326         file = get_certificate_path(cert->host, buf, cert->fingerprint);
327         g_unlink (file);
328         g_free(file);
329         g_free(buf);
330 }
331
332 SSLCertificate *ssl_certificate_find (gchar *host, gushort port, const gchar *fingerprint)
333 {
334         return ssl_certificate_find_lookup (host, port, fingerprint, TRUE);
335 }
336
337 SSLCertificate *ssl_certificate_find_lookup (gchar *host, gushort port, const gchar *fingerprint, gboolean lookup)
338 {
339         gchar *file = NULL;
340         gchar *buf;
341         gchar *fqdn_host;
342         SSLCertificate *cert = NULL;
343 #if USE_OPENSSL
344         X509 *tmp_x509;
345 #else
346         gnutls_x509_crt tmp_x509;
347 #endif
348         FILE *fp = NULL;
349         gboolean must_rename = FALSE;
350
351         if (lookup)
352                 fqdn_host = get_fqdn(host);
353         else
354                 fqdn_host = g_strdup(host);
355
356         buf = g_strdup_printf("%d", port);
357         
358         if (fingerprint != NULL) {
359                 file = get_certificate_path(fqdn_host, buf, fingerprint);
360                 fp = g_fopen(file, "rb");
361         }
362         if (fp == NULL) {
363                 /* see if we have the old one */
364                 debug_print("didn't get %s\n", file);
365                 g_free(file);
366                 file = get_certificate_path(fqdn_host, buf, NULL);
367                 fp = g_fopen(file, "rb");
368
369                 if (fp) {
370                         debug_print("got %s\n", file);
371                         must_rename = (fingerprint != NULL);
372                 }
373         } else {
374                 debug_print("got %s first try\n", file);
375         }
376         if (fp == NULL) {
377                 g_free(file);
378                 g_free(fqdn_host);
379                 g_free(buf);
380                 return NULL;
381         }
382         
383         if ((tmp_x509 = d2i_X509_fp(fp, 0)) != NULL) {
384                 cert = ssl_certificate_new_lookup(tmp_x509, fqdn_host, port, lookup);
385                 debug_print("got cert %p\n", cert);
386 #if USE_OPENSSL
387                 X509_free(tmp_x509);
388 #else
389                 gnutls_x509_crt_deinit(tmp_x509);
390 #endif
391         }
392         fclose(fp);
393         g_free(file);
394         
395         if (must_rename) {
396                 gchar *old = get_certificate_path(fqdn_host, buf, NULL);
397                 gchar *new = get_certificate_path(fqdn_host, buf, fingerprint);
398                 if (strcmp(old, new))
399                         move_file(old, new, TRUE);
400                 g_free(old);
401                 g_free(new);
402         }
403         g_free(buf);
404         g_free(fqdn_host);
405
406         return cert;
407 }
408
409 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
410 {
411 #ifdef USE_OPENSSL
412         if (cert_a == NULL || cert_b == NULL)
413                 return FALSE;
414         else if (!X509_cmp(cert_a->x509_cert, cert_b->x509_cert))
415                 return TRUE;
416         else
417                 return FALSE;
418 #else
419         char *output_a;
420         char *output_b;
421         size_t cert_size_a = 0, cert_size_b = 0;
422         int r;
423
424         if (cert_a == NULL || cert_b == NULL)
425                 return FALSE;
426
427         if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a)) 
428             != GNUTLS_E_SHORT_MEMORY_BUFFER) {
429                 g_warning("couldn't gnutls_x509_crt_export to get size a %s\n", gnutls_strerror(r));
430                 return FALSE;
431         }
432
433         if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
434             != GNUTLS_E_SHORT_MEMORY_BUFFER) {
435                 g_warning("couldn't gnutls_x509_crt_export to get size b %s\n", gnutls_strerror(r));
436                 return FALSE;
437         }
438
439         output_a = malloc(cert_size_a);
440         output_b = malloc(cert_size_b);
441         if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
442                 g_warning("couldn't gnutls_x509_crt_export a %s\n", gnutls_strerror(r));
443                 return FALSE;
444         }
445         if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
446                 g_warning("couldn't gnutls_x509_crt_export b %s\n", gnutls_strerror(r));
447                 return FALSE;
448         }
449         if (cert_size_a != cert_size_b) {
450                 g_warning("size differ %d %d\n", cert_size_a, cert_size_b);
451                 return FALSE;
452         }
453         if (memcmp(output_a, output_b, cert_size_a)) {
454                 g_warning("contents differ\n");
455                 return FALSE;
456         }
457         
458         return TRUE;
459 #endif
460 }
461
462 #if USE_OPENSSL
463 char *ssl_certificate_check_signer (X509 *cert) 
464 {
465         X509_STORE_CTX store_ctx;
466         X509_STORE *store;
467         char *err_msg = NULL;
468
469         store = X509_STORE_new();
470         if (store == NULL) {
471                 g_print("Can't create X509_STORE\n");
472                 return NULL;
473         }
474         if (!X509_STORE_set_default_paths(store)) {
475                 X509_STORE_free (store);
476                 return g_strdup(_("Couldn't load X509 default paths"));
477         }
478         
479         X509_STORE_CTX_init (&store_ctx, store, cert, NULL);
480
481         if(!X509_verify_cert (&store_ctx)) {
482                 err_msg = g_strdup(X509_verify_cert_error_string(
483                                         X509_STORE_CTX_get_error(&store_ctx)));
484                 debug_print("Can't check signer: %s\n", err_msg);
485                 X509_STORE_CTX_cleanup (&store_ctx);
486                 X509_STORE_free (store);
487                 return err_msg;
488                         
489         }
490         X509_STORE_CTX_cleanup (&store_ctx);
491         X509_STORE_free (store);
492         return NULL;
493 }
494 #else
495 char *ssl_certificate_check_signer (gnutls_x509_crt cert, guint status) 
496 {
497         if (status == (guint)-1)
498                 return g_strdup(_("Uncheckable"));
499
500         if (status & GNUTLS_CERT_INVALID) {
501                 if (gnutls_x509_crt_check_issuer(cert, cert))
502                         return g_strdup(_("Self-signed certificate"));
503         }
504         if (status & GNUTLS_CERT_REVOKED)
505                 return g_strdup(_("Revoked certificate"));
506         if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
507                 return g_strdup(_("No certificate issuer found"));
508         if (status & GNUTLS_CERT_SIGNER_NOT_CA)
509                 return g_strdup(_("Certificate issuer is not a CA"));
510
511
512         return NULL;
513 }
514 #endif
515
516 #if USE_OPENSSL
517 gboolean ssl_certificate_check (X509 *x509_cert, gchar *fqdn, gchar *host, gushort port)
518 #else
519 gboolean ssl_certificate_check (gnutls_x509_crt x509_cert, guint status, gchar *fqdn, gchar *host, gushort port)
520 #endif
521 {
522         SSLCertificate *current_cert = NULL;
523         SSLCertificate *known_cert;
524         SSLCertHookData cert_hook_data;
525         gchar *fqdn_host = NULL;        
526         gchar *fingerprint;
527         unsigned int n;
528         unsigned char md[128];  
529
530         if (fqdn)
531                 fqdn_host = g_strdup(fqdn);
532         else if (host)
533                 fqdn_host = get_fqdn(host);
534         else {
535                 g_warning("no host!\n");
536                 return FALSE;
537         }
538                 
539         current_cert = ssl_certificate_new_lookup(x509_cert, fqdn_host, port, FALSE);
540         
541         if (current_cert == NULL) {
542                 debug_print("Buggy certificate !\n");
543                 g_free(fqdn_host);
544                 return FALSE;
545         }
546
547 #if USE_GNUTLS
548         current_cert->status = status;
549 #endif
550         /* fingerprint */
551 #if USE_OPENSSL
552         X509_digest(x509_cert, EVP_md5(), md, &n);
553         fingerprint = readable_fingerprint(md, (int)n);
554 #else
555         n = 128;
556         gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
557         fingerprint = readable_fingerprint(md, (int)n);
558 #endif
559
560         known_cert = ssl_certificate_find_lookup (fqdn_host, port, fingerprint, FALSE);
561
562         g_free(fingerprint);
563         g_free(fqdn_host);
564
565         if (known_cert == NULL) {
566                 cert_hook_data.cert = current_cert;
567                 cert_hook_data.old_cert = NULL;
568                 cert_hook_data.expired = FALSE;
569                 cert_hook_data.accept = FALSE;
570                 
571                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
572                 
573                 if (!cert_hook_data.accept) {
574                         ssl_certificate_destroy(current_cert);
575                         return FALSE;
576                 } else {
577                         ssl_certificate_save(current_cert);
578                         ssl_certificate_destroy(current_cert);
579                         return TRUE;
580                 }
581         } else if (!ssl_certificate_compare (current_cert, known_cert)) {
582                 cert_hook_data.cert = current_cert;
583                 cert_hook_data.old_cert = known_cert;
584                 cert_hook_data.expired = FALSE;
585                 cert_hook_data.accept = FALSE;
586                 
587                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
588
589                 if (!cert_hook_data.accept) {
590                         ssl_certificate_destroy(current_cert);
591                         ssl_certificate_destroy(known_cert);
592                         return FALSE;
593                 } else {
594                         ssl_certificate_save(current_cert);
595                         ssl_certificate_destroy(current_cert);
596                         ssl_certificate_destroy(known_cert);
597                         return TRUE;
598                 }
599 #if USE_OPENSSL
600         } else if (asn1toTime(X509_get_notAfter(current_cert->x509_cert)) < time(NULL)) {
601 #else
602         } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
603 #endif
604                 gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
605                 
606                 if (warned_expired == NULL)
607                         warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
608                 
609                 if (g_hash_table_lookup(warned_expired, tmp)) {
610                         g_free(tmp);
611                         ssl_certificate_destroy(current_cert);
612                         ssl_certificate_destroy(known_cert);
613                         return TRUE;
614                 }
615                         
616                 cert_hook_data.cert = current_cert;
617                 cert_hook_data.old_cert = NULL;
618                 cert_hook_data.expired = TRUE;
619                 cert_hook_data.accept = FALSE;
620                 
621                 hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
622
623                 if (!cert_hook_data.accept) {
624                         g_free(tmp);
625                         ssl_certificate_destroy(current_cert);
626                         ssl_certificate_destroy(known_cert);
627                         return FALSE;
628                 } else {
629                         g_hash_table_insert(warned_expired, tmp, GINT_TO_POINTER(1));
630                         ssl_certificate_destroy(current_cert);
631                         ssl_certificate_destroy(known_cert);
632                         return TRUE;
633                 }
634         }
635
636         ssl_certificate_destroy(current_cert);
637         ssl_certificate_destroy(known_cert);
638         return TRUE;
639 }
640
641 #endif /* USE_OPENSSL */