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