]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: Record session ID, host key and sig at intital KEX
authordjm@openbsd.org <djm@openbsd.org>
Sun, 19 Dec 2021 22:08:06 +0000 (22:08 +0000)
committerDamien Miller <djm@mindrot.org>
Sun, 19 Dec 2021 22:24:42 +0000 (09:24 +1100)
These will be used later for agent session ID / hostkey binding

ok markus@

OpenBSD-Commit-ID: a9af29e33772b18e3e867c6fa8ab35e1694a81fe

kex.c
kex.h
kexgen.c
kexgexc.c
kexgexs.c

diff --git a/kex.c b/kex.c
index 709a0ec63aa01099bc76114deb4582ac1f07c81a..55babbcecf306f82dd40c17e2cfb483de57af526 100644 (file)
--- a/kex.c
+++ b/kex.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kex.c,v 1.168 2021/04/03 06:18:40 djm Exp $ */
+/* $OpenBSD: kex.c,v 1.169 2021/12/19 22:08:06 djm Exp $ */
 /*
  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
  *
@@ -699,6 +699,8 @@ kex_free(struct kex *kex)
        sshbuf_free(kex->server_version);
        sshbuf_free(kex->client_pub);
        sshbuf_free(kex->session_id);
+       sshbuf_free(kex->initial_sig);
+       sshkey_free(kex->initial_hostkey);
        free(kex->failed_choice);
        free(kex->hostkey_alg);
        free(kex->name);
diff --git a/kex.h b/kex.h
index 9605ed528ea4363865e9b20b19e4ba0c27b1309a..70b8909bc9ff03698a1cc71ee59befe5c895b482 100644 (file)
--- a/kex.h
+++ b/kex.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: kex.h,v 1.114 2021/01/31 22:55:29 djm Exp $ */
+/* $OpenBSD: kex.h,v 1.115 2021/12/19 22:08:06 djm Exp $ */
 
 /*
  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
@@ -130,6 +130,7 @@ struct newkeys {
 };
 
 struct ssh;
+struct sshbuf;
 
 struct kex {
        struct newkeys  *newkeys[MODE_MAX];
@@ -148,6 +149,8 @@ struct kex {
        struct sshbuf *client_version;
        struct sshbuf *server_version;
        struct sshbuf *session_id;
+       struct sshbuf *initial_sig;
+       struct sshkey *initial_hostkey;
        sig_atomic_t done;
        u_int   flags;
        int     hash_alg;
index bde28053ddf1e58f0978d0e2c704f3764315c62f..20f3c57110a8491e20a8bd6206bf2087da1490ca 100644 (file)
--- a/kexgen.c
+++ b/kexgen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kexgen.c,v 1.7 2021/04/03 06:18:40 djm Exp $ */
+/* $OpenBSD: kexgen.c,v 1.8 2021/12/19 22:08:06 djm Exp $ */
 /*
  * Copyright (c) 2019 Markus Friedl.  All rights reserved.
  *
@@ -218,8 +218,26 @@ input_kex_gen_reply(int type, u_int32_t seq, struct ssh *ssh)
            kex->hostkey_alg, ssh->compat, NULL)) != 0)
                goto out;
 
-       if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
-               r = kex_send_newkeys(ssh);
+       if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
+           (r = kex_send_newkeys(ssh)) != 0)
+               goto out;
+
+       /* save initial signature and hostkey */
+       if ((kex->flags & KEX_INITIAL) != 0) {
+               if (kex->initial_hostkey != NULL || kex->initial_sig != NULL) {
+                       r = SSH_ERR_INTERNAL_ERROR;
+                       goto out;
+               }
+               if ((kex->initial_sig = sshbuf_new()) == NULL) {
+                       r = SSH_ERR_ALLOC_FAIL;
+                       goto out;
+               }
+               if ((r = sshbuf_put(kex->initial_sig, signature, slen)) != 0)
+                       goto out;
+               kex->initial_hostkey = server_host_key;
+               server_host_key = NULL;
+       }
+       /* success */
 out:
        explicit_bzero(hash, sizeof(hash));
        explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
@@ -333,8 +351,15 @@ input_kex_gen_init(int type, u_int32_t seq, struct ssh *ssh)
            (r = sshpkt_send(ssh)) != 0)
                goto out;
 
-       if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
-               r = kex_send_newkeys(ssh);
+       if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
+           (r = kex_send_newkeys(ssh)) != 0)
+               goto out;
+       /* retain copy of hostkey used at initial KEX */
+       if (kex->initial_hostkey == NULL &&
+           (r = sshkey_from_private(server_host_public,
+           &kex->initial_hostkey)) != 0)
+               goto out;
+       /* success */
 out:
        explicit_bzero(hash, sizeof(hash));
        sshbuf_free(server_host_key_blob);
index 4a2e741d84580286f72ba90bfdfc35b7ffcd4c59..e99e0cf216e79fa2778e419390f3868385faca45 100644 (file)
--- a/kexgexc.c
+++ b/kexgexc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kexgexc.c,v 1.37 2021/01/31 22:55:29 djm Exp $ */
+/* $OpenBSD: kexgexc.c,v 1.38 2021/12/19 22:08:06 djm Exp $ */
 /*
  * Copyright (c) 2000 Niels Provos.  All rights reserved.
  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
@@ -206,8 +206,26 @@ input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
            hashlen, kex->hostkey_alg, ssh->compat, NULL)) != 0)
                goto out;
 
-       if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
-               r = kex_send_newkeys(ssh);
+       if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
+           (r = kex_send_newkeys(ssh)) != 0)
+               goto out;
+
+       /* save initial signature and hostkey */
+       if ((kex->flags & KEX_INITIAL) != 0) {
+               if (kex->initial_hostkey != NULL || kex->initial_sig != NULL) {
+                       r = SSH_ERR_INTERNAL_ERROR;
+                       goto out;
+               }
+               if ((kex->initial_sig = sshbuf_new()) == NULL) {
+                       r = SSH_ERR_ALLOC_FAIL;
+                       goto out;
+               }
+               if ((r = sshbuf_put(kex->initial_sig, signature, slen)) != 0)
+                       goto out;
+               kex->initial_hostkey = server_host_key;
+               server_host_key = NULL;
+       }
+       /* success */
  out:
        explicit_bzero(hash, sizeof(hash));
        DH_free(kex->dh);
index f0fbcb9125434f65c3c3630c9e95185b38328fba..72b444f6906b262f426515481d5add0dec8da12c 100644 (file)
--- a/kexgexs.c
+++ b/kexgexs.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kexgexs.c,v 1.43 2021/01/31 22:55:29 djm Exp $ */
+/* $OpenBSD: kexgexs.c,v 1.44 2021/12/19 22:08:06 djm Exp $ */
 /*
  * Copyright (c) 2000 Niels Provos.  All rights reserved.
  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
@@ -194,8 +194,16 @@ input_kex_dh_gex_init(int type, u_int32_t seq, struct ssh *ssh)
            (r = sshpkt_send(ssh)) != 0)
                goto out;
 
-       if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
-               r = kex_send_newkeys(ssh);
+       if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
+           (r = kex_send_newkeys(ssh)) != 0)
+               goto out;
+
+       /* retain copy of hostkey used at initial KEX */
+       if (kex->initial_hostkey == NULL &&
+           (r = sshkey_from_private(server_host_public,
+           &kex->initial_hostkey)) != 0)
+               goto out;
+       /* success */
  out:
        explicit_bzero(hash, sizeof(hash));
        DH_free(kex->dh);