]> source.dussan.org Git - tigervnc.git/commitdiff
[Development] os: add gnutls_x509_crt_print() implementation, older systems
authorAdam Tkac <atkac@redhat.com>
Wed, 9 Feb 2011 14:15:09 +0000 (14:15 +0000)
committerAdam Tkac <atkac@redhat.com>
Wed, 9 Feb 2011 14:15:09 +0000 (14:15 +0000)
don't have it and improve backward compatibility of TLS code.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4277 3789f03b-4d11-0410-bbf8-ca57d06f2519

common/os/CMakeLists.txt
common/os/Makefile.am
common/os/tls.cxx [new file with mode: 0644]
common/os/tls.h [new file with mode: 0644]
common/rfb/CSecurityTLS.cxx
configure.ac

index 77ff363f24c828be9e9d458337a04ff6d909cc35..82f11659abec1006bc28aea2cf4552588d30d6b1 100644 (file)
@@ -3,4 +3,5 @@ include_directories(${CMAKE_SOURCE_DIR}/common)
 add_library(os STATIC
   print.c
   net.c
-  os.cxx)
+  os.cxx
+  tls.cxx)
index d87a0dac80d49f31a69f8363c69e42e04f59838b..ab4f2d7bd3c5c7a36c7160558850411890627c9c 100644 (file)
@@ -1,8 +1,8 @@
 noinst_LTLIBRARIES = libos.la
 
-HDRS = net.h print.h os.h
+HDRS = net.h print.h os.h tls.h
 
-libos_la_SOURCES = $(HDRS) print.c net.c os.cxx
+libos_la_SOURCES = $(HDRS) print.c net.c os.cxx tls.cxx
 
 libos_la_CPPFLAGS = -I$(top_srcdir)/common
 
