]>
Commit | Line | Data |
---|---|---|
1c14df9e | 1 | /* |
2 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | #include "includes.h" | |
26 | RCSID("$OpenBSD: kexdhs.c,v 1.1 2003/02/16 17:09:57 markus Exp $"); | |
27 | ||
28 | #include "xmalloc.h" | |
29 | #include "key.h" | |
30 | #include "kex.h" | |
31 | #include "log.h" | |
32 | #include "packet.h" | |
33 | #include "dh.h" | |
34 | #include "ssh2.h" | |
35 | #include "monitor_wrap.h" | |
36 | ||
37 | void | |
38 | kexdh_server(Kex *kex) | |
39 | { | |
40 | BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; | |
41 | DH *dh; | |
42 | Key *server_host_key; | |
43 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; | |
44 | u_int sbloblen, klen, kout; | |
45 | u_int slen; | |
46 | ||
47 | /* generate server DH public key */ | |
48 | dh = dh_new_group1(); | |
49 | dh_gen_key(dh, kex->we_need * 8); | |
50 | ||
51 | debug("expecting SSH2_MSG_KEXDH_INIT"); | |
52 | packet_read_expect(SSH2_MSG_KEXDH_INIT); | |
53 | ||
54 | if (kex->load_host_key == NULL) | |
55 | fatal("Cannot load hostkey"); | |
56 | server_host_key = kex->load_host_key(kex->hostkey_type); | |
57 | if (server_host_key == NULL) | |
58 | fatal("Unsupported hostkey type %d", kex->hostkey_type); | |
59 | ||
60 | /* key, cert */ | |
61 | if ((dh_client_pub = BN_new()) == NULL) | |
62 | fatal("dh_client_pub == NULL"); | |
63 | packet_get_bignum2(dh_client_pub); | |
64 | packet_check_eom(); | |
65 | ||
66 | #ifdef DEBUG_KEXDH | |
67 | fprintf(stderr, "dh_client_pub= "); | |
68 | BN_print_fp(stderr, dh_client_pub); | |
69 | fprintf(stderr, "\n"); | |
70 | debug("bits %d", BN_num_bits(dh_client_pub)); | |
71 | #endif | |
72 | ||
73 | #ifdef DEBUG_KEXDH | |
74 | DHparams_print_fp(stderr, dh); | |
75 | fprintf(stderr, "pub= "); | |
76 | BN_print_fp(stderr, dh->pub_key); | |
77 | fprintf(stderr, "\n"); | |
78 | #endif | |
79 | if (!dh_pub_is_valid(dh, dh_client_pub)) | |
80 | packet_disconnect("bad client public DH value"); | |
81 | ||
82 | klen = DH_size(dh); | |
83 | kbuf = xmalloc(klen); | |
84 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | |
85 | #ifdef DEBUG_KEXDH | |
86 | dump_digest("shared secret", kbuf, kout); | |
87 | #endif | |
88 | if ((shared_secret = BN_new()) == NULL) | |
89 | fatal("kexdh_server: BN_new failed"); | |
90 | BN_bin2bn(kbuf, kout, shared_secret); | |
91 | memset(kbuf, 0, klen); | |
92 | xfree(kbuf); | |
93 | ||
94 | key_to_blob(server_host_key, &server_host_key_blob, &sbloblen); | |
95 | ||
96 | /* calc H */ | |
97 | hash = kex_dh_hash( | |
98 | kex->client_version_string, | |
99 | kex->server_version_string, | |
100 | buffer_ptr(&kex->peer), buffer_len(&kex->peer), | |
101 | buffer_ptr(&kex->my), buffer_len(&kex->my), | |
102 | server_host_key_blob, sbloblen, | |
103 | dh_client_pub, | |
104 | dh->pub_key, | |
105 | shared_secret | |
106 | ); | |
107 | BN_clear_free(dh_client_pub); | |
108 | ||
109 | /* save session id := H */ | |
110 | /* XXX hashlen depends on KEX */ | |
111 | if (kex->session_id == NULL) { | |
112 | kex->session_id_len = 20; | |
113 | kex->session_id = xmalloc(kex->session_id_len); | |
114 | memcpy(kex->session_id, hash, kex->session_id_len); | |
115 | } | |
116 | ||
117 | /* sign H */ | |
118 | /* XXX hashlen depends on KEX */ | |
119 | PRIVSEP(key_sign(server_host_key, &signature, &slen, hash, 20)); | |
120 | ||
121 | /* destroy_sensitive_data(); */ | |
122 | ||
123 | /* send server hostkey, DH pubkey 'f' and singed H */ | |
124 | packet_start(SSH2_MSG_KEXDH_REPLY); | |
125 | packet_put_string(server_host_key_blob, sbloblen); | |
126 | packet_put_bignum2(dh->pub_key); /* f */ | |
127 | packet_put_string(signature, slen); | |
128 | packet_send(); | |
129 | ||
130 | xfree(signature); | |
131 | xfree(server_host_key_blob); | |
132 | /* have keys, free DH */ | |
133 | DH_free(dh); | |
134 | ||
135 | kex_derive_keys(kex, hash, shared_secret); | |
136 | BN_clear_free(shared_secret); | |
137 | kex_finish(kex); | |
138 | } |