rcolebaugh / rpms / openssh

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