diff --git a/common/os/tls.cxx b/common/os/tls.cxx
new file mode 100644 (file)
index 0000000..5ef8898
--- /dev/null
@@ -0,0 +1,193 @@
+/* Copyright (C) 2011 TightVNC Team.  All Rights Reserved.
+ *
+ * This 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 software 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 software; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
+ * USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <os/tls.h>
+
+#include <iomanip>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sstream>
+#include <sys/types.h>
+#include <time.h>
+
+using namespace std;
+
+#ifdef HAVE_GNUTLS
+#include <gnutls/gnutls.h>
+#include <gnutls/x509.h>
+
+#ifndef HAVE_GNUTLS_X509_CRT_PRINT
+
+#define UNKNOWN_SUBJECT(err) \
+       do { \
+               ss << "unknown subject (" << gnutls_strerror(err) << "), "; \
+       } while (0)
+
+#define UNKNOWN_ISSUER(err) \
+       do { \
+               ss << "unknown issuer (" << gnutls_strerror(err) << "), "; \
+       } while (0)
+
+
+static void
+hexprint(ostringstream &ss, const char *data, size_t len)
+{
+       size_t j;
+       char tmp[3];
+
+       if (len == 0)
+               ss << "00";
+       else {
+               for (j = 0; j < len; j++) {
+                       snprintf(tmp, sizeof(tmp), "%.2x", (unsigned char) data[j]);
+                       ss << tmp;
+               }
+       }
+}
+
+/* Implementation based on gnutls_x509_crt_print from GNUTLS */
+int
+gnutls_x509_crt_print(gnutls_x509_crt_t cert,
+                     gnutls_certificate_print_formats_t format,
+                     gnutls_datum_t * out)
+{
+       ostringstream ss;
+       
+       int err;
+
+       char *dn;
+       size_t dn_size = 0;
+
+       /* Subject */
+       err = gnutls_x509_crt_get_dn(cert, NULL, &dn_size);
+       if (err != GNUTLS_E_SHORT_MEMORY_BUFFER)
+               UNKNOWN_SUBJECT(err);
+       else {
+               dn = (char *)malloc(dn_size);
+               if (dn == NULL) {
+                       UNKNOWN_SUBJECT(GNUTLS_E_MEMORY_ERROR);
+               } else {
+                       err = gnutls_x509_crt_get_dn(cert, dn, &dn_size);
+                       if (err < 0) {
+                               UNKNOWN_SUBJECT(err);
+                       } else
+                               ss << "subject `" << dn << "', ";
+                       free(dn);
+               }
+       }
+
+       /* Issuer */
+       dn = NULL;
+       dn_size = 0;
+       err = gnutls_x509_crt_get_issuer_dn(cert, NULL, &dn_size);
+       if (err != GNUTLS_E_SHORT_MEMORY_BUFFER)
+               UNKNOWN_ISSUER(err);
+       else {
+               dn = (char *)malloc(dn_size);
+               if (dn == NULL) {
+                       UNKNOWN_ISSUER(GNUTLS_E_MEMORY_ERROR);
+               } else {
+                       err = gnutls_x509_crt_get_issuer_dn(cert, dn, &dn_size);
+                       if (err < 0)
+                               UNKNOWN_ISSUER(err);
+                       else
+                               ss << "issuer `" << dn << "', ";
+                       free(dn);
+               }
+       }
+
+       /* Key algorithm and size */
+       unsigned int bits;
+       const char *name;
+       name = gnutls_pk_algorithm_get_name( (gnutls_pk_algorithm_t)
+               gnutls_x509_crt_get_pk_algorithm(cert, &bits));
+       if (name == NULL)
+               name = "Unknown";
+       ss << name << " key " << bits << " bits, ";
+
+       /* Signature algorithm */
+       err = gnutls_x509_crt_get_signature_algorithm(cert);
+       if (err < 0) {
+               ss << "unknown signature algorithm (" << gnutls_strerror(err)
+                  << "), ";
+       } else {
+               const char *name;
+               name = gnutls_sign_algorithm_get_name((gnutls_sign_algorithm_t)err);
+               if (name == NULL)
+                       name = "Unknown";
+
+               ss << "signed using " << name;
+               if (err == GNUTLS_SIGN_RSA_MD5 || err == GNUTLS_SIGN_RSA_MD2)
+                       ss << " (broken!)";
+               ss << ", ";
+       }
+
+       /* Validity */
+       time_t tim;
+       char s[42];
+       size_t max = sizeof(s);
+       struct tm t;
+
+       tim = gnutls_x509_crt_get_activation_time(cert);
+       if (gmtime_r(&tim, &t) == NULL)
+               ss << "unknown activation (" << (unsigned long) tim << ")";
+       else if (strftime(s, max, "%Y-%m-%d %H:%M:%S UTC", &t) == 0)
+               ss << "failed activation (" << (unsigned long) tim << ")";
+       else
+               ss << "activated `" << s << "'";
+       ss << ", ";
+
+       tim = gnutls_x509_crt_get_expiration_time(cert);
+       if (gmtime_r(&tim, &t) == NULL)
+               ss << "unknown expiry (" << (unsigned long) tim << ")";
+       else if (strftime(s, max, "%Y-%m-%d %H:%M:%S UTC", &t) == 0)
+               ss << "failed expiry (" << (unsigned long) tim << ")";
+       else
+               ss << "expires `" << s << "'";
+       ss << ", ";
+
+       /* Fingerprint */
+       char buffer[20];
+       size_t size = sizeof(buffer);
+
+       err = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_SHA1, buffer, &size);
+       if (err < 0)
+               ss << "unknown fingerprint (" << gnutls_strerror(err) << ")";
+       else {
+               ss << "SHA-1 fingerprint `";
+               hexprint(ss, buffer, size);
+               ss << "'";
+       }
+
+       out->data = (unsigned char *) strdup(ss.str().c_str());
+       if (out->data == NULL)
+               return GNUTLS_E_MEMORY_ERROR;
+       out->size = strlen((char *)out->data);
+
+       return 0;
+}
+
+#endif /* HAVE_GNUTLS_X509_CRT_PRINT */
+
+#endif /* HAVE_GNUTLS */
+
diff --git a/common/os/tls.h b/common/os/tls.h
new file mode 100644 (file)
index 0000000..a6d09da
--- /dev/null
@@ -0,0 +1,49 @@
+/* Copyright (C) 2011 TightVNC Team.  All Rights Reserved.
+ *
+ * This 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 software 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 software; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
+ * USA.
+ */
+
+#ifndef OS_TLS_H
+#define OS_TLS_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_GNUTLS
+#include <gnutls/gnutls.h>
+
+#ifndef HAVE_GNUTLS_X509_CRT_PRINT
+#ifdef WIN32
+#error "Please install more recent GNUTLS with gnutls_x509_crt_print() function"
+#endif
+
+typedef enum {
+       GNUTLS_CRT_PRINT_ONELINE = 1
+} gnutls_certificate_print_formats_t;
+
+/*
+ * Prints certificate in human-readable form.
+ */
+int
+gnutls_x509_crt_print(gnutls_x509_crt_t cert,
+                     gnutls_certificate_print_formats_t format,
+                     gnutls_datum_t * out);
+#endif /* HAVE_GNUTLS_X509_CRT_PRINT */
+#endif /* HAVE_GNUTLS */
+
+#endif /* OS_TLS_H */
+
index 7d5846906fb38d2400ca2eb383076fac96813924..f81c6340a4b73b3a12be7022770acf4b6cf3724f 100644 (file)
 #include <rdr/TLSOutStream.h>
 #include <os/os.h>
 #include <os/print.h>
