File openssh-7.6p1-audit.patch of Package openssh.10835

# HG changeset patch
# Parent  e2684163df2bc1f6af65c0b657d16a9dcba124ee
Extended auditing through the Linux Auditing subsystem
RH patch from git://pkgs.fedoraproject.org/openssh.git

Index: openssh-7.6p1/Makefile.in
===================================================================
--- openssh-7.6p1.orig/Makefile.in	2019-03-12 14:38:32.416914318 +0100
+++ openssh-7.6p1/Makefile.in	2019-03-12 14:38:33.264919086 +0100
@@ -102,6 +102,8 @@ LIBSSH_OBJS += fips.o
 
 LIBSSH_OBJS += kexgssc.o kexgsss.o
 
+LIBSSH_OBJS += auditstub.o
+
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
 	sshconnect.o sshconnect2.o mux.o
 
Index: openssh-7.6p1/audit-bsm.c
===================================================================
--- openssh-7.6p1.orig/audit-bsm.c	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/audit-bsm.c	2019-03-12 14:38:33.264919086 +0100
@@ -373,10 +373,23 @@ audit_connection_from(const char *host,
 #endif
 }
 
-void
+int
 audit_run_command(const char *command)
 {
 	/* not implemented */
+	return 0;
+}
+
+void
+audit_end_command(int handle, const char *command)
+{
+	/* not implemented */
+}
+
+void
+audit_count_session_open(void)
+{
+	/* not necessary */
 }
 
 void
@@ -391,6 +404,12 @@ audit_session_close(struct logininfo *li
 	/* not implemented */
 }
 
+int
+audit_keyusage(int host_user, char *fp, int rv)
+{
+	/* not implemented */
+}
+
 void
 audit_event(ssh_audit_event_t event)
 {
@@ -452,4 +471,34 @@ audit_event(ssh_audit_event_t event)
 		debug("%s: unhandled event %d", __func__, event);
 	}
 }
+
+void
+audit_unsupported_body(int what)
+{
+	/* not implemented */
+}
+
+void
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
+{
+	/* not implemented */
+}
+
+void
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
+{
+	/* not implemented */
+}
+
+void
+audit_destroy_sensitive_data(const char *fp)
+{
+	/* not implemented */
+}
+
+void
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
+{
+	/* not implemented */
+}
 #endif /* BSM */
Index: openssh-7.6p1/audit-linux.c
===================================================================
--- openssh-7.6p1.orig/audit-linux.c	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/audit-linux.c	2019-03-12 14:38:33.264919086 +0100
@@ -33,27 +33,40 @@
 
 #include "log.h"
 #include "audit.h"
+#include "key.h"
+#include "hostfile.h"
+#include "auth.h"
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
+#include "servconf.h"
 #include "canohost.h"
 #include "packet.h"
-
+#include "cipher.h"
+#include "channels.h"
+#include "session.h"
+
+#define AUDIT_LOG_SIZE 256
+
+extern ServerOptions options;
+extern Authctxt *the_authctxt;
+extern u_int utmp_len;
 const char *audit_username(void);
 
-int
-linux_audit_record_event(int uid, const char *username, const char *hostname,
-    const char *ip, const char *ttyn, int success)
+static void
+linux_audit_user_logxxx(int uid, const char *username,
+    const char *ip, const char *ttyn, int success, int event)
 {
 	int audit_fd, rc, saved_errno;
 
 	if ((audit_fd = audit_open()) < 0) {
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
 		    errno == EAFNOSUPPORT)
-			return 1; /* No audit support in kernel */
+			return; /* No audit support in kernel */
 		else
-			return 0; /* Must prevent login */
+			goto fatal_report; /* Must prevent login */
 	}
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
+	rc = audit_log_acct_message(audit_fd, event,
 	    NULL, "login", username ? username : "(unknown)",
-	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
 	saved_errno = errno;
 	close(audit_fd);
 
@@ -65,9 +78,96 @@ linux_audit_record_event(int uid, const
 		rc = 0;
 	errno = saved_errno;
 
-	return rc >= 0;
+	if (rc < 0) {
+fatal_report:
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
+	}
 }
 
+static void
+linux_audit_user_auth(int uid, const char *username,
+    const char *ip, const char *ttyn, int success, int event)
+{
+	int audit_fd, rc, saved_errno;
+	static const char *event_name[] = {
+		"maxtries exceeded",
+		"root denied",
+		"success",
+		"none",
+		"password",
+		"challenge-response",
+		"pubkey",
+		"hostbased",
+		"gssapi",
+		"invalid user",
+		"nologin",
+		"connection closed",
+		"connection abandoned",
+		"unknown"
+	};
+
+	audit_fd = audit_open();
+	if (audit_fd < 0) {
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
+		    errno == EAFNOSUPPORT)
+			return; /* No audit support in kernel */
+		else
+			goto fatal_report; /* Must prevent login */
+	}
+
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
+		event = SSH_AUDIT_UNKNOWN;
+
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
+	    NULL, event_name[event], username ? username : "(unknown)",
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
+	saved_errno = errno;
+	close(audit_fd);
+	/*
+	 * Do not report error if the error is EPERM and sshd is run as non
+	 * root user.
+	 */
+	if ((rc == -EPERM) && (geteuid() != 0))
+		rc = 0;
+	errno = saved_errno;
+	if (rc < 0) {
+fatal_report:
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
+	}
+}
+
+int
+audit_keyusage(int host_user, char *fp, int rv)
+{
+	char buf[AUDIT_LOG_SIZE];
+	int audit_fd, rc, saved_errno;
+
+	audit_fd = audit_open();
+	if (audit_fd < 0) {
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
+					 errno == EAFNOSUPPORT)
+			return 1; /* No audit support in kernel */
+		else
+			return 0; /* Must prevent login */
+	}
+	snprintf(buf, sizeof(buf), "%s_auth grantors=auth-key", host_user ? "pubkey" : "hostbased");
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(active_state), NULL, rv);
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
+		goto out;
+	snprintf(buf, sizeof(buf), "op=negotiate kind=auth-key fp=%s", fp);
+	rc = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER, buf, NULL,
+		ssh_remote_ipaddr(active_state), NULL, rv);
+out:
+	saved_errno = errno;
+	audit_close(audit_fd);
+	errno = saved_errno;
+	/* do not report error if the error is EPERM and sshd is run as non root user */
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
+}
+
+static int user_login_count = 0;
+
 /* Below is the sshd audit API code */
 
 void
@@ -76,24 +176,55 @@ audit_connection_from(const char *host,
 	/* not implemented */
 }
 
-void
+int
 audit_run_command(const char *command)
 {
-	/* not implemented */
+	if (!user_login_count++)
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
+		    ssh_remote_ipaddr(active_state),
+		    "ssh", 1, AUDIT_USER_LOGIN);
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
+	    ssh_remote_ipaddr(active_state),
+	    "ssh", 1, AUDIT_USER_START);
+	return 0;
+}
+
+void
+audit_end_command(int handle, const char *command)
+{
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
+	    ssh_remote_ipaddr(active_state),
+	    "ssh", 1, AUDIT_USER_END);
+	if (user_login_count && !--user_login_count)
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
+		    ssh_remote_ipaddr(active_state),
+		    "ssh", 1, AUDIT_USER_LOGOUT);
+}
+
+void
+audit_count_session_open(void)
+{
+	user_login_count++;
 }
 
 void
 audit_session_open(struct logininfo *li)
 {
-	if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,
-	    li->line, 1) == 0)
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
+	if (!user_login_count++)
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
+		    li->line, 1, AUDIT_USER_LOGIN);
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
+	    li->line, 1, AUDIT_USER_START);
 }
 
 void
 audit_session_close(struct logininfo *li)
 {
-	/* not implemented */
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
+	    li->line, 1, AUDIT_USER_END);
+	if (user_login_count && !--user_login_count)
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
+		    li->line, 1, AUDIT_USER_LOGOUT);
 }
 
 void
