-/* $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.
*
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);
-/* $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.
};
struct ssh;
+struct sshbuf;
struct kex {
struct newkeys *newkeys[MODE_MAX];
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;
-/* $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.
*
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));
(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);
-/* $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.
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);
-/* $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.
(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);