jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone
017ff1
diff --git a/Makefile.in b/Makefile.in
017ff1
index 0f0d39f..9d8c2eb 100644
017ff1
--- a/Makefile.in
017ff1
+++ b/Makefile.in
017ff1
@@ -82,7 +82,8 @@ LIBSSH_OBJS=authfd.o authfile.o bufaux.o bufbn.o buffer.o \
017ff1
 	ssh-pkcs11.o krl.o smult_curve25519_ref.o \
017ff1
 	kexc25519.o kexc25519c.o poly1305.o chacha.o cipher-chachapoly.o \
017ff1
 	ssh-ed25519.o digest-openssl.o hmac.o utf8_stringprep.o \
017ff1
-	sc25519.o ge25519.o fe25519.o ed25519.o verify.o hash.o blocks.o
017ff1
+	sc25519.o ge25519.o fe25519.o ed25519.o verify.o hash.o blocks.o \
017ff1
+	auditstub.o
f09e2e
 
f09e2e
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
f09e2e
 	sshconnect.o sshconnect1.o sshconnect2.o mux.o \
017ff1
diff --git a/audit-bsm.c b/audit-bsm.c
017ff1
index 6135591..5160869 100644
017ff1
--- a/audit-bsm.c
017ff1
+++ b/audit-bsm.c
017ff1
@@ -375,10 +375,23 @@ audit_connection_from(const char *host, int port)
f09e2e
 #endif
f09e2e
 }
f09e2e
 
f09e2e
-void
f09e2e
+int
f09e2e
 audit_run_command(const char *command)
f09e2e
 {
f09e2e
 	/* not implemented */
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_end_command(int handle, const char *command)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_count_session_open(void)
f09e2e
+{
f09e2e
+	/* not necessary */
f09e2e
 }
f09e2e
 
f09e2e
 void
017ff1
@@ -393,6 +406,12 @@ audit_session_close(struct logininfo *li)
f09e2e
 	/* not implemented */
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
 void
f09e2e
 audit_event(ssh_audit_event_t event)
f09e2e
 {
f09e2e
@@ -454,4 +473,40 @@ audit_event(ssh_audit_event_t event)
f09e2e
 		debug("%s: unhandled event %d", __func__, event);
f09e2e
 	}
f09e2e
 }
f09e2e
+
f09e2e
+void
f09e2e
+audit_unsupported_body(int what)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_destroy_sensitive_data(const char *fp)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_generate_ephemeral_server_key(const char *fp)
f09e2e
+{
f09e2e
+	/* not implemented */
f09e2e
+}
f09e2e
 #endif /* BSM */
017ff1
diff --git a/audit-linux.c b/audit-linux.c
017ff1
index b3ee2f4..946f7fa 100644
017ff1
--- a/audit-linux.c
017ff1
+++ b/audit-linux.c
f09e2e
@@ -35,13 +35,24 @@
f09e2e
 
f09e2e
 #include "log.h"
f09e2e
 #include "audit.h"
f09e2e
+#include "key.h"
f09e2e
+#include "hostfile.h"
f09e2e
+#include "auth.h"
f09e2e
+#include "servconf.h"
f09e2e
 #include "canohost.h"
f09e2e
+#include "packet.h"
f09e2e
+#include "cipher.h"
f09e2e
 
017ff1
+#define AUDIT_LOG_SIZE 256
f09e2e
+
f09e2e
+extern ServerOptions options;
f09e2e
+extern Authctxt *the_authctxt;
f09e2e
+extern u_int utmp_len;
f09e2e
 const char* audit_username(void);
f09e2e
 
f09e2e
-int
f09e2e
-linux_audit_record_event(int uid, const char *username,
f09e2e
-    const char *hostname, const char *ip, const char *ttyn, int success)
f09e2e
+static void
f09e2e
+linux_audit_user_logxxx(int uid, const char *username,
f09e2e
+    const char *hostname, const char *ip, const char *ttyn, int success, int event)
f09e2e
 {
f09e2e
 	int audit_fd, rc, saved_errno;
f09e2e
 
017ff1
@@ -49,11 +60,11 @@ linux_audit_record_event(int uid, const char *username,
f09e2e
 	if (audit_fd < 0) {
f09e2e
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
 		    errno == EAFNOSUPPORT)
f09e2e
-			return 1; /* No audit support in kernel */
f09e2e
+			return; /* No audit support in kernel */
f09e2e
 		else
f09e2e
-			return 0; /* Must prevent login */
f09e2e
+			goto fatal_report; /* Must prevent login */
f09e2e
 	}
f09e2e
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
f09e2e
+	rc = audit_log_acct_message(audit_fd, event,
f09e2e
 	    NULL, "login", username ? username : "(unknown)",
f09e2e
 	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
f09e2e
 	saved_errno = errno;
017ff1
@@ -65,35 +76,150 @@ linux_audit_record_event(int uid, const char *username,
f09e2e
 	if ((rc == -EPERM) && (geteuid() != 0))
f09e2e
 		rc = 0;
f09e2e
 	errno = saved_errno;
f09e2e
-	return (rc >= 0);
f09e2e
+	if (rc < 0) {
f09e2e
+fatal_report:
f09e2e
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
f09e2e
+	}
f09e2e
 }
f09e2e
 
f09e2e
+static void
f09e2e
+linux_audit_user_auth(int uid, const char *username,
f09e2e
+    const char *hostname, const char *ip, const char *ttyn, int success, int event)
f09e2e
+{
f09e2e
+	int audit_fd, rc, saved_errno;
f09e2e
+	static const char *event_name[] = {
f09e2e
+		"maxtries exceeded",
f09e2e
+		"root denied",
f09e2e
+		"success",
f09e2e
+		"none",
f09e2e
+		"password",
f09e2e
+		"challenge-response",
f09e2e
+		"pubkey",
f09e2e
+		"hostbased",
f09e2e
+		"gssapi",
f09e2e
+		"invalid user",
f09e2e
+		"nologin",
f09e2e
+		"connection closed",
f09e2e
+		"connection abandoned",
f09e2e
+		"unknown"
f09e2e
+	};
f09e2e
+
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
+		    errno == EAFNOSUPPORT)
f09e2e
+			return; /* No audit support in kernel */
f09e2e
+		else
f09e2e
+			goto fatal_report; /* Must prevent login */
f09e2e
+	}
f09e2e
+	
f09e2e
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
f09e2e
+		event = SSH_AUDIT_UNKNOWN;
f09e2e
+
f09e2e
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
f09e2e
+	    NULL, event_name[event], username ? username : "(unknown)",
f09e2e
+	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
f09e2e
+	saved_errno = errno;
f09e2e
+	close(audit_fd);
f09e2e
+	/*
f09e2e
+	 * Do not report error if the error is EPERM and sshd is run as non
f09e2e
+	 * root user.
f09e2e
+	 */
f09e2e
+	if ((rc == -EPERM) && (geteuid() != 0))
f09e2e
+		rc = 0;
f09e2e
+	errno = saved_errno;
f09e2e
+	if (rc < 0) {
f09e2e
+fatal_report:
f09e2e
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
f09e2e
+	}
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
f09e2e
+{
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, rc, saved_errno;
f09e2e
+
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
+					 errno == EAFNOSUPPORT)
f09e2e
+			return 1; /* No audit support in kernel */
f09e2e
+		else                                                                                                                                       
f09e2e
+			return 0; /* Must prevent login */
f09e2e
+	}
f09e2e
+	snprintf(buf, sizeof(buf), "%s_auth rport=%d", host_user ? "pubkey" : "hostbased", get_remote_port());
f09e2e
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
f09e2e
+		buf, audit_username(), -1, NULL, get_remote_ipaddr(), NULL, rv);
f09e2e
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
f09e2e
+		goto out;
f09e2e
+	snprintf(buf, sizeof(buf), "key algo=%s size=%d fp=%s%s rport=%d",
f09e2e
+			type, bits, key_fingerprint_prefix(), fp, get_remote_port());
f09e2e
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
f09e2e
+		buf, audit_username(), -1, NULL, get_remote_ipaddr(), NULL, rv);
f09e2e
+out:
f09e2e
+	saved_errno = errno;
f09e2e
+	audit_close(audit_fd);
f09e2e
+	errno = saved_errno;
f09e2e
+	/* do not report error if the error is EPERM and sshd is run as non root user */
f09e2e
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
f09e2e
+}
f09e2e
+
f09e2e
+static int user_login_count = 0;
f09e2e
+
f09e2e
 /* Below is the sshd audit API code */
f09e2e
 
f09e2e
 void
f09e2e
 audit_connection_from(const char *host, int port)
f09e2e
 {
f09e2e
-}
f09e2e
 	/* not implemented */
f09e2e
+}
f09e2e
 
f09e2e
-void
f09e2e
+int
f09e2e
 audit_run_command(const char *command)
f09e2e
 {
f09e2e
-	/* not implemented */
f09e2e
+	if (!user_login_count++) 
f09e2e
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+		    NULL, "ssh", 1, AUDIT_USER_LOGIN);
f09e2e
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+	    NULL, "ssh", 1, AUDIT_USER_START);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_end_command(int handle, const char *command)
f09e2e
+{
f09e2e
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+	    NULL, "ssh", 1, AUDIT_USER_END);
f09e2e
+	if (user_login_count && !--user_login_count) 
f09e2e
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+		    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_count_session_open(void)
f09e2e
+{
f09e2e
+	user_login_count++;
f09e2e
 }
f09e2e
 
f09e2e
 void
f09e2e
 audit_session_open(struct logininfo *li)
f09e2e
 {
f09e2e
-	if (linux_audit_record_event(li->uid, NULL, li->hostname,
f09e2e
-	    NULL, li->line, 1) == 0)
f09e2e
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
f09e2e
+	if (!user_login_count++) 
f09e2e
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f09e2e
+		    NULL, li->line, 1, AUDIT_USER_LOGIN);
f09e2e
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f09e2e
+	    NULL, li->line, 1, AUDIT_USER_START);
f09e2e
 }
f09e2e
 
f09e2e
 void
f09e2e
 audit_session_close(struct logininfo *li)
f09e2e
 {
f09e2e
-	/* not implemented */
f09e2e
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f09e2e
+	    NULL, li->line, 1, AUDIT_USER_END);
f09e2e
+	if (user_login_count && !--user_login_count) 
f09e2e
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f09e2e
+		    NULL, li->line, 1, AUDIT_USER_LOGOUT);
f09e2e
 }
f09e2e
 
f09e2e
 void
