Annotation of src/usr.bin/ssh/kexgexc.c, Revision 1.35
1.35 ! djm 1: /* $OpenBSD: kexgexc.c,v 1.34 2019/01/23 00:30:41 djm Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2000 Niels Provos. All rights reserved.
4: * Copyright (c) 2001 Markus Friedl. All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.9 deraadt 27: #include <sys/types.h>
1.12 djm 28:
29: #include <openssl/dh.h>
1.7 stevesk 30:
1.8 stevesk 31: #include <stdio.h>
1.7 stevesk 32: #include <string.h>
1.9 deraadt 33: #include <signal.h>
1.1 markus 34:
1.19 markus 35: #include "sshkey.h"
1.9 deraadt 36: #include "cipher.h"
1.19 markus 37: #include "digest.h"
1.1 markus 38: #include "kex.h"
39: #include "log.h"
40: #include "packet.h"
41: #include "dh.h"
42: #include "ssh2.h"
43: #include "compat.h"
1.19 markus 44: #include "dispatch.h"
45: #include "ssherr.h"
46: #include "sshbuf.h"
1.23 deraadt 47: #include "misc.h"
1.1 markus 48:
1.25 markus 49: static int input_kex_dh_gex_group(int, u_int32_t, struct ssh *);
50: static int input_kex_dh_gex_reply(int, u_int32_t, struct ssh *);
1.19 markus 51:
52: int
53: kexgex_client(struct ssh *ssh)
1.1 markus 54: {
1.19 markus 55: struct kex *kex = ssh->kex;
56: int r;
57: u_int nbits;
1.1 markus 58:
1.16 dtucker 59: nbits = dh_estimate(kex->dh_need * 8);
1.1 markus 60:
1.19 markus 61: kex->min = DH_GRP_MIN;
62: kex->max = DH_GRP_MAX;
63: kex->nbits = nbits;
1.22 dtucker 64: if (datafellows & SSH_BUG_DHGEX_LARGE)
1.23 deraadt 65: kex->nbits = MINIMUM(kex->nbits, 4096);
1.21 djm 66: /* New GEX request */
67: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
68: (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
69: (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
70: (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
71: (r = sshpkt_send(ssh)) != 0)
72: goto out;
73: debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
74: kex->min, kex->nbits, kex->max);
1.1 markus 75: #ifdef DEBUG_KEXDH
76: fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
1.19 markus 77: kex->min, kex->nbits, kex->max);
1.1 markus 78: #endif
1.19 markus 79: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
80: &input_kex_dh_gex_group);
81: r = 0;
82: out:
83: return r;
84: }
1.1 markus 85:
1.19 markus 86: static int
1.25 markus 87: input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
1.19 markus 88: {
89: struct kex *kex = ssh->kex;
90: BIGNUM *p = NULL, *g = NULL;
1.28 djm 91: const BIGNUM *pub_key;
1.19 markus 92: int r, bits;
1.1 markus 93:
1.19 markus 94: debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
1.1 markus 95:
1.30 djm 96: if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 ||
97: (r = sshpkt_get_bignum2(ssh, &g)) != 0 ||
1.19 markus 98: (r = sshpkt_get_end(ssh)) != 0)
99: goto out;
100: if ((bits = BN_num_bits(p)) < 0 ||
101: (u_int)bits < kex->min || (u_int)bits > kex->max) {
102: r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
103: goto out;
104: }
105: if ((kex->dh = dh_new_group(g, p)) == NULL) {
106: r = SSH_ERR_ALLOC_FAIL;
107: goto out;
108: }
109: p = g = NULL; /* belong to kex->dh now */
1.1 markus 110:
1.19 markus 111: /* generate and send 'e', client DH public key */
1.28 djm 112: if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
113: goto out;
114: DH_get0_key(kex->dh, &pub_key, NULL);
115: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
116: (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
1.19 markus 117: (r = sshpkt_send(ssh)) != 0)
118: goto out;
119: debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
1.1 markus 120: #ifdef DEBUG_KEXDH
1.19 markus 121: DHparams_print_fp(stderr, kex->dh);
1.1 markus 122: fprintf(stderr, "pub= ");
1.28 djm 123: BN_print_fp(stderr, pub_key);
1.1 markus 124: fprintf(stderr, "\n");
125: #endif
1.19 markus 126: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
127: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
128: r = 0;
129: out:
1.27 jsing 130: BN_clear_free(p);
131: BN_clear_free(g);
1.19 markus 132: return r;
133: }
1.1 markus 134:
1.19 markus 135: static int
1.25 markus 136: input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
1.19 markus 137: {
138: struct kex *kex = ssh->kex;
1.32 djm 139: BIGNUM *dh_server_pub = NULL;
1.28 djm 140: const BIGNUM *pub_key, *dh_p, *dh_g;
1.32 djm 141: struct sshbuf *shared_secret = NULL;
1.34 djm 142: struct sshbuf *tmp = NULL, *server_host_key_blob = NULL;
1.19 markus 143: struct sshkey *server_host_key = NULL;
1.34 djm 144: u_char *signature = NULL;
1.19 markus 145: u_char hash[SSH_DIGEST_MAX_LENGTH];
1.34 djm 146: size_t slen, hashlen;
1.32 djm 147: int r;
1.19 markus 148:
149: debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
1.1 markus 150: /* key, cert */
1.34 djm 151: if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
1.19 markus 152: goto out;
1.34 djm 153: /* sshkey_fromb() consumes its buffer, so make a copy */
154: if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
155: r = SSH_ERR_ALLOC_FAIL;
156: goto out;
157: }
158: if ((r = sshkey_fromb(tmp, &server_host_key)) != 0 ||
159: (r = kex_verify_host_key(ssh, server_host_key)) != 0)
1.19 markus 160: goto out;
1.30 djm 161: /* DH parameter f, server public DH key, signed H */
162: if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
1.19 markus 163: (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
164: (r = sshpkt_get_end(ssh)) != 0)
165: goto out;
1.32 djm 166: if ((shared_secret = sshbuf_new()) == NULL) {
1.19 markus 167: r = SSH_ERR_ALLOC_FAIL;
168: goto out;
169: }
1.32 djm 170: if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0)
1.19 markus 171: goto out;
172: if (ssh->compat & SSH_OLD_DHGEX)
173: kex->min = kex->max = -1;
1.1 markus 174:
175: /* calc and verify H */
1.28 djm 176: DH_get0_key(kex->dh, &pub_key, NULL);
177: DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
1.19 markus 178: hashlen = sizeof(hash);
179: if ((r = kexgex_hash(
1.14 djm 180: kex->hash_alg,
1.29 djm 181: kex->client_version,
182: kex->server_version,
1.34 djm 183: kex->my,
184: kex->peer,
185: server_host_key_blob,
1.19 markus 186: kex->min, kex->nbits, kex->max,
1.28 djm 187: dh_p, dh_g,
188: pub_key,
1.1 markus 189: dh_server_pub,
1.32 djm 190: sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
1.19 markus 191: hash, &hashlen)) != 0)
192: goto out;
1.3 djm 193:
1.19 markus 194: if ((r = sshkey_verify(server_host_key, signature, slen, hash,
1.35 ! djm 195: hashlen, kex->hostkey_alg, ssh->compat, NULL)) != 0)
1.19 markus 196: goto out;
1.1 markus 197:
1.32 djm 198: if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
1.19 markus 199: r = kex_send_newkeys(ssh);
200: out:
201: explicit_bzero(hash, sizeof(hash));
202: DH_free(kex->dh);
203: kex->dh = NULL;
1.27 jsing 204: BN_clear_free(dh_server_pub);
1.32 djm 205: sshbuf_free(shared_secret);
1.19 markus 206: sshkey_free(server_host_key);
1.34 djm 207: sshbuf_free(tmp);
208: sshbuf_free(server_host_key_blob);
1.19 markus 209: free(signature);
210: return r;
1.1 markus 211: }