rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
f09e2e
diff -up openssh-6.3p1/Makefile.in.audit openssh-6.3p1/Makefile.in
f09e2e
--- openssh-6.3p1/Makefile.in.audit	2013-06-11 03:26:10.000000000 +0200
f09e2e
+++ openssh-6.3p1/Makefile.in	2013-10-07 15:53:34.246717277 +0200
f09e2e
@@ -73,7 +73,7 @@ LIBSSH_OBJS=authfd.o authfile.o bufaux.o
f09e2e
 	monitor_fdpass.o rijndael.o ssh-dss.o ssh-ecdsa.o ssh-rsa.o dh.o \
f09e2e
 	kexdh.o kexgex.o kexdhc.o kexgexc.o bufec.o kexecdh.o kexecdhc.o \
f09e2e
 	msg.o progressmeter.o dns.o entropy.o gss-genr.o umac.o umac128.o \
f09e2e
-	jpake.o schnorr.o ssh-pkcs11.o krl.o
f09e2e
+	jpake.o schnorr.o ssh-pkcs11.o krl.o auditstub.o
f09e2e
 
f09e2e
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
f09e2e
 	sshconnect.o sshconnect1.o sshconnect2.o mux.o \
f09e2e
diff -up openssh-6.3p1/audit-bsm.c.audit openssh-6.3p1/audit-bsm.c
f09e2e
--- openssh-6.3p1/audit-bsm.c.audit	2012-02-24 00:40:43.000000000 +0100
f09e2e
+++ openssh-6.3p1/audit-bsm.c	2013-10-07 15:53:34.246717277 +0200
f09e2e
@@ -375,10 +375,23 @@ audit_connection_from(const char *host,
f09e2e
 #endif
f09e2e
 }
f09e2e
 
f09e2e
-void
f09e2e
+int
f09e2e
 audit_run_command(const char *command)
f09e2e
 {
f09e2e
 	/* not implemented */
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_end_command(int handle, const char *command)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_count_session_open(void)
f09e2e
+{
f09e2e
+	/* not necessary */
f09e2e
 }
f09e2e
 
f09e2e
 void
f09e2e
@@ -393,6 +406,12 @@ audit_session_close(struct logininfo *li
f09e2e
 	/* not implemented */
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
 void
f09e2e
 audit_event(ssh_audit_event_t event)
f09e2e
 {
f09e2e
@@ -454,4 +473,40 @@ audit_event(ssh_audit_event_t event)
f09e2e
 		debug("%s: unhandled event %d", __func__, event);
f09e2e
 	}
f09e2e
 }
f09e2e
+
f09e2e
+void
f09e2e
+audit_unsupported_body(int what)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_destroy_sensitive_data(const char *fp)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_generate_ephemeral_server_key(const char *fp)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
 #endif /* BSM */
f09e2e
diff -up openssh-6.3p1/audit-linux.c.audit openssh-6.3p1/audit-linux.c
f09e2e
--- openssh-6.3p1/audit-linux.c.audit	2011-01-17 11:15:30.000000000 +0100
f09e2e
+++ openssh-6.3p1/audit-linux.c	2013-10-07 15:53:34.246717277 +0200
f09e2e
@@ -35,13 +35,24 @@
f09e2e
 
f09e2e
 #include "log.h"
f09e2e
 #include "audit.h"
f09e2e
+#include "key.h"
f09e2e
+#include "hostfile.h"
f09e2e
+#include "auth.h"
f09e2e
+#include "servconf.h"
f09e2e
 #include "canohost.h"
f09e2e
+#include "packet.h"
f09e2e
+#include "cipher.h"
f09e2e
 
f09e2e
+#define AUDIT_LOG_SIZE 128
f09e2e
+
f09e2e
+extern ServerOptions options;
f09e2e
+extern Authctxt *the_authctxt;
f09e2e
+extern u_int utmp_len;
f09e2e
 const char* audit_username(void);
f09e2e
 
f09e2e
-int
f09e2e
-linux_audit_record_event(int uid, const char *username,
f09e2e
-    const char *hostname, const char *ip, const char *ttyn, int success)
f09e2e
+static void
f09e2e
+linux_audit_user_logxxx(int uid, const char *username,
f09e2e
+    const char *hostname, const char *ip, const char *ttyn, int success, int event)
f09e2e
 {
f09e2e
 	int audit_fd, rc, saved_errno;
f09e2e
 
f09e2e
@@ -49,11 +60,11 @@ linux_audit_record_event(int uid, const
f09e2e
 	if (audit_fd < 0) {
f09e2e
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
 		    errno == EAFNOSUPPORT)
f09e2e
-			return 1; /* No audit support in kernel */
f09e2e
+			return; /* No audit support in kernel */
f09e2e
 		else
f09e2e
-			return 0; /* Must prevent login */
f09e2e
+			goto fatal_report; /* Must prevent login */
f09e2e
 	}
f09e2e
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
f09e2e
+	rc = audit_log_acct_message(audit_fd, event,
f09e2e
 	    NULL, "login", username ? username : "(unknown)",
f09e2e
 	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
f09e2e
 	saved_errno = errno;
f09e2e
@@ -65,35 +76,150 @@ linux_audit_record_event(int uid, const
f09e2e
 	if ((rc == -EPERM) && (geteuid() != 0))
f09e2e
 		rc = 0;
f09e2e
 	errno = saved_errno;
f09e2e
-	return (rc >= 0);
f09e2e
+	if (rc < 0) {
f09e2e
+fatal_report:
f09e2e
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
f09e2e
+	}
f09e2e
 }
f09e2e
 
f09e2e
+static void
f09e2e
+linux_audit_user_auth(int uid, const char *username,
f09e2e
+    const char *hostname, const char *ip, const char *ttyn, int success, int event)
f09e2e
+{
f09e2e
+	int audit_fd, rc, saved_errno;
f09e2e
+	static const char *event_name[] = {
f09e2e
+		"maxtries exceeded",
f09e2e
+		"root denied",
f09e2e
+		"success",
f09e2e
+		"none",
f09e2e
+		"password",
f09e2e
+		"challenge-response",
f09e2e
+		"pubkey",
f09e2e
+		"hostbased",
f09e2e
+		"gssapi",
f09e2e
+		"invalid user",
f09e2e
+		"nologin",
f09e2e
+		"connection closed",
f09e2e
+		"connection abandoned",
f09e2e
+		"unknown"
f09e2e
+	};
f09e2e
+
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
+		    errno == EAFNOSUPPORT)
f09e2e
+			return; /* No audit support in kernel */
f09e2e
+		else
f09e2e
+			goto fatal_report; /* Must prevent login */
f09e2e
+	}
f09e2e
+	
f09e2e
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
f09e2e
+		event = SSH_AUDIT_UNKNOWN;
f09e2e
+
f09e2e
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
f09e2e
+	    NULL, event_name[event], username ? username : "(unknown)",
f09e2e
+	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
f09e2e
+	saved_errno = errno;
f09e2e
+	close(audit_fd);
f09e2e
+	/*
f09e2e
+	 * Do not report error if the error is EPERM and sshd is run as non
f09e2e
+	 * root user.
f09e2e
+	 */
f09e2e
+	if ((rc == -EPERM) && (geteuid() != 0))
f09e2e
+		rc = 0;
f09e2e
+	errno = saved_errno;
f09e2e
+	if (rc < 0) {
f09e2e
+fatal_report:
f09e2e
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
f09e2e
+	}
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
f09e2e
+{
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, rc, saved_errno;
f09e2e
+
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
+					 errno == EAFNOSUPPORT)
f09e2e
+			return 1; /* No audit support in kernel */
f09e2e
+		else                                                                                                                                       
f09e2e
+			return 0; /* Must prevent login */
f09e2e
+	}
f09e2e
+	snprintf(buf, sizeof(buf), "%s_auth rport=%d", host_user ? "pubkey" : "hostbased", get_remote_port());
f09e2e
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
f09e2e
+		buf, audit_username(), -1, NULL, get_remote_ipaddr(), NULL, rv);
f09e2e
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
f09e2e
+		goto out;
f09e2e
+	snprintf(buf, sizeof(buf), "key algo=%s size=%d fp=%s%s rport=%d",
f09e2e
+			type, bits, key_fingerprint_prefix(), fp, get_remote_port());
f09e2e
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
f09e2e
+		buf, audit_username(), -1, NULL, get_remote_ipaddr(), NULL, rv);
f09e2e
+out:
f09e2e
+	saved_errno = errno;
f09e2e
+	audit_close(audit_fd);
f09e2e
+	errno = saved_errno;
f09e2e
+	/* do not report error if the error is EPERM and sshd is run as non root user */
f09e2e
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
f09e2e
+}
f09e2e
+
f09e2e
+static int user_login_count = 0;
f09e2e
+
f09e2e
 /* Below is the sshd audit API code */
f09e2e
 
f09e2e
 void
f09e2e
 audit_connection_from(const char *host, int port)
f09e2e
 {
f09e2e
-}
f09e2e
 	/* not implemented */
f09e2e
+}
f09e2e
 
f09e2e
-void
f09e2e
+int
f09e2e
 audit_run_command(const char *command)
f09e2e
 {
f09e2e
-	/* not implemented */
f09e2e
+	if (!user_login_count++) 
f09e2e
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+		    NULL, "ssh", 1, AUDIT_USER_LOGIN);
f09e2e
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+	    NULL, "ssh", 1, AUDIT_USER_START);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_end_command(int handle, const char *command)
f09e2e
+{
f09e2e
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+	    NULL, "ssh", 1, AUDIT_USER_END);
f09e2e
+	if (user_login_count && !--user_login_count) 
f09e2e
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+		    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_count_session_open(void)
f09e2e
+{
f09e2e
+	user_login_count++;
f09e2e
 }
f09e2e
 
f09e2e
 void
f09e2e
 audit_session_open(struct logininfo *li)
f09e2e
 {
f09e2e
-	if (linux_audit_record_event(li->uid, NULL, li->hostname,
f09e2e
-	    NULL, li->line, 1) == 0)
f09e2e
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
f09e2e
+	if (!user_login_count++) 
f09e2e
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f09e2e
+		    NULL, li->line, 1, AUDIT_USER_LOGIN);
f09e2e
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f09e2e
+	    NULL, li->line, 1, AUDIT_USER_START);
f09e2e
 }
f09e2e
 
f09e2e
 void
f09e2e
 audit_session_close(struct logininfo *li)
f09e2e
 {
f09e2e
-	/* not implemented */
f09e2e
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f09e2e
+	    NULL, li->line, 1, AUDIT_USER_END);
f09e2e
+	if (user_login_count && !--user_login_count) 
f09e2e
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f09e2e
+		    NULL, li->line, 1, AUDIT_USER_LOGOUT);
f09e2e
 }
f09e2e
 
f09e2e
 void
