1 /* $OpenBSD: roaming_client.c,v 1.1 2009/10/24 11:22:37 andreas 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.
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
27 #include <openssl/crypto.h>
28 #include <openssl/sha.h>
35 #include "clientloop.h"
46 #include "sshconnect.h"
49 extern Options options;
51 extern struct sockaddr_storage hostaddr;
52 extern int session_resumed;
54 static u_int32_t roaming_id;
55 static u_int64_t cookie;
56 static u_int64_t lastseenchall;
57 static u_int64_t key1, key2, oldkey1, oldkey2;
60 roaming_reply(int type, u_int32_t seq, void *ctxt)
62 if (type == SSH2_MSG_REQUEST_FAILURE) {
63 logit("Server denied roaming");
66 verbose("Roaming enabled");
67 roaming_id = packet_get_int();
68 cookie = packet_get_int64();
69 key1 = oldkey1 = packet_get_int64();
70 key2 = oldkey2 = packet_get_int64();
71 set_out_buffer_size(packet_get_int() + get_snd_buf_size());
78 packet_start(SSH2_MSG_GLOBAL_REQUEST);
79 packet_put_cstring(ROAMING_REQUEST);
81 packet_put_int(get_recv_buf_size());
83 client_register_global_confirm(roaming_reply, NULL);
87 roaming_auth_required(void)
89 u_char digest[SHA_DIGEST_LENGTH];
92 const EVP_MD *evp_md = EVP_sha1();
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 EVP_DigestInit(&md, evp_md);
107 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
108 EVP_DigestFinal(&md, digest, NULL);
111 packet_start(SSH2_MSG_KEX_ROAMING_AUTH);
112 packet_put_int64(key1 ^ get_recv_bytes());
113 packet_put_raw(digest, sizeof(digest));
118 calculate_new_key(&key1, cookie, chall);
119 calculate_new_key(&key2, cookie, chall);
121 debug("Received %" PRIu64 " bytes", get_recv_bytes());
122 debug("Sent roaming_auth packet");
129 * This should not happen - if the client sends the kex method
130 * resume@appgate.com then the kex is done in roaming_resume().
138 u_int64_t recv_bytes;
139 char *str = NULL, *kexlist = NULL, *c;
141 int timeout_ms = options.connection_timeout * 1000;
145 resume_in_progress = 1;
147 /* Exchange banners */
148 ssh_exchange_identification(timeout_ms);
149 packet_set_nonblocking();
151 /* Send a kexinit message with resume@appgate.com as only kex algo */
152 packet_start(SSH2_MSG_KEXINIT);
153 for (i = 0; i < KEX_COOKIE_LEN; i++) {
156 packet_put_char(rnd & 0xff);
159 packet_put_cstring(KEX_RESUME);
160 for (i = 1; i < PROPOSAL_MAX; i++) {
161 /* kex algorithm added so start with i=1 and not 0 */
162 packet_put_cstring(""); /* Not used when we resume */
164 packet_put_char(1); /* first kex_packet follows */
165 packet_put_int(0); /* reserved */
168 /* Assume that resume@appgate.com will be accepted */
169 packet_start(SSH2_MSG_KEX_ROAMING_RESUME);
170 packet_put_int(roaming_id);
173 /* Read the server's kexinit and check for resume@appgate.com */
174 if ((type = packet_read()) != SSH2_MSG_KEXINIT) {
175 debug("expected kexinit on resume, got %d", type);
178 for (i = 0; i < KEX_COOKIE_LEN; i++)
179 (void)packet_get_char();
180 kexlist = packet_get_string(&len);
182 || (str = match_list(KEX_RESUME, kexlist, NULL)) == NULL) {
183 debug("server doesn't allow resume");
187 for (i = 1; i < PROPOSAL_MAX; i++) {
188 /* kex algorithm taken care of so start with i=1 and not 0 */
189 xfree(packet_get_string(&len));
191 i = packet_get_char(); /* first_kex_packet_follows */
192 if (i && (c = strchr(kexlist, ',')))
194 if (i && strcmp(kexlist, KEX_RESUME)) {
195 debug("server's kex guess (%s) was wrong, skipping", kexlist);
196 (void)packet_read(); /* Wrong guess - discard packet */
200 * Read the ROAMING_AUTH_REQUIRED challenge from the server and
203 if ((type = packet_read()) != SSH2_MSG_KEX_ROAMING_AUTH_REQUIRED) {
204 debug("expected roaming_auth_required, got %d", type);
207 roaming_auth_required();
209 /* Read ROAMING_AUTH_OK from the server */
210 if ((type = packet_read()) != SSH2_MSG_KEX_ROAMING_AUTH_OK) {
211 debug("expected roaming_auth_ok, got %d", type);
214 recv_bytes = packet_get_int64() ^ oldkey2;
215 debug("Peer received %" PRIu64 " bytes", recv_bytes);
216 resend_bytes(packet_get_connection_out(), &recv_bytes);
218 resume_in_progress = 0;
220 session_resumed = 1; /* Tell clientloop */
227 if (packet_get_connection_in() == packet_get_connection_out())
228 close(packet_get_connection_in());
230 close(packet_get_connection_in());
231 close(packet_get_connection_out());
237 wait_for_roaming_reconnect(void)
239 static int reenter_guard = 0;
240 int timeout_ms = options.connection_timeout * 1000;
243 if (reenter_guard != 0)
244 fatal("Server refused resume, roaming timeout may be exceeded");
247 fprintf(stderr, "[connection suspended, press return to resume]");
249 packet_backup_state();
250 /* TODO Perhaps we should read from tty here */
251 while ((c = fgetc(stdin)) != EOF) {
253 kill(getpid(), SIGTSTP);
256 if (c != '\n' && c != '\r')
259 if (ssh_connect(host, &hostaddr, options.port,
260 options.address_family, 1, &timeout_ms,
261 options.tcp_keep_alive, options.use_privileged_port,
262 options.proxy_command) == 0 && roaming_resume() == 0) {
263 packet_restore_state();
265 fprintf(stderr, "[connection resumed]\n");
270 fprintf(stderr, "[reconnect failed, press return to retry]");
273 fprintf(stderr, "[exiting]\n");