Fix printf formats for size_t and goffset arguments.
[claws.git] / src / common / ssl_certificate.c
index b48d4d4b9dad2170495ffbfc3a13423021ac0fe7..ebe6168187356bf3b6669aedcc8045546b134864 100644 (file)
@@ -1,7 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> 
- * and the Claws Mail team
+ * Copyright (C) 1999-2016 Colin Leroy and the Claws Mail team
  *
  * 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
@@ -15,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -48,6 +46,7 @@
 #include "socket.h"
 #include "hooks.h"
 #include "defs.h"
+#include "file-utils.h"
 
 static GHashTable *warned_expired = NULL;
 
@@ -55,14 +54,22 @@ gboolean prefs_common_unsafe_ssl_certs(void);
 
 static gchar *get_certificate_path(const gchar *host, const gchar *port, const gchar *fp)
 {
-       if (fp != NULL && prefs_common_unsafe_ssl_certs())
-               return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
-                         "certs", G_DIR_SEPARATOR_S,
-                         host, ".", port, ".", fp, ".cert", NULL);
-       else 
-               return g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
-                         "certs", G_DIR_SEPARATOR_S,
-                         host, ".", port, ".cert", NULL);
+       gchar *ret;
+       gchar *filename;
+
+       if (fp != NULL && prefs_common_unsafe_ssl_certs()) {
+               filename = g_strconcat(host, ".", port, ".", fp, ".cert", NULL);
+       } else {
+               filename = g_strconcat(host, ".", port, ".cert", NULL);
+       }
+       subst_for_filename(filename);
+
+       ret = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+                 "certs", G_DIR_SEPARATOR_S,
+                 filename, NULL);
+
+       g_free(filename);
+       return ret;
 }
 
 static gchar *get_certificate_chain_path(const gchar *host, const gchar *port, const gchar *fp)
@@ -107,13 +114,13 @@ static gnutls_x509_crt_t x509_crt_copy(gnutls_x509_crt_t src)
     size = 0;
     
     if (gnutls_x509_crt_init(&dest) != 0) {
-       g_warning("couldn't gnutls_x509_crt_init\n");
+       g_warning("couldn't gnutls_x509_crt_init");
         return NULL;
     }
 
     if (gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, NULL, &size) 
         != GNUTLS_E_SHORT_MEMORY_BUFFER) {
-       g_warning("couldn't gnutls_x509_crt_export to get size\n");
+       g_warning("couldn't gnutls_x509_crt_export to get size");
         gnutls_x509_crt_deinit(dest);
         return NULL;
     }
@@ -125,12 +132,12 @@ static gnutls_x509_crt_t x509_crt_copy(gnutls_x509_crt_t src)
         tmp.size = size;
         ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
        if (ret) {
-               g_warning("couldn't gnutls_x509_crt_import for real (%d %s)\n", ret, gnutls_strerror(ret));
+               g_warning("couldn't gnutls_x509_crt_import for real (%d %s)", ret, gnutls_strerror(ret));
                gnutls_x509_crt_deinit(dest);
                dest = NULL;
        }
     } else {
-       g_warning("couldn't gnutls_x509_crt_export for real (%d %s)\n", ret, gnutls_strerror(ret));
+       g_warning("couldn't gnutls_x509_crt_export for real (%d %s)", ret, gnutls_strerror(ret));
         gnutls_x509_crt_deinit(dest);
         dest = NULL;
     }