@@ -102,25 +233,155 @@ audit_event(ssh_audit_event_t event)
 	struct ssh *ssh = active_state; /* XXX */
 
 	switch(event) {
-	case SSH_AUTH_SUCCESS:
-	case SSH_CONNECTION_CLOSE:
 	case SSH_NOLOGIN:
-	case SSH_LOGIN_EXCEED_MAXTRIES:
 	case SSH_LOGIN_ROOT_DENIED:
+		linux_audit_user_auth(-1, audit_username(),
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
+		linux_audit_user_logxxx(-1, audit_username(),
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
 		break;
-	case SSH_AUTH_FAIL_NONE:
 	case SSH_AUTH_FAIL_PASSWD:
+		if (options.use_pam)
+			break;
+	case SSH_LOGIN_EXCEED_MAXTRIES:
 	case SSH_AUTH_FAIL_KBDINT:
 	case SSH_AUTH_FAIL_PUBKEY:
 	case SSH_AUTH_FAIL_HOSTBASED:
 	case SSH_AUTH_FAIL_GSSAPI:
+		linux_audit_user_auth(-1, audit_username(),
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
+		break;
+
+	case SSH_CONNECTION_CLOSE:
+		if (user_login_count) {
+			while (user_login_count--)
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
+				    ssh_remote_ipaddr(ssh),
+				    "ssh", 1, AUDIT_USER_END);
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
+			    ssh_remote_ipaddr(ssh),
+			    "ssh", 1, AUDIT_USER_LOGOUT);
+		}
+		break;
+
+	case SSH_CONNECTION_ABANDON:
 	case SSH_INVALID_USER:
-		linux_audit_record_event(-1, audit_username(), NULL,
-		    ssh_remote_ipaddr(ssh), "sshd", 0);
+		linux_audit_user_logxxx(-1, audit_username(),
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
 		break;
 	default:
 		debug("%s: unhandled event %d", __func__, event);
 		break;
 	}
 }
+
+void
+audit_unsupported_body(int what)
+{
+#ifdef AUDIT_CRYPTO_SESSION
+	char buf[AUDIT_LOG_SIZE];
+	const static char *name[] = { "cipher", "mac", "comp" };
+	char *s;
+	int audit_fd;
+
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
+		name[what], ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())),
+		ssh_local_port(active_state));
+	free(s);
+	audit_fd = audit_open();
+	if (audit_fd < 0)
+		/* no problem, the next instruction will be fatal() */
+		return;
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 0);
+	audit_close(audit_fd);
+#endif
+}
+
+const static char *direction[] = { "from-server", "from-client", "both" };
+
+void
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
+	       uid_t uid)
+{
+#ifdef AUDIT_CRYPTO_SESSION
+	char buf[AUDIT_LOG_SIZE];
+	int audit_fd, audit_ok;
+	const struct sshcipher *cipher = cipher_by_name(enc);
+	char *s;
+
+	snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d mac=%s pfs=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs,
+		(intmax_t)pid, (intmax_t)uid,
+		ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())), ssh_local_port(active_state));
+	free(s);
+	audit_fd = audit_open();
+	if (audit_fd < 0) {
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
+					 errno == EAFNOSUPPORT)
+			return; /* No audit support in kernel */
+		else
+			fatal("cannot open audit"); /* Must prevent login */
+	}
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
+	audit_close(audit_fd);
+	/* do not abort if the error is EPERM and sshd is run as non root user */
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
+		fatal("cannot write into audit"); /* Must prevent login */
+#endif
+}
+
+void
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
+{
+	char buf[AUDIT_LOG_SIZE];
+	int audit_fd, audit_ok;
+	char *s;
+
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
+		 ssh_remote_port(active_state),
+		 (s = get_local_ipaddr(packet_get_connection_in())),
+		 ssh_local_port(active_state));
+	free(s);
+	audit_fd = audit_open();
+	if (audit_fd < 0) {
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
+					 errno != EAFNOSUPPORT)
+			error("cannot open audit");
+		return;
+	}
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
+	audit_close(audit_fd);
+	/* do not abort if the error is EPERM and sshd is run as non root user */
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
+		error("cannot write into audit");
+}
+
+void
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
+{
+	char buf[AUDIT_LOG_SIZE];
+	int audit_fd, audit_ok;
+
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
+		fp, (intmax_t)pid, (intmax_t)uid);
+	audit_fd = audit_open();
+	if (audit_fd < 0) {
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
+					 errno != EAFNOSUPPORT)
+			error("cannot open audit");
+		return;
+	}
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
+			buf, NULL,
+			listening_for_clients() ? NULL : ssh_remote_ipaddr(active_state),
+			NULL, 1);
+	audit_close(audit_fd);
+	/* do not abort if the error is EPERM and sshd is run as non root user */
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
+		error("cannot write into audit");
+}
 #endif /* USE_LINUX_AUDIT */
Index: openssh-7.6p1/audit.c
===================================================================
--- openssh-7.6p1.orig/audit.c	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/audit.c	2019-03-12 14:38:33.264919086 +0100
@@ -26,6 +26,7 @@
 
 #include <stdarg.h>
 #include <string.h>
+#include <unistd.h>
 
 #ifdef SSH_AUDIT_EVENTS
 
@@ -34,6 +35,12 @@
 #include "key.h"
 #include "hostfile.h"
 #include "auth.h"
+#include "ssh-gss.h"
+#include "monitor_wrap.h"
+#include "xmalloc.h"
+#include "misc.h"
+#include "servconf.h"
+#include "ssherr.h"
 
 /*
  * Care must be taken when using this since it WILL NOT be initialized when
@@ -41,6 +48,7 @@
  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
  */
 extern Authctxt *the_authctxt;
+extern ServerOptions options;
 
 /* Maybe add the audit class to struct Authmethod? */
 ssh_audit_event_t
@@ -69,13 +77,10 @@ audit_classify_auth(const char *method)
 const char *
 audit_username(void)
 {
-	static const char unknownuser[] = "(unknown user)";
-	static const char invaliduser[] = "(invalid user)";
+	static const char unknownuser[] = "(unknown)";
 
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
 		return (unknownuser);
-	if (!the_authctxt->valid)
-		return (invaliduser);
 	return (the_authctxt->user);
 }
 
@@ -109,6 +114,35 @@ audit_event_lookup(ssh_audit_event_t ev)
 	return(event_lookup[i].name);
 }
 
+void
+audit_key(int host_user, int *rv, const Key *key)
+{
+	char *fp;
+
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
+	if (audit_keyusage(host_user, fp, (*rv == 0)) == 0)
+		*rv = -SSH_ERR_INTERNAL_ERROR;
+	free(fp);
+}
+
+void
+audit_unsupported(int what)
+{
+	PRIVSEP(audit_unsupported_body(what));
+}
+
+void
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
+{
+	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, pfs, getpid(), getuid()));
+}
+
+void
+audit_session_key_free(int ctos)
+{
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
+}
+
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
 /*
  * Null implementations of audit functions.
@@ -138,6 +172,17 @@ audit_event(ssh_audit_event_t event)
 }
 
 /*
+ * Called when a child process has called, or will soon call,
+ * audit_session_open.
+ */
+void
+audit_count_session_open(void)
+{
+	debug("audit count session open euid %d user %s", geteuid(),
+	      audit_username());
+}
+
+/*
  * Called when a user session is started.  Argument is the tty allocated to
  * the session, or NULL if no tty was allocated.
  *
@@ -172,13 +217,82 @@ audit_session_close(struct logininfo *li
 /*
  * This will be called when a user runs a non-interactive command.  Note that
  * it may be called multiple times for a single connection since SSH2 allows
- * multiple sessions within a single connection.
+ * multiple sessions within a single connection.  Returns a "handle" for
+ * audit_end_command.
  */
-void
+int
 audit_run_command(const char *command)
 {
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
 	    audit_username(), command);
+	return 0;
+}
+
+/*
+ * This will be called when the non-interactive command finishes.  Note that
+ * it may be called multiple times for a single connection since SSH2 allows
+ * multiple sessions within a single connection.  "handle" should come from
+ * the corresponding audit_run_command.
+ */
+void
+audit_end_command(int handle, const char *command)
+{
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
+	    audit_username(), command);
+}
+
+/*
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
+ *
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
+ */
+int
+audit_keyusage(int host_user, char *fp, int rv)
+{
+	debug("audit %s key usage euid %d user %s fingerprint %s, result %d",
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(),
+		fp, rv);
+}
+
+/*
+ * This will be called when the protocol negotiation fails.
+ */
+void
+audit_unsupported_body(int what)
+{
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
+}
+
+/*
+ * This will be called on succesfull protocol negotiation.
+ */
+void
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
+	       uid_t uid)
+{
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s pfs %s from pid %ld uid %u",
+		(unsigned)geteuid(), ctos, enc, mac, compress, pfs, (long)pid,
+	        (unsigned)uid);
+}
+
+/*
+ * This will be called on succesfull session key discard
+ */
+void
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
+{
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
+}
+
+/*
+ * This will be called on destroy private part of the server key
+ */
+void
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
+{
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
+		geteuid(), fp, (long)pid, (unsigned)uid);
 }
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
 #endif /* SSH_AUDIT_EVENTS */
Index: openssh-7.6p1/audit.h
===================================================================
--- openssh-7.6p1.orig/audit.h	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/audit.h	2019-03-12 14:38:33.264919086 +0100
@@ -26,6 +26,7 @@
 # define _SSH_AUDIT_H
 
 #include "loginrec.h"
