Annotation of src/usr.bin/ssh/kexgexs.c, Revision 1.24
1.24 ! djm 1: /* $OpenBSD: kexgexs.c,v 1.23 2015/01/20 23:14:00 deraadt 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.23 deraadt 27: #include <sys/param.h> /* MIN MAX */
1.5 stevesk 28:
1.7 stevesk 29: #include <stdio.h>
1.5 stevesk 30: #include <string.h>
1.8 deraadt 31: #include <signal.h>
1.14 djm 32:
33: #include <openssl/dh.h>
1.1 markus 34:
1.21 markus 35: #include "sshkey.h"
1.8 deraadt 36: #include "cipher.h"
1.21 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.8 deraadt 44: #ifdef GSSAPI
45: #include "ssh-gss.h"
46: #endif
1.1 markus 47: #include "monitor_wrap.h"
1.21 markus 48: #include "dispatch.h"
49: #include "ssherr.h"
50: #include "sshbuf.h"
1.1 markus 51:
1.21 markus 52: static int input_kex_dh_gex_request(int, u_int32_t, void *);
53: static int input_kex_dh_gex_init(int, u_int32_t, void *);
54:
55: int
56: kexgex_server(struct ssh *ssh)
1.1 markus 57: {
1.21 markus 58: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST_OLD,
59: &input_kex_dh_gex_request);
60: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
61: &input_kex_dh_gex_request);
62: debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
63: return 0;
64: }
1.1 markus 65:
1.21 markus 66: static int
67: input_kex_dh_gex_request(int type, u_int32_t seq, void *ctxt)
68: {
69: struct ssh *ssh = ctxt;
70: struct kex *kex = ssh->kex;
71: int r;
72: u_int min = 0, max = 0, nbits = 0;
1.1 markus 73:
74: switch (type) {
75: case SSH2_MSG_KEX_DH_GEX_REQUEST:
76: debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
1.21 markus 77: if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
78: (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
79: (r = sshpkt_get_u32(ssh, &max)) != 0 ||
80: (r = sshpkt_get_end(ssh)) != 0)
81: goto out;
82: kex->nbits = nbits;
83: kex->min = min;
84: kex->max = max;
1.1 markus 85: min = MAX(DH_GRP_MIN, min);
86: max = MIN(DH_GRP_MAX, max);
1.11 djm 87: nbits = MAX(DH_GRP_MIN, nbits);
88: nbits = MIN(DH_GRP_MAX, nbits);
1.1 markus 89: break;
90: case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
91: debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
1.21 markus 92: if ((r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
93: (r = sshpkt_get_end(ssh)) != 0)
94: goto out;
95: kex->nbits = nbits;
1.1 markus 96: /* unused for old GEX */
1.21 markus 97: kex->min = min = DH_GRP_MIN;
98: kex->max = max = DH_GRP_MAX;
1.1 markus 99: break;
100: default:
1.21 markus 101: r = SSH_ERR_INVALID_ARGUMENT;
102: goto out;
1.1 markus 103: }
104:
1.21 markus 105: if (kex->max < kex->min || kex->nbits < kex->min ||
106: kex->max < kex->nbits) {
107: r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
108: goto out;
109: }
1.1 markus 110:
111: /* Contact privileged parent */
1.21 markus 112: kex->dh = PRIVSEP(choose_dh(min, nbits, max));
113: if (kex->dh == NULL) {
114: sshpkt_disconnect(ssh, "no matching DH grp found");
115: r = SSH_ERR_ALLOC_FAIL;
116: goto out;
117: }
1.1 markus 118: debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
1.21 markus 119: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
120: (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 ||
121: (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 ||
122: (r = sshpkt_send(ssh)) != 0)
123: goto out;
1.1 markus 124:
1.21 markus 125: /* Compute our exchange value in parallel with the client */
126: if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
127: goto out;
1.1 markus 128:
1.21 markus 129: /* old KEX does not use min/max in kexgex_hash() */
130: if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
131: kex->min = kex->max = -1;
1.1 markus 132:
133: debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
1.21 markus 134: ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
135: r = 0;
136: out:
137: return r;
138: }
139:
140: static int
141: input_kex_dh_gex_init(int type, u_int32_t seq, void *ctxt)
142: {
143: struct ssh *ssh = ctxt;
144: struct kex *kex = ssh->kex;
145: BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
146: struct sshkey *server_host_public, *server_host_private;
147: u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
148: u_char hash[SSH_DIGEST_MAX_LENGTH];
149: size_t sbloblen, slen;
150: size_t klen = 0, hashlen;
151: int kout, r;
152:
153: if (kex->load_host_public_key == NULL ||
154: kex->load_host_private_key == NULL) {
155: r = SSH_ERR_INVALID_ARGUMENT;
156: goto out;
157: }
1.24 ! djm 158: server_host_public = kex->load_host_public_key(kex->hostkey_type,
! 159: kex->hostkey_nid, ssh);
! 160: server_host_private = kex->load_host_private_key(kex->hostkey_type,
! 161: kex->hostkey_nid, ssh);
1.22 djm 162: if (server_host_public == NULL) {
1.21 markus 163: r = SSH_ERR_NO_HOSTKEY_LOADED;
164: goto out;
165: }
1.1 markus 166:
167: /* key, cert */
1.21 markus 168: if ((dh_client_pub = BN_new()) == NULL) {
169: r = SSH_ERR_ALLOC_FAIL;
170: goto out;
171: }
172: if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
173: (r = sshpkt_get_end(ssh)) != 0)
174: goto out;
1.1 markus 175:
176: #ifdef DEBUG_KEXDH
177: fprintf(stderr, "dh_client_pub= ");
178: BN_print_fp(stderr, dh_client_pub);
179: fprintf(stderr, "\n");
180: debug("bits %d", BN_num_bits(dh_client_pub));
181: #endif
182:
183: #ifdef DEBUG_KEXDH
1.21 markus 184: DHparams_print_fp(stderr, kex->dh);
1.1 markus 185: fprintf(stderr, "pub= ");
1.21 markus 186: BN_print_fp(stderr, kex->dh->pub_key);
1.1 markus 187: fprintf(stderr, "\n");
188: #endif
1.21 markus 189: if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
190: sshpkt_disconnect(ssh, "bad client public DH value");
191: r = SSH_ERR_MESSAGE_INCOMPLETE;
192: goto out;
193: }
1.1 markus 194:
1.21 markus 195: klen = DH_size(kex->dh);
196: if ((kbuf = malloc(klen)) == NULL ||
197: (shared_secret = BN_new()) == NULL) {
198: r = SSH_ERR_ALLOC_FAIL;
199: goto out;
200: }
201: if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
202: BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
203: r = SSH_ERR_LIBCRYPTO_ERROR;
204: goto out;
205: }
1.1 markus 206: #ifdef DEBUG_KEXDH
207: dump_digest("shared secret", kbuf, kout);
208: #endif
1.21 markus 209: if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
210: &sbloblen)) != 0)
211: goto out;
1.2 djm 212: /* calc H */
1.21 markus 213: hashlen = sizeof(hash);
214: if ((r = kexgex_hash(
1.17 djm 215: kex->hash_alg,
1.1 markus 216: kex->client_version_string,
217: kex->server_version_string,
1.21 markus 218: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
219: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1 markus 220: server_host_key_blob, sbloblen,
1.21 markus 221: kex->min, kex->nbits, kex->max,
222: kex->dh->p, kex->dh->g,
1.1 markus 223: dh_client_pub,
1.21 markus 224: kex->dh->pub_key,
1.2 djm 225: shared_secret,
1.21 markus 226: hash, &hashlen)) != 0)
227: goto out;
1.1 markus 228:
229: /* save session id := H */
230: if (kex->session_id == NULL) {
1.2 djm 231: kex->session_id_len = hashlen;
1.21 markus 232: kex->session_id = malloc(kex->session_id_len);
233: if (kex->session_id == NULL) {
234: r = SSH_ERR_ALLOC_FAIL;
235: goto out;
236: }
1.1 markus 237: memcpy(kex->session_id, hash, kex->session_id_len);
238: }
239:
240: /* sign H */
1.21 markus 241: if ((r = kex->sign(server_host_private, server_host_public,
242: &signature, &slen, hash, hashlen, ssh->compat)) < 0)
243: goto out;
1.1 markus 244:
245: /* destroy_sensitive_data(); */
246:
247: /* send server hostkey, DH pubkey 'f' and singed H */
1.21 markus 248: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
249: (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
250: (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
251: (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
252: (r = sshpkt_send(ssh)) != 0)
253: goto out;
254:
255: if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
256: r = kex_send_newkeys(ssh);
257: out:
258: DH_free(kex->dh);
259: kex->dh = NULL;
260: if (dh_client_pub)
261: BN_clear_free(dh_client_pub);
262: if (kbuf) {
263: explicit_bzero(kbuf, klen);
264: free(kbuf);
265: }
266: if (shared_secret)
267: BN_clear_free(shared_secret);
268: free(server_host_key_blob);
1.15 djm 269: free(signature);
1.21 markus 270: return r;
1.1 markus 271: }