3e8b5b
diff -up openssh/audit-bsm.c.audit openssh/audit-bsm.c
3e8b5b
--- openssh/audit-bsm.c.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/audit-bsm.c	2019-04-03 17:02:20.713886041 +0200
3e8b5b
@@ -372,13 +372,26 @@ audit_connection_from(const char *host,
3e8b5b
 #endif
3e8b5b
 }
3e8b5b
 
3e8b5b
+int
3e8b5b
+audit_run_command(struct ssh *ssh, const char *command)
3e8b5b
+{
3e8b5b
+	/* not implemented */
3e8b5b
+	return 0;
3e8b5b
+}
3e8b5b
+
3e8b5b
 void
3e8b5b
-audit_run_command(const char *command)
3e8b5b
+audit_end_command(struct ssh *ssh, int handle, const char *command)
3e8b5b
 {
3e8b5b
 	/* not implemented */
3e8b5b
 }
3e8b5b
 
3e8b5b
 void
3e8b5b
+audit_count_session_open(void)
3e8b5b
+{
3e8b5b
+	/* not necessary */
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
 audit_session_open(struct logininfo *li)
3e8b5b
 {
3e8b5b
 	/* not implemented */
3e8b5b
@@ -390,6 +403,12 @@ audit_session_close(struct logininfo *li
3e8b5b
 	/* not implemented */
3e8b5b
 }
3e8b5b
 
3e8b5b
+int
3e8b5b
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
3e8b5b
+{
3e8b5b
+	/* not implemented */
3e8b5b
+}
3e8b5b
+
3e8b5b
 void
3e8b5b
 audit_event(struct ssh *ssh, ssh_audit_event_t event)
3e8b5b
 {
3e8b5b
@@ -451,4 +470,28 @@ audit_event(struct ssh *ssh, ssh_audit_e
3e8b5b
 		debug("%s: unhandled event %d", __func__, event);
3e8b5b
 	}
3e8b5b
 }
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_unsupported_body(struct ssh *ssh, int what)
3e8b5b
+{
3e8b5b
+	/* not implemented */
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+	/* not implemented */
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_session_key_free_body(struct ssh * ssh, int ctos, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+	/* not implemented */
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+	/* not implemented */
3e8b5b
+}
3e8b5b
 #endif /* BSM */
3e8b5b
diff -up openssh/audit.c.audit openssh/audit.c
3e8b5b
--- openssh/audit.c.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/audit.c	2019-04-03 17:02:20.713886041 +0200
3e8b5b
@@ -34,6 +34,12 @@
3e8b5b
 #include "log.h"
3e8b5b
 #include "hostfile.h"
3e8b5b
 #include "auth.h"
3e8b5b
+#include "ssh-gss.h"
3e8b5b
+#include "monitor_wrap.h"
3e8b5b
+#include "xmalloc.h"
3e8b5b
+#include "misc.h"
3e8b5b
+#include "servconf.h"
3e8b5b
+#include "ssherr.h"
3e8b5b
 
3e8b5b
 /*
3e8b5b
  * Care must be taken when using this since it WILL NOT be initialized when
3e8b5b
@@ -41,6 +47,7 @@
3e8b5b
  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
3e8b5b
  */
3e8b5b
 extern Authctxt *the_authctxt;
3e8b5b
+extern ServerOptions options;
3e8b5b
 
3e8b5b
 /* Maybe add the audit class to struct Authmethod? */
3e8b5b
 ssh_audit_event_t
3e8b5b
@@ -69,13 +76,10 @@ audit_classify_auth(const char *method)
3e8b5b
 const char *
3e8b5b
 audit_username(void)
3e8b5b
 {
3e8b5b
-	static const char unknownuser[] = "(unknown user)";
3e8b5b
-	static const char invaliduser[] = "(invalid user)";
3e8b5b
+	static const char unknownuser[] = "(unknown)";
3e8b5b
 
3e8b5b
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
3e8b5b
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
3e8b5b
 		return (unknownuser);
3e8b5b
-	if (!the_authctxt->valid)
3e8b5b
-		return (invaliduser);
3e8b5b
 	return (the_authctxt->user);
3e8b5b
 }
3e8b5b
 
3e8b5b
@@ -109,6 +113,35 @@ audit_event_lookup(ssh_audit_event_t ev)
3e8b5b
 	return(event_lookup[i].name);
3e8b5b
 }
3e8b5b
 
3e8b5b
+void
3e8b5b
+audit_key(struct ssh *ssh, int host_user, int *rv, const struct sshkey *key)
3e8b5b
+{
3e8b5b
+	char *fp;
3e8b5b
+
3e8b5b
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
3e8b5b
+	if (audit_keyusage(ssh, host_user, fp, (*rv == 0)) == 0)
3e8b5b
+		*rv = -SSH_ERR_INTERNAL_ERROR;
3e8b5b
+	free(fp);
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_unsupported(struct ssh *ssh, int what)
3e8b5b
+{
3e8b5b
+	PRIVSEP(audit_unsupported_body(ssh, what));
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_kex(struct ssh *ssh, int ctos, char *enc, char *mac, char *comp, char *pfs)
3e8b5b
+{
3e8b5b
+	PRIVSEP(audit_kex_body(ssh, ctos, enc, mac, comp, pfs, getpid(), getuid()));
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_session_key_free(struct ssh *ssh, int ctos)
3e8b5b
+{
3e8b5b
+	PRIVSEP(audit_session_key_free_body(ssh, ctos, getpid(), getuid()));
3e8b5b
+}
3e8b5b
+
3e8b5b
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
3e8b5b
 /*
3e8b5b
  * Null implementations of audit functions.
3e8b5b
@@ -138,6 +171,17 @@ audit_event(struct ssh *ssh, ssh_audit_e
3e8b5b
 }
3e8b5b
 
3e8b5b
 /*
3e8b5b
+ * Called when a child process has called, or will soon call,
3e8b5b
+ * audit_session_open.
3e8b5b
+ */
3e8b5b
+void
3e8b5b
+audit_count_session_open(void)
3e8b5b
+{
3e8b5b
+	debug("audit count session open euid %d user %s", geteuid(),
3e8b5b
+	      audit_username());
3e8b5b
+}
3e8b5b
+
3e8b5b
+/*
3e8b5b
  * Called when a user session is started.  Argument is the tty allocated to
3e8b5b
  * the session, or NULL if no tty was allocated.
3e8b5b
  *
3e8b5b
@@ -172,13 +216,82 @@ audit_session_close(struct logininfo *li
3e8b5b
 /*
3e8b5b
  * This will be called when a user runs a non-interactive command.  Note that
3e8b5b
  * it may be called multiple times for a single connection since SSH2 allows
3e8b5b
- * multiple sessions within a single connection.
3e8b5b
+ * multiple sessions within a single connection.  Returns a "handle" for
3e8b5b
+ * audit_end_command.
3e8b5b
  */
3e8b5b
-void
3e8b5b
-audit_run_command(const char *command)
3e8b5b
+int
3e8b5b
+audit_run_command(struct ssh *ssh, const char *command)
3e8b5b
 {
3e8b5b
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
3e8b5b
 	    audit_username(), command);
3e8b5b
+	return 0;
3e8b5b
+}
3e8b5b
+
3e8b5b
+/*
3e8b5b
+ * This will be called when the non-interactive command finishes.  Note that
3e8b5b
+ * it may be called multiple times for a single connection since SSH2 allows
3e8b5b
+ * multiple sessions within a single connection.  "handle" should come from
3e8b5b
+ * the corresponding audit_run_command.
3e8b5b
+ */
3e8b5b
+void
3e8b5b
+audit_end_command(struct ssh *ssh, int handle, const char *command)
3e8b5b
+{
3e8b5b
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
3e8b5b
+	    audit_username(), command);
3e8b5b
+}
3e8b5b
+
3e8b5b
+/*
3e8b5b
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
3e8b5b
+ *
3e8b5b
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
3e8b5b
+ */
3e8b5b
+int
3e8b5b
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
3e8b5b
+{
3e8b5b
+	debug("audit %s key usage euid %d user %s fingerprint %s, result %d",
3e8b5b
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(),
3e8b5b
+		fp, rv);
3e8b5b
+}
3e8b5b
+
3e8b5b
+/*
3e8b5b
+ * This will be called when the protocol negotiation fails.
3e8b5b
+ */
3e8b5b
+void
3e8b5b
+audit_unsupported_body(struct ssh *ssh, int what)
3e8b5b
+{
3e8b5b
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
3e8b5b
+}
3e8b5b
+
3e8b5b
+/*
3e8b5b
+ * This will be called on succesfull protocol negotiation.
3e8b5b
+ */
3e8b5b
+void
3e8b5b
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
3e8b5b
+	       uid_t uid)
3e8b5b
+{
3e8b5b
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s pfs %s from pid %ld uid %u",
3e8b5b
+		(unsigned)geteuid(), ctos, enc, mac, compress, pfs, (long)pid,
3e8b5b
+	        (unsigned)uid);
3e8b5b
+}
3e8b5b
+
3e8b5b
+/*
3e8b5b
+ * This will be called on succesfull session key discard
3e8b5b
+ */
3e8b5b
+void
3e8b5b
+audit_session_key_free_body(struct ssh *, int ctos, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
3e8b5b
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
3e8b5b
+}
3e8b5b
+
3e8b5b
+/*
3e8b5b
+ * This will be called on destroy private part of the server key
3e8b5b
+ */
3e8b5b
+void
3e8b5b
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
3e8b5b
+		geteuid(), fp, (long)pid, (unsigned)uid);
3e8b5b
 }
3e8b5b
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
3e8b5b
 #endif /* SSH_AUDIT_EVENTS */
3e8b5b
diff -up openssh/audit.h.audit openssh/audit.h
3e8b5b
--- openssh/audit.h.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/audit.h	2019-04-03 17:02:20.713886041 +0200
3e8b5b
@@ -26,6 +26,7 @@
3e8b5b
 # define _SSH_AUDIT_H
3e8b5b
 
3e8b5b
 #include "loginrec.h"
3e8b5b
+#include "sshkey.h"
3e8b5b
 
3e8b5b
 struct ssh;
3e8b5b
 
3e8b5b
@@ -45,13 +46,32 @@ enum ssh_audit_event_type {
3e8b5b
 	SSH_CONNECTION_ABANDON,	/* closed without completing auth */
3e8b5b
 	SSH_AUDIT_UNKNOWN
3e8b5b
 };
3e8b5b
+
3e8b5b
+enum ssh_audit_kex {
3e8b5b
+	SSH_AUDIT_UNSUPPORTED_CIPHER,
3e8b5b
+	SSH_AUDIT_UNSUPPORTED_MAC,
3e8b5b
+	SSH_AUDIT_UNSUPPORTED_COMPRESSION
3e8b5b
+};
3e8b5b
 typedef enum ssh_audit_event_type ssh_audit_event_t;
3e8b5b
 
3e8b5b
+int	listening_for_clients(void);
3e8b5b
+
3e8b5b
 void	audit_connection_from(const char *, int);
3e8b5b
 void	audit_event(struct ssh *, ssh_audit_event_t);
3e8b5b
+void	audit_count_session_open(void);
3e8b5b
 void	audit_session_open(struct logininfo *);
3e8b5b
 void	audit_session_close(struct logininfo *);
3e8b5b
-void	audit_run_command(const char *);
3e8b5b
+int	audit_run_command(struct ssh *, const char *);
3e8b5b
+void 	audit_end_command(struct ssh *, int, const char *);
3e8b5b
 ssh_audit_event_t audit_classify_auth(const char *);
3e8b5b
+int	audit_keyusage(struct ssh *, int, char *, int);
3e8b5b
+void	audit_key(struct ssh *, int, int *, const struct sshkey *);
3e8b5b
+void	audit_unsupported(struct ssh *, int);
3e8b5b
+void	audit_kex(struct ssh *, int, char *, char *, char *, char *);
3e8b5b
+void	audit_unsupported_body(struct ssh *, int);
3e8b5b
+void	audit_kex_body(struct ssh *, int, char *, char *, char *, char *, pid_t, uid_t);
3e8b5b
+void	audit_session_key_free(struct ssh *, int ctos);
3e8b5b
+void	audit_session_key_free_body(struct ssh *, int ctos, pid_t, uid_t);
3e8b5b
+void	audit_destroy_sensitive_data(struct ssh *, const char *, pid_t, uid_t);
3e8b5b
 
3e8b5b
 #endif /* _SSH_AUDIT_H */
3e8b5b
diff -up openssh/audit-linux.c.audit openssh/audit-linux.c
3e8b5b
--- openssh/audit-linux.c.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/audit-linux.c	2019-04-03 17:02:20.713886041 +0200
3e8b5b
@@ -33,27 +33,40 @@
3e8b5b
 
3e8b5b
 #include "log.h"
3e8b5b
 #include "audit.h"
3e8b5b
+#include "sshkey.h"
3e8b5b
+#include "hostfile.h"
3e8b5b
+#include "auth.h"
3e8b5b
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
3e8b5b
+#include "servconf.h"
3e8b5b
 #include "canohost.h"
3e8b5b
 #include "packet.h"
3e8b5b
-
3e8b5b
+#include "cipher.h"
3e8b5b
+#include "channels.h"
3e8b5b
+#include "session.h"
3e8b5b
+
3e8b5b
+#define AUDIT_LOG_SIZE 256
3e8b5b
+
3e8b5b
+extern ServerOptions options;
3e8b5b
+extern Authctxt *the_authctxt;
3e8b5b
+extern u_int utmp_len;
3e8b5b
 const char *audit_username(void);
3e8b5b
 