+#include <os/tls.h>
 
 #include <gnutls/x509.h>
 
-#if !defined(GNUTLS_VERSION_NUMBER) || (GNUTLS_VERSION_NUMBER < 0x020708)
-#define GNUTLS_CERT_NOT_ACTIVATED 512
-#define GNUTLS_CERT_EXPIRED 1024
-#endif
-
-#if !defined(GNUTLS_VERSION_NUMBER) || (GNUTLS_VERSION_NUMBER < 0x020301)
-#define GNUTLS_CRT_PRINT_ONELINE 1
+/*
+ * GNUTLS 2.6.5 and older didn't have some variables defined so don't use them.
+ * GNUTLS 1.X.X defined LIBGNUTLS_VERSION_NUMBER so treat it as "old" gnutls as
+ * well
+ */
+#if (defined(GNUTLS_VERSION_NUMBER) && GNUTLS_VERSION_NUMBER < 0x020606) || \
+    defined(LIBGNUTLS_VERSION_NUMBER)
+#define WITHOUT_X509_TIMES
 #endif
 
 #define TLS_DEBUG
@@ -284,6 +286,7 @@ void CSecurityTLS::checkSession()
   if (status & GNUTLS_CERT_REVOKED)
     throw AuthFailureException("server certificate has been revoked");
 
+#ifndef WITHOUT_X509_TIMES
   if (status & GNUTLS_CERT_NOT_ACTIVATED)
     throw AuthFailureException("server certificate has not been activated");
 
@@ -294,6 +297,7 @@ void CSecurityTLS::checkSession()
                         "do you want to continue?"))
       throw AuthFailureException("server certificate has expired");
   }
+#endif
   /* Process other errors later */
 
   cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
@@ -338,7 +342,6 @@ void CSecurityTLS::checkSession()
 
   vlog.debug("Saved server certificates don't match");
 
-  #if defined(GNUTLS_VERSION_NUMBER) && (GNUTLS_VERSION_NUMBER >= 0x010706)
   if (gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &info)) {
     /*
      * GNUTLS doesn't correctly export gnutls_free symbol which is
@@ -352,9 +355,8 @@ void CSecurityTLS::checkSession()
 #endif
     throw AuthFailureException("Could not find certificate to display");
   }
-  #endif
 
-  size_t out_size;
+  size_t out_size = 0;
   char *out_buf = NULL;
   char *certinfo = NULL;
   int len = 0;
index a05e2fd053b05aa494d6135798cccecf45e69723..0c70936933cd75679b2d3742caac4959442674b2 100644 (file)
@@ -107,6 +107,12 @@ if test "x$enable_gnutls" = xyes; then
                       AC_MSG_RESULT(yes),
                       [AC_DEFINE(HAVE_OLD_GNUTLS, 1, [Does gnutls lack the gnutls_transport_set_global_errno() function? ])
                        AC_MSG_RESULT(no)])
+       AC_MSG_CHECKING([for gnutls_x509_crt_print() function])
+       AC_LINK_IFELSE(AC_LANG_CALL([], gnutls_x509_crt_print),
+                      [AC_DEFINE(HAVE_GNUTLS_X509_CRT_PRINT, 1, [Is the gnutls_x509_crt_print() function present? ])
+                       AC_MSG_RESULT(yes)],
+                       AC_MSG_RESULT(no))
+       
        LIBS=${SAVE_LIBS}
 fi
 AC_SUBST([GNUTLS_LIBS])