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