f09e2e
@@ -101,21 +227,43 @@ audit_event(ssh_audit_event_t event)
f09e2e
 {
f09e2e
 	switch(event) {
f09e2e
 	case SSH_AUTH_SUCCESS:
f09e2e
-	case SSH_CONNECTION_CLOSE:
f09e2e
+		linux_audit_user_auth(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 1, event);
f09e2e
+		break;
f09e2e
+
f09e2e
 	case SSH_NOLOGIN:
f09e2e
-	case SSH_LOGIN_EXCEED_MAXTRIES:
f09e2e
 	case SSH_LOGIN_ROOT_DENIED:
f09e2e
+		linux_audit_user_auth(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 0, event);
f09e2e
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 0, AUDIT_USER_LOGIN);
f09e2e
 		break;
f09e2e
 
f09e2e
+	case SSH_LOGIN_EXCEED_MAXTRIES:
f09e2e
 	case SSH_AUTH_FAIL_NONE:
f09e2e
 	case SSH_AUTH_FAIL_PASSWD:
f09e2e
 	case SSH_AUTH_FAIL_KBDINT:
f09e2e
 	case SSH_AUTH_FAIL_PUBKEY:
f09e2e
 	case SSH_AUTH_FAIL_HOSTBASED:
f09e2e
 	case SSH_AUTH_FAIL_GSSAPI:
f09e2e
+		linux_audit_user_auth(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 0, event);
f09e2e
+		break;
f09e2e
+
f09e2e
+	case SSH_CONNECTION_CLOSE:
f09e2e
+		if (user_login_count) {
f09e2e
+			while (user_login_count--)
f09e2e
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+				    NULL, "ssh", 1, AUDIT_USER_END);
f09e2e
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+			    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
f09e2e
+		}
f09e2e
+		break;
f09e2e
+
f09e2e
+	case SSH_CONNECTION_ABANDON:
f09e2e
 	case SSH_INVALID_USER:
f09e2e
-		linux_audit_record_event(-1, audit_username(), NULL,
f09e2e
-			get_remote_ipaddr(), "sshd", 0);
f09e2e
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 0, AUDIT_USER_LOGIN);
f09e2e
 		break;
f09e2e
 
f09e2e
 	default:
f09e2e
@@ -123,4 +271,135 @@ audit_event(ssh_audit_event_t event)
f09e2e
 	}
f09e2e
 }
f09e2e
 
f09e2e
+void
f09e2e
+audit_unsupported_body(int what)
f09e2e
+{
f09e2e
+#ifdef AUDIT_CRYPTO_SESSION
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	const static char *name[] = { "cipher", "mac", "comp" };
f09e2e
+	char *s;
f09e2e
+	int audit_fd;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
f09e2e
+		name[what], get_remote_port(), (s = get_local_ipaddr(packet_get_connection_in())),
f09e2e
+		get_local_port());
f09e2e
+	free(s);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0)
f09e2e
+		/* no problem, the next instruction will be fatal() */
f09e2e
+		return;
f09e2e
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
f09e2e
+			buf, NULL, get_remote_ipaddr(), NULL, 0);
f09e2e
+	audit_close(audit_fd);
f09e2e
+#endif
f09e2e
+}
f09e2e
+
f09e2e
+const static char *direction[] = { "from-server", "from-client", "both" };
f09e2e
+
f09e2e
+void
f09e2e
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, pid_t pid,
f09e2e
+	       uid_t uid)
f09e2e
+{
f09e2e
+#ifdef AUDIT_CRYPTO_SESSION
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, audit_ok;
f09e2e
+	Cipher *cipher = cipher_by_name(enc);
f09e2e
+	char *s;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d mac=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
f09e2e
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac,
f09e2e
+		(intmax_t)pid, (intmax_t)uid,
f09e2e
+		get_remote_port(), (s = get_local_ipaddr(packet_get_connection_in())), get_local_port());
f09e2e
+	free(s);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
+					 errno == EAFNOSUPPORT)
f09e2e
+			return; /* No audit support in kernel */
f09e2e
+		else                                                                                                                                       
f09e2e
+			fatal("cannot open audit"); /* Must prevent login */
f09e2e
+	}
f09e2e
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
f09e2e
+			buf, NULL, get_remote_ipaddr(), NULL, 1);
f09e2e
+	audit_close(audit_fd);
f09e2e
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f09e2e
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f09e2e
+		fatal("cannot write into audit"); /* Must prevent login */
f09e2e
+#endif
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, audit_ok;
f09e2e
+	char *s;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
f09e2e
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
f09e2e
+		 get_remote_port(),
f09e2e
+		 (s = get_local_ipaddr(packet_get_connection_in())),
f09e2e
+		 get_local_port());
f09e2e
+	free(s);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
f09e2e
+					 errno != EAFNOSUPPORT)
f09e2e
+			error("cannot open audit");
f09e2e
+		return;
f09e2e
+	}
f09e2e
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
f09e2e
+			buf, NULL, get_remote_ipaddr(), NULL, 1);
f09e2e
+	audit_close(audit_fd);
f09e2e
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f09e2e
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f09e2e
+		error("cannot write into audit");
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, audit_ok;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
f09e2e
+		fp, (intmax_t)pid, (intmax_t)uid);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
f09e2e
+					 errno != EAFNOSUPPORT)
f09e2e
+			error("cannot open audit");
f09e2e
+		return;
f09e2e
+	}
f09e2e
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
f09e2e
+			buf, NULL,
f09e2e
+			listening_for_clients() ? NULL : get_remote_ipaddr(),
f09e2e
+			NULL, 1);
f09e2e
+	audit_close(audit_fd);
f09e2e
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f09e2e
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f09e2e
+		error("cannot write into audit");
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_generate_ephemeral_server_key(const char *fp)
f09e2e
+{
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, audit_ok;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=create kind=server fp=%s direction=? ", fp);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
f09e2e
+					 errno != EAFNOSUPPORT)
f09e2e
+			error("cannot open audit");
f09e2e
+		return;
f09e2e
+	}
f09e2e
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
f09e2e
+			buf, NULL, 0, NULL, 1);
f09e2e
+	audit_close(audit_fd);
f09e2e
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f09e2e
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f09e2e
+		error("cannot write into audit");
f09e2e
+}
f09e2e
 #endif /* USE_LINUX_AUDIT */
f09e2e
diff -up openssh-6.3p1/audit.c.audit openssh-6.3p1/audit.c
f09e2e
--- openssh-6.3p1/audit.c.audit	2011-01-17 11:15:30.000000000 +0100
f09e2e
+++ openssh-6.3p1/audit.c	2013-10-07 15:53:34.246717277 +0200
f09e2e
@@ -28,6 +28,7 @@
f09e2e
 
f09e2e
 #include <stdarg.h>
f09e2e
 #include <string.h>
f09e2e
+#include <unistd.h>
f09e2e
 
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
 
f09e2e
@@ -36,6 +37,9 @@
f09e2e
 #include "key.h"
f09e2e
 #include "hostfile.h"
f09e2e
 #include "auth.h"
f09e2e
+#include "ssh-gss.h"
f09e2e
+#include "monitor_wrap.h"
f09e2e
+#include "xmalloc.h"
f09e2e
 
f09e2e
 /*
f09e2e
  * Care must be taken when using this since it WILL NOT be initialized when
f09e2e
@@ -111,6 +115,40 @@ audit_event_lookup(ssh_audit_event_t ev)
f09e2e
 	return(event_lookup[i].name);
f09e2e
 }
f09e2e
 
f09e2e
+void
f09e2e
+audit_key(int host_user, int *rv, const Key *key)
f09e2e
+{
f09e2e
+	char *fp;
f09e2e
+	const char *crypto_name;
f09e2e
+
f09e2e
+	fp = key_selected_fingerprint(key, SSH_FP_HEX);
f09e2e
+	if (key->type == KEY_RSA1)
f09e2e
+		crypto_name = "ssh-rsa1";
f09e2e
+	else
f09e2e
+		crypto_name = key_ssh_name(key);
f09e2e
+	if (audit_keyusage(host_user, crypto_name, key_size(key), fp, *rv) == 0)
f09e2e
+		*rv = 0;
f09e2e
+	free(fp);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_unsupported(int what)
f09e2e
+{
f09e2e
+	PRIVSEP(audit_unsupported_body(what));
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_kex(int ctos, char *enc, char *mac, char *comp)
f09e2e
+{
f09e2e
+	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, getpid(), getuid()));
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free(int ctos)
f09e2e
+{
f09e2e
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
f09e2e
+}
f09e2e
+
f09e2e
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
f09e2e
 /*
f09e2e
  * Null implementations of audit functions.
f09e2e
@@ -140,6 +178,17 @@ audit_event(ssh_audit_event_t event)
f09e2e
 }
f09e2e
 
f09e2e
 /*
f09e2e
+ * Called when a child process has called, or will soon call,
f09e2e
+ * audit_session_open.
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_count_session_open(void)
f09e2e
+{
f09e2e
+	debug("audit count session open euid %d user %s", geteuid(),
f09e2e
+	      audit_username());
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
  * Called when a user session is started.  Argument is the tty allocated to
f09e2e
  * the session, or NULL if no tty was allocated.
f09e2e
  *
f09e2e
@@ -174,13 +223,91 @@ audit_session_close(struct logininfo *li
f09e2e
 /*
f09e2e
  * This will be called when a user runs a non-interactive command.  Note that
f09e2e
  * it may be called multiple times for a single connection since SSH2 allows
f09e2e
- * multiple sessions within a single connection.
f09e2e
+ * multiple sessions within a single connection.  Returns a "handle" for
f09e2e
+ * audit_end_command.
f09e2e
  */
f09e2e
-void
f09e2e
+int
f09e2e
 audit_run_command(const char *command)
f09e2e
 {
f09e2e
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
f09e2e
 	    audit_username(), command);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called when the non-interactive command finishes.  Note that
f09e2e
+ * it may be called multiple times for a single connection since SSH2 allows
f09e2e
+ * multiple sessions within a single connection.  "handle" should come from
f09e2e
+ * the corresponding audit_run_command.
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_end_command(int handle, const char *command)
f09e2e
+{
f09e2e
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
f09e2e
+	    audit_username(), command);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
f09e2e
+ *
f09e2e
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
f09e2e
+ */
f09e2e
+int
f09e2e
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
f09e2e
+{
f09e2e
+	debug("audit %s key usage euid %d user %s key type %s key length %d fingerprint %s%s, result %d", 
f09e2e
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(), type, bits,
f09e2e
+		key_fingerprint_prefix(), fp, rv);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called when the protocol negotiation fails.
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_unsupported_body(int what)
f09e2e
+{
f09e2e
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called on succesfull protocol negotiation.
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, pid_t pid,
f09e2e
+	       uid_t uid)
f09e2e
+{
f09e2e
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s from pid %ld uid %u",
f09e2e
+		(unsigned)geteuid(), ctos, enc, mac, compress, (long)pid,
f09e2e
+	        (unsigned)uid);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called on succesfull session key discard
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
f09e2e
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called on destroy private part of the server key
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
f09e2e
+		geteuid(), fp, (long)pid, (unsigned)uid);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called on generation of the ephemeral server key
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_generate_ephemeral_server_key(const char *)
f09e2e
+{
f09e2e
+	debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp);
f09e2e
 }
f09e2e
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
f09e2e
 #endif /* SSH_AUDIT_EVENTS */
f09e2e
diff -up openssh-6.3p1/audit.h.audit openssh-6.3p1/audit.h
f09e2e
--- openssh-6.3p1/audit.h.audit	2011-01-17 11:15:30.000000000 +0100
f09e2e
+++ openssh-6.3p1/audit.h	2013-10-07 15:53:34.246717277 +0200
f09e2e
@@ -28,6 +28,7 @@
f09e2e
 # define _SSH_AUDIT_H
f09e2e
 
f09e2e
 #include "loginrec.h"
f09e2e
+#include "key.h"
f09e2e
 