3e8b5b
-int
3e8b5b
-linux_audit_record_event(int uid, const char *username, const char *hostname,
3e8b5b
-    const char *ip, const char *ttyn, int success)
3e8b5b
+static void
3e8b5b
+linux_audit_user_logxxx(int uid, const char *username,
3e8b5b
+    const char *ip, const char *ttyn, int success, int event)
3e8b5b
 {
3e8b5b
 	int audit_fd, rc, saved_errno;
3e8b5b
 
3e8b5b
 	if ((audit_fd = audit_open()) < 0) {
3e8b5b
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
3e8b5b
 		    errno == EAFNOSUPPORT)
3e8b5b
-			return 1; /* No audit support in kernel */
3e8b5b
+			return; /* No audit support in kernel */
3e8b5b
 		else
3e8b5b
-			return 0; /* Must prevent login */
3e8b5b
+			goto fatal_report; /* Must prevent login */
3e8b5b
 	}
3e8b5b
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
3e8b5b
+	rc = audit_log_acct_message(audit_fd, event,
3e8b5b
 	    NULL, "login", username ? username : "(unknown)",
3e8b5b
-	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
3e8b5b
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
3e8b5b
 	saved_errno = errno;
3e8b5b
 	close(audit_fd);
3e8b5b
 
3e8b5b
@@ -65,9 +78,96 @@ linux_audit_record_event(int uid, const
3e8b5b
 		rc = 0;
3e8b5b
 	errno = saved_errno;
3e8b5b
 
3e8b5b
-	return rc >= 0;
3e8b5b
+	if (rc < 0) {
3e8b5b
+fatal_report:
3e8b5b
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
3e8b5b
+	}
3e8b5b
+}
3e8b5b
+
3e8b5b
+static void
3e8b5b
+linux_audit_user_auth(int uid, const char *username,
3e8b5b
+    const char *ip, const char *ttyn, int success, int event)
3e8b5b
+{
3e8b5b
+	int audit_fd, rc, saved_errno;
3e8b5b
+	static const char *event_name[] = {
3e8b5b
+		"maxtries exceeded",
3e8b5b
+		"root denied",
3e8b5b
+		"success",
3e8b5b
+		"none",
3e8b5b
+		"password",
3e8b5b
+		"challenge-response",
3e8b5b
+		"pubkey",
3e8b5b
+		"hostbased",
3e8b5b
+		"gssapi",
3e8b5b
+		"invalid user",
3e8b5b
+		"nologin",
3e8b5b
+		"connection closed",
3e8b5b
+		"connection abandoned",
3e8b5b
+		"unknown"
3e8b5b
+	};
3e8b5b
+
3e8b5b
+	audit_fd = audit_open();
3e8b5b
+	if (audit_fd < 0) {
3e8b5b
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
3e8b5b
+		    errno == EAFNOSUPPORT)
3e8b5b
+			return; /* No audit support in kernel */
3e8b5b
+		else
3e8b5b
+			goto fatal_report; /* Must prevent login */
3e8b5b
+	}
3e8b5b
+
3e8b5b
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
3e8b5b
+		event = SSH_AUDIT_UNKNOWN;
3e8b5b
+
3e8b5b
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
3e8b5b
+	    NULL, event_name[event], username ? username : "(unknown)",
3e8b5b
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
3e8b5b
+	saved_errno = errno;
3e8b5b
+	close(audit_fd);
3e8b5b
+	/*
3e8b5b
+	 * Do not report error if the error is EPERM and sshd is run as non
3e8b5b
+	 * root user.
3e8b5b
+	 */
3e8b5b
+	if ((rc == -EPERM) && (geteuid() != 0))
3e8b5b
+		rc = 0;
3e8b5b
+	errno = saved_errno;
3e8b5b
+	if (rc < 0) {
3e8b5b
+fatal_report:
3e8b5b
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
3e8b5b
+	}
3e8b5b
+}
3e8b5b
+
3e8b5b
+int
3e8b5b
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
3e8b5b
+{
3e8b5b
+	char buf[AUDIT_LOG_SIZE];
3e8b5b
+	int audit_fd, rc, saved_errno;
3e8b5b
+
3e8b5b
+	audit_fd = audit_open();
3e8b5b
+	if (audit_fd < 0) {
3e8b5b
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
3e8b5b
+					 errno == EAFNOSUPPORT)
3e8b5b
+			return 1; /* No audit support in kernel */
3e8b5b
+		else
3e8b5b
+			return 0; /* Must prevent login */
3e8b5b
+	}
3e8b5b
+	snprintf(buf, sizeof(buf), "%s_auth grantors=auth-key", host_user ? "pubkey" : "hostbased");
3e8b5b
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
3e8b5b
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(ssh), NULL, rv);
3e8b5b
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
3e8b5b
+		goto out;
3e8b5b
+	snprintf(buf, sizeof(buf), "op=negotiate kind=auth-key fp=%s", fp);
3e8b5b
+	rc = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER, buf, NULL,
3e8b5b
+		ssh_remote_ipaddr(ssh), NULL, rv);
3e8b5b
+out:
3e8b5b
+	saved_errno = errno;
3e8b5b
+	audit_close(audit_fd);
3e8b5b
+	errno = saved_errno;
3e8b5b
+	/* do not report error if the error is EPERM and sshd is run as non root user */
3e8b5b
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
3e8b5b
 }
3e8b5b
 
3e8b5b
+static int user_login_count = 0;
3e8b5b
+
3e8b5b
 /* Below is the sshd audit API code */
3e8b5b
 
3e8b5b
 void
3e8b5b
@@ -76,49 +176,210 @@ audit_connection_from(const char *host,
3e8b5b
 	/* not implemented */
3e8b5b
 }
3e8b5b
 
3e8b5b
+int
3e8b5b
+audit_run_command(struct ssh *ssh, const char *command)
3e8b5b
+{
3e8b5b
+	if (!user_login_count++)
3e8b5b
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
3e8b5b
+		    ssh_remote_ipaddr(ssh),
3e8b5b
+		    "ssh", 1, AUDIT_USER_LOGIN);
3e8b5b
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
3e8b5b
+	    ssh_remote_ipaddr(ssh),
3e8b5b
+	    "ssh", 1, AUDIT_USER_START);
3e8b5b
+	return 0;
3e8b5b
+}
3e8b5b
+
3e8b5b
 void
3e8b5b
-audit_run_command(const char *command)
3e8b5b
+audit_end_command(struct ssh *ssh, int handle, const char *command)
3e8b5b
 {
3e8b5b
-	/* not implemented */
3e8b5b
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
3e8b5b
+	    ssh_remote_ipaddr(ssh),
3e8b5b
+	    "ssh", 1, AUDIT_USER_END);
3e8b5b
+	if (user_login_count && !--user_login_count)
3e8b5b
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
3e8b5b
+		    ssh_remote_ipaddr(ssh),
3e8b5b
+		    "ssh", 1, AUDIT_USER_LOGOUT);
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_count_session_open(void)
3e8b5b
+{
3e8b5b
+	user_login_count++;
3e8b5b
 }
3e8b5b
 
3e8b5b
 void
3e8b5b
 audit_session_open(struct logininfo *li)
3e8b5b
 {
3e8b5b
-	if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,
3e8b5b
-	    li->line, 1) == 0)
3e8b5b
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
3e8b5b
+	if (!user_login_count++)
3e8b5b
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
3e8b5b
+		    li->line, 1, AUDIT_USER_LOGIN);
3e8b5b
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
3e8b5b
+	    li->line, 1, AUDIT_USER_START);
3e8b5b
 }
3e8b5b
 
3e8b5b
 void
3e8b5b
 audit_session_close(struct logininfo *li)
3e8b5b
 {
3e8b5b
-	/* not implemented */
3e8b5b
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
3e8b5b
+	    li->line, 1, AUDIT_USER_END);
3e8b5b
+	if (user_login_count && !--user_login_count)
3e8b5b
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
3e8b5b
+		    li->line, 1, AUDIT_USER_LOGOUT);
3e8b5b
 }
3e8b5b
 
3e8b5b
 void
3e8b5b
 audit_event(struct ssh *ssh, ssh_audit_event_t event)
3e8b5b
 {
3e8b5b
 	switch(event) {
3e8b5b
-	case SSH_AUTH_SUCCESS:
3e8b5b
-	case SSH_CONNECTION_CLOSE:
3e8b5b
 	case SSH_NOLOGIN:
3e8b5b
-	case SSH_LOGIN_EXCEED_MAXTRIES:
3e8b5b
 	case SSH_LOGIN_ROOT_DENIED:
3e8b5b
+		linux_audit_user_auth(-1, audit_username(),
3e8b5b
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
3e8b5b
+		linux_audit_user_logxxx(-1, audit_username(),
3e8b5b
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
3e8b5b
 		break;
3e8b5b
-	case SSH_AUTH_FAIL_NONE:
3e8b5b
 	case SSH_AUTH_FAIL_PASSWD:
3e8b5b
+		if (options.use_pam)
3e8b5b
+			break;
3e8b5b
+	case SSH_LOGIN_EXCEED_MAXTRIES:
3e8b5b
 	case SSH_AUTH_FAIL_KBDINT:
3e8b5b
 	case SSH_AUTH_FAIL_PUBKEY:
3e8b5b
 	case SSH_AUTH_FAIL_HOSTBASED:
3e8b5b
 	case SSH_AUTH_FAIL_GSSAPI:
3e8b5b
+		linux_audit_user_auth(-1, audit_username(),
3e8b5b
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
3e8b5b
+		break;
3e8b5b
+
3e8b5b
+	case SSH_CONNECTION_CLOSE:
3e8b5b
+		if (user_login_count) {
3e8b5b
+			while (user_login_count--)
3e8b5b
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
3e8b5b
+				    ssh_remote_ipaddr(ssh),
3e8b5b
+				    "ssh", 1, AUDIT_USER_END);
3e8b5b
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
3e8b5b
+			    ssh_remote_ipaddr(ssh),
3e8b5b
+			    "ssh", 1, AUDIT_USER_LOGOUT);
3e8b5b
+		}
3e8b5b
+		break;
3e8b5b
+
3e8b5b
+	case SSH_CONNECTION_ABANDON:
3e8b5b
 	case SSH_INVALID_USER:
3e8b5b
-		linux_audit_record_event(-1, audit_username(), NULL,
3e8b5b
-		    ssh_remote_ipaddr(ssh), "sshd", 0);
3e8b5b
+		linux_audit_user_logxxx(-1, audit_username(),
3e8b5b
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
3e8b5b
 		break;
3e8b5b
 	default:
3e8b5b
 		debug("%s: unhandled event %d", __func__, event);
3e8b5b
 		break;
3e8b5b
 	}
3e8b5b
 }
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_unsupported_body(struct ssh *ssh, int what)
3e8b5b
+{
3e8b5b
+#ifdef AUDIT_CRYPTO_SESSION
3e8b5b
+	char buf[AUDIT_LOG_SIZE];
3e8b5b
+	const static char *name[] = { "cipher", "mac", "comp" };
3e8b5b
+	char *s;
3e8b5b
+	int audit_fd;
3e8b5b
+
3e8b5b
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
3e8b5b
+		name[what], ssh_remote_port(ssh), (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))),
3e8b5b
+		ssh_local_port(ssh));
3e8b5b
+	free(s);
3e8b5b
+	audit_fd = audit_open();
3e8b5b
+	if (audit_fd < 0)
3e8b5b
+		/* no problem, the next instruction will be fatal() */
3e8b5b
+		return;
3e8b5b
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
3e8b5b
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 0);
3e8b5b
+	audit_close(audit_fd);
3e8b5b
+#endif
3e8b5b
+}
3e8b5b
+
3e8b5b
+const static char *direction[] = { "from-server", "from-client", "both" };
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress,
3e8b5b
+    char *pfs, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+#ifdef AUDIT_CRYPTO_SESSION
3e8b5b
+	char buf[AUDIT_LOG_SIZE];
3e8b5b
+	int audit_fd, audit_ok;
3e8b5b
+	const struct sshcipher *cipher = cipher_by_name(enc);
3e8b5b
+	char *s;
3e8b5b
+
3e8b5b
+	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 ",
3e8b5b
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs,
3e8b5b
+		(intmax_t)pid, (intmax_t)uid,
3e8b5b
+		ssh_remote_port(ssh), (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))), ssh_local_port(ssh));
3e8b5b
+	free(s);
3e8b5b
+	audit_fd = audit_open();
3e8b5b
+	if (audit_fd < 0) {
3e8b5b
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
3e8b5b
+					 errno == EAFNOSUPPORT)
3e8b5b
+			return; /* No audit support in kernel */
3e8b5b
+		else
3e8b5b
+			fatal("cannot open audit"); /* Must prevent login */
3e8b5b
+	}
3e8b5b
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
3e8b5b
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 1);
3e8b5b
+	audit_close(audit_fd);
3e8b5b
+	/* do not abort if the error is EPERM and sshd is run as non root user */
3e8b5b
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
3e8b5b
+		fatal("cannot write into audit"); /* Must prevent login */
3e8b5b
+#endif
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+	char buf[AUDIT_LOG_SIZE];
3e8b5b
+	int audit_fd, audit_ok;
3e8b5b
+	char *s;
3e8b5b
+
3e8b5b
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
3e8b5b
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
3e8b5b
+		 ssh_remote_port(ssh),
3e8b5b
+		 (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))),
3e8b5b
+		 ssh_local_port(ssh));
3e8b5b
+	free(s);
3e8b5b
+	audit_fd = audit_open();
3e8b5b
+	if (audit_fd < 0) {
3e8b5b
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
3e8b5b
+					 errno != EAFNOSUPPORT)
3e8b5b
+			error("cannot open audit");
3e8b5b
+		return;
3e8b5b
+	}
3e8b5b
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
3e8b5b
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 1);
3e8b5b
+	audit_close(audit_fd);
3e8b5b
+	/* do not abort if the error is EPERM and sshd is run as non root user */
3e8b5b
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
3e8b5b
+		error("cannot write into audit");
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+	char buf[AUDIT_LOG_SIZE];
3e8b5b
+	int audit_fd, audit_ok;
3e8b5b
+
3e8b5b
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
3e8b5b
+		fp, (intmax_t)pid, (intmax_t)uid);
3e8b5b
+	audit_fd = audit_open();
3e8b5b
+	if (audit_fd < 0) {
3e8b5b
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
3e8b5b
+					 errno != EAFNOSUPPORT)
3e8b5b
+			error("cannot open audit");
3e8b5b
+		return;
3e8b5b
+	}
3e8b5b
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
3e8b5b
+			buf, NULL,
3e8b5b
+			listening_for_clients() ? NULL : ssh_remote_ipaddr(ssh),
3e8b5b
+			NULL, 1);
3e8b5b
+	audit_close(audit_fd);
3e8b5b
+	/* do not abort if the error is EPERM and sshd is run as non root user */
3e8b5b
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
3e8b5b
+		error("cannot write into audit");
3e8b5b
+}
3e8b5b
 #endif /* USE_LINUX_AUDIT */