f09e2e
@@ -101,21 +227,43 @@ audit_event(ssh_audit_event_t event)
f09e2e
 {
f09e2e
 	switch(event) {
f09e2e
 	case SSH_AUTH_SUCCESS:
f09e2e
-	case SSH_CONNECTION_CLOSE:
f09e2e
+		linux_audit_user_auth(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 1, event);
f09e2e
+		break;
f09e2e
+
f09e2e
 	case SSH_NOLOGIN:
f09e2e
-	case SSH_LOGIN_EXCEED_MAXTRIES:
f09e2e
 	case SSH_LOGIN_ROOT_DENIED:
f09e2e
+		linux_audit_user_auth(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 0, event);
f09e2e
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 0, AUDIT_USER_LOGIN);
f09e2e
 		break;
f09e2e
 
f09e2e
+	case SSH_LOGIN_EXCEED_MAXTRIES:
f09e2e
 	case SSH_AUTH_FAIL_NONE:
f09e2e
 	case SSH_AUTH_FAIL_PASSWD:
f09e2e
 	case SSH_AUTH_FAIL_KBDINT:
f09e2e
 	case SSH_AUTH_FAIL_PUBKEY:
f09e2e
 	case SSH_AUTH_FAIL_HOSTBASED:
f09e2e
 	case SSH_AUTH_FAIL_GSSAPI:
f09e2e
+		linux_audit_user_auth(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 0, event);
f09e2e
+		break;
f09e2e
+
f09e2e
+	case SSH_CONNECTION_CLOSE:
f09e2e
+		if (user_login_count) {
f09e2e
+			while (user_login_count--)
f09e2e
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+				    NULL, "ssh", 1, AUDIT_USER_END);
f09e2e
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
f09e2e
+			    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
f09e2e
+		}
f09e2e
+		break;
f09e2e
+
f09e2e
+	case SSH_CONNECTION_ABANDON:
f09e2e
 	case SSH_INVALID_USER:
f09e2e
-		linux_audit_record_event(-1, audit_username(), NULL,
f09e2e
-			get_remote_ipaddr(), "sshd", 0);
f09e2e
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
f09e2e
+			get_remote_ipaddr(), "ssh", 0, AUDIT_USER_LOGIN);
f09e2e
 		break;
f09e2e
 
f09e2e
 	default:
f09e2e
@@ -123,4 +271,135 @@ audit_event(ssh_audit_event_t event)
f09e2e
 	}
f09e2e
 }
f09e2e
 
f09e2e
+void
f09e2e
+audit_unsupported_body(int what)
f09e2e
+{
f09e2e
+#ifdef AUDIT_CRYPTO_SESSION
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	const static char *name[] = { "cipher", "mac", "comp" };
f09e2e
+	char *s;
f09e2e
+	int audit_fd;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
f09e2e
+		name[what], get_remote_port(), (s = get_local_ipaddr(packet_get_connection_in())),
f09e2e
+		get_local_port());
f09e2e
+	free(s);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0)
f09e2e
+		/* no problem, the next instruction will be fatal() */
f09e2e
+		return;
f09e2e
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
f09e2e
+			buf, NULL, get_remote_ipaddr(), NULL, 0);
f09e2e
+	audit_close(audit_fd);
f09e2e
+#endif
f09e2e
+}
f09e2e
+
f09e2e
+const static char *direction[] = { "from-server", "from-client", "both" };
f09e2e
+
f09e2e
+void
f09e2e
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, pid_t pid,
f09e2e
+	       uid_t uid)
f09e2e
+{
f09e2e
+#ifdef AUDIT_CRYPTO_SESSION
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, audit_ok;
f09e2e
+	Cipher *cipher = cipher_by_name(enc);
f09e2e
+	char *s;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d mac=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
f09e2e
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac,
f09e2e
+		(intmax_t)pid, (intmax_t)uid,
f09e2e
+		get_remote_port(), (s = get_local_ipaddr(packet_get_connection_in())), get_local_port());
f09e2e
+	free(s);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
+					 errno == EAFNOSUPPORT)
f09e2e
+			return; /* No audit support in kernel */
f09e2e
+		else                                                                                                                                       
f09e2e
+			fatal("cannot open audit"); /* Must prevent login */
f09e2e
+	}
f09e2e
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
f09e2e
+			buf, NULL, get_remote_ipaddr(), NULL, 1);
f09e2e
+	audit_close(audit_fd);
f09e2e
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f09e2e
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f09e2e
+		fatal("cannot write into audit"); /* Must prevent login */
f09e2e
+#endif
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, audit_ok;
f09e2e
+	char *s;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
f09e2e
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
f09e2e
+		 get_remote_port(),
f09e2e
+		 (s = get_local_ipaddr(packet_get_connection_in())),
f09e2e
+		 get_local_port());
f09e2e
+	free(s);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
f09e2e
+					 errno != EAFNOSUPPORT)
f09e2e
+			error("cannot open audit");
f09e2e
+		return;
f09e2e
+	}
f09e2e
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
f09e2e
+			buf, NULL, get_remote_ipaddr(), NULL, 1);
f09e2e
+	audit_close(audit_fd);
f09e2e
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f09e2e
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f09e2e
+		error("cannot write into audit");
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, audit_ok;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
f09e2e
+		fp, (intmax_t)pid, (intmax_t)uid);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
f09e2e
+					 errno != EAFNOSUPPORT)
f09e2e
+			error("cannot open audit");
f09e2e
+		return;
f09e2e
+	}
f09e2e
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
f09e2e
+			buf, NULL,
f09e2e
+			listening_for_clients() ? NULL : get_remote_ipaddr(),
f09e2e
+			NULL, 1);
f09e2e
+	audit_close(audit_fd);
f09e2e
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f09e2e
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f09e2e
+		error("cannot write into audit");
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_generate_ephemeral_server_key(const char *fp)
f09e2e
+{
f09e2e
+	char buf[AUDIT_LOG_SIZE];
f09e2e
+	int audit_fd, audit_ok;
f09e2e
+
f09e2e
+	snprintf(buf, sizeof(buf), "op=create kind=server fp=%s direction=? ", fp);
f09e2e
+	audit_fd = audit_open();
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
f09e2e
+					 errno != EAFNOSUPPORT)
f09e2e
+			error("cannot open audit");
f09e2e
+		return;
f09e2e
+	}
f09e2e
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
f09e2e
+			buf, NULL, 0, NULL, 1);
f09e2e
+	audit_close(audit_fd);
f09e2e
+	/* do not abort if the error is EPERM and sshd is run as non root user */
f09e2e
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
f09e2e
+		error("cannot write into audit");
f09e2e
+}
f09e2e
 #endif /* USE_LINUX_AUDIT */
017ff1
diff --git a/audit.c b/audit.c
017ff1
index ced57fa..13c6849 100644
017ff1
--- a/audit.c
017ff1
+++ b/audit.c
f09e2e
@@ -28,6 +28,7 @@
f09e2e
 
f09e2e
 #include <stdarg.h>
f09e2e
 #include <string.h>
f09e2e
+#include <unistd.h>
f09e2e
 
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
 
f09e2e
@@ -36,6 +37,9 @@
f09e2e
 #include "key.h"
f09e2e
 #include "hostfile.h"
f09e2e
 #include "auth.h"
f09e2e
+#include "ssh-gss.h"
f09e2e
+#include "monitor_wrap.h"
f09e2e
+#include "xmalloc.h"
f09e2e
 
f09e2e
 /*
f09e2e
  * Care must be taken when using this since it WILL NOT be initialized when
017ff1
@@ -71,13 +75,10 @@ audit_classify_auth(const char *method)
017ff1
 const char *
017ff1
 audit_username(void)
017ff1
 {
017ff1
-	static const char unknownuser[] = "(unknown user)";
017ff1
-	static const char invaliduser[] = "(invalid user)";
017ff1
+	static const char unknownuser[] = "(unknown)";
017ff1
 
017ff1
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
017ff1
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
017ff1
 		return (unknownuser);
017ff1
-	if (!the_authctxt->valid)
017ff1
-		return (invaliduser);
017ff1
 	return (the_authctxt->user);
017ff1
 }
017ff1
 
017ff1
@@ -111,6 +112,40 @@ audit_event_lookup(ssh_audit_event_t ev)
f09e2e
 	return(event_lookup[i].name);
f09e2e
 }
f09e2e
 
f09e2e
+void
f09e2e
+audit_key(int host_user, int *rv, const Key *key)
f09e2e
+{
f09e2e
+	char *fp;
f09e2e
+	const char *crypto_name;
f09e2e
+
f09e2e
+	fp = key_selected_fingerprint(key, SSH_FP_HEX);
f09e2e
+	if (key->type == KEY_RSA1)
f09e2e
+		crypto_name = "ssh-rsa1";
f09e2e
+	else
f09e2e
+		crypto_name = key_ssh_name(key);
f09e2e
+	if (audit_keyusage(host_user, crypto_name, key_size(key), fp, *rv) == 0)
f09e2e
+		*rv = 0;
f09e2e
+	free(fp);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_unsupported(int what)
f09e2e
+{
f09e2e
+	PRIVSEP(audit_unsupported_body(what));
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_kex(int ctos, char *enc, char *mac, char *comp)
f09e2e
+{
f09e2e
+	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, getpid(), getuid()));
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free(int ctos)
f09e2e
+{
f09e2e
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
f09e2e
+}
f09e2e
+
f09e2e
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
f09e2e
 /*
f09e2e
  * Null implementations of audit functions.
017ff1
@@ -140,6 +175,17 @@ audit_event(ssh_audit_event_t event)
f09e2e
 }
f09e2e
 
f09e2e
 /*
f09e2e
+ * Called when a child process has called, or will soon call,
f09e2e
+ * audit_session_open.
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_count_session_open(void)
f09e2e
+{
f09e2e
+	debug("audit count session open euid %d user %s", geteuid(),
f09e2e
+	      audit_username());
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
  * Called when a user session is started.  Argument is the tty allocated to
f09e2e
  * the session, or NULL if no tty was allocated.
f09e2e
  *
017ff1
@@ -174,13 +220,91 @@ audit_session_close(struct logininfo *li)
f09e2e
 /*
f09e2e
  * This will be called when a user runs a non-interactive command.  Note that
f09e2e
  * it may be called multiple times for a single connection since SSH2 allows
f09e2e
- * multiple sessions within a single connection.
f09e2e
+ * multiple sessions within a single connection.  Returns a "handle" for
f09e2e
+ * audit_end_command.
f09e2e
  */
f09e2e
-void
f09e2e
+int
f09e2e
 audit_run_command(const char *command)
f09e2e
 {
f09e2e
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
f09e2e
 	    audit_username(), command);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called when the non-interactive command finishes.  Note that
f09e2e
+ * it may be called multiple times for a single connection since SSH2 allows
f09e2e
+ * multiple sessions within a single connection.  "handle" should come from
f09e2e
+ * the corresponding audit_run_command.
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_end_command(int handle, const char *command)
f09e2e
+{
f09e2e
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
f09e2e
+	    audit_username(), command);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
f09e2e
+ *
f09e2e
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
f09e2e
+ */
f09e2e
+int
f09e2e
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
f09e2e
+{
f09e2e
+	debug("audit %s key usage euid %d user %s key type %s key length %d fingerprint %s%s, result %d", 
f09e2e
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(), type, bits,
f09e2e
+		key_fingerprint_prefix(), fp, rv);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called when the protocol negotiation fails.
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_unsupported_body(int what)
f09e2e
+{
f09e2e
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called on succesfull protocol negotiation.
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, pid_t pid,
f09e2e
+	       uid_t uid)
f09e2e
+{
f09e2e
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s from pid %ld uid %u",
f09e2e
+		(unsigned)geteuid(), ctos, enc, mac, compress, (long)pid,
f09e2e
+	        (unsigned)uid);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called on succesfull session key discard
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
f09e2e
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called on destroy private part of the server key
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
f09e2e
+		geteuid(), fp, (long)pid, (unsigned)uid);
f09e2e
+}
f09e2e
+
f09e2e
+/*
f09e2e
+ * This will be called on generation of the ephemeral server key
f09e2e
+ */
f09e2e
+void
f09e2e
+audit_generate_ephemeral_server_key(const char *)
f09e2e
+{
f09e2e
+	debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp);
f09e2e
 }
