Blame qemu-0.9.0-vnc-authentication.patch

Daniel P. Berrange 304b40
diff -rupN qemu-0.9.0.orig/configure qemu-0.9.0.new/configure
Daniel P. Berrange 304b40
--- qemu-0.9.0.orig/configure	2007-02-05 18:01:54.000000000 -0500
Daniel P. Berrange 304b40
+++ qemu-0.9.0.new/configure	2007-08-27 13:35:29.000000000 -0400
Daniel P. Berrange 304b40
@@ -86,6 +86,7 @@ alsa="no"
Daniel P. Berrange 304b40
 fmod="no"
Daniel P. Berrange 304b40
 fmod_lib=""
Daniel P. Berrange 304b40
 fmod_inc=""
Daniel P. Berrange 304b40
+vnc_tls="yes"
Daniel P. Berrange 304b40
 bsd="no"
Daniel P. Berrange 304b40
 linux="no"
Daniel P. Berrange 304b40
 kqemu="no"
Daniel P. Berrange 304b40
@@ -224,6 +225,8 @@ for opt do
Daniel P. Berrange 304b40
   ;;
Daniel P. Berrange 304b40
   --fmod-inc=*) fmod_inc="$optarg"
Daniel P. Berrange 304b40
   ;;
Daniel P. Berrange 304b40
+  --disable-vnc-tls) vnc_tls="no"
Daniel P. Berrange 304b40
+  ;;
Daniel P. Berrange 304b40
   --enable-mingw32) mingw32="yes" ; cross_prefix="i386-mingw32-" ; user="no"
Daniel P. Berrange 304b40
   ;;
Daniel P. Berrange 304b40
   --disable-slirp) slirp="no"
Daniel P. Berrange 304b40
@@ -293,6 +296,7 @@ echo "  --enable-coreaudio       enable 
Daniel P. Berrange 304b40
 echo "  --enable-alsa            enable ALSA audio driver"
Daniel P. Berrange 304b40
 echo "  --enable-fmod            enable FMOD audio driver"
Daniel P. Berrange 304b40
 echo "  --enabled-dsound         enable DirectSound audio driver"
Daniel P. Berrange 304b40
+echo "  --disable-vnc-tls        disable TLS encryption for VNC server"
Daniel P. Berrange 304b40
 echo "  --enable-system          enable all system emulation targets"
Daniel P. Berrange 304b40
 echo "  --disable-system         disable all system emulation targets"
Daniel P. Berrange 304b40
 echo "  --enable-linux-user      enable all linux usermode emulation targets"
Daniel P. Berrange 304b40
@@ -538,6 +542,16 @@ else
Daniel P. Berrange 304b40
 fi # -z $sdl
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 ##########################################
Daniel P. Berrange 304b40
+# VNC TLS detection
Daniel P. Berrange 304b40
+if test "$vnc_tls" = "yes" ; then
Daniel P. Berrange 304b40
+  `pkg-config gnutls` || vnc_tls="no"
Daniel P. Berrange 304b40
+fi
Daniel P. Berrange 304b40
+if test "$vnc_tls" = "yes" ; then
Daniel P. Berrange 304b40
+  vnc_tls_cflags=`pkg-config --cflags gnutls`
Daniel P. Berrange 304b40
+  vnc_tls_libs=`pkg-config --libs gnutls`
Daniel P. Berrange 304b40
+fi
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+##########################################
Daniel P. Berrange 304b40
 # alsa sound support libraries
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 if test "$alsa" = "yes" ; then
Daniel P. Berrange 304b40
@@ -622,6 +636,11 @@ else
Daniel P. Berrange 304b40
     fmod_support=""
Daniel P. Berrange 304b40
 fi
Daniel P. Berrange 304b40
 echo "FMOD support      $fmod $fmod_support"
Daniel P. Berrange 304b40
+echo "VNC TLS support   $vnc_tls"
Daniel P. Berrange 304b40
+if test "$vnc_tls" = "yes" ; then
Daniel P. Berrange 304b40
+    echo "    TLS CFLAGS    $vnc_tls_cflags"
Daniel P. Berrange 304b40
+    echo "    TLS LIBS      $vnc_tls_libs"
Daniel P. Berrange 304b40
+fi
Daniel P. Berrange 304b40
 echo "kqemu support     $kqemu"
Daniel P. Berrange 304b40
 echo "Documentation     $build_docs"
Daniel P. Berrange 304b40
 [ ! -z "$uname_release" ] && \
Daniel P. Berrange 304b40
@@ -765,6 +784,12 @@ if test "$fmod" = "yes" ; then
Daniel P. Berrange 304b40
   echo "CONFIG_FMOD_INC=$fmod_inc" >> $config_mak
Daniel P. Berrange 304b40
   echo "#define CONFIG_FMOD 1" >> $config_h
Daniel P. Berrange 304b40
 fi
Daniel P. Berrange 304b40
+if test "$vnc_tls" = "yes" ; then
Daniel P. Berrange 304b40
+  echo "CONFIG_VNC_TLS=yes" >> $config_mak
Daniel P. Berrange 304b40
+  echo "CONFIG_VNC_TLS_CFLAGS=$vnc_tls_cflags" >> $config_mak
Daniel P. Berrange 304b40
+  echo "CONFIG_VNC_TLS_LIBS=$vnc_tls_libs" >> $config_mak
Daniel P. Berrange 304b40
+  echo "#define CONFIG_VNC_TLS 1" >> $config_h
Daniel P. Berrange 304b40
+fi
Daniel P. Berrange 304b40
 qemu_version=`head $source_path/VERSION`
Daniel P. Berrange 304b40
 echo "VERSION=$qemu_version" >>$config_mak
Daniel P. Berrange 304b40
 echo "#define QEMU_VERSION \"$qemu_version\"" >> $config_h