@@ -156,7 +163,7 @@ static SSLCertificate *ssl_certificate_new(gnutls_x509_crt_t x509_cert, const gc
        cert->port = port;
        
        /* fingerprint */
-       n = 128;
+       n = sizeof(md);
        gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
        cert->fingerprint = readable_fingerprint(md, (int)n);
        return cert;
@@ -169,13 +176,16 @@ static void gnutls_export_X509_fp(FILE *fp, gnutls_x509_crt_t x509_cert, gnutls_
        int r;
        
        if ((r = gnutls_x509_crt_export(x509_cert, format, output, &cert_size)) < 0) {
-               g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
+               g_warning("couldn't export cert %s (%"G_GSIZE_FORMAT")", gnutls_strerror(r), cert_size);
                return;
        }
-
+#ifdef G_OS_WIN32
+       debug_print("writing %Iu bytes\n",cert_size);
+#else
        debug_print("writing %zd bytes\n",cert_size);
-       if (fwrite(&output, 1, cert_size, fp) < cert_size) {
-               g_warning("failed to write cert: %d %s\n", errno, strerror(errno));
+#endif
+       if (claws_fwrite(&output, 1, cert_size, fp) < cert_size) {
+               g_warning("failed to write cert: %d %s", errno, g_strerror(errno));
        }
 }
 
@@ -190,7 +200,7 @@ size_t gnutls_i2d_X509(gnutls_x509_crt_t x509_cert, unsigned char **output)
        *output = malloc(cert_size);
 
        if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, *output, &cert_size)) < 0) {
-               g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
+               g_warning("couldn't export cert %s (%"G_GSIZE_FORMAT")", gnutls_strerror(r), cert_size);
                free(*output);
                *output = NULL;
                return 0;
@@ -209,7 +219,7 @@ size_t gnutls_i2d_PrivateKey(gnutls_x509_privkey_t pkey, unsigned char **output)
        *output = malloc(key_size);
 
        if ((r = gnutls_x509_privkey_export(pkey, GNUTLS_X509_FMT_DER, *output, &key_size)) < 0) {
-               g_warning("couldn't export key %s (%zd)\n", gnutls_strerror(r), key_size);
+               g_warning("couldn't export key %s (%"G_GSIZE_FORMAT")", gnutls_strerror(r), key_size);
                free(*output);
                *output = NULL;
                return 0;
@@ -242,8 +252,8 @@ static int gnutls_import_X509_list_fp(FILE *fp, gnutls_x509_crt_fmt_t format,
        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");
+       if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
+               perror("claws_fread");
                free(tmp.data);
                free(crt_list);
                return -EIO;
@@ -301,8 +311,8 @@ static gnutls_x509_privkey_t gnutls_import_key_fp(FILE *fp, gnutls_x509_crt_fmt_
        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");
+       if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
+               perror("claws_fread");
                free(tmp.data);
                return NULL;
        }
@@ -326,15 +336,15 @@ static gnutls_pkcs12_t gnutls_import_PKCS12_fp(FILE *fp, gnutls_x509_crt_fmt_t f
        int r;
        if (fstat(fileno(fp), &s) < 0) {
                log_error(LOG_PROTOCOL, _("Cannot stat P12 certificate file (%s)\n"),
-                                 strerror(errno));
+                                 g_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) {
+       if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
                log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
-                                 strerror(errno));
+                                 g_strerror(errno));
                free(tmp.data);
                return NULL;
        }
@@ -368,7 +378,7 @@ static void ssl_certificate_save (SSLCertificate *cert)
        file = get_certificate_path(cert->host, port, cert->fingerprint);
 
        g_free(port);
-       fp = g_fopen(file, "wb");
+       fp = claws_fopen(file, "wb");
        if (fp == NULL) {
                g_free(file);
                debug_print("Can't save certificate !\n");
@@ -378,7 +388,7 @@ static void ssl_certificate_save (SSLCertificate *cert)
        gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
 
        g_free(file);
-       fclose(fp);
+       claws_safe_fclose(fp);
 
 }
 
@@ -422,14 +432,14 @@ SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gch
        
        if (fingerprint != NULL) {
                file = get_certificate_path(host, buf, fingerprint);
-               fp = g_fopen(file, "rb");
+               fp = claws_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(host, buf, NULL);
-               fp = g_fopen(file, "rb");
+               fp = claws_fopen(file, "rb");
 
                if (fp) {
                        debug_print("got %s\n", file);
@@ -450,7 +460,7 @@ SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gch
                gnutls_x509_crt_deinit(tmp_x509);
        }
 
-       fclose(fp);
+       claws_fclose(fp);
        g_free(file);
        
        if (must_rename) {
@@ -478,38 +488,38 @@ static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate
 
        if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_a)) 
            != GNUTLS_E_SHORT_MEMORY_BUFFER) {
-               g_warning("couldn't gnutls_x509_crt_export to get size a %s\n", gnutls_strerror(r));
+               g_warning("couldn't gnutls_x509_crt_export to get size a %s", gnutls_strerror(r));
                return FALSE;
        }
 
        if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, NULL, &cert_size_b))
            != GNUTLS_E_SHORT_MEMORY_BUFFER) {
-               g_warning("couldn't gnutls_x509_crt_export to get size b %s\n", gnutls_strerror(r));
+               g_warning("couldn't gnutls_x509_crt_export to get size b %s", gnutls_strerror(r));
                return FALSE;
        }
 
        output_a = g_malloc(cert_size_a);
        output_b = g_malloc(cert_size_b);
        if ((r = gnutls_x509_crt_export(cert_a->x509_cert, GNUTLS_X509_FMT_DER, output_a, &cert_size_a)) < 0) {
-               g_warning("couldn't gnutls_x509_crt_export a %s\n", gnutls_strerror(r));
+               g_warning("couldn't gnutls_x509_crt_export a %s", gnutls_strerror(r));
                g_free(output_a);
                g_free(output_b);
                return FALSE;
        }
        if ((r = gnutls_x509_crt_export(cert_b->x509_cert, GNUTLS_X509_FMT_DER, output_b, &cert_size_b)) < 0) {
-               g_warning("couldn't gnutls_x509_crt_export b %s\n", gnutls_strerror(r));
+               g_warning("couldn't gnutls_x509_crt_export b %s", gnutls_strerror(r));
                g_free(output_a);
                g_free(output_b);
                return FALSE;
        }
        if (cert_size_a != cert_size_b) {
-               g_warning("size differ %zd %zd\n", cert_size_a, cert_size_b);
+               g_warning("size differ %"G_GSIZE_FORMAT" %"G_GSIZE_FORMAT, cert_size_a, cert_size_b);
                g_free(output_a);
                g_free(output_b);
                return FALSE;
        }
        if (memcmp(output_a, output_b, cert_size_a)) {
-               g_warning("contents differ\n");
+               g_warning("contents differ");
                g_free(output_a);
                g_free(output_b);
                return FALSE;
@@ -532,16 +542,19 @@ static guint check_cert(SSLCertificate *cert)
        FILE *fp;
 
        if (claws_ssl_get_cert_file())
-               fp = g_fopen(claws_ssl_get_cert_file(), "r");
+               fp = claws_fopen(claws_ssl_get_cert_file(), "r");
        else
                return (guint)-1;
 
+       if (fp == NULL)
+               return (guint)-1;
+
        if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max_ca)) < 0) {
                debug_print("CA import failed: %s\n", gnutls_strerror(r));
-               fclose(fp);
+               claws_fclose(fp);
                return (guint)-1;
        }
