No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / kexdhs.c
blob6704213d066d5b4907905f8ac2fa65626a5a76f0
1 /* $NetBSD: kexdhs.c,v 1.1.1.2 2009/12/27 01:06:56 christos Exp $ */
2 /* $OpenBSD: kexdhs.c,v 1.10 2009/06/21 07:37:15 dtucker Exp $ */
3 /*
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "includes.h"
28 __RCSID("$NetBSD: kexdhs.c,v 1.2 2009/06/07 22:38:46 christos Exp $");
29 #include <sys/types.h>
30 #include <string.h>
31 #include <signal.h>
33 #include "xmalloc.h"
34 #include "buffer.h"
35 #include "key.h"
36 #include "cipher.h"
37 #include "kex.h"
38 #include "log.h"
39 #include "packet.h"
40 #include "dh.h"
41 #include "ssh2.h"
42 #ifdef GSSAPI
43 #include "ssh-gss.h"
44 #endif
45 #include "monitor_wrap.h"
47 void
48 kexdh_server(Kex *kex)
50 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
51 DH *dh = NULL; /* XXX: GCC */
52 Key *server_host_key;
53 u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
54 u_int sbloblen, klen, hashlen, slen;
55 int kout;
57 /* generate server DH public key */
58 switch (kex->kex_type) {
59 case KEX_DH_GRP1_SHA1:
60 dh = dh_new_group1();
61 break;
62 case KEX_DH_GRP14_SHA1:
63 dh = dh_new_group14();
64 break;
65 default:
66 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type);
68 dh_gen_key(dh, kex->we_need * 8);
70 debug("expecting SSH2_MSG_KEXDH_INIT");
71 packet_read_expect(SSH2_MSG_KEXDH_INIT);
73 if (kex->load_host_key == NULL)
74 fatal("Cannot load hostkey");
75 server_host_key = kex->load_host_key(kex->hostkey_type);
76 if (server_host_key == NULL)
77 fatal("Unsupported hostkey type %d", kex->hostkey_type);
79 /* key, cert */
80 if ((dh_client_pub = BN_new()) == NULL)
81 fatal("dh_client_pub == NULL");
82 packet_get_bignum2(dh_client_pub);
83 packet_check_eom();
85 #ifdef DEBUG_KEXDH
86 fprintf(stderr, "dh_client_pub= ");
87 BN_print_fp(stderr, dh_client_pub);
88 fprintf(stderr, "\n");
89 debug("bits %d", BN_num_bits(dh_client_pub));
90 #endif
92 #ifdef DEBUG_KEXDH
93 DHparams_print_fp(stderr, dh);
94 fprintf(stderr, "pub= ");
95 BN_print_fp(stderr, dh->pub_key);
96 fprintf(stderr, "\n");
97 #endif
98 if (!dh_pub_is_valid(dh, dh_client_pub))
99 packet_disconnect("bad client public DH value");
101 klen = DH_size(dh);
102 kbuf = xmalloc(klen);
103 if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
104 fatal("DH_compute_key: failed");
105 #ifdef DEBUG_KEXDH
106 dump_digest("shared secret", kbuf, kout);
107 #endif
108 if ((shared_secret = BN_new()) == NULL)
109 fatal("kexdh_server: BN_new failed");
110 if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
111 fatal("kexdh_server: BN_bin2bn failed");
112 memset(kbuf, 0, klen);
113 xfree(kbuf);
115 key_to_blob(server_host_key, &server_host_key_blob, &sbloblen);
117 /* calc H */
118 kex_dh_hash(
119 kex->client_version_string,
120 kex->server_version_string,
121 buffer_ptr(&kex->peer), buffer_len(&kex->peer),
122 buffer_ptr(&kex->my), buffer_len(&kex->my),
123 server_host_key_blob, sbloblen,
124 dh_client_pub,
125 dh->pub_key,
126 shared_secret,
127 &hash, &hashlen
129 BN_clear_free(dh_client_pub);
131 /* save session id := H */
132 if (kex->session_id == NULL) {
133 kex->session_id_len = hashlen;
134 kex->session_id = xmalloc(kex->session_id_len);
135 memcpy(kex->session_id, hash, kex->session_id_len);
138 /* sign H */
139 if (PRIVSEP(key_sign(server_host_key, &signature, &slen, hash,
140 hashlen)) < 0)
141 fatal("kexdh_server: key_sign failed");
143 /* destroy_sensitive_data(); */
145 /* send server hostkey, DH pubkey 'f' and singed H */
146 packet_start(SSH2_MSG_KEXDH_REPLY);
147 packet_put_string(server_host_key_blob, sbloblen);
148 packet_put_bignum2(dh->pub_key); /* f */
149 packet_put_string(signature, slen);
150 packet_send();
152 xfree(signature);
153 xfree(server_host_key_blob);
154 /* have keys, free DH */
155 DH_free(dh);
157 kex_derive_keys(kex, hash, hashlen, shared_secret);
158 BN_clear_free(shared_secret);
159 kex_finish(kex);