diff options
Diffstat (limited to 'kexdhc.c')
-rw-r--r-- | kexdhc.c | 194 |
1 files changed, 120 insertions, 74 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: kexdhc.c,v 1.16 2015/01/19 19:52:16 markus Exp $ */ | 1 | /* $OpenBSD: kexdhc.c,v 1.17 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 | * |
@@ -36,129 +36,175 @@ | |||
36 | #include <string.h> | 36 | #include <string.h> |
37 | #include <signal.h> | 37 | #include <signal.h> |
38 | 38 | ||
39 | #include "xmalloc.h" | 39 | #include "sshkey.h" |
40 | #include "buffer.h" | ||
41 | #include "key.h" | ||
42 | #include "cipher.h" | 40 | #include "cipher.h" |
41 | #include "digest.h" | ||
43 | #include "kex.h" | 42 | #include "kex.h" |
44 | #include "log.h" | 43 | #include "log.h" |
45 | #include "packet.h" | 44 | #include "packet.h" |
46 | #include "dh.h" | 45 | #include "dh.h" |
47 | #include "ssh2.h" | 46 | #include "ssh2.h" |
47 | #include "dispatch.h" | ||
48 | #include "compat.h" | ||
49 | #include "ssherr.h" | ||
50 | #include "sshbuf.h" | ||
48 | 51 | ||
49 | void | 52 | static int input_kex_dh(int, u_int32_t, void *); |
50 | kexdh_client(Kex *kex) | 53 | |
54 | int | ||
55 | kexdh_client(struct ssh *ssh) | ||
51 | { | 56 | { |
52 | BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; | 57 | struct kex *kex = ssh->kex; |
53 | DH *dh; | 58 | int r; |
54 | Key *server_host_key; | ||
55 | u_char *server_host_key_blob = NULL, *signature = NULL; | ||
56 | u_char *kbuf, *hash; | ||
57 | u_int klen, slen, sbloblen, hashlen; | ||
58 | int kout; | ||
59 | 59 | ||
60 | /* generate and send 'e', client DH public key */ | 60 | /* generate and send 'e', client DH public key */ |
61 | switch (kex->kex_type) { | 61 | switch (kex->kex_type) { |
62 | case KEX_DH_GRP1_SHA1: | 62 | case KEX_DH_GRP1_SHA1: |
63 | dh = dh_new_group1(); | 63 | kex->dh = dh_new_group1(); |
64 | break; | 64 | break; |
65 | case KEX_DH_GRP14_SHA1: | 65 | case KEX_DH_GRP14_SHA1: |
66 | dh = dh_new_group14(); | 66 | kex->dh = dh_new_group14(); |
67 | break; | 67 | break; |
68 | default: | 68 | default: |
69 | fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type); | 69 | r = SSH_ERR_INVALID_ARGUMENT; |
70 | goto out; | ||
71 | } | ||
72 | if (kex->dh == NULL) { | ||
73 | r = SSH_ERR_ALLOC_FAIL; | ||
74 | goto out; | ||
70 | } | 75 | } |
71 | dh_gen_key(dh, kex->we_need * 8); | ||
72 | packet_start(SSH2_MSG_KEXDH_INIT); | ||
73 | packet_put_bignum2(dh->pub_key); | ||
74 | packet_send(); | ||
75 | |||
76 | debug("sending SSH2_MSG_KEXDH_INIT"); | 76 | debug("sending SSH2_MSG_KEXDH_INIT"); |
77 | if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 || | ||
78 | (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 || | ||
79 | (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || | ||
80 | (r = sshpkt_send(ssh)) != 0) | ||
81 | goto out; | ||
77 | #ifdef DEBUG_KEXDH | 82 | #ifdef DEBUG_KEXDH |
78 | DHparams_print_fp(stderr, dh); | 83 | DHparams_print_fp(stderr, kex->dh); |
79 | fprintf(stderr, "pub= "); | 84 | fprintf(stderr, "pub= "); |
80 | BN_print_fp(stderr, dh->pub_key); | 85 | BN_print_fp(stderr, kex->dh->pub_key); |
81 | fprintf(stderr, "\n"); | 86 | fprintf(stderr, "\n"); |
82 | #endif | 87 | #endif |
83 | |||
84 | debug("expecting SSH2_MSG_KEXDH_REPLY"); | 88 | debug("expecting SSH2_MSG_KEXDH_REPLY"); |
85 | packet_read_expect(SSH2_MSG_KEXDH_REPLY); | 89 | ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh); |
90 | r = 0; | ||
91 | out: | ||
92 | return r; | ||
93 | } | ||
86 | 94 | ||
95 | static int | ||
96 | input_kex_dh(int type, u_int32_t seq, void *ctxt) | ||
97 | { | ||
98 | struct ssh *ssh = ctxt; | ||
99 | struct kex *kex = ssh->kex; | ||
100 | BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; | ||
101 | struct sshkey *server_host_key = NULL; | ||
102 | u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL; | ||
103 | u_char hash[SSH_DIGEST_MAX_LENGTH]; | ||
104 | size_t klen = 0, slen, sbloblen, hashlen; | ||
105 | int kout, r; | ||
106 | |||
107 | if (kex->verify_host_key == NULL) { | ||
108 | r = SSH_ERR_INVALID_ARGUMENT; | ||
109 | goto out; | ||
110 | } | ||
87 | /* key, cert */ | 111 | /* key, cert */ |
88 | server_host_key_blob = packet_get_string(&sbloblen); | 112 | if ((r = sshpkt_get_string(ssh, &server_host_key_blob, |
89 | server_host_key = key_from_blob(server_host_key_blob, sbloblen); | 113 | &sbloblen)) != 0 || |
90 | if (server_host_key == NULL) | 114 | (r = sshkey_from_blob(server_host_key_blob, sbloblen, |
91 | fatal("cannot decode server_host_key_blob"); | 115 | &server_host_key)) != 0) |
92 | if (server_host_key->type != kex->hostkey_type) | 116 | goto out; |
93 | fatal("type mismatch for decoded server_host_key_blob"); | 117 | if (server_host_key->type != kex->hostkey_type) { |
94 | if (kex->verify_host_key == NULL) | 118 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
95 | fatal("cannot verify server_host_key"); | 119 | goto out; |
96 | if (kex->verify_host_key(server_host_key) == -1) | 120 | } |
97 | fatal("server_host_key verification failed"); | 121 | if (kex->verify_host_key(server_host_key, ssh) == -1) { |
98 | 122 | r = SSH_ERR_SIGNATURE_INVALID; | |
123 | goto out; | ||
124 | } | ||
99 | /* DH parameter f, server public DH key */ | 125 | /* DH parameter f, server public DH key */ |
100 | if ((dh_server_pub = BN_new()) == NULL) | 126 | if ((dh_server_pub = BN_new()) == NULL) { |
101 | fatal("dh_server_pub == NULL"); | 127 | r = SSH_ERR_ALLOC_FAIL; |
102 | packet_get_bignum2(dh_server_pub); | 128 | goto out; |
103 | 129 | } | |
130 | /* signed H */ | ||
131 | if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 || | ||
132 | (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || | ||
133 | (r = sshpkt_get_end(ssh)) != 0) | ||
134 | goto out; | ||
104 | #ifdef DEBUG_KEXDH | 135 | #ifdef DEBUG_KEXDH |
105 | fprintf(stderr, "dh_server_pub= "); | 136 | fprintf(stderr, "dh_server_pub= "); |
106 | BN_print_fp(stderr, dh_server_pub); | 137 | BN_print_fp(stderr, dh_server_pub); |
107 | fprintf(stderr, "\n"); | 138 | fprintf(stderr, "\n"); |
108 | debug("bits %d", BN_num_bits(dh_server_pub)); | 139 | debug("bits %d", BN_num_bits(dh_server_pub)); |
109 | #endif | 140 | #endif |
141 | if (!dh_pub_is_valid(kex->dh, dh_server_pub)) { | ||
142 | sshpkt_disconnect(ssh, "bad server public DH value"); | ||
143 | r = SSH_ERR_MESSAGE_INCOMPLETE; | ||
144 | goto out; | ||
145 | } | ||
110 | 146 | ||
111 | /* signed H */ | 147 | klen = DH_size(kex->dh); |
112 | signature = packet_get_string(&slen); | 148 | if ((kbuf = malloc(klen)) == NULL || |
113 | packet_check_eom(); | 149 | (shared_secret = BN_new()) == NULL) { |
114 | 150 | r = SSH_ERR_ALLOC_FAIL; | |
115 | if (!dh_pub_is_valid(dh, dh_server_pub)) | 151 | goto out; |
116 | packet_disconnect("bad server public DH value"); | 152 | } |
117 | 153 | if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 || | |
118 | klen = DH_size(dh); | 154 | BN_bin2bn(kbuf, kout, shared_secret) == NULL) { |
119 | kbuf = xmalloc(klen); | 155 | r = SSH_ERR_LIBCRYPTO_ERROR; |
120 | if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0) | 156 | goto out; |
121 | fatal("DH_compute_key: failed"); | 157 | } |
122 | #ifdef DEBUG_KEXDH | 158 | #ifdef DEBUG_KEXDH |
123 | dump_digest("shared secret", kbuf, kout); | 159 | dump_digest("shared secret", kbuf, kout); |
124 | #endif | 160 | #endif |
125 | if ((shared_secret = BN_new()) == NULL) | ||
126 | fatal("kexdh_client: BN_new failed"); | ||
127 | if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) | ||
128 | fatal("kexdh_client: BN_bin2bn failed"); | ||
129 | explicit_bzero(kbuf, klen); | ||
130 | free(kbuf); | ||
131 | 161 | ||
132 | /* calc and verify H */ | 162 | /* calc and verify H */ |
133 | kex_dh_hash( | 163 | hashlen = sizeof(hash); |
164 | if ((r = kex_dh_hash( | ||
134 | kex->client_version_string, | 165 | kex->client_version_string, |
135 | kex->server_version_string, | 166 | kex->server_version_string, |
136 | buffer_ptr(kex->my), buffer_len(kex->my), | 167 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
137 | buffer_ptr(kex->peer), buffer_len(kex->peer), | 168 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
138 | server_host_key_blob, sbloblen, | 169 | server_host_key_blob, sbloblen, |
139 | dh->pub_key, | 170 | kex->dh->pub_key, |
140 | dh_server_pub, | 171 | dh_server_pub, |
141 | shared_secret, | 172 | shared_secret, |
142 | &hash, &hashlen | 173 | hash, &hashlen)) != 0) |
143 | ); | 174 | goto out; |
144 | free(server_host_key_blob); | ||
145 | BN_clear_free(dh_server_pub); | ||
146 | DH_free(dh); | ||
147 | 175 | ||
148 | if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1) | 176 | if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen, |
149 | fatal("key_verify failed for server_host_key"); | 177 | ssh->compat)) != 0) |
150 | key_free(server_host_key); | 178 | goto out; |
151 | free(signature); | ||
152 | 179 | ||
153 | /* save session id */ | 180 | /* save session id */ |
154 | if (kex->session_id == NULL) { | 181 | if (kex->session_id == NULL) { |
155 | kex->session_id_len = hashlen; | 182 | kex->session_id_len = hashlen; |
156 | kex->session_id = xmalloc(kex->session_id_len); | 183 | kex->session_id = malloc(kex->session_id_len); |
184 | if (kex->session_id == NULL) { | ||
185 | r = SSH_ERR_ALLOC_FAIL; | ||
186 | goto out; | ||
187 | } | ||
157 | memcpy(kex->session_id, hash, kex->session_id_len); | 188 | memcpy(kex->session_id, hash, kex->session_id_len); |
158 | } | 189 | } |
159 | 190 | ||
160 | kex_derive_keys_bn(kex, hash, hashlen, shared_secret); | 191 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) |
161 | BN_clear_free(shared_secret); | 192 | r = kex_send_newkeys(ssh); |
162 | kex_finish(kex); | 193 | out: |
194 | explicit_bzero(hash, sizeof(hash)); | ||
195 | DH_free(kex->dh); | ||
196 | kex->dh = NULL; | ||
197 | if (dh_server_pub) | ||
198 | BN_clear_free(dh_server_pub); | ||
199 | if (kbuf) { | ||
200 | explicit_bzero(kbuf, klen); | ||
201 | free(kbuf); | ||
202 | } | ||
203 | if (shared_secret) | ||
204 | BN_clear_free(shared_secret); | ||
205 | sshkey_free(server_host_key); | ||
206 | free(server_host_key_blob); | ||
207 | free(signature); | ||
208 | return r; | ||
163 | } | 209 | } |
164 | #endif /* WITH_OPENSSL */ | 210 | #endif /* WITH_OPENSSL */ |