-       fclose(fp);
+       claws_fclose(fp);
        fp = NULL;
        
        buf = g_strdup_printf("%d", cert->port);
@@ -549,24 +562,29 @@ static guint check_cert(SSLCertificate *cert)
        g_free(buf);
        if (is_file_exist(chain_file)) {
                unsigned char md[128];
-               size_t n;
+               size_t n = 128;
                char *fingerprint;
 
-               fp = g_fopen(chain_file, "r");
+               fp = claws_fopen(chain_file, "r");
+               if (fp == NULL) {
+                       debug_print("claws_fopen %s failed: %s\n", chain_file, g_strerror(errno));
+                       g_free(chain_file);
+                       return (guint)-1;
+               }
                if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &chain, &max_certs)) < 0) {
                        debug_print("chain import failed: %s\n", gnutls_strerror(r));
-                       fclose(fp);
+                       claws_fclose(fp);
                        g_free(chain_file);
                        return (guint)-1;
                }
                g_free(chain_file);
-               fclose(fp);
+               claws_fclose(fp);
                fp = NULL;
 
                gnutls_x509_crt_get_fingerprint(chain[0], GNUTLS_DIG_MD5, md, &n);
                fingerprint = readable_fingerprint(md, n);
                if (!fingerprint || strcmp(fingerprint, cert->fingerprint)) {
-                       debug_print("Saved chain fingerprint does not match current : %s / %s",
+                       debug_print("saved chain fingerprint does not match current : %s / %s\n",
                                cert->fingerprint, fingerprint);
                                
                        return (guint)-1;
@@ -603,6 +621,17 @@ static guint check_cert(SSLCertificate *cert)
 
 }
 
+static gboolean ssl_certificate_is_valid(SSLCertificate *cert, guint status)
+{
+       gchar *str_status = ssl_certificate_check_signer(cert, status);
+
+       if (str_status != NULL) {
+               g_free(str_status);
+               return FALSE;
+       }
+       return ssl_certificate_check_subject_cn(cert);
+}
+
 char *ssl_certificate_check_signer (SSLCertificate *cert, guint status) 
 {
        gnutls_x509_crt_t x509_cert = cert ? cert->x509_cert : NULL;
@@ -640,18 +669,19 @@ static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const
                size_t n;
                unsigned char md[128];  
                gnutls_x509_crt_t cert = certs[i];
-               gchar *fingerprint;
 
                if (i == 0) {
+                       n = sizeof(md);
                        gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, md, &n);
-                       fingerprint = readable_fingerprint(md, n);
+                       gchar *fingerprint = readable_fingerprint(md, n);
                        gchar *buf = g_strdup_printf("%d", port);
 
                        file = get_certificate_chain_path(host, buf, fingerprint);
+                       g_free(fingerprint);
 
                        g_free(buf);
 
-                       fp = g_fopen(file, "wb");
+                       fp = claws_fopen(file, "wb");
                        if (fp == NULL) {
                                g_free(file);
                                debug_print("Can't save certificate !\n");
@@ -664,20 +694,23 @@ static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const
 
        }
        if (fp)
-               fclose(fp);
+               claws_safe_fclose(fp);
 }
 
-gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, const gchar *host, gushort port)
+gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, 
+                               const gchar *host, gushort port,
+                               gboolean accept_if_valid)
 {
        SSLCertificate *current_cert = NULL;
        SSLCertificate *known_cert;
        SSLCertHookData cert_hook_data;
        gchar *fingerprint;
        size_t n;
-       unsigned char md[128];  
+       unsigned char md[128];
+       gboolean valid = FALSE;
 
        current_cert = ssl_certificate_new(x509_cert, host, port);
-       
+
        if (current_cert == NULL) {
                debug_print("Buggy certificate !\n");
                return FALSE;
@@ -685,7 +718,7 @@ gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, const
 
        current_cert->status = status;
        /* fingerprint */
-       n = 128;
+       n = sizeof(md);
        gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
        fingerprint = readable_fingerprint(md, n);
 
@@ -693,14 +726,25 @@ gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, const
 
        g_free(fingerprint);
 
+       if (accept_if_valid)
+               valid = ssl_certificate_is_valid(current_cert, status);
+       else
+               valid = FALSE; /* Force check */
+
        if (known_cert == NULL) {
+               if (valid) {
+                       ssl_certificate_save(current_cert);
+                       ssl_certificate_destroy(current_cert);
+                       return TRUE;
+               }
+
                cert_hook_data.cert = current_cert;
                cert_hook_data.old_cert = NULL;
                cert_hook_data.expired = FALSE;
                cert_hook_data.accept = FALSE;
-               
+
                hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
-               
+
                if (!cert_hook_data.accept) {
                        ssl_certificate_destroy(current_cert);
                        return FALSE;
@@ -710,11 +754,18 @@ gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, const
                        return TRUE;
                }
        } else if (!ssl_certificate_compare (current_cert, known_cert)) {
+               if (valid) {
+                       ssl_certificate_save(current_cert);
+                       ssl_certificate_destroy(current_cert);
+                       ssl_certificate_destroy(known_cert);
+                       return TRUE;
+               }
+
                cert_hook_data.cert = current_cert;
                cert_hook_data.old_cert = known_cert;
                cert_hook_data.expired = FALSE;
                cert_hook_data.accept = FALSE;
-               
+
                hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
 
                if (!cert_hook_data.accept) {
@@ -729,22 +780,22 @@ gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, const
                }
        } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
                gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
-               
+
                if (warned_expired == NULL)
                        warned_expired = g_hash_table_new(g_str_hash, g_str_equal);
-               
+
                if (g_hash_table_lookup(warned_expired, tmp)) {
                        g_free(tmp);
                        ssl_certificate_destroy(current_cert);
                        ssl_certificate_destroy(known_cert);
                        return TRUE;
                }
-                       
+
                cert_hook_data.cert = current_cert;
                cert_hook_data.old_cert = NULL;
                cert_hook_data.expired = TRUE;
                cert_hook_data.accept = FALSE;
-               
+
                hooks_invoke(SSLCERT_ASK_HOOKLIST, &cert_hook_data);
 
                if (!cert_hook_data.accept) {
@@ -765,7 +816,9 @@ gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, const
        return TRUE;
 }
 
-gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len, const gchar *host, gushort port)
+gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len,
+                                    const gchar *host, gushort port,
+                                    gboolean accept_if_valid)
 {
        int ncas = 0;
        gnutls_x509_crt_t *cas = NULL;
@@ -774,16 +827,16 @@ gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len, c
        gint status;
 
        if (claws_ssl_get_cert_file()) {
-               FILE *fp = g_fopen(claws_ssl_get_cert_file(), "rb");
+               FILE *fp = claws_fopen(claws_ssl_get_cert_file(), "rb");
                int r = -errno;
 
                if (fp) {
                        r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
-                       fclose(fp);
+                       claws_fclose(fp);
                }
 
                if (r < 0)
-                       g_warning("Can't read SSL_CERT_FILE %s: %s\n",
+                       g_warning("Can't read SSL_CERT_FILE '%s': %s",
                                claws_ssl_get_cert_file(), 
                                gnutls_strerror(r));
        } else {
@@ -798,7 +851,8 @@ gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len, c
                              GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT,
                              &status);
 
-       result = ssl_certificate_check(certs[0], status, host, port);
+       result = ssl_certificate_check(certs[0], status, host, port,
+                                       accept_if_valid);
 
        if (result == TRUE) {
                ssl_certificate_save_chain(certs, chain_len, host, port);
@@ -818,18 +872,18 @@ gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
                return NULL;
        
        if (is_file_exist(file)) {
-               FILE *fp = g_fopen(file, "r");
+               FILE *fp = claws_fopen(file, "r");
                if (fp) {
                        x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
-                       fclose(fp);
+                       claws_fclose(fp);
                        return x509;
                } else {
                        log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
-                                 file, strerror(errno));
+                                 file, g_strerror(errno));
                }
        } else {
                log_error(LOG_PROTOCOL, _("Certificate file %s missing (%s)\n"),
-                         file, strerror(errno));
+                         file, g_strerror(errno));
        }
        return NULL;
 }
