diff options
author | markus@openbsd.org <markus@openbsd.org> | 2015-01-19 20:16:15 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2015-01-20 09:19:39 +1100 |
commit | 57d10cbe861a235dd269c74fb2fe248469ecee9d (patch) | |
tree | c65deed24700490bd3b20300c4829d4d5466ff6d /kexdhs.c | |
parent | 3fdc88a0def4f86aa88a5846ac079dc964c0546a (diff) |
upstream commit
adapt kex to sshbuf and struct ssh; ok djm@
Diffstat (limited to 'kexdhs.c')
-rw-r--r-- | kexdhs.c | 186 |
1 files changed, 119 insertions, 67 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: kexdhs.c,v 1.19 2015/01/19 19:52:16 markus Exp $ */ | 1 | /* $OpenBSD: kexdhs.c,v 1.20 2015/01/19 20:16:15 markus Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | 3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. |
4 | * | 4 | * |
@@ -35,55 +35,88 @@ | |||
35 | 35 | ||
36 | #include <openssl/dh.h> | 36 | #include <openssl/dh.h> |
37 | 37 | ||
38 | #include "xmalloc.h" | 38 | #include "sshkey.h" |
39 | #include "buffer.h" | ||
40 | #include "key.h" | ||
41 | #include "cipher.h" | 39 | #include "cipher.h" |
40 | #include "digest.h" | ||
42 | #include "kex.h" | 41 | #include "kex.h" |
43 | #include "log.h" | 42 | #include "log.h" |
44 | #include "packet.h" | 43 | #include "packet.h" |
45 | #include "dh.h" | 44 | #include "dh.h" |
46 | #include "ssh2.h" | 45 | #include "ssh2.h" |
47 | 46 | ||
48 | void | 47 | #include "dispatch.h" |
49 | kexdh_server(Kex *kex) | 48 | #include "compat.h" |
49 | #include "ssherr.h" | ||
50 | #include "sshbuf.h" | ||
51 | |||
52 | static int input_kex_dh_init(int, u_int32_t, void *); | ||
53 | |||
54 | int | ||
55 | kexdh_server(struct ssh *ssh) | ||
50 | { | 56 | { |
51 | BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; | 57 | struct kex *kex = ssh->kex; |
52 | DH *dh; | 58 | int r; |
53 | Key *server_host_public, *server_host_private; | ||
54 | u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL; | ||
55 | u_int sbloblen, klen, hashlen, slen; | ||
56 | int kout; | ||
57 | 59 | ||
58 | /* generate server DH public key */ | 60 | /* generate server DH public key */ |
59 | switch (kex->kex_type) { | 61 | switch (kex->kex_type) { |
60 | case KEX_DH_GRP1_SHA1: | 62 | case KEX_DH_GRP1_SHA1: |
61 | dh = dh_new_group1(); | 63 | kex->dh = dh_new_group1(); |
62 | break; | 64 | break; |
63 | case KEX_DH_GRP14_SHA1: | 65 | case KEX_DH_GRP14_SHA1: |
64 | dh = dh_new_group14(); | 66 | kex->dh = dh_new_group14(); |
65 | break; | 67 | break; |
66 | default: | 68 | default: |
67 | fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type); | 69 | r = SSH_ERR_INVALID_ARGUMENT; |
70 | goto out; | ||
68 | } | 71 | } |
69 | dh_gen_key(dh, kex->we_need * 8); | 72 | if (kex->dh == NULL) { |
73 | r = SSH_ERR_ALLOC_FAIL; | ||
74 | goto out; | ||
75 | } | ||
76 | if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) | ||
77 | goto out; | ||
70 | 78 | ||
71 | debug("expecting SSH2_MSG_KEXDH_INIT"); | 79 | debug("expecting SSH2_MSG_KEXDH_INIT"); |
72 | packet_read_expect(SSH2_MSG_KEXDH_INIT); | 80 | ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init); |
81 | r = 0; | ||
82 | out: | ||
83 | return r; | ||
84 | } | ||
85 | |||
86 | int | ||
87 | input_kex_dh_init(int type, u_int32_t seq, void *ctxt) | ||
88 | { | ||
89 | struct ssh *ssh = ctxt; | ||
90 | struct kex *kex = ssh->kex; | ||
91 | BIGNUM *shared_secret = NULL, *dh_client_pub = NULL; | ||
92 | struct sshkey *server_host_public, *server_host_private; | ||
93 | u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL; | ||
94 | u_char hash[SSH_DIGEST_MAX_LENGTH]; | ||
95 | size_t sbloblen, slen; | ||
96 | size_t klen = 0, hashlen; | ||
97 | int kout, r; | ||
73 | 98 | ||
74 | if (kex->load_host_public_key == NULL || | 99 | if (kex->load_host_public_key == NULL || |
75 | kex->load_host_private_key == NULL) | 100 | kex->load_host_private_key == NULL) { |
76 | fatal("Cannot load hostkey"); | 101 | r = SSH_ERR_INVALID_ARGUMENT; |
77 | server_host_public = kex->load_host_public_key(kex->hostkey_type); | 102 | goto out; |
78 | if (server_host_public == NULL) | 103 | } |
79 | fatal("Unsupported hostkey type %d", kex->hostkey_type); | 104 | if ((server_host_public = kex->load_host_public_key(kex->hostkey_type, |
80 | server_host_private = kex->load_host_private_key(kex->hostkey_type); | 105 | ssh)) == NULL || |
106 | (server_host_private = kex->load_host_private_key(kex->hostkey_type, | ||
107 | ssh)) == NULL) { | ||
108 | r = SSH_ERR_NO_HOSTKEY_LOADED; | ||
109 | goto out; | ||
110 | } | ||
81 | 111 | ||
82 | /* key, cert */ | 112 | /* key, cert */ |
83 | if ((dh_client_pub = BN_new()) == NULL) | 113 | if ((dh_client_pub = BN_new()) == NULL) { |
84 | fatal("dh_client_pub == NULL"); | 114 | r = SSH_ERR_ALLOC_FAIL; |
85 | packet_get_bignum2(dh_client_pub); | 115 | goto out; |
86 | packet_check_eom(); | 116 | } |
117 | if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 || | ||
118 | (r = sshpkt_get_end(ssh)) != 0) | ||
119 | goto out; | ||
87 | 120 | ||
88 | #ifdef DEBUG_KEXDH | 121 | #ifdef DEBUG_KEXDH |
89 | fprintf(stderr, "dh_client_pub= "); | 122 | fprintf(stderr, "dh_client_pub= "); |
@@ -93,71 +126,90 @@ kexdh_server(Kex *kex) | |||
93 | #endif | 126 | #endif |
94 | 127 | ||
95 | #ifdef DEBUG_KEXDH | 128 | #ifdef DEBUG_KEXDH |
96 | DHparams_print_fp(stderr, dh); | 129 | DHparams_print_fp(stderr, kex->dh); |
97 | fprintf(stderr, "pub= "); | 130 | fprintf(stderr, "pub= "); |
98 | BN_print_fp(stderr, dh->pub_key); | 131 | BN_print_fp(stderr, kex->dh->pub_key); |
99 | fprintf(stderr, "\n"); | 132 | fprintf(stderr, "\n"); |
100 | #endif | 133 | #endif |
101 | if (!dh_pub_is_valid(dh, dh_client_pub)) | 134 | if (!dh_pub_is_valid(kex->dh, dh_client_pub)) { |
102 | packet_disconnect("bad client public DH value"); | 135 | sshpkt_disconnect(ssh, "bad client public DH value"); |
136 | r = SSH_ERR_MESSAGE_INCOMPLETE; | ||
137 | goto out; | ||
138 | } | ||
103 | 139 | ||
104 | klen = DH_size(dh); | 140 | klen = DH_size(kex->dh); |
105 | kbuf = xmalloc(klen); | 141 | if ((kbuf = malloc(klen)) == NULL || |
106 | if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0) | 142 | (shared_secret = BN_new()) == NULL) { |
107 | fatal("DH_compute_key: failed"); | 143 | r = SSH_ERR_ALLOC_FAIL; |
144 | goto out; | ||
145 | } | ||
146 | if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 || | ||
147 | BN_bin2bn(kbuf, kout, shared_secret) == NULL) { | ||
148 | r = SSH_ERR_LIBCRYPTO_ERROR; | ||
149 | goto out; | ||
150 | } | ||
108 | #ifdef DEBUG_KEXDH | 151 | #ifdef DEBUG_KEXDH |
109 | dump_digest("shared secret", kbuf, kout); | 152 | dump_digest("shared secret", kbuf, kout); |
110 | #endif | 153 | #endif |
111 | if ((shared_secret = BN_new()) == NULL) | 154 | if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob, |
112 | fatal("kexdh_server: BN_new failed"); | 155 | &sbloblen)) != 0) |
113 | if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) | 156 | goto out; |
114 | fatal("kexdh_server: BN_bin2bn failed"); | ||
115 | explicit_bzero(kbuf, klen); | ||
116 | free(kbuf); | ||
117 | |||
118 | key_to_blob(server_host_public, &server_host_key_blob, &sbloblen); | ||
119 | |||
120 | /* calc H */ | 157 | /* calc H */ |
121 | kex_dh_hash( | 158 | hashlen = sizeof(hash); |
159 | if ((r = kex_dh_hash( | ||
122 | kex->client_version_string, | 160 | kex->client_version_string, |
123 | kex->server_version_string, | 161 | kex->server_version_string, |
124 | buffer_ptr(kex->peer), buffer_len(kex->peer), | 162 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
125 | buffer_ptr(kex->my), buffer_len(kex->my), | 163 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
126 | server_host_key_blob, sbloblen, | 164 | server_host_key_blob, sbloblen, |
127 | dh_client_pub, | 165 | dh_client_pub, |
128 | dh->pub_key, | 166 | kex->dh->pub_key, |
129 | shared_secret, | 167 | shared_secret, |
130 | &hash, &hashlen | 168 | hash, &hashlen)) != 0) |
131 | ); | 169 | goto out; |
132 | BN_clear_free(dh_client_pub); | ||
133 | 170 | ||
134 | /* save session id := H */ | 171 | /* save session id := H */ |
135 | if (kex->session_id == NULL) { | 172 | if (kex->session_id == NULL) { |
136 | kex->session_id_len = hashlen; | 173 | kex->session_id_len = hashlen; |
137 | kex->session_id = xmalloc(kex->session_id_len); | 174 | kex->session_id = malloc(kex->session_id_len); |
175 | if (kex->session_id == NULL) { | ||
176 | r = SSH_ERR_ALLOC_FAIL; | ||
177 | goto out; | ||
178 | } | ||
138 | memcpy(kex->session_id, hash, kex->session_id_len); | 179 | memcpy(kex->session_id, hash, kex->session_id_len); |
139 | } | 180 | } |
140 | 181 | ||
141 | /* sign H */ | 182 | /* sign H */ |
142 | kex->sign(server_host_private, server_host_public, &signature, &slen, | 183 | if ((r = kex->sign(server_host_private, server_host_public, |
143 | hash, hashlen); | 184 | &signature, &slen, hash, hashlen, ssh->compat)) < 0) |
185 | goto out; | ||
144 | 186 | ||
145 | /* destroy_sensitive_data(); */ | 187 | /* destroy_sensitive_data(); */ |
146 | 188 | ||
147 | /* send server hostkey, DH pubkey 'f' and singed H */ | 189 | /* send server hostkey, DH pubkey 'f' and singed H */ |
148 | packet_start(SSH2_MSG_KEXDH_REPLY); | 190 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 || |
149 | packet_put_string(server_host_key_blob, sbloblen); | 191 | (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 || |
150 | packet_put_bignum2(dh->pub_key); /* f */ | 192 | (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */ |
151 | packet_put_string(signature, slen); | 193 | (r = sshpkt_put_string(ssh, signature, slen)) != 0 || |
152 | packet_send(); | 194 | (r = sshpkt_send(ssh)) != 0) |
153 | 195 | goto out; | |
154 | free(signature); | 196 | |
197 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) | ||
198 | r = kex_send_newkeys(ssh); | ||
199 | out: | ||
200 | explicit_bzero(hash, sizeof(hash)); | ||
201 | DH_free(kex->dh); | ||
202 | kex->dh = NULL; | ||
203 | if (dh_client_pub) | ||
204 | BN_clear_free(dh_client_pub); | ||
205 | if (kbuf) { | ||
206 | explicit_bzero(kbuf, klen); | ||
207 | free(kbuf); | ||
208 | } | ||
209 | if (shared_secret) | ||
210 | BN_clear_free(shared_secret); | ||
155 | free(server_host_key_blob); | 211 | free(server_host_key_blob); |
156 | /* have keys, free DH */ | 212 | free(signature); |
157 | DH_free(dh); | 213 | return r; |
158 | |||
159 | kex_derive_keys_bn(kex, hash, hashlen, shared_secret); | ||
160 | BN_clear_free(shared_secret); | ||
161 | kex_finish(kex); | ||
162 | } | 214 | } |
163 | #endif /* WITH_OPENSSL */ | 215 | #endif /* WITH_OPENSSL */ |