jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone
9070b3
diff -up openssh-8.6p1/audit-bsm.c.audit openssh-8.6p1/audit-bsm.c
9070b3
--- openssh-8.6p1/audit-bsm.c.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/audit-bsm.c	2021-05-06 12:05:27.376464524 +0200
9070b3
@@ -373,13 +373,26 @@ audit_connection_from(const char *host,
9070b3
 #endif
9070b3
 }
9070b3
 
9070b3
+int
9070b3
+audit_run_command(struct ssh *ssh, const char *command)
9070b3
+{
9070b3
+	/* not implemented */
9070b3
+	return 0;
9070b3
+}
9070b3
+
9070b3
 void
9070b3
-audit_run_command(const char *command)
9070b3
+audit_end_command(struct ssh *ssh, int handle, const char *command)
9070b3
 {
9070b3
 	/* not implemented */
9070b3
 }
9070b3
 
9070b3
 void
9070b3
+audit_count_session_open(void)
9070b3
+{
9070b3
+	/* not necessary */
9070b3
+}
9070b3
+
9070b3
+void
9070b3
 audit_session_open(struct logininfo *li)
9070b3
 {
9070b3
 	/* not implemented */
9070b3
@@ -391,6 +404,12 @@ audit_session_close(struct logininfo *li
9070b3
 	/* not implemented */
9070b3
 }
9070b3
 
9070b3
+int
9070b3
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
9070b3
+{
9070b3
+	/* not implemented */
9070b3
+}
9070b3
+
9070b3
 void
9070b3
 audit_event(struct ssh *ssh, ssh_audit_event_t event)
9070b3
 {
9070b3
@@ -452,4 +471,28 @@ audit_event(struct ssh *ssh, ssh_audit_e
9070b3
 		debug("%s: unhandled event %d", __func__, event);
9070b3
 	}
9070b3
 }
9070b3
+
9070b3
+void
9070b3
+audit_unsupported_body(struct ssh *ssh, int what)
9070b3
+{
9070b3
+	/* not implemented */
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+	/* not implemented */
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_session_key_free_body(struct ssh * ssh, int ctos, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+	/* not implemented */
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+	/* not implemented */
9070b3
+}
9070b3
 #endif /* BSM */
9070b3
diff -up openssh-8.6p1/audit.c.audit openssh-8.6p1/audit.c
9070b3
--- openssh-8.6p1/audit.c.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/audit.c	2021-05-06 12:05:27.376464524 +0200
9070b3
@@ -34,6 +34,12 @@
9070b3
 #include "log.h"
9070b3
 #include "hostfile.h"
9070b3
 #include "auth.h"
9070b3
+#include "ssh-gss.h"
9070b3
+#include "monitor_wrap.h"
9070b3
+#include "xmalloc.h"
9070b3
+#include "misc.h"
9070b3
+#include "servconf.h"
9070b3
+#include "ssherr.h"
9070b3
 
9070b3
 /*
9070b3
  * Care must be taken when using this since it WILL NOT be initialized when
9070b3
@@ -41,6 +47,7 @@
9070b3
  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
9070b3
  */
9070b3
 extern Authctxt *the_authctxt;
9070b3
+extern ServerOptions options;
9070b3
 
9070b3
 /* Maybe add the audit class to struct Authmethod? */
9070b3
 ssh_audit_event_t
9070b3
@@ -69,13 +76,10 @@ audit_classify_auth(const char *method)
9070b3
 const char *
9070b3
 audit_username(void)
9070b3
 {
9070b3
-	static const char unknownuser[] = "(unknown user)";
9070b3
-	static const char invaliduser[] = "(invalid user)";
9070b3
+	static const char unknownuser[] = "(unknown)";
9070b3
 
9070b3
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
9070b3
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
9070b3
 		return (unknownuser);
9070b3
-	if (!the_authctxt->valid)
9070b3
-		return (invaliduser);
9070b3
 	return (the_authctxt->user);
9070b3
 }
9070b3
 
9070b3
@@ -109,6 +113,35 @@ audit_event_lookup(ssh_audit_event_t ev)
9070b3
 	return(event_lookup[i].name);
9070b3
 }
9070b3
 
9070b3
+void
9070b3
+audit_key(struct ssh *ssh, int host_user, int *rv, const struct sshkey *key)
9070b3
+{
9070b3
+	char *fp;
9070b3
+
9070b3
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
9070b3
+	if (audit_keyusage(ssh, host_user, fp, (*rv == 0)) == 0)
9070b3
+		*rv = -SSH_ERR_INTERNAL_ERROR;
9070b3
+	free(fp);
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_unsupported(struct ssh *ssh, int what)
9070b3
+{
9070b3
+	PRIVSEP(audit_unsupported_body(ssh, what));
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_kex(struct ssh *ssh, int ctos, char *enc, char *mac, char *comp, char *pfs)
9070b3
+{
9070b3
+	PRIVSEP(audit_kex_body(ssh, ctos, enc, mac, comp, pfs, getpid(), getuid()));
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_session_key_free(struct ssh *ssh, int ctos)
9070b3
+{
9070b3
+	PRIVSEP(audit_session_key_free_body(ssh, ctos, getpid(), getuid()));
9070b3
+}
9070b3
+
9070b3
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
9070b3
 /*
9070b3
  * Null implementations of audit functions.
9070b3
@@ -138,6 +171,17 @@ audit_event(struct ssh *ssh, ssh_audit_e
9070b3
 }
9070b3
 
9070b3
 /*
9070b3
+ * Called when a child process has called, or will soon call,
9070b3
+ * audit_session_open.
9070b3
+ */
9070b3
+void
9070b3
+audit_count_session_open(void)
9070b3
+{
9070b3
+	debug("audit count session open euid %d user %s", geteuid(),
9070b3
+	      audit_username());
9070b3
+}
9070b3
+
9070b3
+/*
9070b3
  * Called when a user session is started.  Argument is the tty allocated to
9070b3
  * the session, or NULL if no tty was allocated.
9070b3
  *
9070b3
@@ -172,13 +216,82 @@ audit_session_close(struct logininfo *li
9070b3
 /*
9070b3
  * This will be called when a user runs a non-interactive command.  Note that
9070b3
  * it may be called multiple times for a single connection since SSH2 allows
9070b3
- * multiple sessions within a single connection.
9070b3
+ * multiple sessions within a single connection.  Returns a "handle" for
9070b3
+ * audit_end_command.
9070b3
  */
9070b3
-void
9070b3
-audit_run_command(const char *command)
9070b3
+int
9070b3
+audit_run_command(struct ssh *ssh, const char *command)
9070b3
 {
9070b3
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
9070b3
 	    audit_username(), command);
9070b3
+	return 0;
9070b3
+}
9070b3
+
9070b3
+/*
9070b3
+ * This will be called when the non-interactive command finishes.  Note that
9070b3
+ * it may be called multiple times for a single connection since SSH2 allows
9070b3
+ * multiple sessions within a single connection.  "handle" should come from
9070b3
+ * the corresponding audit_run_command.
9070b3
+ */
9070b3
+void
9070b3
+audit_end_command(struct ssh *ssh, int handle, const char *command)
9070b3
+{
9070b3
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
9070b3
+	    audit_username(), command);
9070b3
+}
9070b3
+
9070b3
+/*
9070b3
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
9070b3
+ *
9070b3
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
9070b3
+ */
9070b3
+int
9070b3
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
9070b3
+{
9070b3
+	debug("audit %s key usage euid %d user %s fingerprint %s, result %d",
9070b3
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(),
9070b3
+		fp, rv);
9070b3
+}
9070b3
+
9070b3
+/*
9070b3
+ * This will be called when the protocol negotiation fails.
9070b3
+ */
9070b3
+void
9070b3
+audit_unsupported_body(struct ssh *ssh, int what)
9070b3
+{
9070b3
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
9070b3
+}
9070b3
+
9070b3
+/*
9070b3
+ * This will be called on succesfull protocol negotiation.
9070b3
+ */
9070b3
+void
9070b3
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
9070b3
+	       uid_t uid)
9070b3
+{
9070b3
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s pfs %s from pid %ld uid %u",
9070b3
+		(unsigned)geteuid(), ctos, enc, mac, compress, pfs, (long)pid,
9070b3
+	        (unsigned)uid);
9070b3
+}
9070b3
+
9070b3
+/*
9070b3
+ * This will be called on succesfull session key discard
9070b3
+ */
9070b3
+void
9070b3
+audit_session_key_free_body(struct ssh *, int ctos, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
9070b3
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
9070b3
+}
9070b3
+
9070b3
+/*
9070b3
+ * This will be called on destroy private part of the server key
9070b3
+ */
9070b3
+void
9070b3
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
9070b3
+		geteuid(), fp, (long)pid, (unsigned)uid);
9070b3
 }
9070b3
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
9070b3
 #endif /* SSH_AUDIT_EVENTS */
9070b3
diff -up openssh-8.6p1/audit.h.audit openssh-8.6p1/audit.h
9070b3
--- openssh-8.6p1/audit.h.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/audit.h	2021-05-06 12:05:27.376464524 +0200
9070b3
@@ -26,6 +26,7 @@
9070b3
 # define _SSH_AUDIT_H
9070b3
 
9070b3
 #include "loginrec.h"
9070b3
+#include "sshkey.h"
9070b3
 
9070b3
 struct ssh;
9070b3
 
9070b3
@@ -45,13 +46,32 @@ enum ssh_audit_event_type {
9070b3
 	SSH_CONNECTION_ABANDON,	/* closed without completing auth */
9070b3
 	SSH_AUDIT_UNKNOWN
9070b3
 };
9070b3
+
9070b3
+enum ssh_audit_kex {
9070b3
+	SSH_AUDIT_UNSUPPORTED_CIPHER,
9070b3
+	SSH_AUDIT_UNSUPPORTED_MAC,
9070b3
+	SSH_AUDIT_UNSUPPORTED_COMPRESSION
9070b3
+};
9070b3
 typedef enum ssh_audit_event_type ssh_audit_event_t;
9070b3
 
9070b3
+int	listening_for_clients(void);
9070b3
+
9070b3
 void	audit_connection_from(const char *, int);
9070b3
 void	audit_event(struct ssh *, ssh_audit_event_t);
9070b3
+void	audit_count_session_open(void);
9070b3
 void	audit_session_open(struct logininfo *);
9070b3
 void	audit_session_close(struct logininfo *);
9070b3
-void	audit_run_command(const char *);
9070b3
+int	audit_run_command(struct ssh *, const char *);
9070b3
+void 	audit_end_command(struct ssh *, int, const char *);
9070b3
 ssh_audit_event_t audit_classify_auth(const char *);
9070b3
+int	audit_keyusage(struct ssh *, int, char *, int);
9070b3
+void	audit_key(struct ssh *, int, int *, const struct sshkey *);
9070b3
+void	audit_unsupported(struct ssh *, int);
9070b3
+void	audit_kex(struct ssh *, int, char *, char *, char *, char *);
9070b3
+void	audit_unsupported_body(struct ssh *, int);
9070b3
+void	audit_kex_body(struct ssh *, int, char *, char *, char *, char *, pid_t, uid_t);
9070b3
+void	audit_session_key_free(struct ssh *, int ctos);
9070b3
+void	audit_session_key_free_body(struct ssh *, int ctos, pid_t, uid_t);
9070b3
+void	audit_destroy_sensitive_data(struct ssh *, const char *, pid_t, uid_t);
9070b3
 
9070b3
 #endif /* _SSH_AUDIT_H */
9070b3
diff -up openssh-8.6p1/audit-linux.c.audit openssh-8.6p1/audit-linux.c
9070b3
--- openssh-8.6p1/audit-linux.c.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/audit-linux.c	2021-05-06 12:05:27.377464532 +0200
9070b3
@@ -33,27 +33,40 @@
9070b3
 
9070b3
 #include "log.h"
9070b3
 #include "audit.h"
9070b3
+#include "sshkey.h"
9070b3
+#include "hostfile.h"
9070b3
+#include "auth.h"
9070b3
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
9070b3
+#include "servconf.h"
9070b3
 #include "canohost.h"
9070b3
 #include "packet.h"
9070b3
-
9070b3
+#include "cipher.h"
9070b3
+#include "channels.h"
9070b3
+#include "session.h"
9070b3
+
9070b3
+#define AUDIT_LOG_SIZE 256
9070b3
+
9070b3
+extern ServerOptions options;
9070b3
+extern Authctxt *the_authctxt;
9070b3
+extern u_int utmp_len;
9070b3
 const char *audit_username(void);
9070b3
 