f09e2e
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
f09e2e
 #endif /* SSH_AUDIT_EVENTS */
017ff1
diff --git a/audit.h b/audit.h
017ff1
index 92ede5b..a2dc3ff 100644
017ff1
--- a/audit.h
017ff1
+++ b/audit.h
f09e2e
@@ -28,6 +28,7 @@
f09e2e
 # define _SSH_AUDIT_H
f09e2e
 
f09e2e
 #include "loginrec.h"
f09e2e
+#include "key.h"
f09e2e
 
f09e2e
 enum ssh_audit_event_type {
f09e2e
 	SSH_LOGIN_EXCEED_MAXTRIES,
f09e2e
@@ -47,11 +48,25 @@ enum ssh_audit_event_type {
f09e2e
 };
f09e2e
 typedef enum ssh_audit_event_type ssh_audit_event_t;
f09e2e
 
f09e2e
+int	listening_for_clients(void);
f09e2e
+
f09e2e
 void	audit_connection_from(const char *, int);
f09e2e
 void	audit_event(ssh_audit_event_t);
f09e2e
+void	audit_count_session_open(void);
f09e2e
 void	audit_session_open(struct logininfo *);
f09e2e
 void	audit_session_close(struct logininfo *);
f09e2e
-void	audit_run_command(const char *);
f09e2e
+int	audit_run_command(const char *);
f09e2e
+void 	audit_end_command(int, const char *);
f09e2e
 ssh_audit_event_t audit_classify_auth(const char *);
f09e2e
+int	audit_keyusage(int, const char *, unsigned, char *, int);
f09e2e
+void	audit_key(int, int *, const Key *);
f09e2e
+void	audit_unsupported(int);
f09e2e
+void	audit_kex(int, char *, char *, char *);
f09e2e
+void	audit_unsupported_body(int);
f09e2e
+void	audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
f09e2e
+void	audit_session_key_free(int ctos);
f09e2e
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
f09e2e
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
f09e2e
+void	audit_generate_ephemeral_server_key(const char *);
f09e2e
 
f09e2e
 #endif /* _SSH_AUDIT_H */
017ff1
diff --git a/auditstub.c b/auditstub.c
017ff1
new file mode 100644
017ff1
index 0000000..45817e0
017ff1
--- /dev/null
017ff1
+++ b/auditstub.c
f09e2e
@@ -0,0 +1,50 @@
f09e2e
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
f09e2e
+
f09e2e
+/*
f09e2e
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
f09e2e
+ * Use is subject to license terms.
f09e2e
+ *
f09e2e
+ * Redistribution and use in source and binary forms, with or without
f09e2e
+ * modification, are permitted provided that the following conditions
f09e2e
+ * are met:
f09e2e
+ * 1. Redistributions of source code must retain the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer.
f09e2e
+ * 2. Redistributions in binary form must reproduce the above copyright
f09e2e
+ *    notice, this list of conditions and the following disclaimer in the
f09e2e
+ *    documentation and/or other materials provided with the distribution.
f09e2e
+ *
f09e2e
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
f09e2e
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f09e2e
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
f09e2e
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
f09e2e
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
f09e2e
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
f09e2e
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
f09e2e
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
f09e2e
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
f09e2e
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f09e2e
+ *
f09e2e
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
f09e2e
+ */
f09e2e
+
f09e2e
+#include <sys/types.h>
f09e2e
+
f09e2e
+void
f09e2e
+audit_unsupported(int n)
f09e2e
+{
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_kex(int ctos, char *enc, char *mac, char *comp)
f09e2e
+{
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free(int ctos)
f09e2e
+{
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+}
017ff1
diff --git a/auth-rsa.c b/auth-rsa.c
017ff1
index 5dad6c3..f225b0b 100644
017ff1
--- a/auth-rsa.c
017ff1
+++ b/auth-rsa.c
017ff1
@@ -93,7 +93,10 @@ auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
f09e2e
 {
f09e2e
 	u_char buf[32], mdbuf[16];
017ff1
 	struct ssh_digest_ctx *md;
f09e2e
-	int len;
f09e2e
+	int len, rv;
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	char *fp;
f09e2e
+#endif
f09e2e
 
f09e2e
 	/* don't allow short keys */
f09e2e
 	if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
017ff1
@@ -117,12 +120,18 @@ auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
017ff1
 	ssh_digest_free(md);
f09e2e
 
f09e2e
 	/* Verify that the response is the original challenge. */
f09e2e
-	if (timingsafe_bcmp(response, mdbuf, 16) != 0) {
f09e2e
-		/* Wrong answer. */
f09e2e
-		return (0);
f09e2e
+	rv = timingsafe_bcmp(response, mdbuf, 16) == 0;
f09e2e
+
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	fp = key_selected_fingerprint(key, SSH_FP_HEX);
f09e2e
+	if (audit_keyusage(1, "ssh-rsa1", RSA_size(key->rsa) * 8, fp, rv) == 0) {
f09e2e
+		debug("unsuccessful audit");
f09e2e
+		rv = 0;
f09e2e
 	}
f09e2e
-	/* Correct answer. */
f09e2e
-	return (1);
f09e2e
+	free(fp);
f09e2e
+#endif
f09e2e
+
f09e2e
+	return rv;
f09e2e
 }
f09e2e
 
f09e2e
 /*
017ff1
diff --git a/auth.c b/auth.c
017ff1
index 420a85b..d613f8c 100644
017ff1
--- a/auth.c
017ff1
+++ b/auth.c
017ff1
@@ -628,9 +628,6 @@ getpwnamallow(const char *user)
017ff1
 		record_failed_login(user,
017ff1
 		    get_canonical_hostname(options.use_dns), "ssh");
017ff1
 #endif
017ff1
-#ifdef SSH_AUDIT_EVENTS
017ff1
-		audit_event(SSH_INVALID_USER);
017ff1
-#endif /* SSH_AUDIT_EVENTS */
017ff1
 		return (NULL);
017ff1
 	}
017ff1
 	if (!allowed_user(pw))
017ff1
diff --git a/auth.h b/auth.h
017ff1
index 4605588..f9d191c 100644
017ff1
--- a/auth.h
017ff1
+++ b/auth.h
017ff1
@@ -186,6 +186,7 @@ void	abandon_challenge_response(Authctxt *);
f09e2e
 
f09e2e
 char	*expand_authorized_keys(const char *, struct passwd *pw);
f09e2e
 char	*authorized_principals_file(struct passwd *);
f09e2e
+int	 user_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
f09e2e
 
f09e2e
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
f09e2e
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
017ff1
@@ -203,6 +204,7 @@ Key	*get_hostkey_private_by_type(int);
f09e2e
 int	 get_hostkey_index(Key *);
f09e2e
 int	 ssh1_session_key(BIGNUM *);
f09e2e
 void	 sshd_hostkey_sign(Key *, Key *, u_char **, u_int *, u_char *, u_int);
f09e2e
+int	 hostbased_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
f09e2e
 
f09e2e
 /* debug messages during authentication */
f09e2e
 void	 auth_debug_add(const char *fmt,...) __attribute__((format(printf, 1, 2)));
017ff1
diff --git a/auth2-hostbased.c b/auth2-hostbased.c
017ff1
index 95d678e..48aede4 100644
017ff1
--- a/auth2-hostbased.c
017ff1
+++ b/auth2-hostbased.c
017ff1
@@ -137,7 +137,7 @@ userauth_hostbased(Authctxt *authctxt)
f09e2e
 	/* test for allowed key and correct signature */
f09e2e
 	authenticated = 0;
f09e2e
 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
f09e2e
-	    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
f09e2e
+	    PRIVSEP(hostbased_key_verify(key, sig, slen, buffer_ptr(&b),
f8987c
 			buffer_len(&b))) == 1) {
f09e2e
 		authenticated = 1;
f8987c
 		authctxt->last_details = pubkey;
017ff1
@@ -154,6 +154,18 @@ done:
f09e2e
 	return authenticated;
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+hostbased_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
f09e2e
+{
f09e2e
+	int rv;
f09e2e
+
f09e2e
+	rv = key_verify(key, sig, slen, data, datalen);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	audit_key(0, &rv, key);
f09e2e
+#endif
f09e2e
+	return rv;
f09e2e
+}
f09e2e
+
f09e2e
 /* return 1 if given hostkey is allowed */
f09e2e
 int
f09e2e
 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
017ff1
diff --git a/auth2-pubkey.c b/auth2-pubkey.c
017ff1
index cb0f931..6d1c872 100644
017ff1
--- a/auth2-pubkey.c
017ff1
+++ b/auth2-pubkey.c
017ff1
@@ -160,7 +160,7 @@ userauth_pubkey(Authctxt *authctxt)
f09e2e
 		/* test for correct signature */
f09e2e
 		authenticated = 0;
f09e2e
 		if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
f09e2e
-		    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
f09e2e
+		    PRIVSEP(user_key_verify(key, sig, slen, buffer_ptr(&b),
f8987c
 		    buffer_len(&b))) == 1) {
f8987c
 			authctxt->last_details = pubkey;
f09e2e
 			authenticated = 1;
017ff1
@@ -231,6 +231,18 @@ pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
f09e2e
 	free(extra);
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+user_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
f09e2e
+{
f09e2e
+	int rv;
f09e2e
+
f09e2e
+	rv = key_verify(key, sig, slen, data, datalen);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	audit_key(1, &rv, key);
f09e2e
+#endif
f09e2e
+	return rv;
f09e2e
+}
f09e2e
+
f09e2e
 static int
f09e2e
 match_principals_option(const char *principal_list, struct KeyCert *cert)
f09e2e
 {
017ff1
diff --git a/auth2.c b/auth2.c
017ff1
index 426dcd6..436cd60 100644
017ff1
--- a/auth2.c
017ff1
+++ b/auth2.c
017ff1
@@ -249,9 +249,6 @@ input_userauth_request(int type, u_int32_t seq, void *ctxt)
f09e2e
 		} else {
f09e2e
 			logit("input_userauth_request: invalid user %s", user);
f09e2e
 			authctxt->pw = fakepw();
f09e2e
-#ifdef SSH_AUDIT_EVENTS
f09e2e
-			PRIVSEP(audit_event(SSH_INVALID_USER));
f09e2e
-#endif
f09e2e
 		}
f09e2e
 #ifdef USE_PAM
f09e2e
 		if (options.use_pam)
017ff1
diff --git a/cipher.c b/cipher.c
017ff1
index 53d9b4f..226e56d 100644
017ff1
--- a/cipher.c
017ff1
+++ b/cipher.c
017ff1
@@ -57,20 +57,6 @@ extern const EVP_CIPHER *evp_ssh1_bf(void);
f09e2e
 extern const EVP_CIPHER *evp_ssh1_3des(void);
f09e2e
 extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
f09e2e
 
f09e2e
-struct Cipher {
f09e2e
-	char	*name;
f09e2e
-	int	number;		/* for ssh1 only */
f09e2e
-	u_int	block_size;
f09e2e
-	u_int	key_len;
f09e2e
-	u_int	iv_len;		/* defaults to block_size */
f09e2e
-	u_int	auth_len;
f09e2e
-	u_int	discard_len;
017ff1
-	u_int	flags;
017ff1
-#define CFLAG_CBC		(1<<0)
017ff1
-#define CFLAG_CHACHAPOLY	(1<<1)
f09e2e
-	const EVP_CIPHER	*(*evptype)(void);
f09e2e
-};
f09e2e
-
f09e2e
 static const struct Cipher ciphers[] = {
f09e2e
 	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
f09e2e
 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
017ff1
diff --git a/cipher.h b/cipher.h
017ff1
index 133d2e7..d41758e 100644
017ff1
--- a/cipher.h
017ff1
+++ b/cipher.h
017ff1
@@ -63,7 +63,20 @@
f09e2e
 typedef struct Cipher Cipher;
f09e2e
 typedef struct CipherContext CipherContext;
f09e2e
 
f09e2e
-struct Cipher;
f09e2e
+struct Cipher {
f09e2e
+	char	*name;
f09e2e
+	int	number;		/* for ssh1 only */
f09e2e
+	u_int	block_size;
f09e2e
+	u_int	key_len;
f09e2e
+	u_int	iv_len;		/* defaults to block_size */
f09e2e
+	u_int	auth_len;
f09e2e
+	u_int	discard_len;
017ff1
+	u_int	flags;
017ff1
+#define CFLAG_CBC		(1<<0)
017ff1
+#define CFLAG_CHACHAPOLY	(1<<1)
f09e2e
+	const EVP_CIPHER	*(*evptype)(void);
f09e2e
+};
f09e2e
+
f09e2e
 struct CipherContext {
f09e2e
 	int	plaintext;
f09e2e
 	int	encrypt;
017ff1
diff --git a/kex.c b/kex.c
017ff1
index bce2ab8..bc3e53e 100644
017ff1
--- a/kex.c
017ff1
+++ b/kex.c
017ff1
@@ -50,6 +50,7 @@
f09e2e
 #include "monitor.h"
f09e2e
 #include "roaming.h"
017ff1
 #include "digest.h"
f09e2e
+#include "audit.h"
f09e2e
 
017ff1
 #ifdef GSSAPI
017ff1
 #include "ssh-gss.h"
017ff1
@@ -366,9 +367,13 @@ static void
f09e2e
 choose_enc(Enc *enc, char *client, char *server)
f09e2e
 {
f09e2e
 	char *name = match_list(client, server, NULL);
f09e2e
-	if (name == NULL)
f09e2e
+	if (name == NULL) {
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		audit_unsupported(0);
f09e2e
+#endif
f09e2e
 		fatal("no matching cipher found: client %s server %s",
f09e2e
 		    client, server);
f09e2e
+	}
f09e2e
 	if ((enc->cipher = cipher_by_name(name)) == NULL)
f09e2e
 		fatal("matching cipher is not supported: %s", name);
f09e2e
 	enc->name = name;
017ff1
@@ -384,9 +389,13 @@ static void
f09e2e
 choose_mac(Mac *mac, char *client, char *server)
f09e2e
 {
f09e2e
 	char *name = match_list(client, server, NULL);
f09e2e
-	if (name == NULL)
f09e2e
+	if (name == NULL) {
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		audit_unsupported(1);
f09e2e
+#endif
f09e2e
 		fatal("no matching mac found: client %s server %s",
f09e2e
 		    client, server);
f09e2e
+	}
f09e2e
 	if (mac_setup(mac, name) < 0)
f09e2e
 		fatal("unsupported mac %s", name);
f09e2e
 	/* truncate the key */
017ff1
@@ -401,8 +410,12 @@ static void
f09e2e
 choose_comp(Comp *comp, char *client, char *server)
f09e2e
 {
f09e2e
 	char *name = match_list(client, server, NULL);
f09e2e
-	if (name == NULL)
f09e2e
+	if (name == NULL) {
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		audit_unsupported(2);
f09e2e
+#endif
f09e2e
 		fatal("no matching comp found: client %s server %s", client, server);
f09e2e
+	}
f09e2e
 	if (strcmp(name, "zlib@openssh.com") == 0) {
f09e2e
 		comp->type = COMP_DELAYED;
f09e2e
 	} else if (strcmp(name, "zlib") == 0) {
017ff1
@@ -517,6 +530,9 @@ kex_choose_conf(Kex *kex)
f09e2e
 		    newkeys->enc.name,
f09e2e
 		    authlen == 0 ? newkeys->mac.name : "<implicit>",
f09e2e
 		    newkeys->comp.name);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		audit_kex(ctos, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name);
f09e2e
+#endif
f09e2e
 	}
f09e2e
 	choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
f09e2e
 	choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
674526
@@ -702,3 +718,53 @@ dump_digest(char *msg, u_char *digest, int len)
f09e2e
 	fprintf(stderr, "\n");
f09e2e
 }
f09e2e
 #endif
f09e2e
+
f09e2e
+static void
f09e2e
+enc_destroy(Enc *enc)
f09e2e
+{
f09e2e
+	if (enc == NULL)
f09e2e
+		return;
f09e2e
+
f09e2e
+	if (enc->key) {
f09e2e
+		memset(enc->key, 0, enc->key_len);
f09e2e
+		free(enc->key);
f09e2e
+	}
f09e2e
+
f09e2e
+	if (enc->iv) {
f09e2e
+		memset(enc->iv,  0, enc->block_size);
f09e2e
+		free(enc->iv);
f09e2e
+	}
f09e2e
+
f09e2e
+	memset(enc, 0, sizeof(*enc));
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+newkeys_destroy(Newkeys *newkeys)
f09e2e
+{
f09e2e
+	if (newkeys == NULL)
f09e2e
+		return;
f09e2e
+
674526
+	free(newkeys->enc.name);
f09e2e
+	enc_destroy(&newkeys->enc);
674526
+
674526
+	if (newkeys->mac.enabled) {
674526
+		mac_clear(&newkeys->mac);
674526
+		free(newkeys->mac.name);
674526
+		mac_destroy(&newkeys->mac);
674526
+	}
674526
+
674526
+	free(newkeys->comp.name);
674526
+
f09e2e
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
f09e2e
+}
f09e2e
+
674526
+void
674526
+newkeys_destroy_and_free(Newkeys *newkeys)
674526
+{
674526
+	if (newkeys == NULL)
674526
+		return;
674526
+
674526
+	newkeys_destroy(newkeys);
674526
+	free(newkeys);
674526
+}
674526
+
017ff1
diff --git a/kex.h b/kex.h
017ff1
index 313bb51..c643250 100644
017ff1
--- a/kex.h
017ff1
+++ b/kex.h
674526
@@ -182,6 +182,9 @@ void	 kexgss_client(Kex *);
017ff1
 void	 kexgss_server(Kex *);
017ff1
 #endif
f09e2e
 
f09e2e
+void	newkeys_destroy(Newkeys *newkeys);
674526
+void	newkeys_destroy_and_free(Newkeys *newkeys);
f09e2e
+
f09e2e
 void
f09e2e
 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
f09e2e
     BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
017ff1
diff --git a/key.c b/key.c
017ff1
index 900b9e3..62f3edb 100644
017ff1
--- a/key.c
017ff1
+++ b/key.c
017ff1
@@ -1925,6 +1925,33 @@ key_demote(const Key *k)
f09e2e
 }
f09e2e
 
f09e2e
 int
f09e2e
+key_is_private(const Key *k)
f09e2e
+{
f09e2e
+	switch (k->type) {
f09e2e
+	case KEY_RSA_CERT_V00:
f09e2e
+	case KEY_RSA_CERT:
f09e2e
+	case KEY_RSA1:
f09e2e
+	case KEY_RSA:
f09e2e
+		return k->rsa->d != NULL;
f09e2e
+	case KEY_DSA_CERT_V00:
f09e2e
+	case KEY_DSA_CERT:
f09e2e
+	case KEY_DSA:
f09e2e
+		return k->dsa->priv_key != NULL;
f09e2e
+#ifdef OPENSSL_HAS_ECC
f09e2e
+	case KEY_ECDSA_CERT:
f09e2e
+	case KEY_ECDSA:
f09e2e
+		return EC_KEY_get0_private_key(k->ecdsa) != NULL;
f09e2e
+#endif
017ff1
+	case KEY_ED25519_CERT:
017ff1
+	case KEY_ED25519:
017ff1
+		return (k->ed25519_pk != NULL);
f09e2e
+	default:
f09e2e
+		fatal("key_is_private: bad key type %d", k->type);
f09e2e
+		return 1;
f09e2e
+	}
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
 key_is_cert(const Key *k)
f09e2e
 {
f09e2e
 	if (k == NULL)
017ff1
diff --git a/key.h b/key.h
017ff1
index d51ed81..8f61605 100644
017ff1
--- a/key.h
017ff1
+++ b/key.h
017ff1
@@ -118,6 +118,7 @@ Key	*key_generate(int, u_int);
f09e2e
 Key	*key_from_private(const Key *);
f09e2e
 int	 key_type_from_name(char *);
f09e2e
 int	 key_is_cert(const Key *);
f09e2e
+int	 key_is_private(const Key *k);
017ff1
 int	 key_type_is_cert(int);
f09e2e
 int	 key_type_plain(int);
f09e2e
 int	 key_to_certified(Key *, int);
017ff1
diff --git a/mac.c b/mac.c
017ff1
index 0977572..9388af4 100644
017ff1
--- a/mac.c
017ff1
+++ b/mac.c
017ff1
@@ -222,6 +222,20 @@ mac_clear(Mac *mac)
f09e2e
 	mac->umac_ctx = NULL;
f09e2e
 }
f09e2e
 
f09e2e
+void
f09e2e
+mac_destroy(Mac *mac)
f09e2e
+{
f09e2e
+	if (mac == NULL)
f09e2e
+		return;
f09e2e
+
f09e2e
+	if (mac->key) {
f09e2e
+		memset(mac->key, 0, mac->key_len);
f09e2e
+		free(mac->key);
f09e2e
+	}
f09e2e
+
f09e2e
+	memset(mac, 0, sizeof(*mac));
f09e2e
+}
f09e2e
+
f09e2e
 /* XXX copied from ciphers_valid */
f09e2e
 #define	MAC_SEP	","
f09e2e
 int
017ff1
diff --git a/mac.h b/mac.h
017ff1
index fbe18c4..7dc7f43 100644
017ff1
--- a/mac.h
017ff1
+++ b/mac.h
f09e2e
@@ -29,3 +29,4 @@ int	 mac_setup(Mac *, char *);
f09e2e
 int	 mac_init(Mac *);
f09e2e
 u_char	*mac_compute(Mac *, u_int32_t, u_char *, int);
f09e2e
 void	 mac_clear(Mac *);
f09e2e
+void	 mac_destroy(Mac *);
017ff1
diff --git a/monitor.c b/monitor.c
017ff1
index 8b18086..5a65114 100644
017ff1
--- a/monitor.c
017ff1
+++ b/monitor.c
017ff1
@@ -97,6 +97,7 @@
017ff1
 #include "ssh2.h"
f09e2e
 #include "roaming.h"
f09e2e
 #include "authfd.h"
f09e2e
+#include "audit.h"
f09e2e
 
f09e2e
 #ifdef GSSAPI
f09e2e
 static Gssctxt *gsscontext = NULL;
017ff1
@@ -113,6 +114,8 @@ extern Buffer auth_debug;
f09e2e
 extern int auth_debug_init;
f09e2e
 extern Buffer loginmsg;
f09e2e
 
f09e2e
+extern void destroy_sensitive_data(int);
f09e2e
+
f09e2e
 /* State exported from the child */
f09e2e
 
f09e2e
 struct {
017ff1
@@ -185,6 +188,11 @@ int mm_answer_gss_updatecreds(int, Buffer *);
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
 int mm_answer_audit_event(int, Buffer *);
f09e2e
 int mm_answer_audit_command(int, Buffer *);
f09e2e
+int mm_answer_audit_end_command(int, Buffer *);
f09e2e
+int mm_answer_audit_unsupported_body(int, Buffer *);
f09e2e
+int mm_answer_audit_kex_body(int, Buffer *);
f09e2e
+int mm_answer_audit_session_key_free_body(int, Buffer *);
f09e2e
+int mm_answer_audit_server_key_free(int, Buffer *);
f09e2e
 #endif
f09e2e
 
f09e2e
 static int monitor_read_log(struct monitor *);
017ff1
@@ -239,6 +247,10 @@ struct mon_table mon_dispatch_proto20[] = {
f09e2e
 #endif
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f09e2e
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f09e2e
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f09e2e
 #endif
f09e2e
 #ifdef BSD_AUTH
f09e2e
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
017ff1
@@ -274,6 +286,11 @@ struct mon_table mon_dispatch_postauth20[] = {
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f09e2e
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
f09e2e
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
f09e2e
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f09e2e
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f09e2e
 #endif
f09e2e
     {0, 0, NULL}
f09e2e
 };
017ff1
@@ -305,6 +322,10 @@ struct mon_table mon_dispatch_proto15[] = {
f09e2e
 #endif
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f09e2e
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f09e2e
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f09e2e
 #endif
f09e2e
     {0, 0, NULL}
f09e2e
 };
017ff1
@@ -316,6 +337,11 @@ struct mon_table mon_dispatch_postauth15[] = {
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
f09e2e
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
f09e2e
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
f09e2e
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
f09e2e
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
f09e2e
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
f09e2e
 #endif
f09e2e
     {0, 0, NULL}
f09e2e
 };
017ff1
@@ -1393,9 +1419,11 @@ mm_answer_keyverify(int sock, Buffer *m)
f09e2e
 	Key *key;
f09e2e
 	u_char *signature, *data, *blob;
f09e2e
 	u_int signaturelen, datalen, bloblen;
f09e2e
+	int type = 0;
f09e2e
 	int verified = 0;
f09e2e
 	int valid_data = 0;
f09e2e
 
f09e2e
+	type = buffer_get_int(m);
f09e2e
 	blob = buffer_get_string(m, &bloblen);
f09e2e
 	signature = buffer_get_string(m, &signaturelen);
f09e2e
 	data = buffer_get_string(m, &datalen);
017ff1
@@ -1403,6 +1431,8 @@ mm_answer_keyverify(int sock, Buffer *m)
f09e2e
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
f09e2e
 	  !monitor_allowed_key(blob, bloblen))
f09e2e
 		fatal("%s: bad key, not previously allowed", __func__);
f09e2e
+	if (type != key_blobtype)
f09e2e
+		fatal("%s: bad key type", __func__);
f09e2e
 
f09e2e
 	key = key_from_blob(blob, bloblen);
f09e2e
 	if (key == NULL)
017ff1
@@ -1423,7 +1453,17 @@ mm_answer_keyverify(int sock, Buffer *m)
f09e2e
 	if (!valid_data)
f09e2e
 		fatal("%s: bad signature data blob", __func__);
f09e2e
 
f09e2e
-	verified = key_verify(key, signature, signaturelen, data, datalen);
f09e2e
+	switch (key_blobtype) {
f09e2e
+	case MM_USERKEY:
f09e2e
+		verified = user_key_verify(key, signature, signaturelen, data, datalen);
f09e2e
+		break;
f09e2e
+	case MM_HOSTKEY:
f09e2e
+		verified = hostbased_key_verify(key, signature, signaturelen, data, datalen);
f09e2e
+		break;
f09e2e
+	default:
f09e2e
+		verified = 0;
f09e2e
+		break;
f09e2e
+	}
f09e2e
 	debug3("%s: key %p signature %s",
f09e2e
 	    __func__, key, (verified == 1) ? "verified" : "unverified");
f09e2e
 
017ff1
@@ -1476,6 +1516,12 @@ mm_session_close(Session *s)
f09e2e
 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
f09e2e
 		session_pty_cleanup2(s);
f09e2e
 	}
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	if (s->command != NULL) {
f09e2e
+		debug3("%s: command %d", __func__, s->command_handle);
f09e2e
+		session_end_command2(s);
f09e2e
+	}
f09e2e
+#endif
f09e2e
 	session_unused(s->self);
f09e2e
 }
f09e2e
 
017ff1
@@ -1756,6 +1802,8 @@ mm_answer_term(int sock, Buffer *req)
f09e2e
 		sshpam_cleanup();
f09e2e
 #endif
f09e2e
 
f09e2e
+	destroy_sensitive_data(0);
f09e2e
+
f09e2e
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
f09e2e
 		if (errno != EINTR)
f09e2e
 			exit(1);
017ff1
@@ -1798,11 +1846,43 @@ mm_answer_audit_command(int socket, Buffer *m)
f09e2e
 {
f09e2e
 	u_int len;
f09e2e
 	char *cmd;
f09e2e
+	Session *s;
f09e2e
 
f09e2e
 	debug3("%s entering", __func__);
f09e2e
 	cmd = buffer_get_string(m, &len;;
f09e2e
+
f09e2e
 	/* sanity check command, if so how? */
f09e2e
-	audit_run_command(cmd);
f09e2e
+	s = session_new();
f09e2e
+	if (s == NULL)
f09e2e
+		fatal("%s: error allocating a session", __func__);
f09e2e
+	s->command = cmd;
f09e2e
+	s->command_handle = audit_run_command(cmd);
f09e2e
+
f09e2e
+	buffer_clear(m);
f09e2e
+	buffer_put_int(m, s->self);
f09e2e
+
f09e2e
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
f09e2e
+
f09e2e
+	return (0);
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_answer_audit_end_command(int socket, Buffer *m)
f09e2e
+{
f09e2e
+	int handle;
f09e2e
+	u_int len;
f09e2e
+	char *cmd;
f09e2e
+	Session *s;
f09e2e
+
f09e2e
+	debug3("%s entering", __func__);
f09e2e
+	handle = buffer_get_int(m);
f09e2e
+	cmd = buffer_get_string(m, &len;;
f09e2e
+
f09e2e
+	s = session_by_id(handle);
f09e2e
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
f09e2e
+	    strcmp(s->command, cmd) != 0)
f09e2e
+		fatal("%s: invalid handle", __func__);
f09e2e
+	mm_session_close(s);
f09e2e
 	free(cmd);
f09e2e
 	return (0);
f09e2e
 }
017ff1
@@ -1946,11 +2026,13 @@ mm_get_keystate(struct monitor *pmonitor)
f09e2e
 
f09e2e
 	blob = buffer_get_string(&m, &bloblen);
f09e2e
 	current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
f09e2e
+	memset(blob, 0, bloblen);
f09e2e
 	free(blob);
f09e2e
 
f09e2e
 	debug3("%s: Waiting for second key", __func__);
f09e2e
 	blob = buffer_get_string(&m, &bloblen);
f09e2e
 	current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
f09e2e
+	memset(blob, 0, bloblen);
f09e2e
 	free(blob);
f09e2e
 
f09e2e
 	/* Now get sequence numbers for the packets */
017ff1
@@ -1996,6 +2078,21 @@ mm_get_keystate(struct monitor *pmonitor)
f09e2e
 	}
f09e2e
 
f09e2e
 	buffer_free(&m);
f09e2e
+
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	if (compat20) {
f09e2e
+		buffer_init(&m);
f09e2e
+		mm_request_receive_expect(pmonitor->m_sendfd,
f09e2e
+					  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
f09e2e
+		mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, &m);
f09e2e
+		buffer_free(&m);
f09e2e
+	}
f09e2e
+#endif
f09e2e
+
f09e2e
+	/* Drain any buffered messages from the child */
f09e2e
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
f09e2e
+		;
f09e2e
+
f09e2e
 }
f09e2e
 
f09e2e
 
674526
@@ -2277,3 +2374,84 @@ mm_answer_gss_updatecreds(int socket, Buffer *m) {
017ff1
 
017ff1
 #endif /* GSSAPI */
f09e2e
 
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+int
f09e2e
+mm_answer_audit_unsupported_body(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	int what;
f09e2e
+
f09e2e
+	what = buffer_get_int(m);
f09e2e
+
f09e2e
+	audit_unsupported_body(what);
f09e2e
+
f09e2e
+	buffer_clear(m);
f09e2e
+
f09e2e
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_answer_audit_kex_body(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	int ctos, len;
f09e2e
+	char *cipher, *mac, *compress;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
+
f09e2e
+	ctos = buffer_get_int(m);
f09e2e
+	cipher = buffer_get_string(m, &len;;
f09e2e
+	mac = buffer_get_string(m, &len;;
f09e2e
+	compress = buffer_get_string(m, &len;;
f09e2e
+	pid = buffer_get_int64(m);
f09e2e
+	uid = buffer_get_int64(m);
f09e2e
+
f09e2e
+	audit_kex_body(ctos, cipher, mac, compress, pid, uid);
f09e2e
+
f09e2e
+	free(cipher);
f09e2e
+	free(mac);
f09e2e
+	free(compress);
f09e2e
+	buffer_clear(m);
f09e2e
+
f09e2e
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_answer_audit_session_key_free_body(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	int ctos;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
+
f09e2e
+	ctos = buffer_get_int(m);
f09e2e
+	pid = buffer_get_int64(m);
f09e2e
+	uid = buffer_get_int64(m);
f09e2e
+
f09e2e
+	audit_session_key_free_body(ctos, pid, uid);
f09e2e
+
f09e2e
+	buffer_clear(m);
f09e2e
+
f09e2e
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_answer_audit_server_key_free(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	int len;
f09e2e
+	char *fp;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
+
f09e2e
+	fp = buffer_get_string(m, &len;;
f09e2e
+	pid = buffer_get_int64(m);
f09e2e
+	uid = buffer_get_int64(m);
f09e2e
+
f09e2e
+	audit_destroy_sensitive_data(fp, pid, uid);
f09e2e
+
f09e2e
+	free(fp);
f09e2e
+	buffer_clear(m);
f09e2e
+
f09e2e
+	return 0;
f09e2e
+}
f09e2e
+#endif /* SSH_AUDIT_EVENTS */
017ff1
diff --git a/monitor.h b/monitor.h
017ff1
index ff79fbb..6dfb234 100644
017ff1
--- a/monitor.h
017ff1
+++ b/monitor.h
017ff1
@@ -69,7 +69,13 @@ enum monitor_reqtype {
f09e2e
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
f09e2e
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
f09e2e
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
f09e2e
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
f09e2e
+	MONITOR_REQ_AUDIT_EVENT = 112,
f09e2e
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
f09e2e
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
f09e2e
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
f09e2e
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
f09e2e
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
674526
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124
f09e2e
 
f09e2e
 };
f09e2e
 
017ff1
diff --git a/monitor_wrap.c b/monitor_wrap.c
017ff1
index d1e1caa..6df236a 100644
017ff1
--- a/monitor_wrap.c
017ff1
+++ b/monitor_wrap.c
017ff1
@@ -450,7 +450,7 @@ mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
f09e2e
  */
f09e2e
 
f09e2e
 int
f09e2e
-mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
+mm_key_verify(enum mm_keytype type, Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
 {
f09e2e
 	Buffer m;
f09e2e
 	u_char *blob;
017ff1
@@ -464,6 +464,7 @@ mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
 		return (0);
f09e2e
 
f09e2e
 	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, type);
f09e2e
 	buffer_put_string(&m, blob, len);
f09e2e
 	buffer_put_string(&m, sig, siglen);
f09e2e
 	buffer_put_string(&m, data, datalen);
017ff1
@@ -481,6 +482,19 @@ mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
 	return (verified);
f09e2e
 }
f09e2e
 
f09e2e
+int
f09e2e
+mm_hostbased_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
+{
f09e2e
+	return mm_key_verify(MM_HOSTKEY, key, sig, siglen, data, datalen);
f09e2e
+}
f09e2e
+
f09e2e
+int
f09e2e
+mm_user_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
f09e2e
+{
f09e2e
+	return mm_key_verify(MM_USERKEY, key, sig, siglen, data, datalen);
f09e2e
+}
f09e2e
+
f09e2e
+
f09e2e
 /* Export key state after authentication */
f09e2e
 Newkeys *
f09e2e
 mm_newkeys_from_blob(u_char *blob, int blen)
017ff1
@@ -659,12 +673,14 @@ mm_send_keystate(struct monitor *monitor)
f09e2e
 		fatal("%s: conversion of newkeys failed", __func__);
f09e2e
 
f09e2e
 	buffer_put_string(&m, blob, bloblen);
f09e2e
+	memset(blob, 0, bloblen);
f09e2e
 	free(blob);
f09e2e
 
f09e2e
 	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
f09e2e
 		fatal("%s: conversion of newkeys failed", __func__);
f09e2e
 
f09e2e
 	buffer_put_string(&m, blob, bloblen);
f09e2e
+	memset(blob, 0, bloblen);
f09e2e
 	free(blob);
f09e2e
 
f09e2e
 	packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
017ff1
@@ -1208,10 +1224,11 @@ mm_audit_event(ssh_audit_event_t event)
f09e2e
 	buffer_free(&m);
f09e2e
 }
f09e2e
 
f09e2e
-void
f09e2e
+int
f09e2e
 mm_audit_run_command(const char *command)
f09e2e
 {
f09e2e
 	Buffer m;
f09e2e
+	int handle;
f09e2e
 
f09e2e
 	debug3("%s entering command %s", __func__, command);
f09e2e
 
017ff1
@@ -1219,6 +1236,26 @@ mm_audit_run_command(const char *command)
f09e2e
 	buffer_put_cstring(&m, command);
f09e2e
 
f09e2e
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, &m);
f09e2e
+
f09e2e
+	handle = buffer_get_int(&m);
f09e2e
+	buffer_free(&m);
f09e2e
+
f09e2e
+	return (handle);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+mm_audit_end_command(int handle, const char *command)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	debug3("%s entering command %s", __func__, command);
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, handle);
f09e2e
+	buffer_put_cstring(&m, command);
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, &m);
f09e2e
 	buffer_free(&m);
f09e2e
 }
f09e2e
 #endif /* SSH_AUDIT_EVENTS */
674526
@@ -1354,3 +1391,69 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
017ff1
 
017ff1
 #endif /* GSSAPI */
017ff1
 
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+void
f09e2e
+mm_audit_unsupported_body(int what)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, what);
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
f09e2e
+				  &m);
f09e2e
+
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+mm_audit_kex_body(int ctos, char *cipher, char *mac, char *compress, pid_t pid,
f09e2e
+		  uid_t uid)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, ctos);
f09e2e
+	buffer_put_cstring(&m, cipher);
f09e2e
+	buffer_put_cstring(&m, (mac ? mac : ""));
f09e2e
+	buffer_put_cstring(&m, compress);
f09e2e
+	buffer_put_int64(&m, pid);
f09e2e
+	buffer_put_int64(&m, uid);
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
f09e2e
+				  &m);
f09e2e
+
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_int(&m, ctos);
f09e2e
+	buffer_put_int64(&m, pid);
f09e2e
+	buffer_put_int64(&m, uid);
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
f09e2e
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
f09e2e
+				  &m);
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_cstring(&m, fp);
f09e2e
+	buffer_put_int64(&m, pid);
f09e2e
+	buffer_put_int64(&m, uid);
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+#endif /* SSH_AUDIT_EVENTS */
017ff1
diff --git a/monitor_wrap.h b/monitor_wrap.h
017ff1
index 93929e0..4cf0c78 100644
017ff1
--- a/monitor_wrap.h
017ff1
+++ b/monitor_wrap.h
017ff1
@@ -52,7 +52,8 @@ int mm_key_allowed(enum mm_keytype, char *, char *, Key *);
f09e2e
 int mm_user_key_allowed(struct passwd *, Key *);
f09e2e
 int mm_hostbased_key_allowed(struct passwd *, char *, char *, Key *);
f09e2e
 int mm_auth_rhosts_rsa_key_allowed(struct passwd *, char *, char *, Key *);
f09e2e
-int mm_key_verify(Key *, u_char *, u_int, u_char *, u_int);
f09e2e
+int mm_hostbased_key_verify(Key *, u_char *, u_int, u_char *, u_int);
f09e2e
+int mm_user_key_verify(Key *, u_char *, u_int, u_char *, u_int);
f09e2e
 int mm_auth_rsa_key_allowed(struct passwd *, BIGNUM *, Key **);
f09e2e
 int mm_auth_rsa_verify_response(Key *, BIGNUM *, u_char *);
f09e2e
 BIGNUM *mm_auth_rsa_generate_challenge(Key *);
017ff1
@@ -79,7 +80,12 @@ void mm_sshpam_free_ctx(void *);
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
 #include "audit.h"
f09e2e
 void mm_audit_event(ssh_audit_event_t);
f09e2e
-void mm_audit_run_command(const char *);
f09e2e
+int mm_audit_run_command(const char *);
f09e2e
+void mm_audit_end_command(int, const char *);
f09e2e
+void mm_audit_unsupported_body(int);
f09e2e
+void mm_audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
f09e2e
+void mm_audit_session_key_free_body(int, pid_t, uid_t);
f09e2e
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
f09e2e
 #endif
f09e2e
 
f09e2e
 struct Session;
017ff1
diff --git a/packet.c b/packet.c
017ff1
index 660a9fc..f5b122b 100644
017ff1
--- a/packet.c
017ff1
+++ b/packet.c
f09e2e
@@ -61,6 +61,7 @@
f09e2e
 #include <time.h>
f09e2e
 
f09e2e
 #include "xmalloc.h"
f09e2e
+#include "audit.h"
f09e2e
 #include "buffer.h"
f09e2e
 #include "packet.h"
f09e2e
 #include "crc32.h"
f09e2e
@@ -476,6 +477,13 @@ packet_get_connection_out(void)
f09e2e
 	return active_state->connection_out;
f09e2e
 }
f09e2e
 
f09e2e
+static int
f09e2e
+packet_state_has_keys (const struct session_state *state)
f09e2e
+{
f09e2e
+	return state != NULL &&
f09e2e
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
f09e2e
+}
f09e2e
+
f09e2e
 /* Closes the connection and clears and frees internal data structures. */
f09e2e
 
f09e2e
 void
f09e2e
@@ -484,13 +492,6 @@ packet_close(void)
f09e2e
 	if (!active_state->initialized)
f09e2e
 		return;
f09e2e
 	active_state->initialized = 0;
f09e2e
-	if (active_state->connection_in == active_state->connection_out) {
f09e2e
-		shutdown(active_state->connection_out, SHUT_RDWR);
f09e2e
-		close(active_state->connection_out);
f09e2e
-	} else {
f09e2e
-		close(active_state->connection_in);
f09e2e
-		close(active_state->connection_out);
f09e2e
-	}
f09e2e
 	buffer_free(&active_state->input);
f09e2e
 	buffer_free(&active_state->output);
f09e2e
 	buffer_free(&active_state->outgoing_packet);
f09e2e
@@ -499,8 +500,18 @@ packet_close(void)
f09e2e
 		buffer_free(&active_state->compression_buffer);
f09e2e
 		buffer_compress_uninit();
f09e2e
 	}
f09e2e
-	cipher_cleanup(&active_state->send_context);
f09e2e
-	cipher_cleanup(&active_state->receive_context);
f09e2e
+	if (packet_state_has_keys(active_state)) {
f09e2e
+		cipher_cleanup(&active_state->send_context);
f09e2e
+		cipher_cleanup(&active_state->receive_context);
f09e2e
+		audit_session_key_free(2);
f09e2e
+	}
f09e2e
+	if (active_state->connection_in == active_state->connection_out) {
f09e2e
+		shutdown(active_state->connection_out, SHUT_RDWR);
f09e2e
+		close(active_state->connection_out);
f09e2e
+	} else {
f09e2e
+		close(active_state->connection_in);
f09e2e
+		close(active_state->connection_out);
f09e2e
+	}
f09e2e
 }
f09e2e
 
f09e2e
 /* Sets remote side protocol flags. */
017ff1
@@ -761,6 +791,7 @@ set_newkeys(int mode)
f09e2e
 	}
f09e2e
 	if (active_state->newkeys[mode] != NULL) {
f09e2e
 		debug("set_newkeys: rekeying");
f09e2e
+		audit_session_key_free(mode);
f09e2e
 		cipher_cleanup(cc);
017ff1
 		enc  = &active_state->newkeys[mode]->enc;
017ff1
 		mac  = &active_state->newkeys[mode]->mac;
017ff1
@@ -2011,6 +2042,47 @@ packet_get_newkeys(int mode)
f09e2e
 	return (void *)active_state->newkeys[mode];
f09e2e
 }
f09e2e
 
f09e2e
+static void
f09e2e
+packet_destroy_state(struct session_state *state)
f09e2e
+{
f09e2e
+	if (state == NULL)
f09e2e
+		return;
f09e2e
+
f09e2e
+	cipher_cleanup(&state->receive_context);
f09e2e
+	cipher_cleanup(&state->send_context);
f09e2e
+
f09e2e
+	buffer_free(&state->input);
f09e2e
+	buffer_free(&state->output);
f09e2e
+	buffer_free(&state->outgoing_packet);
f09e2e
+	buffer_free(&state->incoming_packet);
f09e2e
+	buffer_free(&state->compression_buffer);
f09e2e
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
f09e2e
+	state->newkeys[MODE_IN] = NULL;
f09e2e
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
f09e2e
+	state->newkeys[MODE_OUT] = NULL;
f09e2e
+	mac_destroy(state->packet_discard_mac);
f09e2e
+//	TAILQ_HEAD(, packet) outgoing;
f09e2e
+//	memset(state, 0, sizeof(state));
f09e2e
+}
f09e2e
+
f09e2e
+void
f09e2e
+packet_destroy_all(int audit_it, int privsep)
f09e2e
+{
f09e2e
+	if (audit_it)
f09e2e
+		audit_it = packet_state_has_keys (active_state) ||
f09e2e
+			packet_state_has_keys (backup_state);
f09e2e
+	packet_destroy_state(active_state);
f09e2e
+	packet_destroy_state(backup_state);
f09e2e
+	if (audit_it) {
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+		if (privsep)
f09e2e
+			audit_session_key_free(2);
f09e2e
+		else
f09e2e
+			audit_session_key_free_body(2, getpid(), getuid());
f09e2e
+#endif
f09e2e
+	}
f09e2e
+}
f09e2e
+
f09e2e
 /*
f09e2e
  * Save the state for the real connection, and use a separate state when
f09e2e
  * resuming a suspended connection.
017ff1
@@ -2018,18 +2090,12 @@ packet_get_newkeys(int mode)
f09e2e
 void
f09e2e
 packet_backup_state(void)
f09e2e
 {
f09e2e
-	struct session_state *tmp;
f09e2e
-
f09e2e
 	close(active_state->connection_in);
f09e2e
 	active_state->connection_in = -1;
f09e2e
 	close(active_state->connection_out);
f09e2e
 	active_state->connection_out = -1;
f09e2e
-	if (backup_state)
f09e2e
-		tmp = backup_state;
f09e2e
-	else
f09e2e
-		tmp = alloc_session_state();
f09e2e
 	backup_state = active_state;
f09e2e
-	active_state = tmp;
f09e2e
+	active_state = alloc_session_state();
f09e2e
 }
f09e2e
 
f09e2e
 /*
017ff1
@@ -2046,9 +2112,7 @@ packet_restore_state(void)
f09e2e
 	backup_state = active_state;
f09e2e
 	active_state = tmp;
f09e2e
 	active_state->connection_in = backup_state->connection_in;
f09e2e
-	backup_state->connection_in = -1;
f09e2e
 	active_state->connection_out = backup_state->connection_out;
f09e2e
-	backup_state->connection_out = -1;
f09e2e
 	len = buffer_len(&backup_state->input);
f09e2e
 	if (len > 0) {
f09e2e
 		buf = buffer_ptr(&backup_state->input);
017ff1
@@ -2056,4 +2120,10 @@ packet_restore_state(void)
f09e2e
 		buffer_clear(&backup_state->input);
f09e2e
 		add_recv_bytes(len);
f09e2e
 	}
f09e2e
+	backup_state->connection_in = -1;
f09e2e
+	backup_state->connection_out = -1;
f09e2e
+	packet_destroy_state(backup_state);
f09e2e
+	free(backup_state);
f09e2e
+	backup_state = NULL;
f09e2e
 }
f09e2e
+
017ff1
diff --git a/packet.h b/packet.h
017ff1
index f8edf85..c36c812 100644
017ff1
--- a/packet.h
017ff1
+++ b/packet.h
f09e2e
@@ -124,4 +124,5 @@ void	 packet_restore_state(void);
f09e2e
 void	*packet_get_input(void);
f09e2e
 void	*packet_get_output(void);
f09e2e
 
f09e2e
+void	 packet_destroy_all(int, int);
f09e2e
 #endif				/* PACKET_H */
017ff1
diff --git a/session.c b/session.c
017ff1
index df43592..b186ca1 100644
017ff1
--- a/session.c
017ff1
+++ b/session.c
017ff1
@@ -138,7 +138,7 @@ extern int log_stderr;
f09e2e
 extern int debug_flag;
f09e2e
 extern u_int utmp_len;
f09e2e
 extern int startup_pipe;
f09e2e
-extern void destroy_sensitive_data(void);
f09e2e
+extern void destroy_sensitive_data(int);
f09e2e
 extern Buffer loginmsg;
f09e2e
 
f09e2e
 /* original command from peer. */
017ff1
@@ -746,6 +746,14 @@ do_exec_pty(Session *s, const char *command)
f09e2e
 	/* Parent.  Close the slave side of the pseudo tty. */
f09e2e
 	close(ttyfd);
f09e2e
 
f09e2e
+#ifndef HAVE_OSF_SIA
f09e2e
+	/* do_login in the child did not affect state in this process,
f09e2e
+	   compensate.  From an architectural standpoint, this is extremely
f09e2e
+	   ugly. */
f09e2e
+	if (!(options.use_login && command == NULL))
f09e2e
+		audit_count_session_open();
f09e2e
+#endif
f09e2e
+
f09e2e
 	/* Enter interactive session. */
f09e2e
 	s->ptymaster = ptymaster;
f09e2e
 	packet_set_interactive(1, 
017ff1
@@ -863,15 +871,19 @@ do_exec(Session *s, const char *command)
017ff1
 	    get_remote_port());
f09e2e
 
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
+	if (s->command != NULL || s->command_handle != -1)
f09e2e
+		fatal("do_exec: command already set");
f09e2e
 	if (command != NULL)
f09e2e
-		PRIVSEP(audit_run_command(command));
f09e2e
+		s->command = xstrdup(command);
f09e2e
 	else if (s->ttyfd == -1) {
f09e2e
 		char *shell = s->pw->pw_shell;
f09e2e
 
f09e2e
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
f09e2e
 			shell =_PATH_BSHELL;
f09e2e
-		PRIVSEP(audit_run_command(shell));
f09e2e
+		s->command = xstrdup(shell);
f09e2e
 	}
674526
+	if (s->command != NULL && s->ptyfd == -1)
f09e2e
+		s->command_handle = PRIVSEP(audit_run_command(s->command));
f09e2e
 #endif
f09e2e
 	if (s->ttyfd != -1)
f09e2e
 		ret = do_exec_pty(s, command);
017ff1
@@ -1708,7 +1720,10 @@ do_child(Session *s, const char *command)
f09e2e
 	int r = 0;
f09e2e
 
f09e2e
 	/* remove hostkey from the child's memory */
f09e2e
-	destroy_sensitive_data();
f09e2e
+	destroy_sensitive_data(1);
f09e2e
+	/* Don't audit this - both us and the parent would be talking to the
f09e2e
+	   monitor over a single socket, with no synchronization. */
f09e2e
+	packet_destroy_all(0, 1);
f09e2e
 
f09e2e
 	/* Force a password change */
f09e2e
 	if (s->authctxt->force_pwchange) {
017ff1
@@ -1933,6 +1948,7 @@ session_unused(int id)
f09e2e
 	sessions[id].ttyfd = -1;
f09e2e
 	sessions[id].ptymaster = -1;
f09e2e
 	sessions[id].x11_chanids = NULL;
f09e2e
+	sessions[id].command_handle = -1;
f09e2e
 	sessions[id].next_unused = sessions_first_unused;
f09e2e
 	sessions_first_unused = id;
f09e2e
 }
017ff1
@@ -2015,6 +2031,19 @@ session_open(Authctxt *authctxt, int chanid)
f09e2e
 }
