8f2528
diff -up openssh-7.4p1/audit-bsm.c.audit openssh-7.4p1/audit-bsm.c
8f2528
--- openssh-7.4p1/audit-bsm.c.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/audit-bsm.c	2016-12-23 18:54:54.433080419 +0100
8f2528
@@ -373,10 +373,23 @@ audit_connection_from(const char *host,
8f2528
 #endif
8f2528
 }
8f2528
 
8f2528
-void
8f2528
+int
8f2528
 audit_run_command(const char *command)
8f2528
 {
8f2528
 	/* not implemented */
8f2528
+	return 0;
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_end_command(int handle, const char *command)
8f2528
+{
8f2528
+	/* not implemented */
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_count_session_open(void)
8f2528
+{
8f2528
+	/* not necessary */
8f2528
 }
8f2528
 
8f2528
 void
8f2528
@@ -391,6 +404,12 @@ audit_session_close(struct logininfo *li
8f2528
 	/* not implemented */
8f2528
 }
8f2528
 
8f2528
+int
8f2528
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
8f2528
+{
8f2528
+	/* not implemented */
8f2528
+}
8f2528
+
8f2528
 void
8f2528
 audit_event(ssh_audit_event_t event)
8f2528
 {
8f2528
@@ -452,4 +471,40 @@ audit_event(ssh_audit_event_t event)
8f2528
 		debug("%s: unhandled event %d", __func__, event);
8f2528
 	}
8f2528
 }
8f2528
+
8f2528
+void
8f2528
+audit_unsupported_body(int what)
8f2528
+{
8f2528
+	/* not implemented */
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+	/* not implemented */
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+	/* not implemented */
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_destroy_sensitive_data(const char *fp)
8f2528
+{
8f2528
+	/* not implemented */
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+	/* not implemented */
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_generate_ephemeral_server_key(const char *fp)
8f2528
+{
8f2528
+	/* not implemented */
8f2528
+}
8f2528
 #endif /* BSM */
8f2528
diff -up openssh-7.4p1/audit.c.audit openssh-7.4p1/audit.c
8f2528
--- openssh-7.4p1/audit.c.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/audit.c	2016-12-23 18:54:54.433080419 +0100
8f2528
@@ -26,6 +26,7 @@
8f2528
 
8f2528
 #include <stdarg.h>
8f2528
 #include <string.h>
8f2528
+#include <unistd.h>
8f2528
 
8f2528
 #ifdef SSH_AUDIT_EVENTS
8f2528
 
8f2528
@@ -34,6 +35,11 @@
8f2528
 #include "key.h"
8f2528
 #include "hostfile.h"
8f2528
 #include "auth.h"
8f2528
+#include "ssh-gss.h"
8f2528
+#include "monitor_wrap.h"
8f2528
+#include "xmalloc.h"
8f2528
+#include "misc.h"
8f2528
+#include "servconf.h"
8f2528
 
8f2528
 /*
8f2528
  * Care must be taken when using this since it WILL NOT be initialized when
8f2528
@@ -41,6 +47,7 @@
8f2528
  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
8f2528
  */
8f2528
 extern Authctxt *the_authctxt;
8f2528
+extern ServerOptions options;
8f2528
 
8f2528
 /* Maybe add the audit class to struct Authmethod? */
8f2528
 ssh_audit_event_t
8f2528
@@ -69,13 +76,10 @@ audit_classify_auth(const char *method)
8f2528
 const char *
8f2528
 audit_username(void)
8f2528
 {
8f2528
-	static const char unknownuser[] = "(unknown user)";
8f2528
-	static const char invaliduser[] = "(invalid user)";
8f2528
+	static const char unknownuser[] = "(unknown)";
8f2528
 
8f2528
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
8f2528
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
8f2528
 		return (unknownuser);
8f2528
-	if (!the_authctxt->valid)
8f2528
-		return (invaliduser);
8f2528
 	return (the_authctxt->user);
8f2528
 }
8f2528
 
8f2528
@@ -109,6 +113,40 @@ audit_event_lookup(ssh_audit_event_t ev)
8f2528
 	return(event_lookup[i].name);
8f2528
 }
8f2528
 
8f2528
+void
8f2528
+audit_key(int host_user, int *rv, const Key *key)
8f2528
+{
8f2528
+	char *fp;
8f2528
+	const char *crypto_name;
8f2528
+
8f2528
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
8f2528
+	if (key->type == KEY_RSA1)
8f2528
+		crypto_name = "ssh-rsa1";
8f2528
+	else
8f2528
+		crypto_name = key_ssh_name(key);
8f2528
+	if (audit_keyusage(host_user, crypto_name, key_size(key), fp, *rv) == 0)
8f2528
+		*rv = 0;
8f2528
+	free(fp);
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_unsupported(int what)
8f2528
+{
8f2528
+	PRIVSEP(audit_unsupported_body(what));
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
8f2528
+{
8f2528
+	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, pfs, getpid(), getuid()));
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_session_key_free(int ctos)
8f2528
+{
8f2528
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
8f2528
+}
8f2528
+
8f2528
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
8f2528
 /*
8f2528
  * Null implementations of audit functions.
8f2528
@@ -138,6 +176,17 @@ audit_event(ssh_audit_event_t event)
8f2528
 }
8f2528
 
8f2528
 /*
8f2528
+ * Called when a child process has called, or will soon call,
8f2528
+ * audit_session_open.
8f2528
+ */
8f2528
+void
8f2528
+audit_count_session_open(void)
8f2528
+{
8f2528
+	debug("audit count session open euid %d user %s", geteuid(),
8f2528
+	      audit_username());
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
  * Called when a user session is started.  Argument is the tty allocated to
8f2528
  * the session, or NULL if no tty was allocated.
8f2528
  *
8f2528
@@ -172,13 +221,91 @@ audit_session_close(struct logininfo *li
8f2528
 /*
8f2528
  * This will be called when a user runs a non-interactive command.  Note that
8f2528
  * it may be called multiple times for a single connection since SSH2 allows
8f2528
- * multiple sessions within a single connection.
8f2528
+ * multiple sessions within a single connection.  Returns a "handle" for
8f2528
+ * audit_end_command.
8f2528
  */
8f2528
-void
8f2528
+int
8f2528
 audit_run_command(const char *command)
8f2528
 {
8f2528
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
8f2528
 	    audit_username(), command);
8f2528
+	return 0;
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
+ * This will be called when the non-interactive command finishes.  Note that
8f2528
+ * it may be called multiple times for a single connection since SSH2 allows
8f2528
+ * multiple sessions within a single connection.  "handle" should come from
8f2528
+ * the corresponding audit_run_command.
8f2528
+ */
8f2528
+void
8f2528
+audit_end_command(int handle, const char *command)
8f2528
+{
8f2528
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
8f2528
+	    audit_username(), command);
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
8f2528
+ *
8f2528
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
8f2528
+ */
8f2528
+int
8f2528
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
8f2528
+{
8f2528
+	debug("audit %s key usage euid %d user %s key type %s key length %d fingerprint %s, result %d",
8f2528
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(), type, bits,
8f2528
+		fp, rv);
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
+ * This will be called when the protocol negotiation fails.
8f2528
+ */
8f2528
+void
8f2528
+audit_unsupported_body(int what)
8f2528
+{
8f2528
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
+ * This will be called on succesfull protocol negotiation.
8f2528
+ */
8f2528
+void
8f2528
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
8f2528
+	       uid_t uid)
8f2528
+{
8f2528
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s pfs %s from pid %ld uid %u",
8f2528
+		(unsigned)geteuid(), ctos, enc, mac, compress, pfs, (long)pid,
8f2528
+	        (unsigned)uid);
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
+ * This will be called on succesfull session key discard
8f2528
+ */
8f2528
+void
8f2528
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
8f2528
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
+ * This will be called on destroy private part of the server key
8f2528
+ */
8f2528
+void
8f2528
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
8f2528
+		geteuid(), fp, (long)pid, (unsigned)uid);
8f2528
+}
8f2528
+
8f2528
+/*
8f2528
+ * This will be called on generation of the ephemeral server key
8f2528
+ */
8f2528
+void
8f2528
+audit_generate_ephemeral_server_key(const char *)
8f2528
+{
8f2528
+	debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp);
8f2528
 }
8f2528
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
8f2528
 #endif /* SSH_AUDIT_EVENTS */
8f2528
diff -up openssh-7.4p1/audit.h.audit openssh-7.4p1/audit.h
8f2528
--- openssh-7.4p1/audit.h.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/audit.h	2016-12-23 18:54:54.433080419 +0100
8f2528
@@ -26,6 +26,7 @@
8f2528
 # define _SSH_AUDIT_H
8f2528
 
8f2528
 #include "loginrec.h"
8f2528
+#include "key.h"
8f2528
 
8f2528
 enum ssh_audit_event_type {
8f2528
 	SSH_LOGIN_EXCEED_MAXTRIES,
8f2528
@@ -43,13 +44,33 @@ enum ssh_audit_event_type {
8f2528
 	SSH_CONNECTION_ABANDON,	/* closed without completing auth */
8f2528
 	SSH_AUDIT_UNKNOWN
8f2528
 };
8f2528
+
8f2528
+enum ssh_audit_kex {
8f2528
+	SSH_AUDIT_UNSUPPORTED_CIPHER,
8f2528
+	SSH_AUDIT_UNSUPPORTED_MAC,
8f2528
+	SSH_AUDIT_UNSUPPORTED_COMPRESSION
8f2528
+};
8f2528
 typedef enum ssh_audit_event_type ssh_audit_event_t;
8f2528
 
8f2528
+int	listening_for_clients(void);
8f2528
+
8f2528
 void	audit_connection_from(const char *, int);
8f2528
 void	audit_event(ssh_audit_event_t);
8f2528
+void	audit_count_session_open(void);
8f2528
 void	audit_session_open(struct logininfo *);
8f2528
 void	audit_session_close(struct logininfo *);
8f2528
-void	audit_run_command(const char *);
8f2528
+int	audit_run_command(const char *);
8f2528
+void 	audit_end_command(int, const char *);
8f2528
 ssh_audit_event_t audit_classify_auth(const char *);
8f2528
+int	audit_keyusage(int, const char *, unsigned, char *, int);
8f2528
+void	audit_key(int, int *, const Key *);
8f2528
+void	audit_unsupported(int);
8f2528
+void	audit_kex(int, char *, char *, char *, char *);
8f2528
+void	audit_unsupported_body(int);
8f2528
+void	audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
8f2528
+void	audit_session_key_free(int ctos);
8f2528
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
8f2528
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
8f2528
+void	audit_generate_ephemeral_server_key(const char *);
8f2528
 
8f2528
 #endif /* _SSH_AUDIT_H */
8f2528
diff -up openssh-7.4p1/audit-linux.c.audit openssh-7.4p1/audit-linux.c
8f2528
--- openssh-7.4p1/audit-linux.c.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/audit-linux.c	2016-12-23 18:54:54.434080419 +0100
8f2528
@@ -33,25 +33,38 @@
8f2528
 
8f2528
 #include "log.h"
8f2528
 #include "audit.h"
8f2528
+#include "key.h"
8f2528
+#include "hostfile.h"
8f2528
+#include "auth.h"
8f2528
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
8f2528
+#include "servconf.h"
8f2528
 #include "canohost.h"
8f2528
 #include "packet.h"
8f2528
-
8f2528
+#include "cipher.h"
8f2528
+#include "channels.h"
8f2528
+#include "session.h"
8f2528
+
8f2528
+#define AUDIT_LOG_SIZE 256
8f2528
+
8f2528
+extern ServerOptions options;
8f2528
+extern Authctxt *the_authctxt;
8f2528
+extern u_int utmp_len;
8f2528
 const char *audit_username(void);
8f2528
 
8f2528
-int
8f2528
-linux_audit_record_event(int uid, const char *username, const char *hostname,
8f2528
-    const char *ip, const char *ttyn, int success)
8f2528
+static void
8f2528
+linux_audit_user_logxxx(int uid, const char *username, const char *hostname,
8f2528
+    const char *ip, const char *ttyn, int success, int event)
8f2528
 {
8f2528
 	int audit_fd, rc, saved_errno;
8f2528
 
8f2528
 	if ((audit_fd = audit_open()) < 0) {
8f2528
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
8f2528
 		    errno == EAFNOSUPPORT)
8f2528
-			return 1; /* No audit support in kernel */
8f2528
+			return; /* No audit support in kernel */
8f2528
 		else
8f2528
-			return 0; /* Must prevent login */
8f2528
+			goto fatal_report; /* Must prevent login */
8f2528
 	}
8f2528
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
8f2528
+	rc = audit_log_acct_message(audit_fd, event,
8f2528
 	    NULL, "login", username ? username : "(unknown)",
8f2528
 	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
8f2528
 	saved_errno = errno;
8f2528
@@ -65,9 +78,97 @@ linux_audit_record_event(int uid, const
8f2528
 		rc = 0;
8f2528
 	errno = saved_errno;
8f2528
 
8f2528
-	return rc >= 0;
8f2528
+	if (rc < 0) {
8f2528
+fatal_report:
8f2528
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
8f2528
+	}
8f2528
+}
8f2528
+
8f2528
+static void
8f2528
+linux_audit_user_auth(int uid, const char *username,
8f2528
+    const char *hostname, const char *ip, const char *ttyn, int success, int event)
8f2528
+{
8f2528
+	int audit_fd, rc, saved_errno;
8f2528
+	static const char *event_name[] = {
8f2528
+		"maxtries exceeded",
8f2528
+		"root denied",
8f2528
+		"success",
8f2528
+		"none",
8f2528
+		"password",
8f2528
+		"challenge-response",
8f2528
+		"pubkey",
8f2528
+		"hostbased",
8f2528
+		"gssapi",
8f2528
+		"invalid user",
8f2528
+		"nologin",
8f2528
+		"connection closed",
8f2528
+		"connection abandoned",
8f2528
+		"unknown"
8f2528
+	};
8f2528
+
8f2528
+	audit_fd = audit_open();
8f2528
+	if (audit_fd < 0) {
8f2528
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
8f2528
+		    errno == EAFNOSUPPORT)
8f2528
+			return; /* No audit support in kernel */
8f2528
+		else
8f2528
+			goto fatal_report; /* Must prevent login */
8f2528
+	}
8f2528
+
8f2528
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
8f2528
+		event = SSH_AUDIT_UNKNOWN;
8f2528
+
8f2528
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
8f2528
+	    NULL, event_name[event], username ? username : "(unknown)",
8f2528
+	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
8f2528
+	saved_errno = errno;
8f2528
+	close(audit_fd);
8f2528
+	/*
8f2528
+	 * Do not report error if the error is EPERM and sshd is run as non
8f2528
+	 * root user.
8f2528
+	 */
8f2528
+	if ((rc == -EPERM) && (geteuid() != 0))
8f2528
+		rc = 0;
8f2528
+	errno = saved_errno;
8f2528
+	if (rc < 0) {
8f2528
+fatal_report:
8f2528
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
8f2528
+	}
8f2528
+}
8f2528
+
8f2528
+int
8f2528
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
8f2528
+{
8f2528
+	char buf[AUDIT_LOG_SIZE];
8f2528
+	int audit_fd, rc, saved_errno;
8f2528
+
8f2528
+	audit_fd = audit_open();
8f2528
+	if (audit_fd < 0) {
8f2528
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
8f2528
+					 errno == EAFNOSUPPORT)
8f2528
+			return 1; /* No audit support in kernel */
8f2528
+		else
8f2528
+			return 0; /* Must prevent login */
8f2528
+	}
8f2528
+	snprintf(buf, sizeof(buf), "%s_auth rport=%d", host_user ? "pubkey" : "hostbased", ssh_remote_port(active_state));
8f2528
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
8f2528
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(active_state), NULL, rv);
8f2528
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
8f2528
+		goto out;
8f2528
+	snprintf(buf, sizeof(buf), "key algo=%s size=%d fp=%s rport=%d",
8f2528
+			type, bits, fp, ssh_remote_port(active_state));
8f2528
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
8f2528
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(active_state), NULL, rv);
8f2528
+out:
8f2528
+	saved_errno = errno;
8f2528
+	audit_close(audit_fd);
8f2528
+	errno = saved_errno;
8f2528
+	/* do not report error if the error is EPERM and sshd is run as non root user */
8f2528
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
8f2528
 }
8f2528
 
8f2528
+static int user_login_count = 0;
8f2528
+
8f2528
 /* Below is the sshd audit API code */
8f2528
 
8f2528
 void
8f2528
@@ -76,24 +177,51 @@ audit_connection_from(const char *host,
8f2528
 	/* not implemented */
8f2528
 }
8f2528
 
8f2528
-void
8f2528
+int
8f2528
 audit_run_command(const char *command)
8f2528
 {
8f2528
-	/* not implemented */
8f2528
+	if (!user_login_count++)
8f2528
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
8f2528
+		    NULL, "ssh", 1, AUDIT_USER_LOGIN);
8f2528
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
8f2528
+	    NULL, "ssh", 1, AUDIT_USER_START);
8f2528
+	return 0;
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_end_command(int handle, const char *command)
8f2528
+{
8f2528
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
8f2528
+	    NULL, "ssh", 1, AUDIT_USER_END);
8f2528
+	if (user_login_count && !--user_login_count)
8f2528
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
8f2528
+		    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_count_session_open(void)
8f2528
+{
8f2528
+	user_login_count++;
8f2528
 }