9070b3
-int
9070b3
-linux_audit_record_event(int uid, const char *username, const char *hostname,
9070b3
-    const char *ip, const char *ttyn, int success)
9070b3
+static void
9070b3
+linux_audit_user_logxxx(int uid, const char *username,
9070b3
+    const char *ip, const char *ttyn, int success, int event)
9070b3
 {
9070b3
 	int audit_fd, rc, saved_errno;
9070b3
 
9070b3
 	if ((audit_fd = audit_open()) < 0) {
9070b3
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
9070b3
 		    errno == EAFNOSUPPORT)
9070b3
-			return 1; /* No audit support in kernel */
9070b3
+			return; /* No audit support in kernel */
9070b3
 		else
9070b3
-			return 0; /* Must prevent login */
9070b3
+			goto fatal_report; /* Must prevent login */
9070b3
 	}
9070b3
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
9070b3
+	rc = audit_log_acct_message(audit_fd, event,
9070b3
 	    NULL, "login", username ? username : "(unknown)",
9070b3
-	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
9070b3
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
9070b3
 	saved_errno = errno;
9070b3
 	close(audit_fd);
9070b3
 
9070b3
@@ -65,9 +78,96 @@ linux_audit_record_event(int uid, const
9070b3
 		rc = 0;
9070b3
 	errno = saved_errno;
9070b3
 
9070b3
-	return rc >= 0;
9070b3
+	if (rc < 0) {
9070b3
+fatal_report:
9070b3
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
9070b3
+	}
9070b3
+}
9070b3
+
9070b3
+static void
9070b3
+linux_audit_user_auth(int uid, const char *username,
9070b3
+    const char *ip, const char *ttyn, int success, int event)
9070b3
+{
9070b3
+	int audit_fd, rc, saved_errno;
9070b3
+	static const char *event_name[] = {
9070b3
+		"maxtries exceeded",
9070b3
+		"root denied",
9070b3
+		"success",
9070b3
+		"none",
9070b3
+		"password",
9070b3
+		"challenge-response",
9070b3
+		"pubkey",
9070b3
+		"hostbased",
9070b3
+		"gssapi",
9070b3
+		"invalid user",
9070b3
+		"nologin",
9070b3
+		"connection closed",
9070b3
+		"connection abandoned",
9070b3
+		"unknown"
9070b3
+	};
9070b3
+
9070b3
+	audit_fd = audit_open();
9070b3
+	if (audit_fd < 0) {
9070b3
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
9070b3
+		    errno == EAFNOSUPPORT)
9070b3
+			return; /* No audit support in kernel */
9070b3
+		else
9070b3
+			goto fatal_report; /* Must prevent login */
9070b3
+	}
9070b3
+
9070b3
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
9070b3
+		event = SSH_AUDIT_UNKNOWN;
9070b3
+
9070b3
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
9070b3
+	    NULL, event_name[event], username ? username : "(unknown)",
9070b3
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
9070b3
+	saved_errno = errno;
9070b3
+	close(audit_fd);
9070b3
+	/*
9070b3
+	 * Do not report error if the error is EPERM and sshd is run as non
9070b3
+	 * root user.
9070b3
+	 */
9070b3
+	if ((rc == -EPERM) && (geteuid() != 0))
9070b3
+		rc = 0;
9070b3
+	errno = saved_errno;
9070b3
+	if (rc < 0) {
9070b3
+fatal_report:
9070b3
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
9070b3
+	}
9070b3
+}
9070b3
+
9070b3
+int
9070b3
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
9070b3
+{
9070b3
+	char buf[AUDIT_LOG_SIZE];
9070b3
+	int audit_fd, rc, saved_errno;
9070b3
+
9070b3
+	audit_fd = audit_open();
9070b3
+	if (audit_fd < 0) {
9070b3
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
9070b3
+					 errno == EAFNOSUPPORT)
9070b3
+			return 1; /* No audit support in kernel */
9070b3
+		else
9070b3
+			return 0; /* Must prevent login */
9070b3
+	}
9070b3
+	snprintf(buf, sizeof(buf), "%s_auth grantors=auth-key", host_user ? "pubkey" : "hostbased");
9070b3
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
9070b3
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(ssh), NULL, rv);
9070b3
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
9070b3
+		goto out;
9070b3
+	snprintf(buf, sizeof(buf), "op=negotiate kind=auth-key fp=%s", fp);
9070b3
+	rc = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER, buf, NULL,
9070b3
+		ssh_remote_ipaddr(ssh), NULL, rv);
9070b3
+out:
9070b3
+	saved_errno = errno;
9070b3
+	audit_close(audit_fd);
9070b3
+	errno = saved_errno;
9070b3
+	/* do not report error if the error is EPERM and sshd is run as non root user */
9070b3
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
9070b3
 }
9070b3
 
9070b3
+static int user_login_count = 0;
9070b3
+
9070b3
 /* Below is the sshd audit API code */
9070b3
 
9070b3
 void
9070b3
@@ -76,49 +176,210 @@ audit_connection_from(const char *host,
9070b3
 	/* not implemented */
9070b3
 }
9070b3
 
9070b3
+int
9070b3
+audit_run_command(struct ssh *ssh, const char *command)
9070b3
+{
9070b3
+	if (!user_login_count++)
9070b3
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
9070b3
+		    ssh_remote_ipaddr(ssh),
9070b3
+		    "ssh", 1, AUDIT_USER_LOGIN);
9070b3
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
9070b3
+	    ssh_remote_ipaddr(ssh),
9070b3
+	    "ssh", 1, AUDIT_USER_START);
9070b3
+	return 0;
9070b3
+}
9070b3
+
9070b3
 void
9070b3
-audit_run_command(const char *command)
9070b3
+audit_end_command(struct ssh *ssh, int handle, const char *command)
9070b3
 {
9070b3
-	/* not implemented */
9070b3
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
9070b3
+	    ssh_remote_ipaddr(ssh),
9070b3
+	    "ssh", 1, AUDIT_USER_END);
9070b3
+	if (user_login_count && !--user_login_count)
9070b3
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
9070b3
+		    ssh_remote_ipaddr(ssh),
9070b3
+		    "ssh", 1, AUDIT_USER_LOGOUT);
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_count_session_open(void)
9070b3
+{
9070b3
+	user_login_count++;
9070b3
 }
9070b3
 
9070b3
 void
9070b3
 audit_session_open(struct logininfo *li)
9070b3
 {
9070b3
-	if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,
9070b3
-	    li->line, 1) == 0)
9070b3
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
9070b3
+	if (!user_login_count++)
9070b3
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
9070b3
+		    li->line, 1, AUDIT_USER_LOGIN);
9070b3
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
9070b3
+	    li->line, 1, AUDIT_USER_START);
9070b3
 }
9070b3
 
9070b3
 void
9070b3
 audit_session_close(struct logininfo *li)
9070b3
 {
9070b3
-	/* not implemented */
9070b3
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
9070b3
+	    li->line, 1, AUDIT_USER_END);
9070b3
+	if (user_login_count && !--user_login_count)
9070b3
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
9070b3
+		    li->line, 1, AUDIT_USER_LOGOUT);
9070b3
 }
9070b3
 
9070b3
 void
9070b3
 audit_event(struct ssh *ssh, ssh_audit_event_t event)
9070b3
 {
9070b3
 	switch(event) {
9070b3
-	case SSH_AUTH_SUCCESS:
9070b3
-	case SSH_CONNECTION_CLOSE:
9070b3
 	case SSH_NOLOGIN:
9070b3
-	case SSH_LOGIN_EXCEED_MAXTRIES:
9070b3
 	case SSH_LOGIN_ROOT_DENIED:
9070b3
+		linux_audit_user_auth(-1, audit_username(),
9070b3
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
9070b3
+		linux_audit_user_logxxx(-1, audit_username(),
9070b3
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
9070b3
 		break;
9070b3
-	case SSH_AUTH_FAIL_NONE:
9070b3
 	case SSH_AUTH_FAIL_PASSWD:
9070b3
+		if (options.use_pam)
9070b3
+			break;
9070b3
+	case SSH_LOGIN_EXCEED_MAXTRIES:
9070b3
 	case SSH_AUTH_FAIL_KBDINT:
9070b3
 	case SSH_AUTH_FAIL_PUBKEY:
9070b3
 	case SSH_AUTH_FAIL_HOSTBASED:
9070b3
 	case SSH_AUTH_FAIL_GSSAPI:
9070b3
+		linux_audit_user_auth(-1, audit_username(),
9070b3
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
9070b3
+		break;
9070b3
+
9070b3
+	case SSH_CONNECTION_CLOSE:
9070b3
+		if (user_login_count) {
9070b3
+			while (user_login_count--)
9070b3
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
9070b3
+				    ssh_remote_ipaddr(ssh),
9070b3
+				    "ssh", 1, AUDIT_USER_END);
9070b3
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
9070b3
+			    ssh_remote_ipaddr(ssh),
9070b3
+			    "ssh", 1, AUDIT_USER_LOGOUT);
9070b3
+		}
9070b3
+		break;
9070b3
+
9070b3
+	case SSH_CONNECTION_ABANDON:
9070b3
 	case SSH_INVALID_USER:
9070b3
-		linux_audit_record_event(-1, audit_username(), NULL,
9070b3
-		    ssh_remote_ipaddr(ssh), "sshd", 0);
9070b3
+		linux_audit_user_logxxx(-1, audit_username(),
9070b3
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
9070b3
 		break;
9070b3
 	default:
9070b3
 		debug("%s: unhandled event %d", __func__, event);
9070b3
 		break;
9070b3
 	}
9070b3
 }
9070b3
+
9070b3
+void
9070b3
+audit_unsupported_body(struct ssh *ssh, int what)
9070b3
+{
9070b3
+#ifdef AUDIT_CRYPTO_SESSION
9070b3
+	char buf[AUDIT_LOG_SIZE];
9070b3
+	const static char *name[] = { "cipher", "mac", "comp" };
9070b3
+	char *s;
9070b3
+	int audit_fd;
9070b3
+
9070b3
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
9070b3
+		name[what], ssh_remote_port(ssh), (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))),
9070b3
+		ssh_local_port(ssh));
9070b3
+	free(s);
9070b3
+	audit_fd = audit_open();
9070b3
+	if (audit_fd < 0)
9070b3
+		/* no problem, the next instruction will be fatal() */
9070b3
+		return;
9070b3
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
9070b3
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 0);
9070b3
+	audit_close(audit_fd);
9070b3
+#endif
9070b3
+}
9070b3
+
9070b3
+const static char *direction[] = { "from-server", "from-client", "both" };
9070b3
+
9070b3
+void
9070b3
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress,
9070b3
+    char *pfs, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+#ifdef AUDIT_CRYPTO_SESSION
9070b3
+	char buf[AUDIT_LOG_SIZE];
9070b3
+	int audit_fd, audit_ok;
9070b3
+	const struct sshcipher *cipher = cipher_by_name(enc);
9070b3
+	char *s;
9070b3
+
9070b3
+	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 ",
9070b3
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs,
9070b3
+		(intmax_t)pid, (intmax_t)uid,
9070b3
+		ssh_remote_port(ssh), (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))), ssh_local_port(ssh));
9070b3
+	free(s);
9070b3
+	audit_fd = audit_open();
9070b3
+	if (audit_fd < 0) {
9070b3
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
9070b3
+					 errno == EAFNOSUPPORT)
9070b3
+			return; /* No audit support in kernel */
9070b3
+		else
9070b3
+			fatal("cannot open audit"); /* Must prevent login */
9070b3
+	}
9070b3
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
9070b3
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 1);
9070b3
+	audit_close(audit_fd);
9070b3
+	/* do not abort if the error is EPERM and sshd is run as non root user */
9070b3
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
9070b3
+		fatal("cannot write into audit"); /* Must prevent login */
9070b3
+#endif
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+	char buf[AUDIT_LOG_SIZE];
9070b3
+	int audit_fd, audit_ok;
9070b3
+	char *s;
9070b3
+
9070b3
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
9070b3
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
9070b3
+		 ssh_remote_port(ssh),
9070b3
+		 (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))),
9070b3
+		 ssh_local_port(ssh));
9070b3
+	free(s);
9070b3
+	audit_fd = audit_open();
9070b3
+	if (audit_fd < 0) {
9070b3
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
9070b3
+					 errno != EAFNOSUPPORT)
9070b3
+			error("cannot open audit");
9070b3
+		return;
9070b3
+	}
9070b3
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
9070b3
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 1);
9070b3
+	audit_close(audit_fd);
9070b3
+	/* do not abort if the error is EPERM and sshd is run as non root user */
9070b3
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
9070b3
+		error("cannot write into audit");
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+	char buf[AUDIT_LOG_SIZE];
9070b3
+	int audit_fd, audit_ok;
9070b3
+
9070b3
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
9070b3
+		fp, (intmax_t)pid, (intmax_t)uid);
9070b3
+	audit_fd = audit_open();
9070b3
+	if (audit_fd < 0) {
9070b3
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
9070b3
+					 errno != EAFNOSUPPORT)
9070b3
+			error("cannot open audit");
9070b3
+		return;
9070b3
+	}
9070b3
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
9070b3
+			buf, NULL,
9070b3
+			listening_for_clients() ? NULL : ssh_remote_ipaddr(ssh),
9070b3
+			NULL, 1);
9070b3
+	audit_close(audit_fd);
9070b3
+	/* do not abort if the error is EPERM and sshd is run as non root user */
9070b3
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
9070b3
+		error("cannot write into audit");
9070b3
+}
9070b3
 #endif /* USE_LINUX_AUDIT */