f09e2e
 
f09e2e
 Session *
f09e2e
+session_by_id(int id)
f09e2e
+{
f09e2e
+	if (id >= 0 && id < sessions_nalloc) {
f09e2e
+		Session *s = &sessions[id];
f09e2e
+		if (s->used)
f09e2e
+			return s;
f09e2e
+	}
f09e2e
+	debug("session_by_id: unknown id %d", id);
f09e2e
+	session_dump();
f09e2e
+	return NULL;
f09e2e
+}
f09e2e
+
f09e2e
+Session *
f09e2e
 session_by_tty(char *tty)
f09e2e
 {
f09e2e
 	int i;
674526
@@ -2531,6 +2560,32 @@ session_exit_message(Session *s, int status)
f09e2e
 		chan_write_failed(c);
f09e2e
 }
f09e2e
 
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+void
f09e2e
+session_end_command2(Session *s)
f09e2e
+{
f09e2e
+	if (s->command != NULL) {
674526
+		if (s->command_handle != -1)
674526
+			audit_end_command(s->command_handle, s->command);
f09e2e
+		free(s->command);
f09e2e
+		s->command = NULL;
f09e2e
+		s->command_handle = -1;
f09e2e
+	}
f09e2e
+}
f09e2e
+
f09e2e
+static void
f09e2e
+session_end_command(Session *s)
f09e2e
+{
f09e2e
+	if (s->command != NULL) {
674526
+		if (s->command_handle != -1)
674526
+			PRIVSEP(audit_end_command(s->command_handle, s->command));
f09e2e
+		free(s->command);
f09e2e
+		s->command = NULL;
f09e2e
+		s->command_handle = -1;
f09e2e
+	}
f09e2e
+}
f09e2e
+#endif
f09e2e
+
f09e2e
 void
