diff options
author | djm@openbsd.org <djm@openbsd.org> | 2019-01-21 10:03:37 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2019-01-21 21:47:28 +1100 |
commit | dec5e9d33891e3bc3f1395d7db0e56fdc7f86dfc (patch) | |
tree | a91c7543cc726563651cc9eb2603f205ab7b27b1 /kexdhc.c | |
parent | e93bd98eab79b9a78f64ee8dd4dffc4d3979c7ae (diff) |
upstream: factor out kex_dh_compute_key() - it's shared between
plain DH KEX and DH GEX in both the client and server implementations
from markus@ ok djm@
OpenBSD-Commit-ID: 12186e18791fffcd4642c82e7e0cfdd7ea37e2ec
Diffstat (limited to 'kexdhc.c')
-rw-r--r-- | kexdhc.c | 45 |
1 files changed, 11 insertions, 34 deletions
@@ -1,4 +1,4 @@ -/* $OpenBSD: kexdhc.c,v 1.27 2019/01/21 10:00:23 djm Exp $ */ +/* $OpenBSD: kexdhc.c,v 1.28 2019/01/21 10:03:37 djm Exp $ */ /* * Copyright (c) 2001 Markus Friedl. All rights reserved. * @@ -86,13 +86,14 @@ static int input_kex_dh(int type, u_int32_t seq, struct ssh *ssh) { struct kex *kex = ssh->kex; - BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; + BIGNUM *dh_server_pub = NULL; const BIGNUM *pub_key; struct sshkey *server_host_key = NULL; - u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL; + struct sshbuf *shared_secret = NULL; + u_char *server_host_key_blob = NULL, *signature = NULL; u_char hash[SSH_DIGEST_MAX_LENGTH]; - size_t klen = 0, slen, sbloblen, hashlen; - int kout, r; + size_t slen, sbloblen, hashlen; + int r; if (kex->verify_host_key == NULL) { r = SSH_ERR_INVALID_ARGUMENT; @@ -119,32 +120,12 @@ input_kex_dh(int type, u_int32_t seq, struct ssh *ssh) (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || (r = sshpkt_get_end(ssh)) != 0) goto out; -#ifdef DEBUG_KEXDH - fprintf(stderr, "dh_server_pub= "); - BN_print_fp(stderr, dh_server_pub); - fprintf(stderr, "\n"); - debug("bits %d", BN_num_bits(dh_server_pub)); -#endif - if (!dh_pub_is_valid(kex->dh, dh_server_pub)) { - sshpkt_disconnect(ssh, "bad server public DH value"); - r = SSH_ERR_MESSAGE_INCOMPLETE; - goto out; - } - - klen = DH_size(kex->dh); - if ((kbuf = malloc(klen)) == NULL || - (shared_secret = BN_new()) == NULL) { + if ((shared_secret = sshbuf_new()) == NULL) { r = SSH_ERR_ALLOC_FAIL; goto out; } - if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 || - BN_bin2bn(kbuf, kout, shared_secret) == NULL) { - r = SSH_ERR_LIBCRYPTO_ERROR; + if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0) goto out; - } -#ifdef DEBUG_KEXDH - dump_digest("shared secret", kbuf, kout); -#endif /* calc and verify H */ DH_get0_key(kex->dh, &pub_key, NULL); @@ -158,7 +139,7 @@ input_kex_dh(int type, u_int32_t seq, struct ssh *ssh) server_host_key_blob, sbloblen, pub_key, dh_server_pub, - shared_secret, + sshbuf_ptr(shared_secret), sshbuf_len(shared_secret), hash, &hashlen)) != 0) goto out; @@ -166,18 +147,14 @@ input_kex_dh(int type, u_int32_t seq, struct ssh *ssh) kex->hostkey_alg, ssh->compat)) != 0) goto out; - if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) + if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0) r = kex_send_newkeys(ssh); out: explicit_bzero(hash, sizeof(hash)); DH_free(kex->dh); kex->dh = NULL; BN_clear_free(dh_server_pub); - if (kbuf) { - explicit_bzero(kbuf, klen); - free(kbuf); - } - BN_clear_free(shared_secret); + sshbuf_free(shared_secret); sshkey_free(server_host_key); free(server_host_key_blob); free(signature); |