9070b3
diff -up openssh-8.6p1/auditstub.c.audit openssh-8.6p1/auditstub.c
9070b3
--- openssh-8.6p1/auditstub.c.audit	2021-05-06 12:05:27.377464532 +0200
9070b3
+++ openssh-8.6p1/auditstub.c	2021-05-06 12:05:27.377464532 +0200
9070b3
@@ -0,0 +1,52 @@
9070b3
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
9070b3
+
9070b3
+/*
9070b3
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
9070b3
+ * Use is subject to license terms.
9070b3
+ *
9070b3
+ * Redistribution and use in source and binary forms, with or without
9070b3
+ * modification, are permitted provided that the following conditions
9070b3
+ * are met:
9070b3
+ * 1. Redistributions of source code must retain the above copyright
9070b3
+ *    notice, this list of conditions and the following disclaimer.
9070b3
+ * 2. Redistributions in binary form must reproduce the above copyright
9070b3
+ *    notice, this list of conditions and the following disclaimer in the
9070b3
+ *    documentation and/or other materials provided with the distribution.
9070b3
+ *
9070b3
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9070b3
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
9070b3
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9070b3
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
9070b3
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
9070b3
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
9070b3
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
9070b3
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
9070b3
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
9070b3
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9070b3
+ *
9070b3
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
9070b3
+ */
9070b3
+
9070b3
+#include <sys/types.h>
9070b3
+
9070b3
+struct ssh;
9070b3
+
9070b3
+void
9070b3
+audit_unsupported(struct ssh *ssh, int n)
9070b3
+{
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_kex(struct ssh *ssh, int ctos, char *enc, char *mac, char *comp, char *pfs)
9070b3
+{
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_session_key_free(struct ssh *ssh, int ctos)
9070b3
+{
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+}
9070b3
diff -up openssh-8.6p1/auth2.c.audit openssh-8.6p1/auth2.c
9070b3
--- openssh-8.6p1/auth2.c.audit	2021-05-06 12:05:27.305463975 +0200
9070b3
+++ openssh-8.6p1/auth2.c	2021-05-06 12:05:27.377464532 +0200
9070b3
@@ -298,9 +298,6 @@ input_userauth_request(int type, u_int32
9070b3
 		} else {
9070b3
 			/* Invalid user, fake password information */
9070b3
 			authctxt->pw = fakepw();
9070b3
-#ifdef SSH_AUDIT_EVENTS
9070b3
-			PRIVSEP(audit_event(ssh, SSH_INVALID_USER));
9070b3
-#endif
9070b3
 		}
9070b3
 #ifdef USE_PAM
9070b3
 		if (options.use_pam)
9070b3
diff -up openssh-8.6p1/auth2-hostbased.c.audit openssh-8.6p1/auth2-hostbased.c
9070b3
--- openssh-8.6p1/auth2-hostbased.c.audit	2021-05-06 12:05:27.283463805 +0200
9070b3
+++ openssh-8.6p1/auth2-hostbased.c	2021-05-06 12:05:27.377464532 +0200
9070b3
@@ -158,7 +158,7 @@ userauth_hostbased(struct ssh *ssh)
9070b3
 	authenticated = 0;
9070b3
 	if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
9070b3
 	    chost, key)) &&
9070b3
-	    PRIVSEP(sshkey_verify(key, sig, slen,
9070b3
+	    PRIVSEP(hostbased_key_verify(ssh, key, sig, slen,
9070b3
 	    sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL)) == 0)
9070b3
 		authenticated = 1;
9070b3
 
9070b3
@@ -175,6 +175,20 @@ done:
9070b3
 	return authenticated;
9070b3
 }
9070b3
 
9070b3
+int
9070b3
+hostbased_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig,
9070b3
+    size_t slen, const u_char *data, size_t datalen, const char *pkalg, u_int compat,
9070b3
+    struct sshkey_sig_details **detailsp)
9070b3
+{
9070b3
+	int rv;
9070b3
+
9070b3
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat, detailsp);
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	audit_key(ssh, 0, &rv, key);
9070b3
+#endif
9070b3
+	return rv;
9070b3
+}
9070b3
+
9070b3
 /* return 1 if given hostkey is allowed */
9070b3
 int
9070b3
 hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
9070b3
diff -up openssh-8.6p1/auth2-pubkey.c.audit openssh-8.6p1/auth2-pubkey.c
9070b3
--- openssh-8.6p1/auth2-pubkey.c.audit	2021-05-06 12:05:27.344464277 +0200
9070b3
+++ openssh-8.6p1/auth2-pubkey.c	2021-05-06 12:05:27.378464540 +0200
9070b3
@@ -213,7 +213,7 @@ userauth_pubkey(struct ssh *ssh)
9070b3
 		/* test for correct signature */
9070b3
 		authenticated = 0;
9070b3
 		if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
9070b3
-		    PRIVSEP(sshkey_verify(key, sig, slen,
9070b3
+		    PRIVSEP(user_key_verify(ssh, key, sig, slen,
9070b3
 		    sshbuf_ptr(b), sshbuf_len(b),
9070b3
 		    (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
9070b3
 		    ssh->compat, &sig_details)) == 0) {
9070b3
@@ -305,6 +305,20 @@ done:
9070b3
 	return authenticated;
9070b3
 }
9070b3
 
9070b3
+int
9070b3
+user_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig,
9070b3
+    size_t slen, const u_char *data, size_t datalen, const char *pkalg, u_int compat,
9070b3
+    struct sshkey_sig_details **detailsp)
9070b3
+{
9070b3
+	int rv;
9070b3
+
9070b3
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat, detailsp);
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	audit_key(ssh, 1, &rv, key);
9070b3
+#endif
9070b3
+	return rv;
9070b3
+}
9070b3
+
9070b3
 static int
9070b3
 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
