Annotation of src/usr.bin/ssh/kexgexc.c, Revision 1.37
1.37 ! djm 1: /* $OpenBSD: kexgexc.c,v 1.36 2021/01/27 09:26:54 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.36 djm 64: if (ssh->compat & 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.37 ! djm 79: debug("expecting SSH2_MSG_KEX_DH_GEX_GROUP");
1.19 markus 80: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
81: &input_kex_dh_gex_group);
82: r = 0;
83: out:
84: return r;
85: }
1.1 markus 86:
1.19 markus 87: static int
1.25 markus 88: input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
1.19 markus 89: {
90: struct kex *kex = ssh->kex;
91: BIGNUM *p = NULL, *g = NULL;
1.28 djm 92: const BIGNUM *pub_key;
1.19 markus 93: int r, bits;
1.1 markus 94:
1.37 ! djm 95: debug("SSH2_MSG_KEX_DH_GEX_GROUP received");
! 96: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, &kex_protocol_error);
1.1 markus 97:
1.30 djm 98: if ((r = sshpkt_get_bignum2(ssh, &p)) != 0 ||
99: (r = sshpkt_get_bignum2(ssh, &g)) != 0 ||
1.19 markus 100: (r = sshpkt_get_end(ssh)) != 0)
101: goto out;
102: if ((bits = BN_num_bits(p)) < 0 ||
103: (u_int)bits < kex->min || (u_int)bits > kex->max) {
104: r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
105: goto out;
106: }
107: if ((kex->dh = dh_new_group(g, p)) == NULL) {
108: r = SSH_ERR_ALLOC_FAIL;
109: goto out;
110: }
111: p = g = NULL; /* belong to kex->dh now */
1.1 markus 112:
1.19 markus 113: /* generate and send 'e', client DH public key */
1.28 djm 114: if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
115: goto out;
116: DH_get0_key(kex->dh, &pub_key, NULL);
117: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
118: (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
1.19 markus 119: (r = sshpkt_send(ssh)) != 0)
120: goto out;
121: debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
1.1 markus 122: #ifdef DEBUG_KEXDH
1.19 markus 123: DHparams_print_fp(stderr, kex->dh);
1.1 markus 124: fprintf(stderr, "pub= ");
1.28 djm 125: BN_print_fp(stderr, pub_key);
1.1 markus 126: fprintf(stderr, "\n");
127: #endif
1.37 ! djm 128: debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
1.19 markus 129: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
130: r = 0;
131: out:
1.27 jsing 132: BN_clear_free(p);
133: BN_clear_free(g);
1.19 markus 134: return r;
135: }
1.1 markus 136:
1.19 markus 137: static int
1.25 markus 138: input_kex_dh_gex_reply(int type, u_int32_t seq, struct ssh *ssh)
1.19 markus 139: {
140: struct kex *kex = ssh->kex;
1.32 djm 141: BIGNUM *dh_server_pub = NULL;
1.28 djm 142: const BIGNUM *pub_key, *dh_p, *dh_g;
1.32 djm 143: struct sshbuf *shared_secret = NULL;
1.34 djm 144: struct sshbuf *tmp = NULL, *server_host_key_blob = NULL;
1.19 markus 145: struct sshkey *server_host_key = NULL;
1.34 djm 146: u_char *signature = NULL;
1.19 markus 147: u_char hash[SSH_DIGEST_MAX_LENGTH];
1.34 djm 148: size_t slen, hashlen;
1.32 djm 149: int r;
1.19 markus 150:
1.37 ! djm 151: debug("SSH2_MSG_KEX_DH_GEX_REPLY received");
! 152: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &kex_protocol_error);
! 153:
1.1 markus 154: /* key, cert */
1.34 djm 155: if ((r = sshpkt_getb_froms(ssh, &server_host_key_blob)) != 0)
1.19 markus 156: goto out;
1.34 djm 157: /* sshkey_fromb() consumes its buffer, so make a copy */
158: if ((tmp = sshbuf_fromb(server_host_key_blob)) == NULL) {
159: r = SSH_ERR_ALLOC_FAIL;
160: goto out;
161: }
162: if ((r = sshkey_fromb(tmp, &server_host_key)) != 0 ||
163: (r = kex_verify_host_key(ssh, server_host_key)) != 0)
1.19 markus 164: goto out;
1.30 djm 165: /* DH parameter f, server public DH key, signed H */
166: if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
1.19 markus 167: (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
168: (r = sshpkt_get_end(ssh)) != 0)
169: goto out;
1.32 djm 170: if ((shared_secret = sshbuf_new()) == NULL) {
1.19 markus 171: r = SSH_ERR_ALLOC_FAIL;
172: goto out;
173: }
1.32 djm 174: if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0)
1.19 markus 175: goto out;
176: if (ssh->compat & SSH_OLD_DHGEX)
177: kex->min = kex->max = -1;
1.1 markus 178:
179: /* calc and verify H */
1.28 djm 180: DH_get0_key(kex->dh, &pub_key, NULL);
181: DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
1.19 markus 182: hashlen = sizeof(hash);
183: if ((r = kexgex_hash(
1.14 djm 184: kex->hash_alg,
1.29 djm 185: kex->client_version,
186: kex->server_version,
1.34 djm 187: kex->my,
188: kex->peer,
189: server_host_key_blob,
1.19 markus 190: kex->min, kex->nbits, kex->max,
1.28 djm 191: dh_p, dh_g,
192: pub_key,
1.1 markus 193: dh_server_pub,
1.32 djm 194: sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
1.19 markus 195: hash, &hashlen)) != 0)
196: goto out;
1.3 djm 197:
1.19 markus 198: if ((r = sshkey_verify(server_host_key, signature, slen, hash,
1.35 djm 199: hashlen, kex->hostkey_alg, ssh->compat, NULL)) != 0)
1.19 markus 200: goto out;
1.1 markus 201:
1.32 djm 202: if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
1.19 markus 203: r = kex_send_newkeys(ssh);
204: out:
205: explicit_bzero(hash, sizeof(hash));
206: DH_free(kex->dh);
207: kex->dh = NULL;
1.27 jsing 208: BN_clear_free(dh_server_pub);
1.32 djm 209: sshbuf_free(shared_secret);
1.19 markus 210: sshkey_free(server_host_key);
1.34 djm 211: sshbuf_free(tmp);
212: sshbuf_free(server_host_key_blob);
1.19 markus 213: free(signature);
214: return r;
1.1 markus 215: }