f09e2e
 session_close(Session *s)
f09e2e
 {
017ff1
@@ -2539,6 +2592,10 @@ session_close(Session *s)
f09e2e
 	debug("session_close: session %d pid %ld", s->self, (long)s->pid);
f09e2e
 	if (s->ttyfd != -1)
f09e2e
 		session_pty_cleanup(s);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	if (s->command)
f09e2e
+		session_end_command(s);
f09e2e
+#endif
f09e2e
 	free(s->term);
f09e2e
 	free(s->display);
f09e2e
 	free(s->x11_chanids);
017ff1
@@ -2753,6 +2810,15 @@ do_authenticated2(Authctxt *authctxt)
f09e2e
 	server_loop2(authctxt);
f09e2e
 }
f09e2e
 
f09e2e
+static void
f09e2e
+do_cleanup_one_session(Session *s)
f09e2e
+{
f09e2e
+	session_pty_cleanup2(s);
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	session_end_command2(s);
f09e2e
+#endif
f09e2e
+}
f09e2e
+
f09e2e
 void
f09e2e
 do_cleanup(Authctxt *authctxt)
f09e2e
 {
017ff1
@@ -2801,5 +2867,5 @@ do_cleanup(Authctxt *authctxt)
f09e2e
 	 * or if running in monitor.
f09e2e
 	 */
f09e2e
 	if (!use_privsep || mm_is_monitor())
f09e2e
-		session_destroy_all(session_pty_cleanup2);
f09e2e
+		session_destroy_all(do_cleanup_one_session);
f09e2e
 }