8f2528
 
8f2528
 void
8f2528
 audit_session_open(struct logininfo *li)
8f2528
 {
8f2528
-	if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,
8f2528
-	    li->line, 1) == 0)
8f2528
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
8f2528
+	if (!user_login_count++)
8f2528
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
8f2528
+		    NULL, li->line, 1, AUDIT_USER_LOGIN);
8f2528
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
8f2528
+	    NULL, li->line, 1, AUDIT_USER_START);
8f2528
 }
8f2528
 
8f2528
 void
8f2528
 audit_session_close(struct logininfo *li)
8f2528
 {
8f2528
-	/* not implemented */
8f2528
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
8f2528
+	    NULL, li->line, 1, AUDIT_USER_END);
8f2528
+	if (user_login_count && !--user_login_count)
8f2528
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
8f2528
+		    NULL, li->line, 1, AUDIT_USER_LOGOUT);
8f2528
 }
8f2528
 
8f2528
 void
8f2528
@@ -103,24 +231,180 @@ audit_event(ssh_audit_event_t event)
8f2528
 
8f2528
 	switch(event) {
8f2528
 	case SSH_AUTH_SUCCESS:
8f2528
-	case SSH_CONNECTION_CLOSE:
8f2528
+		linux_audit_user_auth(-1, audit_username(), NULL,
8f2528
+			ssh_remote_ipaddr(ssh), "ssh", 1, event);
8f2528
+		break;
8f2528
+
8f2528
 	case SSH_NOLOGIN:
8f2528
-	case SSH_LOGIN_EXCEED_MAXTRIES:
8f2528
 	case SSH_LOGIN_ROOT_DENIED:
8f2528
+		linux_audit_user_auth(-1, audit_username(), NULL,
8f2528
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
8f2528
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
8f2528
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
8f2528
 		break;
8f2528
+	case SSH_LOGIN_EXCEED_MAXTRIES:
8f2528
 	case SSH_AUTH_FAIL_NONE:
8f2528
 	case SSH_AUTH_FAIL_PASSWD:
8f2528
 	case SSH_AUTH_FAIL_KBDINT:
8f2528
 	case SSH_AUTH_FAIL_PUBKEY:
8f2528
 	case SSH_AUTH_FAIL_HOSTBASED:
8f2528
 	case SSH_AUTH_FAIL_GSSAPI:
8f2528
+		linux_audit_user_auth(-1, audit_username(), NULL,
8f2528
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
8f2528
+		break;
8f2528
+
8f2528
+	case SSH_CONNECTION_CLOSE:
8f2528
+		if (user_login_count) {
8f2528
+			while (user_login_count--)
8f2528
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
8f2528
+				    session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
8f2528
+				    NULL, "ssh", 1, AUDIT_USER_END);
8f2528
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
8f2528
+			    session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
8f2528
+			    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
8f2528
+		}
8f2528
+		break;
8f2528
+
8f2528
+	case SSH_CONNECTION_ABANDON:
8f2528
 	case SSH_INVALID_USER:
8f2528
-		linux_audit_record_event(-1, audit_username(), NULL,
8f2528
-		    ssh_remote_ipaddr(ssh), "sshd", 0);
8f2528
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
8f2528
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
8f2528
 		break;
8f2528
 	default:
8f2528
 		debug("%s: unhandled event %d", __func__, event);
8f2528
 		break;
8f2528
 	}
8f2528
 }
