]> andersk Git - openssh.git/blob - kexdhc.c
- deraadt@cvs.openbsd.org 2006/08/03 03:34:42
[openssh.git] / kexdhc.c
1 /* $OpenBSD: kexdhc.c,v 1.9 2006/08/03 03:34:42 deraadt Exp $ */
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
26 #include "includes.h"
27
28 #include <sys/types.h>
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <signal.h>
33
34 #include "xmalloc.h"
35 #include "buffer.h"
36 #include "key.h"
37 #include "cipher.h"
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;
52         u_int klen, kout, slen, sbloblen, hashlen;
53
54         /* generate and send 'e', client DH public key */
55         switch (kex->kex_type) {
56         case KEX_DH_GRP1_SHA1:
57                 dh = dh_new_group1();
58                 break;
59         case KEX_DH_GRP14_SHA1:
60                 dh = dh_new_group14();
61                 break;
62         default:
63                 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
64         }
65         dh_gen_key(dh, kex->we_need * 8);
66         packet_start(SSH2_MSG_KEXDH_INIT);
67         packet_put_bignum2(dh->pub_key);
68         packet_send();
69
70         debug("sending SSH2_MSG_KEXDH_INIT");
71 #ifdef DEBUG_KEXDH
72         DHparams_print_fp(stderr, dh);
73         fprintf(stderr, "pub= ");
74         BN_print_fp(stderr, dh->pub_key);
75         fprintf(stderr, "\n");
76 #endif
77
78         debug("expecting SSH2_MSG_KEXDH_REPLY");
79         packet_read_expect(SSH2_MSG_KEXDH_REPLY);
80
81         /* key, cert */
82         server_host_key_blob = packet_get_string(&sbloblen);
83         server_host_key = key_from_blob(server_host_key_blob, sbloblen);
84         if (server_host_key == NULL)
85                 fatal("cannot decode server_host_key_blob");
86         if (server_host_key->type != kex->hostkey_type)
87                 fatal("type mismatch for decoded server_host_key_blob");
88         if (kex->verify_host_key == NULL)
89                 fatal("cannot verify server_host_key");
90         if (kex->verify_host_key(server_host_key) == -1)
91                 fatal("server_host_key verification failed");
92
93         /* DH parameter f, server public DH key */
94         if ((dh_server_pub = BN_new()) == NULL)
95                 fatal("dh_server_pub == NULL");
96         packet_get_bignum2(dh_server_pub);
97
98 #ifdef DEBUG_KEXDH
99         fprintf(stderr, "dh_server_pub= ");
100         BN_print_fp(stderr, dh_server_pub);
101         fprintf(stderr, "\n");
102         debug("bits %d", BN_num_bits(dh_server_pub));
103 #endif
104
105         /* signed H */
106         signature = packet_get_string(&slen);
107         packet_check_eom();
108
109         if (!dh_pub_is_valid(dh, dh_server_pub))
110                 packet_disconnect("bad server public DH value");
111
112         klen = DH_size(dh);
113         kbuf = xmalloc(klen);
114         kout = DH_compute_key(kbuf, dh_server_pub, dh);
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");
120         BN_bin2bn(kbuf, kout, shared_secret);
121         memset(kbuf, 0, klen);
122         xfree(kbuf);
123
124         /* calc and verify H */
125         kex_dh_hash(
126             kex->client_version_string,
127             kex->server_version_string,
128             buffer_ptr(&kex->my), buffer_len(&kex->my),
129             buffer_ptr(&kex->peer), buffer_len(&kex->peer),
130             server_host_key_blob, sbloblen,
131             dh->pub_key,
132             dh_server_pub,
133             shared_secret,
134             &hash, &hashlen
135         );
136         xfree(server_host_key_blob);
137         BN_clear_free(dh_server_pub);
138         DH_free(dh);
139
140         if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1)
141                 fatal("key_verify failed for server_host_key");
142         key_free(server_host_key);
143         xfree(signature);
144
145         /* save session id */
146         if (kex->session_id == NULL) {
147                 kex->session_id_len = hashlen;
148                 kex->session_id = xmalloc(kex->session_id_len);
149                 memcpy(kex->session_id, hash, kex->session_id_len);
150         }
151
152         kex_derive_keys(kex, hash, hashlen, shared_secret);
153         BN_clear_free(shared_secret);
154         kex_finish(kex);
155 }
This page took 0.070021 seconds and 5 git commands to generate.