f09e2e
 enum ssh_audit_event_type {
f09e2e
 	SSH_LOGIN_EXCEED_MAXTRIES,
f09e2e
@@ -47,11 +48,25 @@ enum ssh_audit_event_type {
f09e2e
 };
f09e2e
 typedef enum ssh_audit_event_type ssh_audit_event_t;
f09e2e
 
f09e2e
+int	listening_for_clients(void);
f09e2e
+
f09e2e
 void	audit_connection_from(const char *, int);
f09e2e
 void	audit_event(ssh_audit_event_t);
f09e2e
+void	audit_count_session_open(void);
f09e2e
 void	audit_session_open(struct logininfo *);
f09e2e
 void	audit_session_close(struct logininfo *);
f09e2e
-void	audit_run_command(const char *);
f09e2e
+int	audit_run_command(const char *);
f09e2e
+void 	audit_end_command(int, const char *);
f09e2e
 ssh_audit_event_t audit_classify_auth(const char *);
f09e2e
+int	audit_keyusage(int, const char *, unsigned, char *, int);
f09e2e
+void	audit_key(int, int *, const Key *);
f09e2e
+void	audit_unsupported(int);
f09e2e
+void	audit_kex(int, char *, char *, char *);
f09e2e
+void	audit_unsupported_body(int);
f09e2e
+void	audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
f09e2e
+void	audit_session_key_free(int ctos);
f09e2e
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
f09e2e
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
f09e2e
+void	audit_generate_ephemeral_server_key(const char *);
f09e2e
 
f09e2e
 #endif /* _SSH_AUDIT_H */
f09e2e
diff -up openssh-6.3p1/auditstub.c.audit openssh-6.3p1/auditstub.c
f09e2e
--- openssh-6.3p1/auditstub.c.audit	2013-10-07 15:53:34.247717272 +0200
f09e2e
+++ openssh-6.3p1/auditstub.c	2013-10-07 15:53:34.247717272 +0200
f09e2e
@@ -0,0 +1,50 @@
f09e2e
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
f09e2e
+
f09e2e
+/*
f09e2e
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
f09e2e
+ * Use is subject to license terms.
f09e2e
+ *
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
f09e2e
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f09e2e
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f09e2e
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f09e2e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f09e2e
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f09e2e
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f09e2e
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f09e2e
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ *
f09e2e
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
f09e2e
+ */
f09e2e
+
f09e2e
+#include <sys/types.h>
f09e2e
+
f09e2e
+void
f09e2e
+audit_unsupported(int n)
f09e2e
+{
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_kex(int ctos, char *enc, char *mac, char *comp)
f09e2e
+{
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free(int ctos)
f09e2e
+{
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+}
f09e2e
diff -up openssh-6.3p1/auth-rsa.c.audit openssh-6.3p1/auth-rsa.c
f09e2e
--- openssh-6.3p1/auth-rsa.c.audit	2013-07-18 08:12:44.000000000 +0200
f09e2e
+++ openssh-6.3p1/auth-rsa.c	2013-10-07 15:53:34.247717272 +0200
f09e2e
@@ -92,7 +92,10 @@ auth_rsa_verify_response(Key *key, BIGNU
f09e2e
 {
f09e2e
 	u_char buf[32], mdbuf[16];
f09e2e
 	MD5_CTX md;
f09e2e
-	int len;
f09e2e
+	int len, rv;
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	char *fp;
f09e2e
+#endif
f09e2e
 
f09e2e
 	/* don't allow short keys */
f09e2e
 	if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
f09e2e
@@ -113,12 +116,18 @@ auth_rsa_verify_response(Key *key, BIGNU
f09e2e
 	MD5_Final(mdbuf, &md);
f09e2e
 
f09e2e
 	/* Verify that the response is the original challenge. */
f09e2e
-	if (timingsafe_bcmp(response, mdbuf, 16) != 0) {
f09e2e
-		/* Wrong answer. */
f09e2e
-		return (0);
f09e2e
+	rv = timingsafe_bcmp(response, mdbuf, 16) == 0;
f09e2e
+
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	fp = key_selected_fingerprint(key, SSH_FP_HEX);
f09e2e
+	if (audit_keyusage(1, "ssh-rsa1", RSA_size(key->rsa) * 8, fp, rv) == 0) {
f09e2e
+		debug("unsuccessful audit");
f09e2e
+		rv = 0;
f09e2e
 	}
f09e2e
-	/* Correct answer. */
f09e2e
-	return (1);
f09e2e
+	free(fp);
f09e2e
+#endif
f09e2e
+
f09e2e
+	return rv;
f09e2e
 }
f09e2e
 
f09e2e
 /*
f09e2e
diff -up openssh-6.3p1/auth.h.audit openssh-6.3p1/auth.h
f09e2e
--- openssh-6.3p1/auth.h.audit	2013-07-20 05:21:53.000000000 +0200
f09e2e
+++ openssh-6.3p1/auth.h	2013-10-07 16:02:38.629171107 +0200
f09e2e
@@ -187,6 +187,7 @@ void	abandon_challenge_response(Authctxt
f09e2e
 
f09e2e
 char	*expand_authorized_keys(const char *, struct passwd *pw);
f09e2e
 char	*authorized_principals_file(struct passwd *);
f09e2e
+int	 user_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
f09e2e
 
f09e2e
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
f09e2e
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
f09e2e
@@ -204,6 +205,7 @@ Key	*get_hostkey_private_by_type(int);
f09e2e
 int	 get_hostkey_index(Key *);
f09e2e
 int	 ssh1_session_key(BIGNUM *);
f09e2e
 void	 sshd_hostkey_sign(Key *, Key *, u_char **, u_int *, u_char *, u_int);
f09e2e
+int	 hostbased_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
f09e2e
 
f09e2e
 /* debug messages during authentication */
f09e2e
 void	 auth_debug_add(const char *fmt,...) __attribute__((format(printf, 1, 2)));
f09e2e
diff -up openssh-6.3p1/auth2-hostbased.c.audit openssh-6.3p1/auth2-hostbased.c
f09e2e
--- openssh-6.3p1/auth2-hostbased.c.audit	2013-10-07 15:53:34.223717384 +0200
f09e2e
+++ openssh-6.3p1/auth2-hostbased.c	2013-10-07 15:53:34.247717272 +0200
f09e2e
@@ -123,7 +123,7 @@ userauth_hostbased(Authctxt *authctxt)
f09e2e
 	/* test for allowed key and correct signature */
f09e2e
 	authenticated = 0;
f09e2e
 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
f09e2e
-	    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
f09e2e
+	    PRIVSEP(hostbased_key_verify(key, sig, slen, buffer_ptr(&b),
f09e2e
 			buffer_len(&b))) == 1)
f09e2e
 		authenticated = 1;
f09e2e
 
f09e2e
@@ -140,6 +140,18 @@ done:
f09e2e
 	return authenticated;
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+hostbased_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
f09e2e
+{
f09e2e
+	int rv;
f09e2e
+
f09e2e
+	rv = key_verify(key, sig, slen, data, datalen);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	audit_key(0, &rv, key);
f09e2e
+#endif
f09e2e
+	return rv;
f09e2e
+}
f09e2e
+
f09e2e
 /* return 1 if given hostkey is allowed */
f09e2e
 int
f09e2e
 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
f09e2e
diff -up openssh-6.3p1/auth2-pubkey.c.audit openssh-6.3p1/auth2-pubkey.c
f09e2e
--- openssh-6.3p1/auth2-pubkey.c.audit	2013-10-07 15:53:34.224717379 +0200
f09e2e
+++ openssh-6.3p1/auth2-pubkey.c	2013-10-08 15:11:42.282436972 +0200
f09e2e
@@ -152,7 +152,7 @@ userauth_pubkey(Authctxt *authctxt)
f09e2e
 		/* test for correct signature */
f09e2e
 		authenticated = 0;
f09e2e
 		if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
f09e2e
-		    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
f09e2e
+		    PRIVSEP(user_key_verify(key, sig, slen, buffer_ptr(&b),
f09e2e
 		    buffer_len(&b))) == 1)
f09e2e
 			authenticated = 1;
f09e2e
 		buffer_free(&b);
f09e2e
@@ -223,6 +223,18 @@ pubkey_auth_info(Authctxt *authctxt, con
f09e2e
 	free(extra);
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+user_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
f09e2e
+{
f09e2e
+	int rv;
f09e2e
+
f09e2e
+	rv = key_verify(key, sig, slen, data, datalen);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	audit_key(1, &rv, key);
f09e2e
+#endif
f09e2e
+	return rv;
f09e2e
+}
f09e2e
+
f09e2e
 static int
f09e2e
 match_principals_option(const char *principal_list, struct KeyCert *cert)
f09e2e
 {
f09e2e
diff -up openssh-6.3p1/auth2.c.audit openssh-6.3p1/auth2.c
f09e2e
--- openssh-6.3p1/auth2.c.audit	2013-06-01 23:41:51.000000000 +0200
f09e2e
+++ openssh-6.3p1/auth2.c	2013-10-07 15:53:34.248717268 +0200
f09e2e
@@ -245,9 +245,6 @@ input_userauth_request(int type, u_int32
f09e2e
 		} else {
f09e2e
 			logit("input_userauth_request: invalid user %s", user);
f09e2e
 			authctxt->pw = fakepw();
f09e2e
-#ifdef SSH_AUDIT_EVENTS
f09e2e
-			PRIVSEP(audit_event(SSH_INVALID_USER));
f09e2e
-#endif
f09e2e
 		}
f09e2e
 #ifdef USE_PAM
f09e2e
 		if (options.use_pam)
f09e2e
diff -up openssh-6.3p1/cipher.c.audit openssh-6.3p1/cipher.c
f09e2e
--- openssh-6.3p1/cipher.c.audit	2013-10-07 15:53:34.248717268 +0200
f09e2e
+++ openssh-6.3p1/cipher.c	2013-10-07 16:06:51.117971891 +0200
f09e2e
@@ -55,18 +55,6 @@ extern const EVP_CIPHER *evp_ssh1_bf(voi
f09e2e
 extern const EVP_CIPHER *evp_ssh1_3des(void);
f09e2e
 extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
f09e2e
 
f09e2e
-struct Cipher {
f09e2e
-	char	*name;
f09e2e
-	int	number;		/* for ssh1 only */
f09e2e
-	u_int	block_size;
f09e2e
-	u_int	key_len;
f09e2e
-	u_int	iv_len;		/* defaults to block_size */
f09e2e
-	u_int	auth_len;
f09e2e
-	u_int	discard_len;
f09e2e
-	u_int	cbc_mode;
f09e2e
-	const EVP_CIPHER	*(*evptype)(void);
f09e2e
-};
f09e2e
-
f09e2e
 static const struct Cipher ciphers[] = {
f09e2e
 	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
f09e2e
 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
f09e2e
diff -up openssh-6.3p1/cipher.h.audit openssh-6.3p1/cipher.h
f09e2e
--- openssh-6.3p1/cipher.h.audit	2013-04-23 11:24:32.000000000 +0200
f09e2e
+++ openssh-6.3p1/cipher.h	2013-10-07 15:53:34.248717268 +0200
f09e2e
@@ -61,7 +61,18 @@
f09e2e
 typedef struct Cipher Cipher;
f09e2e
 typedef struct CipherContext CipherContext;
f09e2e
 
f09e2e
-struct Cipher;
f09e2e
+struct Cipher {
f09e2e
+	char	*name;
f09e2e
+	int	number;		/* for ssh1 only */
f09e2e
+	u_int	block_size;
f09e2e
+	u_int	key_len;
f09e2e
+	u_int	iv_len;		/* defaults to block_size */
f09e2e
+	u_int	auth_len;
f09e2e
+	u_int	discard_len;
f09e2e
+	u_int	cbc_mode;
f09e2e
+	const EVP_CIPHER	*(*evptype)(void);
f09e2e
+};
f09e2e
+
f09e2e
 struct CipherContext {
f09e2e
 	int	plaintext;
f09e2e
 	int	encrypt;
f09e2e
diff -up openssh-6.3p1/kex.c.audit openssh-6.3p1/kex.c
f09e2e
--- openssh-6.3p1/kex.c.audit	2013-06-01 23:31:18.000000000 +0200
f09e2e
+++ openssh-6.3p1/kex.c	2013-10-07 15:53:34.249717264 +0200
f09e2e
@@ -49,6 +49,7 @@
f09e2e
 #include "dispatch.h"
f09e2e
 #include "monitor.h"
f09e2e
 #include "roaming.h"
f09e2e
+#include "audit.h"
f09e2e
 
f09e2e
 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
f09e2e
 # if defined(HAVE_EVP_SHA256)
f09e2e
@@ -341,9 +342,13 @@ static void
f09e2e
 choose_enc(Enc *enc, char *client, char *server)
f09e2e
 {
f09e2e
 	char *name = match_list(client, server, NULL);
f09e2e
-	if (name == NULL)
f09e2e
+	if (name == NULL) {
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		audit_unsupported(0);
f09e2e
+#endif
f09e2e
 		fatal("no matching cipher found: client %s server %s",
f09e2e
 		    client, server);
f09e2e
+	}
f09e2e
 	if ((enc->cipher = cipher_by_name(name)) == NULL)
f09e2e
 		fatal("matching cipher is not supported: %s", name);
f09e2e
 	enc->name = name;
f09e2e
@@ -359,9 +364,13 @@ static void
f09e2e
 choose_mac(Mac *mac, char *client, char *server)
f09e2e
 {
f09e2e
 	char *name = match_list(client, server, NULL);
f09e2e
-	if (name == NULL)
f09e2e
+	if (name == NULL) {
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		audit_unsupported(1);
f09e2e
+#endif
f09e2e
 		fatal("no matching mac found: client %s server %s",
f09e2e
 		    client, server);
f09e2e
+	}
f09e2e
 	if (mac_setup(mac, name) < 0)
f09e2e
 		fatal("unsupported mac %s", name);
f09e2e
 	/* truncate the key */
f09e2e
@@ -376,8 +385,12 @@ static void
f09e2e
 choose_comp(Comp *comp, char *client, char *server)
f09e2e
 {
f09e2e
 	char *name = match_list(client, server, NULL);
f09e2e
-	if (name == NULL)
f09e2e
+	if (name == NULL) {
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		audit_unsupported(2);
f09e2e
+#endif
f09e2e
 		fatal("no matching comp found: client %s server %s", client, server);
f09e2e
+	}
f09e2e
 	if (strcmp(name, "zlib@openssh.com") == 0) {
f09e2e
 		comp->type = COMP_DELAYED;
f09e2e
 	} else if (strcmp(name, "zlib") == 0) {
f09e2e
@@ -492,6 +505,9 @@ kex_choose_conf(Kex *kex)
f09e2e
 		    newkeys->enc.name,
f09e2e
 		    authlen == 0 ? newkeys->mac.name : "<implicit>",
f09e2e
 		    newkeys->comp.name);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		audit_kex(ctos, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name);
f09e2e
+#endif
f09e2e
 	}
f09e2e
 	choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
f09e2e
 	choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
f09e2e
@@ -656,3 +672,34 @@ dump_digest(char *msg, u_char *digest, i
f09e2e
 	fprintf(stderr, "\n");
f09e2e
 }
f09e2e
 #endif
f09e2e
+
f09e2e
+static void
f09e2e
+enc_destroy(Enc *enc)
f09e2e
+{
f09e2e
+	if (enc == NULL)
f09e2e
+		return;
f09e2e
+
f09e2e
+	if (enc->key) {
f09e2e
+		memset(enc->key, 0, enc->key_len);
f09e2e
+		free(enc->key);
f09e2e
+	}
f09e2e
+
f09e2e
+	if (enc->iv) {
f09e2e
+		memset(enc->iv,  0, enc->block_size);
f09e2e
+		free(enc->iv);
f09e2e
+	}
f09e2e
+
f09e2e
+	memset(enc, 0, sizeof(*enc));
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+newkeys_destroy(Newkeys *newkeys)
f09e2e
+{
f09e2e
+	if (newkeys == NULL)
f09e2e
+		return;
f09e2e
+
f09e2e
+	enc_destroy(&newkeys->enc);
f09e2e
+	mac_destroy(&newkeys->mac);
f09e2e
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
f09e2e
+}
f09e2e
+
f09e2e
diff -up openssh-6.3p1/kex.h.audit openssh-6.3p1/kex.h
f09e2e
--- openssh-6.3p1/kex.h.audit	2013-07-20 05:21:53.000000000 +0200
f09e2e
+++ openssh-6.3p1/kex.h	2013-10-07 15:53:34.249717264 +0200
f09e2e
@@ -162,6 +162,8 @@ void	 kexgex_server(Kex *);
f09e2e
 void	 kexecdh_client(Kex *);
f09e2e
 void	 kexecdh_server(Kex *);
f09e2e
 
f09e2e
+void	newkeys_destroy(Newkeys *newkeys);
f09e2e
+
f09e2e
 void
f09e2e
 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
f09e2e
     BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
f09e2e
diff -up openssh-6.3p1/key.c.audit openssh-6.3p1/key.c
f09e2e
--- openssh-6.3p1/key.c.audit	2013-10-07 15:53:34.224717379 +0200
f09e2e
+++ openssh-6.3p1/key.c	2013-10-07 15:53:34.249717264 +0200
f09e2e
@@ -1773,6 +1773,30 @@ key_demote(const Key *k)
f09e2e
 }
f09e2e
 
f09e2e
 int
f09e2e
+key_is_private(const Key *k)
f09e2e
+{
f09e2e
+	switch (k->type) {
f09e2e
+	case KEY_RSA_CERT_V00:
f09e2e
+	case KEY_RSA_CERT:
f09e2e
+	case KEY_RSA1:
f09e2e
+	case KEY_RSA:
f09e2e
+		return k->rsa->d != NULL;
f09e2e
+	case KEY_DSA_CERT_V00:
f09e2e
+	case KEY_DSA_CERT:
f09e2e
+	case KEY_DSA:
f09e2e
+		return k->dsa->priv_key != NULL;
f09e2e
+#ifdef OPENSSL_HAS_ECC
f09e2e
+	case KEY_ECDSA_CERT:
f09e2e
+	case KEY_ECDSA:
f09e2e
+		return EC_KEY_get0_private_key(k->ecdsa) != NULL;
f09e2e
+#endif
f09e2e
+	default:
f09e2e
+		fatal("key_is_private: bad key type %d", k->type);
f09e2e
+		return 1;
f09e2e
+	}
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
 key_is_cert(const Key *k)
f09e2e
 {
f09e2e
 	if (k == NULL)
f09e2e
diff -up openssh-6.3p1/key.h.audit openssh-6.3p1/key.h
f09e2e
--- openssh-6.3p1/key.h.audit	2013-10-07 15:53:34.224717379 +0200
f09e2e
+++ openssh-6.3p1/key.h	2013-10-07 15:53:34.249717264 +0200
f09e2e
@@ -110,6 +110,7 @@ Key	*key_generate(int, u_int);
f09e2e
 Key	*key_from_private(const Key *);
f09e2e
 int	 key_type_from_name(char *);
f09e2e
 int	 key_is_cert(const Key *);
f09e2e
+int	 key_is_private(const Key *k);
f09e2e
 int	 key_type_plain(int);
f09e2e
 int	 key_to_certified(Key *, int);
f09e2e
 int	 key_drop_cert(Key *);
f09e2e
diff -up openssh-6.3p1/mac.c.audit openssh-6.3p1/mac.c
f09e2e
--- openssh-6.3p1/mac.c.audit	2013-06-06 00:12:37.000000000 +0200
f09e2e
+++ openssh-6.3p1/mac.c	2013-10-07 15:53:34.250717259 +0200
f09e2e
@@ -224,6 +224,20 @@ mac_clear(Mac *mac)
f09e2e
 	mac->umac_ctx = NULL;
f09e2e
 }
f09e2e
 
f09e2e
+void
f09e2e
+mac_destroy(Mac *mac)
f09e2e
+{
f09e2e
+	if (mac == NULL)
f09e2e
+		return;
f09e2e
+
f09e2e
+	if (mac->key) {
f09e2e
+		memset(mac->key, 0, mac->key_len);
f09e2e
+		free(mac->key);
f09e2e
+	}
f09e2e
+
f09e2e
+	memset(mac, 0, sizeof(*mac));
f09e2e
+}
f09e2e
+
f09e2e
 /* XXX copied from ciphers_valid */
f09e2e
 #define	MAC_SEP	","
f09e2e
 int
f09e2e
diff -up openssh-6.3p1/mac.h.audit openssh-6.3p1/mac.h
f09e2e
--- openssh-6.3p1/mac.h.audit	2013-04-23 11:24:32.000000000 +0200
f09e2e
+++ openssh-6.3p1/mac.h	2013-10-07 15:53:34.250717259 +0200
f09e2e
@@ -29,3 +29,4 @@ int	 mac_setup(Mac *, char *);
f09e2e
 int	 mac_init(Mac *);
f09e2e
 u_char	*mac_compute(Mac *, u_int32_t, u_char *, int);
f09e2e
 void	 mac_clear(Mac *);
f09e2e
+void	 mac_destroy(Mac *);
f09e2e
diff -up openssh-6.3p1/monitor.c.audit openssh-6.3p1/monitor.c
f09e2e
--- openssh-6.3p1/monitor.c.audit	2013-10-07 15:53:34.217717411 +0200
f09e2e
+++ openssh-6.3p1/monitor.c	2013-10-08 15:10:38.270726936 +0200
f09e2e
@@ -98,6 +98,7 @@
f09e2e
 #include "jpake.h"
f09e2e
 #include "roaming.h"
f09e2e
 #include "authfd.h"
f09e2e
+#include "audit.h"
f09e2e
 
f09e2e
 #ifdef GSSAPI
f09e2e
 static Gssctxt *gsscontext = NULL;
f09e2e
@@ -114,6 +115,8 @@ extern Buffer auth_debug;
f09e2e
 extern int auth_debug_init;
f09e2e
 extern Buffer loginmsg;
f09e2e
 
f09e2e
+extern void destroy_sensitive_data(int);
f09e2e
+
f09e2e
 /* State exported from the child */
f09e2e
 
f09e2e
 struct {
f09e2e
@@ -186,6 +189,11 @@ int mm_answer_gss_checkmic(int, Buffer *
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
 int mm_answer_audit_event(int, Buffer *);
f09e2e
 int mm_answer_audit_command(int, Buffer *);
f09e2e
+int mm_answer_audit_end_command(int, Buffer *);
f09e2e
+int mm_answer_audit_unsupported_body(int, Buffer *);
f09e2e
+int mm_answer_audit_kex_body(int, Buffer *);
f09e2e
+int mm_answer_audit_session_key_free_body(int, Buffer *);
f09e2e
+int mm_answer_audit_server_key_free(int, Buffer *);
f09e2e
 #endif
f09e2e
 
f09e2e
 static int monitor_read_log(struct monitor *);
f09e2e
@@ -237,6 +245,10 @@ struct mon_table mon_dispatch_proto20[]
f09e2e
 #endif
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f09e2e
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f09e2e
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f09e2e
 #endif
f09e2e
 #ifdef BSD_AUTH
f09e2e
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
f09e2e
@@ -273,6 +285,11 @@ struct mon_table mon_dispatch_postauth20
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f09e2e
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
f09e2e
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
f09e2e
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f09e2e
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f09e2e
 #endif
f09e2e
     {0, 0, NULL}
f09e2e
 };
f09e2e
@@ -304,6 +321,10 @@ struct mon_table mon_dispatch_proto15[]
f09e2e
 #endif
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f09e2e
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f09e2e
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f09e2e
 #endif
f09e2e
     {0, 0, NULL}
f09e2e
 };
f09e2e
@@ -315,6 +336,11 @@ struct mon_table mon_dispatch_postauth15
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f09e2e
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
f09e2e
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
f09e2e
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f09e2e
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f09e2e
 #endif
f09e2e
     {0, 0, NULL}
f09e2e
 };
f09e2e
@@ -1365,9 +1391,11 @@ mm_answer_keyverify(int sock, Buffer *m)
f09e2e
 	Key *key;
f09e2e
 	u_char *signature, *data, *blob;
f09e2e
 	u_int signaturelen, datalen, bloblen;
f09e2e
+	int type = 0;
f09e2e
 	int verified = 0;
f09e2e
 	int valid_data = 0;
f09e2e
 
f09e2e
+	type = buffer_get_int(m);
f09e2e
 	blob = buffer_get_string(m, &bloblen);
f09e2e
 	signature = buffer_get_string(m, &signaturelen);
f09e2e
 	data = buffer_get_string(m, &datalen);
f09e2e
@@ -1375,6 +1403,8 @@ mm_answer_keyverify(int sock, Buffer *m)
f09e2e
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
f09e2e
 	  !monitor_allowed_key(blob, bloblen))
f09e2e
 		fatal("%s: bad key, not previously allowed", __func__);
f09e2e
+	if (type != key_blobtype)
f09e2e
+		fatal("%s: bad key type", __func__);
f09e2e
 
f09e2e
 	key = key_from_blob(blob, bloblen);
f09e2e
 	if (key == NULL)
f09e2e
@@ -1395,7 +1425,17 @@ mm_answer_keyverify(int sock, Buffer *m)
f09e2e
 	if (!valid_data)
f09e2e
 		fatal("%s: bad signature data blob", __func__);
f09e2e
 
f09e2e
-	verified = key_verify(key, signature, signaturelen, data, datalen);
f09e2e
+	switch (key_blobtype) {
f09e2e
+	case MM_USERKEY:
f09e2e
+		verified = user_key_verify(key, signature, signaturelen, data, datalen);
f09e2e
+		break;
f09e2e
+	case MM_HOSTKEY:
f09e2e
+		verified = hostbased_key_verify(key, signature, signaturelen, data, datalen);
f09e2e
+		break;
f09e2e
+	default:
f09e2e
+		verified = 0;
f09e2e
+		break;
f09e2e
+	}
f09e2e
 	debug3("%s: key %p signature %s",
f09e2e
 	    __func__, key, (verified == 1) ? "verified" : "unverified");
f09e2e
 
f09e2e
@@ -1448,6 +1488,12 @@ mm_session_close(Session *s)
f09e2e
 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
f09e2e
 		session_pty_cleanup2(s);
f09e2e
 	}
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	if (s->command != NULL) {
f09e2e
+		debug3("%s: command %d", __func__, s->command_handle);
f09e2e
+		session_end_command2(s);
f09e2e
+	}
f09e2e
+#endif
f09e2e
 	session_unused(s->self);
f09e2e
 }
f09e2e
 
f09e2e
@@ -1728,6 +1774,8 @@ mm_answer_term(int sock, Buffer *req)
f09e2e
 		sshpam_cleanup();
f09e2e
 #endif
f09e2e
 
f09e2e
+	destroy_sensitive_data(0);
f09e2e
+
f09e2e
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
f09e2e
 		if (errno != EINTR)
f09e2e
 			exit(1);
f09e2e
@@ -1770,11 +1818,43 @@ mm_answer_audit_command(int socket, Buff
f09e2e
 {
f09e2e
 	u_int len;
f09e2e
 	char *cmd;
f09e2e
+	Session *s;
f09e2e
 
f09e2e
 	debug3("%s entering", __func__);
f09e2e
 	cmd = buffer_get_string(m, &len;;
f09e2e
+
f09e2e
 	/* sanity check command, if so how? */
f09e2e
-	audit_run_command(cmd);
f09e2e
+	s = session_new();
f09e2e
+	if (s == NULL)
f09e2e
+		fatal("%s: error allocating a session", __func__);
f09e2e
+	s->command = cmd;
f09e2e
+	s->command_handle = audit_run_command(cmd);
f09e2e
+
f09e2e
+	buffer_clear(m);
f09e2e
+	buffer_put_int(m, s->self);
f09e2e
+
f09e2e
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
f09e2e
+
f09e2e
+	return (0);
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_answer_audit_end_command(int socket, Buffer *m)
f09e2e
+{
f09e2e
+	int handle;
f09e2e
+	u_int len;
f09e2e
+	char *cmd;
f09e2e
+	Session *s;
f09e2e
+
f09e2e
+	debug3("%s entering", __func__);
f09e2e
+	handle = buffer_get_int(m);
f09e2e
+	cmd = buffer_get_string(m, &len;;
f09e2e
+
f09e2e
+	s = session_by_id(handle);
f09e2e
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
f09e2e
+	    strcmp(s->command, cmd) != 0)
f09e2e
+		fatal("%s: invalid handle", __func__);
f09e2e
+	mm_session_close(s);
f09e2e
 	free(cmd);
f09e2e
 	return (0);
f09e2e
 }
f09e2e
@@ -1910,11 +1990,13 @@ mm_get_keystate(struct monitor *pmonitor
f09e2e
 
f09e2e
 	blob = buffer_get_string(&m, &bloblen);
f09e2e
 	current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
f09e2e
+	memset(blob, 0, bloblen);
f09e2e
 	free(blob);
f09e2e
 
f09e2e
 	debug3("%s: Waiting for second key", __func__);
f09e2e
 	blob = buffer_get_string(&m, &bloblen);
f09e2e
 	current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
f09e2e
+	memset(blob, 0, bloblen);
f09e2e
 	free(blob);
f09e2e
 
f09e2e
 	/* Now get sequence numbers for the packets */
f09e2e
@@ -1960,6 +2042,21 @@ mm_get_keystate(struct monitor *pmonitor
f09e2e
 	}
f09e2e
 
f09e2e
 	buffer_free(&m);
f09e2e
+
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	if (compat20) {
f09e2e
+		buffer_init(&m);
f09e2e
+		mm_request_receive_expect(pmonitor->m_sendfd,
f09e2e
+					  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
f09e2e
+		mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, &m);
f09e2e
+		buffer_free(&m);
f09e2e
+	}
f09e2e
+#endif
f09e2e
+
f09e2e
+	/* Drain any buffered messages from the child */
f09e2e
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
f09e2e
+		;
f09e2e
+
f09e2e
 }
f09e2e
 
f09e2e
 
f09e2e
@@ -2361,3 +2458,86 @@ mm_answer_jpake_check_confirm(int sock,
f09e2e
 }
f09e2e
 
f09e2e
 #endif /* JPAKE */
f09e2e
+
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+int
f09e2e
+mm_answer_audit_unsupported_body(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	int what;
f09e2e
+
f09e2e
+	what = buffer_get_int(m);
f09e2e
+
f09e2e
+	audit_unsupported_body(what);
f09e2e
+
f09e2e
+	buffer_clear(m);
f09e2e
+
f09e2e
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_answer_audit_kex_body(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	int ctos, len;
f09e2e
+	char *cipher, *mac, *compress;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
+
f09e2e
+	ctos = buffer_get_int(m);
f09e2e
+	cipher = buffer_get_string(m, &len;;
f09e2e
+	mac = buffer_get_string(m, &len;;
f09e2e
+	compress = buffer_get_string(m, &len;;
f09e2e
+	pid = buffer_get_int64(m);
f09e2e
+	uid = buffer_get_int64(m);
f09e2e
+
f09e2e
+	audit_kex_body(ctos, cipher, mac, compress, pid, uid);
f09e2e
+
f09e2e
+	free(cipher);
f09e2e
+	free(mac);
f09e2e
+	free(compress);
f09e2e
+	buffer_clear(m);
f09e2e
+
f09e2e
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_answer_audit_session_key_free_body(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	int ctos;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
+
f09e2e
+	ctos = buffer_get_int(m);
f09e2e
+	pid = buffer_get_int64(m);
f09e2e
+	uid = buffer_get_int64(m);
f09e2e
+
f09e2e
+	audit_session_key_free_body(ctos, pid, uid);
f09e2e
+
f09e2e
+	buffer_clear(m);
f09e2e
+
f09e2e
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_answer_audit_server_key_free(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	int len;
f09e2e
+	char *fp;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
+
f09e2e
+	fp = buffer_get_string(m, &len;;
f09e2e
+	pid = buffer_get_int64(m);
f09e2e
+	uid = buffer_get_int64(m);
f09e2e
+
f09e2e
+	audit_destroy_sensitive_data(fp, pid, uid);
f09e2e
+
f09e2e
+	free(fp);
f09e2e
+	buffer_clear(m);
f09e2e
+
f09e2e
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SERVER_KEY_FREE, m);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+#endif /* SSH_AUDIT_EVENTS */
f09e2e
diff -up openssh-6.3p1/monitor.h.audit openssh-6.3p1/monitor.h
f09e2e
--- openssh-6.3p1/monitor.h.audit	2012-12-02 23:53:21.000000000 +0100
f09e2e
+++ openssh-6.3p1/monitor.h	2013-10-07 15:53:34.251717254 +0200
f09e2e
@@ -68,7 +68,13 @@ enum monitor_reqtype {
f09e2e
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
f09e2e
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
f09e2e
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
f09e2e
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
f09e2e
+	MONITOR_REQ_AUDIT_EVENT = 112,
f09e2e
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
f09e2e
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
f09e2e
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
f09e2e
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
f09e2e
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
f09e2e
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124, MONITOR_ANS_AUDIT_SERVER_KEY_FREE = 125
f09e2e
 
f09e2e
 };
f09e2e
 
f09e2e
diff -up openssh-6.4p1/monitor_wrap.c.audit openssh-6.4p1/monitor_wrap.c
f09e2e
--- openssh-6.4p1/monitor_wrap.c.audit	2013-11-08 13:26:47.062595006 +0100
f09e2e
+++ openssh-6.4p1/monitor_wrap.c	2013-11-08 13:26:47.089594877 +0100
f09e2e
@@ -433,7 +433,7 @@ mm_key_allowed(enum mm_keytype type, cha
f09e2e
  */
f09e2e
 
f09e2e
 int
f09e2e
-mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
+mm_key_verify(enum mm_keytype type, Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
 {
f09e2e
 	Buffer m;
f09e2e
 	u_char *blob;
f09e2e
@@ -447,6 +447,7 @@ mm_key_verify(Key *key, u_char *sig, u_i
f09e2e
 		return (0);
f09e2e
 
f09e2e
 	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, type);
f09e2e
 	buffer_put_string(&m, blob, len);
f09e2e
 	buffer_put_string(&m, sig, siglen);
f09e2e
 	buffer_put_string(&m, data, datalen);
f09e2e
@@ -464,6 +465,19 @@ mm_key_verify(Key *key, u_char *sig, u_i
f09e2e
 	return (verified);
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+mm_hostbased_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
+{
f09e2e
+	return mm_key_verify(MM_HOSTKEY, key, sig, siglen, data, datalen);
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_user_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
+{
f09e2e
+	return mm_key_verify(MM_USERKEY, key, sig, siglen, data, datalen);
f09e2e
+}
f09e2e
+
f09e2e
+
f09e2e
 /* Export key state after authentication */
f09e2e
 Newkeys *
f09e2e
 mm_newkeys_from_blob(u_char *blob, int blen)
f09e2e
@@ -642,12 +656,14 @@ mm_send_keystate(struct monitor *monitor
f09e2e
 		fatal("%s: conversion of newkeys failed", __func__);
f09e2e
 
f09e2e
 	buffer_put_string(&m, blob, bloblen);
f09e2e
+	memset(blob, 0, bloblen);
f09e2e
 	free(blob);
f09e2e
 
f09e2e
 	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
f09e2e
 		fatal("%s: conversion of newkeys failed", __func__);
f09e2e
 
f09e2e
 	buffer_put_string(&m, blob, bloblen);
f09e2e
+	memset(blob, 0, bloblen);
f09e2e
 	free(blob);
f09e2e
 
f09e2e
 	packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
f09e2e
@@ -1191,10 +1207,11 @@ mm_audit_event(ssh_audit_event_t event)
f09e2e
 	buffer_free(&m);
f09e2e
 }
f09e2e
 
f09e2e
-void
f09e2e
+int
f09e2e
 mm_audit_run_command(const char *command)
f09e2e
 {
f09e2e
 	Buffer m;
f09e2e
+	int handle;
f09e2e
 
f09e2e
 	debug3("%s entering command %s", __func__, command);
f09e2e
 
f09e2e
@@ -1202,6 +1219,26 @@ mm_audit_run_command(const char *command
f09e2e
 	buffer_put_cstring(&m, command);
f09e2e
 
f09e2e
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, &m);
f09e2e
+
f09e2e
+	handle = buffer_get_int(&m);
f09e2e
+	buffer_free(&m);
f09e2e
+
f09e2e
+	return (handle);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+mm_audit_end_command(int handle, const char *command)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	debug3("%s entering command %s", __func__, command);
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, handle);
f09e2e
+	buffer_put_cstring(&m, command);
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, &m);
f09e2e
 	buffer_free(&m);
f09e2e
 }
f09e2e
 #endif /* SSH_AUDIT_EVENTS */
f09e2e
@@ -1453,3 +1490,72 @@ mm_jpake_check_confirm(const BIGNUM *k,
f09e2e
 	return success;
f09e2e
 }
f09e2e
 #endif /* JPAKE */
f09e2e
+
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+void
f09e2e
+mm_audit_unsupported_body(int what)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, what);
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
f09e2e
+				  &m);
f09e2e
+
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+mm_audit_kex_body(int ctos, char *cipher, char *mac, char *compress, pid_t pid,
f09e2e
+		  uid_t uid)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, ctos);
f09e2e
+	buffer_put_cstring(&m, cipher);
f09e2e
+	buffer_put_cstring(&m, (mac ? mac : ""));
f09e2e
+	buffer_put_cstring(&m, compress);
f09e2e
+	buffer_put_int64(&m, pid);
f09e2e
+	buffer_put_int64(&m, uid);
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
f09e2e
+				  &m);
f09e2e
+
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, ctos);
f09e2e
+	buffer_put_int64(&m, pid);
f09e2e
+	buffer_put_int64(&m, uid);
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
f09e2e
+				  &m);
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_cstring(&m, fp);
f09e2e
+	buffer_put_int64(&m, pid);
f09e2e
+	buffer_put_int64(&m, uid);
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SERVER_KEY_FREE,
f09e2e
+				  &m);
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+#endif /* SSH_AUDIT_EVENTS */
f09e2e
diff -up openssh-6.3p1/monitor_wrap.h.audit openssh-6.3p1/monitor_wrap.h
f09e2e
--- openssh-6.3p1/monitor_wrap.h.audit	2011-06-20 06:42:23.000000000 +0200
f09e2e
+++ openssh-6.3p1/monitor_wrap.h	2013-10-07 15:53:34.252717250 +0200
f09e2e
@@ -49,7 +49,8 @@ int mm_key_allowed(enum mm_keytype, char
f09e2e
 int mm_user_key_allowed(struct passwd *, Key *);
f09e2e
 int mm_hostbased_key_allowed(struct passwd *, char *, char *, Key *);
f09e2e
 int mm_auth_rhosts_rsa_key_allowed(struct passwd *, char *, char *, Key *);
f09e2e
-int mm_key_verify(Key *, u_char *, u_int, u_char *, u_int);
f09e2e
+int mm_hostbased_key_verify(Key *, u_char *, u_int, u_char *, u_int);
f09e2e
+int mm_user_key_verify(Key *, u_char *, u_int, u_char *, u_int);
f09e2e
 int mm_auth_rsa_key_allowed(struct passwd *, BIGNUM *, Key **);
f09e2e
 int mm_auth_rsa_verify_response(Key *, BIGNUM *, u_char *);
f09e2e
 BIGNUM *mm_auth_rsa_generate_challenge(Key *);
f09e2e
@@ -74,7 +75,12 @@ void mm_sshpam_free_ctx(void *);
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
 #include "audit.h"
f09e2e
 void mm_audit_event(ssh_audit_event_t);
f09e2e
-void mm_audit_run_command(const char *);
f09e2e
+int mm_audit_run_command(const char *);
f09e2e
+void mm_audit_end_command(int, const char *);
f09e2e
+void mm_audit_unsupported_body(int);
f09e2e
+void mm_audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
f09e2e
+void mm_audit_session_key_free_body(int, pid_t, uid_t);
f09e2e
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
f09e2e
 #endif
f09e2e
 
f09e2e
 struct Session;
f09e2e
diff -up openssh-6.3p1/packet.c.audit openssh-6.3p1/packet.c
f09e2e
--- openssh-6.3p1/packet.c.audit	2013-10-07 15:53:34.231717347 +0200
f09e2e
+++ openssh-6.3p1/packet.c	2013-10-07 16:08:00.764639577 +0200
f09e2e
@@ -61,6 +61,7 @@
f09e2e
 #include <time.h>
f09e2e
 
f09e2e
 #include "xmalloc.h"
f09e2e
+#include "audit.h"
f09e2e
 #include "buffer.h"
f09e2e
 #include "packet.h"
f09e2e
 #include "crc32.h"
f09e2e
@@ -476,6 +477,13 @@ packet_get_connection_out(void)
f09e2e
 	return active_state->connection_out;
f09e2e
 }
f09e2e
 
f09e2e
+static int
f09e2e
+packet_state_has_keys (const struct session_state *state)
f09e2e
+{
f09e2e
+	return state != NULL &&
f09e2e
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
f09e2e
+}
f09e2e
+
f09e2e
 /* Closes the connection and clears and frees internal data structures. */
f09e2e
 
f09e2e
 void
f09e2e
@@ -484,13 +492,6 @@ packet_close(void)
f09e2e
 	if (!active_state->initialized)
f09e2e
 		return;
f09e2e
 	active_state->initialized = 0;
f09e2e
-	if (active_state->connection_in == active_state->connection_out) {
f09e2e
-		shutdown(active_state->connection_out, SHUT_RDWR);
f09e2e
-		close(active_state->connection_out);
f09e2e
-	} else {
f09e2e
-		close(active_state->connection_in);
f09e2e
-		close(active_state->connection_out);
f09e2e
-	}
f09e2e
 	buffer_free(&active_state->input);
f09e2e
 	buffer_free(&active_state->output);
f09e2e
 	buffer_free(&active_state->outgoing_packet);
f09e2e
@@ -499,8 +500,18 @@ packet_close(void)
f09e2e
 		buffer_free(&active_state->compression_buffer);
f09e2e
 		buffer_compress_uninit();
f09e2e
 	}
f09e2e
-	cipher_cleanup(&active_state->send_context);
f09e2e
-	cipher_cleanup(&active_state->receive_context);
f09e2e
+	if (packet_state_has_keys(active_state)) {
f09e2e
+		cipher_cleanup(&active_state->send_context);
f09e2e
+		cipher_cleanup(&active_state->receive_context);
f09e2e
+		audit_session_key_free(2);
f09e2e
+	}
f09e2e
+	if (active_state->connection_in == active_state->connection_out) {
f09e2e
+		shutdown(active_state->connection_out, SHUT_RDWR);
f09e2e
+		close(active_state->connection_out);
f09e2e
+	} else {
f09e2e
+		close(active_state->connection_in);
f09e2e
+		close(active_state->connection_out);
f09e2e
+	}
f09e2e
 }
f09e2e
 
f09e2e
 /* Sets remote side protocol flags. */
f09e2e
@@ -735,6 +746,25 @@ packet_send1(void)
f09e2e
 	 */
f09e2e
 }
f09e2e
 
f09e2e
+static void
f09e2e
+newkeys_destroy_and_free(Newkeys *newkeys)
f09e2e
+{
f09e2e
+	if (newkeys == NULL)
f09e2e
+		return;
f09e2e
+
f09e2e
+	free(newkeys->enc.name);
f09e2e
+
f09e2e
+	if (newkeys->mac.enabled) {
f09e2e
+		mac_clear(&newkeys->mac);
f09e2e
+		free(newkeys->mac.name);
f09e2e
+	}
f09e2e
+
f09e2e
+	free(newkeys->comp.name);
f09e2e
+
f09e2e
+	newkeys_destroy(newkeys);
f09e2e
+	free(newkeys);
f09e2e
+}
f09e2e
+
f09e2e
 void
f09e2e
 set_newkeys(int mode)
f09e2e
 {
f09e2e
@@ -760,21 +790,9 @@ set_newkeys(int mode)
f09e2e
 	}
f09e2e
 	if (active_state->newkeys[mode] != NULL) {
f09e2e
 		debug("set_newkeys: rekeying");
f09e2e
+		audit_session_key_free(mode);
f09e2e
 		cipher_cleanup(cc);
f09e2e
-		enc  = &active_state->newkeys[mode]->enc;
f09e2e
-		mac  = &active_state->newkeys[mode]->mac;
f09e2e
-		comp = &active_state->newkeys[mode]->comp;
f09e2e
-		mac_clear(mac);
f09e2e
-		memset(enc->iv,  0, enc->iv_len);
f09e2e
-		memset(enc->key, 0, enc->key_len);
f09e2e
-		memset(mac->key, 0, mac->key_len);
f09e2e
-		free(enc->name);
f09e2e
-		free(enc->iv);
f09e2e
-		free(enc->key);
f09e2e
-		free(mac->name);
f09e2e
-		free(mac->key);
f09e2e
-		free(comp->name);
f09e2e
-		free(active_state->newkeys[mode]);
f09e2e
+		newkeys_destroy_and_free(active_state->newkeys[mode]);
f09e2e
 	}
f09e2e
 	active_state->newkeys[mode] = kex_get_newkeys(mode);
f09e2e
 	if (active_state->newkeys[mode] == NULL)
f09e2e
@@ -2003,6 +2021,47 @@ packet_get_newkeys(int mode)
f09e2e
 	return (void *)active_state->newkeys[mode];
f09e2e
 }
f09e2e
 
f09e2e
+static void
f09e2e
+packet_destroy_state(struct session_state *state)
f09e2e
+{
f09e2e
+	if (state == NULL)
f09e2e
+		return;
f09e2e
+
f09e2e
+	cipher_cleanup(&state->receive_context);
f09e2e
+	cipher_cleanup(&state->send_context);
f09e2e
+
f09e2e
+	buffer_free(&state->input);
f09e2e
+	buffer_free(&state->output);
f09e2e
+	buffer_free(&state->outgoing_packet);
f09e2e
+	buffer_free(&state->incoming_packet);
f09e2e
+	buffer_free(&state->compression_buffer);
f09e2e
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
f09e2e
+	state->newkeys[MODE_IN] = NULL;
f09e2e
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
f09e2e
+	state->newkeys[MODE_OUT] = NULL;
f09e2e
+	mac_destroy(state->packet_discard_mac);
f09e2e
+//	TAILQ_HEAD(, packet) outgoing;
f09e2e
+//	memset(state, 0, sizeof(state));
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+packet_destroy_all(int audit_it, int privsep)
f09e2e
+{
f09e2e
+	if (audit_it)
f09e2e
+		audit_it = packet_state_has_keys (active_state) ||
f09e2e
+			packet_state_has_keys (backup_state);
f09e2e
+	packet_destroy_state(active_state);
f09e2e
+	packet_destroy_state(backup_state);
f09e2e
+	if (audit_it) {
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		if (privsep)
f09e2e
+			audit_session_key_free(2);
f09e2e
+		else
f09e2e
+			audit_session_key_free_body(2, getpid(), getuid());
f09e2e
+#endif
f09e2e
+	}
f09e2e
+}
f09e2e
+
f09e2e
 /*
f09e2e
  * Save the state for the real connection, and use a separate state when
f09e2e
  * resuming a suspended connection.
f09e2e
@@ -2010,18 +2069,12 @@ packet_get_newkeys(int mode)
f09e2e
 void
f09e2e
 packet_backup_state(void)
f09e2e
 {
f09e2e
-	struct session_state *tmp;
f09e2e
-
f09e2e
 	close(active_state->connection_in);
f09e2e
 	active_state->connection_in = -1;
f09e2e
 	close(active_state->connection_out);
f09e2e
 	active_state->connection_out = -1;
f09e2e
-	if (backup_state)
f09e2e
-		tmp = backup_state;
f09e2e
-	else
f09e2e
-		tmp = alloc_session_state();
f09e2e
 	backup_state = active_state;
f09e2e
-	active_state = tmp;
f09e2e
+	active_state = alloc_session_state();
f09e2e
 }
f09e2e
 
f09e2e
 /*
f09e2e
@@ -2038,9 +2091,7 @@ packet_restore_state(void)
f09e2e
 	backup_state = active_state;
f09e2e
 	active_state = tmp;
f09e2e
 	active_state->connection_in = backup_state->connection_in;
f09e2e
-	backup_state->connection_in = -1;
f09e2e
 	active_state->connection_out = backup_state->connection_out;
f09e2e
-	backup_state->connection_out = -1;
f09e2e
 	len = buffer_len(&backup_state->input);
f09e2e
 	if (len > 0) {
f09e2e
 		buf = buffer_ptr(&backup_state->input);
f09e2e
@@ -2048,4 +2099,10 @@ packet_restore_state(void)
f09e2e
 		buffer_clear(&backup_state->input);
f09e2e
 		add_recv_bytes(len);
f09e2e
 	}
f09e2e
+	backup_state->connection_in = -1;
f09e2e
+	backup_state->connection_out = -1;
f09e2e
+	packet_destroy_state(backup_state);
f09e2e
+	free(backup_state);
f09e2e
+	backup_state = NULL;
f09e2e
 }
f09e2e
+
f09e2e
diff -up openssh-6.3p1/packet.h.audit openssh-6.3p1/packet.h
f09e2e
--- openssh-6.3p1/packet.h.audit	2013-07-18 08:12:45.000000000 +0200
f09e2e
+++ openssh-6.3p1/packet.h	2013-10-07 15:53:34.252717250 +0200
f09e2e
@@ -124,4 +124,5 @@ void	 packet_restore_state(void);
f09e2e
 void	*packet_get_input(void);
f09e2e
 void	*packet_get_output(void);
f09e2e
 
f09e2e
+void	 packet_destroy_all(int, int);
f09e2e
 #endif				/* PACKET_H */
f09e2e
diff -up openssh-6.3p1/session.c.audit openssh-6.3p1/session.c
f09e2e
--- openssh-6.3p1/session.c.audit	2013-07-20 05:21:53.000000000 +0200
f09e2e
+++ openssh-6.3p1/session.c	2013-10-07 16:03:43.975861636 +0200
f09e2e
@@ -137,7 +137,7 @@ extern int log_stderr;
f09e2e
 extern int debug_flag;
f09e2e
 extern u_int utmp_len;
f09e2e
 extern int startup_pipe;
f09e2e
-extern void destroy_sensitive_data(void);
f09e2e
+extern void destroy_sensitive_data(int);
f09e2e
 extern Buffer loginmsg;
f09e2e
 
f09e2e
 /* original command from peer. */
f09e2e
@@ -745,6 +745,14 @@ do_exec_pty(Session *s, const char *comm
f09e2e
 	/* Parent.  Close the slave side of the pseudo tty. */
f09e2e
 	close(ttyfd);
f09e2e
 
f09e2e
+#ifndef HAVE_OSF_SIA
f09e2e
+	/* do_login in the child did not affect state in this process,
f09e2e
+	   compensate.  From an architectural standpoint, this is extremely
f09e2e
+	   ugly. */
f09e2e
+	if (!(options.use_login && command == NULL))
f09e2e
+		audit_count_session_open();
f09e2e
+#endif
f09e2e
+
f09e2e
 	/* Enter interactive session. */
f09e2e
 	s->ptymaster = ptymaster;
f09e2e
 	packet_set_interactive(1, 
f09e2e
@@ -816,15 +824,19 @@ do_exec(Session *s, const char *command)
f09e2e
 	}
f09e2e
 
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
+	if (s->command != NULL || s->command_handle != -1)
f09e2e
+		fatal("do_exec: command already set");
f09e2e
 	if (command != NULL)
f09e2e
-		PRIVSEP(audit_run_command(command));
f09e2e
+		s->command = xstrdup(command);
f09e2e
 	else if (s->ttyfd == -1) {
f09e2e
 		char *shell = s->pw->pw_shell;
f09e2e
 
f09e2e
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
f09e2e
 			shell =_PATH_BSHELL;
f09e2e
-		PRIVSEP(audit_run_command(shell));
f09e2e
+		s->command = xstrdup(shell);
f09e2e
 	}
f09e2e
+	if (s->command != NULL)
f09e2e
+		s->command_handle = PRIVSEP(audit_run_command(s->command));
f09e2e
 #endif
f09e2e
 	if (s->ttyfd != -1)
f09e2e
 		ret = do_exec_pty(s, command);
f09e2e
@@ -1642,7 +1654,10 @@ do_child(Session *s, const char *command
f09e2e
 	int r = 0;
f09e2e
 
f09e2e
 	/* remove hostkey from the child's memory */
f09e2e
-	destroy_sensitive_data();
f09e2e
+	destroy_sensitive_data(1);
f09e2e
+	/* Don't audit this - both us and the parent would be talking to the
f09e2e
+	   monitor over a single socket, with no synchronization. */
f09e2e
+	packet_destroy_all(0, 1);
f09e2e
 
f09e2e
 	/* Force a password change */
f09e2e
 	if (s->authctxt->force_pwchange) {
f09e2e
@@ -1869,6 +1884,7 @@ session_unused(int id)
f09e2e
 	sessions[id].ttyfd = -1;
f09e2e
 	sessions[id].ptymaster = -1;
f09e2e
 	sessions[id].x11_chanids = NULL;
f09e2e
+	sessions[id].command_handle = -1;
f09e2e
 	sessions[id].next_unused = sessions_first_unused;
f09e2e
 	sessions_first_unused = id;
f09e2e
 }
f09e2e
@@ -1951,6 +1967,19 @@ session_open(Authctxt *authctxt, int cha
f09e2e
 }
f09e2e
 
f09e2e
 Session *
f09e2e
+session_by_id(int id)
f09e2e
+{
f09e2e
+	if (id >= 0 && id < sessions_nalloc) {
f09e2e
+		Session *s = &sessions[id];
f09e2e
+		if (s->used)
f09e2e
+			return s;
f09e2e
+	}
f09e2e
+	debug("session_by_id: unknown id %d", id);
f09e2e
+	session_dump();
f09e2e
+	return NULL;
f09e2e
+}
f09e2e
+
f09e2e
+Session *
f09e2e
 session_by_tty(char *tty)
f09e2e
 {
f09e2e
 	int i;
f09e2e
@@ -2467,6 +2496,30 @@ session_exit_message(Session *s, int sta
f09e2e
 		chan_write_failed(c);
f09e2e
 }
f09e2e
 
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+void
f09e2e
+session_end_command2(Session *s)
f09e2e
+{
f09e2e
+	if (s->command != NULL) {
f09e2e
+		audit_end_command(s->command_handle, s->command);
f09e2e
+		free(s->command);
f09e2e
+		s->command = NULL;
f09e2e
+		s->command_handle = -1;
f09e2e
+	}
f09e2e
+}
f09e2e
+
f09e2e
+static void
f09e2e
+session_end_command(Session *s)
f09e2e
+{
f09e2e
+	if (s->command != NULL) {
f09e2e
+		PRIVSEP(audit_end_command(s->command_handle, s->command));
f09e2e
+		free(s->command);
f09e2e
+		s->command = NULL;
f09e2e
+		s->command_handle = -1;
f09e2e
+	}
f09e2e
+}
f09e2e
+#endif
f09e2e
+
f09e2e
 void
f09e2e
 session_close(Session *s)
f09e2e
 {
f09e2e
@@ -2475,6 +2528,10 @@ session_close(Session *s)
f09e2e
 	debug("session_close: session %d pid %ld", s->self, (long)s->pid);
f09e2e
 	if (s->ttyfd != -1)
f09e2e
 		session_pty_cleanup(s);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	if (s->command)
f09e2e
+		session_end_command(s);
f09e2e
+#endif
f09e2e
 	free(s->term);
f09e2e
 	free(s->display);
f09e2e
 	free(s->x11_chanids);
f09e2e
@@ -2688,6 +2745,15 @@ do_authenticated2(Authctxt *authctxt)
f09e2e
 	server_loop2(authctxt);
f09e2e
 }
f09e2e
 
f09e2e
+static void
f09e2e
+do_cleanup_one_session(Session *s)
f09e2e
+{
f09e2e
+	session_pty_cleanup2(s);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	session_end_command2(s);
f09e2e
+#endif
f09e2e
+}
f09e2e
+
f09e2e
 void
f09e2e
 do_cleanup(Authctxt *authctxt)
f09e2e
 {
f09e2e
@@ -2736,5 +2802,5 @@ do_cleanup(Authctxt *authctxt)
f09e2e
 	 * or if running in monitor.
f09e2e
 	 */
f09e2e
 	if (!use_privsep || mm_is_monitor())
f09e2e
-		session_destroy_all(session_pty_cleanup2);
f09e2e
+		session_destroy_all(do_cleanup_one_session);
f09e2e
 }
f09e2e
diff -up openssh-6.3p1/session.h.audit openssh-6.3p1/session.h
f09e2e
--- openssh-6.3p1/session.h.audit	2008-05-19 07:34:50.000000000 +0200
f09e2e
+++ openssh-6.3p1/session.h	2013-10-07 15:53:34.253717245 +0200
f09e2e
@@ -60,6 +60,12 @@ struct Session {
f09e2e
 		char	*name;
f09e2e
 		char	*val;
f09e2e
 	} *env;
f09e2e
+
f09e2e
+	/* exec */
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	int	command_handle;
f09e2e
+	char	*command;
f09e2e
+#endif
f09e2e
 };
f09e2e
 
f09e2e
 void	 do_authenticated(Authctxt *);
f09e2e
@@ -72,8 +78,10 @@ void	 session_close_by_pid(pid_t, int);
f09e2e
 void	 session_close_by_channel(int, void *);
f09e2e
 void	 session_destroy_all(void (*)(Session *));
f09e2e
 void	 session_pty_cleanup2(Session *);
f09e2e
+void	 session_end_command2(Session *);
f09e2e
 
f09e2e
 Session	*session_new(void);
f09e2e
+Session *session_by_id(int);
f09e2e
 Session	*session_by_tty(char *);
f09e2e
 void	 session_close(Session *);
f09e2e
 void	 do_setusercontext(struct passwd *);
f09e2e
diff -up openssh-6.3p1/sshd.c.audit openssh-6.3p1/sshd.c
f09e2e
--- openssh-6.3p1/sshd.c.audit	2013-10-07 15:53:34.221717393 +0200
f09e2e
+++ openssh-6.3p1/sshd.c	2013-10-07 15:53:34.254717240 +0200
f09e2e
@@ -119,6 +119,7 @@
f09e2e
 #endif
f09e2e
 #include "monitor_wrap.h"
f09e2e
 #include "roaming.h"
f09e2e
+#include "audit.h"
f09e2e
 #include "ssh-sandbox.h"
f09e2e
 #include "version.h"
f09e2e
 
f09e2e
@@ -260,7 +261,7 @@ Buffer loginmsg;
f09e2e
 struct passwd *privsep_pw = NULL;
f09e2e
 
f09e2e
 /* Prototypes for various functions defined later in this file. */
f09e2e
-void destroy_sensitive_data(void);
f09e2e
+void destroy_sensitive_data(int);
f09e2e
 void demote_sensitive_data(void);
f09e2e
 
f09e2e
 static void do_ssh1_kex(void);
f09e2e
@@ -279,6 +280,15 @@ close_listen_socks(void)
f09e2e
 	num_listen_socks = -1;
f09e2e
 }
f09e2e
 
f09e2e
+/*
f09e2e
+ * Is this process listening for clients (i.e. not specific to any specific
f09e2e
+ * client connection?)
f09e2e
+ */
f09e2e
+int listening_for_clients(void)
f09e2e
+{
f09e2e
+	return num_listen_socks > 0;
f09e2e
+}
f09e2e
+
f09e2e
 static void
f09e2e
 close_startup_pipes(void)
f09e2e
 {
f09e2e
@@ -550,22 +560,47 @@ sshd_exchange_identification(int sock_in
f09e2e
 	}
f09e2e
 }
f09e2e
 
f09e2e
-/* Destroy the host and server keys.  They will no longer be needed. */
f09e2e
+/*
f09e2e
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
f09e2e
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
f09e2e
+ */
f09e2e
 void
f09e2e
-destroy_sensitive_data(void)
f09e2e
+destroy_sensitive_data(int privsep)
f09e2e
 {
f09e2e
 	int i;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
 
f09e2e
 	if (sensitive_data.server_key) {
f09e2e
 		key_free(sensitive_data.server_key);
f09e2e
 		sensitive_data.server_key = NULL;
f09e2e
 	}
f09e2e
+	pid = getpid();
f09e2e
+	uid = getuid();
f09e2e
 	for (i = 0; i < options.num_host_key_files; i++) {
f09e2e
 		if (sensitive_data.host_keys[i]) {
f09e2e
+			char *fp;
f09e2e
+
f09e2e
+			if (key_is_private(sensitive_data.host_keys[i]))
f09e2e
+				fp = key_fingerprint(sensitive_data.host_keys[i],
f09e2e
+					FIPS_mode() ? SSH_FP_SHA1 : SSH_FP_MD5,
f09e2e
+					SSH_FP_HEX);
f09e2e
+			else
f09e2e
+				fp = NULL;
f09e2e
 			key_free(sensitive_data.host_keys[i]);
f09e2e
 			sensitive_data.host_keys[i] = NULL;
f09e2e
+			if (fp != NULL) {
f09e2e
+				if (privsep)
f09e2e
+					PRIVSEP(audit_destroy_sensitive_data(fp,
f09e2e
+						pid, uid));
f09e2e
+				else
f09e2e
+					audit_destroy_sensitive_data(fp,
f09e2e
+						pid, uid);
f09e2e
+				free(fp);
f09e2e
+			}
f09e2e
 		}
f09e2e
-		if (sensitive_data.host_certificates[i]) {
f09e2e
+		if (sensitive_data.host_certificates
f09e2e
+		    && sensitive_data.host_certificates[i]) {
f09e2e
 			key_free(sensitive_data.host_certificates[i]);
f09e2e
 			sensitive_data.host_certificates[i] = NULL;
f09e2e
 		}
f09e2e
@@ -579,6 +614,8 @@ void
f09e2e
 demote_sensitive_data(void)
f09e2e
 {
f09e2e
 	Key *tmp;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
 	int i;
f09e2e
 
f09e2e
 	if (sensitive_data.server_key) {
f09e2e
@@ -587,13 +624,27 @@ demote_sensitive_data(void)
f09e2e
 		sensitive_data.server_key = tmp;
f09e2e
 	}
f09e2e
 
f09e2e
+	pid = getpid();
f09e2e
+	uid = getuid();
f09e2e
 	for (i = 0; i < options.num_host_key_files; i++) {
f09e2e
 		if (sensitive_data.host_keys[i]) {
f09e2e
+			char *fp;
f09e2e
+
f09e2e
+			if (key_is_private(sensitive_data.host_keys[i]))
f09e2e
+				fp = key_fingerprint(sensitive_data.host_keys[i],
f09e2e
+					FIPS_mode() ? SSH_FP_SHA1 : SSH_FP_MD5,
f09e2e
+					SSH_FP_HEX);
f09e2e
+			else
f09e2e
+				fp = NULL;
f09e2e
 			tmp = key_demote(sensitive_data.host_keys[i]);
f09e2e
 			key_free(sensitive_data.host_keys[i]);
f09e2e
 			sensitive_data.host_keys[i] = tmp;
f09e2e
 			if (tmp->type == KEY_RSA1)
f09e2e
 				sensitive_data.ssh1_host_key = tmp;
f09e2e
+			if (fp != NULL) {
f09e2e
+				audit_destroy_sensitive_data(fp, pid, uid);
f09e2e
+				free(fp);
f09e2e
+			}
f09e2e
 		}
f09e2e
 		/* Certs do not need demotion */
f09e2e
 	}
770374
@@ -652,7 +703,7 @@ privsep_preauth(Authctxt *authctxt)
770374
 
770374
 	if (use_privsep == PRIVSEP_ON)
770374
 		box = ssh_sandbox_init();
770374
-	pid = fork();
770374
+	pmonitor->m_pid = pid = fork();
770374
 	if (pid == -1) {
770374
 		fatal("fork of unprivileged child failed");
770374
 	} else if (pid != 0) {
f09e2e
@@ -708,6 +759,8 @@ privsep_preauth(Authctxt *authctxt)
f09e2e
 	}
f09e2e
 }
f09e2e
 
f09e2e
+extern Newkeys *current_keys[];
f09e2e
+
f09e2e
 static void
f09e2e
 privsep_postauth(Authctxt *authctxt)
f09e2e
 {
f09e2e
@@ -732,6 +785,10 @@ privsep_postauth(Authctxt *authctxt)
f09e2e
 	else if (pmonitor->m_pid != 0) {
f09e2e
 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
f09e2e
 		buffer_clear(&loginmsg);
f09e2e
+ 		newkeys_destroy(current_keys[MODE_OUT]);
f09e2e
+		newkeys_destroy(current_keys[MODE_IN]);
f09e2e
+		audit_session_key_free_body(2, getpid(), getuid());
f09e2e
+		packet_destroy_all(0, 0);
f09e2e
 		monitor_child_postauth(pmonitor);
f09e2e
 
f09e2e
 		/* NEVERREACHED */
f09e2e
@@ -1178,6 +1235,7 @@ server_accept_loop(int *sock_in, int *so
f09e2e
 		if (received_sigterm) {
f09e2e
 			logit("Received signal %d; terminating.",
f09e2e
 			    (int) received_sigterm);
f09e2e
+			destroy_sensitive_data(0);
f09e2e
 			close_listen_socks();
f09e2e
 			unlink(options.pid_file);
f09e2e
 			exit(received_sigterm == SIGTERM ? 0 : 255);
f09e2e
@@ -2093,6 +2151,7 @@ main(int ac, char **av)
f09e2e
 	 */
f09e2e
 	if (use_privsep) {
f09e2e
 		mm_send_keystate(pmonitor);
f09e2e
+		packet_destroy_all(1, 1);
f09e2e
 		exit(0);
f09e2e
 	}
f09e2e
 
f09e2e
@@ -2135,7 +2194,7 @@ main(int ac, char **av)
f09e2e
 		privsep_postauth(authctxt);
f09e2e
 		/* the monitor process [priv] will not return */
f09e2e
 		if (!compat20)
f09e2e
-			destroy_sensitive_data();
f09e2e
+			destroy_sensitive_data(0);
f09e2e
 	}
f09e2e
 
f09e2e
 	packet_set_timeout(options.client_alive_interval,
f09e2e
@@ -2145,6 +2204,9 @@ main(int ac, char **av)
f09e2e
 	do_authenticated(authctxt);
f09e2e
 
f09e2e
 	/* The connection has been terminated. */
f09e2e
+	packet_destroy_all(1, 1);
f09e2e
+	destroy_sensitive_data(1);
f09e2e
+
f09e2e
 	packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
f09e2e
 	packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
f09e2e
 	verbose("Transferred: sent %llu, received %llu bytes",
f09e2e
@@ -2302,6 +2364,10 @@ do_ssh1_kex(void)
f09e2e
 		if (cookie[i] != packet_get_char())
f09e2e
 			packet_disconnect("IP Spoofing check bytes do not match.");
f09e2e
 
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	audit_kex(2, cipher_name(cipher_type), "crc", "none");
f09e2e
+#endif
f09e2e
+
f09e2e
 	debug("Encryption type: %.200s", cipher_name(cipher_type));
f09e2e
 
f09e2e
 	/* Get the encrypted integer. */
f09e2e
@@ -2368,7 +2434,7 @@ do_ssh1_kex(void)
f09e2e
 			session_id[i] = session_key[i] ^ session_key[i + 16];
f09e2e
 	}
f09e2e
 	/* Destroy the private and public keys. No longer. */
f09e2e
-	destroy_sensitive_data();
f09e2e
+	destroy_sensitive_data(0);
f09e2e
 
f09e2e
 	if (use_privsep)
f09e2e
 		mm_ssh1_session_id(session_id);
f09e2e
@@ -2480,6 +2546,16 @@ do_ssh2_kex(void)
f09e2e
 void
f09e2e
 cleanup_exit(int i)
f09e2e
 {
f09e2e
+	static int in_cleanup = 0;
f09e2e
+	int is_privsep_child;
f09e2e
+
f09e2e
+	/* cleanup_exit can be called at the very least from the privsep
f09e2e
+	   wrappers used for auditing.  Make sure we don't recurse
f09e2e
+	   indefinitely. */
f09e2e
+	if (in_cleanup)
f09e2e
+		_exit(i);
f09e2e
+	in_cleanup = 1;
f09e2e
+
f09e2e
 	if (the_authctxt) {
f09e2e
 		do_cleanup(the_authctxt);
f09e2e
 		if (use_privsep && privsep_is_preauth && pmonitor->m_pid > 1) {
f09e2e
@@ -2490,9 +2566,14 @@ cleanup_exit(int i)
f09e2e
 				    pmonitor->m_pid, strerror(errno));
f09e2e
 		}
f09e2e
 	}
770374
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
f09e2e
+	if (sensitive_data.host_keys != NULL)
f09e2e
+		destroy_sensitive_data(is_privsep_child);
f09e2e
+	packet_destroy_all(1, is_privsep_child);
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
f09e2e
-	if (!use_privsep || mm_is_monitor())
f09e2e
+	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&
f09e2e
+	    (!use_privsep || mm_is_monitor()))
f09e2e
 		audit_event(SSH_CONNECTION_ABANDON);
f09e2e
 #endif
f09e2e
 	_exit(i);