017ff1
diff --git a/session.h b/session.h
017ff1
index 6a2f35e..e9b312e 100644
017ff1
--- a/session.h
017ff1
+++ b/session.h
017ff1
@@ -61,6 +61,12 @@ struct Session {
f09e2e
 		char	*name;
f09e2e
 		char	*val;
f09e2e
 	} *env;
f09e2e
+
f09e2e
+	/* exec */
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	int	command_handle;
f09e2e
+	char	*command;
f09e2e
+#endif
f09e2e
 };
f09e2e
 
f09e2e
 void	 do_authenticated(Authctxt *);
017ff1
@@ -73,8 +79,10 @@ void	 session_close_by_pid(pid_t, int);
f09e2e
 void	 session_close_by_channel(int, void *);
f09e2e
 void	 session_destroy_all(void (*)(Session *));
f09e2e
 void	 session_pty_cleanup2(Session *);
f09e2e
+void	 session_end_command2(Session *);
f09e2e
 
f09e2e
 Session	*session_new(void);
f09e2e
+Session *session_by_id(int);
f09e2e
 Session	*session_by_tty(char *);
f09e2e
 void	 session_close(Session *);
f09e2e
 void	 do_setusercontext(struct passwd *);
017ff1
diff --git a/sshd.c b/sshd.c
017ff1
index 8a0740a..2813aa2 100644
017ff1
--- a/sshd.c
017ff1
+++ b/sshd.c
f09e2e
@@ -119,6 +119,7 @@
f09e2e
 #endif