+#include "key.h"
 
 enum ssh_audit_event_type {
 	SSH_LOGIN_EXCEED_MAXTRIES,
@@ -43,13 +44,32 @@ enum ssh_audit_event_type {
 	SSH_CONNECTION_ABANDON,	/* closed without completing auth */
 	SSH_AUDIT_UNKNOWN
 };
+
+enum ssh_audit_kex {
+	SSH_AUDIT_UNSUPPORTED_CIPHER,
+	SSH_AUDIT_UNSUPPORTED_MAC,
+	SSH_AUDIT_UNSUPPORTED_COMPRESSION
+};
 typedef enum ssh_audit_event_type ssh_audit_event_t;
 
+int	listening_for_clients(void);
+
 void	audit_connection_from(const char *, int);
 void	audit_event(ssh_audit_event_t);
+void	audit_count_session_open(void);
 void	audit_session_open(struct logininfo *);
 void	audit_session_close(struct logininfo *);
-void	audit_run_command(const char *);
+int	audit_run_command(const char *);
+void 	audit_end_command(int, const char *);
 ssh_audit_event_t audit_classify_auth(const char *);
+int	audit_keyusage(int, char *, int);
+void	audit_key(int, int *, const Key *);
+void	audit_unsupported(int);
+void	audit_kex(int, char *, char *, char *, char *);
+void	audit_unsupported_body(int);
+void	audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
+void	audit_session_key_free(int ctos);
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
 
 #endif /* _SSH_AUDIT_H */
Index: openssh-7.6p1/auditstub.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ openssh-7.6p1/auditstub.c	2019-03-12 14:38:33.264919086 +0100
@@ -0,0 +1,50 @@
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
+
+/*
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
+ */
+
+#include <sys/types.h>
+
+void
+audit_unsupported(int n)
+{
+}
+
+void
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
+{
+}
+
+void
+audit_session_key_free(int ctos)
+{
+}
+
+void
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
+{
+}
Index: openssh-7.6p1/auth.c
===================================================================
--- openssh-7.6p1.orig/auth.c	2019-03-12 14:38:32.420914340 +0100
+++ openssh-7.6p1/auth.c	2019-03-12 14:38:33.264919086 +0100
@@ -360,7 +360,7 @@ auth_log(Authctxt *authctxt, int authent
 # endif
 #endif
 #ifdef SSH_AUDIT_EVENTS
-	if (authenticated == 0 && !authctxt->postponed)
+	if (authenticated == 0 && !authctxt->postponed && !partial)
 		audit_event(audit_classify_auth(method));
 #endif
 }
@@ -599,9 +599,6 @@ getpwnamallow(const char *user)
 		record_failed_login(user,
 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
 #endif
-#ifdef SSH_AUDIT_EVENTS
-		audit_event(SSH_INVALID_USER);
-#endif /* SSH_AUDIT_EVENTS */
 		return (NULL);
 	}
 	if (!allowed_user(pw))
Index: openssh-7.6p1/auth.h
===================================================================
--- openssh-7.6p1.orig/auth.h	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/auth.h	2019-03-12 14:38:33.264919086 +0100
@@ -194,6 +194,7 @@ struct passwd * getpwnamallow(const char
 
 char	*expand_authorized_keys(const char *, struct passwd *pw);
 char	*authorized_principals_file(struct passwd *);
+int	 user_key_verify(const struct sshkey *, const u_char *, size_t, const u_char *, size_t, u_int);
 
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
@@ -213,6 +214,7 @@ struct sshkey	*get_hostkey_private_by_ty
 int	 get_hostkey_index(struct sshkey *, int, struct ssh *);
 int	 sshd_hostkey_sign(struct sshkey *, struct sshkey *, u_char **,
 	     size_t *, const u_char *, size_t, const char *, u_int);
+int	 hostbased_key_verify(const struct sshkey *, const u_char *, size_t, const u_char *, size_t, u_int);
 
 /* debug messages during authentication */
 void	 auth_debug_add(const char *fmt,...) __attribute__((format(printf, 1, 2)));
Index: openssh-7.6p1/auth2-hostbased.c
===================================================================
--- openssh-7.6p1.orig/auth2-hostbased.c	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/auth2-hostbased.c	2019-03-12 14:38:33.264919086 +0100
@@ -143,7 +143,7 @@ userauth_hostbased(struct ssh *ssh)
 	/* test for allowed key and correct signature */
 	authenticated = 0;
 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
-	    PRIVSEP(sshkey_verify(key, sig, slen,
+	    PRIVSEP(hostbased_key_verify(key, sig, slen,
 	    sshbuf_ptr(b), sshbuf_len(b), ssh->compat)) == 0)
 		authenticated = 1;
 
@@ -160,6 +160,19 @@ done:
 	return authenticated;
 }
 
+int
+hostbased_key_verify(const struct sshkey *key, const u_char *sig, size_t slen,
+    const u_char *data, size_t datalen, u_int compat)
+{
+	int rv;
+
+	rv = sshkey_verify(key, sig, slen, data, datalen, compat);
+#ifdef SSH_AUDIT_EVENTS
+	audit_key(0, &rv, key);
+#endif
+	return rv;
+}
+
 /* return 1 if given hostkey is allowed */
 int
 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Index: openssh-7.6p1/auth2-pubkey.c
===================================================================
--- openssh-7.6p1.orig/auth2-pubkey.c	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/auth2-pubkey.c	2019-03-12 14:38:33.264919086 +0100
@@ -197,7 +197,7 @@ userauth_pubkey(struct ssh *ssh)
 		/* test for correct signature */
 		authenticated = 0;
 		if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
-		    PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b),
+		    PRIVSEP(user_key_verify(key, sig, slen, sshbuf_ptr(b),
 		    sshbuf_len(b), ssh->compat)) == 0) {
 			authenticated = 1;
 		}
@@ -241,6 +241,19 @@ done:
 	return authenticated;
 }
 