8f2528
+
8f2528
+void
8f2528
+audit_unsupported_body(int what)
8f2528
+{
8f2528
+#ifdef AUDIT_CRYPTO_SESSION
8f2528
+	char buf[AUDIT_LOG_SIZE];
8f2528
+	const static char *name[] = { "cipher", "mac", "comp" };
8f2528
+	char *s;
8f2528
+	int audit_fd;
8f2528
+
8f2528
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
8f2528
+		name[what], ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())),
8f2528
+		ssh_local_port(active_state));
8f2528
+	free(s);
8f2528
+	audit_fd = audit_open();
8f2528
+	if (audit_fd < 0)
8f2528
+		/* no problem, the next instruction will be fatal() */
8f2528
+		return;
8f2528
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
8f2528
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 0);
8f2528
+	audit_close(audit_fd);
8f2528
+#endif
8f2528
+}
8f2528
+
8f2528
+const static char *direction[] = { "from-server", "from-client", "both" };
8f2528
+
8f2528
+void
8f2528
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
8f2528
+	       uid_t uid)
8f2528
+{
8f2528
+#ifdef AUDIT_CRYPTO_SESSION
8f2528
+	char buf[AUDIT_LOG_SIZE];
8f2528
+	int audit_fd, audit_ok;
8f2528
+	const struct sshcipher *cipher = cipher_by_name(enc);
8f2528
+	char *s;
8f2528
+
8f2528
+	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 ",
8f2528
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs,
8f2528
+		(intmax_t)pid, (intmax_t)uid,
8f2528
+		ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())), ssh_local_port(active_state));
8f2528
+	free(s);
8f2528
+	audit_fd = audit_open();
8f2528
+	if (audit_fd < 0) {
8f2528
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
8f2528
+					 errno == EAFNOSUPPORT)
8f2528
+			return; /* No audit support in kernel */
8f2528
+		else
8f2528
+			fatal("cannot open audit"); /* Must prevent login */
8f2528
+	}
8f2528
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
8f2528
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
8f2528
+	audit_close(audit_fd);
8f2528
+	/* do not abort if the error is EPERM and sshd is run as non root user */
8f2528
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
8f2528
+		fatal("cannot write into audit"); /* Must prevent login */
8f2528
+#endif
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+	char buf[AUDIT_LOG_SIZE];
8f2528
+	int audit_fd, audit_ok;
8f2528
+	char *s;
8f2528
+
8f2528
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
8f2528
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
8f2528
+		 ssh_remote_port(active_state),
8f2528
+		 (s = get_local_ipaddr(packet_get_connection_in())),
8f2528
+		 ssh_local_port(active_state));
8f2528
+	free(s);
8f2528
+	audit_fd = audit_open();
8f2528
+	if (audit_fd < 0) {
8f2528
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
8f2528
+					 errno != EAFNOSUPPORT)
8f2528
+			error("cannot open audit");
8f2528
+		return;
8f2528
+	}
8f2528
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
8f2528
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
8f2528
+	audit_close(audit_fd);
8f2528
+	/* do not abort if the error is EPERM and sshd is run as non root user */
8f2528
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
8f2528
+		error("cannot write into audit");
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+	char buf[AUDIT_LOG_SIZE];
8f2528
+	int audit_fd, audit_ok;
8f2528
+
8f2528
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
8f2528
+		fp, (intmax_t)pid, (intmax_t)uid);
8f2528
+	audit_fd = audit_open();
8f2528
+	if (audit_fd < 0) {
8f2528
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
8f2528
+					 errno != EAFNOSUPPORT)
8f2528
+			error("cannot open audit");
8f2528
+		return;
8f2528
+	}
8f2528
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
8f2528
+			buf, NULL,
8f2528
+			listening_for_clients() ? NULL : ssh_remote_ipaddr(active_state),
8f2528
+			NULL, 1);
8f2528
+	audit_close(audit_fd);
8f2528
+	/* do not abort if the error is EPERM and sshd is run as non root user */
8f2528
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
8f2528
+		error("cannot write into audit");
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_generate_ephemeral_server_key(const char *fp)
8f2528
+{
8f2528
+	char buf[AUDIT_LOG_SIZE];
8f2528
+	int audit_fd, audit_ok;
8f2528
+
8f2528
+	snprintf(buf, sizeof(buf), "op=create kind=server fp=%s direction=? ", fp);
8f2528
+	audit_fd = audit_open();
8f2528
+	if (audit_fd < 0) {
8f2528
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
8f2528
+					 errno != EAFNOSUPPORT)
8f2528
+			error("cannot open audit");
8f2528
+		return;
8f2528
+	}
8f2528
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
8f2528
+			buf, NULL, 0, NULL, 1);
8f2528
+	audit_close(audit_fd);
8f2528
+	/* do not abort if the error is EPERM and sshd is run as non root user */
8f2528
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
8f2528
+		error("cannot write into audit");
8f2528
+}
8f2528
 #endif /* USE_LINUX_AUDIT */
8f2528
diff -up openssh-7.4p1/auditstub.c.audit openssh-7.4p1/auditstub.c
8f2528
--- openssh-7.4p1/auditstub.c.audit	2016-12-23 18:54:54.434080419 +0100
8f2528
+++ openssh-7.4p1/auditstub.c	2016-12-23 18:54:54.434080419 +0100
8f2528
@@ -0,0 +1,50 @@
8f2528
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
8f2528
+
8f2528
+/*
8f2528
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
8f2528
+ * Use is subject to license terms.
8f2528
+ *
8f2528
+ * Redistribution and use in source and binary forms, with or without
8f2528
+ * modification, are permitted provided that the following conditions
8f2528
+ * are met:
8f2528
+ * 1. Redistributions of source code must retain the above copyright
8f2528
+ *    notice, this list of conditions and the following disclaimer.
8f2528
+ * 2. Redistributions in binary form must reproduce the above copyright
8f2528
+ *    notice, this list of conditions and the following disclaimer in the
8f2528
+ *    documentation and/or other materials provided with the distribution.
8f2528
+ *
8f2528
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
8f2528
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8f2528
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
8f2528
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
8f2528
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
8f2528
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8f2528
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8f2528
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8f2528
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
8f2528
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8f2528
+ *
8f2528
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
8f2528
+ */
8f2528
+
8f2528
+#include <sys/types.h>
8f2528
+
8f2528
+void
8f2528
+audit_unsupported(int n)
8f2528
+{
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
8f2528
+{
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_session_key_free(int ctos)
8f2528
+{
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+}
8f2528
diff -up openssh-7.4p1/auth2.c.audit openssh-7.4p1/auth2.c
8f2528
--- openssh-7.4p1/auth2.c.audit	2016-12-23 18:54:54.422080416 +0100
8f2528
+++ openssh-7.4p1/auth2.c	2016-12-23 18:54:54.434080419 +0100
8f2528
@@ -249,9 +249,6 @@ input_userauth_request(int type, u_int32
8f2528
 		} else {
8f2528
 			logit("input_userauth_request: invalid user %s", user);
8f2528
 			authctxt->pw = fakepw();
8f2528
-#ifdef SSH_AUDIT_EVENTS
8f2528
-			PRIVSEP(audit_event(SSH_INVALID_USER));
8f2528
-#endif
8f2528
 		}
8f2528
 #ifdef USE_PAM
8f2528
 		if (options.use_pam)
8f2528
diff -up openssh-7.4p1/auth2-hostbased.c.audit openssh-7.4p1/auth2-hostbased.c
8f2528
--- openssh-7.4p1/auth2-hostbased.c.audit	2016-12-23 18:54:54.422080416 +0100
8f2528
+++ openssh-7.4p1/auth2-hostbased.c	2016-12-23 18:54:54.434080419 +0100
8f2528
@@ -148,7 +148,7 @@ userauth_hostbased(Authctxt *authctxt)
8f2528
 	/* test for allowed key and correct signature */
8f2528
 	authenticated = 0;
8f2528
 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
8f2528
-	    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
8f2528
+	    PRIVSEP(hostbased_key_verify(key, sig, slen, buffer_ptr(&b),
8f2528
 			buffer_len(&b))) == 1) {
8f2528
 		authenticated = 1;
8f2528
 		authctxt->last_details = pubkey;
8f2528
@@ -169,6 +169,18 @@ done:
8f2528
 	return authenticated;
8f2528
 }
8f2528
 
8f2528
+int
8f2528
+hostbased_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
8f2528
+{
8f2528
+	int rv;
8f2528
+
8f2528
+	rv = key_verify(key, sig, slen, data, datalen);
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	audit_key(0, &rv, key);
8f2528
+#endif
8f2528
+	return rv;
8f2528
+}
8f2528
+
8f2528
 /* return 1 if given hostkey is allowed */
8f2528
 int
8f2528
 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
8f2528
diff -up openssh-7.4p1/auth2-pubkey.c.audit openssh-7.4p1/auth2-pubkey.c
8f2528
--- openssh-7.4p1/auth2-pubkey.c.audit	2016-12-23 18:54:54.423080416 +0100
8f2528
+++ openssh-7.4p1/auth2-pubkey.c	2016-12-23 18:54:54.435080419 +0100
8f2528
@@ -183,7 +183,7 @@ userauth_pubkey(Authctxt *authctxt)
8f2528
 		/* test for correct signature */
8f2528
 		authenticated = 0;
8f2528
 		if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
8f2528
-		    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
8f2528
+		    PRIVSEP(user_key_verify(key, sig, slen, buffer_ptr(&b),
8f2528
 		    buffer_len(&b))) == 1) {
8f2528
 			authenticated = 1;
8f2528
 			authctxt->last_details = pubkey;
8f2528
@@ -252,6 +252,18 @@ pubkey_auth_info(Authctxt *authctxt, con
8f2528
 	free(extra);
8f2528
 }
8f2528
 
8f2528
+int
8f2528
+user_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
8f2528
+{
8f2528
+	int rv;
8f2528
+
8f2528
+	rv = key_verify(key, sig, slen, data, datalen);
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	audit_key(1, &rv, key);
8f2528
+#endif
8f2528
+	return rv;
8f2528
+}
8f2528
+
8f2528
 /*
8f2528
  * Splits 's' into an argument vector. Handles quoted string and basic
8f2528
  * escape characters (\\, \", \'). Caller must free the argument vector
8f2528
diff -up openssh-7.4p1/auth.c.audit openssh-7.4p1/auth.c
8f2528
--- openssh-7.4p1/auth.c.audit	2016-12-23 18:54:54.373080404 +0100
8f2528
+++ openssh-7.4p1/auth.c	2016-12-23 18:54:54.435080419 +0100
8f2528
@@ -666,9 +666,6 @@ getpwnamallow(const char *user)
8f2528
 		record_failed_login(user,
8f2528
 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
8f2528
 #endif
8f2528
-#ifdef SSH_AUDIT_EVENTS
8f2528
-		audit_event(SSH_INVALID_USER);
8f2528
-#endif /* SSH_AUDIT_EVENTS */
8f2528
 		return (NULL);
8f2528
 	}
8f2528
 	if (!allowed_user(pw))
