[Development] Merge CSecurityTLS and CSecurityX509 classes into CSecurityTLSBase class.


git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4109 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/common/rfb/CSecurityTLSBase.cxx b/common/rfb/CSecurityTLSBase.cxx
index 289015a..bc9d3f5 100644
--- a/common/rfb/CSecurityTLSBase.cxx
+++ b/common/rfb/CSecurityTLSBase.cxx
@@ -28,16 +28,22 @@
 #endif
 
 #include <rfb/CSecurityTLSBase.h>
+#include <rfb/SSecurityVeNCrypt.h> 
 #include <rfb/CConnection.h>
 #include <rfb/LogWriter.h>
 #include <rfb/Exception.h>
 #include <rdr/TLSInStream.h>
 #include <rdr/TLSOutStream.h>
 
+#include <gnutls/x509.h>
+
 #define TLS_DEBUG
 
 using namespace rfb;
 
+StringParameter CSecurityTLSBase::x509ca("x509ca", "X509 CA certificate", "", ConfViewer);
+StringParameter CSecurityTLSBase::x509crl("x509crl", "X509 CRL file", "", ConfViewer);
+
 static LogWriter vlog("TLS");
 
 #ifdef TLS_DEBUG
@@ -63,31 +69,48 @@
   }
 }
 
-CSecurityTLSBase::CSecurityTLSBase() : session(0)
+CSecurityTLSBase::CSecurityTLSBase(bool _anon) : session(0), anon_cred(0),
+						 anon(_anon), fis(0), fos(0)
 {
-  fis = 0;
-  fos = 0;
+  cafile = x509ca.getData();
+  crlfile = x509crl.getData();
 }
 
 void CSecurityTLSBase::shutdown()
 {
-  if(session)
-    ;//gnutls_bye(session, GNUTLS_SHUT_RDWR);
+  if (session)
+    gnutls_bye(session, GNUTLS_SHUT_RDWR);
+
+  if (anon_cred) {
+    gnutls_anon_free_client_credentials(anon_cred);
+    anon_cred = 0;
+  }
+
+  if (cert_cred) {
+    gnutls_certificate_free_credentials(cert_cred);
+    cert_cred = 0;
+  }
+
+  if (session) {
+    gnutls_deinit(session);
+    session = 0;
+
+    gnutls_global_deinit();
+  }
 }
 
 
 CSecurityTLSBase::~CSecurityTLSBase()
 {
-  if (session) {
-    //gnutls_bye(session, GNUTLS_SHUT_RDWR);
-    gnutls_deinit (session);
-    session = 0;
-  }
+  shutdown();
+
   if (fis)
     delete fis;
   if (fos)
     delete fos;
-  /* FIXME: should be doing gnutls_global_deinit() at some point */
+
+  delete[] cafile;
+  delete[] crlfile;
 }
 
 bool CSecurityTLSBase::processMsg(CConnection* cc)
@@ -108,7 +131,7 @@
     gnutls_init(&session, GNUTLS_CLIENT);
     gnutls_set_default_priority(session);
 
-    setParam(session);
+    setParam();
     
     gnutls_transport_set_pull_function(session, rdr::gnutls_InStream_pull);
     gnutls_transport_set_push_function(session, rdr::gnutls_OutStream_push);
@@ -124,13 +147,11 @@
 
   if (err != GNUTLS_E_SUCCESS) {
     vlog.error("TLS Handshake failed: %s\n", gnutls_strerror (err));
-    gnutls_bye(session, GNUTLS_SHUT_RDWR);
-    freeResources();
-    gnutls_deinit(session);
-    session = 0;
+    shutdown();
     throw AuthFailureException("TLS Handshake failed");
   }
-  checkSession(session);
+
+  checkSession();
 
   cc->setStreams(fis = new rdr::TLSInStream(is, session),
 		 fos = new rdr::TLSOutStream(os, session));
@@ -138,3 +159,79 @@
   return true;
 }
 
+void CSecurityTLSBase::setParam()
+{
+  static const int kx_anon_priority[] = { GNUTLS_KX_ANON_DH, 0 };
+  static const int kx_priority[] = { GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA,
+				     GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0 };
+
+  if (anon) {
+    gnutls_kx_set_priority(session, kx_anon_priority);
+    gnutls_anon_allocate_client_credentials(&anon_cred);
+    gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred);
+
+    vlog.debug("Anonymous session has been set");
+  } else {
+    gnutls_kx_set_priority(session, kx_priority);
+    gnutls_certificate_allocate_credentials(&cert_cred);
+
+    if (*cafile && gnutls_certificate_set_x509_trust_file(cert_cred,cafile,GNUTLS_X509_FMT_PEM) < 0)
+      throw AuthFailureException("load of CA cert failed");
+
+    if (*crlfile && gnutls_certificate_set_x509_crl_file(cert_cred,crlfile,GNUTLS_X509_FMT_PEM) < 0)
+      throw AuthFailureException("load of CRL failed");
+
+    gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred);
+
+    vlog.debug("X509 session has been set");
+  }
+}
+
+void CSecurityTLSBase::checkSession()
+{
+  int status;
+  const gnutls_datum *cert_list;
+  unsigned int cert_list_size = 0;
+  unsigned int i;
+
+  if (anon)
+    return;
+
+  if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509)
+    throw AuthFailureException("unsupported certificate type");
+
+  cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
+  if (!cert_list_size)
+    throw AuthFailureException("unsupported certificate type");
+
+  status = gnutls_certificate_verify_peers(session);
+  if (status == GNUTLS_E_NO_CERTIFICATE_FOUND)
+    throw AuthFailureException("no certificate sent");
+
+  if (status < 0) {
+    vlog.error("X509 verify failed: %s\n", gnutls_strerror (status));
+    throw AuthFailureException("certificate verification failed");
+  }
+
+  if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
+    throw AuthFailureException("certificate issuer unknown");
+
+  if (status & GNUTLS_CERT_INVALID)
+    throw AuthFailureException("certificate not trusted");
+
+  for (i = 0; i < cert_list_size; i++) {
+    gnutls_x509_crt crt;
+    gnutls_x509_crt_init(&crt);
+
+    if (gnutls_x509_crt_import(crt, &cert_list[i],GNUTLS_X509_FMT_DER) < 0)
+      throw AuthFailureException("Decoding of certificate failed");
+
+    if (gnutls_x509_crt_check_hostname(crt, client->getServerName()) == 0) {
+#if 0
+      throw AuthFailureException("Hostname mismatch"); /* Non-fatal for now... */
+#endif
+    }
+    gnutls_x509_crt_deinit(crt);
+  }
+}
+