Annotation of src/usr.bin/ssh/kexdhc.c, Revision 1.9
1.9 ! deraadt 1: /* $OpenBSD: kexdhc.c,v 1.8 2006/08/01 23:22:47 stevesk 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.3 djm 50: u_int klen, kout, slen, sbloblen, hashlen;
1.1 markus 51:
52: /* generate and send 'e', client DH public key */
1.2 djm 53: switch (kex->kex_type) {
54: case KEX_DH_GRP1_SHA1:
55: dh = dh_new_group1();
56: break;
57: case KEX_DH_GRP14_SHA1:
58: dh = dh_new_group14();
59: break;
60: default:
61: fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
62: }
1.1 markus 63: dh_gen_key(dh, kex->we_need * 8);
64: packet_start(SSH2_MSG_KEXDH_INIT);
65: packet_put_bignum2(dh->pub_key);
66: packet_send();
67:
68: debug("sending SSH2_MSG_KEXDH_INIT");
69: #ifdef DEBUG_KEXDH
70: DHparams_print_fp(stderr, dh);
71: fprintf(stderr, "pub= ");
72: BN_print_fp(stderr, dh->pub_key);
73: fprintf(stderr, "\n");
74: #endif
75:
76: debug("expecting SSH2_MSG_KEXDH_REPLY");
77: packet_read_expect(SSH2_MSG_KEXDH_REPLY);
78:
79: /* key, cert */
80: server_host_key_blob = packet_get_string(&sbloblen);
81: server_host_key = key_from_blob(server_host_key_blob, sbloblen);
82: if (server_host_key == NULL)
83: fatal("cannot decode server_host_key_blob");
84: if (server_host_key->type != kex->hostkey_type)
85: fatal("type mismatch for decoded server_host_key_blob");
86: if (kex->verify_host_key == NULL)
87: fatal("cannot verify server_host_key");
88: if (kex->verify_host_key(server_host_key) == -1)
89: fatal("server_host_key verification failed");
90:
1.6 miod 91: /* DH parameter f, server public DH key */
1.1 markus 92: if ((dh_server_pub = BN_new()) == NULL)
93: fatal("dh_server_pub == NULL");
94: packet_get_bignum2(dh_server_pub);
95:
96: #ifdef DEBUG_KEXDH
97: fprintf(stderr, "dh_server_pub= ");
98: BN_print_fp(stderr, dh_server_pub);
99: fprintf(stderr, "\n");
100: debug("bits %d", BN_num_bits(dh_server_pub));
101: #endif
102:
103: /* signed H */
104: signature = packet_get_string(&slen);
105: packet_check_eom();
106:
107: if (!dh_pub_is_valid(dh, dh_server_pub))
108: packet_disconnect("bad server public DH value");
109:
110: klen = DH_size(dh);
111: kbuf = xmalloc(klen);
112: kout = DH_compute_key(kbuf, dh_server_pub, dh);
113: #ifdef DEBUG_KEXDH
114: dump_digest("shared secret", kbuf, kout);
115: #endif
116: if ((shared_secret = BN_new()) == NULL)
117: fatal("kexdh_client: BN_new failed");
118: BN_bin2bn(kbuf, kout, shared_secret);
119: memset(kbuf, 0, klen);
120: xfree(kbuf);
121:
122: /* calc and verify H */
1.3 djm 123: kex_dh_hash(
1.1 markus 124: kex->client_version_string,
125: kex->server_version_string,
126: buffer_ptr(&kex->my), buffer_len(&kex->my),
127: buffer_ptr(&kex->peer), buffer_len(&kex->peer),
128: server_host_key_blob, sbloblen,
129: dh->pub_key,
130: dh_server_pub,
1.3 djm 131: shared_secret,
132: &hash, &hashlen
1.1 markus 133: );
134: xfree(server_host_key_blob);
135: BN_clear_free(dh_server_pub);
136: DH_free(dh);
137:
1.3 djm 138: if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
1.1 markus 139: fatal("key_verify failed for server_host_key");
140: key_free(server_host_key);
141: xfree(signature);
142:
143: /* save session id */
144: if (kex->session_id == NULL) {
1.3 djm 145: kex->session_id_len = hashlen;
1.1 markus 146: kex->session_id = xmalloc(kex->session_id_len);
147: memcpy(kex->session_id, hash, kex->session_id_len);
148: }
149:
1.3 djm 150: kex_derive_keys(kex, hash, hashlen, shared_secret);
1.1 markus 151: BN_clear_free(shared_secret);
152: kex_finish(kex);
153: }