Annotation of src/usr.bin/ssh/kexgexc.c, Revision 1.20
1.20 ! djm 1: /* $OpenBSD: kexgexc.c,v 1.19 2015/01/19 20:16:15 markus 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.1 markus 47:
1.19 markus 48: static int input_kex_dh_gex_group(int, u_int32_t, void *);
49: static int input_kex_dh_gex_reply(int, u_int32_t, void *);
50:
51: int
52: kexgex_client(struct ssh *ssh)
1.1 markus 53: {
1.19 markus 54: struct kex *kex = ssh->kex;
55: int r;
56: u_int nbits;
1.1 markus 57:
1.16 dtucker 58: nbits = dh_estimate(kex->dh_need * 8);
1.1 markus 59:
1.19 markus 60: kex->min = DH_GRP_MIN;
61: kex->max = DH_GRP_MAX;
62: kex->nbits = nbits;
63: if (ssh->compat & SSH_OLD_DHGEX) {
1.1 markus 64: /* Old GEX request */
1.19 markus 65: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD))
66: != 0 ||
67: (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
68: (r = sshpkt_send(ssh)) != 0)
69: goto out;
70: debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", kex->nbits);
1.1 markus 71: } else {
72: /* New GEX request */
1.19 markus 73: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST)) != 0 ||
74: (r = sshpkt_put_u32(ssh, kex->min)) != 0 ||
75: (r = sshpkt_put_u32(ssh, kex->nbits)) != 0 ||
76: (r = sshpkt_put_u32(ssh, kex->max)) != 0 ||
77: (r = sshpkt_send(ssh)) != 0)
78: goto out;
1.2 markus 79: debug("SSH2_MSG_KEX_DH_GEX_REQUEST(%u<%u<%u) sent",
1.19 markus 80: kex->min, kex->nbits, kex->max);
1.1 markus 81: }
82: #ifdef DEBUG_KEXDH
83: fprintf(stderr, "\nmin = %d, nbits = %d, max = %d\n",
1.19 markus 84: kex->min, kex->nbits, kex->max);
1.1 markus 85: #endif
1.19 markus 86: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP,
87: &input_kex_dh_gex_group);
88: r = 0;
89: out:
90: return r;
91: }
1.1 markus 92:
1.19 markus 93: static int
94: input_kex_dh_gex_group(int type, u_int32_t seq, void *ctxt)
95: {
96: struct ssh *ssh = ctxt;
97: struct kex *kex = ssh->kex;
98: BIGNUM *p = NULL, *g = NULL;
99: int r, bits;
1.1 markus 100:
1.19 markus 101: debug("got SSH2_MSG_KEX_DH_GEX_GROUP");
1.1 markus 102:
1.19 markus 103: if ((p = BN_new()) == NULL ||
104: (g = BN_new()) == NULL) {
105: r = SSH_ERR_ALLOC_FAIL;
106: goto out;
107: }
108: if ((r = sshpkt_get_bignum2(ssh, p)) != 0 ||
109: (r = sshpkt_get_bignum2(ssh, g)) != 0 ||
110: (r = sshpkt_get_end(ssh)) != 0)
111: goto out;
112: if ((bits = BN_num_bits(p)) < 0 ||
113: (u_int)bits < kex->min || (u_int)bits > kex->max) {
114: r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
115: goto out;
116: }
117: if ((kex->dh = dh_new_group(g, p)) == NULL) {
118: r = SSH_ERR_ALLOC_FAIL;
119: goto out;
120: }
121: p = g = NULL; /* belong to kex->dh now */
1.1 markus 122:
1.19 markus 123: /* generate and send 'e', client DH public key */
124: if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
125: (r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_INIT)) != 0 ||
126: (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
127: (r = sshpkt_send(ssh)) != 0)
128: goto out;
129: debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
1.1 markus 130: #ifdef DEBUG_KEXDH
1.19 markus 131: DHparams_print_fp(stderr, kex->dh);
1.1 markus 132: fprintf(stderr, "pub= ");
1.19 markus 133: BN_print_fp(stderr, kex->dh->pub_key);
1.1 markus 134: fprintf(stderr, "\n");
135: #endif
1.19 markus 136: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_GROUP, NULL);
137: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
138: r = 0;
139: out:
140: if (p)
141: BN_clear_free(p);
142: if (g)
143: BN_clear_free(g);
144: return r;
145: }
1.1 markus 146:
1.19 markus 147: static int
148: input_kex_dh_gex_reply(int type, u_int32_t seq, void *ctxt)
149: {
150: struct ssh *ssh = ctxt;
151: struct kex *kex = ssh->kex;
152: BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
153: struct sshkey *server_host_key = NULL;
154: u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
155: u_char hash[SSH_DIGEST_MAX_LENGTH];
156: size_t klen = 0, slen, sbloblen, hashlen;
157: int kout, r;
158:
159: debug("got SSH2_MSG_KEX_DH_GEX_REPLY");
160: if (kex->verify_host_key == NULL) {
161: r = SSH_ERR_INVALID_ARGUMENT;
162: goto out;
163: }
1.1 markus 164: /* key, cert */
1.19 markus 165: if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
166: &sbloblen)) != 0 ||
167: (r = sshkey_from_blob(server_host_key_blob, sbloblen,
168: &server_host_key)) != 0)
169: goto out;
170: if (server_host_key->type != kex->hostkey_type) {
1.20 ! djm 171: r = SSH_ERR_KEY_TYPE_MISMATCH;
! 172: goto out;
! 173: }
! 174: if (server_host_key->type != kex->hostkey_type ||
! 175: (kex->hostkey_type == KEY_ECDSA &&
! 176: server_host_key->ecdsa_nid != kex->hostkey_nid)) {
1.19 markus 177: r = SSH_ERR_KEY_TYPE_MISMATCH;
178: goto out;
179: }
180: if (kex->verify_host_key(server_host_key, ssh) == -1) {
181: r = SSH_ERR_SIGNATURE_INVALID;
182: goto out;
183: }
1.6 miod 184: /* DH parameter f, server public DH key */
1.19 markus 185: if ((dh_server_pub = BN_new()) == NULL) {
186: r = SSH_ERR_ALLOC_FAIL;
187: goto out;
188: }
189: /* signed H */
190: if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
191: (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
192: (r = sshpkt_get_end(ssh)) != 0)
193: goto out;
1.1 markus 194: #ifdef DEBUG_KEXDH
195: fprintf(stderr, "dh_server_pub= ");
196: BN_print_fp(stderr, dh_server_pub);
197: fprintf(stderr, "\n");
198: debug("bits %d", BN_num_bits(dh_server_pub));
199: #endif
1.19 markus 200: if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
201: sshpkt_disconnect(ssh, "bad server public DH value");
202: r = SSH_ERR_MESSAGE_INCOMPLETE;
203: goto out;
204: }
1.1 markus 205:
1.19 markus 206: klen = DH_size(kex->dh);
207: if ((kbuf = malloc(klen)) == NULL ||
208: (shared_secret = BN_new()) == NULL) {
209: r = SSH_ERR_ALLOC_FAIL;
210: goto out;
211: }
212: if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
213: BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
214: r = SSH_ERR_LIBCRYPTO_ERROR;
215: goto out;
216: }
1.1 markus 217: #ifdef DEBUG_KEXDH
218: dump_digest("shared secret", kbuf, kout);
219: #endif
1.19 markus 220: if (ssh->compat & SSH_OLD_DHGEX)
221: kex->min = kex->max = -1;
1.1 markus 222:
223: /* calc and verify H */
1.19 markus 224: hashlen = sizeof(hash);
225: if ((r = kexgex_hash(
1.14 djm 226: kex->hash_alg,
1.1 markus 227: kex->client_version_string,
228: kex->server_version_string,
1.19 markus 229: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
230: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
1.1 markus 231: server_host_key_blob, sbloblen,
1.19 markus 232: kex->min, kex->nbits, kex->max,
233: kex->dh->p, kex->dh->g,
234: kex->dh->pub_key,
1.1 markus 235: dh_server_pub,
1.3 djm 236: shared_secret,
1.19 markus 237: hash, &hashlen)) != 0)
238: goto out;
1.3 djm 239:
1.19 markus 240: if ((r = sshkey_verify(server_host_key, signature, slen, hash,
241: hashlen, ssh->compat)) != 0)
242: goto out;
1.1 markus 243:
244: /* save session id */
245: if (kex->session_id == NULL) {
1.3 djm 246: kex->session_id_len = hashlen;
1.19 markus 247: kex->session_id = malloc(kex->session_id_len);
248: if (kex->session_id == NULL) {
249: r = SSH_ERR_ALLOC_FAIL;
250: goto out;
251: }
1.1 markus 252: memcpy(kex->session_id, hash, kex->session_id_len);
253: }
254:
1.19 markus 255: if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
256: r = kex_send_newkeys(ssh);
257: out:
258: explicit_bzero(hash, sizeof(hash));
259: DH_free(kex->dh);
260: kex->dh = NULL;
261: if (dh_server_pub)
262: BN_clear_free(dh_server_pub);
263: if (kbuf) {
264: explicit_bzero(kbuf, klen);
265: free(kbuf);
266: }
267: if (shared_secret)
268: BN_clear_free(shared_secret);
269: sshkey_free(server_host_key);
270: free(server_host_key_blob);
271: free(signature);
272: return r;
1.1 markus 273: }