1 /* $OpenBSD: roaming_client.c,v 1.9 2015/01/27 12:54:06 okan 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>
33 #include "clientloop.h"
44 #include "sshconnect.h"
48 extern Options options;
50 extern struct sockaddr_storage hostaddr;
51 extern int session_resumed;
53 static u_int32_t roaming_id;
54 static u_int64_t cookie;
55 static u_int64_t lastseenchall;
56 static u_int64_t key1, key2, oldkey1, oldkey2;
59 roaming_reply(int type, u_int32_t seq, void *ctxt)
61 if (type == SSH2_MSG_REQUEST_FAILURE) {
62 logit("Server denied roaming");
65 verbose("Roaming enabled");
66 roaming_id = packet_get_int();
67 cookie = packet_get_int64();
68 key1 = oldkey1 = packet_get_int64();
69 key2 = oldkey2 = packet_get_int64();
70 set_out_buffer_size(packet_get_int() + get_snd_buf_size());
77 packet_start(SSH2_MSG_GLOBAL_REQUEST);
78 packet_put_cstring(ROAMING_REQUEST);
80 packet_put_int(get_recv_buf_size());
82 client_register_global_confirm(roaming_reply, NULL);
86 roaming_auth_required(void)
88 u_char digest[SSH_DIGEST_MAX_LENGTH];
90 u_int64_t chall, oldchall;
92 chall = packet_get_int64();
93 oldchall = packet_get_int64();
94 if (oldchall != lastseenchall) {
98 lastseenchall = chall;
101 buffer_put_int64(&b, cookie);
102 buffer_put_int64(&b, chall);
103 if (ssh_digest_buffer(SSH_DIGEST_SHA1, &b, digest, sizeof(digest)) != 0)
104 fatal("%s: ssh_digest_buffer failed", __func__);
107 packet_start(SSH2_MSG_KEX_ROAMING_AUTH);
108 packet_put_int64(key1 ^ get_recv_bytes());
109 packet_put_raw(digest, ssh_digest_bytes(SSH_DIGEST_SHA1));
114 calculate_new_key(&key1, cookie, chall);
115 calculate_new_key(&key2, cookie, chall);
117 debug("Received %llu bytes", (unsigned long long)get_recv_bytes());
118 debug("Sent roaming_auth packet");
125 * This should not happen - if the client sends the kex method
126 * resume@appgate.com then the kex is done in roaming_resume().
134 u_int64_t recv_bytes;
135 char *str = NULL, *kexlist = NULL, *c;
137 int timeout_ms = options.connection_timeout * 1000;
141 resume_in_progress = 1;
143 /* Exchange banners */
144 ssh_exchange_identification(timeout_ms);
145 packet_set_nonblocking();
147 /* Send a kexinit message with resume@appgate.com as only kex algo */
148 packet_start(SSH2_MSG_KEXINIT);
149 for (i = 0; i < KEX_COOKIE_LEN; i++) {
152 packet_put_char(rnd & 0xff);
155 packet_put_cstring(KEX_RESUME);
156 for (i = 1; i < PROPOSAL_MAX; i++) {
157 /* kex algorithm added so start with i=1 and not 0 */
158 packet_put_cstring(""); /* Not used when we resume */
160 packet_put_char(1); /* first kex_packet follows */
161 packet_put_int(0); /* reserved */
164 /* Assume that resume@appgate.com will be accepted */
165 packet_start(SSH2_MSG_KEX_ROAMING_RESUME);
166 packet_put_int(roaming_id);
169 /* Read the server's kexinit and check for resume@appgate.com */
170 if ((type = packet_read()) != SSH2_MSG_KEXINIT) {
171 debug("expected kexinit on resume, got %d", type);
174 for (i = 0; i < KEX_COOKIE_LEN; i++)
175 (void)packet_get_char();
176 kexlist = packet_get_string(&len);
178 || (str = match_list(KEX_RESUME, kexlist, NULL)) == NULL) {
179 debug("server doesn't allow resume");
183 for (i = 1; i < PROPOSAL_MAX; i++) {
184 /* kex algorithm taken care of so start with i=1 and not 0 */
185 free(packet_get_string(&len));
187 i = packet_get_char(); /* first_kex_packet_follows */
188 if (i && (c = strchr(kexlist, ',')))
190 if (i && strcmp(kexlist, KEX_RESUME)) {
191 debug("server's kex guess (%s) was wrong, skipping", kexlist);
192 (void)packet_read(); /* Wrong guess - discard packet */
196 * Read the ROAMING_AUTH_REQUIRED challenge from the server and
199 if ((type = packet_read()) != SSH2_MSG_KEX_ROAMING_AUTH_REQUIRED) {
200 debug("expected roaming_auth_required, got %d", type);
203 roaming_auth_required();
205 /* Read ROAMING_AUTH_OK from the server */
206 if ((type = packet_read()) != SSH2_MSG_KEX_ROAMING_AUTH_OK) {
207 debug("expected roaming_auth_ok, got %d", type);
210 recv_bytes = packet_get_int64() ^ oldkey2;
211 debug("Peer received %llu bytes", (unsigned long long)recv_bytes);
212 resend_bytes(packet_get_connection_out(), &recv_bytes);
214 resume_in_progress = 0;
216 session_resumed = 1; /* Tell clientloop */
222 if (packet_get_connection_in() == packet_get_connection_out())
223 close(packet_get_connection_in());
225 close(packet_get_connection_in());
226 close(packet_get_connection_out());
232 wait_for_roaming_reconnect(void)
234 static int reenter_guard = 0;
235 int timeout_ms = options.connection_timeout * 1000;
238 if (reenter_guard != 0)
239 fatal("Server refused resume, roaming timeout may be exceeded");
242 fprintf(stderr, "[connection suspended, press return to resume]");
244 packet_backup_state();
245 /* TODO Perhaps we should read from tty here */
246 while ((c = fgetc(stdin)) != EOF) {
248 kill(getpid(), SIGTSTP);
251 if (c != '\n' && c != '\r')
254 if (ssh_connect(host, NULL, &hostaddr, options.port,
255 options.address_family, 1, &timeout_ms,
256 options.tcp_keep_alive, options.use_privileged_port) == 0 &&
257 roaming_resume() == 0) {
258 packet_restore_state();
260 fprintf(stderr, "[connection resumed]\n");
265 fprintf(stderr, "[reconnect failed, press return to retry]");
268 fprintf(stderr, "[exiting]\n");