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