]> andersk Git - openssh.git/blob - roaming_client.c
- (dtucker) Wrap use of IPPROTO_IPV6 in an ifdef for platforms that don't
[openssh.git] / roaming_client.c
1 /* $OpenBSD: roaming_client.c,v 1.1 2009/10/24 11:22:37 andreas Exp $ */
2 /*
3  * Copyright (c) 2004-2009 AppGate Network Security AB
4  *
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.
8  *
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.
16  */
17
18 #include "includes.h"
19
20 #include <sys/queue.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23
24 #include <inttypes.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include <openssl/crypto.h>
30 #include <openssl/sha.h>
31
32 #include "xmalloc.h"
33 #include "buffer.h"
34 #include "channels.h"
35 #include "cipher.h"
36 #include "dispatch.h"
37 #include "clientloop.h"
38 #include "log.h"
39 #include "match.h"
40 #include "misc.h"
41 #include "packet.h"
42 #include "ssh.h"
43 #include "key.h"
44 #include "kex.h"
45 #include "readconf.h"
46 #include "roaming.h"
47 #include "ssh2.h"
48 #include "sshconnect.h"
49
50 /* import */
51 extern Options options;
52 extern char *host;
53 extern struct sockaddr_storage hostaddr;
54 extern int session_resumed;
55
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;
60
61 void
62 roaming_reply(int type, u_int32_t seq, void *ctxt)
63 {
64         if (type == SSH2_MSG_REQUEST_FAILURE) {
65                 logit("Server denied roaming");
66                 return;
67         }
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());
74         roaming_enabled = 1;
75 }
76
77 void
78 request_roaming(void)
79 {
80         packet_start(SSH2_MSG_GLOBAL_REQUEST);
81         packet_put_cstring(ROAMING_REQUEST);
82         packet_put_char(1);
83         packet_put_int(get_recv_buf_size());
84         packet_send();
85         client_register_global_confirm(roaming_reply, NULL);
86 }
87
88 static void
89 roaming_auth_required(void)
90 {
91         u_char digest[SHA_DIGEST_LENGTH];
92         EVP_MD_CTX md;
93         Buffer b;
94         const EVP_MD *evp_md = EVP_sha1();
95         u_int64_t chall, oldchall;
96
97         chall = packet_get_int64();
98         oldchall = packet_get_int64();
99         if (oldchall != lastseenchall) {
100                 key1 = oldkey1;
101                 key2 = oldkey2;
102         }
103         lastseenchall = chall;
104
105         buffer_init(&b);
106         buffer_put_int64(&b, cookie);
107         buffer_put_int64(&b, chall);
108         EVP_DigestInit(&md, evp_md);
109         EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
110         EVP_DigestFinal(&md, digest, NULL);
111         buffer_free(&b);
112
113         packet_start(SSH2_MSG_KEX_ROAMING_AUTH);
114         packet_put_int64(key1 ^ get_recv_bytes());
115         packet_put_raw(digest, sizeof(digest));
116         packet_send();
117
118         oldkey1 = key1;
119         oldkey2 = key2;
120         calculate_new_key(&key1, cookie, chall);
121         calculate_new_key(&key2, cookie, chall);
122
123         debug("Received %" PRIu64 " bytes", get_recv_bytes());
124         debug("Sent roaming_auth packet");
125 }
126
127 int
128 resume_kex(void)
129 {
130         /*
131          * This should not happen - if the client sends the kex method
132          * resume@appgate.com then the kex is done in roaming_resume().
133          */
134         return 1;
135 }
136
137 static int
138 roaming_resume(void)
139 {
140         u_int64_t recv_bytes;
141         char *str = NULL, *kexlist = NULL, *c;
142         int i, type;
143         int timeout_ms = options.connection_timeout * 1000;
144         u_int len;
145         u_int32_t rnd = 0;
146
147         resume_in_progress = 1;
148
149         /* Exchange banners */
150         ssh_exchange_identification(timeout_ms);
151         packet_set_nonblocking();
152
153         /* Send a kexinit message with resume@appgate.com as only kex algo */
154         packet_start(SSH2_MSG_KEXINIT);
155         for (i = 0; i < KEX_COOKIE_LEN; i++) {
156                 if (i % 4 == 0)
157                         rnd = arc4random();
158                 packet_put_char(rnd & 0xff);
159                 rnd >>= 8;
160         }
161         packet_put_cstring(KEX_RESUME);
162         for (i = 1; i < PROPOSAL_MAX; i++) {
163                 /* kex algorithm added so start with i=1 and not 0 */
164                 packet_put_cstring(""); /* Not used when we resume */
165         }
166         packet_put_char(1); /* first kex_packet follows */
167         packet_put_int(0); /* reserved */
168         packet_send();
169
170         /* Assume that resume@appgate.com will be accepted */
171         packet_start(SSH2_MSG_KEX_ROAMING_RESUME);
172         packet_put_int(roaming_id);
173         packet_send();
174
175         /* Read the server's kexinit and check for resume@appgate.com */
176         if ((type = packet_read()) != SSH2_MSG_KEXINIT) {
177                 debug("expected kexinit on resume, got %d", type);
178                 goto fail;
179         }
180         for (i = 0; i < KEX_COOKIE_LEN; i++)
181                 (void)packet_get_char();
182         kexlist = packet_get_string(&len);
183         if (!kexlist
184             || (str = match_list(KEX_RESUME, kexlist, NULL)) == NULL) {
185                 debug("server doesn't allow resume");
186                 goto fail;
187         }
188         xfree(str);
189         for (i = 1; i < PROPOSAL_MAX; i++) {
190                 /* kex algorithm taken care of so start with i=1 and not 0 */
191                 xfree(packet_get_string(&len));
192         }
193         i = packet_get_char(); /* first_kex_packet_follows */
194         if (i && (c = strchr(kexlist, ',')))
195                 *c = 0;
196         if (i && strcmp(kexlist, KEX_RESUME)) {
197                 debug("server's kex guess (%s) was wrong, skipping", kexlist);
198                 (void)packet_read(); /* Wrong guess - discard packet */
199         }
200
201         /*
202          * Read the ROAMING_AUTH_REQUIRED challenge from the server and
203          * send ROAMING_AUTH
204          */
205         if ((type = packet_read()) != SSH2_MSG_KEX_ROAMING_AUTH_REQUIRED) {
206                 debug("expected roaming_auth_required, got %d", type);
207                 goto fail;
208         }
209         roaming_auth_required();
210
211         /* Read ROAMING_AUTH_OK from the server */
212         if ((type = packet_read()) != SSH2_MSG_KEX_ROAMING_AUTH_OK) {
213                 debug("expected roaming_auth_ok, got %d", type);
214                 goto fail;
215         }
216         recv_bytes = packet_get_int64() ^ oldkey2;
217         debug("Peer received %" PRIu64 " bytes", recv_bytes);
218         resend_bytes(packet_get_connection_out(), &recv_bytes);
219
220         resume_in_progress = 0;
221
222         session_resumed = 1; /* Tell clientloop */
223
224         return 0;
225
226 fail:
227         if (kexlist)
228                 xfree(kexlist);
229         if (packet_get_connection_in() == packet_get_connection_out())
230                 close(packet_get_connection_in());
231         else {
232                 close(packet_get_connection_in());
233                 close(packet_get_connection_out());
234         }
235         return 1;
236 }
237
238 int
239 wait_for_roaming_reconnect(void)
240 {
241         static int reenter_guard = 0;
242         int timeout_ms = options.connection_timeout * 1000;
243         int c;
244
245         if (reenter_guard != 0)
246                 fatal("Server refused resume, roaming timeout may be exceeded");
247         reenter_guard = 1;
248
249         fprintf(stderr, "[connection suspended, press return to resume]");
250         fflush(stderr);
251         packet_backup_state();
252         /* TODO Perhaps we should read from tty here */
253         while ((c = fgetc(stdin)) != EOF) {
254                 if (c == 'Z' - 64) {
255                         kill(getpid(), SIGTSTP);
256                         continue;
257                 }
258                 if (c != '\n' && c != '\r')
259                         continue;
260
261                 if (ssh_connect(host, &hostaddr, options.port,
262                     options.address_family, 1, &timeout_ms,
263                     options.tcp_keep_alive, options.use_privileged_port,
264                     options.proxy_command) == 0 && roaming_resume() == 0) {
265                         packet_restore_state();
266                         reenter_guard = 0;
267                         fprintf(stderr, "[connection resumed]\n");
268                         fflush(stderr);
269                         return 0;
270                 }
271
272                 fprintf(stderr, "[reconnect failed, press return to retry]");
273                 fflush(stderr);
274         }
275         fprintf(stderr, "[exiting]\n");
276         fflush(stderr);
277         exit(0);
278 }
This page took 0.06152 seconds and 5 git commands to generate.