9070b3
 {
9070b3
diff -up openssh-8.6p1/auth.c.audit openssh-8.6p1/auth.c
9070b3
--- openssh-8.6p1/auth.c.audit	2021-05-06 12:05:27.304463967 +0200
9070b3
+++ openssh-8.6p1/auth.c	2021-05-06 12:05:27.378464540 +0200
9070b3
@@ -597,9 +597,6 @@ getpwnamallow(struct ssh *ssh, const cha
9070b3
 		record_failed_login(ssh, user,
9070b3
 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
9070b3
 #endif
9070b3
-#ifdef SSH_AUDIT_EVENTS
9070b3
-		audit_event(ssh, SSH_INVALID_USER);
9070b3
-#endif /* SSH_AUDIT_EVENTS */
9070b3
 		return (NULL);
9070b3
 	}
9070b3
 	if (!allowed_user(ssh, pw))
9070b3
diff -up openssh-8.6p1/auth.h.audit openssh-8.6p1/auth.h
9070b3
--- openssh-8.6p1/auth.h.audit	2021-05-06 12:05:27.318464076 +0200
9070b3
+++ openssh-8.6p1/auth.h	2021-05-06 12:05:27.378464540 +0200
9070b3
@@ -193,6 +193,8 @@ struct passwd * getpwnamallow(struct ssh
9070b3
 
9070b3
 char	*expand_authorized_keys(const char *, struct passwd *pw);
9070b3
 char	*authorized_principals_file(struct passwd *);
9070b3
+int	 user_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
9070b3
+    const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **);
9070b3
 
9070b3
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
9070b3
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
9070b3
@@ -212,6 +214,8 @@ struct sshkey	*get_hostkey_private_by_ty
9070b3
 int	 get_hostkey_index(struct sshkey *, int, struct ssh *);
9070b3
 int	 sshd_hostkey_sign(struct ssh *, struct sshkey *, struct sshkey *,
9070b3
     u_char **, size_t *, const u_char *, size_t, const char *);
9070b3
+int	 hostbased_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
9070b3
+    const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **);
9070b3
 
9070b3
 /* Key / cert options linkage to auth layer */
9070b3
 const struct sshauthopt *auth_options(struct ssh *);
9070b3
diff -up openssh-8.6p1/cipher.c.audit openssh-8.6p1/cipher.c
9070b3
--- openssh-8.6p1/cipher.c.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/cipher.c	2021-05-06 12:05:27.378464540 +0200
9070b3
@@ -64,25 +64,6 @@ struct sshcipher_ctx {
9070b3
 	const struct sshcipher *cipher;
9070b3
 };
9070b3
 
9070b3
-struct sshcipher {
9070b3
-	char	*name;
9070b3
-	u_int	block_size;
9070b3
-	u_int	key_len;
9070b3
-	u_int	iv_len;		/* defaults to block_size */
9070b3
-	u_int	auth_len;
9070b3
-	u_int	flags;
9070b3
-#define CFLAG_CBC		(1<<0)
9070b3
-#define CFLAG_CHACHAPOLY	(1<<1)
9070b3
-#define CFLAG_AESCTR		(1<<2)
9070b3
-#define CFLAG_NONE		(1<<3)
9070b3
-#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
9070b3
-#ifdef WITH_OPENSSL
9070b3
-	const EVP_CIPHER	*(*evptype)(void);
9070b3
-#else
9070b3
-	void	*ignored;
9070b3
-#endif
9070b3
-};
9070b3
-
9070b3
 static const struct sshcipher ciphers[] = {
9070b3
 #ifdef WITH_OPENSSL
9070b3
 #ifndef OPENSSL_NO_DES
9070b3
@@ -422,7 +403,7 @@ cipher_get_length(struct sshcipher_ctx *
9070b3
 void
9070b3
 cipher_free(struct sshcipher_ctx *cc)
9070b3
 {
9070b3
-	if (cc == NULL)
9070b3
+	if (cc == NULL || cc->cipher == NULL)
9070b3
 		return;
9070b3
 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
9070b3
 		chachapoly_free(cc->cp_ctx);
9070b3
diff -up openssh-8.6p1/cipher.h.audit openssh-8.6p1/cipher.h
9070b3
--- openssh-8.6p1/cipher.h.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/cipher.h	2021-05-06 12:05:27.378464540 +0200
9070b3
@@ -47,7 +47,25 @@
9070b3
 #define CIPHER_ENCRYPT		1
9070b3
 #define CIPHER_DECRYPT		0
9070b3
 
9070b3
-struct sshcipher;
9070b3
+struct sshcipher {
9070b3
+	char	*name;
9070b3
+	u_int	block_size;
9070b3
+	u_int	key_len;
9070b3
+	u_int	iv_len;		/* defaults to block_size */
9070b3
+	u_int	auth_len;
9070b3
+	u_int	flags;
9070b3
+#define CFLAG_CBC		(1<<0)
9070b3
+#define CFLAG_CHACHAPOLY	(1<<1)
9070b3
+#define CFLAG_AESCTR		(1<<2)
9070b3
+#define CFLAG_NONE		(1<<3)
9070b3
+#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
9070b3
+#ifdef WITH_OPENSSL
9070b3
+	const EVP_CIPHER	*(*evptype)(void);
9070b3
+#else
9070b3
+	void	*ignored;
9070b3
+#endif
9070b3
+};
9070b3
+
9070b3
 struct sshcipher_ctx;
9070b3
 
9070b3
 const struct sshcipher *cipher_by_name(const char *);
9070b3
diff -up openssh-8.6p1/kex.c.audit openssh-8.6p1/kex.c
9070b3
--- openssh-8.6p1/kex.c.audit	2021-05-06 12:05:27.368464462 +0200
9070b3
+++ openssh-8.6p1/kex.c	2021-05-06 12:05:27.379464547 +0200
9070b3
@@ -65,6 +65,7 @@
9070b3
 #include "ssherr.h"
9070b3
 #include "sshbuf.h"
9070b3
 #include "digest.h"
9070b3
+#include "audit.h"
9070b3
 
9070b3
 #ifdef GSSAPI
9070b3
 #include "ssh-gss.h"
9070b3
@@ -816,12 +817,16 @@ kex_start_rekex(struct ssh *ssh)
9070b3
 }
9070b3
 
9070b3
 static int
9070b3
-choose_enc(struct sshenc *enc, char *client, char *server)
9070b3
+choose_enc(struct ssh *ssh, struct sshenc *enc, char *client, char *server)
9070b3
 {
9070b3
 	char *name = match_list(client, server, NULL);
9070b3
 
9070b3
-	if (name == NULL)
9070b3
+	if (name == NULL) {
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_CIPHER);
9070b3
+#endif
9070b3
 		return SSH_ERR_NO_CIPHER_ALG_MATCH;
9070b3
+	}
9070b3
 	if ((enc->cipher = cipher_by_name(name)) == NULL) {
9070b3
 		error_f("unsupported cipher %s", name);
9070b3
 		free(name);
9070b3
@@ -842,8 +847,12 @@ choose_mac(struct ssh *ssh, struct sshma
9070b3
 {
9070b3
 	char *name = match_list(client, server, NULL);
9070b3
 
9070b3
-	if (name == NULL)
9070b3
+	if (name == NULL) {
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_MAC);
9070b3
+#endif
9070b3
 		return SSH_ERR_NO_MAC_ALG_MATCH;
9070b3
+	}
9070b3
 	if (mac_setup(mac, name) < 0) {
9070b3
 		error_f("unsupported MAC %s", name);
9070b3
 		free(name);
9070b3
@@ -856,12 +865,16 @@ choose_mac(struct ssh *ssh, struct sshma
9070b3
 }
9070b3
 
9070b3
 static int
9070b3
-choose_comp(struct sshcomp *comp, char *client, char *server)
9070b3
+choose_comp(struct ssh *ssh, struct sshcomp *comp, char *client, char *server)
9070b3
 {
9070b3
 	char *name = match_list(client, server, NULL);
9070b3
 
9070b3
-	if (name == NULL)
9070b3
+	if (name == NULL) {
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_COMPRESSION);
9070b3
+#endif
9070b3
 		return SSH_ERR_NO_COMPRESS_ALG_MATCH;
9070b3
+	}
9070b3
 #ifdef WITH_ZLIB
9070b3
 	if (strcmp(name, "zlib@openssh.com") == 0) {
9070b3
 		comp->type = COMP_DELAYED;
9070b3
@@ -1002,7 +1015,7 @@ kex_choose_conf(struct ssh *ssh)
9070b3
 		nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
9070b3
 		nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
9070b3
 		ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
9070b3
-		if ((r = choose_enc(&newkeys->enc, cprop[nenc],
9070b3
+		if ((r = choose_enc(ssh, &newkeys->enc, cprop[nenc],
9070b3
 		    sprop[nenc])) != 0) {
9070b3
 			kex->failed_choice = peer[nenc];
9070b3
 			peer[nenc] = NULL;
9070b3
@@ -1017,7 +1030,7 @@ kex_choose_conf(struct ssh *ssh)
9070b3
 			peer[nmac] = NULL;
9070b3
 			goto out;
9070b3
 		}
9070b3
-		if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
9070b3
+		if ((r = choose_comp(ssh, &newkeys->comp, cprop[ncomp],
9070b3
 		    sprop[ncomp])) != 0) {
9070b3
 			kex->failed_choice = peer[ncomp];
9070b3
 			peer[ncomp] = NULL;
9070b3
@@ -1040,6 +1053,10 @@ kex_choose_conf(struct ssh *ssh)
9070b3
 		dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
9070b3
 		dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
9070b3
 		dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
9070b3
+		debug("kex: %s need=%d dh_need=%d", kex->name, need, dh_need);
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+		audit_kex(ssh, mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
9070b3
+#endif
9070b3
 	}
9070b3
 	/* XXX need runden? */
9070b3
 	kex->we_need = need;
9070b3
@@ -1297,6 +1314,36 @@ dump_digest(const char *msg, const u_cha
9070b3
 }
9070b3
 #endif
9070b3
 
9070b3
+static void
9070b3
+enc_destroy(struct sshenc *enc)
9070b3
+{
9070b3
+	if (enc == NULL)
9070b3
+		return;
9070b3
+
9070b3
+	if (enc->key) {
9070b3
+		memset(enc->key, 0, enc->key_len);
9070b3
+		free(enc->key);
9070b3
+	}
9070b3
+
9070b3
+	if (enc->iv) {
9070b3
+		memset(enc->iv,  0, enc->iv_len);
9070b3
+		free(enc->iv);
9070b3
+	}
9070b3
+
9070b3
+	memset(enc, 0, sizeof(*enc));
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+newkeys_destroy(struct newkeys *newkeys)
9070b3
+{
9070b3
+	if (newkeys == NULL)
9070b3
+		return;
9070b3
+
9070b3
+	enc_destroy(&newkeys->enc);
9070b3
+	mac_destroy(&newkeys->mac);
9070b3
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
9070b3
+}
9070b3
+
9070b3
 /*
9070b3
  * Send a plaintext error message to the peer, suffixed by \r\n.
9070b3
  * Only used during banner exchange, and there only for the server.
9070b3
diff -up openssh-8.6p1/kex.h.audit openssh-8.6p1/kex.h
9070b3
--- openssh-8.6p1/kex.h.audit	2021-05-06 12:05:27.306463983 +0200
9070b3
+++ openssh-8.6p1/kex.h	2021-05-06 12:05:27.379464547 +0200
9070b3
@@ -226,6 +226,8 @@ int	 kexgss_client(struct ssh *);
9070b3
 int	 kexgss_server(struct ssh *);
9070b3
 #endif
9070b3
 
9070b3
+void	newkeys_destroy(struct newkeys *newkeys);
9070b3
+
9070b3
 int	 kex_dh_keypair(struct kex *);
9070b3
 int	 kex_dh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
9070b3
     struct sshbuf **);
9070b3
diff -up openssh-8.6p1/mac.c.audit openssh-8.6p1/mac.c
9070b3
--- openssh-8.6p1/mac.c.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/mac.c	2021-05-06 12:05:27.379464547 +0200
9070b3
@@ -239,6 +239,20 @@ mac_clear(struct sshmac *mac)
9070b3
 	mac->umac_ctx = NULL;
9070b3
 }
9070b3
 
9070b3
+void
9070b3
+mac_destroy(struct sshmac *mac)
9070b3
+{
9070b3
+	if (mac == NULL)
9070b3
+		return;
9070b3
+
9070b3
+	if (mac->key) {
9070b3
+		memset(mac->key, 0, mac->key_len);
9070b3
+		free(mac->key);
9070b3
+	}
9070b3
+
9070b3
+	memset(mac, 0, sizeof(*mac));
9070b3
+}
9070b3
+
9070b3
 /* XXX copied from ciphers_valid */
9070b3
 #define	MAC_SEP	","
9070b3
 int
9070b3
diff -up openssh-8.6p1/mac.h.audit openssh-8.6p1/mac.h
9070b3
--- openssh-8.6p1/mac.h.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/mac.h	2021-05-06 12:05:27.379464547 +0200
9070b3
@@ -49,5 +49,6 @@ int	 mac_compute(struct sshmac *, u_int3
9070b3
 int	 mac_check(struct sshmac *, u_int32_t, const u_char *, size_t,
9070b3
     const u_char *, size_t);
9070b3
 void	 mac_clear(struct sshmac *);
9070b3
+void	 mac_destroy(struct sshmac *);
9070b3
 
9070b3
 #endif /* SSHMAC_H */
9070b3
diff -up openssh-8.6p1/Makefile.in.audit openssh-8.6p1/Makefile.in
9070b3
--- openssh-8.6p1/Makefile.in.audit	2021-05-06 12:05:27.352464339 +0200
9070b3
+++ openssh-8.6p1/Makefile.in	2021-05-06 12:05:27.380464555 +0200
9070b3
@@ -112,7 +112,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
9070b3
 	kexsntrup761x25519.o sntrup761.o kexgen.o \
9070b3
 	kexgssc.o \
9070b3
 	sftp-realpath.o platform-pledge.o platform-tracing.o platform-misc.o \
9070b3
-	sshbuf-io.o
9070b3
+	sshbuf-io.o auditstub.o
9070b3
 
9070b3
 SKOBJS=	ssh-sk-client.o
9070b3
 
9070b3
diff -up openssh-8.6p1/monitor.c.audit openssh-8.6p1/monitor.c
9070b3
--- openssh-8.6p1/monitor.c.audit	2021-05-06 12:05:27.326464138 +0200
9070b3
+++ openssh-8.6p1/monitor.c	2021-05-06 12:05:27.380464555 +0200
9070b3
@@ -93,6 +93,7 @@
9070b3
 #include "compat.h"
9070b3
 #include "ssh2.h"
9070b3
 #include "authfd.h"
9070b3
+#include "audit.h"
9070b3
 #include "match.h"
9070b3
 #include "ssherr.h"
9070b3
 #include "sk-api.h"
9070b3
@@ -107,6 +108,8 @@ extern u_int utmp_len;
9070b3
 extern struct sshbuf *loginmsg;
9070b3
 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
9070b3
 
9070b3
+extern void destroy_sensitive_data(struct ssh *, int);
9070b3
+
9070b3
 /* State exported from the child */
9070b3
 static struct sshbuf *child_state;
9070b3
 
9070b3
@@ -157,6 +160,11 @@ int mm_answer_gss_updatecreds(struct ssh
9070b3
 #ifdef SSH_AUDIT_EVENTS
9070b3
 int mm_answer_audit_event(struct ssh *, int, struct sshbuf *);
9070b3
 int mm_answer_audit_command(struct ssh *, int, struct sshbuf *);
9070b3
+int mm_answer_audit_end_command(struct ssh *, int, struct sshbuf *);
9070b3
+int mm_answer_audit_unsupported_body(struct ssh *, int, struct sshbuf *);
9070b3
+int mm_answer_audit_kex_body(struct ssh *, int, struct sshbuf *);
9070b3
+int mm_answer_audit_session_key_free_body(struct ssh *, int, struct sshbuf *);
9070b3
+int mm_answer_audit_server_key_free(struct ssh *, int, struct sshbuf *);
9070b3
 #endif
9070b3
 
9070b3
 static Authctxt *authctxt;
9070b3
@@ -215,6 +223,10 @@ struct mon_table mon_dispatch_proto20[]
9070b3
 #endif
9070b3
 #ifdef SSH_AUDIT_EVENTS
9070b3
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
9070b3
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
9070b3
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
9070b3
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
9070b3
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
9070b3
 #endif
9070b3
 #ifdef BSD_AUTH
9070b3
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
9070b3
@@ -249,6 +261,11 @@ struct mon_table mon_dispatch_postauth20
9070b3
 #ifdef SSH_AUDIT_EVENTS
9070b3
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
9070b3
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
9070b3
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
9070b3
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
9070b3
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
9070b3
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
9070b3
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
9070b3
 #endif
9070b3
     {0, 0, NULL}
9070b3
 };
9070b3
@@ -1444,8 +1461,10 @@ mm_answer_keyverify(struct ssh *ssh, int
9070b3
 	int r, ret, req_presence = 0, req_verify = 0, valid_data = 0;
9070b3
 	int encoded_ret;
9070b3
 	struct sshkey_sig_details *sig_details = NULL;
9070b3
+	int type = 0;
9070b3
 
9070b3
-	if ((r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
9070b3
+	if ((r = sshbuf_get_u32(m, &type)) != 0 ||
9070b3
+	    (r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
9070b3
 	    (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 ||
9070b3
 	    (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 ||
9070b3
 	    (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
9070b3
@@ -1454,6 +1473,8 @@ mm_answer_keyverify(struct ssh *ssh, int
9070b3
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
9070b3
 	  !monitor_allowed_key(blob, bloblen))
9070b3
 		fatal_f("bad key, not previously allowed");
9070b3
+	if (type != key_blobtype)
9070b3
+		fatal_f("bad key type");
9070b3
 
9070b3
 	/* Empty signature algorithm means NULL. */
9070b3
 	if (*sigalg == '\0') {
9070b3
@@ -1469,14 +1490,19 @@ mm_answer_keyverify(struct ssh *ssh, int
9070b3
 	case MM_USERKEY:
9070b3
 		valid_data = monitor_valid_userblob(ssh, data, datalen);
9070b3
 		auth_method = "publickey";
9070b3
+		ret = user_key_verify(ssh, key, signature, signaturelen, data,
9070b3
+		    datalen, sigalg, ssh->compat, &sig_details);
9070b3
 		break;
9070b3
 	case MM_HOSTKEY:
9070b3
 		valid_data = monitor_valid_hostbasedblob(data, datalen,
9070b3
 		    hostbased_cuser, hostbased_chost);
9070b3
 		auth_method = "hostbased";
9070b3
+		ret = hostbased_key_verify(ssh, key, signature, signaturelen, data,
9070b3
+		    datalen, sigalg, ssh->compat, &sig_details);
9070b3
 		break;
9070b3
 	default:
9070b3
 		valid_data = 0;
9070b3
+		ret = 0;
9070b3
 		break;
9070b3
 	}
9070b3
 	if (!valid_data)
9070b3
@@ -1488,8 +1514,6 @@ mm_answer_keyverify(struct ssh *ssh, int
9070b3
 	    SSH_FP_DEFAULT)) == NULL)
9070b3
 		fatal_f("sshkey_fingerprint failed");
9070b3
 
9070b3
-	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
9070b3
-	    sigalg, ssh->compat, &sig_details);
9070b3
	debug3_f("%s %s signature %s%s%s", auth_method, sshkey_type(key),
9070b3
	    (ret == 0) ? "verified" : "unverified",
9070b3
	    (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
9070b3
@@ -1576,13 +1600,19 @@ mm_record_login(struct ssh *ssh, Session
9070b3
 }
9070b3
 
9070b3
 static void
9070b3
-mm_session_close(Session *s)
9070b3
+mm_session_close(struct ssh *ssh, Session *s)
9070b3
 {
9070b3
 	debug3_f("session %d pid %ld", s->self, (long)s->pid);
9070b3
 	if (s->ttyfd != -1) {
9070b3
 		debug3_f("tty %s ptyfd %d", s->tty, s->ptyfd);
9070b3
 		session_pty_cleanup2(s);
9070b3
 	}
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	if (s->command != NULL) {
9070b3
+		debug3_f("command %d", s->command_handle);
9070b3
+		session_end_command2(ssh, s);
9070b3
+	}
9070b3
+#endif
9070b3
 	session_unused(s->self);
9070b3
 }
9070b3
 
9070b3
@@ -1649,7 +1679,7 @@ mm_answer_pty(struct ssh *ssh, int sock,
9070b3
 
9070b3
  error:
9070b3
 	if (s != NULL)
9070b3
-		mm_session_close(s);
9070b3
+		mm_session_close(ssh, s);
9070b3
 	if ((r = sshbuf_put_u32(m, 0)) != 0)
9070b3
 		fatal_fr(r, "assemble 0");
9070b3
 	mm_request_send(sock, MONITOR_ANS_PTY, m);
9070b3
@@ -1668,7 +1698,7 @@ mm_answer_pty_cleanup(struct ssh *ssh, i
9070b3
 	if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
9070b3
 		fatal_fr(r, "parse tty");
9070b3
 	if ((s = session_by_tty(tty)) != NULL)
9070b3
-		mm_session_close(s);
9070b3
+		mm_session_close(ssh, s);
9070b3
 	sshbuf_reset(m);
9070b3
 	free(tty);
9070b3
 	return (0);
9070b3
@@ -1690,6 +1720,8 @@ mm_answer_term(struct ssh *ssh, int sock
9070b3
 		sshpam_cleanup();
9070b3
 #endif
9070b3
 
9070b3
+	destroy_sensitive_data(ssh, 0);
9070b3
+
9070b3
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
9070b3
 		if (errno != EINTR)
9070b3
 			exit(1);
9070b3
@@ -1736,12 +1768,47 @@ mm_answer_audit_command(struct ssh *ssh,
9070b3
 {
9070b3
 	char *cmd;
9070b3
 	int r;
9070b3
+	Session *s;
9070b3
 
9070b3
 	debug3("%s entering", __func__);
9070b3
 	if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
9070b3
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
9070b3
+
9070b3
 	/* sanity check command, if so how? */
9070b3
-	audit_run_command(cmd);
9070b3
+	s = session_new();
9070b3
+	if (s == NULL)
9070b3
+		fatal_f("error allocating a session");
9070b3
+	s->command = cmd;
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	s->command_handle = audit_run_command(ssh, cmd);
9070b3
+#endif
9070b3
+
9070b3
+	sshbuf_reset(m);
9070b3
+	sshbuf_put_u32(m, s->self);
9070b3
+
9070b3
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
9070b3
+
9070b3
+	return (0);
9070b3
+}
9070b3
+
9070b3
+int
9070b3
+mm_answer_audit_end_command(struct ssh *ssh, int socket, struct sshbuf *m)
9070b3
+{
9070b3
+	int handle, r;
9070b3
+	size_t len;
9070b3
+	u_char *cmd = NULL;
9070b3
+	Session *s;
9070b3
+
9070b3
+	debug3_f("entering");
9070b3
+	if ((r = sshbuf_get_u32(m, &handle)) != 0 ||
9070b3
+	    (r = sshbuf_get_string(m, &cmd, &len)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+
9070b3
+	s = session_by_id(handle);
9070b3
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
9070b3
+	    strcmp(s->command, cmd) != 0)
9070b3
+		fatal_f("invalid handle");
9070b3
+	mm_session_close(ssh, s);
9070b3
 	free(cmd);
9070b3
 	return (0);
9070b3
 }
9070b3
@@ -1813,6 +1880,7 @@ monitor_apply_keystate(struct ssh *ssh,
9070b3
 void
9070b3
 mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
9070b3
 {
9070b3
+	struct sshbuf *m;
9070b3
 	debug3_f("Waiting for new keys");
9070b3
 
9070b3
 	if ((child_state = sshbuf_new()) == NULL)
9070b3
@@ -1820,6 +1888,19 @@ mm_get_keystate(struct ssh *ssh, struct
9070b3
 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
9070b3
 	    child_state);
9070b3
 	debug3_f("GOT new keys");
9070b3
+
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	m = sshbuf_new();
9070b3
+	mm_request_receive_expect(pmonitor->m_sendfd,
9070b3
+				  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
9070b3
+	mm_answer_audit_session_key_free_body(ssh, pmonitor->m_sendfd, m);
9070b3
+	sshbuf_free(m);
9070b3
+#endif
9070b3
+
9070b3
+	/* Drain any buffered messages from the child */
9070b3
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
9070b3
+		;
9070b3
+
9070b3
 }
9070b3
 
9070b3
 
9070b3
@@ -2111,3 +2192,102 @@ mm_answer_gss_updatecreds(struct ssh *ss
9070b3
 
9070b3
 #endif /* GSSAPI */
9070b3
 
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+int
9070b3
+mm_answer_audit_unsupported_body(struct ssh *ssh, int sock, struct sshbuf *m)
9070b3
+{
9070b3
+	int what, r;
9070b3
+
9070b3
+	if ((r = sshbuf_get_u32(m, &what)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+
9070b3
+	audit_unsupported_body(ssh, what);
9070b3
+
9070b3
+	sshbuf_reset(m);
9070b3
+
9070b3
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
9070b3
+	return 0;
9070b3
+}
9070b3
+
9070b3
+int
9070b3
+mm_answer_audit_kex_body(struct ssh *ssh, int sock, struct sshbuf *m)
9070b3
+{
9070b3
+	int ctos, r;
9070b3
+	char *cipher, *mac, *compress, *pfs;
9070b3
+	u_int64_t tmp;
9070b3
+	pid_t pid;
9070b3
+	uid_t uid;
9070b3
+
9070b3
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
9070b3
+	    (r = sshbuf_get_cstring(m, &cipher, NULL)) != 0 ||
9070b3
+	    (r = sshbuf_get_cstring(m, &mac, NULL)) != 0 ||
9070b3
+	    (r = sshbuf_get_cstring(m, &compress, NULL)) != 0 ||
9070b3
+	    (r = sshbuf_get_cstring(m, &pfs, NULL)) != 0 ||
9070b3
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+	pid = (pid_t) tmp;
9070b3
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+	uid = (pid_t) tmp;
9070b3
+
9070b3
+	audit_kex_body(ssh, ctos, cipher, mac, compress, pfs, pid, uid);
9070b3
+
9070b3
+	free(cipher);
9070b3
+	free(mac);
9070b3
+	free(compress);
9070b3
+	free(pfs);
9070b3
+	sshbuf_reset(m);
9070b3
+
9070b3
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
9070b3
+	return 0;
9070b3
+}
9070b3
+
9070b3
+int
9070b3
+mm_answer_audit_session_key_free_body(struct ssh *ssh, int sock, struct sshbuf *m)
9070b3
+{
9070b3
+	int ctos, r;
9070b3
+	u_int64_t tmp;
9070b3
+	pid_t pid;
9070b3
+	uid_t uid;
9070b3
+
9070b3
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
9070b3
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+	pid = (pid_t) tmp;
9070b3
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+	uid = (uid_t) tmp;
9070b3
+
9070b3
+	audit_session_key_free_body(ssh, ctos, pid, uid);
9070b3
+
9070b3
+	sshbuf_reset(m);
9070b3
+
9070b3
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
9070b3
+	return 0;
9070b3
+}
9070b3
+
9070b3
+int
9070b3
+mm_answer_audit_server_key_free(struct ssh *ssh, int sock, struct sshbuf *m)
9070b3
+{
9070b3
+	size_t len, r;
9070b3
+	char *fp;
9070b3
+	u_int64_t tmp;
9070b3
+	pid_t pid;
9070b3
+	uid_t uid;
9070b3
+
9070b3
+	if ((r = sshbuf_get_cstring(m, &fp, &len)) != 0 ||
9070b3
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+	pid = (pid_t) tmp;
9070b3
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+	uid = (uid_t) tmp;
9070b3
+
9070b3
+	audit_destroy_sensitive_data(ssh, fp, pid, uid);
9070b3
+
9070b3
+	free(fp);
9070b3
+	sshbuf_reset(m);
9070b3
+
9070b3
+	return 0;
9070b3
+}
9070b3
+#endif /* SSH_AUDIT_EVENTS */
9070b3
diff -up openssh-8.6p1/monitor.h.audit openssh-8.6p1/monitor.h
9070b3
--- openssh-8.6p1/monitor.h.audit	2021-05-06 12:05:27.326464138 +0200
9070b3
+++ openssh-8.6p1/monitor.h	2021-05-06 12:05:27.380464555 +0200
9070b3
@@ -65,7 +65,13 @@ enum monitor_reqtype {
9070b3
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
9070b3
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
9070b3
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
9070b3
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
9070b3
+	MONITOR_REQ_AUDIT_EVENT = 112,
9070b3
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
9070b3
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
9070b3
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
9070b3
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
9070b3
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
9070b3
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124,
9070b3
 
9070b3
 	MONITOR_REQ_GSSSIGN = 150, MONITOR_ANS_GSSSIGN = 151,
9070b3
 	MONITOR_REQ_GSSUPCREDS = 152, MONITOR_ANS_GSSUPCREDS = 153,
9070b3
diff -up openssh-8.6p1/monitor_wrap.c.audit openssh-8.6p1/monitor_wrap.c
9070b3
--- openssh-8.6p1/monitor_wrap.c.audit	2021-05-06 12:05:27.307463991 +0200
9070b3
+++ openssh-8.6p1/monitor_wrap.c	2021-05-06 12:05:27.381464563 +0200
9070b3
@@ -520,7 +520,7 @@ mm_key_allowed(enum mm_keytype type, con
9070b3
  */
9070b3
 
9070b3
 int
9070b3
-mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
9070b3
+mm_sshkey_verify(enum mm_keytype type, const struct sshkey *key, const u_char *sig, size_t siglen,
9070b3
     const u_char *data, size_t datalen, const char *sigalg, u_int compat,
9070b3
     struct sshkey_sig_details **sig_detailsp)
9070b3
 {
9070b3
@@ -536,7 +536,8 @@ mm_sshkey_verify(const struct sshkey *ke
9070b3
 		*sig_detailsp = NULL;
9070b3
 	if ((m = sshbuf_new()) == NULL)
9070b3
 		fatal_f("sshbuf_new failed");
9070b3
-	if ((r = sshkey_puts(key, m)) != 0 ||
9070b3
+	if ((r = sshbuf_put_u32(m, type)) != 0 ||
9070b3
+	    (r = sshkey_puts(key, m)) != 0 ||
9070b3
 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
9070b3
 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
9070b3
 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
9070b3
@@ -569,6 +570,22 @@ mm_sshkey_verify(const struct sshkey *ke
9070b3
 	return 0;
9070b3
 }
9070b3
 
9070b3
+int
9070b3
+mm_hostbased_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig, size_t siglen,
9070b3
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat,
9070b3
+    struct sshkey_sig_details **detailsp)
9070b3
+{
9070b3
+	return mm_sshkey_verify(MM_HOSTKEY, key, sig, siglen, data, datalen, pkalg, compat, detailsp);
9070b3
+}
9070b3
+
9070b3
+int
9070b3
+mm_user_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig, size_t siglen,
9070b3
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat,
9070b3
+    struct sshkey_sig_details **detailsp)
9070b3
+{
9070b3
+	return mm_sshkey_verify(MM_USERKEY, key, sig, siglen, data, datalen, pkalg, compat, detailsp);
9070b3
+}
9070b3
+
9070b3
 void
9070b3
 mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
9070b3
 {
9070b3
@@ -921,11 +938,12 @@ mm_audit_event(struct ssh *ssh, ssh_audi
9070b3
 	sshbuf_free(m);
9070b3
 }
9070b3
 
9070b3
-void
9070b3
-mm_audit_run_command(const char *command)
9070b3
+int
9070b3
+mm_audit_run_command(struct ssh *ssh, const char *command)
9070b3
 {
9070b3
 	struct sshbuf *m;
9070b3
 	int r;
9070b3
+	int handle;
9070b3
 
9070b3
 	debug3("%s entering command %s", __func__, command);
9070b3
 
9070b3
@@ -935,6 +953,30 @@ mm_audit_run_command(const char *command
9070b3
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
9070b3
 
9070b3
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
9070b3
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, m);
9070b3
+
9070b3
+	if ((r = sshbuf_get_u32(m, &handle)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+	sshbuf_free(m);
9070b3
+
9070b3
+	return (handle);
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+mm_audit_end_command(struct ssh *ssh, int handle, const char *command)
9070b3
+{
9070b3
+	int r;
9070b3
+	struct sshbuf *m;
9070b3
+
9070b3
+	debug3_f("entering command %s", command);
9070b3
+
9070b3
+ 	if ((m = sshbuf_new()) == NULL)
9070b3
+ 		fatal_f("sshbuf_new failed");
9070b3
+	if ((r = sshbuf_put_u32(m, handle)) != 0 ||
9070b3
+	    (r = sshbuf_put_cstring(m, command)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+
9070b3
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, m);
9070b3
 	sshbuf_free(m);
9070b3
 }
9070b3
 #endif /* SSH_AUDIT_EVENTS */
9070b3
@@ -1095,3 +1137,83 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_cc
9070b3
 }
9070b3
 
9070b3
 #endif /* GSSAPI */
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+void
9070b3
+mm_audit_unsupported_body(struct ssh *ssh, int what)
9070b3
+{
9070b3
+	int r;
9070b3
+	struct sshbuf *m;
9070b3
+
9070b3
+ 	if ((m = sshbuf_new()) == NULL)
9070b3
+ 		fatal_f("sshbuf_new failed");
9070b3
+	if ((r = sshbuf_put_u32(m, what)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+
9070b3
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, m);
9070b3
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
9070b3
+				  m);
9070b3
+
9070b3
+	sshbuf_free(m);
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+mm_audit_kex_body(struct ssh *ssh, int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
9070b3
+		  uid_t uid)
9070b3
+{
9070b3
+	int r;
9070b3
+	struct sshbuf *m;
9070b3
+
9070b3
+ 	if ((m = sshbuf_new()) == NULL)
9070b3
+ 		fatal_f("sshbuf_new failed");
9070b3
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
9070b3
+	    (r = sshbuf_put_cstring(m, cipher)) != 0 ||
9070b3
+	    (r = sshbuf_put_cstring(m, (mac ? mac : "<implicit>"))) != 0 ||
9070b3
+	    (r = sshbuf_put_cstring(m, compress)) != 0 ||
9070b3
+	    (r = sshbuf_put_cstring(m, fps)) != 0 ||
9070b3
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
9070b3
+	    (r = sshbuf_put_u64(m, uid)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+
9070b3
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, m);
9070b3
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
9070b3
+				  m);
9070b3
+
9070b3
+	sshbuf_free(m);
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+mm_audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+	int r;
9070b3
+	struct sshbuf *m;
9070b3
+
9070b3
+ 	if ((m = sshbuf_new()) == NULL)
9070b3
+ 		fatal_f("sshbuf_new failed");
9070b3
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
9070b3
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
9070b3
+	    (r = sshbuf_put_u64(m, uid)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+
9070b3
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
9070b3
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
9070b3
+				  m);
9070b3
+	sshbuf_free(m);
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+mm_audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
9070b3
+{
9070b3
+	int r;
9070b3
+	struct sshbuf *m;
9070b3
+
9070b3
+ 	if ((m = sshbuf_new()) == NULL)
9070b3
+ 		fatal_f("sshbuf_new failed");
9070b3
+	if ((r = sshbuf_put_cstring(m, fp)) != 0 ||
9070b3
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
9070b3
+	    (r = sshbuf_put_u64(m, uid)) != 0)
9070b3
+		fatal_fr(r, "buffer error");
9070b3
+
9070b3
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, m);
9070b3
+	sshbuf_free(m);
9070b3
+}
9070b3
+#endif /* SSH_AUDIT_EVENTS */
9070b3
diff -up openssh-8.6p1/monitor_wrap.h.audit openssh-8.6p1/monitor_wrap.h
9070b3
--- openssh-8.6p1/monitor_wrap.h.audit	2021-05-06 12:05:27.307463991 +0200
9070b3
+++ openssh-8.6p1/monitor_wrap.h	2021-05-06 12:05:27.381464563 +0200
9070b3
@@ -61,7 +61,9 @@ int mm_user_key_allowed(struct ssh *, st
9070b3
     struct sshauthopt **);
9070b3
 int mm_hostbased_key_allowed(struct ssh *, struct passwd *, const char *,
9070b3
     const char *, struct sshkey *);
9070b3
-int mm_sshkey_verify(const struct sshkey *, const u_char *, size_t,
9070b3
+int mm_hostbased_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
9070b3
+    const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **);
9070b3
+int mm_user_key_verify(struct ssh*, const struct sshkey *, const u_char *, size_t,
9070b3
     const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **);
9070b3
 
9070b3
 #ifdef GSSAPI
9070b3
@@ -86,7 +88,12 @@ void mm_sshpam_free_ctx(void *);
9070b3
 #ifdef SSH_AUDIT_EVENTS
9070b3
 #include "audit.h"
9070b3
 void mm_audit_event(struct ssh *, ssh_audit_event_t);
9070b3
-void mm_audit_run_command(const char *);
9070b3
+int mm_audit_run_command(struct ssh *ssh, const char *);
9070b3
+void mm_audit_end_command(struct ssh *ssh, int, const char *);
9070b3
+void mm_audit_unsupported_body(struct ssh *, int);
9070b3
+void mm_audit_kex_body(struct ssh *, int, char *, char *, char *, char *, pid_t, uid_t);
9070b3
+void mm_audit_session_key_free_body(struct ssh *, int, pid_t, uid_t);
9070b3
+void mm_audit_destroy_sensitive_data(struct ssh *, const char *, pid_t, uid_t);
9070b3
 #endif
9070b3
 
9070b3
 struct Session;
9070b3
diff -up openssh-8.6p1/packet.c.audit openssh-8.6p1/packet.c
9070b3
--- openssh-8.6p1/packet.c.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/packet.c	2021-05-06 12:07:38.535478683 +0200
9070b3
@@ -81,6 +81,7 @@
9070b3
 #endif
9070b3
 
9070b3
 #include "xmalloc.h"
9070b3
+#include "audit.h"
9070b3
 #include "compat.h"
9070b3
 #include "ssh2.h"
9070b3
 #include "cipher.h"
9070b3
@@ -506,6 +507,13 @@ ssh_packet_get_connection_out(struct ssh
9070b3
 	return ssh->state->connection_out;
9070b3
 }
9070b3
 
9070b3
+static int
9070b3
+packet_state_has_keys (const struct session_state *state)
9070b3
+{
9070b3
+	return state != NULL &&
9070b3
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
9070b3
+}
9070b3
+
9070b3
 /*
9070b3
  * Returns the IP-address of the remote host as a string.  The returned
9070b3
  * string must not be freed.
9070b3
@@ -583,22 +591,19 @@ ssh_packet_close_internal(struct ssh *ss
9070b3
 {
9070b3
 	struct session_state *state = ssh->state;
9070b3
 	u_int mode;
9070b3
+	u_int had_keys = packet_state_has_keys(state);
9070b3
 
9070b3
 	if (!state->initialized)
9070b3
 		return;
9070b3
 	state->initialized = 0;
9070b3
-	if (do_close) {
9070b3
-		if (state->connection_in == state->connection_out) {
9070b3
-			close(state->connection_out);
9070b3
-		} else {
9070b3
-			close(state->connection_in);
9070b3
-			close(state->connection_out);
9070b3
-		}
9070b3
-	}
9070b3
 	sshbuf_free(state->input);
9070b3
+	state->input = NULL;
9070b3
 	sshbuf_free(state->output);
9070b3
+	state->output = NULL;
9070b3
 	sshbuf_free(state->outgoing_packet);
9070b3
+	state->outgoing_packet = NULL;
9070b3
 	sshbuf_free(state->incoming_packet);
9070b3
+	state->incoming_packet = NULL;
9070b3
 	for (mode = 0; mode < MODE_MAX; mode++) {
9070b3
 		kex_free_newkeys(state->newkeys[mode]);	/* current keys */
9070b3
 		state->newkeys[mode] = NULL;
9070b3
@@ -634,8 +639,18 @@ ssh_packet_close_internal(struct ssh *ss
9070b3
 #endif	/* WITH_ZLIB */
9070b3
 	cipher_free(state->send_context);
9070b3
 	cipher_free(state->receive_context);
9070b3
+	if (had_keys && state->server_side) {
9070b3
+		/* Assuming this is called only from privsep child */
9070b3
+		audit_session_key_free(ssh, MODE_MAX);
9070b3
+	}
9070b3
 	state->send_context = state->receive_context = NULL;
9070b3
 	if (do_close) {
9070b3
+		if (state->connection_in == state->connection_out) {
9070b3
+			close(state->connection_out);
9070b3
+		} else {
9070b3
+			close(state->connection_in);
9070b3
+			close(state->connection_out);
9070b3
+		}
9070b3
 		free(ssh->local_ipaddr);
9070b3
 		ssh->local_ipaddr = NULL;
9070b3
 		free(ssh->remote_ipaddr);
9070b3
@@ -892,6 +907,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
9070b3
 		    (unsigned long long)state->p_send.bytes,
9070b3
 		    (unsigned long long)state->p_send.blocks);
9070b3
 		kex_free_newkeys(state->newkeys[mode]);
9070b3
+		audit_session_key_free(ssh, mode);
9070b3
 		state->newkeys[mode] = NULL;
9070b3
 	}
9070b3
 	/* note that both bytes and the seqnr are not reset */
9070b3
@@ -2173,6 +2189,72 @@ ssh_packet_get_output(struct ssh *ssh)
9070b3
 	return (void *)ssh->state->output;
9070b3
 }
9070b3
 
9070b3
+static void
9070b3
+newkeys_destroy_and_free(struct newkeys *newkeys)
9070b3
+{
9070b3
+	if (newkeys == NULL)
9070b3
+		return;
9070b3
+
9070b3
+	free(newkeys->enc.name);
9070b3
+
9070b3
+	if (newkeys->mac.enabled) {
9070b3
+		mac_clear(&newkeys->mac);
9070b3
+		free(newkeys->mac.name);
9070b3
+	}
9070b3
+
9070b3
+	free(newkeys->comp.name);
9070b3
+
9070b3
+	newkeys_destroy(newkeys);
9070b3
+	free(newkeys);
9070b3
+}
9070b3
+
9070b3
+static void
9070b3
+packet_destroy_state(struct session_state *state)
9070b3
+{
9070b3
+	if (state == NULL)
9070b3
+		return;
9070b3
+
9070b3
+	cipher_free(state->receive_context);
9070b3
+	cipher_free(state->send_context);
9070b3
+ 	state->send_context = state->receive_context = NULL;
9070b3
+
9070b3
+	sshbuf_free(state->input);
9070b3
+	state->input = NULL;
9070b3
+	sshbuf_free(state->output);
9070b3
+	state->output = NULL;
9070b3
+	sshbuf_free(state->outgoing_packet);
9070b3
+	state->outgoing_packet = NULL;
9070b3
+	sshbuf_free(state->incoming_packet);
9070b3
+	state->incoming_packet = NULL;
9070b3
+	if (state->compression_buffer) {
9070b3
+		sshbuf_free(state->compression_buffer);
9070b3
+		state->compression_buffer = NULL;
9070b3
+	}
9070b3
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
9070b3
+	state->newkeys[MODE_IN] = NULL;
9070b3
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
9070b3
+	state->newkeys[MODE_OUT] = NULL;
9070b3
+	mac_destroy(state->packet_discard_mac);
9070b3
+//	TAILQ_HEAD(, packet) outgoing;
9070b3
+//	memset(state, 0, sizeof(state));
9070b3
+}
9070b3
+
9070b3
+void
9070b3
+packet_destroy_all(struct ssh *ssh, int audit_it, int privsep)
9070b3
+{
9070b3
+	if (audit_it)
9070b3
+		audit_it = packet_state_has_keys(ssh->state);
9070b3
+	packet_destroy_state(ssh->state);
9070b3
+	if (audit_it) {
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+		if (privsep)
9070b3
+			audit_session_key_free(ssh, MODE_MAX);
9070b3
+		else
9070b3
+			audit_session_key_free_body(ssh, MODE_MAX, getpid(), getuid());
9070b3
+#endif
9070b3
+	}
9070b3
+}
9070b3
+
9070b3
 /* Reset after_authentication and reset compression in post-auth privsep */
9070b3
 static int
9070b3
 ssh_packet_set_postauth(struct ssh *ssh)
9070b3
diff -up openssh-8.6p1/packet.h.audit openssh-8.6p1/packet.h
9070b3
--- openssh-8.6p1/packet.h.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/packet.h	2021-05-06 12:05:27.382464571 +0200
9070b3
@@ -218,4 +218,5 @@ const u_char	*sshpkt_ptr(struct ssh *, s
9070b3
 # undef EC_POINT
9070b3
 #endif
9070b3
 
9070b3
+void	 packet_destroy_all(struct ssh *, int, int);
9070b3
 #endif				/* PACKET_H */
9070b3
diff -up openssh-8.6p1/session.c.audit openssh-8.6p1/session.c
9070b3
--- openssh-8.6p1/session.c.audit	2021-05-06 12:05:27.340464246 +0200
9070b3
+++ openssh-8.6p1/session.c	2021-05-06 12:05:27.383464578 +0200
9070b3
@@ -136,7 +136,7 @@ extern char *__progname;
9070b3
 extern int debug_flag;
9070b3
 extern u_int utmp_len;
9070b3
 extern int startup_pipe;
9070b3
-extern void destroy_sensitive_data(void);
9070b3
+extern void destroy_sensitive_data(struct ssh *, int);
9070b3
 extern struct sshbuf *loginmsg;
9070b3
 extern struct sshauthopt *auth_opts;
9070b3
 extern char *tun_fwd_ifnames; /* serverloop.c */
9070b3
@@ -644,6 +644,14 @@ do_exec_pty(struct ssh *ssh, Session *s,
9070b3
 	/* Parent.  Close the slave side of the pseudo tty. */
9070b3
 	close(ttyfd);
9070b3
 
9070b3
+#if !defined(HAVE_OSF_SIA) && defined(SSH_AUDIT_EVENTS)
9070b3
+	/* do_login in the child did not affect state in this process,
9070b3
+	   compensate.  From an architectural standpoint, this is extremely
9070b3
+	   ugly. */
9070b3
+	if (command != NULL)
9070b3
+		audit_count_session_open();
9070b3
+#endif
9070b3
+
9070b3
 	/* Enter interactive session. */
9070b3
 	s->ptymaster = ptymaster;
9070b3
 	ssh_packet_set_interactive(ssh, 1,
9070b3
@@ -736,15 +744,19 @@ do_exec(struct ssh *ssh, Session *s, con
9070b3
 	    s->self);
9070b3
 
9070b3
 #ifdef SSH_AUDIT_EVENTS
9070b3
+	if (s->command != NULL || s->command_handle != -1)
9070b3
+		fatal("do_exec: command already set");
9070b3
 	if (command != NULL)
9070b3
-		PRIVSEP(audit_run_command(command));
9070b3
+		s->command = xstrdup(command);
9070b3
 	else if (s->ttyfd == -1) {
9070b3
 		char *shell = s->pw->pw_shell;
9070b3
 
9070b3
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
9070b3
 			shell =_PATH_BSHELL;
9070b3
-		PRIVSEP(audit_run_command(shell));
9070b3
+		s->command = xstrdup(shell);
9070b3
 	}
9070b3
+	if (s->command != NULL && s->ptyfd == -1)
9070b3
+		s->command_handle = PRIVSEP(audit_run_command(ssh, s->command));
9070b3
 #endif
9070b3
 	if (s->ttyfd != -1)
9070b3
 		ret = do_exec_pty(ssh, s, command);
9070b3
@@ -1550,8 +1562,11 @@ do_child(struct ssh *ssh, Session *s, co
9070b3
 	sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
9070b3
 
9070b3
 	/* remove hostkey from the child's memory */
9070b3
-	destroy_sensitive_data();
9070b3
+	destroy_sensitive_data(ssh, 1);
9070b3
 	ssh_packet_clear_keys(ssh);
9070b3
+	/* Don't audit this - both us and the parent would be talking to the
9070b3
+	   monitor over a single socket, with no synchronization. */
9070b3
+	packet_destroy_all(ssh, 0, 1);
9070b3
 
9070b3
 	/* Force a password change */
9070b3
 	if (s->authctxt->force_pwchange) {
9070b3
@@ -1763,6 +1778,9 @@ session_unused(int id)
9070b3
 	sessions[id].ttyfd = -1;
9070b3
 	sessions[id].ptymaster = -1;
9070b3
 	sessions[id].x11_chanids = NULL;
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	sessions[id].command_handle = -1;
9070b3
+#endif
9070b3
 	sessions[id].next_unused = sessions_first_unused;
9070b3
 	sessions_first_unused = id;
9070b3
 }
9070b3
@@ -1843,6 +1861,19 @@ session_open(Authctxt *authctxt, int cha
9070b3
 }
9070b3
 
9070b3
 Session *
9070b3
+session_by_id(int id)
9070b3
+{
9070b3
+	if (id >= 0 && id < sessions_nalloc) {
9070b3
+		Session *s = &sessions[id];
9070b3
+		if (s->used)
9070b3
+			return s;
9070b3
+	}
9070b3
+	debug_f("unknown id %d", id);
9070b3
+	session_dump();
9070b3
+	return NULL;
9070b3
+}
9070b3
+
9070b3
+Session *
9070b3
 session_by_tty(char *tty)
9070b3
 {
9070b3
 	int i;
9070b3
@@ -2450,6 +2481,32 @@ session_exit_message(struct ssh *ssh, Se
9070b3
 		chan_write_failed(ssh, c);
9070b3
 }
9070b3
 
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+void
9070b3
+session_end_command2(struct ssh *ssh, Session *s)
9070b3
+{
9070b3
+	if (s->command != NULL) {
9070b3
+		if (s->command_handle != -1)
9070b3
+			audit_end_command(ssh, s->command_handle, s->command);
9070b3
+		free(s->command);
9070b3
+		s->command = NULL;
9070b3
+		s->command_handle = -1;
9070b3
+	}
9070b3
+}
9070b3
+
9070b3
+static void
9070b3
+session_end_command(struct ssh *ssh, Session *s)
9070b3
+{
9070b3
+	if (s->command != NULL) {
9070b3
+		if (s->command_handle != -1)
9070b3
+			PRIVSEP(audit_end_command(ssh, s->command_handle, s->command));
9070b3
+		free(s->command);
9070b3
+		s->command = NULL;
9070b3
+		s->command_handle = -1;
9070b3
+	}
9070b3
+}
9070b3
+#endif
9070b3
+
9070b3
 void
9070b3
 session_close(struct ssh *ssh, Session *s)
9070b3
 {
9070b3
@@ -2463,6 +2520,10 @@ session_close(struct ssh *ssh, Session *
9070b3
 
9070b3
 	if (s->ttyfd != -1)
9070b3
 		session_pty_cleanup(s);
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	if (s->command)
9070b3
+		session_end_command(ssh, s);
9070b3
+#endif
9070b3
 	free(s->term);
9070b3
 	free(s->display);
9070b3
 	free(s->x11_chanids);
9070b3
@@ -2537,14 +2598,14 @@ session_close_by_channel(struct ssh *ssh
9070b3
 }
9070b3
 
9070b3
 void
9070b3
-session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
9070b3
+session_destroy_all(struct ssh *ssh, void (*closefunc)(struct ssh *ssh, Session *))
9070b3
 {
9070b3
 	int i;
9070b3
 	for (i = 0; i < sessions_nalloc; i++) {
9070b3
 		Session *s = &sessions[i];
9070b3
 		if (s->used) {
9070b3
 			if (closefunc != NULL)
9070b3
-				closefunc(s);
9070b3
+				closefunc(ssh, s);
9070b3
 			else
9070b3
 				session_close(ssh, s);
9070b3
 		}
9070b3
@@ -2671,6 +2732,15 @@ do_authenticated2(struct ssh *ssh, Authc
9070b3
 	server_loop2(ssh, authctxt);
9070b3
 }
9070b3
 
9070b3
+static void
9070b3
+do_cleanup_one_session(struct ssh *ssh, Session *s)
9070b3
+{
9070b3
+	session_pty_cleanup2(s);
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	session_end_command2(ssh, s);
9070b3
+#endif
9070b3
+}
9070b3
+
9070b3
 void
9070b3
 do_cleanup(struct ssh *ssh, Authctxt *authctxt)
9070b3
 {
9070b3
@@ -2734,7 +2804,7 @@ do_cleanup(struct ssh *ssh, Authctxt *au
9070b3
 	 * or if running in monitor.
9070b3
 	 */
9070b3
 	if (!use_privsep || mm_is_monitor())
9070b3
-		session_destroy_all(ssh, session_pty_cleanup2);
9070b3
+		session_destroy_all(ssh, do_cleanup_one_session);
9070b3
 }
9070b3
 
9070b3
 /* Return a name for the remote host that fits inside utmp_size */
9070b3
diff -up openssh-8.6p1/session.h.audit openssh-8.6p1/session.h
9070b3
--- openssh-8.6p1/session.h.audit	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/session.h	2021-05-06 12:05:27.384464586 +0200
9070b3
@@ -61,6 +61,12 @@ struct Session {
9070b3
 		char	*name;
9070b3
 		char	*val;
9070b3
 	} *env;
9070b3
+
9070b3
+	/* exec */
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	int	command_handle;
9070b3
+	char	*command;
9070b3
+#endif
9070b3
 };
9070b3
 
9070b3
 void	 do_authenticated(struct ssh *, Authctxt *);
9070b3
@@ -71,10 +77,12 @@ void	 session_unused(int);
9070b3
 int	 session_input_channel_req(struct ssh *, Channel *, const char *);
9070b3
 void	 session_close_by_pid(struct ssh *ssh, pid_t, int);
9070b3
 void	 session_close_by_channel(struct ssh *, int, void *);
9070b3
-void	 session_destroy_all(struct ssh *, void (*)(Session *));
9070b3
+void	 session_destroy_all(struct ssh *, void (*)(struct ssh*, Session *));
9070b3
 void	 session_pty_cleanup2(Session *);
9070b3
+void	 session_end_command2(struct ssh *ssh, Session *);
9070b3
 
9070b3
 Session	*session_new(void);
9070b3
+Session *session_by_id(int);
9070b3
 Session	*session_by_tty(char *);
9070b3
 void	 session_close(struct ssh *, Session *);
9070b3
 void	 do_setusercontext(struct passwd *);
9070b3
diff -up openssh-8.6p1/sshd.c.audit openssh-8.6p1/sshd.c
9070b3
--- openssh-8.6p1/sshd.c.audit	2021-05-06 12:05:27.346464292 +0200
9070b3
+++ openssh-8.6p1/sshd.c	2021-05-06 12:05:27.385464594 +0200
9070b3
@@ -122,6 +122,7 @@
9070b3
 #include "ssh-gss.h"
9070b3
 #endif
9070b3
 #include "monitor_wrap.h"
9070b3
+#include "audit.h"
9070b3
 #include "ssh-sandbox.h"
9070b3
 #include "auth-options.h"
9070b3
 #include "version.h"
9070b3
@@ -260,8 +261,8 @@ struct sshbuf *loginmsg;
9070b3
 struct passwd *privsep_pw = NULL;
9070b3
 
9070b3
 /* Prototypes for various functions defined later in this file. */
9070b3
-void destroy_sensitive_data(void);
9070b3
-void demote_sensitive_data(void);
9070b3
+void destroy_sensitive_data(struct ssh *, int);
9070b3
+void demote_sensitive_data(struct ssh *);
9070b3
 static void do_ssh2_kex(struct ssh *);
9070b3
 
9070b3
 static char *listener_proctitle;
9070b3
@@ -279,6 +280,15 @@ close_listen_socks(void)
9070b3
 	num_listen_socks = -1;
9070b3
 }
9070b3
 
9070b3
+/*
9070b3
+ * Is this process listening for clients (i.e. not specific to any specific
9070b3
+ * client connection?)
9070b3
+ */
9070b3
+int listening_for_clients(void)
9070b3
+{
9070b3
+	return num_listen_socks >= 0;
9070b3
+}
9070b3
+
9070b3
 static void
9070b3
 close_startup_pipes(void)
9070b3
 {
9070b3
@@ -377,18 +387,45 @@ grace_alarm_handler(int sig)
9070b3
 	}
9070b3
 }
9070b3
 
9070b3
-/* Destroy the host and server keys.  They will no longer be needed. */
9070b3
+/*
9070b3
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
9070b3
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
9070b3
+ */
9070b3
 void
9070b3
-destroy_sensitive_data(void)
9070b3
+destroy_sensitive_data(struct ssh *ssh, int privsep)
9070b3
 {
9070b3
 	u_int i;
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	pid_t pid;
9070b3
+	uid_t uid;
9070b3
 
9070b3
+	pid = getpid();
9070b3
+	uid = getuid();
9070b3
+#endif
9070b3
 	for (i = 0; i < options.num_host_key_files; i++) {
9070b3
 		if (sensitive_data.host_keys[i]) {
9070b3
+			char *fp;
9070b3
+
9070b3
+			if (sshkey_is_private(sensitive_data.host_keys[i]))
9070b3
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
9070b3
+			else
9070b3
+				fp = NULL;
9070b3
 			sshkey_free(sensitive_data.host_keys[i]);
9070b3
 			sensitive_data.host_keys[i] = NULL;
9070b3
+			if (fp != NULL) {
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+				if (privsep)
9070b3
+					PRIVSEP(audit_destroy_sensitive_data(ssh, fp,
9070b3
+						pid, uid));
9070b3
+				else
9070b3
+					audit_destroy_sensitive_data(ssh, fp,
9070b3
+						pid, uid);
9070b3
+#endif
9070b3
+				free(fp);
9070b3
+			}
9070b3
 		}
9070b3
-		if (sensitive_data.host_certificates[i]) {
9070b3
+		if (sensitive_data.host_certificates
9070b3
+		    && sensitive_data.host_certificates[i]) {
9070b3
 			sshkey_free(sensitive_data.host_certificates[i]);
9070b3
 			sensitive_data.host_certificates[i] = NULL;
9070b3
 		}
9070b3
@@ -397,20 +434,38 @@ destroy_sensitive_data(void)
9070b3
 
9070b3
 /* Demote private to public keys for network child */
9070b3
 void
9070b3
-demote_sensitive_data(void)
9070b3
+demote_sensitive_data(struct ssh *ssh)
9070b3
 {
9070b3
 	struct sshkey *tmp;
9070b3
 	u_int i;
9070b3
 	int r;
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+	pid_t pid;
9070b3
+	uid_t uid;
9070b3
 
9070b3
+	pid = getpid();
9070b3
+	uid = getuid();
9070b3
+#endif
9070b3
 	for (i = 0; i < options.num_host_key_files; i++) {
9070b3
 		if (sensitive_data.host_keys[i]) {
9070b3
+			char *fp;
9070b3
+
9070b3
+			if (sshkey_is_private(sensitive_data.host_keys[i]))
9070b3
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
9070b3
+			else
9070b3
+				fp = NULL;
9070b3
 			if ((r = sshkey_from_private(
9070b3
 			    sensitive_data.host_keys[i], &tmp)) != 0)
9070b3
 				fatal_r(r, "could not demote host %s key",
9070b3
 				    sshkey_type(sensitive_data.host_keys[i]));
9070b3
 			sshkey_free(sensitive_data.host_keys[i]);
9070b3
 			sensitive_data.host_keys[i] = tmp;
9070b3
+			if (fp != NULL) {
9070b3
+#ifdef SSH_AUDIT_EVENTS
9070b3
+				audit_destroy_sensitive_data(ssh, fp, pid, uid);
9070b3
+#endif
9070b3
+				free(fp);
9070b3
+			}
9070b3
 		}
9070b3
 		/* Certs do not need demotion */
9070b3
 	}
9070b3
@@ -438,7 +493,7 @@ reseed_prngs(void)
9070b3
 }
9070b3
 
9070b3
 static void
9070b3
-privsep_preauth_child(void)
9070b3
+privsep_preauth_child(struct ssh *ssh)
9070b3
 {
9070b3
 	gid_t gidset[1];
9070b3
 
9070b3
@@ -453,7 +508,7 @@ privsep_preauth_child(void)
9070b3
 	reseed_prngs();
9070b3
 
9070b3
 	/* Demote the private keys to public keys. */
9070b3
-	demote_sensitive_data();
9070b3
+	demote_sensitive_data(ssh);
9070b3
 
9070b3
 #ifdef WITH_SELINUX
9070b3
 	sshd_selinux_change_privsep_preauth_context();
9070b3
@@ -492,7 +547,7 @@ privsep_preauth(struct ssh *ssh)
9070b3
 
9070b3
 	if (use_privsep == PRIVSEP_ON)
9070b3
 		box = ssh_sandbox_init(pmonitor);
9070b3
-	pid = fork();
9070b3
+	pmonitor->m_pid = pid = fork();
9070b3
 	if (pid == -1) {
9070b3
 		fatal("fork of unprivileged child failed");
9070b3
 	} else if (pid != 0) {
9070b3
@@ -537,7 +592,7 @@ privsep_preauth(struct ssh *ssh)
9070b3
 		/* Arrange for logging to be sent to the monitor */
9070b3
 		set_log_handler(mm_log_handler, pmonitor);
9070b3
 
9070b3
-		privsep_preauth_child();
9070b3
+		privsep_preauth_child(ssh);
9070b3
 		setproctitle("%s", "[net]");
9070b3
 		if (box != NULL)
9070b3
 			ssh_sandbox_child(box);
9070b3
@@ -589,7 +644,7 @@ privsep_postauth(struct ssh *ssh, Authct
9070b3
 		set_log_handler(mm_log_handler, pmonitor);
9070b3
 
9070b3
 	/* Demote the private keys to public keys. */
9070b3
-	demote_sensitive_data();
9070b3
+	demote_sensitive_data(ssh);
9070b3
 
9070b3
 	reseed_prngs();
9070b3
 
9070b3
@@ -1143,7 +1198,7 @@ server_listen(void)
9070b3
  * from this function are in a forked subprocess.
9070b3
  */
9070b3
 static void
9070b3
-server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
9070b3
+server_accept_loop(struct ssh *ssh, int *sock_in, int *sock_out, int *newsock, int *config_s)
9070b3
 {
9070b3
 	fd_set *fdset;
9070b3
 	int i, j, ret, maxfd;
9070b3
@@ -1204,6 +1259,7 @@ server_accept_loop(int *sock_in, int *so
9070b3
 		if (received_sigterm) {
9070b3
 			logit("Received signal %d; terminating.",
9070b3
 			    (int) received_sigterm);
9070b3
+			destroy_sensitive_data(ssh, 0);
9070b3
 			close_listen_socks();
9070b3
 			if (options.pid_file != NULL)
9070b3
 				unlink(options.pid_file);
9070b3
@@ -2098,7 +2154,7 @@ main(int ac, char **av)
9070b3
 #endif
9070b3
 
9070b3
 		/* Accept a connection and return in a forked child */
9070b3
-		server_accept_loop(&sock_in, &sock_out,
9070b3
+		server_accept_loop(ssh, &sock_in, &sock_out,
9070b3
 		    &newsock, config_s);
9070b3
 	}
9070b3
 
9070b3
@@ -2333,6 +2389,9 @@ main(int ac, char **av)
9070b3
 	do_authenticated(ssh, authctxt);
9070b3
 
9070b3
 	/* The connection has been terminated. */
9070b3
+	packet_destroy_all(ssh, 1, 1);
9070b3
+	destroy_sensitive_data(ssh, 1);
9070b3
+
9070b3
 	ssh_packet_get_bytes(ssh, &ibytes, &obytes);
9070b3
 	verbose("Transferred: sent %llu, received %llu bytes",
9070b3
 	    (unsigned long long)obytes, (unsigned long long)ibytes);
9070b3
@@ -2513,6 +2572,15 @@ do_ssh2_kex(struct ssh *ssh)
9070b3
 void
9070b3
 cleanup_exit(int i)
9070b3
 {
9070b3
+	static int in_cleanup = 0;
9070b3
+	int is_privsep_child;
9070b3
+
9070b3
+	/* cleanup_exit can be called at the very least from the privsep
9070b3
+	   wrappers used for auditing.  Make sure we don't recurse
9070b3
+	   indefinitely. */
9070b3
+	if (in_cleanup)
9070b3
+		_exit(i);
9070b3
+	in_cleanup = 1;
9070b3
 	if (the_active_state != NULL && the_authctxt != NULL) {
9070b3
 		do_cleanup(the_active_state, the_authctxt);
9070b3
 		if (use_privsep && privsep_is_preauth &&
9070b3
@@ -2525,9 +2593,16 @@ cleanup_exit(int i)
9070b3
 			}
9070b3
 		}
9070b3
 	}
9070b3
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
9070b3
+	if (sensitive_data.host_keys != NULL && the_active_state != NULL)
9070b3
+		destroy_sensitive_data(the_active_state, is_privsep_child);
9070b3
+	if (the_active_state != NULL)
9070b3
+		packet_destroy_all(the_active_state, 1, is_privsep_child);
9070b3
 #ifdef SSH_AUDIT_EVENTS
9070b3
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
9070b3
-	if (the_active_state != NULL && (!use_privsep || mm_is_monitor()))
9070b3
+	if (the_active_state != NULL &&
9070b3
+	    (the_authctxt == NULL || !the_authctxt->authenticated) &&
9070b3
+	    (!use_privsep || mm_is_monitor()))
9070b3
 		audit_event(the_active_state, SSH_CONNECTION_ABANDON);
9070b3
 #endif
9070b3
 	_exit(i);
9070b3
diff -up openssh-8.6p1/sshkey.c.audit openssh-8.6p1/sshkey.c
9070b3
--- openssh-8.6p1/sshkey.c.audit	2021-05-06 12:05:27.364464431 +0200
9070b3
+++ openssh-8.6p1/sshkey.c	2021-05-06 12:05:27.386464602 +0200
9070b3
@@ -371,6 +371,38 @@ sshkey_type_is_valid_ca(int type)
9070b3
 }
9070b3
 
9070b3
 int
9070b3
+sshkey_is_private(const struct sshkey *k)
9070b3
+{
9070b3
+      switch (k->type) {
9070b3
+#ifdef WITH_OPENSSL
9070b3
+      case KEY_RSA_CERT:
9070b3
+      case KEY_RSA: {
9070b3
+              const BIGNUM *d;
9070b3
+              RSA_get0_key(k->rsa, NULL, NULL, &d);
9070b3
+              return d != NULL;
9070b3
+          }
9070b3
+      case KEY_DSA_CERT:
9070b3
+      case KEY_DSA: {
9070b3
+              const BIGNUM *priv_key;
9070b3
+              DSA_get0_key(k->dsa, NULL, &priv_key);
9070b3
+              return priv_key != NULL;
9070b3
+          }
9070b3
+#ifdef OPENSSL_HAS_ECC
9070b3
+      case KEY_ECDSA_CERT:
9070b3
+      case KEY_ECDSA:
9070b3
+              return EC_KEY_get0_private_key(k->ecdsa) != NULL;
9070b3
+#endif /* OPENSSL_HAS_ECC */
9070b3
+#endif /* WITH_OPENSSL */
9070b3
+      case KEY_ED25519_CERT:
9070b3
+      case KEY_ED25519:
9070b3
+              return (k->ed25519_pk != NULL);
9070b3
+      default:
9070b3
+              /* fatal("key_is_private: bad key type %d", k->type); */
9070b3
+              return 0;
9070b3
+      }
9070b3
+}
9070b3
+
9070b3
+int
9070b3
 sshkey_is_cert(const struct sshkey *k)
9070b3
 {
9070b3
 	if (k == NULL)
9070b3
diff -up openssh-8.6p1/sshkey.h.audit openssh-8.6p1/sshkey.h
9070b3
--- openssh-8.6p1/sshkey.h.audit	2021-05-06 12:05:27.365464439 +0200
9070b3
+++ openssh-8.6p1/sshkey.h	2021-05-06 12:05:27.386464602 +0200
9070b3
@@ -189,6 +189,7 @@ int		 sshkey_shield_private(struct sshke
9070b3
 int		 sshkey_unshield_private(struct sshkey *);
9070b3
 
9070b3
 int	 sshkey_type_from_name(const char *);
9070b3
+int	 sshkey_is_private(const struct sshkey *);
9070b3
 int	 sshkey_is_cert(const struct sshkey *);
9070b3
 int	 sshkey_is_sk(const struct sshkey *);
9070b3
 int	 sshkey_type_is_cert(int);