@@ -841,18 +895,18 @@ gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
                return NULL;
        
        if (is_file_exist(file)) {
-               FILE *fp = g_fopen(file, "r");
+               FILE *fp = claws_fopen(file, "r");
                if (fp) {
                        key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
-                       fclose(fp);
+                       claws_fclose(fp);
                        return key;
                } else {
                        log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
-                       file, strerror(errno));
+                       file, g_strerror(errno));
                }
        } else {
                log_error(LOG_PROTOCOL, _("Key file %s missing (%s)\n"), file,
-                         strerror(errno));
+                         g_strerror(errno));
        }
        return NULL;
 }
@@ -994,20 +1048,20 @@ void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gc
                return;
 
        if (is_file_exist(file)) {
-               FILE *fp = g_fopen(file, "r");
+               FILE *fp = claws_fopen(file, "r");
                if (fp) {
                        p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
-                       fclose(fp);
+                       claws_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));
+                                 file, g_strerror(errno));
                }
        } else {
                log_error(LOG_PROTOCOL, _("P12 Certificate file %s missing (%s)\n"), file,
-                         strerror(errno));
+                         g_strerror(errno));
        }
        if (p12 != NULL) {
                if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
@@ -1031,7 +1085,7 @@ gchar *ssl_certificate_get_subject_cn(SSLCertificate *cert)
 
        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(_("<not in certificate>"));
 
        return g_strdup(subject_cn);
 }