Daniel P. Berrange 304b40
diff -rupN qemu-0.9.0.orig/d3des.c qemu-0.9.0.new/d3des.c
Daniel P. Berrange 304b40
--- qemu-0.9.0.orig/d3des.c	1969-12-31 19:00:00.000000000 -0500
Daniel P. Berrange 304b40
+++ qemu-0.9.0.new/d3des.c	2007-08-27 13:29:22.000000000 -0400
Daniel P. Berrange 304b40
@@ -0,0 +1,434 @@
Daniel P. Berrange 304b40
+/*
Daniel P. Berrange 304b40
+ * This is D3DES (V5.09) by Richard Outerbridge with the double and
Daniel P. Berrange 304b40
+ * triple-length support removed for use in VNC.  Also the bytebit[] array
Daniel P. Berrange 304b40
+ * has been reversed so that the most significant bit in each byte of the
Daniel P. Berrange 304b40
+ * key is ignored, not the least significant.
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * These changes are:
Daniel P. Berrange 304b40
+ *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * This software is distributed in the hope that it will be useful,
Daniel P. Berrange 304b40
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel P. Berrange 304b40
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Daniel P. Berrange 304b40
+ */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+/* D3DES (V5.09) -
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * A portable, public domain, version of the Data Encryption Standard.
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * Written with Symantec's THINK (Lightspeed) C by Richard Outerbridge.
Daniel P. Berrange 304b40
+ * Thanks to: Dan Hoey for his excellent Initial and Inverse permutation
Daniel P. Berrange 304b40
+ * code;  Jim Gillogly & Phil Karn for the DES key schedule code; Dennis
Daniel P. Berrange 304b40
+ * Ferguson, Eric Young and Dana How for comparing notes; and Ray Lau,
Daniel P. Berrange 304b40
+ * for humouring me on.
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge.
Daniel P. Berrange 304b40
+ * (GEnie : OUTER; CIS : [71755,204]) Graven Imagery, 1992.
Daniel P. Berrange 304b40
+ */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#include "d3des.h"
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static void scrunch(unsigned char *, unsigned long *);
Daniel P. Berrange 304b40
+static void unscrun(unsigned long *, unsigned char *);
Daniel P. Berrange 304b40
+static void desfunc(unsigned long *, unsigned long *);
Daniel P. Berrange 304b40
+static void cookey(unsigned long *);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long KnL[32] = { 0L };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned short bytebit[8]	= {
Daniel P. Berrange 304b40
+	01, 02, 04, 010, 020, 040, 0100, 0200 };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long bigbyte[24] = {
Daniel P. Berrange 304b40
+	0x800000L,	0x400000L,	0x200000L,	0x100000L,
Daniel P. Berrange 304b40
+	0x80000L,	0x40000L,	0x20000L,	0x10000L,
Daniel P. Berrange 304b40
+	0x8000L,	0x4000L,	0x2000L,	0x1000L,
Daniel P. Berrange 304b40
+	0x800L, 	0x400L, 	0x200L, 	0x100L,
Daniel P. Berrange 304b40
+	0x80L,		0x40L,		0x20L,		0x10L,
Daniel P. Berrange 304b40
+	0x8L,		0x4L,		0x2L,		0x1L	};
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+/* Use the key schedule specified in the Standard (ANSI X3.92-1981). */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned char pc1[56] = {
Daniel P. Berrange 304b40
+	56, 48, 40, 32, 24, 16,  8,	 0, 57, 49, 41, 33, 25, 17,
Daniel P. Berrange 304b40
+	 9,  1, 58, 50, 42, 34, 26,	18, 10,  2, 59, 51, 43, 35,
Daniel P. Berrange 304b40
+	62, 54, 46, 38, 30, 22, 14,	 6, 61, 53, 45, 37, 29, 21,
Daniel P. Berrange 304b40
+	13,  5, 60, 52, 44, 36, 28,	20, 12,  4, 27, 19, 11,  3 };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned char totrot[16] = {
Daniel P. Berrange 304b40
+	1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned char pc2[48] = {
Daniel P. Berrange 304b40
+	13, 16, 10, 23,  0,  4,  2, 27, 14,  5, 20,  9,
Daniel P. Berrange 304b40
+	22, 18, 11,  3, 25,  7, 15,  6, 26, 19, 12,  1,
Daniel P. Berrange 304b40
+	40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
Daniel P. Berrange 304b40
+	43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+void deskey(key, edf)	/* Thanks to James Gillogly & Phil Karn! */
Daniel P. Berrange 304b40
+unsigned char *key;
Daniel P. Berrange 304b40
+int edf;
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+	register int i, j, l, m, n;
Daniel P. Berrange 304b40
+	unsigned char pc1m[56], pcr[56];
Daniel P. Berrange 304b40
+	unsigned long kn[32];
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	for ( j = 0; j < 56; j++ ) {
Daniel P. Berrange 304b40
+		l = pc1[j];
Daniel P. Berrange 304b40
+		m = l & 07;
Daniel P. Berrange 304b40
+		pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0;
Daniel P. Berrange 304b40
+		}
Daniel P. Berrange 304b40
+	for( i = 0; i < 16; i++ ) {
Daniel P. Berrange 304b40
+		if( edf == DE1 ) m = (15 - i) << 1;
Daniel P. Berrange 304b40
+		else m = i << 1;
Daniel P. Berrange 304b40
+		n = m + 1;
Daniel P. Berrange 304b40
+		kn[m] = kn[n] = 0L;
Daniel P. Berrange 304b40
+		for( j = 0; j < 28; j++ ) {
Daniel P. Berrange 304b40
+			l = j + totrot[i];
Daniel P. Berrange 304b40
+			if( l < 28 ) pcr[j] = pc1m[l];
Daniel P. Berrange 304b40
+			else pcr[j] = pc1m[l - 28];
Daniel P. Berrange 304b40
+			}
Daniel P. Berrange 304b40
+		for( j = 28; j < 56; j++ ) {
Daniel P. Berrange 304b40
+		    l = j + totrot[i];
Daniel P. Berrange 304b40
+		    if( l < 56 ) pcr[j] = pc1m[l];
Daniel P. Berrange 304b40
+		    else pcr[j] = pc1m[l - 28];
Daniel P. Berrange 304b40
+		    }
Daniel P. Berrange 304b40
+		for( j = 0; j < 24; j++ ) {
Daniel P. Berrange 304b40
+			if( pcr[pc2[j]] ) kn[m] |= bigbyte[j];
Daniel P. Berrange 304b40
+			if( pcr[pc2[j+24]] ) kn[n] |= bigbyte[j];
Daniel P. Berrange 304b40
+			}
Daniel P. Berrange 304b40
+		}
Daniel P. Berrange 304b40
+	cookey(kn);
Daniel P. Berrange 304b40
+	return;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static void cookey(raw1)
Daniel P. Berrange 304b40
+register unsigned long *raw1;
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+	register unsigned long *cook, *raw0;
Daniel P. Berrange 304b40
+	unsigned long dough[32];
Daniel P. Berrange 304b40
+	register int i;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	cook = dough;
Daniel P. Berrange 304b40
+	for( i = 0; i < 16; i++, raw1++ ) {
Daniel P. Berrange 304b40
+		raw0 = raw1++;
Daniel P. Berrange 304b40
+		*cook	 = (*raw0 & 0x00fc0000L) << 6;
Daniel P. Berrange 304b40
+		*cook	|= (*raw0 & 0x00000fc0L) << 10;
Daniel P. Berrange 304b40
+		*cook	|= (*raw1 & 0x00fc0000L) >> 10;
Daniel P. Berrange 304b40
+		*cook++ |= (*raw1 & 0x00000fc0L) >> 6;
Daniel P. Berrange 304b40
+		*cook	 = (*raw0 & 0x0003f000L) << 12;
Daniel P. Berrange 304b40
+		*cook	|= (*raw0 & 0x0000003fL) << 16;
Daniel P. Berrange 304b40
+		*cook	|= (*raw1 & 0x0003f000L) >> 4;
Daniel P. Berrange 304b40
+		*cook++ |= (*raw1 & 0x0000003fL);
Daniel P. Berrange 304b40
+		}
Daniel P. Berrange 304b40
+	usekey(dough);
Daniel P. Berrange 304b40
+	return;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+void cpkey(into)
Daniel P. Berrange 304b40
+register unsigned long *into;
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+	register unsigned long *from, *endp;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	from = KnL, endp = &KnL[32];
Daniel P. Berrange 304b40
+	while( from < endp ) *into++ = *from++;
Daniel P. Berrange 304b40
+	return;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+void usekey(from)
Daniel P. Berrange 304b40
+register unsigned long *from;
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+	register unsigned long *to, *endp;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	to = KnL, endp = &KnL[32];
Daniel P. Berrange 304b40
+	while( to < endp ) *to++ = *from++;
Daniel P. Berrange 304b40
+	return;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+void des(inblock, outblock)
Daniel P. Berrange 304b40
+unsigned char *inblock, *outblock;
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+	unsigned long work[2];
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	scrunch(inblock, work);
Daniel P. Berrange 304b40
+	desfunc(work, KnL);
Daniel P. Berrange 304b40
+	unscrun(work, outblock);
Daniel P. Berrange 304b40
+	return;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static void scrunch(outof, into)
Daniel P. Berrange 304b40
+register unsigned char *outof;
Daniel P. Berrange 304b40
+register unsigned long *into;
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+	*into	 = (*outof++ & 0xffL) << 24;
Daniel P. Berrange 304b40
+	*into	|= (*outof++ & 0xffL) << 16;
Daniel P. Berrange 304b40
+	*into	|= (*outof++ & 0xffL) << 8;
Daniel P. Berrange 304b40
+	*into++ |= (*outof++ & 0xffL);
Daniel P. Berrange 304b40
+	*into	 = (*outof++ & 0xffL) << 24;
Daniel P. Berrange 304b40
+	*into	|= (*outof++ & 0xffL) << 16;
Daniel P. Berrange 304b40
+	*into	|= (*outof++ & 0xffL) << 8;
Daniel P. Berrange 304b40
+	*into	|= (*outof   & 0xffL);
Daniel P. Berrange 304b40
+	return;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static void unscrun(outof, into)
Daniel P. Berrange 304b40
+register unsigned long *outof;
Daniel P. Berrange 304b40
+register unsigned char *into;
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+	*into++ = (unsigned char)((*outof >> 24) & 0xffL);
Daniel P. Berrange 304b40
+	*into++ = (unsigned char)((*outof >> 16) & 0xffL);
Daniel P. Berrange 304b40
+	*into++ = (unsigned char)((*outof >>  8) & 0xffL);
Daniel P. Berrange 304b40
+	*into++ = (unsigned char)(*outof++	 & 0xffL);
Daniel P. Berrange 304b40
+	*into++ = (unsigned char)((*outof >> 24) & 0xffL);
Daniel P. Berrange 304b40
+	*into++ = (unsigned char)((*outof >> 16) & 0xffL);
Daniel P. Berrange 304b40
+	*into++ = (unsigned char)((*outof >>  8) & 0xffL);
Daniel P. Berrange 304b40
+	*into	=  (unsigned char)(*outof	 & 0xffL);
Daniel P. Berrange 304b40
+	return;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long SP1[64] = {
Daniel P. Berrange 304b40
+	0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
Daniel P. Berrange 304b40
+	0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
Daniel P. Berrange 304b40
+	0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
Daniel P. Berrange 304b40
+	0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
Daniel P. Berrange 304b40
+	0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
Daniel P. Berrange 304b40
+	0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
Daniel P. Berrange 304b40
+	0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
Daniel P. Berrange 304b40
+	0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
Daniel P. Berrange 304b40
+	0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
Daniel P. Berrange 304b40
+	0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
Daniel P. Berrange 304b40
+	0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
Daniel P. Berrange 304b40
+	0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
Daniel P. Berrange 304b40
+	0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
Daniel P. Berrange 304b40
+	0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
Daniel P. Berrange 304b40
+	0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
Daniel P. Berrange 304b40
+	0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long SP2[64] = {
Daniel P. Berrange 304b40
+	0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
Daniel P. Berrange 304b40
+	0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
Daniel P. Berrange 304b40
+	0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
Daniel P. Berrange 304b40
+	0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
Daniel P. Berrange 304b40
+	0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
Daniel P. Berrange 304b40
+	0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
Daniel P. Berrange 304b40
+	0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
Daniel P. Berrange 304b40
+	0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
Daniel P. Berrange 304b40
+	0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
Daniel P. Berrange 304b40
+	0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
Daniel P. Berrange 304b40
+	0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
Daniel P. Berrange 304b40
+	0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
Daniel P. Berrange 304b40
+	0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
Daniel P. Berrange 304b40
+	0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
Daniel P. Berrange 304b40
+	0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
Daniel P. Berrange 304b40
+	0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long SP3[64] = {
Daniel P. Berrange 304b40
+	0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
Daniel P. Berrange 304b40
+	0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
Daniel P. Berrange 304b40
+	0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
Daniel P. Berrange 304b40
+	0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
Daniel P. Berrange 304b40
+	0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
Daniel P. Berrange 304b40
+	0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
Daniel P. Berrange 304b40
+	0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
Daniel P. Berrange 304b40
+	0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
Daniel P. Berrange 304b40
+	0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
Daniel P. Berrange 304b40
+	0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
Daniel P. Berrange 304b40
+	0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
Daniel P. Berrange 304b40
+	0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
Daniel P. Berrange 304b40
+	0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
Daniel P. Berrange 304b40
+	0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
Daniel P. Berrange 304b40
+	0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
Daniel P. Berrange 304b40
+	0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long SP4[64] = {
Daniel P. Berrange 304b40
+	0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
Daniel P. Berrange 304b40
+	0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
Daniel P. Berrange 304b40
+	0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
Daniel P. Berrange 304b40
+	0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
Daniel P. Berrange 304b40
+	0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
Daniel P. Berrange 304b40
+	0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
Daniel P. Berrange 304b40
+	0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
Daniel P. Berrange 304b40
+	0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
Daniel P. Berrange 304b40
+	0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
Daniel P. Berrange 304b40
+	0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
Daniel P. Berrange 304b40
+	0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
Daniel P. Berrange 304b40
+	0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
Daniel P. Berrange 304b40
+	0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
Daniel P. Berrange 304b40
+	0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
Daniel P. Berrange 304b40
+	0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
Daniel P. Berrange 304b40
+	0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long SP5[64] = {
Daniel P. Berrange 304b40
+	0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
Daniel P. Berrange 304b40
+	0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
Daniel P. Berrange 304b40
+	0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
Daniel P. Berrange 304b40
+	0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
Daniel P. Berrange 304b40
+	0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
Daniel P. Berrange 304b40
+	0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
Daniel P. Berrange 304b40
+	0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
Daniel P. Berrange 304b40
+	0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
Daniel P. Berrange 304b40
+	0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
Daniel P. Berrange 304b40
+	0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,
Daniel P. Berrange 304b40
+	0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,
Daniel P. Berrange 304b40
+	0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,
Daniel P. Berrange 304b40
+	0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,
Daniel P. Berrange 304b40
+	0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,
Daniel P. Berrange 304b40
+	0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,
Daniel P. Berrange 304b40
+	0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long SP6[64] = {
Daniel P. Berrange 304b40
+	0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,
Daniel P. Berrange 304b40
+	0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,
Daniel P. Berrange 304b40
+	0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,
Daniel P. Berrange 304b40
+	0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,
Daniel P. Berrange 304b40
+	0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,
Daniel P. Berrange 304b40
+	0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,
Daniel P. Berrange 304b40
+	0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,
Daniel P. Berrange 304b40
+	0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,
Daniel P. Berrange 304b40
+	0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,
Daniel P. Berrange 304b40
+	0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,
Daniel P. Berrange 304b40
+	0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,
Daniel P. Berrange 304b40
+	0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,
Daniel P. Berrange 304b40
+	0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,
Daniel P. Berrange 304b40
+	0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,
Daniel P. Berrange 304b40
+	0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,
Daniel P. Berrange 304b40
+	0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long SP7[64] = {
Daniel P. Berrange 304b40
+	0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,
Daniel P. Berrange 304b40
+	0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,
Daniel P. Berrange 304b40
+	0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,
Daniel P. Berrange 304b40
+	0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,
Daniel P. Berrange 304b40
+	0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,
Daniel P. Berrange 304b40
+	0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,
Daniel P. Berrange 304b40
+	0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,
Daniel P. Berrange 304b40
+	0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,
Daniel P. Berrange 304b40
+	0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,
Daniel P. Berrange 304b40
+	0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,
Daniel P. Berrange 304b40
+	0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,
Daniel P. Berrange 304b40
+	0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,
Daniel P. Berrange 304b40
+	0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,
Daniel P. Berrange 304b40
+	0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,
Daniel P. Berrange 304b40
+	0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,
Daniel P. Berrange 304b40
+	0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static unsigned long SP8[64] = {
Daniel P. Berrange 304b40
+	0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,
Daniel P. Berrange 304b40
+	0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,
Daniel P. Berrange 304b40
+	0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,
Daniel P. Berrange 304b40
+	0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,
Daniel P. Berrange 304b40
+	0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,
Daniel P. Berrange 304b40
+	0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,
Daniel P. Berrange 304b40
+	0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,
Daniel P. Berrange 304b40
+	0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,
Daniel P. Berrange 304b40
+	0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,
Daniel P. Berrange 304b40
+	0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,
Daniel P. Berrange 304b40
+	0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,
Daniel P. Berrange 304b40
+	0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,
Daniel P. Berrange 304b40
+	0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,
Daniel P. Berrange 304b40
+	0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,
Daniel P. Berrange 304b40
+	0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,
Daniel P. Berrange 304b40
+	0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static void desfunc(block, keys)
Daniel P. Berrange 304b40
+register unsigned long *block, *keys;
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+	register unsigned long fval, work, right, leftt;
Daniel P. Berrange 304b40
+	register int round;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	leftt = block[0];
Daniel P. Berrange 304b40
+	right = block[1];
Daniel P. Berrange 304b40
+	work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL;
Daniel P. Berrange 304b40
+	right ^= work;
Daniel P. Berrange 304b40
+	leftt ^= (work << 4);
Daniel P. Berrange 304b40
+	work = ((leftt >> 16) ^ right) & 0x0000ffffL;
Daniel P. Berrange 304b40
+	right ^= work;
Daniel P. Berrange 304b40
+	leftt ^= (work << 16);
Daniel P. Berrange 304b40
+	work = ((right >> 2) ^ leftt) & 0x33333333L;
Daniel P. Berrange 304b40
+	leftt ^= work;
Daniel P. Berrange 304b40
+	right ^= (work << 2);
Daniel P. Berrange 304b40
+	work = ((right >> 8) ^ leftt) & 0x00ff00ffL;
Daniel P. Berrange 304b40
+	leftt ^= work;
Daniel P. Berrange 304b40
+	right ^= (work << 8);
Daniel P. Berrange 304b40
+	right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL;
Daniel P. Berrange 304b40
+	work = (leftt ^ right) & 0xaaaaaaaaL;
Daniel P. Berrange 304b40
+	leftt ^= work;
Daniel P. Berrange 304b40
+	right ^= work;
Daniel P. Berrange 304b40
+	leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	for( round = 0; round < 8; round++ ) {
Daniel P. Berrange 304b40
+		work  = (right << 28) | (right >> 4);
Daniel P. Berrange 304b40
+		work ^= *keys++;
Daniel P. Berrange 304b40
+		fval  = SP7[ work		 & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP5[(work >>  8) & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP3[(work >> 16) & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP1[(work >> 24) & 0x3fL];
Daniel P. Berrange 304b40
+		work  = right ^ *keys++;
Daniel P. Berrange 304b40
+		fval |= SP8[ work		 & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP6[(work >>  8) & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP4[(work >> 16) & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP2[(work >> 24) & 0x3fL];
Daniel P. Berrange 304b40
+		leftt ^= fval;
Daniel P. Berrange 304b40
+		work  = (leftt << 28) | (leftt >> 4);
Daniel P. Berrange 304b40
+		work ^= *keys++;
Daniel P. Berrange 304b40
+		fval  = SP7[ work		 & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP5[(work >>  8) & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP3[(work >> 16) & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP1[(work >> 24) & 0x3fL];
Daniel P. Berrange 304b40
+		work  = leftt ^ *keys++;
Daniel P. Berrange 304b40
+		fval |= SP8[ work		 & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP6[(work >>  8) & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP4[(work >> 16) & 0x3fL];
Daniel P. Berrange 304b40
+		fval |= SP2[(work >> 24) & 0x3fL];
Daniel P. Berrange 304b40
+		right ^= fval;
Daniel P. Berrange 304b40
+		}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	right = (right << 31) | (right >> 1);
Daniel P. Berrange 304b40
+	work = (leftt ^ right) & 0xaaaaaaaaL;
Daniel P. Berrange 304b40
+	leftt ^= work;
Daniel P. Berrange 304b40
+	right ^= work;
Daniel P. Berrange 304b40
+	leftt = (leftt << 31) | (leftt >> 1);
Daniel P. Berrange 304b40
+	work = ((leftt >> 8) ^ right) & 0x00ff00ffL;
Daniel P. Berrange 304b40
+	right ^= work;
Daniel P. Berrange 304b40
+	leftt ^= (work << 8);
Daniel P. Berrange 304b40
+	work = ((leftt >> 2) ^ right) & 0x33333333L;
Daniel P. Berrange 304b40
+	right ^= work;
Daniel P. Berrange 304b40
+	leftt ^= (work << 2);
Daniel P. Berrange 304b40
+	work = ((right >> 16) ^ leftt) & 0x0000ffffL;
Daniel P. Berrange 304b40
+	leftt ^= work;
Daniel P. Berrange 304b40
+	right ^= (work << 16);
Daniel P. Berrange 304b40
+	work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL;
Daniel P. Berrange 304b40
+	leftt ^= work;
Daniel P. Berrange 304b40
+	right ^= (work << 4);
Daniel P. Berrange 304b40
+	*block++ = right;
Daniel P. Berrange 304b40
+	*block = leftt;
Daniel P. Berrange 304b40
+	return;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+/* Validation sets:
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * Single-length key, single-length plaintext -
Daniel P. Berrange 304b40
+ * Key	  : 0123 4567 89ab cdef
Daniel P. Berrange 304b40
+ * Plain  : 0123 4567 89ab cde7
Daniel P. Berrange 304b40
+ * Cipher : c957 4425 6a5e d31d
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * Double-length key, single-length plaintext -
Daniel P. Berrange 304b40
+ * Key	  : 0123 4567 89ab cdef fedc ba98 7654 3210
Daniel P. Berrange 304b40
+ * Plain  : 0123 4567 89ab cde7
Daniel P. Berrange 304b40
+ * Cipher : 7f1d 0a77 826b 8aff
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * Double-length key, double-length plaintext -
Daniel P. Berrange 304b40
+ * Key	  : 0123 4567 89ab cdef fedc ba98 7654 3210
Daniel P. Berrange 304b40
+ * Plain  : 0123 4567 89ab cdef 0123 4567 89ab cdff
Daniel P. Berrange 304b40
+ * Cipher : 27a0 8440 406a df60 278f 47cf 42d6 15d7
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * Triple-length key, single-length plaintext -
Daniel P. Berrange 304b40
+ * Key	  : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567
Daniel P. Berrange 304b40
+ * Plain  : 0123 4567 89ab cde7
Daniel P. Berrange 304b40
+ * Cipher : de0b 7c06 ae5e 0ed5
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * Triple-length key, double-length plaintext -
Daniel P. Berrange 304b40
+ * Key	  : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567
Daniel P. Berrange 304b40
+ * Plain  : 0123 4567 89ab cdef 0123 4567 89ab cdff
Daniel P. Berrange 304b40
+ * Cipher : ad0d 1b30 ac17 cf07 0ed1 1c63 81e4 4de5
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * d3des V5.0a rwo 9208.07 18:44 Graven Imagery
Daniel P. Berrange 304b40
+ **********************************************************************/
Daniel P. Berrange 304b40
diff -rupN qemu-0.9.0.orig/d3des.h qemu-0.9.0.new/d3des.h
Daniel P. Berrange 304b40
--- qemu-0.9.0.orig/d3des.h	1969-12-31 19:00:00.000000000 -0500
Daniel P. Berrange 304b40
+++ qemu-0.9.0.new/d3des.h	2007-08-27 13:29:22.000000000 -0400
Daniel P. Berrange 304b40
@@ -0,0 +1,51 @@
Daniel P. Berrange 304b40
+/*
Daniel P. Berrange 304b40
+ * This is D3DES (V5.09) by Richard Outerbridge with the double and
Daniel P. Berrange 304b40
+ * triple-length support removed for use in VNC.
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * These changes are:
Daniel P. Berrange 304b40
+ *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * This software is distributed in the hope that it will be useful,
Daniel P. Berrange 304b40
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel P. Berrange 304b40
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Daniel P. Berrange 304b40
+ */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+/* d3des.h -
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ *	Headers and defines for d3des.c
Daniel P. Berrange 304b40
+ *	Graven Imagery, 1992.
Daniel P. Berrange 304b40
+ *
Daniel P. Berrange 304b40
+ * Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge
Daniel P. Berrange 304b40
+ *	(GEnie : OUTER; CIS : [71755,204])
Daniel P. Berrange 304b40
+ */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#define EN0	0	/* MODE == encrypt */
Daniel P. Berrange 304b40
+#define DE1	1	/* MODE == decrypt */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+extern void deskey(unsigned char *, int);
Daniel P. Berrange 304b40
+/*		      hexkey[8]     MODE
Daniel P. Berrange 304b40
+ * Sets the internal key register according to the hexadecimal
Daniel P. Berrange 304b40
+ * key contained in the 8 bytes of hexkey, according to the DES,
Daniel P. Berrange 304b40
+ * for encryption or decryption according to MODE.
Daniel P. Berrange 304b40
+ */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+extern void usekey(unsigned long *);
Daniel P. Berrange 304b40
+/*		    cookedkey[32]
Daniel P. Berrange 304b40
+ * Loads the internal key register with the data in cookedkey.
Daniel P. Berrange 304b40
+ */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+extern void cpkey(unsigned long *);
Daniel P. Berrange 304b40
+/*		   cookedkey[32]
Daniel P. Berrange 304b40
+ * Copies the contents of the internal key register into the storage
Daniel P. Berrange 304b40
+ * located at &cookedkey[0].
Daniel P. Berrange 304b40
+ */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+extern void des(unsigned char *, unsigned char *);
Daniel P. Berrange 304b40
+/*		    from[8]	      to[8]
Daniel P. Berrange 304b40
+ * Encrypts/Decrypts (according to the key currently loaded in the
Daniel P. Berrange 304b40
+ * internal key register) one block of eight bytes at address 'from'
Daniel P. Berrange 304b40
+ * into the block at address 'to'.  They can be the same.
Daniel P. Berrange 304b40
+ */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+/* d3des.h V5.09 rwo 9208.04 15:06 Graven Imagery
Daniel P. Berrange 304b40
+ ********************************************************************/
Daniel P. Berrange 304b40
diff -rupN qemu-0.9.0.orig/Makefile.target qemu-0.9.0.new/Makefile.target
Daniel P. Berrange 304b40
--- qemu-0.9.0.orig/Makefile.target	2007-08-27 12:44:06.000000000 -0400
Daniel P. Berrange 304b40
+++ qemu-0.9.0.new/Makefile.target	2007-08-27 13:27:34.000000000 -0400
Daniel P. Berrange 304b40
@@ -356,6 +356,11 @@ SOUND_HW += fmopl.o adlib.o
Daniel P. Berrange 304b40
 endif
Daniel P. Berrange 304b40
 AUDIODRV+= wavcapture.o
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
+ifdef CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+CPPFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
Daniel P. Berrange 304b40
+LIBS += $(CONFIG_VNC_TLS_LIBS)
Daniel P. Berrange 304b40
+endif
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
 # SCSI layer
Daniel P. Berrange 304b40
 VL_OBJS+= scsi-disk.o cdrom.o lsi53c895a.o
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
@@ -413,7 +418,7 @@ endif
Daniel P. Berrange 304b40
 ifdef CONFIG_SDL
Daniel P. Berrange 304b40
 VL_OBJS+=sdl.o x_keymap.o
Daniel P. Berrange 304b40
 endif
Daniel P. Berrange 304b40
-VL_OBJS+=vnc.o
Daniel P. Berrange 304b40
+VL_OBJS+=vnc.o d3des.o
Daniel P. Berrange 304b40
 ifdef CONFIG_COCOA
Daniel P. Berrange 304b40
 VL_OBJS+=cocoa.o
Daniel P. Berrange 304b40
 COCOA_LIBS=-F/System/Library/Frameworks -framework Cocoa -framework IOKit
Daniel P. Berrange 304b40
@@ -471,7 +476,7 @@ cocoa.o: cocoa.m
Daniel P. Berrange 304b40
 sdl.o: sdl.c keymaps.c sdl_keysym.h
Daniel P. Berrange 304b40
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(SDL_CFLAGS) $(BASE_CFLAGS) -c -o $@ $<
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-vnc.o: vnc.c keymaps.c sdl_keysym.h vnchextile.h
Daniel P. Berrange 304b40
+vnc.o: vnc.c keymaps.c sdl_keysym.h vnchextile.h d3des.c d3des.h
Daniel P. Berrange 304b40
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(BASE_CFLAGS) -c -o $@ $<
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 sdlaudio.o: sdlaudio.c
Daniel P. Berrange 304b40
diff -rupN qemu-0.9.0.orig/monitor.c qemu-0.9.0.new/monitor.c
Daniel P. Berrange 304b40
--- qemu-0.9.0.orig/monitor.c	2007-02-05 18:01:54.000000000 -0500
Daniel P. Berrange 304b40
+++ qemu-0.9.0.new/monitor.c	2007-08-27 13:15:46.000000000 -0400
Daniel P. Berrange 304b40
@@ -362,7 +362,7 @@ static void do_eject(int force, const ch
Daniel P. Berrange 304b40
     eject_device(bs, force);
Daniel P. Berrange 304b40
 }
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-static void do_change(const char *device, const char *filename)
Daniel P. Berrange 304b40
+static void do_change_block(const char *device, const char *filename)
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
     BlockDriverState *bs;
Daniel P. Berrange 304b40
     int i;
Daniel P. Berrange 304b40
@@ -387,6 +387,30 @@ static void do_change(const char *device
Daniel P. Berrange 304b40
     }
Daniel P. Berrange 304b40
 }
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
+static void do_change_vnc(const char *target)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    if (strcmp(target, "passwd") == 0 ||
Daniel P. Berrange 304b40
+	strcmp(target, "password") == 0) {
Daniel P. Berrange 304b40
+	char password[9];
Daniel P. Berrange 304b40
+	monitor_readline("Password: ", 1, password, sizeof(password)-1);
Daniel P. Berrange 304b40
+	password[sizeof(password)-1] = '\0';
Daniel P. Berrange 304b40
+	if (vnc_display_password(NULL, password) < 0)
Daniel P. Berrange 304b40
+	    term_printf("could not set VNC server password\n");
Daniel P. Berrange 304b40
+    } else {
Daniel P. Berrange 304b40
+	if (vnc_display_open(NULL, target) < 0)
Daniel P. Berrange 304b40
+	    term_printf("could not start VNC server on %s\n", target);
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static void do_change(const char *device, const char *target)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    if (strcmp(device, "vnc") == 0) {
Daniel P. Berrange 304b40
+	do_change_vnc(target);
Daniel P. Berrange 304b40
+    } else {
Daniel P. Berrange 304b40
+	do_change_block(device, target);
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
 static void do_screen_dump(const char *filename)
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
     vga_hw_screen_dump(filename);
Daniel P. Berrange 304b40
diff -rupN qemu-0.9.0.orig/vl.c qemu-0.9.0.new/vl.c
Daniel P. Berrange 304b40
--- qemu-0.9.0.orig/vl.c	2007-02-05 18:01:54.000000000 -0500
Daniel P. Berrange 304b40
+++ qemu-0.9.0.new/vl.c	2007-08-27 13:16:25.000000000 -0400
Daniel P. Berrange 304b40
@@ -173,6 +173,7 @@ const char *option_rom[MAX_OPTION_ROMS];
Daniel P. Berrange 304b40
 int nb_option_roms;
Daniel P. Berrange 304b40
 int semihosting_enabled = 0;
Daniel P. Berrange 304b40
 int autostart = 1;
Daniel P. Berrange 304b40
+const char *qemu_name;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 /***********************************************************/
Daniel P. Berrange 304b40
 /* x86 ISA bus support */
Daniel P. Berrange 304b40
@@ -4140,6 +4141,7 @@ typedef struct IOHandlerRecord {
Daniel P. Berrange 304b40
     IOCanRWHandler *fd_read_poll;
Daniel P. Berrange 304b40
     IOHandler *fd_read;
Daniel P. Berrange 304b40
     IOHandler *fd_write;
Daniel P. Berrange 304b40
+    int deleted;
Daniel P. Berrange 304b40
     void *opaque;
Daniel P. Berrange 304b40
     /* temporary data */
Daniel P. Berrange 304b40
     struct pollfd *ufd;
Daniel P. Berrange 304b40
@@ -4165,8 +4167,7 @@ int qemu_set_fd_handler2(int fd, 
Daniel P. Berrange 304b40
             if (ioh == NULL)
Daniel P. Berrange 304b40
                 break;
Daniel P. Berrange 304b40
             if (ioh->fd == fd) {
Daniel P. Berrange 304b40
-                *pioh = ioh->next;
Daniel P. Berrange 304b40
-                qemu_free(ioh);
Daniel P. Berrange 304b40
+                ioh->deleted = 1;
Daniel P. Berrange 304b40
                 break;
Daniel P. Berrange 304b40
             }
Daniel P. Berrange 304b40
             pioh = &ioh->next;
Daniel P. Berrange 304b40
@@ -4187,6 +4188,7 @@ int qemu_set_fd_handler2(int fd, 
Daniel P. Berrange 304b40
         ioh->fd_read = fd_read;
Daniel P. Berrange 304b40
         ioh->fd_write = fd_write;
Daniel P. Berrange 304b40
         ioh->opaque = opaque;
Daniel P. Berrange 304b40
+        ioh->deleted = 0;
Daniel P. Berrange 304b40
     }
Daniel P. Berrange 304b40
     return 0;
Daniel P. Berrange 304b40
 }
Daniel P. Berrange 304b40
@@ -5835,7 +5837,7 @@ void qemu_system_powerdown_request(void)
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 void main_loop_wait(int timeout)
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
-    IOHandlerRecord *ioh, *ioh_next;
Daniel P. Berrange 304b40
+    IOHandlerRecord *ioh;
Daniel P. Berrange 304b40
     fd_set rfds, wfds, xfds;
Daniel P. Berrange 304b40
     int ret, nfds;
Daniel P. Berrange 304b40
     struct timeval tv;
Daniel P. Berrange 304b40
@@ -5870,6 +5872,8 @@ void main_loop_wait(int timeout)
Daniel P. Berrange 304b40
     FD_ZERO(&wfds);
Daniel P. Berrange 304b40
     FD_ZERO(&xfds);
Daniel P. Berrange 304b40
     for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
Daniel P. Berrange 304b40
+        if (ioh->deleted)
Daniel P. Berrange 304b40
+            continue;
Daniel P. Berrange 304b40
         if (ioh->fd_read &&
Daniel P. Berrange 304b40
             (!ioh->fd_read_poll ||
Daniel P. Berrange 304b40
              ioh->fd_read_poll(ioh->opaque) != 0)) {
Daniel P. Berrange 304b40
@@ -5897,16 +5901,27 @@ void main_loop_wait(int timeout)
Daniel P. Berrange 304b40
 #endif
Daniel P. Berrange 304b40
     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv;;
Daniel P. Berrange 304b40
     if (ret > 0) {
Daniel P. Berrange 304b40
-        /* XXX: better handling of removal */
Daniel P. Berrange 304b40
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
Daniel P. Berrange 304b40
-            ioh_next = ioh->next;
Daniel P. Berrange 304b40
-            if (FD_ISSET(ioh->fd, &rfds)) {
Daniel P. Berrange 304b40
+        IOHandlerRecord **pioh;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
Daniel P. Berrange 304b40
+            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
Daniel P. Berrange 304b40
                 ioh->fd_read(ioh->opaque);
Daniel P. Berrange 304b40
             }
Daniel P. Berrange 304b40
-            if (FD_ISSET(ioh->fd, &wfds)) {
Daniel P. Berrange 304b40
+            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
Daniel P. Berrange 304b40
                 ioh->fd_write(ioh->opaque);
Daniel P. Berrange 304b40
             }
Daniel P. Berrange 304b40
         }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	/* remove deleted IO handlers */
Daniel P. Berrange 304b40
+	pioh = &first_io_handler;
Daniel P. Berrange 304b40
+	while (*pioh) {
Daniel P. Berrange 304b40
+            ioh = *pioh;
Daniel P. Berrange 304b40
+            if (ioh->deleted) {
Daniel P. Berrange 304b40
+                *pioh = ioh->next;
Daniel P. Berrange 304b40
+                qemu_free(ioh);
Daniel P. Berrange 304b40
+            } else 
Daniel P. Berrange 304b40
+                pioh = &ioh->next;
Daniel P. Berrange 304b40
+        }
Daniel P. Berrange 304b40
     }
Daniel P. Berrange 304b40
 #if defined(CONFIG_SLIRP)
Daniel P. Berrange 304b40
     if (slirp_inited) {
Daniel P. Berrange 304b40
@@ -6052,6 +6067,7 @@ void help(void)
Daniel P. Berrange 304b40
 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
Daniel P. Berrange 304b40
            "-g WxH[xDEPTH]  Set the initial graphical resolution and depth\n"
Daniel P. Berrange 304b40
 #endif
Daniel P. Berrange 304b40
+           "-name string    set the name of the guest\n"
Daniel P. Berrange 304b40
            "\n"
Daniel P. Berrange 304b40
            "Network options:\n"
Daniel P. Berrange 304b40
            "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
Daniel P. Berrange 304b40
@@ -6205,7 +6221,8 @@ enum {
Daniel P. Berrange 304b40
     QEMU_OPTION_no_reboot,
Daniel P. Berrange 304b40
     QEMU_OPTION_daemonize,
Daniel P. Berrange 304b40
     QEMU_OPTION_option_rom,
Daniel P. Berrange 304b40
-    QEMU_OPTION_semihosting
Daniel P. Berrange 304b40
+    QEMU_OPTION_semihosting,
Daniel P. Berrange 304b40
+    QEMU_OPTION_name,
Daniel P. Berrange 304b40
 };
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 typedef struct QEMUOption {
Daniel P. Berrange 304b40
@@ -6292,6 +6309,7 @@ const QEMUOption qemu_options[] = {
Daniel P. Berrange 304b40
 #if defined(TARGET_ARM)
Daniel P. Berrange 304b40
     { "semihosting", 0, QEMU_OPTION_semihosting },
Daniel P. Berrange 304b40
 #endif
Daniel P. Berrange 304b40
+    { "name", HAS_ARG, QEMU_OPTION_name },
Daniel P. Berrange 304b40
     { NULL },
Daniel P. Berrange 304b40
 };
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
@@ -6958,6 +6976,9 @@ int main(int argc, char **argv)
Daniel P. Berrange 304b40
             case QEMU_OPTION_semihosting:
Daniel P. Berrange 304b40
                 semihosting_enabled = 1;
Daniel P. Berrange 304b40
                 break;
Daniel P. Berrange 304b40
+            case QEMU_OPTION_name:
Daniel P. Berrange 304b40
+                qemu_name = optarg;
Daniel P. Berrange 304b40
+                break;
Daniel P. Berrange 304b40
             }
Daniel P. Berrange 304b40
         }
Daniel P. Berrange 304b40
     }
Daniel P. Berrange 304b40
@@ -7154,7 +7175,9 @@ int main(int argc, char **argv)
Daniel P. Berrange 304b40
     if (nographic) {
Daniel P. Berrange 304b40
         dumb_display_init(ds);
Daniel P. Berrange 304b40
     } else if (vnc_display != NULL) {
Daniel P. Berrange 304b40
-	vnc_display_init(ds, vnc_display);
Daniel P. Berrange 304b40
+        vnc_display_init(ds);
Daniel P. Berrange 304b40
+        if (vnc_display_open(ds, vnc_display) < 0)
Daniel P. Berrange 304b40
+            exit(1);
Daniel P. Berrange 304b40
     } else {
Daniel P. Berrange 304b40
 #if defined(CONFIG_SDL)
Daniel P. Berrange 304b40
         sdl_display_init(ds, full_screen);
Daniel P. Berrange 304b40
diff -rupN qemu-0.9.0.orig/vl.h qemu-0.9.0.new/vl.h
Daniel P. Berrange 304b40
--- qemu-0.9.0.orig/vl.h	2007-02-05 18:01:54.000000000 -0500
Daniel P. Berrange 304b40
+++ qemu-0.9.0.new/vl.h	2007-08-27 13:15:46.000000000 -0400
Daniel P. Berrange 304b40
@@ -114,6 +114,7 @@ void hw_error(const char *fmt, ...);
Daniel P. Berrange 304b40
 extern const char *bios_dir;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 extern int vm_running;
Daniel P. Berrange 304b40
+extern const char *qemu_name;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 typedef struct vm_change_state_entry VMChangeStateEntry;
Daniel P. Berrange 304b40
 typedef void VMChangeStateHandler(void *opaque, int running);
Daniel P. Berrange 304b40
@@ -908,7 +909,10 @@ void sdl_display_init(DisplayState *ds, 
Daniel P. Berrange 304b40
 void cocoa_display_init(DisplayState *ds, int full_screen);
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 /* vnc.c */
Daniel P. Berrange 304b40
-void vnc_display_init(DisplayState *ds, const char *display);
Daniel P. Berrange 304b40
+void vnc_display_init(DisplayState *ds);
Daniel P. Berrange 304b40
+void vnc_display_close(DisplayState *ds);
Daniel P. Berrange 304b40
+int vnc_display_open(DisplayState *ds, const char *display);
Daniel P. Berrange 304b40
+int vnc_display_password(DisplayState *ds, const char *password);
Daniel P. Berrange 304b40
 void do_info_vnc(void);
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 /* x_keymap.c */
Daniel P. Berrange 304b40
diff -rupN qemu-0.9.0.orig/vnc.c qemu-0.9.0.new/vnc.c
Daniel P. Berrange 304b40
--- qemu-0.9.0.orig/vnc.c	2007-02-05 18:01:54.000000000 -0500
Daniel P. Berrange 304b40
+++ qemu-0.9.0.new/vnc.c	2007-08-27 13:15:46.000000000 -0400
Daniel P. Berrange 304b40
@@ -30,6 +30,28 @@
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 #include "vnc_keysym.h"
Daniel P. Berrange 304b40
 #include "keymaps.c"
Daniel P. Berrange 304b40
+#include "d3des.h"
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+#include <gnutls/gnutls.h>
Daniel P. Berrange 304b40
+#include <gnutls/x509.h>
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+// #define _VNC_DEBUG 1
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#if _VNC_DEBUG
Daniel P. Berrange 304b40
+#define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS && _VNC_DEBUG >= 2
Daniel P. Berrange 304b40
+/* Very verbose, so only enabled for _VNC_DEBUG >= 2 */
Daniel P. Berrange 304b40
+static void vnc_debug_gnutls_log(int level, const char* str) {
Daniel P. Berrange 304b40
+    VNC_DEBUG("%d %s", level, str);
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS && _VNC_DEBUG */
Daniel P. Berrange 304b40
+#else
Daniel P. Berrange 304b40
+#define VNC_DEBUG(fmt, ...) do { } while (0)
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 typedef struct Buffer
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
@@ -54,6 +76,45 @@ typedef void VncSendHextileTile(VncState
Daniel P. Berrange 304b40
 #define VNC_MAX_HEIGHT 2048
Daniel P. Berrange 304b40
 #define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
+#define VNC_AUTH_CHALLENGE_SIZE 16
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+enum {
Daniel P. Berrange 304b40
+    VNC_AUTH_INVALID = 0,
Daniel P. Berrange 304b40
+    VNC_AUTH_NONE = 1,
Daniel P. Berrange 304b40
+    VNC_AUTH_VNC = 2,
Daniel P. Berrange 304b40
+    VNC_AUTH_RA2 = 5,
Daniel P. Berrange 304b40
+    VNC_AUTH_RA2NE = 6,
Daniel P. Berrange 304b40
+    VNC_AUTH_TIGHT = 16,
Daniel P. Berrange 304b40
+    VNC_AUTH_ULTRA = 17,
Daniel P. Berrange 304b40
+    VNC_AUTH_TLS = 18,
Daniel P. Berrange 304b40
+    VNC_AUTH_VENCRYPT = 19
Daniel P. Berrange 304b40
+};
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+enum {
Daniel P. Berrange 304b40
+    VNC_WIREMODE_CLEAR,
Daniel P. Berrange 304b40
+    VNC_WIREMODE_TLS,
Daniel P. Berrange 304b40
+};
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+enum {
Daniel P. Berrange 304b40
+    VNC_AUTH_VENCRYPT_PLAIN = 256,
Daniel P. Berrange 304b40
+    VNC_AUTH_VENCRYPT_TLSNONE = 257,
Daniel P. Berrange 304b40
+    VNC_AUTH_VENCRYPT_TLSVNC = 258,
Daniel P. Berrange 304b40
+    VNC_AUTH_VENCRYPT_TLSPLAIN = 259,
Daniel P. Berrange 304b40
+    VNC_AUTH_VENCRYPT_X509NONE = 260,
Daniel P. Berrange 304b40
+    VNC_AUTH_VENCRYPT_X509VNC = 261,
Daniel P. Berrange 304b40
+    VNC_AUTH_VENCRYPT_X509PLAIN = 262,
Daniel P. Berrange 304b40
+};
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+#define X509_CA_CERT_FILE "ca-cert.pem"
Daniel P. Berrange 304b40
+#define X509_CA_CRL_FILE "ca-crl.pem"
Daniel P. Berrange 304b40
+#define X509_SERVER_KEY_FILE "server-key.pem"
Daniel P. Berrange 304b40
+#define X509_SERVER_CERT_FILE "server-cert.pem"
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
 struct VncState
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
     QEMUTimer *timer;
Daniel P. Berrange 304b40
@@ -73,7 +134,27 @@ struct VncState
Daniel P. Berrange 304b40
     int last_x;
Daniel P. Berrange 304b40
     int last_y;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-    const char *display;
Daniel P. Berrange 304b40
+    int major;
Daniel P. Berrange 304b40
+    int minor;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    char *display;
Daniel P. Berrange 304b40
+    char *password;
Daniel P. Berrange 304b40
+    int auth;
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+    int subauth;
Daniel P. Berrange 304b40
+    int x509verify;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    char *x509cacert;
Daniel P. Berrange 304b40
+    char *x509cacrl;
Daniel P. Berrange 304b40
+    char *x509cert;
Daniel P. Berrange 304b40
+    char *x509key;
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+    char challenge[VNC_AUTH_CHALLENGE_SIZE];
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+    int wiremode;
Daniel P. Berrange 304b40
+    gnutls_session_t tls_session;
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     Buffer output;
Daniel P. Berrange 304b40
     Buffer input;
Daniel P. Berrange 304b40
@@ -550,12 +631,20 @@ static int vnc_client_io_error(VncState 
Daniel P. Berrange 304b40
 	if (ret == -1 && (last_errno == EINTR || last_errno == EAGAIN))
Daniel P. Berrange 304b40
 	    return 0;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
+	VNC_DEBUG("Closing down client sock %d %d\n", ret, ret < 0 ? last_errno : 0);
Daniel P. Berrange 304b40
 	qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
Daniel P. Berrange 304b40
 	closesocket(vs->csock);
Daniel P. Berrange 304b40
 	vs->csock = -1;
Daniel P. Berrange 304b40
 	buffer_reset(&vs->input);
Daniel P. Berrange 304b40
 	buffer_reset(&vs->output);
Daniel P. Berrange 304b40
 	vs->need_update = 0;
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+	if (vs->tls_session) {
Daniel P. Berrange 304b40
+	    gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+	    vs->tls_session = NULL;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+	vs->wiremode = VNC_WIREMODE_CLEAR;
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
 	return 0;
Daniel P. Berrange 304b40
     }
Daniel P. Berrange 304b40
     return ret;
Daniel P. Berrange 304b40
@@ -571,7 +660,19 @@ static void vnc_client_write(void *opaqu
Daniel P. Berrange 304b40
     long ret;
Daniel P. Berrange 304b40
     VncState *vs = opaque;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-    ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+    if (vs->tls_session) {
Daniel P. Berrange 304b40
+	ret = gnutls_write(vs->tls_session, vs->output.buffer, vs->output.offset);
Daniel P. Berrange 304b40
+	if (ret < 0) {
Daniel P. Berrange 304b40
+	    if (ret == GNUTLS_E_AGAIN)
Daniel P. Berrange 304b40
+		errno = EAGAIN;
Daniel P. Berrange 304b40
+	    else
Daniel P. Berrange 304b40
+		errno = EIO;
Daniel P. Berrange 304b40
+	    ret = -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+    } else
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
+	ret = send(vs->csock, vs->output.buffer, vs->output.offset, 0);
Daniel P. Berrange 304b40
     ret = vnc_client_io_error(vs, ret, socket_error());
Daniel P. Berrange 304b40
     if (!ret)
Daniel P. Berrange 304b40
 	return;
Daniel P. Berrange 304b40
@@ -597,7 +698,19 @@ static void vnc_client_read(void *opaque
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     buffer_reserve(&vs->input, 4096);
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-    ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0);
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+    if (vs->tls_session) {
Daniel P. Berrange 304b40
+	ret = gnutls_read(vs->tls_session, buffer_end(&vs->input), 4096);
Daniel P. Berrange 304b40
+	if (ret < 0) {
Daniel P. Berrange 304b40
+	    if (ret == GNUTLS_E_AGAIN)
Daniel P. Berrange 304b40
+		errno = EAGAIN;
Daniel P. Berrange 304b40
+	    else
Daniel P. Berrange 304b40
+		errno = EIO;
Daniel P. Berrange 304b40
+	    ret = -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+    } else
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
+	ret = recv(vs->csock, buffer_end(&vs->input), 4096, 0);
Daniel P. Berrange 304b40
     ret = vnc_client_io_error(vs, ret, socket_error());
Daniel P. Berrange 304b40
     if (!ret)
Daniel P. Berrange 304b40
 	return;
Daniel P. Berrange 304b40
@@ -692,6 +805,41 @@ static uint32_t read_u32(uint8_t *data, 
Daniel P. Berrange 304b40
 	    (data[offset + 2] << 8) | data[offset + 3]);
Daniel P. Berrange 304b40
 }
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+ssize_t vnc_tls_push(gnutls_transport_ptr_t transport,
Daniel P. Berrange 304b40
+		     const void *data,
Daniel P. Berrange 304b40
+		     size_t len) {
Daniel P. Berrange 304b40
+    struct VncState *vs = (struct VncState *)transport;
Daniel P. Berrange 304b40
+    int ret;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+ retry:
Daniel P. Berrange 304b40
+    ret = send(vs->csock, data, len, 0);
Daniel P. Berrange 304b40
+    if (ret < 0) {
Daniel P. Berrange 304b40
+	if (errno == EINTR)
Daniel P. Berrange 304b40
+	    goto retry;
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    return ret;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+ssize_t vnc_tls_pull(gnutls_transport_ptr_t transport,
Daniel P. Berrange 304b40
+		     void *data,
Daniel P. Berrange 304b40
+		     size_t len) {
Daniel P. Berrange 304b40
+    struct VncState *vs = (struct VncState *)transport;
Daniel P. Berrange 304b40
+    int ret;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+ retry:
Daniel P. Berrange 304b40
+    ret = recv(vs->csock, data, len, 0);
Daniel P. Berrange 304b40
+    if (ret < 0) {
Daniel P. Berrange 304b40
+	if (errno == EINTR)
Daniel P. Berrange 304b40
+	    goto retry;
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    return ret;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
 static void client_cut_text(VncState *vs, size_t len, char *text)
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
 }
Daniel P. Berrange 304b40
@@ -852,6 +1000,15 @@ static void framebuffer_update_request(V
Daniel P. Berrange 304b40
 				       int x_position, int y_position,
Daniel P. Berrange 304b40
 				       int w, int h)
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
+    if (x_position > vs->ds->width)
Daniel P. Berrange 304b40
+        x_position = vs->ds->width;
Daniel P. Berrange 304b40
+    if (y_position > vs->ds->height)
Daniel P. Berrange 304b40
+        y_position = vs->ds->height;
Daniel P. Berrange 304b40
+    if (x_position + w >= vs->ds->width)
Daniel P. Berrange 304b40
+        w = vs->ds->width  - x_position;
Daniel P. Berrange 304b40
+    if (y_position + h >= vs->ds->height)
Daniel P. Berrange 304b40
+        h = vs->ds->height - y_position;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
     int i;
Daniel P. Berrange 304b40
     vs->need_update = 1;
Daniel P. Berrange 304b40
     if (!incremental) {
Daniel P. Berrange 304b40
@@ -1056,6 +1213,8 @@ static int protocol_client_msg(VncState 
Daniel P. Berrange 304b40
 static int protocol_client_init(VncState *vs, char *data, size_t len)
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
     char pad[3] = { 0, 0, 0 };
Daniel P. Berrange 304b40
+    char buf[1024];
Daniel P. Berrange 304b40
+    int size;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     vs->width = vs->ds->width;
Daniel P. Berrange 304b40
     vs->height = vs->ds->height;
Daniel P. Berrange 304b40
@@ -1100,8 +1259,13 @@ static int protocol_client_init(VncState
Daniel P. Berrange 304b40
 	
Daniel P. Berrange 304b40
     vnc_write(vs, pad, 3);           /* padding */
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-    vnc_write_u32(vs, 4);        
Daniel P. Berrange 304b40
-    vnc_write(vs, "QEMU", 4);
Daniel P. Berrange 304b40
+    if (qemu_name)
Daniel P. Berrange 304b40
+        size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name);
Daniel P. Berrange 304b40
+    else
Daniel P. Berrange 304b40
+        size = snprintf(buf, sizeof(buf), "QEMU");
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    vnc_write_u32(vs, size);
Daniel P. Berrange 304b40
+    vnc_write(vs, buf, size);
Daniel P. Berrange 304b40
     vnc_flush(vs);
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     vnc_read_when(vs, protocol_client_msg, 1);
Daniel P. Berrange 304b40
@@ -1109,23 +1273,587 @@ static int protocol_client_init(VncState
Daniel P. Berrange 304b40
     return 0;
Daniel P. Berrange 304b40
 }
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
+static void make_challenge(VncState *vs)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    int i;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    srand(time(NULL)+getpid()+getpid()*987654+rand());
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    for (i = 0 ; i < sizeof(vs->challenge) ; i++)
Daniel P. Berrange 304b40
+        vs->challenge[i] = (int) (256.0*rand()/(RAND_MAX+1.0));
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int protocol_client_auth_vnc(VncState *vs, char *data, size_t len)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    char response[VNC_AUTH_CHALLENGE_SIZE];
Daniel P. Berrange 304b40
+    int i, j, pwlen;
Daniel P. Berrange 304b40
+    char key[8];
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (!vs->password || !vs->password[0]) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("No password configured on server");
Daniel P. Berrange 304b40
+	vnc_write_u32(vs, 1); /* Reject auth */
Daniel P. Berrange 304b40
+	if (vs->minor >= 8) {
Daniel P. Berrange 304b40
+	    static const char err[] = "Authentication failed";
Daniel P. Berrange 304b40
+	    vnc_write_u32(vs, sizeof(err));
Daniel P. Berrange 304b40
+	    vnc_write(vs, err, sizeof(err));
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+	vnc_flush(vs);
Daniel P. Berrange 304b40
+	vnc_client_error(vs);
Daniel P. Berrange 304b40
+	return 0;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    /* Calculate the expected challenge response */
Daniel P. Berrange 304b40
+    pwlen = strlen(vs->password);
Daniel P. Berrange 304b40
+    for (i=0; i
Daniel P. Berrange 304b40
+        key[i] = i<pwlen ? vs->password[i] : 0;
Daniel P. Berrange 304b40
+    deskey(key, EN0);
Daniel P. Berrange 304b40
+    for (j = 0; j < VNC_AUTH_CHALLENGE_SIZE; j += 8)
Daniel P. Berrange 304b40
+        des(response+j, response+j);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    /* Compare expected vs actual challenge response */
Daniel P. Berrange 304b40
+    if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Client challenge reponse did not match\n");
Daniel P. Berrange 304b40
+	vnc_write_u32(vs, 1); /* Reject auth */
Daniel P. Berrange 304b40
+	if (vs->minor >= 8) {
Daniel P. Berrange 304b40
+	    static const char err[] = "Authentication failed";
Daniel P. Berrange 304b40
+	    vnc_write_u32(vs, sizeof(err));
Daniel P. Berrange 304b40
+	    vnc_write(vs, err, sizeof(err));
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+	vnc_flush(vs);
Daniel P. Berrange 304b40
+	vnc_client_error(vs);
Daniel P. Berrange 304b40
+    } else {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Accepting VNC challenge response\n");
Daniel P. Berrange 304b40
+	vnc_write_u32(vs, 0); /* Accept auth */
Daniel P. Berrange 304b40
+	vnc_flush(vs);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	vnc_read_when(vs, protocol_client_init, 1);
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int start_auth_vnc(VncState *vs)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    make_challenge(vs);
Daniel P. Berrange 304b40
+    /* Send client a 'random' challenge */
Daniel P. Berrange 304b40
+    vnc_write(vs, vs->challenge, sizeof(vs->challenge));
Daniel P. Berrange 304b40
+    vnc_flush(vs);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge));
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+#define DH_BITS 1024
Daniel P. Berrange 304b40
+static gnutls_dh_params_t dh_params;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int vnc_tls_initialize(void)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    static int tlsinitialized = 0;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (tlsinitialized)
Daniel P. Berrange 304b40
+	return 1;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (gnutls_global_init () < 0)
Daniel P. Berrange 304b40
+	return 0;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    /* XXX ought to re-generate diffie-hellmen params periodically */
Daniel P. Berrange 304b40
+    if (gnutls_dh_params_init (&dh_params) < 0)
Daniel P. Berrange 304b40
+	return 0;
Daniel P. Berrange 304b40
+    if (gnutls_dh_params_generate2 (dh_params, DH_BITS) < 0)
Daniel P. Berrange 304b40
+	return 0;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#if _VNC_DEBUG == 2
Daniel P. Berrange 304b40
+    gnutls_global_set_log_level(10);
Daniel P. Berrange 304b40
+    gnutls_global_set_log_function(vnc_debug_gnutls_log);
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    tlsinitialized = 1;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    return 1;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static gnutls_anon_server_credentials vnc_tls_initialize_anon_cred(void)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    gnutls_anon_server_credentials anon_cred;
Daniel P. Berrange 304b40
+    int ret;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if ((ret = gnutls_anon_allocate_server_credentials(&anon_cred)) < 0) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
Daniel P. Berrange 304b40
+	return NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    gnutls_anon_set_server_dh_params(anon_cred, dh_params);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    return anon_cred;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static gnutls_certificate_credentials_t vnc_tls_initialize_x509_cred(VncState *vs)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    gnutls_certificate_credentials_t x509_cred;
Daniel P. Berrange 304b40
+    int ret;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (!vs->x509cacert) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("No CA x509 certificate specified\n");
Daniel P. Berrange 304b40
+	return NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    if (!vs->x509cert) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("No server x509 certificate specified\n");
Daniel P. Berrange 304b40
+	return NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    if (!vs->x509key) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("No server private key specified\n");
Daniel P. Berrange 304b40
+	return NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if ((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret));
Daniel P. Berrange 304b40
+	return NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    if ((ret = gnutls_certificate_set_x509_trust_file(x509_cred,
Daniel P. Berrange 304b40
+						      vs->x509cacert,
Daniel P. Berrange 304b40
+						      GNUTLS_X509_FMT_PEM)) < 0) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Cannot load CA certificate %s\n", gnutls_strerror(ret));
Daniel P. Berrange 304b40
+	gnutls_certificate_free_credentials(x509_cred);
Daniel P. Berrange 304b40
+	return NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if ((ret = gnutls_certificate_set_x509_key_file (x509_cred,
Daniel P. Berrange 304b40
+						     vs->x509cert,
Daniel P. Berrange 304b40
+						     vs->x509key,
Daniel P. Berrange 304b40
+						     GNUTLS_X509_FMT_PEM)) < 0) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Cannot load certificate & key %s\n", gnutls_strerror(ret));
Daniel P. Berrange 304b40
+	gnutls_certificate_free_credentials(x509_cred);
Daniel P. Berrange 304b40
+	return NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (vs->x509cacrl) {
Daniel P. Berrange 304b40
+	if ((ret = gnutls_certificate_set_x509_crl_file(x509_cred,
Daniel P. Berrange 304b40
+							vs->x509cacrl,
Daniel P. Berrange 304b40
+							GNUTLS_X509_FMT_PEM)) < 0) {
Daniel P. Berrange 304b40
+	    VNC_DEBUG("Cannot load CRL %s\n", gnutls_strerror(ret));
Daniel P. Berrange 304b40
+	    gnutls_certificate_free_credentials(x509_cred);
Daniel P. Berrange 304b40
+	    return NULL;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    gnutls_certificate_set_dh_params (x509_cred, dh_params);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    return x509_cred;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int vnc_validate_certificate(struct VncState *vs)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    int ret;
Daniel P. Berrange 304b40
+    unsigned int status;
Daniel P. Berrange 304b40
+    const gnutls_datum_t *certs;
Daniel P. Berrange 304b40
+    unsigned int nCerts, i;
Daniel P. Berrange 304b40
+    time_t now;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    VNC_DEBUG("Validating client certificate\n");
Daniel P. Berrange 304b40
+    if ((ret = gnutls_certificate_verify_peers2 (vs->tls_session, &status)) < 0) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Verify failed %s\n", gnutls_strerror(ret));
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if ((now = time(NULL)) == ((time_t)-1)) {
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (status != 0) {
Daniel P. Berrange 304b40
+	if (status & GNUTLS_CERT_INVALID)
Daniel P. Berrange 304b40
+	    VNC_DEBUG("The certificate is not trusted.\n");
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
Daniel P. Berrange 304b40
+	    VNC_DEBUG("The certificate hasn't got a known issuer.\n");
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (status & GNUTLS_CERT_REVOKED)
Daniel P. Berrange 304b40
+	    VNC_DEBUG("The certificate has been revoked.\n");
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
Daniel P. Berrange 304b40
+	    VNC_DEBUG("The certificate uses an insecure algorithm\n");
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+    } else {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Certificate is valid!\n");
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    /* Only support x509 for now */
Daniel P. Berrange 304b40
+    if (gnutls_certificate_type_get(vs->tls_session) != GNUTLS_CRT_X509)
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (!(certs = gnutls_certificate_get_peers(vs->tls_session, &nCerts)))
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    for (i = 0 ; i < nCerts ; i++) {
Daniel P. Berrange 304b40
+	gnutls_x509_crt_t cert;
Daniel P. Berrange 304b40
+	VNC_DEBUG ("Checking certificate chain %d\n", i);
Daniel P. Berrange 304b40
+	if (gnutls_x509_crt_init (&cert) < 0)
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (gnutls_x509_crt_import(cert, &certs[i], GNUTLS_X509_FMT_DER) < 0) {
Daniel P. Berrange 304b40
+	    gnutls_x509_crt_deinit (cert);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (gnutls_x509_crt_get_expiration_time (cert) < now) {
Daniel P. Berrange 304b40
+	    VNC_DEBUG("The certificate has expired\n");
Daniel P. Berrange 304b40
+	    gnutls_x509_crt_deinit (cert);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (gnutls_x509_crt_get_activation_time (cert) > now) {
Daniel P. Berrange 304b40
+	    VNC_DEBUG("The certificate is not yet activated\n");
Daniel P. Berrange 304b40
+	    gnutls_x509_crt_deinit (cert);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (gnutls_x509_crt_get_activation_time (cert) > now) {
Daniel P. Berrange 304b40
+	    VNC_DEBUG("The certificate is not yet activated\n");
Daniel P. Berrange 304b40
+	    gnutls_x509_crt_deinit (cert);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	gnutls_x509_crt_deinit (cert);
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int start_auth_vencrypt_subauth(VncState *vs)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    switch (vs->subauth) {
Daniel P. Berrange 304b40
+    case VNC_AUTH_VENCRYPT_TLSNONE:
Daniel P. Berrange 304b40
+    case VNC_AUTH_VENCRYPT_X509NONE:
Daniel P. Berrange 304b40
+       VNC_DEBUG("Accept TLS auth none\n");
Daniel P. Berrange 304b40
+       vnc_write_u32(vs, 0); /* Accept auth completion */
Daniel P. Berrange 304b40
+       vnc_read_when(vs, protocol_client_init, 1);
Daniel P. Berrange 304b40
+       break;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    case VNC_AUTH_VENCRYPT_TLSVNC:
Daniel P. Berrange 304b40
+    case VNC_AUTH_VENCRYPT_X509VNC:
Daniel P. Berrange 304b40
+       VNC_DEBUG("Start TLS auth VNC\n");
Daniel P. Berrange 304b40
+       return start_auth_vnc(vs);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    default: /* Should not be possible, but just in case */
Daniel P. Berrange 304b40
+       VNC_DEBUG("Reject auth %d\n", vs->auth);
Daniel P. Berrange 304b40
+       vnc_write_u8(vs, 1);
Daniel P. Berrange 304b40
+       if (vs->minor >= 8) {
Daniel P. Berrange 304b40
+           static const char err[] = "Unsupported authentication type";
Daniel P. Berrange 304b40
+           vnc_write_u32(vs, sizeof(err));
Daniel P. Berrange 304b40
+           vnc_write(vs, err, sizeof(err));
Daniel P. Berrange 304b40
+       }
Daniel P. Berrange 304b40
+       vnc_client_error(vs);
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static void vnc_handshake_io(void *opaque);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int vnc_continue_handshake(struct VncState *vs) {
Daniel P. Berrange 304b40
+    int ret;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if ((ret = gnutls_handshake(vs->tls_session)) < 0) {
Daniel P. Berrange 304b40
+       if (!gnutls_error_is_fatal(ret)) {
Daniel P. Berrange 304b40
+           VNC_DEBUG("Handshake interrupted (blocking)\n");
Daniel P. Berrange 304b40
+           if (!gnutls_record_get_direction(vs->tls_session))
Daniel P. Berrange 304b40
+               qemu_set_fd_handler(vs->csock, vnc_handshake_io, NULL, vs);
Daniel P. Berrange 304b40
+           else
Daniel P. Berrange 304b40
+               qemu_set_fd_handler(vs->csock, NULL, vnc_handshake_io, vs);
Daniel P. Berrange 304b40
+           return 0;
Daniel P. Berrange 304b40
+       }
Daniel P. Berrange 304b40
+       VNC_DEBUG("Handshake failed %s\n", gnutls_strerror(ret));
Daniel P. Berrange 304b40
+       vnc_client_error(vs);
Daniel P. Berrange 304b40
+       return -1;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (vs->x509verify) {
Daniel P. Berrange 304b40
+	if (vnc_validate_certificate(vs) < 0) {
Daniel P. Berrange 304b40
+	    VNC_DEBUG("Client verification failed\n");
Daniel P. Berrange 304b40
+	    vnc_client_error(vs);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	} else {
Daniel P. Berrange 304b40
+	    VNC_DEBUG("Client verification passed\n");
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    VNC_DEBUG("Handshake done, switching to TLS data mode\n");
Daniel P. Berrange 304b40
+    vs->wiremode = VNC_WIREMODE_TLS;
Daniel P. Berrange 304b40
+    qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    return start_auth_vencrypt_subauth(vs);
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static void vnc_handshake_io(void *opaque) {
Daniel P. Berrange 304b40
+    struct VncState *vs = (struct VncState *)opaque;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    VNC_DEBUG("Handshake IO continue\n");
Daniel P. Berrange 304b40
+    vnc_continue_handshake(vs);
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#define NEED_X509_AUTH(vs)			      \
Daniel P. Berrange 304b40
+    ((vs)->subauth == VNC_AUTH_VENCRYPT_X509NONE ||   \
Daniel P. Berrange 304b40
+     (vs)->subauth == VNC_AUTH_VENCRYPT_X509VNC ||    \
Daniel P. Berrange 304b40
+     (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN)
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int vnc_start_tls(struct VncState *vs) {
Daniel P. Berrange 304b40
+    static const int cert_type_priority[] = { GNUTLS_CRT_X509, 0 };
Daniel P. Berrange 304b40
+    static const int protocol_priority[]= { GNUTLS_TLS1_1, GNUTLS_TLS1_0, GNUTLS_SSL3, 0 };
Daniel P. Berrange 304b40
+    static const int kx_anon[] = {GNUTLS_KX_ANON_DH, 0};
Daniel P. Berrange 304b40
+    static const int kx_x509[] = {GNUTLS_KX_DHE_DSS, GNUTLS_KX_RSA, GNUTLS_KX_DHE_RSA, GNUTLS_KX_SRP, 0};
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    VNC_DEBUG("Do TLS setup\n");
Daniel P. Berrange 304b40
+    if (vnc_tls_initialize() < 0) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Failed to init TLS\n");
Daniel P. Berrange 304b40
+	vnc_client_error(vs);
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    if (vs->tls_session == NULL) {
Daniel P. Berrange 304b40
+	if (gnutls_init(&vs->tls_session, GNUTLS_SERVER) < 0) {
Daniel P. Berrange 304b40
+	    vnc_client_error(vs);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (gnutls_set_default_priority(vs->tls_session) < 0) {
Daniel P. Berrange 304b40
+	    gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+	    vs->tls_session = NULL;
Daniel P. Berrange 304b40
+	    vnc_client_error(vs);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (gnutls_kx_set_priority(vs->tls_session, NEED_X509_AUTH(vs) ? kx_x509 : kx_anon) < 0) {
Daniel P. Berrange 304b40
+	    gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+	    vs->tls_session = NULL;
Daniel P. Berrange 304b40
+	    vnc_client_error(vs);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (gnutls_certificate_type_set_priority(vs->tls_session, cert_type_priority) < 0) {
Daniel P. Berrange 304b40
+	    gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+	    vs->tls_session = NULL;
Daniel P. Berrange 304b40
+	    vnc_client_error(vs);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (gnutls_protocol_set_priority(vs->tls_session, protocol_priority) < 0) {
Daniel P. Berrange 304b40
+	    gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+	    vs->tls_session = NULL;
Daniel P. Berrange 304b40
+	    vnc_client_error(vs);
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (NEED_X509_AUTH(vs)) {
Daniel P. Berrange 304b40
+	    gnutls_certificate_server_credentials x509_cred = vnc_tls_initialize_x509_cred(vs);
Daniel P. Berrange 304b40
+	    if (!x509_cred) {
Daniel P. Berrange 304b40
+		gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+		vs->tls_session = NULL;
Daniel P. Berrange 304b40
+		vnc_client_error(vs);
Daniel P. Berrange 304b40
+		return -1;
Daniel P. Berrange 304b40
+	    }
Daniel P. Berrange 304b40
+	    if (gnutls_credentials_set(vs->tls_session, GNUTLS_CRD_CERTIFICATE, x509_cred) < 0) {
Daniel P. Berrange 304b40
+		gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+		vs->tls_session = NULL;
Daniel P. Berrange 304b40
+		gnutls_certificate_free_credentials(x509_cred);
Daniel P. Berrange 304b40
+		vnc_client_error(vs);
Daniel P. Berrange 304b40
+		return -1;
Daniel P. Berrange 304b40
+	    }
Daniel P. Berrange 304b40
+	    if (vs->x509verify) {
Daniel P. Berrange 304b40
+		VNC_DEBUG("Requesting a client certificate\n");
Daniel P. Berrange 304b40
+		gnutls_certificate_server_set_request (vs->tls_session, GNUTLS_CERT_REQUEST);
Daniel P. Berrange 304b40
+	    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	} else {
Daniel P. Berrange 304b40
+	    gnutls_anon_server_credentials anon_cred = vnc_tls_initialize_anon_cred();
Daniel P. Berrange 304b40
+	    if (!anon_cred) {
Daniel P. Berrange 304b40
+		gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+		vs->tls_session = NULL;
Daniel P. Berrange 304b40
+		vnc_client_error(vs);
Daniel P. Berrange 304b40
+		return -1;
Daniel P. Berrange 304b40
+	    }
Daniel P. Berrange 304b40
+	    if (gnutls_credentials_set(vs->tls_session, GNUTLS_CRD_ANON, anon_cred) < 0) {
Daniel P. Berrange 304b40
+		gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+		vs->tls_session = NULL;
Daniel P. Berrange 304b40
+		gnutls_anon_free_server_credentials(anon_cred);
Daniel P. Berrange 304b40
+		vnc_client_error(vs);
Daniel P. Berrange 304b40
+		return -1;
Daniel P. Berrange 304b40
+	    }
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	gnutls_transport_set_ptr(vs->tls_session, (gnutls_transport_ptr_t)vs);
Daniel P. Berrange 304b40
+	gnutls_transport_set_push_function(vs->tls_session, vnc_tls_push);
Daniel P. Berrange 304b40
+	gnutls_transport_set_pull_function(vs->tls_session, vnc_tls_pull);
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    VNC_DEBUG("Start TLS handshake process\n");
Daniel P. Berrange 304b40
+    return vnc_continue_handshake(vs);
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int protocol_client_vencrypt_auth(VncState *vs, char *data, size_t len)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    int auth = read_u32(data, 0);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (auth != vs->subauth) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Rejecting auth %d\n", auth);
Daniel P. Berrange 304b40
+	vnc_write_u8(vs, 0); /* Reject auth */
Daniel P. Berrange 304b40
+	vnc_flush(vs);
Daniel P. Berrange 304b40
+	vnc_client_error(vs);
Daniel P. Berrange 304b40
+    } else {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Accepting auth %d, starting handshake\n", auth);
Daniel P. Berrange 304b40
+	vnc_write_u8(vs, 1); /* Accept auth */
Daniel P. Berrange 304b40
+	vnc_flush(vs);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (vnc_start_tls(vs) < 0) {
Daniel P. Berrange 304b40
+	    VNC_DEBUG("Failed to complete TLS\n");
Daniel P. Berrange 304b40
+	    return 0;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	if (vs->wiremode == VNC_WIREMODE_TLS) {
Daniel P. Berrange 304b40
+	    VNC_DEBUG("Starting VeNCrypt subauth\n");
Daniel P. Berrange 304b40
+	    return start_auth_vencrypt_subauth(vs);
Daniel P. Berrange 304b40
+	} else {
Daniel P. Berrange 304b40
+	    VNC_DEBUG("TLS handshake blocked\n");
Daniel P. Berrange 304b40
+	    return 0;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int protocol_client_vencrypt_init(VncState *vs, char *data, size_t len)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    if (data[0] != 0 ||
Daniel P. Berrange 304b40
+	data[1] != 2) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data[0], (int)data[1]);
Daniel P. Berrange 304b40
+	vnc_write_u8(vs, 1); /* Reject version */
Daniel P. Berrange 304b40
+	vnc_flush(vs);
Daniel P. Berrange 304b40
+	vnc_client_error(vs);
Daniel P. Berrange 304b40
+    } else {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Sending allowed auth %d\n", vs->subauth);
Daniel P. Berrange 304b40
+	vnc_write_u8(vs, 0); /* Accept version */
Daniel P. Berrange 304b40
+	vnc_write_u8(vs, 1); /* Number of sub-auths */
Daniel P. Berrange 304b40
+	vnc_write_u32(vs, vs->subauth); /* The supported auth */
Daniel P. Berrange 304b40
+	vnc_flush(vs);
Daniel P. Berrange 304b40
+	vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int start_auth_vencrypt(VncState *vs)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    /* Send VeNCrypt version 0.2 */
Daniel P. Berrange 304b40
+    vnc_write_u8(vs, 0);
Daniel P. Berrange 304b40
+    vnc_write_u8(vs, 2);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    vnc_read_when(vs, protocol_client_vencrypt_init, 2);
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int protocol_client_auth(VncState *vs, char *data, size_t len)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    /* We only advertise 1 auth scheme at a time, so client
Daniel P. Berrange 304b40
+     * must pick the one we sent. Verify this */
Daniel P. Berrange 304b40
+    if (data[0] != vs->auth) { /* Reject auth */
Daniel P. Berrange 304b40
+       VNC_DEBUG("Reject auth %d\n", (int)data[0]);
Daniel P. Berrange 304b40
+       vnc_write_u32(vs, 1);
Daniel P. Berrange 304b40
+       if (vs->minor >= 8) {
Daniel P. Berrange 304b40
+           static const char err[] = "Authentication failed";
Daniel P. Berrange 304b40
+           vnc_write_u32(vs, sizeof(err));
Daniel P. Berrange 304b40
+           vnc_write(vs, err, sizeof(err));
Daniel P. Berrange 304b40
+       }
Daniel P. Berrange 304b40
+       vnc_client_error(vs);
Daniel P. Berrange 304b40
+    } else { /* Accept requested auth */
Daniel P. Berrange 304b40
+       VNC_DEBUG("Client requested auth %d\n", (int)data[0]);
Daniel P. Berrange 304b40
+       switch (vs->auth) {
Daniel P. Berrange 304b40
+       case VNC_AUTH_NONE:
Daniel P. Berrange 304b40
+           VNC_DEBUG("Accept auth none\n");
Daniel P. Berrange 304b40
+           vnc_write_u32(vs, 0); /* Accept auth completion */
Daniel P. Berrange 304b40
+           vnc_read_when(vs, protocol_client_init, 1);
Daniel P. Berrange 304b40
+           break;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+       case VNC_AUTH_VNC:
Daniel P. Berrange 304b40
+           VNC_DEBUG("Start VNC auth\n");
Daniel P. Berrange 304b40
+           return start_auth_vnc(vs);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+       case VNC_AUTH_VENCRYPT:
Daniel P. Berrange 304b40
+           VNC_DEBUG("Accept VeNCrypt auth\n");;
Daniel P. Berrange 304b40
+           return start_auth_vencrypt(vs);
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+       default: /* Should not be possible, but just in case */
Daniel P. Berrange 304b40
+           VNC_DEBUG("Reject auth %d\n", vs->auth);
Daniel P. Berrange 304b40
+           vnc_write_u8(vs, 1);
Daniel P. Berrange 304b40
+           if (vs->minor >= 8) {
Daniel P. Berrange 304b40
+               static const char err[] = "Authentication failed";
Daniel P. Berrange 304b40
+               vnc_write_u32(vs, sizeof(err));
Daniel P. Berrange 304b40
+               vnc_write(vs, err, sizeof(err));
Daniel P. Berrange 304b40
+           }
Daniel P. Berrange 304b40
+           vnc_client_error(vs);
Daniel P. Berrange 304b40
+       }
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
 static int protocol_version(VncState *vs, char *version, size_t len)
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
     char local[13];
Daniel P. Berrange 304b40
-    int maj, min;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     memcpy(local, version, 12);
Daniel P. Berrange 304b40
     local[12] = 0;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-    if (sscanf(local, "RFB %03d.%03d\n", &maj, &min) != 2) {
Daniel P. Berrange 304b40
+    if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Malformed protocol version %s\n", local);
Daniel P. Berrange 304b40
 	vnc_client_error(vs);
Daniel P. Berrange 304b40
 	return 0;
Daniel P. Berrange 304b40
     }
Daniel P. Berrange 304b40
-
Daniel P. Berrange 304b40
-    vnc_write_u32(vs, 1); /* None */
Daniel P. Berrange 304b40
-    vnc_flush(vs);
Daniel P. Berrange 304b40
-
Daniel P. Berrange 304b40
-    vnc_read_when(vs, protocol_client_init, 1);
Daniel P. Berrange 304b40
+    VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor);
Daniel P. Berrange 304b40
+    if (vs->major != 3 ||
Daniel P. Berrange 304b40
+	(vs->minor != 3 &&
Daniel P. Berrange 304b40
+	 vs->minor != 5 &&
Daniel P. Berrange 304b40
+	 vs->minor != 7 &&
Daniel P. Berrange 304b40
+	 vs->minor != 8)) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Unsupported client version\n");
Daniel P. Berrange 304b40
+	vnc_write_u32(vs, VNC_AUTH_INVALID);
Daniel P. Berrange 304b40
+	vnc_flush(vs);
Daniel P. Berrange 304b40
+	vnc_client_error(vs);
Daniel P. Berrange 304b40
+	return 0;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    /* Some broken client report v3.5 which spec requires to be treated
Daniel P. Berrange 304b40
+     * as equivalent to v3.3 by servers
Daniel P. Berrange 304b40
+     */
Daniel P. Berrange 304b40
+    if (vs->minor == 5)
Daniel P. Berrange 304b40
+	vs->minor = 3;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (vs->minor == 3) {
Daniel P. Berrange 304b40
+	if (vs->auth == VNC_AUTH_NONE) {
Daniel P. Berrange 304b40
+            VNC_DEBUG("Tell client auth none\n");
Daniel P. Berrange 304b40
+            vnc_write_u32(vs, vs->auth);
Daniel P. Berrange 304b40
+            vnc_flush(vs);
Daniel P. Berrange 304b40
+            vnc_read_when(vs, protocol_client_init, 1);
Daniel P. Berrange 304b40
+       } else if (vs->auth == VNC_AUTH_VNC) {
Daniel P. Berrange 304b40
+            VNC_DEBUG("Tell client VNC auth\n");
Daniel P. Berrange 304b40
+            vnc_write_u32(vs, vs->auth);
Daniel P. Berrange 304b40
+            vnc_flush(vs);
Daniel P. Berrange 304b40
+            start_auth_vnc(vs);
Daniel P. Berrange 304b40
+       } else {
Daniel P. Berrange 304b40
+            VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs->auth);
Daniel P. Berrange 304b40
+            vnc_write_u32(vs, VNC_AUTH_INVALID);
Daniel P. Berrange 304b40
+            vnc_flush(vs);
Daniel P. Berrange 304b40
+            vnc_client_error(vs);
Daniel P. Berrange 304b40
+       }
Daniel P. Berrange 304b40
+    } else {
Daniel P. Berrange 304b40
+	VNC_DEBUG("Telling client we support auth %d\n", vs->auth);
Daniel P. Berrange 304b40
+	vnc_write_u8(vs, 1); /* num auth */
Daniel P. Berrange 304b40
+	vnc_write_u8(vs, vs->auth);
Daniel P. Berrange 304b40
+	vnc_read_when(vs, protocol_client_auth, 1);
Daniel P. Berrange 304b40
+	vnc_flush(vs);
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     return 0;
Daniel P. Berrange 304b40
 }
Daniel P. Berrange 304b40
@@ -1138,9 +1866,10 @@ static void vnc_listen_read(void *opaque
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     vs->csock = accept(vs->lsock, (struct sockaddr *)&addr, &addrlen);
Daniel P. Berrange 304b40
     if (vs->csock != -1) {
Daniel P. Berrange 304b40
+	VNC_DEBUG("New client on socket %d\n", vs->csock);
Daniel P. Berrange 304b40
         socket_set_nonblock(vs->csock);
Daniel P. Berrange 304b40
 	qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, opaque);
Daniel P. Berrange 304b40
-	vnc_write(vs, "RFB 003.003\n", 12);
Daniel P. Berrange 304b40
+	vnc_write(vs, "RFB 003.008\n", 12);
Daniel P. Berrange 304b40
 	vnc_flush(vs);
Daniel P. Berrange 304b40
 	vnc_read_when(vs, protocol_version, 12);
Daniel P. Berrange 304b40
 	memset(vs->old_data, 0, vs->ds->linesize * vs->ds->height);
Daniel P. Berrange 304b40
@@ -1153,16 +1882,8 @@ static void vnc_listen_read(void *opaque
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 extern int parse_host_port(struct sockaddr_in *saddr, const char *str);
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-void vnc_display_init(DisplayState *ds, const char *arg)
Daniel P. Berrange 304b40
+void vnc_display_init(DisplayState *ds)
Daniel P. Berrange 304b40
 {
Daniel P. Berrange 304b40
-    struct sockaddr *addr;
Daniel P. Berrange 304b40
-    struct sockaddr_in iaddr;
Daniel P. Berrange 304b40
-#ifndef _WIN32
Daniel P. Berrange 304b40
-    struct sockaddr_un uaddr;
Daniel P. Berrange 304b40
-#endif
Daniel P. Berrange 304b40
-    int reuse_addr, ret;
Daniel P. Berrange 304b40
-    socklen_t addrlen;
Daniel P. Berrange 304b40
-    const char *p;
Daniel P. Berrange 304b40
     VncState *vs;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     vs = qemu_mallocz(sizeof(VncState));
Daniel P. Berrange 304b40
@@ -1171,7 +1892,8 @@ void vnc_display_init(DisplayState *ds, 
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     ds->opaque = vs;
Daniel P. Berrange 304b40
     vnc_state = vs;
Daniel P. Berrange 304b40
-    vs->display = arg;
Daniel P. Berrange 304b40
+    vs->display = NULL;
Daniel P. Berrange 304b40
+    vs->password = NULL;
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     vs->lsock = -1;
Daniel P. Berrange 304b40
     vs->csock = -1;
Daniel P. Berrange 304b40
@@ -1196,16 +1918,232 @@ void vnc_display_init(DisplayState *ds, 
Daniel P. Berrange 304b40
     memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     vnc_dpy_resize(vs->ds, 640, 400);
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+static int vnc_set_x509_credential(VncState *vs,
Daniel P. Berrange 304b40
+				   const char *certdir,
Daniel P. Berrange 304b40
+				   const char *filename,
Daniel P. Berrange 304b40
+				   char **cred,
Daniel P. Berrange 304b40
+				   int ignoreMissing)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    struct stat sb;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (*cred) {
Daniel P. Berrange 304b40
+	qemu_free(*cred);
Daniel P. Berrange 304b40
+	*cred = NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (!(*cred = qemu_malloc(strlen(certdir) + strlen(filename) + 2)))
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    strcpy(*cred, certdir);
Daniel P. Berrange 304b40
+    strcat(*cred, "/");
Daniel P. Berrange 304b40
+    strcat(*cred, filename);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    VNC_DEBUG("Check %s\n", *cred);
Daniel P. Berrange 304b40
+    if (stat(*cred, &sb) < 0) {
Daniel P. Berrange 304b40
+	qemu_free(*cred);
Daniel P. Berrange 304b40
+	*cred = NULL;
Daniel P. Berrange 304b40
+	if (ignoreMissing && errno == ENOENT)
Daniel P. Berrange 304b40
+	    return 0;
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+static int vnc_set_x509_credential_dir(VncState *vs,
Daniel P. Berrange 304b40
+				       const char *certdir)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    if (vnc_set_x509_credential(vs, certdir, X509_CA_CERT_FILE, &vs->x509cacert, 0) < 0)
Daniel P. Berrange 304b40
+	goto cleanup;
Daniel P. Berrange 304b40
+    if (vnc_set_x509_credential(vs, certdir, X509_CA_CRL_FILE, &vs->x509cacrl, 1) < 0)
Daniel P. Berrange 304b40
+	goto cleanup;
Daniel P. Berrange 304b40
+    if (vnc_set_x509_credential(vs, certdir, X509_SERVER_CERT_FILE, &vs->x509cert, 0) < 0)
Daniel P. Berrange 304b40
+	goto cleanup;
Daniel P. Berrange 304b40
+    if (vnc_set_x509_credential(vs, certdir, X509_SERVER_KEY_FILE, &vs->x509key, 0) < 0)
Daniel P. Berrange 304b40
+	goto cleanup;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+ cleanup:
Daniel P. Berrange 304b40
+    qemu_free(vs->x509cacert);
Daniel P. Berrange 304b40
+    qemu_free(vs->x509cacrl);
Daniel P. Berrange 304b40
+    qemu_free(vs->x509cert);
Daniel P. Berrange 304b40
+    qemu_free(vs->x509key);
Daniel P. Berrange 304b40
+    vs->x509cacert = vs->x509cacrl = vs->x509cert = vs->x509key = NULL;
Daniel P. Berrange 304b40
+    return -1;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+void vnc_display_close(DisplayState *ds)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    VncState *vs = ds ? (VncState *)ds->opaque : vnc_state;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (vs->display) {
Daniel P. Berrange 304b40
+	qemu_free(vs->display);
Daniel P. Berrange 304b40
+	vs->display = NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    if (vs->lsock != -1) {
Daniel P. Berrange 304b40
+	qemu_set_fd_handler2(vs->lsock, NULL, NULL, NULL, NULL);
Daniel P. Berrange 304b40
+	close(vs->lsock);
Daniel P. Berrange 304b40
+	vs->lsock = -1;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    if (vs->csock != -1) {
Daniel P. Berrange 304b40
+	qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
Daniel P. Berrange 304b40
+	closesocket(vs->csock);
Daniel P. Berrange 304b40
+	vs->csock = -1;
Daniel P. Berrange 304b40
+	buffer_reset(&vs->input);
Daniel P. Berrange 304b40
+	buffer_reset(&vs->output);
Daniel P. Berrange 304b40
+	vs->need_update = 0;
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+	if (vs->tls_session) {
Daniel P. Berrange 304b40
+	    gnutls_deinit(vs->tls_session);
Daniel P. Berrange 304b40
+	    vs->tls_session = NULL;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+	vs->wiremode = VNC_WIREMODE_CLEAR;
Daniel P. Berrange 304b40
+#endif /* CONFIG_VNC_TLS */
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    vs->auth = VNC_AUTH_INVALID;
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+    vs->subauth = VNC_AUTH_INVALID;
Daniel P. Berrange 304b40
+    vs->x509verify = 0;
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+int vnc_display_password(DisplayState *ds, const char *password)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    VncState *vs = ds ? (VncState *)ds->opaque : vnc_state;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (vs->password) {
Daniel P. Berrange 304b40
+	qemu_free(vs->password);
Daniel P. Berrange 304b40
+	vs->password = NULL;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+    if (password && password[0]) {
Daniel P. Berrange 304b40
+	if (!(vs->password = qemu_strdup(password)))
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    return 0;
Daniel P. Berrange 304b40
+}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+int vnc_display_open(DisplayState *ds, const char *display)
Daniel P. Berrange 304b40
+{
Daniel P. Berrange 304b40
+    struct sockaddr *addr;
Daniel P. Berrange 304b40
+    struct sockaddr_in iaddr;
Daniel P. Berrange 304b40
+#ifndef _WIN32
Daniel P. Berrange 304b40
+    struct sockaddr_un uaddr;
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+    int reuse_addr, ret;
Daniel P. Berrange 304b40
+    socklen_t addrlen;
Daniel P. Berrange 304b40
+    const char *p;
Daniel P. Berrange 304b40
+    VncState *vs = ds ? (VncState *)ds->opaque : vnc_state;
Daniel P. Berrange 304b40
+    const char *options;
Daniel P. Berrange 304b40
+    int password = 0;
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+    int tls = 0, x509 = 0;
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    vnc_display_close(ds);
Daniel P. Berrange 304b40
+    if (strcmp(display, "none") == 0)
Daniel P. Berrange 304b40
+	return 0;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (!(vs->display = strdup(display)))
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    options = display;
Daniel P. Berrange 304b40
+    while ((options = strchr(options, ','))) {
Daniel P. Berrange 304b40
+	options++;
Daniel P. Berrange 304b40
+	if (strncmp(options, "password", 8) == 0) {
Daniel P. Berrange 304b40
+	    password = 1; /* Require password auth */
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+	} else if (strncmp(options, "tls", 3) == 0) {
Daniel P. Berrange 304b40
+	    tls = 1; /* Require TLS */
Daniel P. Berrange 304b40
+	} else if (strncmp(options, "x509", 4) == 0) {
Daniel P. Berrange 304b40
+	    char *start, *end;
Daniel P. Berrange 304b40
+	    x509 = 1; /* Require x509 certificates */
Daniel P. Berrange 304b40
+	    if (strncmp(options, "x509verify", 10) == 0)
Daniel P. Berrange 304b40
+	        vs->x509verify = 1; /* ...and verify client certs */
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	    /* Now check for 'x509=/some/path' postfix
Daniel P. Berrange 304b40
+	     * and use that to setup x509 certificate/key paths */
Daniel P. Berrange 304b40
+	    start = strchr(options, '=');
Daniel P. Berrange 304b40
+	    end = strchr(options, ',');
Daniel P. Berrange 304b40
+	    if (start && (!end || (start < end))) {
Daniel P. Berrange 304b40
+		int len = end ? end-(start+1) : strlen(start+1);
Daniel P. Berrange 304b40
+		char *path = qemu_malloc(len+1);
Daniel P. Berrange 304b40
+		strncpy(path, start+1, len);
Daniel P. Berrange 304b40
+		path[len] = '\0';
Daniel P. Berrange 304b40
+		VNC_DEBUG("Trying certificate path '%s'\n", path);
Daniel P. Berrange 304b40
+		if (vnc_set_x509_credential_dir(vs, path) < 0) {
Daniel P. Berrange 304b40
+		    fprintf(stderr, "Failed to find x509 certificates/keys in %s\n", path);
Daniel P. Berrange 304b40
+		    qemu_free(path);
Daniel P. Berrange 304b40
+		    qemu_free(vs->display);
Daniel P. Berrange 304b40
+		    vs->display = NULL;
Daniel P. Berrange 304b40
+		    return -1;
Daniel P. Berrange 304b40
+		}
Daniel P. Berrange 304b40
+		qemu_free(path);
Daniel P. Berrange 304b40
+	    } else {
Daniel P. Berrange 304b40
+		fprintf(stderr, "No certificate path provided\n");
Daniel P. Berrange 304b40
+		qemu_free(vs->display);
Daniel P. Berrange 304b40
+		vs->display = NULL;
Daniel P. Berrange 304b40
+		return -1;
Daniel P. Berrange 304b40
+	    }
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+    if (password) {
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+	if (tls) {
Daniel P. Berrange 304b40
+	    vs->auth = VNC_AUTH_VENCRYPT;
Daniel P. Berrange 304b40
+	    if (x509) {
Daniel P. Berrange 304b40
+		VNC_DEBUG("Initializing VNC server with x509 password auth\n");
Daniel P. Berrange 304b40
+		vs->subauth = VNC_AUTH_VENCRYPT_X509VNC;
Daniel P. Berrange 304b40
+	    } else {
Daniel P. Berrange 304b40
+		VNC_DEBUG("Initializing VNC server with TLS password auth\n");
Daniel P. Berrange 304b40
+		vs->subauth = VNC_AUTH_VENCRYPT_TLSVNC;
Daniel P. Berrange 304b40
+	    }
Daniel P. Berrange 304b40
+	} else {
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+	    VNC_DEBUG("Initializing VNC server with password auth\n");
Daniel P. Berrange 304b40
+	    vs->auth = VNC_AUTH_VNC;
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+	    vs->subauth = VNC_AUTH_INVALID;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+    } else {
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+	if (tls) {
Daniel P. Berrange 304b40
+	    vs->auth = VNC_AUTH_VENCRYPT;
Daniel P. Berrange 304b40
+	    if (x509) {
Daniel P. Berrange 304b40
+		VNC_DEBUG("Initializing VNC server with x509 no auth\n");
Daniel P. Berrange 304b40
+		vs->subauth = VNC_AUTH_VENCRYPT_X509NONE;
Daniel P. Berrange 304b40
+	    } else {
Daniel P. Berrange 304b40
+		VNC_DEBUG("Initializing VNC server with TLS no auth\n");
Daniel P. Berrange 304b40
+		vs->subauth = VNC_AUTH_VENCRYPT_TLSNONE;
Daniel P. Berrange 304b40
+	    }
Daniel P. Berrange 304b40
+	} else {
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+	    VNC_DEBUG("Initializing VNC server with no auth\n");
Daniel P. Berrange 304b40
+	    vs->auth = VNC_AUTH_NONE;
Daniel P. Berrange 304b40
+#if CONFIG_VNC_TLS
Daniel P. Berrange 304b40
+	    vs->subauth = VNC_AUTH_INVALID;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+#endif
Daniel P. Berrange 304b40
+    }
Daniel P. Berrange 304b40
 #ifndef _WIN32
Daniel P. Berrange 304b40
-    if (strstart(arg, "unix:", &p)) {
Daniel P. Berrange 304b40
+    if (strstart(display, "unix:", &p)) {
Daniel P. Berrange 304b40
 	addr = (struct sockaddr *)&uaddr;
Daniel P. Berrange 304b40
 	addrlen = sizeof(uaddr);
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 	vs->lsock = socket(PF_UNIX, SOCK_STREAM, 0);
Daniel P. Berrange 304b40
 	if (vs->lsock == -1) {
Daniel P. Berrange 304b40
 	    fprintf(stderr, "Could not create socket\n");
Daniel P. Berrange 304b40
-	    exit(1);
Daniel P. Berrange 304b40
+	    free(vs->display);
Daniel P. Berrange 304b40
+	    vs->display = NULL;
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
 	}
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
 	uaddr.sun_family = AF_UNIX;
Daniel P. Berrange 304b40
@@ -1219,40 +2157,53 @@ void vnc_display_init(DisplayState *ds, 
Daniel P. Berrange 304b40
 	addr = (struct sockaddr *)&iaddr;
Daniel P. Berrange 304b40
 	addrlen = sizeof(iaddr);
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
+	if (parse_host_port(&iaddr, display) < 0) {
Daniel P. Berrange 304b40
+	    fprintf(stderr, "Could not parse VNC address\n");
Daniel P. Berrange 304b40
+	    free(vs->display);
Daniel P. Berrange 304b40
+	    vs->display = NULL;
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
+	}
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
+	iaddr.sin_port = htons(ntohs(iaddr.sin_port) + 5900);
Daniel P. Berrange 304b40
+
Daniel P. Berrange 304b40
 	vs->lsock = socket(PF_INET, SOCK_STREAM, 0);
Daniel P. Berrange 304b40
 	if (vs->lsock == -1) {
Daniel P. Berrange 304b40
 	    fprintf(stderr, "Could not create socket\n");
Daniel P. Berrange 304b40
-	    exit(1);
Daniel P. Berrange 304b40
+	    free(vs->display);
Daniel P. Berrange 304b40
+	    vs->display = NULL;
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
 	}
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-	if (parse_host_port(&iaddr, arg) < 0) {
Daniel P. Berrange 304b40
-	    fprintf(stderr, "Could not parse VNC address\n");
Daniel P. Berrange 304b40
-	    exit(1);
Daniel P. Berrange 304b40
-	}
Daniel P. Berrange 304b40
-	    
Daniel P. Berrange 304b40
-	iaddr.sin_port = htons(ntohs(iaddr.sin_port) + 5900);
Daniel P. Berrange 304b40
-
Daniel P. Berrange 304b40
 	reuse_addr = 1;
Daniel P. Berrange 304b40
 	ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR,
Daniel P. Berrange 304b40
 			 (const char *)&reuse_addr, sizeof(reuse_addr));
Daniel P. Berrange 304b40
 	if (ret == -1) {
Daniel P. Berrange 304b40
 	    fprintf(stderr, "setsockopt() failed\n");
Daniel P. Berrange 304b40
-	    exit(1);
Daniel P. Berrange 304b40
+	    close(vs->lsock);
Daniel P. Berrange 304b40
+	    vs->lsock = -1;
Daniel P. Berrange 304b40
+	    free(vs->display);
Daniel P. Berrange 304b40
+	    vs->display = NULL;
Daniel P. Berrange 304b40
+	    return -1;
Daniel P. Berrange 304b40
 	}
Daniel P. Berrange 304b40
     }
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     if (bind(vs->lsock, addr, addrlen) == -1) {
Daniel P. Berrange 304b40
 	fprintf(stderr, "bind() failed\n");
Daniel P. Berrange 304b40
-	exit(1);
Daniel P. Berrange 304b40
+	close(vs->lsock);
Daniel P. Berrange 304b40
+	vs->lsock = -1;
Daniel P. Berrange 304b40
+	free(vs->display);
Daniel P. Berrange 304b40
+	vs->display = NULL;
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
     }
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
     if (listen(vs->lsock, 1) == -1) {
Daniel P. Berrange 304b40
 	fprintf(stderr, "listen() failed\n");
Daniel P. Berrange 304b40
-	exit(1);
Daniel P. Berrange 304b40
+	close(vs->lsock);
Daniel P. Berrange 304b40
+	vs->lsock = -1;
Daniel P. Berrange 304b40
+	free(vs->display);
Daniel P. Berrange 304b40
+	vs->display = NULL;
Daniel P. Berrange 304b40
+	return -1;
Daniel P. Berrange 304b40
     }
Daniel P. Berrange 304b40
 
Daniel P. Berrange 304b40
-    ret = qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
Daniel P. Berrange 304b40
-    if (ret == -1) {
Daniel P. Berrange 304b40
-	exit(1);
Daniel P. Berrange 304b40
-    }
Daniel P. Berrange 304b40
+    return qemu_set_fd_handler2(vs->lsock, vnc_listen_poll, vnc_listen_read, NULL, vs);
Daniel P. Berrange 304b40
 }