f09e2e
 #include "monitor_wrap.h"
f09e2e
 #include "roaming.h"
f09e2e
+#include "audit.h"
f09e2e
 #include "ssh-sandbox.h"
f09e2e
 #include "version.h"
f09e2e
 
017ff1
@@ -264,7 +265,7 @@ Buffer loginmsg;
f09e2e
 struct passwd *privsep_pw = NULL;
f09e2e
 
f09e2e
 /* Prototypes for various functions defined later in this file. */
f09e2e
-void destroy_sensitive_data(void);
f09e2e
+void destroy_sensitive_data(int);
f09e2e
 void demote_sensitive_data(void);
f09e2e
 
f09e2e
 static void do_ssh1_kex(void);
017ff1
@@ -283,6 +284,15 @@ close_listen_socks(void)
f09e2e
 	num_listen_socks = -1;
f09e2e
 }
f09e2e
 
f09e2e
+/*
f09e2e
+ * Is this process listening for clients (i.e. not specific to any specific
f09e2e
+ * client connection?)
f09e2e
+ */
f09e2e
+int listening_for_clients(void)
f09e2e
+{
f09e2e
+	return num_listen_socks > 0;
f09e2e
+}
f09e2e
+
f09e2e
 static void
f09e2e
 close_startup_pipes(void)
