1 /* $OpenBSD: roaming_client.c,v 1.8 2014/04/29 18:01:49 markus Exp $ */
3 * Copyright (c) 2004-2009 AppGate Network Security AB
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "openbsd-compat/sys-queue.h"
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #ifdef HAVE_INTTYPES_H
36 #include "clientloop.h"
47 #include "sshconnect.h"
51 extern Options options;
53 extern struct sockaddr_storage hostaddr;
54 extern int session_resumed;
56 static u_int32_t roaming_id;
57 static u_int64_t cookie;
58 static u_int64_t lastseenchall;
59 static u_int64_t key1, key2, oldkey1, oldkey2;
62 roaming_reply(int type, u_int32_t seq, void *ctxt)
64 if (type == SSH2_MSG_REQUEST_FAILURE) {
65 logit("Server denied roaming");
68 verbose("Roaming enabled");
69 roaming_id = packet_get_int();
70 cookie = packet_get_int64();
71 key1 = oldkey1 = packet_get_int64();
72 key2 = oldkey2 = packet_get_int64();
73 set_out_buffer_size(packet_get_int() + get_snd_buf_size());
80 packet_start(SSH2_MSG_GLOBAL_REQUEST);
81 packet_put_cstring(ROAMING_REQUEST);
83 packet_put_int(get_recv_buf_size());
85 client_register_global_confirm(roaming_reply, NULL);
89 roaming_auth_required(void)
91 u_char digest[SSH_DIGEST_MAX_LENGTH];
93 u_int64_t chall, oldchall;
95 chall = packet_get_int64();
96 oldchall = packet_get_int64();
97 if (oldchall != lastseenchall) {
101 lastseenchall = chall;
104 buffer_put_int64(&b, cookie);
105 buffer_put_int64(&b, chall);
106 if (ssh_digest_buffer(SSH_DIGEST_SHA1, &b, digest, sizeof(digest)) != 0)
107 fatal("%s: ssh_digest_buffer failed", __func__);
110 packet_start(SSH2_MSG_KEX_ROAMING_AUTH);
111 packet_put_int64(key1 ^ get_recv_bytes());
112 packet_put_raw(digest, ssh_digest_bytes(SSH_DIGEST_SHA1));
117 calculate_new_key(&key1, cookie, chall);
118 calculate_new_key(&key2, cookie, chall);
120 debug("Received %llu bytes", (unsigned long long)get_recv_bytes());
121 debug("Sent roaming_auth packet");
128 * This should not happen - if the client sends the kex method
129 * resume@appgate.com then the kex is done in roaming_resume().
137 u_int64_t recv_bytes;
138 char *str = NULL, *kexlist = NULL, *c;
140 int timeout_ms = options.connection_timeout * 1000;
144 resume_in_progress = 1;
146 /* Exchange banners */
147 ssh_exchange_identification(timeout_ms);
148 packet_set_nonblocking();
150 /* Send a kexinit message with resume@appgate.com as only kex algo */
151 packet_start(SSH2_MSG_KEXINIT);
152 for (i = 0; i < KEX_COOKIE_LEN; i++) {
155 packet_put_char(rnd & 0xff);
158 packet_put_cstring(KEX_RESUME);
159 for (i = 1; i < PROPOSAL_MAX; i++) {
160 /* kex algorithm added so start with i=1 and not 0 */
161 packet_put_cstring(""); /* Not used when we resume */
163 packet_put_char(1); /* first kex_packet follows */
164 packet_put_int(0); /* reserved */
167 /* Assume that resume@appgate.com will be accepted */
168 packet_start(SSH2_MSG_KEX_ROAMING_RESUME);
169 packet_put_int(roaming_id);
172 /* Read the server's kexinit and check for resume@appgate.com */
173 if ((type = packet_read()) != SSH2_MSG_KEXINIT) {
174 debug("expected kexinit on resume, got %d", type);
177 for (i = 0; i < KEX_COOKIE_LEN; i++)
178 (void)packet_get_char();
179 kexlist = packet_get_string(&len);
181 || (str = match_list(KEX_RESUME, kexlist, NULL)) == NULL) {
182 debug("server doesn't allow resume");
186 for (i = 1; i < PROPOSAL_MAX; i++) {
187 /* kex algorithm taken care of so start with i=1 and not 0 */
188 free(packet_get_string(&len));
190 i = packet_get_char(); /* first_kex_packet_follows */
191 if (i && (c = strchr(kexlist, ',')))
193 if (i && strcmp(kexlist, KEX_RESUME)) {
194 debug("server's kex guess (%s) was wrong, skipping", kexlist);
195 (void)packet_read(); /* Wrong guess - discard packet */
199 * Read the ROAMING_AUTH_REQUIRED challenge from the server and
202 if ((type = packet_read()) != SSH2_MSG_KEX_ROAMING_AUTH_REQUIRED) {
203 debug("expected roaming_auth_required, got %d", type);
206 roaming_auth_required();
208 /* Read ROAMING_AUTH_OK from the server */
209 if ((type = packet_read()) != SSH2_MSG_KEX_ROAMING_AUTH_OK) {
210 debug("expected roaming_auth_ok, got %d", type);
213 recv_bytes = packet_get_int64() ^ oldkey2;
214 debug("Peer received %llu bytes", (unsigned long long)recv_bytes);
215 resend_bytes(packet_get_connection_out(), &recv_bytes);
217 resume_in_progress = 0;
219 session_resumed = 1; /* Tell clientloop */
225 if (packet_get_connection_in() == packet_get_connection_out())
226 close(packet_get_connection_in());
228 close(packet_get_connection_in());
229 close(packet_get_connection_out());
235 wait_for_roaming_reconnect(void)
237 static int reenter_guard = 0;
238 int timeout_ms = options.connection_timeout * 1000;
241 if (reenter_guard != 0)
242 fatal("Server refused resume, roaming timeout may be exceeded");
245 fprintf(stderr, "[connection suspended, press return to resume]");
247 packet_backup_state();
248 /* TODO Perhaps we should read from tty here */
249 while ((c = fgetc(stdin)) != EOF) {
251 kill(getpid(), SIGTSTP);
254 if (c != '\n' && c != '\r')
257 if (ssh_connect(host, NULL, &hostaddr, options.port,
258 options.address_family, 1, &timeout_ms,
259 options.tcp_keep_alive, options.use_privileged_port) == 0 &&
260 roaming_resume() == 0) {
261 packet_restore_state();
263 fprintf(stderr, "[connection resumed]\n");
268 fprintf(stderr, "[reconnect failed, press return to retry]");
271 fprintf(stderr, "[exiting]\n");