3e8b5b
diff -up openssh/auditstub.c.audit openssh/auditstub.c
3e8b5b
--- openssh/auditstub.c.audit	2019-04-03 17:02:20.714886050 +0200
3e8b5b
+++ openssh/auditstub.c	2019-04-03 17:02:20.714886050 +0200
3e8b5b
@@ -0,0 +1,52 @@
3e8b5b
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
3e8b5b
+
3e8b5b
+/*
3e8b5b
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
3e8b5b
+ * Use is subject to license terms.
3e8b5b
+ *
3e8b5b
+ * Redistribution and use in source and binary forms, with or without
3e8b5b
+ * modification, are permitted provided that the following conditions
3e8b5b
+ * are met:
3e8b5b
+ * 1. Redistributions of source code must retain the above copyright
3e8b5b
+ *    notice, this list of conditions and the following disclaimer.
3e8b5b
+ * 2. Redistributions in binary form must reproduce the above copyright
3e8b5b
+ *    notice, this list of conditions and the following disclaimer in the
3e8b5b
+ *    documentation and/or other materials provided with the distribution.
3e8b5b
+ *
3e8b5b
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
3e8b5b
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3e8b5b
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
3e8b5b
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3e8b5b
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3e8b5b
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3e8b5b
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3e8b5b
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3e8b5b
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3e8b5b
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3e8b5b
+ *
3e8b5b
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
3e8b5b
+ */
3e8b5b
+
3e8b5b
+#include <sys/types.h>
3e8b5b
+
3e8b5b
+struct ssh;
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_unsupported(struct ssh *ssh, int n)
3e8b5b
+{
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_kex(struct ssh *ssh, int ctos, char *enc, char *mac, char *comp, char *pfs)
3e8b5b
+{
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_session_key_free(struct ssh *ssh, int ctos)
3e8b5b
+{
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+}
3e8b5b
diff -up openssh/auth2.c.audit openssh/auth2.c
3e8b5b
--- openssh/auth2.c.audit	2019-04-03 17:02:20.651885453 +0200
3e8b5b
+++ openssh/auth2.c	2019-04-03 17:02:20.714886050 +0200
3e8b5b
@@ -303,9 +303,6 @@ input_userauth_request(int type, u_int32
3e8b5b
 		} else {
3e8b5b
 			/* Invalid user, fake password information */
3e8b5b
 			authctxt->pw = fakepw();
3e8b5b
-#ifdef SSH_AUDIT_EVENTS
3e8b5b
-			PRIVSEP(audit_event(ssh, SSH_INVALID_USER));
3e8b5b
-#endif
3e8b5b
 		}
3e8b5b
 #ifdef USE_PAM
3e8b5b
 		if (options.use_pam)
3e8b5b
diff -up openssh/auth2-hostbased.c.audit openssh/auth2-hostbased.c
3e8b5b
--- openssh/auth2-hostbased.c.audit	2019-04-03 17:02:20.612885083 +0200
3e8b5b
+++ openssh/auth2-hostbased.c	2019-04-03 17:02:20.714886050 +0200
3e8b5b
@@ -158,7 +158,7 @@ userauth_hostbased(struct ssh *ssh)
3e8b5b
 	authenticated = 0;
3e8b5b
 	if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
3e8b5b
 	    chost, key)) &&
3e8b5b
-	    PRIVSEP(sshkey_verify(key, sig, slen,
3e8b5b
+	    PRIVSEP(hostbased_key_verify(ssh, key, sig, slen,
3e8b5b
 	    sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat)) == 0)
3e8b5b
 		authenticated = 1;
3e8b5b
 
3e8b5b
@@ -175,6 +175,19 @@ done:
3e8b5b
 	return authenticated;
3e8b5b
 }
3e8b5b
 
3e8b5b
+int
3e8b5b
+hostbased_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig,
3e8b5b
+    size_t slen, const u_char *data, size_t datalen, const char *pkalg, u_int compat)
3e8b5b
+{
3e8b5b
+	int rv;
3e8b5b
+
3e8b5b
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat);
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	audit_key(ssh, 0, &rv, key);
3e8b5b
+#endif
3e8b5b
+	return rv;
3e8b5b
+}
3e8b5b
+
3e8b5b
 /* return 1 if given hostkey is allowed */
3e8b5b
 int
3e8b5b
 hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
3e8b5b
diff -up openssh/auth2-pubkey.c.audit openssh/auth2-pubkey.c
3e8b5b
--- openssh/auth2-pubkey.c.audit	2019-04-03 17:02:20.691885832 +0200
3e8b5b
+++ openssh/auth2-pubkey.c	2019-04-03 17:02:20.714886050 +0200
3e8b5b
@@ -219,7 +219,7 @@ userauth_pubkey(struct ssh *ssh)
3e8b5b
 		/* test for correct signature */
3e8b5b
 		authenticated = 0;
3e8b5b
 		if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
3e8b5b
-		    PRIVSEP(sshkey_verify(key, sig, slen,
3e8b5b
+		    PRIVSEP(user_key_verify(ssh, key, sig, slen,
3e8b5b
 		    sshbuf_ptr(b), sshbuf_len(b),
3e8b5b
 		    (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
3e8b5b
 		    ssh->compat)) == 0) {
3e8b5b
@@ -278,6 +278,19 @@ done:
3e8b5b
 	return authenticated;
3e8b5b
 }
3e8b5b
 
3e8b5b
+int
3e8b5b
+user_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig,
3e8b5b
+    size_t slen, const u_char *data, size_t datalen, const char *pkalg, u_int compat)
3e8b5b
+{
3e8b5b
+	int rv;
3e8b5b
+
3e8b5b
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat);
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	audit_key(ssh, 1, &rv, key);
3e8b5b
+#endif
3e8b5b
+	return rv;
3e8b5b
+}
3e8b5b
+
3e8b5b
 static int
3e8b5b
 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
