Annotation of src/usr.bin/ssh/kexdhc.c, Revision 1.11
1.11 ! markus 1: /* $OpenBSD: kexdhc.c,v 1.10 2006/10/31 16:33:12 markus Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2001 Markus Friedl. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: *
14: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24: */
25:
1.9 deraadt 26: #include <sys/types.h>
1.7 stevesk 27:
1.8 stevesk 28: #include <stdio.h>
1.7 stevesk 29: #include <string.h>
1.9 deraadt 30: #include <signal.h>
1.1 markus 31:
32: #include "xmalloc.h"
1.9 deraadt 33: #include "buffer.h"
1.1 markus 34: #include "key.h"
1.9 deraadt 35: #include "cipher.h"
1.1 markus 36: #include "kex.h"
37: #include "log.h"
38: #include "packet.h"
39: #include "dh.h"
40: #include "ssh2.h"
41:
42: void
43: kexdh_client(Kex *kex)
44: {
45: BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
46: DH *dh;
47: Key *server_host_key;
48: u_char *server_host_key_blob = NULL, *signature = NULL;
49: u_char *kbuf, *hash;
1.10 markus 50: u_int klen, slen, sbloblen, hashlen;
51: int kout;
1.1 markus 52:
53: /* generate and send 'e', client DH public key */
1.2 djm 54: switch (kex->kex_type) {
55: case KEX_DH_GRP1_SHA1:
56: dh = dh_new_group1();
57: break;
58: case KEX_DH_GRP14_SHA1:
59: dh = dh_new_group14();
60: break;
61: default:
62: fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
63: }
1.1 markus 64: dh_gen_key(dh, kex->we_need * 8);
65: packet_start(SSH2_MSG_KEXDH_INIT);
66: packet_put_bignum2(dh->pub_key);
67: packet_send();
68:
69: debug("sending SSH2_MSG_KEXDH_INIT");
70: #ifdef DEBUG_KEXDH
71: DHparams_print_fp(stderr, dh);
72: fprintf(stderr, "pub= ");
73: BN_print_fp(stderr, dh->pub_key);
74: fprintf(stderr, "\n");
75: #endif
76:
77: debug("expecting SSH2_MSG_KEXDH_REPLY");
78: packet_read_expect(SSH2_MSG_KEXDH_REPLY);
79:
80: /* key, cert */
81: server_host_key_blob = packet_get_string(&sbloblen);
82: server_host_key = key_from_blob(server_host_key_blob, sbloblen);
83: if (server_host_key == NULL)
84: fatal("cannot decode server_host_key_blob");
85: if (server_host_key->type != kex->hostkey_type)
86: fatal("type mismatch for decoded server_host_key_blob");
87: if (kex->verify_host_key == NULL)
88: fatal("cannot verify server_host_key");
89: if (kex->verify_host_key(server_host_key) == -1)
90: fatal("server_host_key verification failed");
91:
1.6 miod 92: /* DH parameter f, server public DH key */
1.1 markus 93: if ((dh_server_pub = BN_new()) == NULL)
94: fatal("dh_server_pub == NULL");
95: packet_get_bignum2(dh_server_pub);
96:
97: #ifdef DEBUG_KEXDH
98: fprintf(stderr, "dh_server_pub= ");
99: BN_print_fp(stderr, dh_server_pub);
100: fprintf(stderr, "\n");
101: debug("bits %d", BN_num_bits(dh_server_pub));
102: #endif
103:
104: /* signed H */
105: signature = packet_get_string(&slen);
106: packet_check_eom();
107:
108: if (!dh_pub_is_valid(dh, dh_server_pub))
109: packet_disconnect("bad server public DH value");
110:
111: klen = DH_size(dh);
112: kbuf = xmalloc(klen);
1.10 markus 113: if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0)
114: fatal("DH_compute_key: failed");
1.1 markus 115: #ifdef DEBUG_KEXDH
116: dump_digest("shared secret", kbuf, kout);
117: #endif
118: if ((shared_secret = BN_new()) == NULL)
119: fatal("kexdh_client: BN_new failed");
1.11 ! markus 120: if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
! 121: fatal("kexdh_client: BN_bin2bn failed");
1.1 markus 122: memset(kbuf, 0, klen);
123: xfree(kbuf);
124:
125: /* calc and verify H */
1.3 djm 126: kex_dh_hash(
1.1 markus 127: kex->client_version_string,
128: kex->server_version_string,
129: buffer_ptr(&kex->my), buffer_len(&kex->my),
130: buffer_ptr(&kex->peer), buffer_len(&kex->peer),
131: server_host_key_blob, sbloblen,
132: dh->pub_key,
133: dh_server_pub,
1.3 djm 134: shared_secret,
135: &hash, &hashlen
1.1 markus 136: );
137: xfree(server_host_key_blob);
138: BN_clear_free(dh_server_pub);
139: DH_free(dh);
140:
1.3 djm 141: if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
1.1 markus 142: fatal("key_verify failed for server_host_key");
143: key_free(server_host_key);
144: xfree(signature);
145:
146: /* save session id */
147: if (kex->session_id == NULL) {
1.3 djm 148: kex->session_id_len = hashlen;
1.1 markus 149: kex->session_id = xmalloc(kex->session_id_len);
150: memcpy(kex->session_id, hash, kex->session_id_len);
151: }
152:
1.3 djm 153: kex_derive_keys(kex, hash, hashlen, shared_secret);
1.1 markus 154: BN_clear_free(shared_secret);
155: kex_finish(kex);
156: }