+int
+user_key_verify(const struct sshkey *key, const u_char *sig, size_t slen,
+    const u_char *data, size_t datalen, u_int compat)
+{
+	int rv;
+
+	rv = sshkey_verify(key, sig, slen, data, datalen, compat);
+#ifdef SSH_AUDIT_EVENTS
+	audit_key(1, &rv, key);
+#endif
+	return rv;
+}
+
 static int
 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
 {
Index: openssh-7.6p1/auth2.c
===================================================================
--- openssh-7.6p1.orig/auth2.c	2019-03-12 14:38:32.420914340 +0100
+++ openssh-7.6p1/auth2.c	2019-03-12 14:38:33.264919086 +0100
@@ -247,9 +247,6 @@ input_userauth_request(int type, u_int32
 		} else {
 			/* Invalid user, fake password information */
 			authctxt->pw = fakepw();
-#ifdef SSH_AUDIT_EVENTS
-			PRIVSEP(audit_event(SSH_INVALID_USER));
-#endif
 		}
 #ifdef USE_PAM
 		if (options.use_pam)
Index: openssh-7.6p1/cipher.c
===================================================================
--- openssh-7.6p1.orig/cipher.c	2019-03-12 14:38:30.636904307 +0100
+++ openssh-7.6p1/cipher.c	2019-03-12 14:38:33.268919109 +0100
@@ -54,25 +54,6 @@
 #include "fips.h"
 #include "log.h"
 
-struct sshcipher {
-	char	*name;
-	u_int	block_size;
-	u_int	key_len;
-	u_int	iv_len;		/* defaults to block_size */
-	u_int	auth_len;
-	u_int	flags;
-#define CFLAG_CBC		(1<<0)
-#define CFLAG_CHACHAPOLY	(1<<1)
-#define CFLAG_AESCTR		(1<<2)
-#define CFLAG_NONE		(1<<3)
-#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
-#ifdef WITH_OPENSSL
-	const EVP_CIPHER	*(*evptype)(void);
-#else
-	void	*ignored;
-#endif
-};
-
 static const struct sshcipher ciphers_all[] = {
 #ifdef WITH_OPENSSL
 	{ "3des-cbc",		8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
@@ -446,7 +427,7 @@ cipher_get_length(struct sshcipher_ctx *
 void
 cipher_free(struct sshcipher_ctx *cc)
 {
-	if (cc == NULL)
+	if (cc == NULL || cc->cipher == NULL)
 		return;
 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
 		explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
Index: openssh-7.6p1/cipher.h
===================================================================
--- openssh-7.6p1.orig/cipher.h	2019-03-12 14:38:30.636904307 +0100
+++ openssh-7.6p1/cipher.h	2019-03-12 14:38:33.268919109 +0100
@@ -45,7 +45,25 @@
 #define CIPHER_ENCRYPT		1
 #define CIPHER_DECRYPT		0
 
-struct sshcipher;
+struct sshcipher {
+	char	*name;
+	u_int	block_size;
+	u_int	key_len;
+	u_int	iv_len;		/* defaults to block_size */
+	u_int	auth_len;
+	u_int	flags;
+#define CFLAG_CBC		(1<<0)
+#define CFLAG_CHACHAPOLY	(1<<1)
+#define CFLAG_AESCTR		(1<<2)
+#define CFLAG_NONE		(1<<3)
+#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
+#ifdef WITH_OPENSSL
+	const EVP_CIPHER	*(*evptype)(void);
+#else
+	void	*ignored;
+#endif
+};
+
 struct sshcipher_ctx {
 	int	plaintext;
 	int	encrypt;
Index: openssh-7.6p1/config.h.in
===================================================================
--- openssh-7.6p1.orig/config.h.in	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/config.h.in	2019-03-12 14:38:33.268919109 +0100
@@ -1198,6 +1198,9 @@
 /* Define if you have syslen in utmpx.h */
 #undef HAVE_SYSLEN_IN_UTMPX
 
+/* Define if you want systemd support. */
+#undef HAVE_SYSTEMD
+
 /* Define to 1 if you have the <sys/audit.h> header file. */
 #undef HAVE_SYS_AUDIT_H
 
@@ -1696,6 +1699,9 @@
 /* Use btmp to log bad logins */
 #undef USE_BTMP
 
+/* platform uses an in-memory credentials cache */
+#undef USE_CCAPI
+
 /* Use libedit for sftp */
 #undef USE_LIBEDIT
 
@@ -1711,6 +1717,9 @@
 /* Use PIPES instead of a socketpair() */
 #undef USE_PIPES
 
+/* platform has the Security Authorization Session API */
+#undef USE_SECURITY_SESSION_API
+
 /* Define if you have Solaris privileges */
 #undef USE_SOLARIS_PRIVS
 
Index: openssh-7.6p1/configure
===================================================================
--- openssh-7.6p1.orig/configure	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/configure	2019-03-12 14:38:33.268919109 +0100
@@ -767,6 +767,7 @@ with_privsep_user
 with_sandbox
 with_selinux
 with_kerberos5
+with_systemd
 with_privsep_path
 with_xauth
 enable_strip
@@ -1462,6 +1463,7 @@ Optional Packages:
   --with-sandbox=style    Specify privilege separation sandbox (no, capsicum, darwin, rlimit, seccomp_filter, systrace, pledge)
   --with-selinux          Enable SELinux support
   --with-kerberos5=PATH   Enable Kerberos 5 support
+  --with-systemd          Enable systemd support
   --with-privsep-path=xxx Path for privilege separation chroot (default=/var/empty)
   --with-xauth=PATH       Specify path to xauth program
   --with-maildir=/path/to/mail    Specify your system mail directory
@@ -7334,6 +7336,63 @@ $as_echo "#define SSH_TUN_COMPAT_AF 1" >
 
 $as_echo "#define SSH_TUN_PREPEND_AF 1" >>confdefs.h
 
+	{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if we have the Security Authorization Session API" >&5
+$as_echo_n "checking if we have the Security Authorization Session API... " >&6; }
+	cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <Security/AuthSession.h>
+int
+main ()
+{
+SessionCreate(0, 0);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  ac_cv_use_security_session_api="yes"
+
+$as_echo "#define USE_SECURITY_SESSION_API 1" >>confdefs.h
+
+		 LIBS="$LIBS -framework Security"
+		 { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+else
+  ac_cv_use_security_session_api="no"
+		 { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+	{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if we have an in-memory credentials cache" >&5
+$as_echo_n "checking if we have an in-memory credentials cache... " >&6; }
+	cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <Kerberos/Kerberos.h>
+int
+main ()
+{
+cc_context_t c;
+		 (void) cc_initialize (&c, 0, NULL, NULL);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+
+$as_echo "#define USE_CCAPI 1" >>confdefs.h
+
+		 LIBS="$LIBS -framework Security"
+		 { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+		 if test "x$ac_cv_use_security_session_api" = "xno"; then
+			as_fn_error $? "*** Need a security framework to use the credentials cache API ***" "$LINENO" 5
+		fi
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
 
 	ac_fn_c_check_decl "$LINENO" "AU_IPv4" "ac_cv_have_decl_AU_IPv4" "$ac_includes_default"
 if test "x$ac_cv_have_decl_AU_IPv4" = xyes; then :
@@ -12686,6 +12745,7 @@ if ac_fn_c_try_run "$LINENO"; then :
 					as_fn_error $? "OpenSSL >= 1.0.1 required (have \"$ssl_library_ver\")" "$LINENO" 5
 			                ;;
 				100*)   ;; # 1.0.x
+				101*)   ;; # 1.1.x is supported by this patch too
 				200*)   ;; # LibreSSL
 			        *)
 					as_fn_error $? "OpenSSL >= 1.1.0 is not yet supported (have \"$ssl_library_ver\")" "$LINENO" 5
@@ -18092,6 +18152,135 @@ fi
 
 
 
+# Check whether user wants systemd support
+SYSTEMD_MSG="no"
+
+# Check whether --with-systemd was given.
+if test "${with_systemd+set}" = set; then :
+  withval=$with_systemd;  if test "x$withval" != "xno" ; then
+		if test -n "$ac_tool_prefix"; then
+  # Extract the first word of "${ac_tool_prefix}pkg-config", so it can be a program name with args.
+set dummy ${ac_tool_prefix}pkg-config; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if ${ac_cv_path_PKGCONFIG+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  case $PKGCONFIG in
+  [\\/]* | ?:[\\/]*)
+  ac_cv_path_PKGCONFIG="$PKGCONFIG" # Let the user override the test with a path.
+  ;;
+  *)
+  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_path_PKGCONFIG="$as_dir/$ac_word$ac_exec_ext"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+  ;;
+esac
+fi
+PKGCONFIG=$ac_cv_path_PKGCONFIG
+if test -n "$PKGCONFIG"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $PKGCONFIG" >&5
+$as_echo "$PKGCONFIG" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+fi
+if test -z "$ac_cv_path_PKGCONFIG"; then
+  ac_pt_PKGCONFIG=$PKGCONFIG
+  # Extract the first word of "pkg-config", so it can be a program name with args.
+set dummy pkg-config; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if ${ac_cv_path_ac_pt_PKGCONFIG+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  case $ac_pt_PKGCONFIG in
+  [\\/]* | ?:[\\/]*)
+  ac_cv_path_ac_pt_PKGCONFIG="$ac_pt_PKGCONFIG" # Let the user override the test with a path.
+  ;;
+  *)
+  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_path_ac_pt_PKGCONFIG="$as_dir/$ac_word$ac_exec_ext"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+  ;;
+esac
+fi
+ac_pt_PKGCONFIG=$ac_cv_path_ac_pt_PKGCONFIG
+if test -n "$ac_pt_PKGCONFIG"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_pt_PKGCONFIG" >&5
+$as_echo "$ac_pt_PKGCONFIG" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+  if test "x$ac_pt_PKGCONFIG" = x; then
+    PKGCONFIG="no"
+  else
+    case $cross_compiling:$ac_tool_warned in
+yes:)
+{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5
+$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;}
+ac_tool_warned=yes ;;
+esac
+    PKGCONFIG=$ac_pt_PKGCONFIG
+  fi
+else
+  PKGCONFIG="$ac_cv_path_PKGCONFIG"
+fi
+
+		if test "$PKGCONFIG" != "no"; then
+			{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for libsystemd" >&5
+$as_echo_n "checking for libsystemd... " >&6; }
+			if $PKGCONFIG --exists libsystemd; then
+				SYSTEMD_CFLAGS=`$PKGCONFIG --cflags libsystemd`
+				SYSTEMD_LIBS=`$PKGCONFIG --libs libsystemd`
+				CPPFLAGS="$CPPFLAGS $SYSTEMD_CFLAGS"
+				SSHDLIBS="$SSHDLIBS $SYSTEMD_LIBS"
+				{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+
+$as_echo "#define HAVE_SYSTEMD 1" >>confdefs.h
+
+				SYSTEMD_MSG="yes"
+			else
+				{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+			fi
+		fi
+	fi
+
+fi
+
+
+
 # Looking for programs, paths and files
 
 PRIVSEP_PATH=/var/empty
@@ -20615,6 +20804,7 @@ echo "                   libldns support
 echo "  Solaris process contract support: $SPC_MSG"
 echo "           Solaris project support: $SP_MSG"
 echo "         Solaris privilege support: $SPP_MSG"
+echo "                   systemd support: $SYSTEMD_MSG"
 echo "       IP address in \$DISPLAY hack: $DISPLAY_HACK_MSG"
 echo "           Translate v4 in v6 hack: $IPV4_IN6_HACK_MSG"
 echo "                  BSD Auth support: $BSD_AUTH_MSG"
Index: openssh-7.6p1/kex.c
===================================================================
--- openssh-7.6p1.orig/kex.c	2019-03-12 14:38:32.424914363 +0100
+++ openssh-7.6p1/kex.c	2019-03-12 14:38:33.268919109 +0100
@@ -53,6 +53,7 @@
 #include "ssherr.h"
 #include "sshbuf.h"
 #include "digest.h"
+#include "audit.h"
 
 #include "fips.h"
 
@@ -713,8 +714,12 @@ choose_enc(struct sshenc *enc, char *cli
 {
 	char *name = match_list(client, server, NULL);
 
-	if (name == NULL)
+	if (name == NULL) {
+#ifdef SSH_AUDIT_EVENTS
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_CIPHER);
+#endif
 		return SSH_ERR_NO_CIPHER_ALG_MATCH;
+	}
 	if ((enc->cipher = cipher_by_name(name)) == NULL) {
 		free(name);
 		return SSH_ERR_INTERNAL_ERROR;
@@ -734,8 +739,12 @@ choose_mac(struct ssh *ssh, struct sshma
 {
 	char *name = match_list(client, server, NULL);
 
-	if (name == NULL)
+	if (name == NULL) {
+#ifdef SSH_AUDIT_EVENTS
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_MAC);
+#endif
 		return SSH_ERR_NO_MAC_ALG_MATCH;
+	}
 	if (mac_setup(mac, name) < 0) {
 		free(name);
 		return SSH_ERR_INTERNAL_ERROR;
@@ -754,8 +763,12 @@ choose_comp(struct sshcomp *comp, char *
 {
 	char *name = match_list(client, server, NULL);
 
-	if (name == NULL)
+	if (name == NULL) {
+#ifdef SSH_AUDIT_EVENTS
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_COMPRESSION);
+#endif
 		return SSH_ERR_NO_COMPRESS_ALG_MATCH;
+	}
 	if (strcmp(name, "zlib@openssh.com") == 0) {
 		comp->type = COMP_DELAYED;
 	} else if (strcmp(name, "zlib") == 0) {
@@ -925,6 +938,10 @@ kex_choose_conf(struct ssh *ssh)
 		dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
 		dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
 		dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
+		debug("kex: %s need=%d dh_need=%d", kex->name, need, dh_need);
+#ifdef SSH_AUDIT_EVENTS
+		audit_kex(mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
+#endif
 	}
 	/* XXX need runden? */
 	kex->we_need = need;
@@ -1058,3 +1075,33 @@ dump_digest(char *msg, u_char *digest, i
 	sshbuf_dump_data(digest, len, stderr);
 }
 #endif
+
+static void
+enc_destroy(struct sshenc *enc)
+{
+	if (enc == NULL)
+		return;
+
+	if (enc->key) {
+		memset(enc->key, 0, enc->key_len);
+		free(enc->key);
+	}
+
+	if (enc->iv) {
+		memset(enc->iv,  0, enc->iv_len);
+		free(enc->iv);
+	}
+
+	memset(enc, 0, sizeof(*enc));
+}
+
+void
+newkeys_destroy(struct newkeys *newkeys)
+{
+	if (newkeys == NULL)
+		return;
+
+	enc_destroy(&newkeys->enc);
+	mac_destroy(&newkeys->mac);
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
+}
Index: openssh-7.6p1/kex.h
===================================================================
--- openssh-7.6p1.orig/kex.h	2019-03-12 14:38:32.424914363 +0100
+++ openssh-7.6p1/kex.h	2019-03-12 14:38:33.268919109 +0100
@@ -212,6 +212,8 @@ int	 kexgss_client(struct ssh *);
 int	 kexgss_server(struct ssh *);
 #endif
 
+void	newkeys_destroy(struct newkeys *newkeys);
+
 int	 kex_dh_hash(int, const char *, const char *,
     const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
     const BIGNUM *, const BIGNUM *, const BIGNUM *, u_char *, size_t *);
Index: openssh-7.6p1/key.h
===================================================================
--- openssh-7.6p1.orig/key.h	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/key.h	2019-03-12 14:38:33.268919109 +0100
@@ -43,6 +43,7 @@ typedef struct sshkey Key;
 #define key_ssh_name_plain	sshkey_ssh_name_plain
 #define key_type_from_name	sshkey_type_from_name
 #define key_is_cert		sshkey_is_cert
+#define key_is_private		sshkey_is_private
 #define key_type_plain		sshkey_type_plain
 #endif
 
Index: openssh-7.6p1/mac.c
===================================================================
--- openssh-7.6p1.orig/mac.c	2019-03-12 14:38:30.072901136 +0100
+++ openssh-7.6p1/mac.c	2019-03-12 14:38:33.268919109 +0100
@@ -280,6 +280,20 @@ mac_clear(struct sshmac *mac)
 	mac->umac_ctx = NULL;
 }
 
+void
+mac_destroy(struct sshmac *mac)
+{
+	if (mac == NULL)
+		return;
+
+	if (mac->key) {
+		memset(mac->key, 0, mac->key_len);
+		free(mac->key);
+	}
+
+	memset(mac, 0, sizeof(*mac));
+}
+
 /* XXX copied from ciphers_valid */
 #define	MAC_SEP	","
 int
Index: openssh-7.6p1/mac.h
===================================================================
--- openssh-7.6p1.orig/mac.h	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/mac.h	2019-03-12 14:38:33.268919109 +0100
@@ -49,5 +49,6 @@ int	 mac_compute(struct sshmac *, u_int3
 int	 mac_check(struct sshmac *, u_int32_t, const u_char *, size_t,
     const u_char *, size_t);
 void	 mac_clear(struct sshmac *);
+void	 mac_destroy(struct sshmac *);
 
 #endif /* SSHMAC_H */
Index: openssh-7.6p1/monitor.c
===================================================================
--- openssh-7.6p1.orig/monitor.c	2019-03-12 14:38:32.424914363 +0100
+++ openssh-7.6p1/monitor.c	2019-03-12 14:38:33.268919109 +0100
@@ -102,6 +102,7 @@
 #include "compat.h"
 #include "ssh2.h"
 #include "authfd.h"
+#include "audit.h"
 #include "match.h"
 #include "ssherr.h"
 
@@ -117,6 +118,8 @@ extern Buffer auth_debug;
 extern int auth_debug_init;
 extern Buffer loginmsg;
 
+extern void destroy_sensitive_data(int);
+
 /* State exported from the child */
 static struct sshbuf *child_state;
 
@@ -164,6 +167,11 @@ int mm_answer_gss_updatecreds(int, Buffe
 #ifdef SSH_AUDIT_EVENTS
 int mm_answer_audit_event(int, Buffer *);
 int mm_answer_audit_command(int, Buffer *);
+int mm_answer_audit_end_command(int, Buffer *);
+int mm_answer_audit_unsupported_body(int, Buffer *);
+int mm_answer_audit_kex_body(int, Buffer *);
+int mm_answer_audit_session_key_free_body(int, Buffer *);
+int mm_answer_audit_server_key_free(int, Buffer *);
 #endif
 
 static int monitor_read_log(struct monitor *);
@@ -216,6 +224,10 @@ struct mon_table mon_dispatch_proto20[]
 #endif
 #ifdef SSH_AUDIT_EVENTS
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
 #endif
 #ifdef BSD_AUTH
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
@@ -248,6 +260,11 @@ struct mon_table mon_dispatch_postauth20
 #ifdef SSH_AUDIT_EVENTS
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
 #endif
 #ifdef GSSAPI
     {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
@@ -1360,7 +1377,9 @@ mm_answer_keyverify(int sock, struct ssh
 	u_char *signature, *data, *blob;
 	size_t signaturelen, datalen, bloblen;
 	int r, ret, valid_data = 0, encoded_ret;
+	int type = 0;
 
+	type = buffer_get_int(m);
 	if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
 	    (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
 	    (r = sshbuf_get_string(m, &data, &datalen)) != 0)
@@ -1369,6 +1388,8 @@ mm_answer_keyverify(int sock, struct ssh
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
 	  !monitor_allowed_key(blob, bloblen))
 		fatal("%s: bad key, not previously allowed", __func__);
+	if (type != key_blobtype)
+		fatal("%s: bad key type", __func__);
 
 	/* XXX use sshkey_froms here; need to change key_blob, etc. */
 	if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
@@ -1378,21 +1399,24 @@ mm_answer_keyverify(int sock, struct ssh
 	case MM_USERKEY:
 		valid_data = monitor_valid_userblob(data, datalen);
 		auth_method = "publickey";
+		ret = user_key_verify(key, signature, signaturelen, data,
+		    datalen, active_state->compat);
 		break;
 	case MM_HOSTKEY:
 		valid_data = monitor_valid_hostbasedblob(data, datalen,
 		    hostbased_cuser, hostbased_chost);
+		ret = hostbased_key_verify(key, signature, signaturelen, data,
+		    datalen, active_state->compat);
 		auth_method = "hostbased";
 		break;
 	default:
 		valid_data = 0;
+		ret = 0;
 		break;
 	}
 	if (!valid_data)
 		fatal("%s: bad signature data blob", __func__);
 
-	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
-	    active_state->compat);
 	debug3("%s: %s %p signature %s", __func__, auth_method, key,
 	    (ret == 0) ? "verified" : "unverified");
 	auth2_record_key(authctxt, ret == 0, key);
@@ -1449,6 +1473,12 @@ mm_session_close(Session *s)
 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
 		session_pty_cleanup2(s);
 	}
+#ifdef SSH_AUDIT_EVENTS
+	if (s->command != NULL) {
+		debug3("%s: command %d", __func__, s->command_handle);
+		session_end_command2(s);
+	}
+#endif
 	session_unused(s->self);
 }
 
@@ -1552,6 +1582,8 @@ mm_answer_term(int sock, Buffer *req)
 		sshpam_cleanup();
 #endif
 
+	destroy_sensitive_data(0);
+
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
 		if (errno != EINTR)
 			exit(1);
@@ -1594,11 +1626,45 @@ mm_answer_audit_command(int socket, Buff
 {
 	u_int len;
 	char *cmd;
+	Session *s;
 
 	debug3("%s entering", __func__);
 	cmd = buffer_get_string(m, &len);
+
 	/* sanity check command, if so how? */
-	audit_run_command(cmd);
+	s = session_new();
+	if (s == NULL)
+		fatal("%s: error allocating a session", __func__);
+	s->command = cmd;
+#ifdef SSH_AUDIT_EVENTS
+	s->command_handle = audit_run_command(cmd);
+#endif
+
+	buffer_clear(m);
+	buffer_put_int(m, s->self);
+
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
+
+	return (0);
+}
+
+int
+mm_answer_audit_end_command(int socket, Buffer *m)
+{
+	int handle;
+	u_int len;
+	char *cmd;
+	Session *s;
+
+	debug3("%s entering", __func__);
+	handle = buffer_get_int(m);
+	cmd = buffer_get_string(m, &len);
+
+	s = session_by_id(handle);
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
+	    strcmp(s->command, cmd) != 0)
+		fatal("%s: invalid handle", __func__);
+	mm_session_close(s);
 	free(cmd);
 	return (0);
 }
@@ -1662,6 +1728,7 @@ monitor_apply_keystate(struct monitor *p
 void
 mm_get_keystate(struct monitor *pmonitor)
 {
+	Buffer m;
 	debug3("%s: Waiting for new keys", __func__);
 
 	if ((child_state = sshbuf_new()) == NULL)
@@ -1669,6 +1736,19 @@ mm_get_keystate(struct monitor *pmonitor
 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
 	    child_state);
 	debug3("%s: GOT new keys", __func__);
+
+#ifdef SSH_AUDIT_EVENTS
+	buffer_init(&m);
+	mm_request_receive_expect(pmonitor->m_sendfd,
+				  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
+	mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, &m);
+	buffer_free(&m);
+#endif
+
+	/* Drain any buffered messages from the child */
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
+		;
+
 }
 
 
@@ -1924,3 +2004,86 @@ mm_answer_gss_updatecreds(int socket, Bu
 
 #endif /* GSSAPI */
 
+#ifdef SSH_AUDIT_EVENTS
+int
+mm_answer_audit_unsupported_body(int sock, Buffer *m)
+{
+	int what;
+
+	what = buffer_get_int(m);
+
+	audit_unsupported_body(what);
+
+	buffer_clear(m);
+
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
+	return 0;
+}
+
+int
+mm_answer_audit_kex_body(int sock, Buffer *m)
+{
+	int ctos, len;
+	char *cipher, *mac, *compress, *pfs;
+	pid_t pid;
+	uid_t uid;
+
+	ctos = buffer_get_int(m);
+	cipher = buffer_get_string(m, &len);
+	mac = buffer_get_string(m, &len);
+	compress = buffer_get_string(m, &len);
+	pfs = buffer_get_string(m, &len);
+	pid = buffer_get_int64(m);
+	uid = buffer_get_int64(m);
+
+	audit_kex_body(ctos, cipher, mac, compress, pfs, pid, uid);
+
+	free(cipher);
+	free(mac);
+	free(compress);
+	free(pfs);
+	buffer_clear(m);
+
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
+	return 0;
+}
+
+int
+mm_answer_audit_session_key_free_body(int sock, Buffer *m)
+{
+	int ctos;
+	pid_t pid;
+	uid_t uid;
+
+	ctos = buffer_get_int(m);
+	pid = buffer_get_int64(m);
+	uid = buffer_get_int64(m);
+
+	audit_session_key_free_body(ctos, pid, uid);
+
+	buffer_clear(m);
+
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
+	return 0;
+}
+
+int
+mm_answer_audit_server_key_free(int sock, Buffer *m)
+{
+	int len;
+	char *fp;
+	pid_t pid;
+	uid_t uid;
+
+	fp = buffer_get_string(m, &len);
+	pid = buffer_get_int64(m);
+	uid = buffer_get_int64(m);
+
+	audit_destroy_sensitive_data(fp, pid, uid);
+
+	free(fp);
+	buffer_clear(m);
+
+	return 0;
+}
+#endif /* SSH_AUDIT_EVENTS */
Index: openssh-7.6p1/monitor.h
===================================================================
--- openssh-7.6p1.orig/monitor.h	2019-03-12 14:38:32.424914363 +0100
+++ openssh-7.6p1/monitor.h	2019-03-12 14:38:33.268919109 +0100
@@ -63,7 +63,13 @@ enum monitor_reqtype {
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
+	MONITOR_REQ_AUDIT_EVENT = 112,
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124,
 
 	MONITOR_REQ_GSSSIGN = 201, MONITOR_ANS_GSSSIGN = 202,
 	MONITOR_REQ_GSSUPCREDS = 203, MONITOR_ANS_GSSUPCREDS = 204,
Index: openssh-7.6p1/monitor_wrap.c
===================================================================
--- openssh-7.6p1.orig/monitor_wrap.c	2019-03-12 14:38:32.424914363 +0100
+++ openssh-7.6p1/monitor_wrap.c	2019-03-12 14:38:33.268919109 +0100
@@ -444,7 +444,7 @@ mm_key_allowed(enum mm_keytype type, con
  */
 
 int
-mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
+mm_sshkey_verify(enum mm_keytype type, const struct sshkey *key, const u_char *sig, size_t siglen,
     const u_char *data, size_t datalen, u_int compat)
 {
 	Buffer m;
@@ -459,6 +459,7 @@ mm_sshkey_verify(const struct sshkey *ke
 		return (0);
 
 	buffer_init(&m);
+	buffer_put_int(&m, type);
 	buffer_put_string(&m, blob, len);
 	buffer_put_string(&m, sig, siglen);
 	buffer_put_string(&m, data, datalen);
@@ -478,6 +479,20 @@ mm_sshkey_verify(const struct sshkey *ke
 	return 0;
 }
 
+int
+mm_hostbased_key_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
+    const u_char *data, size_t datalen, u_int compat)
+{
+	return mm_sshkey_verify(MM_HOSTKEY, key, sig, siglen, data, datalen, compat);
+}
+
+int
+mm_user_key_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
+    const u_char *data, size_t datalen, u_int compat)
+{
+	return mm_sshkey_verify(MM_USERKEY, key, sig, siglen, data, datalen, compat);
+}
+
 void
 mm_send_keystate(struct monitor *monitor)
 {
@@ -855,10 +870,11 @@ mm_audit_event(ssh_audit_event_t event)
 	buffer_free(&m);
 }
 
-void
+int
 mm_audit_run_command(const char *command)
 {
 	Buffer m;
+	int handle;
 
 	debug3("%s entering command %s", __func__, command);
 
@@ -866,6 +882,26 @@ mm_audit_run_command(const char *command
 	buffer_put_cstring(&m, command);
 
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, &m);
+
+	handle = buffer_get_int(&m);
+	buffer_free(&m);
+
+	return (handle);
+}
+
+void
+mm_audit_end_command(int handle, const char *command)
+{
+	Buffer m;
+
+	debug3("%s entering command %s", __func__, command);
+
+	buffer_init(&m);
+	buffer_put_int(&m, handle);
+	buffer_put_cstring(&m, command);
+
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, &m);
 	buffer_free(&m);
 }
 #endif /* SSH_AUDIT_EVENTS */
@@ -1001,3 +1037,70 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_cc
 
 #endif /* GSSAPI */
 
+#ifdef SSH_AUDIT_EVENTS
+void
+mm_audit_unsupported_body(int what)
+{
+	Buffer m;
+
+	buffer_init(&m);
+	buffer_put_int(&m, what);
+
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, &m);
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
+				  &m);
+
+	buffer_free(&m);
+}
+
+void
+mm_audit_kex_body(int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
+		  uid_t uid)
+{
+	Buffer m;
+
+	buffer_init(&m);
+	buffer_put_int(&m, ctos);
+	buffer_put_cstring(&m, cipher);
+	buffer_put_cstring(&m, (mac ? mac : "<implicit>"));
+	buffer_put_cstring(&m, compress);
+	buffer_put_cstring(&m, fps);
+	buffer_put_int64(&m, pid);
+	buffer_put_int64(&m, uid);
+
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, &m);
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
+				  &m);
+
+	buffer_free(&m);
+}
+
+void
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
+{
+	Buffer m;
+
+	buffer_init(&m);
+	buffer_put_int(&m, ctos);
+	buffer_put_int64(&m, pid);
+	buffer_put_int64(&m, uid);
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
+				  &m);
+	buffer_free(&m);
+}
+
+void
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
+{
+	Buffer m;
+
+	buffer_init(&m);
+	buffer_put_cstring(&m, fp);
+	buffer_put_int64(&m, pid);
+	buffer_put_int64(&m, uid);
+
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
+	buffer_free(&m);
+}
+#endif /* SSH_AUDIT_EVENTS */
Index: openssh-7.6p1/monitor_wrap.h
===================================================================
--- openssh-7.6p1.orig/monitor_wrap.h	2019-03-12 14:38:32.424914363 +0100
+++ openssh-7.6p1/monitor_wrap.h	2019-03-12 14:38:33.268919109 +0100
@@ -50,7 +50,9 @@ int mm_key_allowed(enum mm_keytype, cons
 int mm_user_key_allowed(struct passwd *, struct sshkey *, int);
 int mm_hostbased_key_allowed(struct passwd *, const char *,
     const char *, struct sshkey *);
-int mm_sshkey_verify(const struct sshkey *, const u_char *, size_t,
+int mm_hostbased_key_verify(const struct sshkey *, const u_char *, size_t,
+    const u_char *, size_t, u_int);
+int mm_user_key_verify(const struct sshkey *, const u_char *, size_t,
     const u_char *, size_t, u_int);
 
 #ifdef GSSAPI
@@ -75,7 +77,12 @@ void mm_sshpam_free_ctx(void *);
 #ifdef SSH_AUDIT_EVENTS
 #include "audit.h"
 void mm_audit_event(ssh_audit_event_t);
-void mm_audit_run_command(const char *);
+int mm_audit_run_command(const char *);
+void mm_audit_end_command(int, const char *);
+void mm_audit_unsupported_body(int);
+void mm_audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
+void mm_audit_session_key_free_body(int, pid_t, uid_t);
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
 #endif
 
 struct Session;
Index: openssh-7.6p1/packet.c
===================================================================
--- openssh-7.6p1.orig/packet.c	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/packet.c	2019-03-12 14:38:33.272919131 +0100
@@ -67,6 +67,7 @@
 #include "key.h"	/* typedefs XXX */
 
 #include "xmalloc.h"
+#include "audit.h"
 #include "crc32.h"
 #include "compat.h"
 #include "ssh2.h"
@@ -500,6 +501,13 @@ ssh_packet_get_connection_out(struct ssh
 	return ssh->state->connection_out;
 }
 
+static int
+packet_state_has_keys (const struct session_state *state)
+{
+	return state != NULL &&
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
+}
+
 /*
  * Returns the IP-address of the remote host as a string.  The returned
  * string must not be freed.
@@ -564,22 +572,19 @@ ssh_packet_close_internal(struct ssh *ss
 {
 	struct session_state *state = ssh->state;
 	u_int mode;
+	u_int had_keys = packet_state_has_keys(state);
 
 	if (!state->initialized)
 		return;
 	state->initialized = 0;
-	if (do_close) {
-		if (state->connection_in == state->connection_out) {
-			close(state->connection_out);
-		} else {
-			close(state->connection_in);
-			close(state->connection_out);
-		}
-	}
 	sshbuf_free(state->input);
+	state->input = NULL;
 	sshbuf_free(state->output);
+	state->output = NULL;
 	sshbuf_free(state->outgoing_packet);
+	state->outgoing_packet = NULL;
 	sshbuf_free(state->incoming_packet);
+	state->incoming_packet = NULL;
 	for (mode = 0; mode < MODE_MAX; mode++) {
 		kex_free_newkeys(state->newkeys[mode]);	/* current keys */
 		state->newkeys[mode] = NULL;
@@ -613,8 +618,18 @@ ssh_packet_close_internal(struct ssh *ss
 	}
 	cipher_free(state->send_context);
 	cipher_free(state->receive_context);
+	if (had_keys && state->server_side) {
+		/* Assuming this is called only from privsep child */
+		audit_session_key_free(MODE_MAX);
+	}
 	state->send_context = state->receive_context = NULL;
 	if (do_close) {
+		if (state->connection_in == state->connection_out) {
+			close(state->connection_out);
+		} else {
+			close(state->connection_in);
+			close(state->connection_out);
+		}
 		free(ssh->remote_ipaddr);
 		ssh->remote_ipaddr = NULL;
 		free(ssh->state);
@@ -852,6 +867,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
 		   (unsigned long long)state->p_read.blocks,
 		   (unsigned long long)state->p_send.bytes,
 		   (unsigned long long)state->p_send.blocks);
+		audit_session_key_free(mode);
 		cipher_free(*ccp);
 		*ccp = NULL;
 		kex_free_newkeys(state->newkeys[mode]);
@@ -2133,6 +2149,72 @@ ssh_packet_get_output(struct ssh *ssh)
 	return (void *)ssh->state->output;
 }
 
+static void
+newkeys_destroy_and_free(struct newkeys *newkeys)
+{
+	if (newkeys == NULL)
+		return;
+
+	free(newkeys->enc.name);
+
+	if (newkeys->mac.enabled) {
+		mac_clear(&newkeys->mac);
+		free(newkeys->mac.name);
+	}
+
+	free(newkeys->comp.name);
+
+	newkeys_destroy(newkeys);
+	free(newkeys);
+}
+
+static void
+packet_destroy_state(struct session_state *state)
+{
+	if (state == NULL)
+		return;
+
+	cipher_free(state->receive_context);
+	cipher_free(state->send_context);
+
+	buffer_free(state->input);
+	state->input = NULL;
+	buffer_free(state->output);
+	state->output = NULL;
+	buffer_free(state->outgoing_packet);
+	state->outgoing_packet = NULL;
+	buffer_free(state->incoming_packet);
+	state->incoming_packet = NULL;
+	if( state->compression_buffer ) {
+		buffer_free(state->compression_buffer);
+		state->compression_buffer = NULL;
+	}
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
+	state->newkeys[MODE_IN] = NULL;
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
+	state->newkeys[MODE_OUT] = NULL;
+	mac_destroy(state->packet_discard_mac);
+//	TAILQ_HEAD(, packet) outgoing;
+//	memset(state, 0, sizeof(state));
+}
+
+void
+packet_destroy_all(int audit_it, int privsep)
+{
+	if (audit_it)
+		audit_it = (active_state != NULL && packet_state_has_keys(active_state->state));
+	if (active_state != NULL)
+		packet_destroy_state(active_state->state);
+	if (audit_it) {
+#ifdef SSH_AUDIT_EVENTS
+		if (privsep)
+			audit_session_key_free(MODE_MAX);
+		else
+			audit_session_key_free_body(MODE_MAX, getpid(), getuid());
+#endif
+	}
+}
+
 /* Reset after_authentication and reset compression in post-auth privsep */
 static int
 ssh_packet_set_postauth(struct ssh *ssh)
Index: openssh-7.6p1/packet.h
===================================================================
--- openssh-7.6p1.orig/packet.h	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/packet.h	2019-03-12 14:38:33.272919131 +0100
@@ -217,4 +217,5 @@ extern struct ssh *active_state;
 # undef EC_POINT
 #endif
 
+void	 packet_destroy_all(int, int);
 #endif				/* PACKET_H */
Index: openssh-7.6p1/session.c
===================================================================
--- openssh-7.6p1.orig/session.c	2019-03-12 14:38:27.580887121 +0100
+++ openssh-7.6p1/session.c	2019-03-12 14:38:33.272919131 +0100
@@ -138,7 +138,7 @@ extern char *__progname;
 extern int debug_flag;
 extern u_int utmp_len;
 extern int startup_pipe;
-extern void destroy_sensitive_data(void);
+extern void destroy_sensitive_data(int);
 extern Buffer loginmsg;
 
 /* original command from peer. */
@@ -604,6 +604,14 @@ do_exec_pty(struct ssh *ssh, Session *s,
 	/* Parent.  Close the slave side of the pseudo tty. */
 	close(ttyfd);
 
+#if !defined(HAVE_OSF_SIA) && defined(SSH_AUDIT_EVENTS)
+	/* do_login in the child did not affect state in this process,
+	   compensate.  From an architectural standpoint, this is extremely
+	   ugly. */
+	if (command != NULL)
+		audit_count_session_open();
+#endif
+
 	/* Enter interactive session. */
 	s->ptymaster = ptymaster;
 	packet_set_interactive(1, 
@@ -695,15 +703,19 @@ do_exec(struct ssh *ssh, Session *s, con
 	    s->self);
 
 #ifdef SSH_AUDIT_EVENTS
+	if (s->command != NULL || s->command_handle != -1)
+		fatal("do_exec: command already set");
 	if (command != NULL)
-		PRIVSEP(audit_run_command(command));
+		s->command = xstrdup(command);
 	else if (s->ttyfd == -1) {
 		char *shell = s->pw->pw_shell;
 
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
 			shell =_PATH_BSHELL;
-		PRIVSEP(audit_run_command(shell));
+		s->command = xstrdup(shell);
 	}
+	if (s->command != NULL && s->ptyfd == -1)
+		s->command_handle = PRIVSEP(audit_run_command(s->command));
 #endif
 	if (s->ttyfd != -1)
 		ret = do_exec_pty(ssh, s, command);
@@ -1483,8 +1495,11 @@ do_child(struct ssh *ssh, Session *s, co
 	int r = 0;
 
 	/* remove hostkey from the child's memory */
-	destroy_sensitive_data();
+	destroy_sensitive_data(1);
 	packet_clear_keys();
+	/* Don't audit this - both us and the parent would be talking to the
+	   monitor over a single socket, with no synchronization. */
+	packet_destroy_all(0, 1);
 
 	/* Force a password change */
 	if (s->authctxt->force_pwchange) {
@@ -1695,6 +1710,9 @@ session_unused(int id)
 	sessions[id].ttyfd = -1;
 	sessions[id].ptymaster = -1;
 	sessions[id].x11_chanids = NULL;
+#ifdef SSH_AUDIT_EVENTS
+	sessions[id].command_handle = -1;
+#endif
 	sessions[id].next_unused = sessions_first_unused;
 	sessions_first_unused = id;
 }
@@ -1777,6 +1795,19 @@ session_open(Authctxt *authctxt, int cha
 }
 
 Session *
+session_by_id(int id)
+{
+	if (id >= 0 && id < sessions_nalloc) {
+		Session *s = &sessions[id];
+		if (s->used)
+			return s;
+	}
+	debug("%s: unknown id %d", __func__, id);
+	session_dump();
+	return NULL;
+}
+
+Session *
 session_by_tty(char *tty)
 {
 	int i;
@@ -2288,6 +2319,32 @@ session_exit_message(struct ssh *ssh, Se
 		chan_write_failed(ssh, c);
 }
 
+#ifdef SSH_AUDIT_EVENTS
+void
+session_end_command2(Session *s)
+{
+	if (s->command != NULL) {
+		if (s->command_handle != -1)
+			audit_end_command(s->command_handle, s->command);
+		free(s->command);
+		s->command = NULL;
+		s->command_handle = -1;
+	}
+}
+
+static void
+session_end_command(Session *s)
+{
+	if (s->command != NULL) {
+		if (s->command_handle != -1)
+			PRIVSEP(audit_end_command(s->command_handle, s->command));
+		free(s->command);
+		s->command = NULL;
+		s->command_handle = -1;
+	}
+}
+#endif
+
 void
 session_close(struct ssh *ssh, Session *s)
 {
@@ -2329,6 +2386,10 @@ session_close(struct ssh *ssh, Session *
 
 	if (s->ttyfd != -1)
 		session_pty_cleanup(s);
+#ifdef SSH_AUDIT_EVENTS
+	if (s->command)
+		session_end_command(s);
+#endif
 	free(s->term);
 	free(s->display);
 	free(s->x11_chanids);
@@ -2536,6 +2597,15 @@ do_authenticated2(struct ssh *ssh, Authc
 	server_loop2(ssh, authctxt);
 }
 
+static void
+do_cleanup_one_session(Session *s)
+{
+	session_pty_cleanup2(s);
+#ifdef SSH_AUDIT_EVENTS
+	session_end_command2(s);
+#endif
+}
+
 void
 do_cleanup(struct ssh *ssh, Authctxt *authctxt)
 {
@@ -2593,7 +2663,7 @@ do_cleanup(struct ssh *ssh, Authctxt *au
 	 * or if running in monitor.
 	 */
 	if (!use_privsep || mm_is_monitor())
-		session_destroy_all(ssh, session_pty_cleanup2);
+		session_destroy_all(ssh, do_cleanup_one_session);
 }
 
 /* Return a name for the remote host that fits inside utmp_size */
Index: openssh-7.6p1/session.h
===================================================================
--- openssh-7.6p1.orig/session.h	2019-03-12 14:33:58.135378431 +0100
+++ openssh-7.6p1/session.h	2019-03-12 14:38:33.272919131 +0100
@@ -60,6 +60,12 @@ struct Session {
 		char	*name;
 		char	*val;
 	} *env;
+
+	/* exec */
+#ifdef SSH_AUDIT_EVENTS
+	int	command_handle;
+	char	*command;
+#endif
 };
 
 void	 do_authenticated(struct ssh *, Authctxt *);
@@ -72,8 +78,10 @@ void	 session_close_by_pid(struct ssh *s
 void	 session_close_by_channel(struct ssh *, int, void *);
 void	 session_destroy_all(struct ssh *, void (*)(Session *));
 void	 session_pty_cleanup2(Session *);
+void	 session_end_command2(Session *);
 
 Session	*session_new(void);
+Session *session_by_id(int);
 Session	*session_by_tty(char *);
 void	 session_close(struct ssh *, Session *);
 void	 do_setusercontext(struct passwd *);
Index: openssh-7.6p1/sshd.c
===================================================================
--- openssh-7.6p1.orig/sshd.c	2019-03-12 14:38:32.432914408 +0100
+++ openssh-7.6p1/sshd.c	2019-03-12 14:38:33.272919131 +0100
@@ -124,6 +124,7 @@
 #include "ssh-gss.h"
 #endif
 #include "monitor_wrap.h"
+#include "audit.h"
 #include "ssh-sandbox.h"
 #include "version.h"
 #include "ssherr.h"
@@ -256,7 +257,7 @@ Buffer loginmsg;
 struct passwd *privsep_pw = NULL;
 
 /* Prototypes for various functions defined later in this file. */
-void destroy_sensitive_data(void);
+void destroy_sensitive_data(int);
 void demote_sensitive_data(void);
 static void do_ssh2_kex(void);
 
@@ -273,6 +274,15 @@ close_listen_socks(void)
 	num_listen_socks = -1;
 }
 
+/*
+ * Is this process listening for clients (i.e. not specific to any specific
+ * client connection?)
+ */
+int listening_for_clients(void)
+{
+	return num_listen_socks >= 0;
+}
+
 static void
 close_startup_pipes(void)
 {
@@ -486,18 +496,45 @@ sshd_exchange_identification(struct ssh
 	}
 }
 
-/* Destroy the host and server keys.  They will no longer be needed. */
+/*
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
+ */
 void
-destroy_sensitive_data(void)
+destroy_sensitive_data(int privsep)
 {
 	int i;
+#ifdef SSH_AUDIT_EVENTS
+	pid_t pid;
+	uid_t uid;
 
+	pid = getpid();
+	uid = getuid();
+#endif
 	for (i = 0; i < options.num_host_key_files; i++) {
 		if (sensitive_data.host_keys[i]) {
+			char *fp;
+
+			if (key_is_private(sensitive_data.host_keys[i]))
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
+			else
+				fp = NULL;
 			key_free(sensitive_data.host_keys[i]);
 			sensitive_data.host_keys[i] = NULL;
+			if (fp != NULL) {
+#ifdef SSH_AUDIT_EVENTS
+				if (privsep)
+					PRIVSEP(audit_destroy_sensitive_data(fp,
+						pid, uid));
+				else
+					audit_destroy_sensitive_data(fp,
+						pid, uid);
+#endif
+				free(fp);
+			}
 		}
-		if (sensitive_data.host_certificates[i]) {
+		if (sensitive_data.host_certificates
+		    && sensitive_data.host_certificates[i]) {
 			key_free(sensitive_data.host_certificates[i]);
 			sensitive_data.host_certificates[i] = NULL;
 		}
@@ -510,12 +547,30 @@ demote_sensitive_data(void)
 {
 	struct sshkey *tmp;
 	int i;
+#ifdef SSH_AUDIT_EVENTS
+	pid_t pid;
+	uid_t uid;
 
+	pid = getpid();
+	uid = getuid();
+#endif
 	for (i = 0; i < options.num_host_key_files; i++) {
 		if (sensitive_data.host_keys[i]) {
+			char *fp;
+
+			if (key_is_private(sensitive_data.host_keys[i]))
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
+			else
+				fp = NULL;
 			tmp = key_demote(sensitive_data.host_keys[i]);
 			key_free(sensitive_data.host_keys[i]);
 			sensitive_data.host_keys[i] = tmp;
+			if (fp != NULL) {
+#ifdef SSH_AUDIT_EVENTS
+				audit_destroy_sensitive_data(fp, pid, uid);
+#endif
+				free(fp);
+			}
 		}
 		/* Certs do not need demotion */
 	}
@@ -594,7 +649,7 @@ privsep_preauth(Authctxt *authctxt)
 
 	if (use_privsep == PRIVSEP_ON)
 		box = ssh_sandbox_init(pmonitor);
-	pid = fork();
+	pmonitor->m_pid = pid = fork();
 	if (pid == -1) {
 		fatal("fork of unprivileged child failed");
 	} else if (pid != 0) {
@@ -1161,6 +1216,7 @@ server_accept_loop(int *sock_in, int *so
 		if (received_sigterm) {
 			logit("Received signal %d; terminating.",
 			    (int) received_sigterm);
+			destroy_sensitive_data(0);
 			close_listen_socks();
 			if (options.pid_file != NULL)
 				unlink(options.pid_file);
@@ -2239,6 +2295,9 @@ main(int ac, char **av)
 	do_authenticated(ssh, authctxt);
 
 	/* The connection has been terminated. */
+	packet_destroy_all(1, 1);
+	destroy_sensitive_data(1);
+
 	packet_get_bytes(&ibytes, &obytes);
 	verbose("Transferred: sent %llu, received %llu bytes",
 	    (unsigned long long)obytes, (unsigned long long)ibytes);
@@ -2414,6 +2473,15 @@ void
 cleanup_exit(int i)
 {
 	struct ssh *ssh = active_state; /* XXX */
+	static int in_cleanup = 0;
+	int is_privsep_child;
+
+	/* cleanup_exit can be called at the very least from the privsep
+	   wrappers used for auditing.  Make sure we don't recurse
+	   indefinitely. */
+	if (in_cleanup)
+		_exit(i);
+	in_cleanup = 1;
 
 	if (the_authctxt) {
 		do_cleanup(ssh, the_authctxt);
@@ -2426,9 +2494,14 @@ cleanup_exit(int i)
 				    pmonitor->m_pid, strerror(errno));
 		}
 	}
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
+	if (sensitive_data.host_keys != NULL)
+		destroy_sensitive_data(is_privsep_child);
+	packet_destroy_all(1, is_privsep_child);
 #ifdef SSH_AUDIT_EVENTS
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
-	if (!use_privsep || mm_is_monitor())
+	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&
+	    (!use_privsep || mm_is_monitor()))
 		audit_event(SSH_CONNECTION_ABANDON);
 #endif
 	_exit(i);
Index: openssh-7.6p1/sshkey.c
===================================================================
--- openssh-7.6p1.orig/sshkey.c	2019-03-12 14:38:32.432914408 +0100
+++ openssh-7.6p1/sshkey.c	2019-03-12 14:38:33.272919131 +0100
@@ -295,6 +295,32 @@ sshkey_type_is_valid_ca(int type)
 }
 
 int
+sshkey_is_private(const struct sshkey *k)
+{
+      switch (k->type) {
+#ifdef WITH_OPENSSL
+      case KEY_RSA_CERT:
+      case KEY_RSA:
+              return k->rsa->d != NULL;
+      case KEY_DSA_CERT:
+      case KEY_DSA:
+              return k->dsa->priv_key != NULL;
+#ifdef OPENSSL_HAS_ECC
+      case KEY_ECDSA_CERT:
+      case KEY_ECDSA:
+              return EC_KEY_get0_private_key(k->ecdsa) != NULL;
+#endif /* OPENSSL_HAS_ECC */
+#endif /* WITH_OPENSSL */
+      case KEY_ED25519_CERT:
+      case KEY_ED25519:
+              return (k->ed25519_pk != NULL);
+      default:
+              /* fatal("key_is_private: bad key type %d", k->type); */
+              return 0;
+      }
+}
+
+int
 sshkey_is_cert(const struct sshkey *k)
 {
 	if (k == NULL)
Index: openssh-7.6p1/sshkey.h
===================================================================
--- openssh-7.6p1.orig/sshkey.h	2019-03-12 14:38:32.432914408 +0100
+++ openssh-7.6p1/sshkey.h	2019-03-12 14:38:33.272919131 +0100
@@ -133,6 +133,7 @@ u_int		 sshkey_size(const struct sshkey
 int		 sshkey_generate(int type, u_int bits, struct sshkey **keyp);
 int		 sshkey_from_private(const struct sshkey *, struct sshkey **);
 int	 sshkey_type_from_name(const char *);
+int	 sshkey_is_private(const struct sshkey *);
 int	 sshkey_is_cert(const struct sshkey *);
 int	 sshkey_type_is_cert(int);
 int	 sshkey_type_plain(int);