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