/*
* Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2011 Colin Leroy <colin@colino.net>
+ * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net>
* and the Claws Mail team
*
* This program is free software; you can redistribute it and/or modify
#ifdef HAVE_CONFIG_H
# include "config.h"
+#include "claws-features.h"
#endif
#ifdef USE_GNUTLS
#include <stdio.h>
#include <glib.h>
#include <glib/gi18n.h>
+#include <errno.h>
#ifdef G_OS_WIN32
# include <winsock2.h>
#else
host, ".", port, ".cert", NULL);
}
-static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup);
-static char * get_fqdn(char *host)
-{
-#ifdef INET6
- gint gai_err;
- struct addrinfo hints, *res;
-#else
- struct hostent *hp;
-#endif
-
- if (host == NULL || strlen(host) == 0)
- return g_strdup("");
-#ifdef INET6
- memset(&hints, 0, sizeof(hints));
- hints.ai_flags = AI_CANONNAME;
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
-
- gai_err = getaddrinfo(host, NULL, &hints, &res);
- if (gai_err != 0) {
- g_warning("getaddrinfo for %s failed: %s\n",
- host, gai_strerror(gai_err));
- return g_strdup(host);
- }
- if (res != NULL) {
- if (res->ai_canonname && strlen(res->ai_canonname)) {
- gchar *fqdn = g_strdup(res->ai_canonname);
- freeaddrinfo(res);
- return fqdn;
- } else {
- freeaddrinfo(res);
- return g_strdup(host);
- }
- } else {
- return g_strdup(host);
- }
-#else
- hp = my_gethostbyname(host);
- if (hp == NULL)
- return g_strdup(host); /*caller should free*/
- else
- return g_strdup(hp->h_name);
-#endif
-}
-
char * readable_fingerprint(unsigned char *src, int len)
{
int i=0;
size_t size;
gnutls_datum tmp;
gnutls_x509_crt dest;
+ size = 0;
if (gnutls_x509_crt_init(&dest) != 0) {
g_warning("couldn't gnutls_x509_crt_init\n");
}
#endif
-static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup)
+static SSLCertificate *ssl_certificate_new(gnutls_x509_crt x509_cert, const gchar *host, gushort port)
{
SSLCertificate *cert = g_new0(SSLCertificate, 1);
size_t n;
}
cert->x509_cert = x509_crt_copy(x509_cert);
cert->status = (guint)-1;
- if (lookup)
- cert->host = get_fqdn(host);
- else
- cert->host = g_strdup(host);
+ cert->host = g_strdup(host);
cert->port = port;
/* fingerprint */
struct stat s;
int r;
if (fstat(fileno(fp), &s) < 0) {
- perror("fstat");
+ log_error(LOG_PROTOCOL, _("Cannot stat P12 certificate file (%s)\n"),
+ strerror(errno));
return NULL;
}
tmp.data = malloc(s.st_size);
memset(tmp.data, 0, s.st_size);
tmp.size = s.st_size;
if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
- perror("fread");
+ log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
+ strerror(errno));
free(tmp.data);
return NULL;
}
gnutls_pkcs12_init(&p12);
if ((r = gnutls_pkcs12_import(p12, &tmp, (format == 0)?GNUTLS_X509_FMT_DER:GNUTLS_X509_FMT_PEM,0)) < 0) {
- g_warning("p12 import failed: %s\n", gnutls_strerror(r));
+ log_error(LOG_PROTOCOL, _("Cannot import P12 certificate file (%s)\n"),
+ gnutls_strerror(r));
gnutls_pkcs12_deinit(p12);
p12 = NULL;
}
g_free(buf);
}
-SSLCertificate *ssl_certificate_find (gchar *host, gushort port, const gchar *fingerprint)
-{
- return ssl_certificate_find_lookup (host, port, fingerprint, TRUE);
-}
-
-SSLCertificate *ssl_certificate_find_lookup (gchar *host, gushort port, const gchar *fingerprint, gboolean lookup)
+SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gchar *fingerprint)
{
gchar *file = NULL;
gchar *buf;
- gchar *fqdn_host;
SSLCertificate *cert = NULL;
gnutls_x509_crt tmp_x509;
FILE *fp = NULL;
gboolean must_rename = FALSE;
- if (lookup)
- fqdn_host = get_fqdn(host);
- else
- fqdn_host = g_strdup(host);
-
buf = g_strdup_printf("%d", port);
if (fingerprint != NULL) {
- file = get_certificate_path(fqdn_host, buf, fingerprint);
+ file = get_certificate_path(host, buf, fingerprint);
fp = g_fopen(file, "rb");
}
if (fp == NULL) {
/* see if we have the old one */
debug_print("didn't get %s\n", file);
g_free(file);
- file = get_certificate_path(fqdn_host, buf, NULL);
+ file = get_certificate_path(host, buf, NULL);
fp = g_fopen(file, "rb");
if (fp) {
}
if (fp == NULL) {
g_free(file);
- g_free(fqdn_host);
g_free(buf);
return NULL;
}
if ((tmp_x509 = gnutls_d2i_X509_fp(fp, 0)) != NULL) {
- cert = ssl_certificate_new_lookup(tmp_x509, fqdn_host, port, lookup);
+ cert = ssl_certificate_new(tmp_x509, host, port);
debug_print("got cert %p\n", cert);
gnutls_x509_crt_deinit(tmp_x509);
}
g_free(file);
if (must_rename) {
- gchar *old = get_certificate_path(fqdn_host, buf, NULL);
- gchar *new = get_certificate_path(fqdn_host, buf, fingerprint);
+ gchar *old = get_certificate_path(host, buf, NULL);
+ gchar *new = get_certificate_path(host, buf, fingerprint);
if (strcmp(old, new))
move_file(old, new, TRUE);
g_free(old);
g_free(new);
}
g_free(buf);
- g_free(fqdn_host);
return cert;
}
return NULL;
}
-gboolean ssl_certificate_check (gnutls_x509_crt x509_cert, guint status, gchar *fqdn, gchar *host, gushort port)
+gboolean ssl_certificate_check (gnutls_x509_crt x509_cert, guint status, const gchar *host, gushort port)
{
SSLCertificate *current_cert = NULL;
SSLCertificate *known_cert;
SSLCertHookData cert_hook_data;
- gchar *fqdn_host = NULL;
gchar *fingerprint;
size_t n;
unsigned char md[128];
- if (fqdn)
- fqdn_host = g_strdup(fqdn);
- else if (host)
- fqdn_host = get_fqdn(host);
- else {
- g_warning("no host!\n");
- return FALSE;
- }
-
- current_cert = ssl_certificate_new_lookup(x509_cert, fqdn_host, port, FALSE);
+ current_cert = ssl_certificate_new(x509_cert, host, port);
if (current_cert == NULL) {
debug_print("Buggy certificate !\n");
- g_free(fqdn_host);
return FALSE;
}
gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
fingerprint = readable_fingerprint(md, n);
- known_cert = ssl_certificate_find_lookup (fqdn_host, port, fingerprint, FALSE);
+ known_cert = ssl_certificate_find(host, port, fingerprint);
g_free(fingerprint);
- g_free(fqdn_host);
if (known_cert == NULL) {
cert_hook_data.cert = current_cert;
x509 = gnutls_d2i_X509_fp(fp, 1);
fclose(fp);
return x509;
+ } else {
+ log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
+ file, strerror(errno));
}
} else {
- log_error(LOG_PROTOCOL, _("Cannot open certificate file %s\n"), file);
+ log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
+ file, strerror(errno));
}
return NULL;
}
key = gnutls_d2i_key_fp(fp, 1);
fclose(fp);
return key;
+ } else {
+ log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
+ file, strerror(errno));
}
} else {
- log_error(LOG_PROTOCOL, _("Cannot open key file %s\n"), file);
+ log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
+ strerror(errno));
}
return NULL;
}
gnutls_x509_privkey * key,
gnutls_x509_crt_t * cert)
{
- gnutls_pkcs12_bag bag = NULL;
+ gnutls_pkcs12_bag_t bag = NULL;
int index = 0;
int ret;
if (fp) {
p12 = gnutls_d2i_PKCS12_fp(fp, 0);
fclose(fp);
+ if (!p12) {
+ log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
+ }
+ } else {
+ log_error(LOG_PROTOCOL, _("Cannot open P12 certificate file %s (%s)\n"),
+ file, strerror(errno));
}
} else {
- log_error(LOG_PROTOCOL, _("Cannot open certificate file %s\n"), file);
+ log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
+ strerror(errno));
}
if (p12 != NULL) {
if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
gnutls_pkcs12_deinit(p12);
}
}
+
+gboolean ssl_certificate_check_subject_cn(SSLCertificate *cert)
+{
+ return gnutls_x509_crt_check_hostname(cert->x509_cert, cert->host) != 0;
+}
+
+gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
+{
+ gchar subject_cn[BUFFSIZE];
+ size_t n = BUFFSIZE;
+
+ if(gnutls_x509_crt_get_dn_by_oid(cert->x509_cert,
+ GNUTLS_OID_X520_COMMON_NAME, 0, 0, subject_cn, &n))
+ strncpy(subject_cn, _("<not in certificate>"), BUFFSIZE);
+
+ return g_strdup(subject_cn);
+}
+
#endif /* USE_GNUTLS */