jonathancammack / rpms / openssh

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