8f2528
diff -up openssh-7.4p1/auth.h.audit openssh-7.4p1/auth.h
8f2528
--- openssh-7.4p1/auth.h.audit	2016-12-23 18:54:54.423080416 +0100
8f2528
+++ openssh-7.4p1/auth.h	2016-12-23 18:54:54.435080419 +0100
8f2528
@@ -185,6 +185,7 @@ struct passwd * getpwnamallow(const char
8f2528
 
8f2528
 char	*expand_authorized_keys(const char *, struct passwd *pw);
8f2528
 char	*authorized_principals_file(struct passwd *);
8f2528
+int	 user_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
8f2528
 
8f2528
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
8f2528
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
8f2528
@@ -204,6 +205,7 @@ Key	*get_hostkey_private_by_type(int, in
8f2528
 int	 get_hostkey_index(Key *, int, struct ssh *);
8f2528
 int	 sshd_hostkey_sign(Key *, Key *, u_char **, size_t *,
8f2528
 	     const u_char *, size_t, const char *, u_int);
8f2528
+int	 hostbased_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
8f2528
 
8f2528
 /* debug messages during authentication */
8f2528
 void	 auth_debug_add(const char *fmt,...) __attribute__((format(printf, 1, 2)));
8f2528
diff -up openssh-7.4p1/cipher.c.audit openssh-7.4p1/cipher.c
8f2528
--- openssh-7.4p1/cipher.c.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/cipher.c	2016-12-23 18:54:54.435080419 +0100
8f2528
@@ -66,26 +66,6 @@ struct sshcipher_ctx {
8f2528
 	const struct sshcipher *cipher;
8f2528
 };
8f2528
 
8f2528
-struct sshcipher {
8f2528
-	char	*name;
8f2528
-	int	number;		/* for ssh1 only */
8f2528
-	u_int	block_size;
8f2528
-	u_int	key_len;
8f2528
-	u_int	iv_len;		/* defaults to block_size */
8f2528
-	u_int	auth_len;
8f2528
-	u_int	discard_len;
8f2528
-	u_int	flags;
8f2528
-#define CFLAG_CBC		(1<<0)
8f2528
-#define CFLAG_CHACHAPOLY	(1<<1)
8f2528
-#define CFLAG_AESCTR		(1<<2)
8f2528
-#define CFLAG_NONE		(1<<3)
8f2528
-#ifdef WITH_OPENSSL
8f2528
-	const EVP_CIPHER	*(*evptype)(void);
8f2528
-#else
8f2528
-	void	*ignored;
8f2528
-#endif
8f2528
-};
8f2528
-
8f2528
 static const struct sshcipher ciphers[] = {
8f2528
 #ifdef WITH_SSH1
8f2528
 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
8f2528
diff -up openssh-7.4p1/cipher.h.audit openssh-7.4p1/cipher.h
8f2528
--- openssh-7.4p1/cipher.h.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/cipher.h	2016-12-23 18:54:54.436080419 +0100
8f2528
@@ -62,7 +62,25 @@
8f2528
 #define CIPHER_ENCRYPT		1
8f2528
 #define CIPHER_DECRYPT		0
8f2528
 
8f2528
-struct sshcipher;
8f2528
+struct sshcipher { /* from cipher.c */
8f2528
+	char	*name;
8f2528
+	int	number;		/* for ssh1 only */
8f2528
+	u_int	block_size;
8f2528
+	u_int	key_len;
8f2528
+	u_int	iv_len;		/* defaults to block_size */
8f2528
+	u_int	auth_len;
8f2528
+	u_int	discard_len;
8f2528
+	u_int	flags;
8f2528
+#define CFLAG_CBC		(1<<0)
8f2528
+#define CFLAG_CHACHAPOLY	(1<<1)
8f2528
+#define CFLAG_AESCTR		(1<<2)
8f2528
+#define CFLAG_NONE		(1<<3)
8f2528
+#ifdef WITH_OPENSSL
8f2528
+	const EVP_CIPHER	*(*evptype)(void);
8f2528
+#else
8f2528
+	void	*ignored;
8f2528
+#endif
8f2528
+};
8f2528
 struct sshcipher_ctx;
8f2528
 
8f2528
 u_int	 cipher_mask_ssh1(int);
8f2528
diff -up openssh-7.4p1/kex.c.audit openssh-7.4p1/kex.c
8f2528
--- openssh-7.4p1/kex.c.audit	2016-12-23 18:54:54.410080413 +0100
8f2528
+++ openssh-7.4p1/kex.c	2016-12-23 18:54:54.436080419 +0100
8f2528
@@ -54,6 +54,7 @@
8f2528
 #include "ssherr.h"
8f2528
 #include "sshbuf.h"
8f2528
 #include "digest.h"
8f2528
+#include "audit.h"
8f2528
 
8f2528
 #ifdef GSSAPI
8f2528
 #include "ssh-gss.h"
8f2528
@@ -683,8 +684,12 @@ choose_enc(struct sshenc *enc, char *cli
8f2528
 {
8f2528
 	char *name = match_list(client, server, NULL);
8f2528
 
8f2528
-	if (name == NULL)
8f2528
+	if (name == NULL) {
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_CIPHER);
8f2528
+#endif
8f2528
 		return SSH_ERR_NO_CIPHER_ALG_MATCH;
8f2528
+	}
8f2528
 	if ((enc->cipher = cipher_by_name(name)) == NULL)
8f2528
 		return SSH_ERR_INTERNAL_ERROR;
8f2528
 	enc->name = name;
8f2528
@@ -702,8 +707,12 @@ choose_mac(struct ssh *ssh, struct sshma
8f2528
 {
8f2528
 	char *name = match_list(client, server, NULL);
8f2528
 
8f2528
-	if (name == NULL)
8f2528
+	if (name == NULL) {
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_MAC);
8f2528
+#endif
8f2528
 		return SSH_ERR_NO_MAC_ALG_MATCH;
8f2528
+	}
8f2528
 	if (mac_setup(mac, name) < 0)
8f2528
 		return SSH_ERR_INTERNAL_ERROR;
8f2528
 	/* truncate the key */
8f2528
@@ -720,8 +729,12 @@ choose_comp(struct sshcomp *comp, char *
8f2528
 {
8f2528
 	char *name = match_list(client, server, NULL);
8f2528
 
8f2528
-	if (name == NULL)
8f2528
+	if (name == NULL) {
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_COMPRESSION);
8f2528
+#endif
8f2528
 		return SSH_ERR_NO_COMPRESS_ALG_MATCH;
8f2528
+	}
8f2528
 	if (strcmp(name, "zlib@openssh.com") == 0) {
8f2528
 		comp->type = COMP_DELAYED;
8f2528
 	} else if (strcmp(name, "zlib") == 0) {
8f2528
@@ -890,6 +903,10 @@ kex_choose_conf(struct ssh *ssh)
8f2528
 		dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
8f2528
 		dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
8f2528
 		dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
8f2528
+		debug("kex: %s need=%d dh_need=%d", kex->name, need, dh_need);
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+		audit_kex(mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
8f2528
+#endif
8f2528
 	}
8f2528
 	/* XXX need runden? */
8f2528
 	kex->we_need = need;
8f2528
@@ -1064,3 +1081,33 @@ dump_digest(char *msg, u_char *digest, i
8f2528
 	sshbuf_dump_data(digest, len, stderr);
8f2528
 }
8f2528
 #endif
8f2528
+
8f2528
+static void
8f2528
+enc_destroy(struct sshenc *enc)
8f2528
+{
8f2528
+	if (enc == NULL)
8f2528
+		return;
8f2528
+
8f2528
+	if (enc->key) {
8f2528
+		memset(enc->key, 0, enc->key_len);
8f2528
+		free(enc->key);
8f2528
+	}
8f2528
+
8f2528
+	if (enc->iv) {
8f2528
+		memset(enc->iv,  0, enc->iv_len);
8f2528
+		free(enc->iv);
8f2528
+	}
8f2528
+
8f2528
+	memset(enc, 0, sizeof(*enc));
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+newkeys_destroy(struct newkeys *newkeys)
8f2528
+{
8f2528
+	if (newkeys == NULL)
8f2528
+		return;
8f2528
+
8f2528
+	enc_destroy(&newkeys->enc);
8f2528
+	mac_destroy(&newkeys->mac);
8f2528
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
8f2528
+}
8f2528
diff -up openssh-7.4p1/kex.h.audit openssh-7.4p1/kex.h
8f2528
--- openssh-7.4p1/kex.h.audit	2016-12-23 18:54:54.410080413 +0100
8f2528
+++ openssh-7.4p1/kex.h	2016-12-23 18:54:54.436080419 +0100
8f2528
@@ -213,6 +213,8 @@ int	 kexgss_client(struct ssh *);
8f2528
 int	 kexgss_server(struct ssh *);
8f2528
 #endif
8f2528
 
8f2528
+void	newkeys_destroy(struct newkeys *newkeys);
8f2528
+
8f2528
 int	 kex_dh_hash(int, const char *, const char *,
8f2528
     const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
8f2528
     const BIGNUM *, const BIGNUM *, const BIGNUM *, u_char *, size_t *);
8f2528
diff -up openssh-7.4p1/key.h.audit openssh-7.4p1/key.h
8f2528
--- openssh-7.4p1/key.h.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/key.h	2016-12-23 18:54:54.436080419 +0100
8f2528
@@ -50,6 +50,7 @@ typedef struct sshkey Key;
8f2528
 #define key_ecdsa_bits_to_nid	sshkey_ecdsa_bits_to_nid
8f2528
 #define key_ecdsa_key_to_nid	sshkey_ecdsa_key_to_nid
8f2528
 #define key_is_cert		sshkey_is_cert
8f2528
+#define key_is_private		sshkey_is_private
8f2528
 #define key_type_plain		sshkey_type_plain
8f2528
 #define key_curve_name_to_nid	sshkey_curve_name_to_nid
8f2528
 #define key_curve_nid_to_bits	sshkey_curve_nid_to_bits
8f2528
diff -up openssh-7.4p1/mac.c.audit openssh-7.4p1/mac.c
8f2528
--- openssh-7.4p1/mac.c.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/mac.c	2016-12-23 18:54:54.436080419 +0100
8f2528
@@ -249,6 +249,20 @@ mac_clear(struct sshmac *mac)
8f2528
 	mac->umac_ctx = NULL;
8f2528
 }
8f2528
 
8f2528
+void
8f2528
+mac_destroy(struct sshmac *mac)
8f2528
+{
8f2528
+	if (mac == NULL)
8f2528
+		return;
8f2528
+
8f2528
+	if (mac->key) {
8f2528
+		memset(mac->key, 0, mac->key_len);
8f2528
+		free(mac->key);
8f2528
+	}
8f2528
+
8f2528
+	memset(mac, 0, sizeof(*mac));
8f2528
+}
8f2528
+
8f2528
 /* XXX copied from ciphers_valid */
8f2528
 #define	MAC_SEP	","
8f2528
 int
8f2528
diff -up openssh-7.4p1/mac.h.audit openssh-7.4p1/mac.h
8f2528
--- openssh-7.4p1/mac.h.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/mac.h	2016-12-23 18:54:54.436080419 +0100
8f2528
@@ -49,5 +49,6 @@ int	 mac_compute(struct sshmac *, u_int3
8f2528
 int	 mac_check(struct sshmac *, u_int32_t, const u_char *, size_t,
8f2528
     const u_char *, size_t);
8f2528
 void	 mac_clear(struct sshmac *);
8f2528
+void	 mac_destroy(struct sshmac *);
8f2528
 
8f2528
 #endif /* SSHMAC_H */
8f2528
diff -up openssh-7.4p1/Makefile.in.audit openssh-7.4p1/Makefile.in
8f2528
--- openssh-7.4p1/Makefile.in.audit	2016-12-23 18:54:54.375080404 +0100
8f2528
+++ openssh-7.4p1/Makefile.in	2016-12-23 18:54:54.436080419 +0100
8f2528
@@ -100,7 +100,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
8f2528
 	kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
8f2528
 	kexdhc.o kexgexc.o kexecdhc.o kexc25519c.o \
8f2528
 	kexdhs.o kexgexs.o kexecdhs.o kexc25519s.o \
8f2528
-	platform-pledge.o platform-tracing.o
8f2528
+	platform-pledge.o platform-tracing.o auditstub.o
8f2528
 
8f2528
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
8f2528
 	sshconnect.o sshconnect1.o sshconnect2.o mux.o
8f2528
diff -up openssh-7.4p1/monitor.c.audit openssh-7.4p1/monitor.c
8f2528
--- openssh-7.4p1/monitor.c.audit	2016-12-23 18:54:54.423080416 +0100
8f2528
+++ openssh-7.4p1/monitor.c	2016-12-23 18:54:54.437080420 +0100
8f2528
@@ -102,6 +102,7 @@
8f2528
 #include "compat.h"
8f2528
 #include "ssh2.h"
8f2528
 #include "authfd.h"
8f2528
+#include "audit.h"
8f2528
 #include "match.h"
8f2528
 #include "ssherr.h"
8f2528
 
8f2528
@@ -117,6 +118,8 @@ extern Buffer auth_debug;
8f2528
 extern int auth_debug_init;
8f2528
 extern Buffer loginmsg;
8f2528
 
8f2528
+extern void destroy_sensitive_data(int);
8f2528
+
8f2528
 /* State exported from the child */
8f2528
 static struct sshbuf *child_state;
8f2528
 
8f2528
@@ -167,6 +170,11 @@ int mm_answer_gss_updatecreds(int, Buffe
8f2528
 #ifdef SSH_AUDIT_EVENTS
8f2528
 int mm_answer_audit_event(int, Buffer *);
8f2528
 int mm_answer_audit_command(int, Buffer *);
8f2528
+int mm_answer_audit_end_command(int, Buffer *);
8f2528
+int mm_answer_audit_unsupported_body(int, Buffer *);
8f2528
+int mm_answer_audit_kex_body(int, Buffer *);
8f2528
+int mm_answer_audit_session_key_free_body(int, Buffer *);
8f2528
+int mm_answer_audit_server_key_free(int, Buffer *);
8f2528
 #endif
8f2528
 
8f2528
 static int monitor_read_log(struct monitor *);
8f2528
@@ -222,6 +230,10 @@ struct mon_table mon_dispatch_proto20[]
8f2528
 #endif
8f2528
 #ifdef SSH_AUDIT_EVENTS
8f2528
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
8f2528
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
8f2528
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
8f2528
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
8f2528
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
8f2528
 #endif
8f2528
 #ifdef BSD_AUTH
8f2528
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
8f2528
@@ -260,6 +272,11 @@ struct mon_table mon_dispatch_postauth20
8f2528
 #ifdef SSH_AUDIT_EVENTS
8f2528
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
8f2528
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
8f2528
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
8f2528
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
8f2528
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
8f2528
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
8f2528
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
8f2528
 #endif
8f2528
     {0, 0, NULL}
8f2528
 };
8f2528
@@ -1396,9 +1413,11 @@ mm_answer_keyverify(int sock, Buffer *m)
8f2528
 	Key *key;
8f2528
 	u_char *signature, *data, *blob;
8f2528
 	u_int signaturelen, datalen, bloblen;
8f2528
+	int type = 0;
8f2528
 	int verified = 0;
8f2528
 	int valid_data = 0;
8f2528
 
8f2528
+	type = buffer_get_int(m);
8f2528
 	blob = buffer_get_string(m, &bloblen);
8f2528
 	signature = buffer_get_string(m, &signaturelen);
8f2528
 	data = buffer_get_string(m, &datalen);
8f2528
@@ -1406,6 +1425,8 @@ mm_answer_keyverify(int sock, Buffer *m)
8f2528
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
8f2528
 	  !monitor_allowed_key(blob, bloblen))
8f2528
 		fatal("%s: bad key, not previously allowed", __func__);
8f2528
+	if (type != key_blobtype)
8f2528
+		fatal("%s: bad key type", __func__);
8f2528
 
8f2528
 	key = key_from_blob(blob, bloblen);
8f2528
 	if (key == NULL)
8f2528
@@ -1426,7 +1447,17 @@ mm_answer_keyverify(int sock, Buffer *m)
8f2528
 	if (!valid_data)
8f2528
 		fatal("%s: bad signature data blob", __func__);
8f2528
 
8f2528
-	verified = key_verify(key, signature, signaturelen, data, datalen);
8f2528
+	switch (key_blobtype) {
8f2528
+	case MM_USERKEY:
8f2528
+		verified = user_key_verify(key, signature, signaturelen, data, datalen);
8f2528
+		break;
8f2528
+	case MM_HOSTKEY:
8f2528
+		verified = hostbased_key_verify(key, signature, signaturelen, data, datalen);
8f2528
+		break;
8f2528
+	default:
8f2528
+		verified = 0;
8f2528
+		break;
8f2528
+	}
8f2528
 	debug3("%s: key %p signature %s",
8f2528
 	    __func__, key, (verified == 1) ? "verified" : "unverified");
8f2528
 
8f2528
@@ -1489,6 +1520,12 @@ mm_session_close(Session *s)
8f2528
 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
8f2528
 		session_pty_cleanup2(s);
8f2528
 	}
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	if (s->command != NULL) {
8f2528
+		debug3("%s: command %d", __func__, s->command_handle);
8f2528
+		session_end_command2(s);
8f2528
+	}
8f2528
+#endif
8f2528
 	session_unused(s->self);
8f2528
 }
8f2528
 
8f2528
@@ -1591,6 +1628,8 @@ mm_answer_term(int sock, Buffer *req)
8f2528
 		sshpam_cleanup();
8f2528
 #endif
8f2528
 
8f2528
+	destroy_sensitive_data(0);
8f2528
+
8f2528
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
8f2528
 		if (errno != EINTR)
8f2528
 			exit(1);
8f2528
@@ -1633,11 +1672,45 @@ mm_answer_audit_command(int socket, Buff
8f2528
 {
8f2528
 	u_int len;
8f2528
 	char *cmd;
8f2528
+	Session *s;
8f2528
 
8f2528
 	debug3("%s entering", __func__);
8f2528
 	cmd = buffer_get_string(m, &len;;
8f2528
+
8f2528
 	/* sanity check command, if so how? */
8f2528
-	audit_run_command(cmd);
8f2528
+	s = session_new();
8f2528
+	if (s == NULL)
8f2528
+		fatal("%s: error allocating a session", __func__);
8f2528
+	s->command = cmd;
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	s->command_handle = audit_run_command(cmd);
8f2528
+#endif
8f2528
+
8f2528
+	buffer_clear(m);
8f2528
+	buffer_put_int(m, s->self);
8f2528
+
8f2528
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
8f2528
+
8f2528
+	return (0);
8f2528
+}
8f2528
+
8f2528
+int
8f2528
+mm_answer_audit_end_command(int socket, Buffer *m)
8f2528
+{
8f2528
+	int handle;
8f2528
+	u_int len;
8f2528
+	char *cmd;
8f2528
+	Session *s;
8f2528
+
8f2528
+	debug3("%s entering", __func__);
8f2528
+	handle = buffer_get_int(m);
8f2528
+	cmd = buffer_get_string(m, &len;;
8f2528
+
8f2528
+	s = session_by_id(handle);
8f2528
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
8f2528
+	    strcmp(s->command, cmd) != 0)
8f2528
+		fatal("%s: invalid handle", __func__);
8f2528
+	mm_session_close(s);
8f2528
 	free(cmd);
8f2528
 	return (0);
8f2528
 }
8f2528
@@ -1690,6 +1763,7 @@ monitor_apply_keystate(struct monitor *p
8f2528
 void
8f2528
 mm_get_keystate(struct monitor *pmonitor)
8f2528
 {
8f2528
+	Buffer m;
8f2528
 	debug3("%s: Waiting for new keys", __func__);
8f2528
 
8f2528
 	if ((child_state = sshbuf_new()) == NULL)
8f2528
@@ -1697,6 +1771,21 @@ mm_get_keystate(struct monitor *pmonitor
8f2528
 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
8f2528
 	    child_state);
8f2528
 	debug3("%s: GOT new keys", __func__);
8f2528
+
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	if (compat20) {
8f2528
+		buffer_init(&m);
8f2528
+		mm_request_receive_expect(pmonitor->m_sendfd,
8f2528
+					  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
8f2528
+		mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, &m);
8f2528
+		buffer_free(&m);
8f2528
+	}
8f2528
+#endif
8f2528
+
8f2528
+	/* Drain any buffered messages from the child */
8f2528
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
8f2528
+		;
8f2528
+
8f2528
 }
8f2528
 
8f2528
 
8f2528
@@ -1953,3 +2042,86 @@ mm_answer_gss_updatecreds(int socket, Bu
8f2528
 
8f2528
 #endif /* GSSAPI */
8f2528
 
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+int
8f2528
+mm_answer_audit_unsupported_body(int sock, Buffer *m)
8f2528
+{
8f2528
+	int what;
8f2528
+
8f2528
+	what = buffer_get_int(m);
8f2528
+
8f2528
+	audit_unsupported_body(what);
8f2528
+
8f2528
+	buffer_clear(m);
8f2528
+
8f2528
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
8f2528
+	return 0;
8f2528
+}
8f2528
+
8f2528
+int
8f2528
+mm_answer_audit_kex_body(int sock, Buffer *m)
8f2528
+{
8f2528
+	int ctos, len;
8f2528
+	char *cipher, *mac, *compress, *pfs;
8f2528
+	pid_t pid;
8f2528
+	uid_t uid;
8f2528
+
8f2528
+	ctos = buffer_get_int(m);
8f2528
+	cipher = buffer_get_string(m, &len;;
8f2528
+	mac = buffer_get_string(m, &len;;
8f2528
+	compress = buffer_get_string(m, &len;;
8f2528
+	pfs = buffer_get_string(m, &len;;
8f2528
+	pid = buffer_get_int64(m);
8f2528
+	uid = buffer_get_int64(m);
8f2528
+
8f2528
+	audit_kex_body(ctos, cipher, mac, compress, pfs, pid, uid);
8f2528
+
8f2528
+	free(cipher);
8f2528
+	free(mac);
8f2528
+	free(compress);
8f2528
+	free(pfs);
8f2528
+	buffer_clear(m);
8f2528
+
8f2528
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
8f2528
+	return 0;
8f2528
+}
8f2528
+
8f2528
+int
8f2528
+mm_answer_audit_session_key_free_body(int sock, Buffer *m)
8f2528
+{
8f2528
+	int ctos;
8f2528
+	pid_t pid;
8f2528
+	uid_t uid;
8f2528
+
8f2528
+	ctos = buffer_get_int(m);
8f2528
+	pid = buffer_get_int64(m);
8f2528
+	uid = buffer_get_int64(m);
8f2528
+
8f2528
+	audit_session_key_free_body(ctos, pid, uid);
8f2528
+
8f2528
+	buffer_clear(m);
8f2528
+
8f2528
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
8f2528
+	return 0;
8f2528
+}
8f2528
+
8f2528
+int
8f2528
+mm_answer_audit_server_key_free(int sock, Buffer *m)
8f2528
+{
8f2528
+	int len;
8f2528
+	char *fp;
8f2528
+	pid_t pid;
8f2528
+	uid_t uid;
8f2528
+
8f2528
+	fp = buffer_get_string(m, &len;;
8f2528
+	pid = buffer_get_int64(m);
8f2528
+	uid = buffer_get_int64(m);
8f2528
+
8f2528
+	audit_destroy_sensitive_data(fp, pid, uid);
8f2528
+
8f2528
+	free(fp);
8f2528
+	buffer_clear(m);
8f2528
+
8f2528
+	return 0;
8f2528
+}
8f2528
+#endif /* SSH_AUDIT_EVENTS */
8f2528
diff -up openssh-7.4p1/monitor.h.audit openssh-7.4p1/monitor.h
8f2528
--- openssh-7.4p1/monitor.h.audit	2016-12-23 18:54:54.393080409 +0100
8f2528
+++ openssh-7.4p1/monitor.h	2016-12-23 18:54:54.437080420 +0100
8f2528
@@ -69,7 +69,13 @@ enum monitor_reqtype {
8f2528
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
8f2528
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
8f2528
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
8f2528
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
8f2528
+	MONITOR_REQ_AUDIT_EVENT = 112,
8f2528
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
8f2528
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
8f2528
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
8f2528
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
8f2528
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
8f2528
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124
8f2528
 
8f2528
 };
8f2528
 
8f2528
diff -up openssh-7.4p1/monitor_wrap.c.audit openssh-7.4p1/monitor_wrap.c
8f2528
--- openssh-7.4p1/monitor_wrap.c.audit	2016-12-23 18:54:54.376080405 +0100
8f2528
+++ openssh-7.4p1/monitor_wrap.c	2016-12-23 18:54:54.437080420 +0100
8f2528
@@ -453,7 +453,7 @@ mm_key_allowed(enum mm_keytype type, con
8f2528
  */
8f2528
 
8f2528
 int
8f2528
-mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
8f2528
+mm_key_verify(enum mm_keytype type, Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
8f2528
 {
8f2528
 	Buffer m;
8f2528
 	u_char *blob;
8f2528
@@ -467,6 +467,7 @@ mm_key_verify(Key *key, u_char *sig, u_i
8f2528
 		return (0);
8f2528
 
8f2528
 	buffer_init(&m);
8f2528
+	buffer_put_int(&m, type);
8f2528
 	buffer_put_string(&m, blob, len);
8f2528
 	buffer_put_string(&m, sig, siglen);
8f2528
 	buffer_put_string(&m, data, datalen);
8f2528
@@ -484,6 +485,18 @@ mm_key_verify(Key *key, u_char *sig, u_i
8f2528
 	return (verified);
8f2528
 }
8f2528
 
8f2528
+int
8f2528
+mm_hostbased_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
8f2528
+{
8f2528
+	return mm_key_verify(MM_HOSTKEY, key, sig, siglen, data, datalen);
8f2528
+}
8f2528
+
8f2528
+int
8f2528
+mm_user_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
8f2528
+{
8f2528
+	return mm_key_verify(MM_USERKEY, key, sig, siglen, data, datalen);
8f2528
+}
8f2528
+
8f2528
 void
8f2528
 mm_send_keystate(struct monitor *monitor)
8f2528
 {
8f2528
@@ -861,10 +874,11 @@ mm_audit_event(ssh_audit_event_t event)
8f2528
 	buffer_free(&m);
8f2528
 }
8f2528
 
8f2528
-void
8f2528
+int
8f2528
 mm_audit_run_command(const char *command)
8f2528
 {
8f2528
 	Buffer m;
8f2528
+	int handle;
8f2528
 
8f2528
 	debug3("%s entering command %s", __func__, command);
8f2528
 
8f2528
@@ -872,6 +886,26 @@ mm_audit_run_command(const char *command
8f2528
 	buffer_put_cstring(&m, command);
8f2528
 
8f2528
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
8f2528
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, &m);
8f2528
+
8f2528
+	handle = buffer_get_int(&m);
8f2528
+	buffer_free(&m);
8f2528
+
8f2528
+	return (handle);
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+mm_audit_end_command(int handle, const char *command)
8f2528
+{
8f2528
+	Buffer m;
8f2528
+
8f2528
+	debug3("%s entering command %s", __func__, command);
8f2528
+
8f2528
+	buffer_init(&m);
8f2528
+	buffer_put_int(&m, handle);
8f2528
+	buffer_put_cstring(&m, command);
8f2528
+
8f2528
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, &m);
8f2528
 	buffer_free(&m);
8f2528
 }
8f2528
 #endif /* SSH_AUDIT_EVENTS */
8f2528
@@ -1007,3 +1041,70 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_cc
8f2528
 
8f2528
 #endif /* GSSAPI */
8f2528
 
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+void
8f2528
+mm_audit_unsupported_body(int what)
8f2528
+{
8f2528
+	Buffer m;
8f2528
+
8f2528
+	buffer_init(&m);
8f2528
+	buffer_put_int(&m, what);
8f2528
+
8f2528
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, &m);
8f2528
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
8f2528
+				  &m);
8f2528
+
8f2528
+	buffer_free(&m);
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+mm_audit_kex_body(int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
8f2528
+		  uid_t uid)
8f2528
+{
8f2528
+	Buffer m;
8f2528
+
8f2528
+	buffer_init(&m);
8f2528
+	buffer_put_int(&m, ctos);
8f2528
+	buffer_put_cstring(&m, cipher);
8f2528
+	buffer_put_cstring(&m, (mac ? mac : "<implicit>"));
8f2528
+	buffer_put_cstring(&m, compress);
8f2528
+	buffer_put_cstring(&m, fps);
8f2528
+	buffer_put_int64(&m, pid);
8f2528
+	buffer_put_int64(&m, uid);
8f2528
+
8f2528
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, &m);
8f2528
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
8f2528
+				  &m);
8f2528
+
8f2528
+	buffer_free(&m);
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+	Buffer m;
8f2528
+
8f2528
+	buffer_init(&m);
8f2528
+	buffer_put_int(&m, ctos);
8f2528
+	buffer_put_int64(&m, pid);
8f2528
+	buffer_put_int64(&m, uid);
8f2528
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
8f2528
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
8f2528
+				  &m);
8f2528
+	buffer_free(&m);
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
8f2528
+{
8f2528
+	Buffer m;
8f2528
+
8f2528
+	buffer_init(&m);
8f2528
+	buffer_put_cstring(&m, fp);
8f2528
+	buffer_put_int64(&m, pid);
8f2528
+	buffer_put_int64(&m, uid);
8f2528
+
8f2528
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
8f2528
+	buffer_free(&m);
8f2528
+}
8f2528
+#endif /* SSH_AUDIT_EVENTS */
8f2528
diff -up openssh-7.4p1/monitor_wrap.h.audit openssh-7.4p1/monitor_wrap.h
8f2528
--- openssh-7.4p1/monitor_wrap.h.audit	2016-12-23 18:54:54.376080405 +0100
8f2528
+++ openssh-7.4p1/monitor_wrap.h	2016-12-23 18:54:54.437080420 +0100
8f2528
@@ -52,7 +52,8 @@ int mm_key_allowed(enum mm_keytype, cons
8f2528
 int mm_user_key_allowed(struct passwd *, Key *, int);
8f2528
 int mm_hostbased_key_allowed(struct passwd *, const char *,
8f2528
     const char *, Key *);
8f2528
-int mm_key_verify(Key *, u_char *, u_int, u_char *, u_int);
8f2528
+int mm_hostbased_key_verify(Key *, u_char *, u_int, u_char *, u_int);
8f2528
+int mm_user_key_verify(Key *, u_char *, u_int, u_char *, u_int);
8f2528
 
8f2528
 #ifdef GSSAPI
8f2528
 OM_uint32 mm_ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
8f2528
@@ -76,7 +77,12 @@ void mm_sshpam_free_ctx(void *);
8f2528
 #ifdef SSH_AUDIT_EVENTS
8f2528
 #include "audit.h"
8f2528
 void mm_audit_event(ssh_audit_event_t);
8f2528
-void mm_audit_run_command(const char *);
8f2528
+int mm_audit_run_command(const char *);
8f2528
+void mm_audit_end_command(int, const char *);
8f2528
+void mm_audit_unsupported_body(int);
8f2528
+void mm_audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
8f2528
+void mm_audit_session_key_free_body(int, pid_t, uid_t);
8f2528
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
8f2528
 #endif
8f2528
 
8f2528
 struct Session;
8f2528
diff -up openssh-7.4p1/packet.c.audit openssh-7.4p1/packet.c
8f2528
--- openssh-7.4p1/packet.c.audit	2016-12-23 18:54:54.318080390 +0100
8f2528
+++ openssh-7.4p1/packet.c	2016-12-23 18:54:54.438080420 +0100
8f2528
@@ -67,6 +67,7 @@
8f2528
 #include "key.h"	/* typedefs XXX */
8f2528
 
8f2528
 #include "xmalloc.h"
8f2528
+#include "audit.h"
8f2528
 #include "crc32.h"
8f2528
 #include "deattack.h"
8f2528
 #include "compat.h"
8f2528
@@ -494,6 +495,13 @@ ssh_packet_get_connection_out(struct ssh
8f2528
 	return ssh->state->connection_out;
8f2528
 }
8f2528
 
8f2528
+static int
8f2528
+packet_state_has_keys (const struct session_state *state)
8f2528
+{
8f2528
+	return state != NULL &&
8f2528
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
8f2528
+}
8f2528
+
8f2528
 /*
8f2528
  * Returns the IP-address of the remote host as a string.  The returned
8f2528
  * string must not be freed.
8f2528
@@ -510,11 +510,12 @@
8f2528
 const char *
8f2528
 ssh_remote_ipaddr(struct ssh *ssh)
8f2528
 {
8f2528
-	const int sock = ssh->state->connection_in;
8f2528
+	int sock;
8f2528
 
8f2528
 	/* Check whether we have cached the ipaddr. */
8f2528
 	if (ssh->remote_ipaddr == NULL) {
8f2528
 		if (ssh_packet_connection_is_on_socket(ssh)) {
8f2528
+			sock = ssh->state->connection_in;
8f2528
 			ssh->remote_ipaddr = get_peer_ipaddr(sock);
8f2528
 			ssh->remote_port = get_peer_port(sock);
8f2528
 			ssh->local_ipaddr = get_local_ipaddr(sock);
8f2528
@@ -562,13 +570,6 @@ ssh_packet_close(struct ssh *ssh)
8f2528
 	if (!state->initialized)
8f2528
 		return;
8f2528
 	state->initialized = 0;
8f2528
-	if (state->connection_in == state->connection_out) {
8f2528
-		shutdown(state->connection_out, SHUT_RDWR);
8f2528
-		close(state->connection_out);
8f2528
-	} else {
8f2528
-		close(state->connection_in);
8f2528
-		close(state->connection_out);
8f2528
-	}
8f2528
 	sshbuf_free(state->input);
8f2528
 	sshbuf_free(state->output);
8f2528
 	sshbuf_free(state->outgoing_packet);
8f2528
@@ -600,11 +601,21 @@ ssh_packet_close(struct ssh *ssh)
8f2528
 				inflateEnd(stream);
8f2528
 		}
8f2528
 	}
8f2528
-	cipher_free(state->send_context);
8f2528
-	cipher_free(state->receive_context);
8f2528
+	if (packet_state_has_keys(state)) {
8f2528
+		cipher_free(state->send_context);
8f2528
+		cipher_free(state->receive_context);
8f2528
+		audit_session_key_free(MODE_MAX);
8f2528
+	}
8f2528
 	state->send_context = state->receive_context = NULL;
8f2528
 	free(ssh->remote_ipaddr);
8f2528
 	ssh->remote_ipaddr = NULL;
8f2528
+	if (state->connection_in == state->connection_out) {
8f2528
+		shutdown(state->connection_out, SHUT_RDWR);
8f2528
+		close(state->connection_out);
8f2528
+	} else {
8f2528
+		close(state->connection_in);
8f2528
+		close(state->connection_out);
8f2528
+	}
8f2528
 	free(ssh->state);
8f2528
 	ssh->state = NULL;
8f2528
 }
8f2528
@@ -950,6 +961,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
8f2528
 		    " (%llu bytes total)", __func__,
8f2528
 		    (unsigned long long)ps->blocks, dir,
8f2528
 		    (unsigned long long)ps->bytes);
8f2528
+		audit_session_key_free(mode);
8f2528
 		cipher_free(*ccp);
8f2528
 		*ccp = NULL;
8f2528
 		enc  = &state->newkeys[mode]->enc;
8f2528
@@ -2440,6 +2452,72 @@ ssh_packet_get_output(struct ssh *ssh)
8f2528
 	return (void *)ssh->state->output;
8f2528
 }
8f2528
 
8f2528
+static void
8f2528
+newkeys_destroy_and_free(struct newkeys *newkeys)
8f2528
+{
8f2528
+	if (newkeys == NULL)
8f2528
+		return;
8f2528
+
8f2528
+	free(newkeys->enc.name);
8f2528
+
8f2528
+	if (newkeys->mac.enabled) {
8f2528
+		mac_clear(&newkeys->mac);
8f2528
+		free(newkeys->mac.name);
8f2528
+	}
8f2528
+
8f2528
+	free(newkeys->comp.name);
8f2528
+
8f2528
+	newkeys_destroy(newkeys);
8f2528
+	free(newkeys);
8f2528
+}
8f2528
+
8f2528
+static void
8f2528
+packet_destroy_state(struct session_state *state)
8f2528
+{
8f2528
+	if (state == NULL)
8f2528
+		return;
8f2528
+
8f2528
+	cipher_free(state->receive_context);
8f2528
+	cipher_free(state->send_context);
8f2528
+
8f2528
+	buffer_free(state->input);
8f2528
+	state->input = NULL;
8f2528
+	buffer_free(state->output);
8f2528
+	state->output = NULL;
8f2528
+	buffer_free(state->outgoing_packet);
8f2528
+	state->outgoing_packet = NULL;
8f2528
+	buffer_free(state->incoming_packet);
8f2528
+	state->incoming_packet = NULL;
8f2528
+	if( state->compression_buffer ) {
8f2528
+		buffer_free(state->compression_buffer);
8f2528
+		state->compression_buffer = NULL;
8f2528
+	}
8f2528
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
8f2528
+	state->newkeys[MODE_IN] = NULL;
8f2528
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
8f2528
+	state->newkeys[MODE_OUT] = NULL;
8f2528
+	mac_destroy(state->packet_discard_mac);
8f2528
+//	TAILQ_HEAD(, packet) outgoing;
8f2528
+//	memset(state, 0, sizeof(state));
8f2528
+}
8f2528
+
8f2528
+void
8f2528
+packet_destroy_all(int audit_it, int privsep)
8f2528
+{
8f2528
+	if (audit_it)
8f2528
+		audit_it = (active_state != NULL && packet_state_has_keys(active_state->state));
8f2528
+	if (active_state != NULL)
8f2528
+		packet_destroy_state(active_state->state);
8f2528
+	if (audit_it) {
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+		if (privsep)
8f2528
+			audit_session_key_free(MODE_MAX);
8f2528
+		else
8f2528
+			audit_session_key_free_body(MODE_MAX, getpid(), getuid());
8f2528
+#endif
8f2528
+	}
8f2528
+}
8f2528
+
8f2528
 /* Reset after_authentication and reset compression in post-auth privsep */
8f2528
 static int
8f2528
 ssh_packet_set_postauth(struct ssh *ssh)
8f2528
diff -up openssh-7.4p1/packet.h.audit openssh-7.4p1/packet.h
8f2528
--- openssh-7.4p1/packet.h.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/packet.h	2016-12-23 18:54:54.438080420 +0100
8f2528
@@ -208,4 +208,5 @@ extern struct ssh *active_state;
8f2528
 # undef EC_POINT
8f2528
 #endif
8f2528
 
8f2528
+void	 packet_destroy_all(int, int);
8f2528
 #endif				/* PACKET_H */
8f2528
diff -up openssh-7.4p1/session.c.audit openssh-7.4p1/session.c
8f2528
--- openssh-7.4p1/session.c.audit	2016-12-23 18:54:54.430080418 +0100
8f2528
+++ openssh-7.4p1/session.c	2016-12-23 18:57:45.068115466 +0100
8f2528
@@ -142,7 +142,7 @@ extern int log_stderr;
8f2528
 extern int debug_flag;
8f2528
 extern u_int utmp_len;
8f2528
 extern int startup_pipe;
8f2528
-extern void destroy_sensitive_data(void);
8f2528
+extern void destroy_sensitive_data(int);
8f2528
 extern Buffer loginmsg;
8f2528
 
8f2528
 /* original command from peer. */
8f2528
@@ -576,6 +576,14 @@ do_exec_pty(Session *s, const char *comm
8f2528
 	/* Parent.  Close the slave side of the pseudo tty. */
8f2528
 	close(ttyfd);
8f2528
 
8f2528
+#if !defined(HAVE_OSF_SIA) && defined(SSH_AUDIT_EVENTS)
8f2528
+	/* do_login in the child did not affect state in this process,
8f2528
+	   compensate.  From an architectural standpoint, this is extremely
8f2528
+	   ugly. */
8f2528
+	if (command != NULL)
8f2528
+		audit_count_session_open();
8f2528
+#endif
8f2528
+
8f2528
 	/* Enter interactive session. */
8f2528
 	s->ptymaster = ptymaster;
8f2528
 	packet_set_interactive(1, 
8f2528
@@ -696,15 +704,19 @@ do_exec(Session *s, const char *command)
8f2528
 	    s->self);
8f2528
 
8f2528
 #ifdef SSH_AUDIT_EVENTS
8f2528
+	if (s->command != NULL || s->command_handle != -1)
8f2528
+		fatal("do_exec: command already set");
8f2528
 	if (command != NULL)
8f2528
-		PRIVSEP(audit_run_command(command));
8f2528
+		s->command = xstrdup(command);
8f2528
 	else if (s->ttyfd == -1) {
8f2528
 		char *shell = s->pw->pw_shell;
8f2528
 
8f2528
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
8f2528
 			shell =_PATH_BSHELL;
8f2528
-		PRIVSEP(audit_run_command(shell));
8f2528
+		s->command = xstrdup(shell);
8f2528
 	}
8f2528
+	if (s->command != NULL && s->ptyfd == -1)
8f2528
+		s->command_handle = PRIVSEP(audit_run_command(s->command));
8f2528
 #endif
8f2528
 	if (s->ttyfd != -1)
8f2528
 		ret = do_exec_pty(s, command);
8f2528
@@ -1543,7 +1555,10 @@ do_child(Session *s, const char *command
8f2528
 	int r = 0;
8f2528
 
8f2528
 	/* remove hostkey from the child's memory */
8f2528
-	destroy_sensitive_data();
8f2528
+	destroy_sensitive_data(1);
8f2528
+	/* Don't audit this - both us and the parent would be talking to the
8f2528
+	   monitor over a single socket, with no synchronization. */
8f2528
+	packet_destroy_all(0, 1);
8f2528
 
8f2528
 	/* Force a password change */
8f2528
 	if (s->authctxt->force_pwchange) {
8f2528
@@ -1757,6 +1772,9 @@ session_unused(int id)
8f2528
 	sessions[id].ttyfd = -1;
8f2528
 	sessions[id].ptymaster = -1;
8f2528
 	sessions[id].x11_chanids = NULL;
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	sessions[id].command_handle = -1;
8f2528
+#endif
8f2528
 	sessions[id].next_unused = sessions_first_unused;
8f2528
 	sessions_first_unused = id;
8f2528
 }
8f2528
@@ -1839,6 +1857,19 @@ session_open(Authctxt *authctxt, int cha
8f2528
 }
8f2528
 
8f2528
 Session *
8f2528
+session_by_id(int id)
8f2528
+{
8f2528
+	if (id >= 0 && id < sessions_nalloc) {
8f2528
+		Session *s = &sessions[id];
8f2528
+		if (s->used)
8f2528
+			return s;
8f2528
+	}
8f2528
+	debug("%s: unknown id %d", __func__, id);
8f2528
+	session_dump();
8f2528
+	return NULL;
8f2528
+}
8f2528
+
8f2528
+Session *
8f2528
 session_by_tty(char *tty)
8f2528
 {
8f2528
 	int i;
8f2528
@@ -2351,6 +2382,32 @@ session_exit_message(Session *s, int sta
8f2528
 		chan_write_failed(c);
8f2528
 }
8f2528
 
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+void
8f2528
+session_end_command2(Session *s)
8f2528
+{
8f2528
+	if (s->command != NULL) {
8f2528
+		if (s->command_handle != -1)
8f2528
+			audit_end_command(s->command_handle, s->command);
8f2528
+		free(s->command);
8f2528
+		s->command = NULL;
8f2528
+		s->command_handle = -1;
8f2528
+	}
8f2528
+}
8f2528
+
8f2528
+static void
8f2528
+session_end_command(Session *s)
8f2528
+{
8f2528
+	if (s->command != NULL) {
8f2528
+		if (s->command_handle != -1)
8f2528
+			PRIVSEP(audit_end_command(s->command_handle, s->command));
8f2528
+		free(s->command);
8f2528
+		s->command = NULL;
8f2528
+		s->command_handle = -1;
8f2528
+	}
8f2528
+}
8f2528
+#endif
8f2528
+
8f2528
 void
8f2528
 session_close(Session *s)
8f2528
 {
8f2528
@@ -2365,6 +2422,10 @@ session_close(Session *s)
8f2528
 
8f2528
 	if (s->ttyfd != -1)
8f2528
 		session_pty_cleanup(s);
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	if (s->command)
8f2528
+		session_end_command(s);
8f2528
+#endif
8f2528
 	free(s->term);
8f2528
 	free(s->display);
8f2528
 	free(s->x11_chanids);
8f2528
@@ -2575,6 +2636,15 @@ do_authenticated2(Authctxt *authctxt)
8f2528
 	server_loop2(authctxt);
8f2528
 }
8f2528
 
8f2528
+static void
8f2528
+do_cleanup_one_session(Session *s)
8f2528
+{
8f2528
+	session_pty_cleanup2(s);
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	session_end_command2(s);
8f2528
+#endif
8f2528
+}
8f2528
+
8f2528
 void
8f2528
 do_cleanup(Authctxt *authctxt)
8f2528
 {
8f2528
@@ -2626,7 +2696,7 @@ do_cleanup(Authctxt *authctxt)
8f2528
 	 * or if running in monitor.
8f2528
 	 */
8f2528
 	if (!use_privsep || mm_is_monitor())
8f2528
-		session_destroy_all(session_pty_cleanup2);
8f2528
+		session_destroy_all(do_cleanup_one_session);
8f2528
 }
8f2528
 
8f2528
 /* Return a name for the remote host that fits inside utmp_size */
8f2528
diff -up openssh-7.4p1/session.h.audit openssh-7.4p1/session.h
8f2528
--- openssh-7.4p1/session.h.audit	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/session.h	2016-12-23 18:54:54.438080420 +0100
8f2528
@@ -60,6 +60,12 @@ struct Session {
8f2528
 		char	*name;
8f2528
 		char	*val;
8f2528
 	} *env;
8f2528
+
8f2528
+	/* exec */
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	int	command_handle;
8f2528
+	char	*command;
8f2528
+#endif
8f2528
 };
8f2528
 
8f2528
 void	 do_authenticated(Authctxt *);
8f2528
@@ -72,8 +78,10 @@ void	 session_close_by_pid(pid_t, int);
8f2528
 void	 session_close_by_channel(int, void *);
8f2528
 void	 session_destroy_all(void (*)(Session *));
8f2528
 void	 session_pty_cleanup2(Session *);
8f2528
+void	 session_end_command2(Session *);
8f2528
 
8f2528
 Session	*session_new(void);
8f2528
+Session *session_by_id(int);
8f2528
 Session	*session_by_tty(char *);
8f2528
 void	 session_close(Session *);
8f2528
 void	 do_setusercontext(struct passwd *);
8f2528
diff -up openssh-7.4p1/sshd.c.audit openssh-7.4p1/sshd.c
8f2528
--- openssh-7.4p1/sshd.c.audit	2016-12-23 18:54:54.403080411 +0100
8f2528
+++ openssh-7.4p1/sshd.c	2016-12-23 18:56:18.992101105 +0100
8f2528
@@ -119,6 +119,7 @@
8f2528
 #include "ssh-gss.h"
8f2528
 #endif
8f2528
 #include "monitor_wrap.h"
8f2528
+#include "audit.h"
8f2528
 #include "ssh-sandbox.h"
8f2528
 #include "version.h"
8f2528
 #include "ssherr.h"
8f2528
@@ -244,7 +245,7 @@ Buffer loginmsg;
8f2528
 struct passwd *privsep_pw = NULL;
8f2528
 
8f2528
 /* Prototypes for various functions defined later in this file. */
8f2528
-void destroy_sensitive_data(void);
8f2528
+void destroy_sensitive_data(int);
8f2528
 void demote_sensitive_data(void);
8f2528
 static void do_ssh2_kex(void);
8f2528
 
8f2528
@@ -261,6 +262,15 @@ close_listen_socks(void)
8f2528
 	num_listen_socks = -1;
8f2528
 }
8f2528
 
8f2528
+/*
8f2528
+ * Is this process listening for clients (i.e. not specific to any specific
8f2528
+ * client connection?)
8f2528
+ */
8f2528
+int listening_for_clients(void)
8f2528
+{
8f2528
+	return num_listen_socks >= 0;
8f2528
+}
8f2528
+
8f2528
 static void
8f2528
 close_startup_pipes(void)
8f2528
 {
8f2528
@@ -473,18 +483,45 @@ sshd_exchange_identification(struct ssh
8f2528
 	}
8f2528
 }
8f2528
 
8f2528
-/* Destroy the host and server keys.  They will no longer be needed. */
8f2528
+/*
8f2528
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
8f2528
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
8f2528
+ */
8f2528
 void
8f2528
-destroy_sensitive_data(void)
8f2528
+destroy_sensitive_data(int privsep)
8f2528
 {
8f2528
 	int i;
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	pid_t pid;
8f2528
+	uid_t uid;
8f2528
 
8f2528
+	pid = getpid();
8f2528
+	uid = getuid();
8f2528
+#endif
8f2528
 	for (i = 0; i < options.num_host_key_files; i++) {
8f2528
 		if (sensitive_data.host_keys[i]) {
8f2528
+			char *fp;
8f2528
+
8f2528
+			if (key_is_private(sensitive_data.host_keys[i]))
8f2528
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
8f2528
+			else
8f2528
+				fp = NULL;
8f2528
 			key_free(sensitive_data.host_keys[i]);
8f2528
 			sensitive_data.host_keys[i] = NULL;
8f2528
+			if (fp != NULL) {
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+				if (privsep)
8f2528
+					PRIVSEP(audit_destroy_sensitive_data(fp,
8f2528
+						pid, uid));
8f2528
+				else
8f2528
+					audit_destroy_sensitive_data(fp,
8f2528
+						pid, uid);
8f2528
+#endif
8f2528
+				free(fp);
8f2528
+			}
8f2528
 		}
8f2528
-		if (sensitive_data.host_certificates[i]) {
8f2528
+		if (sensitive_data.host_certificates
8f2528
+		    && sensitive_data.host_certificates[i]) {
8f2528
 			key_free(sensitive_data.host_certificates[i]);
8f2528
 			sensitive_data.host_certificates[i] = NULL;
8f2528
 		}
8f2528
@@ -497,12 +534,30 @@ demote_sensitive_data(void)
8f2528
 {
8f2528
 	Key *tmp;
8f2528
 	int i;
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+	pid_t pid;
8f2528
+	uid_t uid;
8f2528
 
8f2528
+	pid = getpid();
8f2528
+	uid = getuid();
8f2528
+#endif
8f2528
 	for (i = 0; i < options.num_host_key_files; i++) {
8f2528
 		if (sensitive_data.host_keys[i]) {
8f2528
+			char *fp;
8f2528
+
8f2528
+			if (key_is_private(sensitive_data.host_keys[i]))
8f2528
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
8f2528
+			else
8f2528
+				fp = NULL;
8f2528
 			tmp = key_demote(sensitive_data.host_keys[i]);
8f2528
 			key_free(sensitive_data.host_keys[i]);
8f2528
 			sensitive_data.host_keys[i] = tmp;
8f2528
+			if (fp != NULL) {
8f2528
+#ifdef SSH_AUDIT_EVENTS
8f2528
+				audit_destroy_sensitive_data(fp, pid, uid);
8f2528
+#endif
8f2528
+				free(fp);
8f2528
+			}
8f2528
 		}
8f2528
 		/* Certs do not need demotion */
8f2528
 	}
8f2528
@@ -585,7 +640,7 @@ privsep_preauth(Authctxt *authctxt)
8f2528
 
8f2528
 	if (use_privsep == PRIVSEP_ON)
8f2528
 		box = ssh_sandbox_init(pmonitor);
8f2528
-	pid = fork();
8f2528
+	pmonitor->m_pid = pid = fork();
8f2528
 	if (pid == -1) {
8f2528
 		fatal("fork of unprivileged child failed");
8f2528
 	} else if (pid != 0) {
8f2528
@@ -665,6 +720,12 @@ privsep_postauth(Authctxt *authctxt)
8f2528
 	else if (pmonitor->m_pid != 0) {
8f2528
 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
8f2528
 		buffer_clear(&loginmsg);
8f2528
+		if (*pmonitor->m_pkex != NULL ){
8f2528
+			newkeys_destroy((*pmonitor->m_pkex)->newkeys[MODE_OUT]);
8f2528
+			newkeys_destroy((*pmonitor->m_pkex)->newkeys[MODE_IN]);
8f2528
+			audit_session_key_free_body(2, getpid(), getuid());
8f2528
+			packet_destroy_all(0, 0);
8f2528
+		}
8f2528
 		monitor_child_postauth(pmonitor);
8f2528
 
8f2528
 		/* NEVERREACHED */
8f2528
@@ -1154,6 +1215,7 @@ server_accept_loop(int *sock_in, int *so
8f2528
 		if (received_sigterm) {
8f2528
 			logit("Received signal %d; terminating.",
8f2528
 			    (int) received_sigterm);
8f2528
+			destroy_sensitive_data(0);
8f2528
 			close_listen_socks();
8f2528
 			if (options.pid_file != NULL)
8f2528
 				unlink(options.pid_file);
8f2528
@@ -2092,6 +2150,7 @@ main(int ac, char **av)
8f2528
 	 */
8f2528
 	if (use_privsep) {
8f2528
 		mm_send_keystate(pmonitor);
8f2528
+		packet_destroy_all(1, 1);
8f2528
 		exit(0);
8f2528
 	}
8f2528
 
8f2528
@@ -2148,6 +2207,9 @@ main(int ac, char **av)
8f2528
 	do_authenticated(authctxt);
8f2528
 
8f2528
 	/* The connection has been terminated. */
8f2528
+	packet_destroy_all(1, 1);
8f2528
+	destroy_sensitive_data(1);
8f2528
+
8f2528
 	packet_get_bytes(&ibytes, &obytes);
8f2528
 	verbose("Transferred: sent %llu, received %llu bytes",
8f2528
 	    (unsigned long long)obytes, (unsigned long long)ibytes);
8f2528
@@ -2321,6 +2383,16 @@ do_ssh2_kex(void)
8f2528
 void
8f2528
 cleanup_exit(int i)
8f2528
 {
8f2528
+	static int in_cleanup = 0;
8f2528
+	int is_privsep_child;
8f2528
+
8f2528
+	/* cleanup_exit can be called at the very least from the privsep
8f2528
+	   wrappers used for auditing.  Make sure we don't recurse
8f2528
+	   indefinitely. */
8f2528
+	if (in_cleanup)
8f2528
+		_exit(i);
8f2528
+	in_cleanup = 1;
8f2528
+
8f2528
 	if (the_authctxt) {
8f2528
 		do_cleanup(the_authctxt);
8f2528
 		if (use_privsep && privsep_is_preauth &&
8f2528
@@ -2332,9 +2404,14 @@ cleanup_exit(int i)
8f2528
 				    pmonitor->m_pid, strerror(errno));
8f2528
 		}
8f2528
 	}
8f2528
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
8f2528
+	if (sensitive_data.host_keys != NULL)
8f2528
+		destroy_sensitive_data(is_privsep_child);
8f2528
+	packet_destroy_all(1, is_privsep_child);
8f2528
 #ifdef SSH_AUDIT_EVENTS
8f2528
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
8f2528
-	if (!use_privsep || mm_is_monitor())
8f2528
+	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&
8f2528
+	    (!use_privsep || mm_is_monitor()))
8f2528
 		audit_event(SSH_CONNECTION_ABANDON);
8f2528
 #endif
8f2528
 	_exit(i);
8f2528
diff -up openssh-7.4p1/sshkey.c.audit openssh-7.4p1/sshkey.c
8f2528
--- openssh-7.4p1/sshkey.c.audit	2016-12-23 18:54:54.425080417 +0100
8f2528
+++ openssh-7.4p1/sshkey.c	2016-12-23 18:54:54.439080420 +0100
8f2528
@@ -303,6 +303,33 @@ sshkey_type_is_valid_ca(int type)
8f2528
 }
8f2528
 
8f2528
 int
8f2528
+sshkey_is_private(const struct sshkey *k)
8f2528
+{
8f2528
+      switch (k->type) {
8f2528
+#ifdef WITH_OPENSSL
8f2528
+      case KEY_RSA_CERT:
8f2528
+      case KEY_RSA1:
8f2528
+      case KEY_RSA:
8f2528
+              return k->rsa->d != NULL;
8f2528
+      case KEY_DSA_CERT:
8f2528
+      case KEY_DSA:
8f2528
+              return k->dsa->priv_key != NULL;
8f2528
+#ifdef OPENSSL_HAS_ECC
8f2528
+      case KEY_ECDSA_CERT:
8f2528
+      case KEY_ECDSA:
8f2528
+              return EC_KEY_get0_private_key(k->ecdsa) != NULL;
8f2528
+#endif /* OPENSSL_HAS_ECC */
8f2528
+#endif /* WITH_OPENSSL */
8f2528
+      case KEY_ED25519_CERT:
8f2528
+      case KEY_ED25519:
8f2528
+              return (k->ed25519_pk != NULL);
8f2528
+      default:
8f2528
+              /* fatal("key_is_private: bad key type %d", k->type); */
8f2528
+              return 0;
8f2528
+      }
8f2528
+}
8f2528
+
8f2528
+int
8f2528
 sshkey_is_cert(const struct sshkey *k)
8f2528
 {
8f2528
 	if (k == NULL)
8f2528
diff -up openssh-7.4p1/sshkey.h.audit openssh-7.4p1/sshkey.h
8f2528
--- openssh-7.4p1/sshkey.h.audit	2016-12-23 18:54:54.425080417 +0100
8f2528
+++ openssh-7.4p1/sshkey.h	2016-12-23 18:54:54.439080420 +0100
8f2528
@@ -134,6 +134,7 @@ u_int		 sshkey_size(const struct sshkey
8f2528
 int		 sshkey_generate(int type, u_int bits, struct sshkey **keyp);
8f2528
 int		 sshkey_from_private(const struct sshkey *, struct sshkey **);
8f2528
 int	 sshkey_type_from_name(const char *);
8f2528
+int	 sshkey_is_private(const struct sshkey *);
8f2528
 int	 sshkey_is_cert(const struct sshkey *);
8f2528
 int	 sshkey_type_is_cert(int);
8f2528
 int	 sshkey_type_plain(int);