2012-05-16 [paul] 3.8.0cvs41
[claws.git] / src / common / ssl_certificate.c
index ed44cf0d3b9f56e3d06d62cc194ce1eb00797273..4911acb8a94cefc7e459040ac4f155b56e00e032 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> 
+ * Copyright (C) 1999-2011 Colin Leroy <colin@colino.net> 
  * and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
 #  include "config.h"
 #endif
 
-#if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
-#if USE_OPENSSL
-#include <openssl/ssl.h>
-#else
+#ifdef USE_GNUTLS
 #include <gnutls/gnutls.h>
 #include <gnutls/x509.h>
 #include <gnutls/pkcs12.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <string.h>
-#endif
 #include <sys/types.h>
 #include <stdio.h>
 #include <glib.h>
@@ -67,104 +63,6 @@ static gchar *get_certificate_path(const gchar *host, const gchar *port, const g
                          host, ".", port, ".cert", NULL);
 }
 
-#if USE_OPENSSL
-static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup);
-#else
-static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup);
-#endif
-#if USE_OPENSSL
-/* from Courier */
-time_t asn1toTime(ASN1_TIME *asn1Time)
-{
-       struct tm tm;
-       int offset;
-
-       if (asn1Time == NULL || asn1Time->length < 13)
-               return 0;
-
-       memset(&tm, 0, sizeof(tm));
-
-#define N2(n)  ((asn1Time->data[n]-'0')*10 + asn1Time->data[(n)+1]-'0')
-
-#define CPY(f,n) (tm.f=N2(n))
-
-       CPY(tm_year,0);
-
-       if(tm.tm_year < 50)
-               tm.tm_year += 100; /* Sux */
-
-       CPY(tm_mon, 2);
-       --tm.tm_mon;
-       CPY(tm_mday, 4);
-       CPY(tm_hour, 6);
-       CPY(tm_min, 8);
-       CPY(tm_sec, 10);
-
-       offset=0;
-
-       if (asn1Time->data[12] != 'Z')
-       {
-               if (asn1Time->length < 17)
-                       return 0;
-
-               offset=N2(13)*3600+N2(15)*60;
-
-               if (asn1Time->data[12] == '-')
-                       offset= -offset;
-       }
-
-#undef N2
-#undef CPY
-
-       return mktime(&tm)-offset;
-}
-#endif
-
-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;
@@ -229,52 +127,37 @@ static gnutls_x509_crt x509_crt_copy(gnutls_x509_crt src)
 }
 #endif
 
-#if USE_OPENSSL
-static SSLCertificate *ssl_certificate_new_lookup(X509 *x509_cert, gchar *host, gushort port, gboolean lookup)
-#else
-static SSLCertificate *ssl_certificate_new_lookup(gnutls_x509_crt x509_cert, gchar *host, gushort port, gboolean lookup)
-#endif
+static SSLCertificate *ssl_certificate_new(gnutls_x509_crt x509_cert, const gchar *host, gushort port)
 {
        SSLCertificate *cert = g_new0(SSLCertificate, 1);
-       unsigned int n;
+       size_t n;
        unsigned char md[128];  
 
        if (host == NULL || x509_cert == NULL) {
                ssl_certificate_destroy(cert);
                return NULL;
        }
-#if USE_OPENSSL
-       cert->x509_cert = X509_dup(x509_cert);
-#else
        cert->x509_cert = x509_crt_copy(x509_cert);
        cert->status = (guint)-1;
-#endif
-       if (lookup)
-               cert->host = get_fqdn(host);
-       else
-               cert->host = g_strdup(host);
+       cert->host = g_strdup(host);
        cert->port = port;
        
        /* fingerprint */
-#if USE_OPENSSL
-       X509_digest(cert->x509_cert, EVP_md5(), md, &n);
-       cert->fingerprint = readable_fingerprint(md, (int)n);
-#else
+       n = 128;
        gnutls_x509_crt_get_fingerprint(cert->x509_cert, GNUTLS_DIG_MD5, md, &n);
        cert->fingerprint = readable_fingerprint(md, (int)n);
-#endif
        return cert;
 }
 
 #ifdef USE_GNUTLS