3e8b5b
 {
3e8b5b
diff -up openssh/auth.c.audit openssh/auth.c
3e8b5b
--- openssh/auth.c.audit	2019-04-03 17:02:20.691885832 +0200
3e8b5b
+++ openssh/auth.c	2019-04-03 17:02:20.714886050 +0200
3e8b5b
@@ -366,7 +366,7 @@ auth_log(struct ssh *ssh, int authentica
3e8b5b
 # endif
3e8b5b
 #endif
3e8b5b
 #ifdef SSH_AUDIT_EVENTS
3e8b5b
-	if (authenticated == 0 && !authctxt->postponed)
3e8b5b
+	if (authenticated == 0 && !authctxt->postponed && !partial)
3e8b5b
 		audit_event(ssh, audit_classify_auth(method));
3e8b5b
 #endif
3e8b5b
 }
3e8b5b
@@ -592,9 +592,6 @@ getpwnamallow(struct ssh *ssh, const cha
3e8b5b
 		record_failed_login(ssh, user,
3e8b5b
 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
3e8b5b
 #endif
3e8b5b
-#ifdef SSH_AUDIT_EVENTS
3e8b5b
-		audit_event(ssh, SSH_INVALID_USER);
3e8b5b
-#endif /* SSH_AUDIT_EVENTS */
3e8b5b
 		return (NULL);
3e8b5b
 	}
3e8b5b
 	if (!allowed_user(ssh, pw))
3e8b5b
diff -up openssh/auth.h.audit openssh/auth.h
3e8b5b
--- openssh/auth.h.audit	2019-04-03 17:02:20.692885842 +0200
3e8b5b
+++ openssh/auth.h	2019-04-03 17:02:20.714886050 +0200
3e8b5b
@@ -195,6 +195,8 @@ struct passwd * getpwnamallow(struct ssh
3e8b5b
 
3e8b5b
 char	*expand_authorized_keys(const char *, struct passwd *pw);
3e8b5b
 char	*authorized_principals_file(struct passwd *);
3e8b5b
+int	 user_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
3e8b5b
+    const u_char *, size_t, const char *, u_int);
3e8b5b
 
3e8b5b
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
3e8b5b
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
3e8b5b
@@ -214,6 +216,8 @@ struct sshkey	*get_hostkey_private_by_ty
3e8b5b
 int	 get_hostkey_index(struct sshkey *, int, struct ssh *);
3e8b5b
 int	 sshd_hostkey_sign(struct ssh *, struct sshkey *, struct sshkey *,
3e8b5b
     u_char **, size_t *, const u_char *, size_t, const char *);
3e8b5b
+int	 hostbased_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
3e8b5b
+    const u_char *, size_t, const char *, u_int);
3e8b5b
 
3e8b5b
 /* Key / cert options linkage to auth layer */
3e8b5b
 const struct sshauthopt *auth_options(struct ssh *);
3e8b5b
diff -up openssh/cipher.c.audit openssh/cipher.c
3e8b5b
--- openssh/cipher.c.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/cipher.c	2019-04-03 17:02:20.714886050 +0200
3e8b5b
@@ -61,25 +61,6 @@ struct sshcipher_ctx {
3e8b5b
 	const struct sshcipher *cipher;
3e8b5b
 };
3e8b5b
 
3e8b5b
-struct sshcipher {
3e8b5b
-	char	*name;
3e8b5b
-	u_int	block_size;
3e8b5b
-	u_int	key_len;
3e8b5b
-	u_int	iv_len;		/* defaults to block_size */
3e8b5b
-	u_int	auth_len;
3e8b5b
-	u_int	flags;
3e8b5b
-#define CFLAG_CBC		(1<<0)
3e8b5b
-#define CFLAG_CHACHAPOLY	(1<<1)
3e8b5b
-#define CFLAG_AESCTR		(1<<2)
3e8b5b
-#define CFLAG_NONE		(1<<3)
3e8b5b
-#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
3e8b5b
-#ifdef WITH_OPENSSL
3e8b5b
-	const EVP_CIPHER	*(*evptype)(void);
3e8b5b
-#else
3e8b5b
-	void	*ignored;
3e8b5b
-#endif
3e8b5b
-};
3e8b5b
-
3e8b5b
 static const struct sshcipher ciphers[] = {
3e8b5b
 #ifdef WITH_OPENSSL
3e8b5b
 #ifndef OPENSSL_NO_DES
3e8b5b
@@ -410,7 +391,7 @@ cipher_get_length(struct sshcipher_ctx *
3e8b5b
 void
3e8b5b
 cipher_free(struct sshcipher_ctx *cc)
3e8b5b
 {
3e8b5b
-	if (cc == NULL)
3e8b5b
+	if (cc == NULL || cc->cipher == NULL)
3e8b5b
 		return;
3e8b5b
 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
3e8b5b
 		explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
3e8b5b
diff -up openssh/cipher.h.audit openssh/cipher.h
3e8b5b
--- openssh/cipher.h.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/cipher.h	2019-04-03 17:02:20.714886050 +0200
3e8b5b
@@ -45,7 +45,25 @@
3e8b5b
 #define CIPHER_ENCRYPT		1
3e8b5b
 #define CIPHER_DECRYPT		0
3e8b5b
 
3e8b5b
-struct sshcipher;
3e8b5b
+struct sshcipher {
3e8b5b
+	char	*name;
3e8b5b
+	u_int	block_size;
3e8b5b
+	u_int	key_len;
3e8b5b
+	u_int	iv_len;		/* defaults to block_size */
3e8b5b
+	u_int	auth_len;
3e8b5b
+	u_int	flags;
3e8b5b
+#define CFLAG_CBC		(1<<0)
3e8b5b
+#define CFLAG_CHACHAPOLY	(1<<1)
3e8b5b
+#define CFLAG_AESCTR		(1<<2)
3e8b5b
+#define CFLAG_NONE		(1<<3)
3e8b5b
+#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
3e8b5b
+#ifdef WITH_OPENSSL
3e8b5b
+	const EVP_CIPHER	*(*evptype)(void);
3e8b5b
+#else
3e8b5b
+	void	*ignored;
3e8b5b
+#endif
3e8b5b
+};
3e8b5b
+
3e8b5b
 struct sshcipher_ctx;
3e8b5b
 
3e8b5b
 const struct sshcipher *cipher_by_name(const char *);
3e8b5b
diff -up openssh/kex.c.audit openssh/kex.c
3e8b5b
--- openssh/kex.c.audit	2019-04-03 17:02:20.652885462 +0200
3e8b5b
+++ openssh/kex.c	2019-04-03 17:02:20.715886060 +0200
3e8b5b
@@ -60,6 +60,7 @@
3e8b5b
 #include "ssherr.h"
3e8b5b
 #include "sshbuf.h"
3e8b5b
 #include "digest.h"
3e8b5b
+#include "audit.h"
3e8b5b
 
3e8b5b
 #ifdef GSSAPI
3e8b5b
 #include "ssh-gss.h"
3e8b5b
@@ -758,12 +759,16 @@ kex_start_rekex(struct ssh *ssh)
3e8b5b
 }
3e8b5b
 
3e8b5b
 static int
3e8b5b
-choose_enc(struct sshenc *enc, char *client, char *server)
3e8b5b
+choose_enc(struct ssh *ssh, struct sshenc *enc, char *client, char *server)
3e8b5b
 {
3e8b5b
 	char *name = match_list(client, server, NULL);
3e8b5b
 
3e8b5b
-	if (name == NULL)
3e8b5b
+	if (name == NULL) {
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_CIPHER);
3e8b5b
+#endif
3e8b5b
 		return SSH_ERR_NO_CIPHER_ALG_MATCH;
3e8b5b
+	}
3e8b5b
 	if ((enc->cipher = cipher_by_name(name)) == NULL) {
3e8b5b
 		free(name);
3e8b5b
 		return SSH_ERR_INTERNAL_ERROR;
3e8b5b
@@ -783,8 +788,12 @@ choose_mac(struct ssh *ssh, struct sshma
3e8b5b
 {
3e8b5b
 	char *name = match_list(client, server, NULL);
3e8b5b
 
3e8b5b
-	if (name == NULL)
3e8b5b
+	if (name == NULL) {
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_MAC);
3e8b5b
+#endif
3e8b5b
 		return SSH_ERR_NO_MAC_ALG_MATCH;
3e8b5b
+	}
3e8b5b
 	if (mac_setup(mac, name) < 0) {
3e8b5b
 		free(name);
3e8b5b
 		return SSH_ERR_INTERNAL_ERROR;
3e8b5b
@@ -796,12 +805,16 @@ choose_mac(struct ssh *ssh, struct sshma
3e8b5b
 }
3e8b5b
 
3e8b5b
 static int
3e8b5b
-choose_comp(struct sshcomp *comp, char *client, char *server)
3e8b5b
+choose_comp(struct ssh *ssh, struct sshcomp *comp, char *client, char *server)
3e8b5b
 {
3e8b5b
 	char *name = match_list(client, server, NULL);
3e8b5b
 
3e8b5b
-	if (name == NULL)
3e8b5b
+	if (name == NULL) {
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_COMPRESSION);
3e8b5b
+#endif
3e8b5b
 		return SSH_ERR_NO_COMPRESS_ALG_MATCH;
3e8b5b
+	}
3e8b5b
 	if (strcmp(name, "zlib@openssh.com") == 0) {
3e8b5b
 		comp->type = COMP_DELAYED;
3e8b5b
 	} else if (strcmp(name, "zlib") == 0) {
3e8b5b
@@ -933,7 +946,7 @@ kex_choose_conf(struct ssh *ssh)
3e8b5b
 		nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
3e8b5b
 		nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
3e8b5b
 		ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
3e8b5b
-		if ((r = choose_enc(&newkeys->enc, cprop[nenc],
3e8b5b
+		if ((r = choose_enc(ssh, &newkeys->enc, cprop[nenc],
3e8b5b
 		    sprop[nenc])) != 0) {
3e8b5b
 			kex->failed_choice = peer[nenc];
3e8b5b
 			peer[nenc] = NULL;
3e8b5b
@@ -948,7 +961,7 @@ kex_choose_conf(struct ssh *ssh)
3e8b5b
 			peer[nmac] = NULL;
3e8b5b
 			goto out;
3e8b5b
 		}
3e8b5b
-		if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
3e8b5b
+		if ((r = choose_comp(ssh, &newkeys->comp, cprop[ncomp],
3e8b5b
 		    sprop[ncomp])) != 0) {
3e8b5b
 			kex->failed_choice = peer[ncomp];
3e8b5b
 			peer[ncomp] = NULL;
3e8b5b
@@ -971,6 +984,10 @@ kex_choose_conf(struct ssh *ssh)
3e8b5b
 		dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
3e8b5b
 		dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
3e8b5b
 		dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
3e8b5b
+		debug("kex: %s need=%d dh_need=%d", kex->name, need, dh_need);
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+		audit_kex(ssh, mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
3e8b5b
+#endif
3e8b5b
 	}
3e8b5b
 	/* XXX need runden? */
3e8b5b
 	kex->we_need = need;
3e8b5b
@@ -1129,6 +1146,36 @@ dump_digest(const char *msg, const u_cha
3e8b5b
 }
3e8b5b
 #endif
3e8b5b
 
3e8b5b
+static void
3e8b5b
+enc_destroy(struct sshenc *enc)
3e8b5b
+{
3e8b5b
+	if (enc == NULL)
3e8b5b
+		return;
3e8b5b
+
3e8b5b
+	if (enc->key) {
3e8b5b
+		memset(enc->key, 0, enc->key_len);
3e8b5b
+		free(enc->key);
3e8b5b
+	}
3e8b5b
+
3e8b5b
+	if (enc->iv) {
3e8b5b
+		memset(enc->iv,  0, enc->iv_len);
3e8b5b
+		free(enc->iv);
3e8b5b
+	}
3e8b5b
+
3e8b5b
+	memset(enc, 0, sizeof(*enc));
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+newkeys_destroy(struct newkeys *newkeys)
3e8b5b
+{
3e8b5b
+	if (newkeys == NULL)
3e8b5b
+		return;
3e8b5b
+
3e8b5b
+	enc_destroy(&newkeys->enc);
3e8b5b
+	mac_destroy(&newkeys->mac);
3e8b5b
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
3e8b5b
+}
3e8b5b
+
3e8b5b
 /*
3e8b5b
  * Send a plaintext error message to the peer, suffixed by \r\n.
3e8b5b
  * Only used during banner exchange, and there only for the server.
3e8b5b
diff -up openssh/kex.h.audit openssh/kex.h
3e8b5b
--- openssh/kex.h.audit	2019-04-03 17:02:20.652885462 +0200
3e8b5b
+++ openssh/kex.h	2019-04-03 17:02:20.715886060 +0200
3e8b5b
@@ -226,6 +226,8 @@ int	 kexgss_client(struct ssh *);
3e8b5b
 int	 kexgss_server(struct ssh *);
3e8b5b
 #endif
3e8b5b
 
3e8b5b
+void	newkeys_destroy(struct newkeys *newkeys);
3e8b5b
+
3e8b5b
 int	 kex_dh_keypair(struct kex *);
3e8b5b
 int	 kex_dh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
3e8b5b
     struct sshbuf **);
3e8b5b
diff -up openssh/mac.c.audit openssh/mac.c
3e8b5b
--- openssh/mac.c.audit	2019-04-03 17:02:20.652885462 +0200
3e8b5b
+++ openssh/mac.c	2019-04-03 17:02:20.715886060 +0200
3e8b5b
@@ -243,6 +243,20 @@ mac_clear(struct sshmac *mac)
3e8b5b
 	mac->umac_ctx = NULL;
3e8b5b
 }
3e8b5b
 
3e8b5b
+void
3e8b5b
+mac_destroy(struct sshmac *mac)
3e8b5b
+{
3e8b5b
+	if (mac == NULL)
3e8b5b
+		return;
3e8b5b
+
3e8b5b
+	if (mac->key) {
3e8b5b
+		memset(mac->key, 0, mac->key_len);
3e8b5b
+		free(mac->key);
3e8b5b
+	}
3e8b5b
+
3e8b5b
+	memset(mac, 0, sizeof(*mac));
3e8b5b
+}
3e8b5b
+
3e8b5b
 /* XXX copied from ciphers_valid */
3e8b5b
 #define	MAC_SEP	","
3e8b5b
 int
3e8b5b
diff -up openssh/mac.h.audit openssh/mac.h
3e8b5b
--- openssh/mac.h.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/mac.h	2019-04-03 17:02:20.715886060 +0200
3e8b5b
@@ -49,5 +49,6 @@ int	 mac_compute(struct sshmac *, u_int3
3e8b5b
 int	 mac_check(struct sshmac *, u_int32_t, const u_char *, size_t,
3e8b5b
     const u_char *, size_t);
3e8b5b
 void	 mac_clear(struct sshmac *);
3e8b5b
+void	 mac_destroy(struct sshmac *);
3e8b5b
 
3e8b5b
 #endif /* SSHMAC_H */
3e8b5b
diff -up openssh/Makefile.in.audit openssh/Makefile.in
3e8b5b
--- openssh/Makefile.in.audit	2019-04-03 17:02:20.705885965 +0200
3e8b5b
+++ openssh/Makefile.in	2019-04-03 17:02:20.715886060 +0200
3e8b5b
@@ -109,7 +109,8 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
3e8b5b
 	kexgexc.o kexgexs.o \
3e8b5b
 	sntrup4591761.o kexsntrup4591761x25519.o kexgen.o \
3e8b5b
 	kexgssc.o \
3e8b5b
-	platform-pledge.o platform-tracing.o platform-misc.o
3e8b5b
+	platform-pledge.o platform-tracing.o platform-misc.o \
3e8b5b
+	auditstub.o
3e8b5b
 
3e8b5b
 
3e8b5b
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
3e8b5b
diff -up openssh/monitor.c.audit openssh/monitor.c
3e8b5b
--- openssh/monitor.c.audit	2019-04-03 17:02:20.674885671 +0200
3e8b5b
+++ openssh/monitor.c	2019-04-03 17:03:17.201421405 +0200
3e8b5b
@@ -93,6 +93,7 @@
3e8b5b
 #include "compat.h"
3e8b5b
 #include "ssh2.h"
3e8b5b
 #include "authfd.h"
3e8b5b
+#include "audit.h"
3e8b5b
 #include "match.h"
3e8b5b
 #include "ssherr.h"
3e8b5b
 
3e8b5b
@@ -107,6 +108,8 @@ extern u_char session_id[];
3e8b5b
 extern struct sshbuf *loginmsg;
3e8b5b
 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
3e8b5b
 
3e8b5b
+extern void destroy_sensitive_data(struct ssh *, int);
3e8b5b
+
3e8b5b
 /* State exported from the child */
3e8b5b
 static struct sshbuf *child_state;
3e8b5b
 
3e8b5b
@@ -157,6 +160,11 @@ int mm_answer_gss_updatecreds(struct ssh
3e8b5b
 #ifdef SSH_AUDIT_EVENTS
3e8b5b
 int mm_answer_audit_event(struct ssh *, int, struct sshbuf *);
3e8b5b
 int mm_answer_audit_command(struct ssh *, int, struct sshbuf *);
3e8b5b
+int mm_answer_audit_end_command(struct ssh *, int, struct sshbuf *);
3e8b5b
+int mm_answer_audit_unsupported_body(struct ssh *, int, struct sshbuf *);
3e8b5b
+int mm_answer_audit_kex_body(struct ssh *, int, struct sshbuf *);
3e8b5b
+int mm_answer_audit_session_key_free_body(struct ssh *, int, struct sshbuf *);
3e8b5b
+int mm_answer_audit_server_key_free(struct ssh *, int, struct sshbuf *);
3e8b5b
 #endif
3e8b5b
 
3e8b5b
 static Authctxt *authctxt;
3e8b5b
@@ -215,6 +223,10 @@ struct mon_table mon_dispatch_proto20[]
3e8b5b
 #endif
3e8b5b
 #ifdef SSH_AUDIT_EVENTS
3e8b5b
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
3e8b5b
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
3e8b5b
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
3e8b5b
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
3e8b5b
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
3e8b5b
 #endif
3e8b5b
 #ifdef BSD_AUTH
3e8b5b
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
3e8b5b
@@ -249,6 +261,11 @@ struct mon_table mon_dispatch_postauth20
3e8b5b
 #ifdef SSH_AUDIT_EVENTS
3e8b5b
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
3e8b5b
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
3e8b5b
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
3e8b5b
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
3e8b5b
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
3e8b5b
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
3e8b5b
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
3e8b5b
 #endif
3e8b5b
     {0, 0, NULL}
3e8b5b
 };
3e8b5b
@@ -1445,8 +1462,10 @@ mm_answer_keyverify(struct ssh *ssh, int
3e8b5b
 	char *sigalg;
3e8b5b
 	size_t signaturelen, datalen, bloblen;
3e8b5b
 	int r, ret, valid_data = 0, encoded_ret;
3e8b5b
+	int type = 0;
3e8b5b
 
3e8b5b
-	if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
3e8b5b
+	if ((r = sshbuf_get_u32(m, &type)) != 0 ||
3e8b5b
+	    (r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
3e8b5b
 	    (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
3e8b5b
 	    (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
3e8b5b
 	    (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
3e8b5b
@@ -1455,6 +1474,8 @@ mm_answer_keyverify(struct ssh *ssh, int
3e8b5b
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
3e8b5b
 	  !monitor_allowed_key(blob, bloblen))
3e8b5b
 		fatal("%s: bad key, not previously allowed", __func__);
3e8b5b
+	if (type != key_blobtype)
3e8b5b
+		fatal("%s: bad key type", __func__);
3e8b5b
 
3e8b5b
 	/* Empty signature algorithm means NULL. */
3e8b5b
 	if (*sigalg == '\0') {
3e8b5b
@@ -1470,21 +1491,24 @@ mm_answer_keyverify(struct ssh *ssh, int
3e8b5b
 	case MM_USERKEY:
3e8b5b
 		valid_data = monitor_valid_userblob(data, datalen);
3e8b5b
 		auth_method = "publickey";
3e8b5b
+		ret = user_key_verify(ssh, key, signature, signaturelen, data,
3e8b5b
+		    datalen, sigalg, ssh->compat);
3e8b5b
 		break;
3e8b5b
 	case MM_HOSTKEY:
3e8b5b
 		valid_data = monitor_valid_hostbasedblob(data, datalen,
3e8b5b
 		    hostbased_cuser, hostbased_chost);
3e8b5b
+		ret = hostbased_key_verify(ssh, key, signature, signaturelen, data,
3e8b5b
+		    datalen, sigalg, ssh->compat);
3e8b5b
 		auth_method = "hostbased";
3e8b5b
 		break;
3e8b5b
 	default:
3e8b5b
 		valid_data = 0;
3e8b5b
+		ret = 0;
3e8b5b
 		break;
3e8b5b
 	}
3e8b5b
 	if (!valid_data)
3e8b5b
 		fatal("%s: bad signature data blob", __func__);
3e8b5b
 
3e8b5b
-	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
3e8b5b
-	    sigalg, ssh->compat);
3e8b5b
 	debug3("%s: %s %p signature %s", __func__, auth_method, key,
3e8b5b
 	    (ret == 0) ? "verified" : "unverified");
3e8b5b
 	auth2_record_key(authctxt, ret == 0, key);
3e8b5b
@@ -1536,13 +1560,19 @@ mm_record_login(struct ssh *ssh, Session
3e8b5b
 }
3e8b5b
 
3e8b5b
 static void
3e8b5b
-mm_session_close(Session *s)
3e8b5b
+mm_session_close(struct ssh *ssh, Session *s)
3e8b5b
 {
3e8b5b
 	debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
3e8b5b
 	if (s->ttyfd != -1) {
3e8b5b
 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
3e8b5b
 		session_pty_cleanup2(s);
3e8b5b
 	}
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	if (s->command != NULL) {
3e8b5b
+		debug3("%s: command %d", __func__, s->command_handle);
3e8b5b
+		session_end_command2(ssh, s);
3e8b5b
+	}
3e8b5b
+#endif
3e8b5b
 	session_unused(s->self);
3e8b5b
 }
3e8b5b
 
3e8b5b
@@ -1609,7 +1639,7 @@ mm_answer_pty(struct ssh *ssh, int sock,
3e8b5b
 
3e8b5b
  error:
3e8b5b
 	if (s != NULL)
3e8b5b
-		mm_session_close(s);
3e8b5b
+		mm_session_close(ssh, s);
3e8b5b
 	if ((r = sshbuf_put_u32(m, 0)) != 0)
3e8b5b
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
 	mm_request_send(sock, MONITOR_ANS_PTY, m);
3e8b5b
@@ -1628,7 +1658,7 @@ mm_answer_pty_cleanup(struct ssh *ssh, i
3e8b5b
 	if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
3e8b5b
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
 	if ((s = session_by_tty(tty)) != NULL)
3e8b5b
-		mm_session_close(s);
3e8b5b
+		mm_session_close(ssh, s);
3e8b5b
 	sshbuf_reset(m);
3e8b5b
 	free(tty);
3e8b5b
 	return (0);
3e8b5b
@@ -1650,6 +1680,8 @@ mm_answer_term(struct ssh *ssh, int sock
3e8b5b
 		sshpam_cleanup();
3e8b5b
 #endif
3e8b5b
 
3e8b5b
+	destroy_sensitive_data(ssh, 0);
3e8b5b
+
3e8b5b
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
3e8b5b
 		if (errno != EINTR)
3e8b5b
 			exit(1);
3e8b5b
@@ -1696,12 +1728,47 @@ mm_answer_audit_command(struct ssh *ssh,
3e8b5b
 {
3e8b5b
 	char *cmd;
3e8b5b
 	int r;
3e8b5b
+	Session *s;
3e8b5b
 
3e8b5b
 	debug3("%s entering", __func__);
3e8b5b
 	if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
3e8b5b
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+
3e8b5b
 	/* sanity check command, if so how? */
3e8b5b
-	audit_run_command(cmd);
3e8b5b
+	s = session_new();
3e8b5b
+	if (s == NULL)
3e8b5b
+		fatal("%s: error allocating a session", __func__);
3e8b5b
+	s->command = cmd;
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	s->command_handle = audit_run_command(ssh, cmd);
3e8b5b
+#endif
3e8b5b
+
3e8b5b
+	sshbuf_reset(m);
3e8b5b
+	sshbuf_put_u32(m, s->self);
3e8b5b
+
3e8b5b
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
3e8b5b
+
3e8b5b
+	return (0);
3e8b5b
+}
3e8b5b
+
3e8b5b
+int
3e8b5b
+mm_answer_audit_end_command(struct ssh *ssh, int socket, struct sshbuf *m)
3e8b5b
+{
3e8b5b
+	int handle, r;
3e8b5b
+	size_t len;
3e8b5b
+	u_char *cmd = NULL;
3e8b5b
+	Session *s;
3e8b5b
+
3e8b5b
+	debug3("%s entering", __func__);
3e8b5b
+	if ((r = sshbuf_get_u32(m, &handle)) != 0 ||
3e8b5b
+	    (r = sshbuf_get_string(m, &cmd, &len)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+
3e8b5b
+	s = session_by_id(handle);
3e8b5b
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
3e8b5b
+	    strcmp(s->command, cmd) != 0)
3e8b5b
+		fatal("%s: invalid handle", __func__);
3e8b5b
+	mm_session_close(ssh, s);
3e8b5b
 	free(cmd);
3e8b5b
 	return (0);
3e8b5b
 }
3e8b5b
@@ -1767,6 +1834,7 @@ monitor_apply_keystate(struct ssh *ssh,
3e8b5b
 void
3e8b5b
 mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
3e8b5b
 {
3e8b5b
+	struct sshbuf *m;
3e8b5b
 	debug3("%s: Waiting for new keys", __func__);
3e8b5b
 
3e8b5b
 	if ((child_state = sshbuf_new()) == NULL)
3e8b5b
@@ -1774,6 +1842,19 @@ mm_get_keystate(struct ssh *ssh, struct
3e8b5b
 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
3e8b5b
 	    child_state);
3e8b5b
 	debug3("%s: GOT new keys", __func__);
3e8b5b
+
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	m = sshbuf_new();
3e8b5b
+	mm_request_receive_expect(pmonitor->m_sendfd,
3e8b5b
+				  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
3e8b5b
+	mm_answer_audit_session_key_free_body(ssh, pmonitor->m_sendfd, m);
3e8b5b
+	sshbuf_free(m);
3e8b5b
+#endif
3e8b5b
+
3e8b5b
+	/* Drain any buffered messages from the child */
3e8b5b
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
3e8b5b
+		;
3e8b5b
+
3e8b5b
 }
3e8b5b
 
3e8b5b
 
3e8b5b
@@ -2066,3 +2147,102 @@ mm_answer_gss_updatecreds(struct ssh *ss
3e8b5b
 
3e8b5b
 #endif /* GSSAPI */
3e8b5b
 
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+int
3e8b5b
+mm_answer_audit_unsupported_body(struct ssh *ssh, int sock, struct sshbuf *m)
3e8b5b
+{
3e8b5b
+	int what, r;
3e8b5b
+
3e8b5b
+	if ((r = sshbuf_get_u32(m, &what)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+
3e8b5b
+	audit_unsupported_body(ssh, what);
3e8b5b
+
3e8b5b
+	sshbuf_reset(m);
3e8b5b
+
3e8b5b
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
3e8b5b
+	return 0;
3e8b5b
+}
3e8b5b
+
3e8b5b
+int
3e8b5b
+mm_answer_audit_kex_body(struct ssh *ssh, int sock, struct sshbuf *m)
3e8b5b
+{
3e8b5b
+	int ctos, r;
3e8b5b
+	char *cipher, *mac, *compress, *pfs;
3e8b5b
+	u_int64_t tmp;
3e8b5b
+	pid_t pid;
3e8b5b
+	uid_t uid;
3e8b5b
+
3e8b5b
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
3e8b5b
+	    (r = sshbuf_get_cstring(m, &cipher, NULL)) != 0 ||
3e8b5b
+	    (r = sshbuf_get_cstring(m, &mac, NULL)) != 0 ||
3e8b5b
+	    (r = sshbuf_get_cstring(m, &compress, NULL)) != 0 ||
3e8b5b
+	    (r = sshbuf_get_cstring(m, &pfs, NULL)) != 0 ||
3e8b5b
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+	pid = (pid_t) tmp;
3e8b5b
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+	uid = (pid_t) tmp;
3e8b5b
+
3e8b5b
+	audit_kex_body(ssh, ctos, cipher, mac, compress, pfs, pid, uid);
3e8b5b
+
3e8b5b
+	free(cipher);
3e8b5b
+	free(mac);
3e8b5b
+	free(compress);
3e8b5b
+	free(pfs);
3e8b5b
+	sshbuf_reset(m);
3e8b5b
+
3e8b5b
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
3e8b5b
+	return 0;
3e8b5b
+}
3e8b5b
+
3e8b5b
+int
3e8b5b
+mm_answer_audit_session_key_free_body(struct ssh *ssh, int sock, struct sshbuf *m)
3e8b5b
+{
3e8b5b
+	int ctos, r;
3e8b5b
+	u_int64_t tmp;
3e8b5b
+	pid_t pid;
3e8b5b
+	uid_t uid;
3e8b5b
+
3e8b5b
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
3e8b5b
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+	pid = (pid_t) tmp;
3e8b5b
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+	uid = (uid_t) tmp;
3e8b5b
+
3e8b5b
+	audit_session_key_free_body(ssh, ctos, pid, uid);
3e8b5b
+
3e8b5b
+	sshbuf_reset(m);
3e8b5b
+
3e8b5b
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
3e8b5b
+	return 0;
3e8b5b
+}
3e8b5b
+
3e8b5b
+int
3e8b5b
+mm_answer_audit_server_key_free(struct ssh *ssh, int sock, struct sshbuf *m)
3e8b5b
+{
3e8b5b
+	size_t len, r;
3e8b5b
+	char *fp;
3e8b5b
+	u_int64_t tmp;
3e8b5b
+	pid_t pid;
3e8b5b
+	uid_t uid;
3e8b5b
+
3e8b5b
+	if ((r = sshbuf_get_cstring(m, &fp, &len)) != 0 ||
3e8b5b
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+	pid = (pid_t) tmp;
3e8b5b
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+	uid = (uid_t) tmp;
3e8b5b
+
3e8b5b
+	audit_destroy_sensitive_data(ssh, fp, pid, uid);
3e8b5b
+
3e8b5b
+	free(fp);
3e8b5b
+	sshbuf_reset(m);
3e8b5b
+
3e8b5b
+	return 0;
3e8b5b
+}
3e8b5b
+#endif /* SSH_AUDIT_EVENTS */
3e8b5b
diff -up openssh/monitor.h.audit openssh/monitor.h
3e8b5b
--- openssh/monitor.h.audit	2019-04-03 17:02:20.674885671 +0200
3e8b5b
+++ openssh/monitor.h	2019-04-03 17:02:20.715886060 +0200
3e8b5b
@@ -65,7 +65,13 @@ enum monitor_reqtype {
3e8b5b
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
3e8b5b
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
3e8b5b
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
3e8b5b
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
3e8b5b
+	MONITOR_REQ_AUDIT_EVENT = 112,
3e8b5b
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
3e8b5b
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
3e8b5b
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
3e8b5b
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
3e8b5b
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
3e8b5b
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124,
3e8b5b
 
3e8b5b
 	MONITOR_REQ_GSSSIGN = 150, MONITOR_ANS_GSSSIGN = 151,
3e8b5b
 	MONITOR_REQ_GSSUPCREDS = 152, MONITOR_ANS_GSSUPCREDS = 153,
3e8b5b
diff -up openssh/monitor_wrap.c.audit openssh/monitor_wrap.c
3e8b5b
--- openssh/monitor_wrap.c.audit	2019-04-03 17:02:20.653885472 +0200
3e8b5b
+++ openssh/monitor_wrap.c	2019-04-03 17:02:20.716886069 +0200
3e8b5b
@@ -513,7 +513,7 @@ mm_key_allowed(enum mm_keytype type, con
3e8b5b
  */
3e8b5b
 
3e8b5b
 int
3e8b5b
-mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
3e8b5b
+mm_sshkey_verify(enum mm_keytype type, const struct sshkey *key, const u_char *sig, size_t siglen,
3e8b5b
     const u_char *data, size_t datalen, const char *sigalg, u_int compat)
3e8b5b
 {
3e8b5b
 	struct sshbuf *m;
3e8b5b
@@ -525,7 +525,8 @@ mm_sshkey_verify(const struct sshkey *ke
3e8b5b
 
3e8b5b
 	if ((m = sshbuf_new()) == NULL)
3e8b5b
 		fatal("%s: sshbuf_new failed", __func__);
3e8b5b
-	if ((r = sshkey_puts(key, m)) != 0 ||
3e8b5b
+	if ((r = sshbuf_put_u32(m, type)) != 0 ||
3e8b5b
+	    (r = sshkey_puts(key, m)) != 0 ||
3e8b5b
 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
3e8b5b
 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
3e8b5b
 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
3e8b5b
@@ -547,6 +548,20 @@ mm_sshkey_verify(const struct sshkey *ke
3e8b5b
 	return 0;
3e8b5b
 }
3e8b5b
 
3e8b5b
+int
3e8b5b
+mm_hostbased_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig, size_t siglen,
3e8b5b
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat)
3e8b5b
+{
3e8b5b
+	return mm_sshkey_verify(MM_HOSTKEY, key, sig, siglen, data, datalen, pkalg, compat);
3e8b5b
+}
3e8b5b
+
3e8b5b
+int
3e8b5b
+mm_user_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig, size_t siglen,
3e8b5b
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat)
3e8b5b
+{
3e8b5b
+	return mm_sshkey_verify(MM_USERKEY, key, sig, siglen, data, datalen, pkalg, compat);
3e8b5b
+}
3e8b5b
+
3e8b5b
 void
3e8b5b
 mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
3e8b5b
 {
3e8b5b
@@ -900,11 +915,12 @@ mm_audit_event(struct ssh *ssh, ssh_audi
3e8b5b
 	sshbuf_free(m);
3e8b5b
 }
3e8b5b
 
3e8b5b
-void
3e8b5b
-mm_audit_run_command(const char *command)
3e8b5b
+int
3e8b5b
+mm_audit_run_command(struct ssh *ssh, const char *command)
3e8b5b
 {
3e8b5b
 	struct sshbuf *m;
3e8b5b
 	int r;
3e8b5b
+	int handle;
3e8b5b
 
3e8b5b
 	debug3("%s entering command %s", __func__, command);
3e8b5b
 
3e8b5b
@@ -914,6 +930,30 @@ mm_audit_run_command(const char *command
3e8b5b
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
 
3e8b5b
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
3e8b5b
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, m);
3e8b5b
+
3e8b5b
+	if ((r = sshbuf_get_u32(m, &handle)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+	sshbuf_free(m);
3e8b5b
+
3e8b5b
+	return (handle);
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+mm_audit_end_command(struct ssh *ssh, int handle, const char *command)
3e8b5b
+{
3e8b5b
+	int r;
3e8b5b
+	struct sshbuf *m;
3e8b5b
+
3e8b5b
+	debug3("%s entering command %s", __func__, command);
3e8b5b
+
3e8b5b
+ 	if ((m = sshbuf_new()) == NULL)
3e8b5b
+ 		fatal("%s: sshbuf_new failed", __func__);
3e8b5b
+	if ((r = sshbuf_put_u32(m, handle)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_cstring(m, command)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+
3e8b5b
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, m);
3e8b5b
 	sshbuf_free(m);
3e8b5b
 }
3e8b5b
 #endif /* SSH_AUDIT_EVENTS */
3e8b5b
@@ -1074,3 +1114,83 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_cc
3e8b5b
 }
3e8b5b
 
3e8b5b
 #endif /* GSSAPI */
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+void
3e8b5b
+mm_audit_unsupported_body(struct ssh *ssh, int what)
3e8b5b
+{
3e8b5b
+	int r;
3e8b5b
+	struct sshbuf *m;
3e8b5b
+
3e8b5b
+ 	if ((m = sshbuf_new()) == NULL)
3e8b5b
+ 		fatal("%s: sshbuf_new failed", __func__);
3e8b5b
+	if ((r = sshbuf_put_u32(m, what)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+
3e8b5b
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, m);
3e8b5b
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
3e8b5b
+				  m);
3e8b5b
+
3e8b5b
+	sshbuf_free(m);
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+mm_audit_kex_body(struct ssh *ssh, int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
3e8b5b
+		  uid_t uid)
3e8b5b
+{
3e8b5b
+	int r;
3e8b5b
+	struct sshbuf *m;
3e8b5b
+
3e8b5b
+ 	if ((m = sshbuf_new()) == NULL)
3e8b5b
+ 		fatal("%s: sshbuf_new failed", __func__);
3e8b5b
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_cstring(m, cipher)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_cstring(m, (mac ? mac : "<implicit>"))) != 0 ||
3e8b5b
+	    (r = sshbuf_put_cstring(m, compress)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_cstring(m, fps)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_u64(m, uid)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+
3e8b5b
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, m);
3e8b5b
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
3e8b5b
+				  m);
3e8b5b
+
3e8b5b
+	sshbuf_free(m);
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+mm_audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+	int r;
3e8b5b
+	struct sshbuf *m;
3e8b5b
+
3e8b5b
+ 	if ((m = sshbuf_new()) == NULL)
3e8b5b
+ 		fatal("%s: sshbuf_new failed", __func__);
3e8b5b
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_u64(m, uid)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+
3e8b5b
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
3e8b5b
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
3e8b5b
+				  m);
3e8b5b
+	sshbuf_free(m);
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+mm_audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
3e8b5b
+{
3e8b5b
+	int r;
3e8b5b
+	struct sshbuf *m;
3e8b5b
+
3e8b5b
+ 	if ((m = sshbuf_new()) == NULL)
3e8b5b
+ 		fatal("%s: sshbuf_new failed", __func__);
3e8b5b
+	if ((r = sshbuf_put_cstring(m, fp)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
3e8b5b
+	    (r = sshbuf_put_u64(m, uid)) != 0)
3e8b5b
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
3e8b5b
+
3e8b5b
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, m);
3e8b5b
+	sshbuf_free(m);
3e8b5b
+}
3e8b5b
+#endif /* SSH_AUDIT_EVENTS */
3e8b5b
diff -up openssh/monitor_wrap.h.audit openssh/monitor_wrap.h
3e8b5b
--- openssh/monitor_wrap.h.audit	2019-04-03 17:02:20.653885472 +0200
3e8b5b
+++ openssh/monitor_wrap.h	2019-04-03 17:02:20.716886069 +0200
3e8b5b
@@ -57,7 +57,9 @@ int mm_user_key_allowed(struct ssh *, st
3e8b5b
     struct sshauthopt **);
3e8b5b
 int mm_hostbased_key_allowed(struct ssh *, struct passwd *, const char *,
3e8b5b
     const char *, struct sshkey *);
3e8b5b
-int mm_sshkey_verify(const struct sshkey *, const u_char *, size_t,
3e8b5b
+int mm_hostbased_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
3e8b5b
+    const u_char *, size_t, const char *, u_int);
3e8b5b
+int mm_user_key_verify(struct ssh*, const struct sshkey *, const u_char *, size_t,
3e8b5b
     const u_char *, size_t, const char *, u_int);
3e8b5b
 
3e8b5b
 #ifdef GSSAPI
3e8b5b
@@ -82,7 +84,12 @@ void mm_sshpam_free_ctx(void *);
3e8b5b
 #ifdef SSH_AUDIT_EVENTS
3e8b5b
 #include "audit.h"
3e8b5b
 void mm_audit_event(struct ssh *, ssh_audit_event_t);
3e8b5b
-void mm_audit_run_command(const char *);
3e8b5b
+int mm_audit_run_command(struct ssh *ssh, const char *);
3e8b5b
+void mm_audit_end_command(struct ssh *ssh, int, const char *);
3e8b5b
+void mm_audit_unsupported_body(struct ssh *, int);
3e8b5b
+void mm_audit_kex_body(struct ssh *, int, char *, char *, char *, char *, pid_t, uid_t);
3e8b5b
+void mm_audit_session_key_free_body(struct ssh *, int, pid_t, uid_t);
3e8b5b
+void mm_audit_destroy_sensitive_data(struct ssh *, const char *, pid_t, uid_t);
3e8b5b
 #endif
3e8b5b
 
3e8b5b
 struct Session;
3e8b5b
diff -up openssh/packet.c.audit openssh/packet.c
3e8b5b
--- openssh/packet.c.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/packet.c	2019-04-03 17:02:20.716886069 +0200
3e8b5b
@@ -77,6 +77,7 @@
3e8b5b
 #include <zlib.h>
3e8b5b
 
3e8b5b
 #include "xmalloc.h"
3e8b5b
+#include "audit.h"
3e8b5b
 #include "crc32.h"
3e8b5b
 #include "compat.h"
3e8b5b
 #include "ssh2.h"
3e8b5b
@@ -510,6 +511,13 @@ ssh_packet_get_connection_out(struct ssh
3e8b5b
 	return ssh->state->connection_out;
3e8b5b
 }
3e8b5b
 
3e8b5b
+static int
3e8b5b
+packet_state_has_keys (const struct session_state *state)
3e8b5b
+{
3e8b5b
+	return state != NULL &&
3e8b5b
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
3e8b5b
+}
3e8b5b
+
3e8b5b
 /*
3e8b5b
  * Returns the IP-address of the remote host as a string.  The returned
3e8b5b
  * string must not be freed.
3e8b5b
@@ -587,22 +595,19 @@ ssh_packet_close_internal(struct ssh *ss
3e8b5b
 {
3e8b5b
 	struct session_state *state = ssh->state;
3e8b5b
 	u_int mode;
3e8b5b
+	u_int had_keys = packet_state_has_keys(state);
3e8b5b
 
3e8b5b
 	if (!state->initialized)
3e8b5b
 		return;
3e8b5b
 	state->initialized = 0;
3e8b5b
-	if (do_close) {
3e8b5b
-		if (state->connection_in == state->connection_out) {
3e8b5b
-			close(state->connection_out);
3e8b5b
-		} else {
3e8b5b
-			close(state->connection_in);
3e8b5b
-			close(state->connection_out);
3e8b5b
-		}
3e8b5b
-	}
3e8b5b
 	sshbuf_free(state->input);
3e8b5b
+	state->input = NULL;
3e8b5b
 	sshbuf_free(state->output);
3e8b5b
+	state->output = NULL;
3e8b5b
 	sshbuf_free(state->outgoing_packet);
3e8b5b
+	state->outgoing_packet = NULL;
3e8b5b
 	sshbuf_free(state->incoming_packet);
3e8b5b
+	state->incoming_packet = NULL;
3e8b5b
 	for (mode = 0; mode < MODE_MAX; mode++) {
3e8b5b
 		kex_free_newkeys(state->newkeys[mode]);	/* current keys */
3e8b5b
 		state->newkeys[mode] = NULL;
3e8b5b
@@ -636,8 +641,18 @@ ssh_packet_close_internal(struct ssh *ss
3e8b5b
 	}
3e8b5b
 	cipher_free(state->send_context);
3e8b5b
 	cipher_free(state->receive_context);
3e8b5b
+	if (had_keys && state->server_side) {
3e8b5b
+		/* Assuming this is called only from privsep child */
3e8b5b
+		audit_session_key_free(ssh, MODE_MAX);
3e8b5b
+	}
3e8b5b
 	state->send_context = state->receive_context = NULL;
3e8b5b
 	if (do_close) {
3e8b5b
+		if (state->connection_in == state->connection_out) {
3e8b5b
+			close(state->connection_out);
3e8b5b
+		} else {
3e8b5b
+			close(state->connection_in);
3e8b5b
+			close(state->connection_out);
3e8b5b
+		}
3e8b5b
 		free(ssh->local_ipaddr);
3e8b5b
 		ssh->local_ipaddr = NULL;
3e8b5b
 		free(ssh->remote_ipaddr);
3e8b5b
@@ -864,6 +879,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
3e8b5b
 		   (unsigned long long)state->p_send.bytes,
3e8b5b
 		   (unsigned long long)state->p_send.blocks);
3e8b5b
 		kex_free_newkeys(state->newkeys[mode]);
3e8b5b
+		audit_session_key_free(ssh, mode);
3e8b5b
 		state->newkeys[mode] = NULL;
3e8b5b
 	}
3e8b5b
 	/* note that both bytes and the seqnr are not reset */
3e8b5b
@@ -2167,6 +2183,71 @@ ssh_packet_get_output(struct ssh *ssh)
3e8b5b
 	return (void *)ssh->state->output;
3e8b5b
 }
3e8b5b
 
3e8b5b
+static void
3e8b5b
+newkeys_destroy_and_free(struct newkeys *newkeys)
3e8b5b
+{
3e8b5b
+	if (newkeys == NULL)
3e8b5b
+		return;
3e8b5b
+
3e8b5b
+	free(newkeys->enc.name);
3e8b5b
+
3e8b5b
+	if (newkeys->mac.enabled) {
3e8b5b
+		mac_clear(&newkeys->mac);
3e8b5b
+		free(newkeys->mac.name);
3e8b5b
+	}
3e8b5b
+
3e8b5b
+	free(newkeys->comp.name);
3e8b5b
+
3e8b5b
+	newkeys_destroy(newkeys);
3e8b5b
+	free(newkeys);
3e8b5b
+}
3e8b5b
+
3e8b5b
+static void
3e8b5b
+packet_destroy_state(struct session_state *state)
3e8b5b
+{
3e8b5b
+	if (state == NULL)
3e8b5b
+		return;
3e8b5b
+
3e8b5b
+	cipher_free(state->receive_context);
3e8b5b
+	cipher_free(state->send_context);
3e8b5b
+
3e8b5b
+	sshbuf_free(state->input);
3e8b5b
+	state->input = NULL;
3e8b5b
+	sshbuf_free(state->output);
3e8b5b
+	state->output = NULL;
3e8b5b
+	sshbuf_free(state->outgoing_packet);
3e8b5b
+	state->outgoing_packet = NULL;
3e8b5b
+	sshbuf_free(state->incoming_packet);
3e8b5b
+	state->incoming_packet = NULL;
3e8b5b
+	if (state->compression_buffer) {
3e8b5b
+		sshbuf_free(state->compression_buffer);
3e8b5b
+		state->compression_buffer = NULL;
3e8b5b
+	}
3e8b5b
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
3e8b5b
+	state->newkeys[MODE_IN] = NULL;
3e8b5b
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
3e8b5b
+	state->newkeys[MODE_OUT] = NULL;
3e8b5b
+	mac_destroy(state->packet_discard_mac);
3e8b5b
+//	TAILQ_HEAD(, packet) outgoing;
3e8b5b
+//	memset(state, 0, sizeof(state));
3e8b5b
+}
3e8b5b
+
3e8b5b
+void
3e8b5b
+packet_destroy_all(struct ssh *ssh, int audit_it, int privsep)
3e8b5b
+{
3e8b5b
+	if (audit_it)
3e8b5b
+		audit_it = packet_state_has_keys(ssh->state);
3e8b5b
+	packet_destroy_state(ssh->state);
3e8b5b
+	if (audit_it) {
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+		if (privsep)
3e8b5b
+			audit_session_key_free(ssh, MODE_MAX);
3e8b5b
+		else
3e8b5b
+			audit_session_key_free_body(ssh, MODE_MAX, getpid(), getuid());
3e8b5b
+#endif
3e8b5b
+	}
3e8b5b
+}
3e8b5b
+
3e8b5b
 /* Reset after_authentication and reset compression in post-auth privsep */
3e8b5b
 static int
3e8b5b
 ssh_packet_set_postauth(struct ssh *ssh)
3e8b5b
diff -up openssh/packet.h.audit openssh/packet.h
3e8b5b
--- openssh/packet.h.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/packet.h	2019-04-03 17:02:20.716886069 +0200
3e8b5b
@@ -217,4 +217,5 @@ const u_char	*sshpkt_ptr(struct ssh *, s
3e8b5b
 # undef EC_POINT
3e8b5b
 #endif
3e8b5b
 
3e8b5b
+void	 packet_destroy_all(struct ssh *, int, int);
3e8b5b
 #endif				/* PACKET_H */
3e8b5b
diff -up openssh/session.c.audit openssh/session.c
3e8b5b
--- openssh/session.c.audit	2019-04-03 17:02:20.712886031 +0200
3e8b5b
+++ openssh/session.c	2019-04-03 17:02:20.716886069 +0200
3e8b5b
@@ -136,7 +136,7 @@ extern char *__progname;
3e8b5b
 extern int debug_flag;
3e8b5b
 extern u_int utmp_len;
3e8b5b
 extern int startup_pipe;
3e8b5b
-extern void destroy_sensitive_data(void);
3e8b5b
+extern void destroy_sensitive_data(struct ssh *, int);
3e8b5b
 extern struct sshbuf *loginmsg;
3e8b5b
 extern struct sshauthopt *auth_opts;
3e8b5b
 extern char *tun_fwd_ifnames; /* serverloop.c */
3e8b5b
@@ -648,6 +648,14 @@ do_exec_pty(struct ssh *ssh, Session *s,
3e8b5b
 	/* Parent.  Close the slave side of the pseudo tty. */
3e8b5b
 	close(ttyfd);
3e8b5b
 
3e8b5b
+#if !defined(HAVE_OSF_SIA) && defined(SSH_AUDIT_EVENTS)
3e8b5b
+	/* do_login in the child did not affect state in this process,
3e8b5b
+	   compensate.  From an architectural standpoint, this is extremely
3e8b5b
+	   ugly. */
3e8b5b
+	if (command != NULL)
3e8b5b
+		audit_count_session_open();
3e8b5b
+#endif
3e8b5b
+
3e8b5b
 	/* Enter interactive session. */
3e8b5b
 	s->ptymaster = ptymaster;
3e8b5b
 	ssh_packet_set_interactive(ssh, 1,
3e8b5b
@@ -740,15 +748,19 @@ do_exec(struct ssh *ssh, Session *s, con
3e8b5b
 	    s->self);
3e8b5b
 
3e8b5b
 #ifdef SSH_AUDIT_EVENTS
3e8b5b
+	if (s->command != NULL || s->command_handle != -1)
3e8b5b
+		fatal("do_exec: command already set");
3e8b5b
 	if (command != NULL)
3e8b5b
-		PRIVSEP(audit_run_command(command));
3e8b5b
+		s->command = xstrdup(command);
3e8b5b
 	else if (s->ttyfd == -1) {
3e8b5b
 		char *shell = s->pw->pw_shell;
3e8b5b
 
3e8b5b
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
3e8b5b
 			shell =_PATH_BSHELL;
3e8b5b
-		PRIVSEP(audit_run_command(shell));
3e8b5b
+		s->command = xstrdup(shell);
3e8b5b
 	}
3e8b5b
+	if (s->command != NULL && s->ptyfd == -1)
3e8b5b
+		s->command_handle = PRIVSEP(audit_run_command(ssh, s->command));
3e8b5b
 #endif
3e8b5b
 	if (s->ttyfd != -1)
3e8b5b
 		ret = do_exec_pty(ssh, s, command);
3e8b5b
@@ -1556,8 +1568,11 @@ do_child(struct ssh *ssh, Session *s, co
3e8b5b
 	sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
3e8b5b
 
3e8b5b
 	/* remove hostkey from the child's memory */
3e8b5b
-	destroy_sensitive_data();
3e8b5b
+	destroy_sensitive_data(ssh, 1);
3e8b5b
 	ssh_packet_clear_keys(ssh);
3e8b5b
+	/* Don't audit this - both us and the parent would be talking to the
3e8b5b
+	   monitor over a single socket, with no synchronization. */
3e8b5b
+	packet_destroy_all(ssh, 0, 1);
3e8b5b
 
3e8b5b
 	/* Force a password change */
3e8b5b
 	if (s->authctxt->force_pwchange) {
3e8b5b
@@ -1769,6 +1784,9 @@ session_unused(int id)
3e8b5b
 	sessions[id].ttyfd = -1;
3e8b5b
 	sessions[id].ptymaster = -1;
3e8b5b
 	sessions[id].x11_chanids = NULL;
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	sessions[id].command_handle = -1;
3e8b5b
+#endif
3e8b5b
 	sessions[id].next_unused = sessions_first_unused;
3e8b5b
 	sessions_first_unused = id;
3e8b5b
 }
3e8b5b
@@ -1851,6 +1869,19 @@ session_open(Authctxt *authctxt, int cha
3e8b5b
 }
3e8b5b
 
3e8b5b
 Session *
3e8b5b
+session_by_id(int id)
3e8b5b
+{
3e8b5b
+	if (id >= 0 && id < sessions_nalloc) {
3e8b5b
+		Session *s = &sessions[id];
3e8b5b
+		if (s->used)
3e8b5b
+			return s;
3e8b5b
+	}
3e8b5b
+	debug("%s: unknown id %d", __func__, id);
3e8b5b
+	session_dump();
3e8b5b
+	return NULL;
3e8b5b
+}
3e8b5b
+
3e8b5b
+Session *
3e8b5b
 session_by_tty(char *tty)
3e8b5b
 {
3e8b5b
 	int i;
3e8b5b
@@ -2461,6 +2492,32 @@ session_exit_message(struct ssh *ssh, Se
3e8b5b
 		chan_write_failed(ssh, c);
3e8b5b
 }
3e8b5b
 
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+void
3e8b5b
+session_end_command2(struct ssh *ssh, Session *s)
3e8b5b
+{
3e8b5b
+	if (s->command != NULL) {
3e8b5b
+		if (s->command_handle != -1)
3e8b5b
+			audit_end_command(ssh, s->command_handle, s->command);
3e8b5b
+		free(s->command);
3e8b5b
+		s->command = NULL;
3e8b5b
+		s->command_handle = -1;
3e8b5b
+	}
3e8b5b
+}
3e8b5b
+
3e8b5b
+static void
3e8b5b
+session_end_command(struct ssh *ssh, Session *s)
3e8b5b
+{
3e8b5b
+	if (s->command != NULL) {
3e8b5b
+		if (s->command_handle != -1)
3e8b5b
+			PRIVSEP(audit_end_command(ssh, s->command_handle, s->command));
3e8b5b
+		free(s->command);
3e8b5b
+		s->command = NULL;
3e8b5b
+		s->command_handle = -1;
3e8b5b
+	}
3e8b5b
+}
3e8b5b
+#endif
3e8b5b
+
3e8b5b
 void
3e8b5b
 session_close(struct ssh *ssh, Session *s)
3e8b5b
 {
3e8b5b
@@ -2474,6 +2531,10 @@ session_close(struct ssh *ssh, Session *
3e8b5b
 
3e8b5b
 	if (s->ttyfd != -1)
3e8b5b
 		session_pty_cleanup(s);
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	if (s->command)
3e8b5b
+		session_end_command(ssh, s);
3e8b5b
+#endif
3e8b5b
 	free(s->term);
3e8b5b
 	free(s->display);
3e8b5b
 	free(s->x11_chanids);
3e8b5b
@@ -2549,14 +2610,14 @@ session_close_by_channel(struct ssh *ssh
3e8b5b
 }
3e8b5b
 
3e8b5b
 void
3e8b5b
-session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
3e8b5b
+session_destroy_all(struct ssh *ssh, void (*closefunc)(struct ssh *ssh, Session *))
3e8b5b
 {
3e8b5b
 	int i;
3e8b5b
 	for (i = 0; i < sessions_nalloc; i++) {
3e8b5b
 		Session *s = &sessions[i];
3e8b5b
 		if (s->used) {
3e8b5b
 			if (closefunc != NULL)
3e8b5b
-				closefunc(s);
3e8b5b
+				closefunc(ssh, s);
3e8b5b
 			else
3e8b5b
 				session_close(ssh, s);
3e8b5b
 		}
3e8b5b
@@ -2683,6 +2744,15 @@ do_authenticated2(struct ssh *ssh, Authc
3e8b5b
 	server_loop2(ssh, authctxt);
3e8b5b
 }
3e8b5b
 
3e8b5b
+static void
3e8b5b
+do_cleanup_one_session(struct ssh *ssh, Session *s)
3e8b5b
+{
3e8b5b
+	session_pty_cleanup2(s);
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	session_end_command2(ssh, s);
3e8b5b
+#endif
3e8b5b
+}
3e8b5b
+
3e8b5b
 void
3e8b5b
 do_cleanup(struct ssh *ssh, Authctxt *authctxt)
3e8b5b
 {
3e8b5b
@@ -2746,7 +2816,7 @@ do_cleanup(struct ssh *ssh, Authctxt *au
3e8b5b
 	 * or if running in monitor.
3e8b5b
 	 */
3e8b5b
 	if (!use_privsep || mm_is_monitor())
3e8b5b
-		session_destroy_all(ssh, session_pty_cleanup2);
3e8b5b
+		session_destroy_all(ssh, do_cleanup_one_session);
3e8b5b
 }
3e8b5b
 
3e8b5b
 /* Return a name for the remote host that fits inside utmp_size */
3e8b5b
diff -up openssh/session.h.audit openssh/session.h
3e8b5b
--- openssh/session.h.audit	2019-03-27 23:26:14.000000000 +0100
3e8b5b
+++ openssh/session.h	2019-04-03 17:02:20.717886079 +0200
3e8b5b
@@ -61,6 +61,12 @@ struct Session {
3e8b5b
 		char	*name;
3e8b5b
 		char	*val;
3e8b5b
 	} *env;
3e8b5b
+
3e8b5b
+	/* exec */
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	int	command_handle;
3e8b5b
+	char	*command;
3e8b5b
+#endif
3e8b5b
 };
3e8b5b
 
3e8b5b
 void	 do_authenticated(struct ssh *, Authctxt *);
3e8b5b
@@ -71,10 +77,12 @@ void	 session_unused(int);
3e8b5b
 int	 session_input_channel_req(struct ssh *, Channel *, const char *);
3e8b5b
 void	 session_close_by_pid(struct ssh *ssh, pid_t, int);
3e8b5b
 void	 session_close_by_channel(struct ssh *, int, void *);
3e8b5b
-void	 session_destroy_all(struct ssh *, void (*)(Session *));
3e8b5b
+void	 session_destroy_all(struct ssh *, void (*)(struct ssh*, Session *));
3e8b5b
 void	 session_pty_cleanup2(Session *);
3e8b5b
+void	 session_end_command2(struct ssh *ssh, Session *);
3e8b5b
 
3e8b5b
 Session	*session_new(void);
3e8b5b
+Session *session_by_id(int);
3e8b5b
 Session	*session_by_tty(char *);
3e8b5b
 void	 session_close(struct ssh *, Session *);
3e8b5b
 void	 do_setusercontext(struct passwd *);
3e8b5b
diff -up openssh/sshd.c.audit openssh/sshd.c
3e8b5b
--- openssh/sshd.c.audit	2019-04-03 17:02:20.692885842 +0200
3e8b5b
+++ openssh/sshd.c	2019-04-03 17:02:20.717886079 +0200
3e8b5b
@@ -122,6 +122,7 @@
3e8b5b
 #include "ssh-gss.h"
3e8b5b
 #endif
3e8b5b
 #include "monitor_wrap.h"
3e8b5b
+#include "audit.h"
3e8b5b
 #include "ssh-sandbox.h"
3e8b5b
 #include "auth-options.h"
3e8b5b
 #include "version.h"
3e8b5b
@@ -261,8 +262,8 @@ struct sshbuf *loginmsg;
3e8b5b
 struct passwd *privsep_pw = NULL;
3e8b5b
 
3e8b5b
 /* Prototypes for various functions defined later in this file. */
3e8b5b
-void destroy_sensitive_data(void);
3e8b5b
-void demote_sensitive_data(void);
3e8b5b
+void destroy_sensitive_data(struct ssh *, int);
3e8b5b
+void demote_sensitive_data(struct ssh *);
3e8b5b
 static void do_ssh2_kex(struct ssh *);
3e8b5b
 
3e8b5b
 /*
3e8b5b
@@ -278,6 +279,15 @@ close_listen_socks(void)
3e8b5b
 	num_listen_socks = -1;
3e8b5b
 }
3e8b5b
 
3e8b5b
+/*
3e8b5b
+ * Is this process listening for clients (i.e. not specific to any specific
3e8b5b
+ * client connection?)
3e8b5b
+ */
3e8b5b
+int listening_for_clients(void)
3e8b5b
+{
3e8b5b
+	return num_listen_socks >= 0;
3e8b5b
+}
3e8b5b
+
3e8b5b
 static void
3e8b5b
 close_startup_pipes(void)
3e8b5b
 {
3e8b5b
@@ -380,18 +390,45 @@ grace_alarm_handler(int sig)
3e8b5b
 	    ssh_remote_port(the_active_state));
3e8b5b
 }
3e8b5b
 
3e8b5b
-/* Destroy the host and server keys.  They will no longer be needed. */
3e8b5b
+/*
3e8b5b
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
3e8b5b
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
3e8b5b
+ */
3e8b5b
 void
3e8b5b
-destroy_sensitive_data(void)
3e8b5b
+destroy_sensitive_data(struct ssh *ssh, int privsep)
3e8b5b
 {
3e8b5b
 	u_int i;
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	pid_t pid;
3e8b5b
+	uid_t uid;
3e8b5b
 
3e8b5b
+	pid = getpid();
3e8b5b
+	uid = getuid();
3e8b5b
+#endif
3e8b5b
 	for (i = 0; i < options.num_host_key_files; i++) {
3e8b5b
 		if (sensitive_data.host_keys[i]) {
3e8b5b
+			char *fp;
3e8b5b
+
3e8b5b
+			if (sshkey_is_private(sensitive_data.host_keys[i]))
3e8b5b
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
3e8b5b
+			else
3e8b5b
+				fp = NULL;
3e8b5b
 			sshkey_free(sensitive_data.host_keys[i]);
3e8b5b
 			sensitive_data.host_keys[i] = NULL;
3e8b5b
+			if (fp != NULL) {
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+				if (privsep)
3e8b5b
+					PRIVSEP(audit_destroy_sensitive_data(ssh, fp,
3e8b5b
+						pid, uid));
3e8b5b
+				else
3e8b5b
+					audit_destroy_sensitive_data(ssh, fp,
3e8b5b
+						pid, uid);
3e8b5b
+#endif
3e8b5b
+				free(fp);
3e8b5b
+			}
3e8b5b
 		}
3e8b5b
-		if (sensitive_data.host_certificates[i]) {
3e8b5b
+		if (sensitive_data.host_certificates
3e8b5b
+		    && sensitive_data.host_certificates[i]) {
3e8b5b
 			sshkey_free(sensitive_data.host_certificates[i]);
3e8b5b
 			sensitive_data.host_certificates[i] = NULL;
3e8b5b
 		}
3e8b5b
@@ -400,14 +437,26 @@ destroy_sensitive_data(void)
3e8b5b
 
3e8b5b
 /* Demote private to public keys for network child */
3e8b5b
 void
3e8b5b
-demote_sensitive_data(void)
3e8b5b
+demote_sensitive_data(struct ssh *ssh)
3e8b5b
 {
3e8b5b
 	struct sshkey *tmp;
3e8b5b
 	u_int i;
3e8b5b
 	int r;
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+	pid_t pid;
3e8b5b
+	uid_t uid;
3e8b5b
 
3e8b5b
+	pid = getpid();
3e8b5b
+	uid = getuid();
3e8b5b
+#endif
3e8b5b
 	for (i = 0; i < options.num_host_key_files; i++) {
3e8b5b
 		if (sensitive_data.host_keys[i]) {
3e8b5b
+			char *fp;
3e8b5b
+
3e8b5b
+			if (sshkey_is_private(sensitive_data.host_keys[i]))
3e8b5b
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
3e8b5b
+			else
3e8b5b
+				fp = NULL;
3e8b5b
 			if ((r = sshkey_from_private(
3e8b5b
 			    sensitive_data.host_keys[i], &tmp)) != 0)
3e8b5b
 				fatal("could not demote host %s key: %s",
3e8b5b
@@ -415,6 +464,12 @@ demote_sensitive_data(void)
3e8b5b
 				    ssh_err(r));
3e8b5b
 			sshkey_free(sensitive_data.host_keys[i]);
3e8b5b
 			sensitive_data.host_keys[i] = tmp;
3e8b5b
+			if (fp != NULL) {
3e8b5b
+#ifdef SSH_AUDIT_EVENTS
3e8b5b
+				audit_destroy_sensitive_data(ssh, fp, pid, uid);
3e8b5b
+#endif
3e8b5b
+				free(fp);
3e8b5b
+			}
3e8b5b
 		}
3e8b5b
 		/* Certs do not need demotion */
3e8b5b
 	}
3e8b5b
@@ -442,7 +497,7 @@ reseed_prngs(void)
3e8b5b
 }
3e8b5b
 
3e8b5b
 static void
3e8b5b
-privsep_preauth_child(void)
3e8b5b
+privsep_preauth_child(struct ssh *ssh)
3e8b5b
 {
3e8b5b
 	gid_t gidset[1];
3e8b5b
 
3e8b5b
@@ -457,7 +512,7 @@ privsep_preauth_child(void)
3e8b5b
 	reseed_prngs();
3e8b5b
 
3e8b5b
 	/* Demote the private keys to public keys. */
3e8b5b
-	demote_sensitive_data();
3e8b5b
+	demote_sensitive_data(ssh);
3e8b5b
 
3e8b5b
 #ifdef WITH_SELINUX
3e8b5b
 	sshd_selinux_change_privsep_preauth_context();
3e8b5b
@@ -496,7 +551,7 @@ privsep_preauth(struct ssh *ssh)
3e8b5b
 
3e8b5b
 	if (use_privsep == PRIVSEP_ON)
3e8b5b
 		box = ssh_sandbox_init(pmonitor);
3e8b5b
-	pid = fork();
3e8b5b
+	pmonitor->m_pid = pid = fork();
3e8b5b
 	if (pid == -1) {
3e8b5b
 		fatal("fork of unprivileged child failed");
3e8b5b
 	} else if (pid != 0) {
3e8b5b
@@ -542,7 +597,7 @@ privsep_preauth(struct ssh *ssh)
3e8b5b
 		/* Arrange for logging to be sent to the monitor */
3e8b5b
 		set_log_handler(mm_log_handler, pmonitor);
3e8b5b
 
3e8b5b
-		privsep_preauth_child();
3e8b5b
+		privsep_preauth_child(ssh);
3e8b5b
 		setproctitle("%s", "[net]");
3e8b5b
 		if (box != NULL)
3e8b5b
 			ssh_sandbox_child(box);
3e8b5b
@@ -594,7 +649,7 @@ privsep_postauth(struct ssh *ssh, Authct
3e8b5b
 		set_log_handler(mm_log_handler, pmonitor);
3e8b5b
 
3e8b5b
 	/* Demote the private keys to public keys. */
3e8b5b
-	demote_sensitive_data();
3e8b5b
+	demote_sensitive_data(ssh);
3e8b5b
 
3e8b5b
 	reseed_prngs();
3e8b5b
 
3e8b5b
@@ -1057,7 +1112,7 @@ server_listen(void)
3e8b5b
  * from this function are in a forked subprocess.
3e8b5b
  */
3e8b5b
 static void
3e8b5b
-server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
3e8b5b
+server_accept_loop(struct ssh *ssh, int *sock_in, int *sock_out, int *newsock, int *config_s)
3e8b5b
 {
3e8b5b
 	fd_set *fdset;
3e8b5b
 	int i, j, ret, maxfd;
3e8b5b
@@ -1112,6 +1167,7 @@ server_accept_loop(int *sock_in, int *so
3e8b5b
 		if (received_sigterm) {
3e8b5b
 			logit("Received signal %d; terminating.",
3e8b5b
 			    (int) received_sigterm);
3e8b5b
+			destroy_sensitive_data(ssh, 0);
3e8b5b
 			close_listen_socks();
3e8b5b
 			if (options.pid_file != NULL)
3e8b5b
 				unlink(options.pid_file);
3e8b5b
@@ -1978,7 +2034,7 @@ main(int ac, char **av)
3e8b5b
 #endif
3e8b5b
 
3e8b5b
 		/* Accept a connection and return in a forked child */
3e8b5b
-		server_accept_loop(&sock_in, &sock_out,
3e8b5b
+		server_accept_loop(ssh, &sock_in, &sock_out,
3e8b5b
 		    &newsock, config_s);
3e8b5b
 	}
3e8b5b
 
3e8b5b
@@ -2222,6 +2278,9 @@ main(int ac, char **av)
3e8b5b
 	do_authenticated(ssh, authctxt);
3e8b5b
 
3e8b5b
 	/* The connection has been terminated. */
3e8b5b
+	packet_destroy_all(ssh, 1, 1);
3e8b5b
+	destroy_sensitive_data(ssh, 1);
3e8b5b
+
3e8b5b
 	ssh_packet_get_bytes(ssh, &ibytes, &obytes);
3e8b5b
 	verbose("Transferred: sent %llu, received %llu bytes",
3e8b5b
 	    (unsigned long long)obytes, (unsigned long long)ibytes);
3e8b5b
@@ -2401,6 +2460,15 @@ do_ssh2_kex(struct ssh *ssh)
3e8b5b
 void
3e8b5b
 cleanup_exit(int i)
3e8b5b
 {
3e8b5b
+	static int in_cleanup = 0;
3e8b5b
+	int is_privsep_child;
3e8b5b
+
3e8b5b
+	/* cleanup_exit can be called at the very least from the privsep
3e8b5b
+	   wrappers used for auditing.  Make sure we don't recurse
3e8b5b
+	   indefinitely. */
3e8b5b
+	if (in_cleanup)
3e8b5b
+		_exit(i);
3e8b5b
+	in_cleanup = 1;
3e8b5b
 	if (the_active_state != NULL && the_authctxt != NULL) {
3e8b5b
 		do_cleanup(the_active_state, the_authctxt);
3e8b5b
 		if (use_privsep && privsep_is_preauth &&
3e8b5b
@@ -2414,9 +2482,16 @@ cleanup_exit(int i)
3e8b5b
 				    pmonitor->m_pid, strerror(errno));
3e8b5b
 		}
3e8b5b
 	}
3e8b5b
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
3e8b5b
+	if (sensitive_data.host_keys != NULL && the_active_state != NULL)
3e8b5b
+		destroy_sensitive_data(the_active_state, is_privsep_child);
3e8b5b
+	if (the_active_state != NULL)
3e8b5b
+		packet_destroy_all(the_active_state, 1, is_privsep_child);
3e8b5b
 #ifdef SSH_AUDIT_EVENTS
3e8b5b
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
3e8b5b
-	if (the_active_state != NULL && (!use_privsep || mm_is_monitor()))
3e8b5b
+	if (the_active_state != NULL &&
3e8b5b
+	    (the_authctxt == NULL || !the_authctxt->authenticated) &&
3e8b5b
+	    (!use_privsep || mm_is_monitor()))
3e8b5b
 		audit_event(the_active_state, SSH_CONNECTION_ABANDON);
3e8b5b
 #endif
3e8b5b
 	_exit(i);
3e8b5b
diff -up openssh/sshkey.c.audit openssh/sshkey.c
3e8b5b
--- openssh/sshkey.c.audit	2019-04-03 17:02:20.657885510 +0200
3e8b5b
+++ openssh/sshkey.c	2019-04-03 17:02:20.718886088 +0200
3e8b5b
@@ -331,6 +331,38 @@ sshkey_type_is_valid_ca(int type)
3e8b5b
 }
3e8b5b
 
3e8b5b
 int
3e8b5b
+sshkey_is_private(const struct sshkey *k)
3e8b5b
+{
3e8b5b
+      switch (k->type) {
3e8b5b
+#ifdef WITH_OPENSSL
3e8b5b
+      case KEY_RSA_CERT:
3e8b5b
+      case KEY_RSA: {
3e8b5b
+              const BIGNUM *d;
3e8b5b
+              RSA_get0_key(k->rsa, NULL, NULL, &d);
3e8b5b
+              return d != NULL;
3e8b5b
+          }
3e8b5b
+      case KEY_DSA_CERT:
3e8b5b
+      case KEY_DSA: {
3e8b5b
+              const BIGNUM *priv_key;
3e8b5b
+              DSA_get0_key(k->dsa, NULL, &priv_key);
3e8b5b
+              return priv_key != NULL;
3e8b5b
+          }
3e8b5b
+#ifdef OPENSSL_HAS_ECC
3e8b5b
+      case KEY_ECDSA_CERT:
3e8b5b
+      case KEY_ECDSA:
3e8b5b
+              return EC_KEY_get0_private_key(k->ecdsa) != NULL;
3e8b5b
+#endif /* OPENSSL_HAS_ECC */
3e8b5b
+#endif /* WITH_OPENSSL */
3e8b5b
+      case KEY_ED25519_CERT:
3e8b5b
+      case KEY_ED25519:
3e8b5b
+              return (k->ed25519_pk != NULL);
3e8b5b
+      default:
3e8b5b
+              /* fatal("key_is_private: bad key type %d", k->type); */
3e8b5b
+              return 0;
3e8b5b
+      }
3e8b5b
+}
3e8b5b
+
3e8b5b
+int
3e8b5b
 sshkey_is_cert(const struct sshkey *k)
3e8b5b
 {
3e8b5b
 	if (k == NULL)
3e8b5b
diff -up openssh/sshkey.h.audit openssh/sshkey.h
3e8b5b
--- openssh/sshkey.h.audit	2019-04-03 17:02:20.657885510 +0200
3e8b5b
+++ openssh/sshkey.h	2019-04-03 17:02:20.718886088 +0200
3e8b5b
@@ -148,6 +148,7 @@ u_int		 sshkey_size(const struct sshkey
3e8b5b
 int		 sshkey_generate(int type, u_int bits, struct sshkey **keyp);
3e8b5b
 int		 sshkey_from_private(const struct sshkey *, struct sshkey **);
3e8b5b
 int	 sshkey_type_from_name(const char *);
3e8b5b
+int	 sshkey_is_private(const struct sshkey *);
3e8b5b
 int	 sshkey_is_cert(const struct sshkey *);
3e8b5b
 int	 sshkey_type_is_cert(int);
3e8b5b
 int	 sshkey_type_plain(int);