f09e2e
 {
017ff1
@@ -562,22 +572,45 @@ sshd_exchange_identification(int sock_in, int sock_out)
f09e2e
 	}
f09e2e
 }
f09e2e
 
f09e2e
-/* Destroy the host and server keys.  They will no longer be needed. */
f09e2e
+/*
f09e2e
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
f09e2e
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
f09e2e
+ */
f09e2e
 void
f09e2e
-destroy_sensitive_data(void)
f09e2e
+destroy_sensitive_data(int privsep)
f09e2e
 {
f09e2e
 	int i;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
 
f09e2e
 	if (sensitive_data.server_key) {
f09e2e
 		key_free(sensitive_data.server_key);
f09e2e
 		sensitive_data.server_key = NULL;
f09e2e
 	}
f09e2e
+	pid = getpid();
f09e2e
+	uid = getuid();
f09e2e
 	for (i = 0; i < options.num_host_key_files; i++) {
f09e2e
 		if (sensitive_data.host_keys[i]) {
f09e2e
+			char *fp;
f09e2e
+
f09e2e
+			if (key_is_private(sensitive_data.host_keys[i]))
017ff1
+				fp = key_selected_fingerprint(sensitive_data.host_keys[i], SSH_FP_HEX);
f09e2e
+			else
f09e2e
+				fp = NULL;
f09e2e
 			key_free(sensitive_data.host_keys[i]);
f09e2e
 			sensitive_data.host_keys[i] = NULL;
f09e2e
+			if (fp != NULL) {
f09e2e
+				if (privsep)
f09e2e
+					PRIVSEP(audit_destroy_sensitive_data(fp,
f09e2e
+						pid, uid));
f09e2e
+				else
f09e2e
+					audit_destroy_sensitive_data(fp,
f09e2e
+						pid, uid);
f09e2e
+				free(fp);
f09e2e
+			}
f09e2e
 		}
f09e2e
-		if (sensitive_data.host_certificates[i]) {
f09e2e
+		if (sensitive_data.host_certificates
f09e2e
+		    && sensitive_data.host_certificates[i]) {
f09e2e
 			key_free(sensitive_data.host_certificates[i]);
f09e2e
 			sensitive_data.host_certificates[i] = NULL;
f09e2e
 		}
017ff1
@@ -591,6 +624,8 @@ void
f09e2e
 demote_sensitive_data(void)
f09e2e
 {
f09e2e
 	Key *tmp;
f09e2e
+	pid_t pid;
f09e2e
+	uid_t uid;
f09e2e
 	int i;
f09e2e
 
f09e2e
 	if (sensitive_data.server_key) {
017ff1
@@ -599,13 +634,25 @@ demote_sensitive_data(void)
f09e2e
 		sensitive_data.server_key = tmp;
f09e2e
 	}
f09e2e
 
f09e2e
+	pid = getpid();
f09e2e
+	uid = getuid();
f09e2e
 	for (i = 0; i < options.num_host_key_files; i++) {
f09e2e
 		if (sensitive_data.host_keys[i]) {
f09e2e
+			char *fp;
f09e2e
+
f09e2e
+			if (key_is_private(sensitive_data.host_keys[i]))
017ff1
+				fp = key_selected_fingerprint(sensitive_data.host_keys[i], SSH_FP_HEX);
f09e2e
+			else
f09e2e
+				fp = NULL;
f09e2e
 			tmp = key_demote(sensitive_data.host_keys[i]);
f09e2e
 			key_free(sensitive_data.host_keys[i]);
f09e2e
 			sensitive_data.host_keys[i] = tmp;
f09e2e
 			if (tmp->type == KEY_RSA1)
f09e2e
 				sensitive_data.ssh1_host_key = tmp;
f09e2e
+			if (fp != NULL) {
f09e2e
+				audit_destroy_sensitive_data(fp, pid, uid);
f09e2e
+				free(fp);
f09e2e
+			}
f09e2e
 		}
f09e2e
 		/* Certs do not need demotion */
f09e2e
 	}
017ff1
@@ -675,7 +722,7 @@ privsep_preauth(Authctxt *authctxt)
770374
 
770374
 	if (use_privsep == PRIVSEP_ON)
017ff1
 		box = ssh_sandbox_init(pmonitor);
770374
-	pid = fork();
770374
+	pmonitor->m_pid = pid = fork();
770374
 	if (pid == -1) {
770374
 		fatal("fork of unprivileged child failed");
770374
 	} else if (pid != 0) {
017ff1
@@ -729,6 +776,8 @@ privsep_preauth(Authctxt *authctxt)
f09e2e
 	}
f09e2e
 }
f09e2e
 
f09e2e
+extern Newkeys *current_keys[];
f09e2e
+
f09e2e
 static void
f09e2e
 privsep_postauth(Authctxt *authctxt)
f09e2e
 {
017ff1
@@ -753,6 +802,10 @@ privsep_postauth(Authctxt *authctxt)
f09e2e
 	else if (pmonitor->m_pid != 0) {
f09e2e
 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
f09e2e
 		buffer_clear(&loginmsg);
674526
+		newkeys_destroy(current_keys[MODE_OUT]);
f09e2e
+		newkeys_destroy(current_keys[MODE_IN]);
f09e2e
+		audit_session_key_free_body(2, getpid(), getuid());
f09e2e
+		packet_destroy_all(0, 0);
f09e2e
 		monitor_child_postauth(pmonitor);
f09e2e
 
f09e2e
 		/* NEVERREACHED */
017ff1
@@ -1211,6 +1264,7 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
f09e2e
 		if (received_sigterm) {
f09e2e
 			logit("Received signal %d; terminating.",
f09e2e
 			    (int) received_sigterm);
f09e2e
+			destroy_sensitive_data(0);
f09e2e
 			close_listen_socks();
f09e2e
 			unlink(options.pid_file);
f09e2e
 			exit(received_sigterm == SIGTERM ? 0 : 255);
017ff1
@@ -2134,6 +2188,7 @@ main(int ac, char **av)
f09e2e
 	 */
f09e2e
 	if (use_privsep) {
f09e2e
 		mm_send_keystate(pmonitor);
f09e2e
+		packet_destroy_all(1, 1);
f09e2e
 		exit(0);
f09e2e
 	}
f09e2e
 
017ff1
@@ -2179,7 +2234,7 @@ main(int ac, char **av)
f09e2e
 		privsep_postauth(authctxt);
f09e2e
 		/* the monitor process [priv] will not return */
f09e2e
 		if (!compat20)
f09e2e
-			destroy_sensitive_data();
f09e2e
+			destroy_sensitive_data(0);
f09e2e
 	}
f09e2e
 
f09e2e
 	packet_set_timeout(options.client_alive_interval,
017ff1
@@ -2189,6 +2244,9 @@ main(int ac, char **av)
f09e2e
 	do_authenticated(authctxt);
f09e2e
 
f09e2e
 	/* The connection has been terminated. */
f09e2e
+	packet_destroy_all(1, 1);
f09e2e
+	destroy_sensitive_data(1);
f09e2e
+
f09e2e
 	packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
f09e2e
 	packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
f09e2e
 	verbose("Transferred: sent %llu, received %llu bytes",
017ff1
@@ -2346,6 +2404,10 @@ do_ssh1_kex(void)
f09e2e
 		if (cookie[i] != packet_get_char())
f09e2e
 			packet_disconnect("IP Spoofing check bytes do not match.");
f09e2e
 
f09e2e
+#ifdef SSH_AUDIT_EVENTS
f09e2e
+	audit_kex(2, cipher_name(cipher_type), "crc", "none");
f09e2e
+#endif
f09e2e
+
f09e2e
 	debug("Encryption type: %.200s", cipher_name(cipher_type));
f09e2e
 
f09e2e
 	/* Get the encrypted integer. */
017ff1
@@ -2418,7 +2480,7 @@ do_ssh1_kex(void)
f09e2e
 			session_id[i] = session_key[i] ^ session_key[i + 16];
f09e2e
 	}
f09e2e
 	/* Destroy the private and public keys. No longer. */
f09e2e
-	destroy_sensitive_data();
f09e2e
+	destroy_sensitive_data(0);
f09e2e
 
f09e2e
 	if (use_privsep)
f09e2e
 		mm_ssh1_session_id(session_id);
017ff1
@@ -2584,6 +2646,16 @@ do_ssh2_kex(void)
f09e2e
 void
f09e2e
 cleanup_exit(int i)
f09e2e
 {
f09e2e
+	static int in_cleanup = 0;
f09e2e
+	int is_privsep_child;
f09e2e
+
f09e2e
+	/* cleanup_exit can be called at the very least from the privsep
f09e2e
+	   wrappers used for auditing.  Make sure we don't recurse
f09e2e
+	   indefinitely. */
f09e2e
+	if (in_cleanup)
f09e2e
+		_exit(i);
f09e2e
+	in_cleanup = 1;
f09e2e
+
f09e2e
 	if (the_authctxt) {
f09e2e
 		do_cleanup(the_authctxt);
f09e2e
 		if (use_privsep && privsep_is_preauth && pmonitor->m_pid > 1) {
017ff1
@@ -2594,9 +2666,14 @@ cleanup_exit(int i)
f09e2e
 				    pmonitor->m_pid, strerror(errno));
f09e2e
 		}
f09e2e
 	}
770374
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
f09e2e
+	if (sensitive_data.host_keys != NULL)
f09e2e
+		destroy_sensitive_data(is_privsep_child);
f09e2e
+	packet_destroy_all(1, is_privsep_child);
f09e2e
 #ifdef SSH_AUDIT_EVENTS
f09e2e
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
f09e2e
-	if (!use_privsep || mm_is_monitor())
f09e2e
+	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&
f09e2e
+	    (!use_privsep || mm_is_monitor()))
f09e2e
 		audit_event(SSH_CONNECTION_ABANDON);
f09e2e
 #endif
f09e2e
 	_exit(i);