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 $ */
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
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.
28 __RCSID("$NetBSD: kexdhs.c,v 1.2 2009/06/07 22:38:46 christos Exp $");
29 #include <sys/types.h>
45 #include "monitor_wrap.h"
48 kexdh_server(Kex
*kex
)
50 BIGNUM
*shared_secret
= NULL
, *dh_client_pub
= NULL
;
51 DH
*dh
= NULL
; /* XXX: GCC */
53 u_char
*kbuf
, *hash
, *signature
= NULL
, *server_host_key_blob
= NULL
;
54 u_int sbloblen
, klen
, hashlen
, slen
;
57 /* generate server DH public key */
58 switch (kex
->kex_type
) {
59 case KEX_DH_GRP1_SHA1
:
62 case KEX_DH_GRP14_SHA1
:
63 dh
= dh_new_group14();
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
);
80 if ((dh_client_pub
= BN_new()) == NULL
)
81 fatal("dh_client_pub == NULL");
82 packet_get_bignum2(dh_client_pub
);
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
));
93 DHparams_print_fp(stderr
, dh
);
94 fprintf(stderr
, "pub= ");
95 BN_print_fp(stderr
, dh
->pub_key
);
96 fprintf(stderr
, "\n");
98 if (!dh_pub_is_valid(dh
, dh_client_pub
))
99 packet_disconnect("bad client public DH value");
102 kbuf
= xmalloc(klen
);
103 if ((kout
= DH_compute_key(kbuf
, dh_client_pub
, dh
)) < 0)
104 fatal("DH_compute_key: failed");
106 dump_digest("shared secret", kbuf
, kout
);
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
);
115 key_to_blob(server_host_key
, &server_host_key_blob
, &sbloblen
);
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
,
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
);
139 if (PRIVSEP(key_sign(server_host_key
, &signature
, &slen
, hash
,
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
);
153 xfree(server_host_key_blob
);
154 /* have keys, free DH */
157 kex_derive_keys(kex
, hash
, hashlen
, shared_secret
);
158 BN_clear_free(shared_secret
);