-static void i2d_X509_fp(FILE *fp, gnutls_x509_crt x509_cert)
+static void gnutls_i2d_X509_fp(FILE *fp, gnutls_x509_crt x509_cert)
 {
        char output[10*1024];
        size_t cert_size = 10*1024;
        int r;
        
        if ((r = gnutls_x509_crt_export(x509_cert, GNUTLS_X509_FMT_DER, output, &cert_size)) < 0) {
-               g_warning("couldn't export cert %s (%d)\n", gnutls_strerror(r), cert_size);
+               g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
                return;
        }
        debug_print("writing %zd bytes\n",cert_size);
@@ -283,7 +166,7 @@ static void i2d_X509_fp(FILE *fp, gnutls_x509_crt x509_cert)
        }
 }
 
-size_t i2d_X509(gnutls_x509_crt x509_cert, unsigned char **output)
+size_t gnutls_i2d_X509(gnutls_x509_crt x509_cert, unsigned char **output)
 {
        size_t cert_size = 10*1024;
        int r;
@@ -294,7 +177,7 @@ size_t i2d_X509(gnutls_x509_crt 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 (%d)\n", gnutls_strerror(r), cert_size);
+               g_warning("couldn't export cert %s (%zd)\n", gnutls_strerror(r), cert_size);
                free(*output);
                *output = NULL;
                return 0;
@@ -302,7 +185,7 @@ size_t i2d_X509(gnutls_x509_crt x509_cert, unsigned char **output)
        return cert_size;
 }
 
-size_t i2d_PrivateKey(gnutls_x509_privkey pkey, unsigned char **output)
+size_t gnutls_i2d_PrivateKey(gnutls_x509_privkey pkey, unsigned char **output)
 {
        size_t key_size = 10*1024;
        int r;
@@ -313,7 +196,7 @@ size_t i2d_PrivateKey(gnutls_x509_privkey 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 (%d)\n", gnutls_strerror(r), key_size);
+               g_warning("couldn't export key %s (%zd)\n", gnutls_strerror(r), key_size);
                free(*output);
                *output = NULL;
                return 0;
@@ -321,7 +204,7 @@ size_t i2d_PrivateKey(gnutls_x509_privkey pkey, unsigned char **output)
        return key_size;
 }
 
-static gnutls_x509_crt d2i_X509_fp(FILE *fp, int format)
+static gnutls_x509_crt gnutls_d2i_X509_fp(FILE *fp, int format)
 {
        gnutls_x509_crt cert = NULL;
        gnutls_datum tmp;
@@ -351,7 +234,7 @@ static gnutls_x509_crt d2i_X509_fp(FILE *fp, int format)
        return cert;
 }
 
-static gnutls_x509_privkey d2i_key_fp(FILE *fp, int format)
+static gnutls_x509_privkey gnutls_d2i_key_fp(FILE *fp, int format)
 {
        gnutls_x509_privkey key = NULL;
        gnutls_datum tmp;
@@ -381,7 +264,7 @@ static gnutls_x509_privkey d2i_key_fp(FILE *fp, int format)
        return key;
 }
 
-static gnutls_pkcs12_t d2i_PKCS12_fp(FILE *fp, int format)
+static gnutls_pkcs12_t gnutls_d2i_PKCS12_fp(FILE *fp, int format)
 {
        gnutls_pkcs12_t p12 = NULL;
        gnutls_datum tmp;
@@ -436,7 +319,11 @@ static void ssl_certificate_save (SSLCertificate *cert)
                debug_print("Can't save certificate !\n");
                return;
        }
+#ifdef USE_GNUTLS
+       gnutls_i2d_X509_fp(fp, cert->x509_cert);
+#else
        i2d_X509_fp(fp, cert->x509_cert);
+#endif
        g_free(file);
        fclose(fp);
 
@@ -448,11 +335,7 @@ void ssl_certificate_destroy(SSLCertificate *cert)
                return;
 
        if (cert->x509_cert)
-#if USE_OPENSSL
-               X509_free(cert->x509_cert);
-#else
                gnutls_x509_crt_deinit(cert->x509_cert);
-#endif
        g_free(cert->host);
        g_free(cert->fingerprint);
        g_free(cert);
@@ -470,41 +353,26 @@ void ssl_certificate_delete_from_disk(SSLCertificate *cert)
        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;
-#if USE_OPENSSL
-       X509 *tmp_x509;
-#else
        gnutls_x509_crt tmp_x509;
-#endif
        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) {
@@ -516,47 +384,33 @@ SSLCertificate *ssl_certificate_find_lookup (gchar *host, gushort port, const gc
        }
        if (fp == NULL) {
                g_free(file);
-               g_free(fqdn_host);
                g_free(buf);
                return NULL;
        }
        
-       if ((tmp_x509 = d2i_X509_fp(fp, 0)) != NULL) {
-               cert = ssl_certificate_new_lookup(tmp_x509, fqdn_host, port, lookup);
+       if ((tmp_x509 = gnutls_d2i_X509_fp(fp, 0)) != NULL) {
+               cert = ssl_certificate_new(tmp_x509, host, port);
                debug_print("got cert %p\n", cert);
-#if USE_OPENSSL
-               X509_free(tmp_x509);
-#else
                gnutls_x509_crt_deinit(tmp_x509);
-#endif
        }
        fclose(fp);
        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;
 }
 
 static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate *cert_b)
 {
-#ifdef USE_OPENSSL
-       if (cert_a == NULL || cert_b == NULL)
-               return FALSE;
-       else if (!X509_cmp(cert_a->x509_cert, cert_b->x509_cert))
-               return TRUE;
-       else
-               return FALSE;
-#else
        char *output_a;
        char *output_b;
        size_t cert_size_a = 0, cert_size_b = 0;
@@ -577,67 +431,103 @@ static gboolean ssl_certificate_compare (SSLCertificate *cert_a, SSLCertificate
                return FALSE;
        }
 
-       output_a = malloc(cert_size_a);
-       output_b = malloc(cert_size_b);
+       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_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_free(output_a);
+               g_free(output_b);
                return FALSE;
        }
        if (cert_size_a != cert_size_b) {
-               g_warning("size differ %d %d\n", cert_size_a, cert_size_b);
+               g_warning("size differ %zd %zd\n", 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_free(output_a);
+               g_free(output_b);
                return FALSE;
        }
+       g_free(output_a);
+       g_free(output_b);
        
        return TRUE;
-#endif
 }
 
-#if USE_OPENSSL
-char *ssl_certificate_check_signer (X509 *cert) 
+static guint check_cert(gnutls_x509_crt cert)
 {
-       X509_STORE_CTX store_ctx;
-       X509_STORE *store;
-       char *err_msg = NULL;
+       gnutls_x509_crt *ca_list;
+       unsigned int max = 512;
+       unsigned int flags = 0;
+       gnutls_datum tmp;
+       struct stat s;
+       int r, i;
+       unsigned int status;
+       FILE *fp;
 
-       store = X509_STORE_new();
-       if (store == NULL) {
-               g_print("Can't create X509_STORE\n");
-               return NULL;
+       if (claws_ssl_get_cert_file())
+               fp = g_fopen(claws_ssl_get_cert_file(), "r");
+       else
+               return (guint)-1;
+
+       if (fstat(fileno(fp), &s) < 0) {
+               perror("fstat");
+               fclose(fp);
+               return (guint)-1;
        }
-       if (!X509_STORE_set_default_paths(store)) {
-               X509_STORE_free (store);
-               return g_strdup(_("Couldn't load X509 default paths"));
+
+       ca_list=(gnutls_x509_crt_t*)malloc(max*sizeof(gnutls_x509_crt_t));
+       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");
+               free(tmp.data);
+               free(ca_list);
+               fclose(fp);
+               return (guint)-1;
        }
-       
-       X509_STORE_CTX_init (&store_ctx, store, cert, NULL);
-
-       if(!X509_verify_cert (&store_ctx)) {
-               err_msg = g_strdup(X509_verify_cert_error_string(
-                                       X509_STORE_CTX_get_error(&store_ctx)));
-               debug_print("Can't check signer: %s\n", err_msg);
-               X509_STORE_CTX_cleanup (&store_ctx);
-               X509_STORE_free (store);
-               return err_msg;
-                       
+
+       if ((r = gnutls_x509_crt_list_import(ca_list, &max, 
+                       &tmp, GNUTLS_X509_FMT_PEM, flags)) < 0) {
+               debug_print("cert import failed: %s\n", gnutls_strerror(r));
+               free(tmp.data);
+               free(ca_list);
+               fclose(fp);
+               return (guint)-1;
        }
-       X509_STORE_CTX_cleanup (&store_ctx);
-       X509_STORE_free (store);
-       return NULL;
+       free(tmp.data);
+       debug_print("got %d certs in ca_list! %p\n", max, &ca_list);
+       r = gnutls_x509_crt_verify(cert, ca_list, max, flags, &status);
+       fclose(fp);
+
+       for (i = 0; i < max; i++)
+               gnutls_x509_crt_deinit(ca_list[i]);
+       free(ca_list);
+
+       if (r < 0)
+               return (guint)-1;
+       else
+               return status;
+
 }
-#else
+
 char *ssl_certificate_check_signer (gnutls_x509_crt cert, guint status) 
 {
-       if (status == (guint)-1)
-               return g_strdup(_("Uncheckable"));
-
+       if (status == (guint)-1) {
+               status = check_cert(cert);
+               if (status == -1)
+                       return g_strdup(_("Uncheckable"));
+       }
        if (status & GNUTLS_CERT_INVALID) {
                if (gnutls_x509_crt_check_issuer(cert, cert))
                        return g_strdup(_("Self-signed certificate"));
@@ -652,56 +542,32 @@ char *ssl_certificate_check_signer (gnutls_x509_crt cert, guint status)
 
        return NULL;
 }
-#endif
 
-#if USE_OPENSSL
-gboolean ssl_certificate_check (X509 *x509_cert, gchar *fqdn, gchar *host, gushort port)
-#else
-gboolean ssl_certificate_check (gnutls_x509_crt x509_cert, guint status, gchar *fqdn, gchar *host, gushort port)
-#endif
+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;
-       unsigned int n;
+       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;
        }
 
-#if USE_GNUTLS
        current_cert->status = status;
-#endif
        /* fingerprint */
-#if USE_OPENSSL
-       X509_digest(x509_cert, EVP_md5(), md, &n);
-       fingerprint = readable_fingerprint(md, (int)n);
-#else
        n = 128;
        gnutls_x509_crt_get_fingerprint(x509_cert, GNUTLS_DIG_MD5, md, &n);
-       fingerprint = readable_fingerprint(md, (int)n);
-#endif
+       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;
@@ -737,11 +603,7 @@ gboolean ssl_certificate_check (gnutls_x509_crt x509_cert, guint status, gchar *
                        ssl_certificate_destroy(known_cert);
                        return TRUE;
                }
-#if USE_OPENSSL
-       } else if (asn1toTime(X509_get_notAfter(current_cert->x509_cert)) < time(NULL)) {
-#else
        } else if (gnutls_x509_crt_get_expiration_time(current_cert->x509_cert) < time(NULL)) {
-#endif
                gchar *tmp = g_strdup_printf("%s:%d", current_cert->host, current_cert->port);
                
                if (warned_expired == NULL)
@@ -779,85 +641,6 @@ gboolean ssl_certificate_check (gnutls_x509_crt x509_cert, guint status, gchar *
        return TRUE;
 }
 
-#if USE_OPENSSL
-X509 *ssl_certificate_get_x509_from_pem_file(const gchar *file)
-{
-       X509 *x509 = NULL;
-       if (!file)
-               return NULL;
-       if (is_file_exist(file)) {
-               FILE *fp = g_fopen(file, "r");
-               if (fp) {
-                       x509 = PEM_read_X509(fp, NULL, NULL, NULL);
-                       fclose(fp);
-                       return x509;
-               }
-       } else {
-               log_error(LOG_PROTOCOL, "Can not open certificate file %s\n", file);
-       }
-       return NULL;
-}
-
-static int ssl_pkey_password_cb(char *buf, int max_len, int flag, void *pwd)
-{
-       return 0;
-}
-
-EVP_PKEY *ssl_certificate_get_pkey_from_pem_file(const gchar *file)
-{
-       EVP_PKEY *pkey = NULL;
-       if (!file)
-               return NULL;
-       if (is_file_exist(file)) {
-               FILE *fp = g_fopen(file, "r");
-               if (fp) {
-                       pkey = PEM_read_PrivateKey(fp, NULL, ssl_pkey_password_cb, NULL);
-                       fclose(fp);
-                       return pkey;
-               }
-       } else {
-               log_error(LOG_PROTOCOL, "Can not open private key file %s\n", file);
-       }
-       return NULL;
-}
-
-void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gchar *password,
-                       X509 **x509, EVP_PKEY **pkey)
-{
-       PKCS12 *p12 = NULL;
-       *x509 = NULL;
-       *pkey = NULL;
-
-       if (!file)
-               return;
-
-       if (is_file_exist(file)) {
-               FILE *fp = g_fopen(file, "r");
-               if (fp) {
-                       p12 = d2i_PKCS12_fp(fp, NULL);
-                       fclose(fp);
-               }
-       } else {
-               log_error(LOG_PROTOCOL, "Can not open certificate file %s\n", file);
-       }
-       if (p12 != NULL) {
-               if (PKCS12_parse(p12, password, pkey, x509, NULL) == 1) {
-                       /* we got the correct password */
-               } else {
-                       gchar *tmp = NULL;
-                       hooks_invoke(SSL_CERT_GET_PASSWORD, &tmp);
-                       if (PKCS12_parse(p12, tmp, pkey, x509, NULL) == 1) {
-                               debug_print("got p12\n");
-                       } else {
-                               log_error(LOG_PROTOCOL, "%s\n", ERR_error_string(ERR_get_error(),NULL));
-                       }
-               }
-               PKCS12_free(p12);
-       }
-}
-#endif
-
-#ifdef USE_GNUTLS
 gnutls_x509_crt ssl_certificate_get_x509_from_pem_file(const gchar *file)
 {
        gnutls_x509_crt x509 = NULL;
@@ -867,12 +650,12 @@ gnutls_x509_crt ssl_certificate_get_x509_from_pem_file(const gchar *file)
        if (is_file_exist(file)) {
                FILE *fp = g_fopen(file, "r");
                if (fp) {
-                       x509 = d2i_X509_fp(fp, 1);
+                       x509 = gnutls_d2i_X509_fp(fp, 1);
                        fclose(fp);
                        return x509;
                }
        } else {
-               log_error(LOG_PROTOCOL, "Can not open certificate file %s\n", file);
+               log_error(LOG_PROTOCOL, _("Cannot open certificate file %s\n"), file);
        }
        return NULL;
 }
@@ -886,12 +669,12 @@ gnutls_x509_privkey ssl_certificate_get_pkey_from_pem_file(const gchar *file)
        if (is_file_exist(file)) {
                FILE *fp = g_fopen(file, "r");
                if (fp) {
-                       key = d2i_key_fp(fp, 1);
+                       key = gnutls_d2i_key_fp(fp, 1);
                        fclose(fp);
                        return key;
                }
        } else {
-               log_error(LOG_PROTOCOL, "Can not open key file %s\n", file);
+               log_error(LOG_PROTOCOL, _("Cannot open key file %s\n"), file);
        }
        return NULL;
 }
@@ -903,7 +686,7 @@ parse_pkcs12 (gnutls_pkcs12_t p12,
              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;
 
@@ -1035,11 +818,11 @@ void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gc
        if (is_file_exist(file)) {
                FILE *fp = g_fopen(file, "r");
                if (fp) {
-                       p12 = d2i_PKCS12_fp(fp, 0);
+                       p12 = gnutls_d2i_PKCS12_fp(fp, 0);
                        fclose(fp);
                }
        } else {
-               log_error(LOG_PROTOCOL, "Can not open certificate file %s\n", file);
+               log_error(LOG_PROTOCOL, _("Cannot open certificate file %s\n"), file);
        }
        if (p12 != NULL) {
                if ((r = parse_pkcs12(p12, password, pkey, x509)) == 0) {
@@ -1050,5 +833,4 @@ void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gc
                gnutls_pkcs12_deinit(p12);
        }
 }
-#endif
-#endif /* USE_OPENSSL */
+#endif /* USE_GNUTLS */