8df7a775 |
1 | #include <windows.h> |
374330e2 |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
fb09bf1c |
4 | #include <stdarg.h> |
5 | #include <assert.h> |
374330e2 |
6 | |
7 | #include "putty.h" |
dacbd0e8 |
8 | #include "tree234.h" |
fb09bf1c |
9 | #include "ssh.h" |
374330e2 |
10 | |
11 | #ifndef FALSE |
12 | #define FALSE 0 |
13 | #endif |
14 | #ifndef TRUE |
15 | #define TRUE 1 |
16 | #endif |
17 | |
fb09bf1c |
18 | #define logevent(s) { logevent(s); \ |
67779be7 |
19 | if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) \ |
b4453f49 |
20 | { fprintf(stderr, "%s\n", s); fflush(stderr); } } |
fb09bf1c |
21 | |
3cd52910 |
22 | #define bombout(msg) ( ssh_state = SSH_STATE_CLOSED, \ |
4a8fc3c4 |
23 | (s ? sk_close(s), s = NULL : 0), \ |
3cd52910 |
24 | connection_fatal msg ) |
8d5de777 |
25 | |
d211621f |
26 | #define SSH1_MSG_DISCONNECT 1 /* 0x1 */ |
27 | #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */ |
28 | #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */ |
29 | #define SSH1_CMSG_USER 4 /* 0x4 */ |
30 | #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */ |
31 | #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */ |
32 | #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */ |
33 | #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */ |
34 | #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */ |
35 | #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */ |
36 | #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */ |
37 | #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */ |
38 | #define SSH1_SMSG_SUCCESS 14 /* 0xe */ |
39 | #define SSH1_SMSG_FAILURE 15 /* 0xf */ |
40 | #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */ |
41 | #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */ |
42 | #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */ |
43 | #define SSH1_CMSG_EOF 19 /* 0x13 */ |
44 | #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */ |
45 | #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */ |
46 | #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */ |
47 | #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */ |
48 | #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */ |
49 | #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */ |
9c964e85 |
50 | #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */ |
51 | #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */ |
52 | #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */ |
d211621f |
53 | #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */ |
54 | #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */ |
d211621f |
55 | #define SSH1_MSG_IGNORE 32 /* 0x20 */ |
9c964e85 |
56 | #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */ |
57 | #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */ |
58 | #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */ |
d211621f |
59 | #define SSH1_MSG_DEBUG 36 /* 0x24 */ |
4ba9b64b |
60 | #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */ |
d211621f |
61 | #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */ |
62 | #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */ |
63 | #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */ |
64 | #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */ |
65 | #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */ |
66 | #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */ |
67 | |
68 | #define SSH1_AUTH_TIS 5 /* 0x5 */ |
69 | #define SSH1_AUTH_CCARD 16 /* 0x10 */ |
70 | |
b96dc54c |
71 | #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */ |
72 | /* Mask for protoflags we will echo back to server if seen */ |
73 | #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */ |
74 | |
d211621f |
75 | #define SSH2_MSG_DISCONNECT 1 /* 0x1 */ |
76 | #define SSH2_MSG_IGNORE 2 /* 0x2 */ |
77 | #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */ |
78 | #define SSH2_MSG_DEBUG 4 /* 0x4 */ |
79 | #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */ |
80 | #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */ |
81 | #define SSH2_MSG_KEXINIT 20 /* 0x14 */ |
82 | #define SSH2_MSG_NEWKEYS 21 /* 0x15 */ |
83 | #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */ |
84 | #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */ |
a92dd380 |
85 | #define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */ |
86 | #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */ |
87 | #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */ |
88 | #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */ |
d211621f |
89 | #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */ |
90 | #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */ |
91 | #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */ |
92 | #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */ |
93 | #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */ |
94 | #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */ |
95 | #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */ |
96 | #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */ |
97 | #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */ |
98 | #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */ |
99 | #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */ |
100 | #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */ |
101 | #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */ |
102 | #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */ |
103 | #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */ |
104 | #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */ |
105 | #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */ |
106 | #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */ |
107 | #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */ |
108 | #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */ |
109 | |
9005f3ba |
110 | #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */ |
111 | #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */ |
112 | #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */ |
113 | #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */ |
114 | #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */ |
115 | #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */ |
116 | #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */ |
117 | #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */ |
118 | #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */ |
119 | #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */ |
120 | #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */ |
38c4a8da |
121 | #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */ |
122 | #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */ |
123 | #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */ |
124 | #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */ |
125 | |
126 | static const char *const ssh2_disconnect_reasons[] = { |
127 | NULL, |
128 | "SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT", |
129 | "SSH_DISCONNECT_PROTOCOL_ERROR", |
130 | "SSH_DISCONNECT_KEY_EXCHANGE_FAILED", |
131 | "SSH_DISCONNECT_HOST_AUTHENTICATION_FAILED", |
132 | "SSH_DISCONNECT_MAC_ERROR", |
133 | "SSH_DISCONNECT_COMPRESSION_ERROR", |
134 | "SSH_DISCONNECT_SERVICE_NOT_AVAILABLE", |
135 | "SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED", |
136 | "SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE", |
137 | "SSH_DISCONNECT_CONNECTION_LOST", |
138 | "SSH_DISCONNECT_BY_APPLICATION", |
139 | "SSH_DISCONNECT_TOO_MANY_CONNECTIONS", |
140 | "SSH_DISCONNECT_AUTH_CANCELLED_BY_USER", |
141 | "SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE", |
142 | "SSH_DISCONNECT_ILLEGAL_USER_NAME", |
143 | }; |
9005f3ba |
144 | |
d211621f |
145 | #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */ |
146 | #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */ |
147 | #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */ |
148 | #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */ |
149 | |
150 | #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */ |
fb09bf1c |
151 | |
7d503c31 |
152 | /* |
153 | * Various remote-bug flags. |
154 | */ |
155 | #define BUG_CHOKES_ON_SSH1_IGNORE 1 |
156 | #define BUG_SSH2_HMAC 2 |
157 | |
fb09bf1c |
158 | #define GET_32BIT(cp) \ |
159 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
160 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
161 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
162 | ((unsigned long)(unsigned char)(cp)[3])) |
163 | |
164 | #define PUT_32BIT(cp, value) { \ |
165 | (cp)[0] = (unsigned char)((value) >> 24); \ |
166 | (cp)[1] = (unsigned char)((value) >> 16); \ |
167 | (cp)[2] = (unsigned char)((value) >> 8); \ |
168 | (cp)[3] = (unsigned char)(value); } |
169 | |
7cca0d81 |
170 | enum { PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM }; |
972a41c8 |
171 | |
374330e2 |
172 | /* Coroutine mechanics for the sillier bits of the code */ |
173 | #define crBegin1 static int crLine = 0; |
174 | #define crBegin2 switch(crLine) { case 0:; |
175 | #define crBegin crBegin1; crBegin2; |
176 | #define crFinish(z) } crLine = 0; return (z) |
177 | #define crFinishV } crLine = 0; return |
178 | #define crReturn(z) \ |
179 | do {\ |
180 | crLine=__LINE__; return (z); case __LINE__:;\ |
181 | } while (0) |
182 | #define crReturnV \ |
183 | do {\ |
184 | crLine=__LINE__; return; case __LINE__:;\ |
185 | } while (0) |
186 | #define crStop(z) do{ crLine = 0; return (z); }while(0) |
187 | #define crStopV do{ crLine = 0; return; }while(0) |
fb09bf1c |
188 | #define crWaitUntil(c) do { crReturn(0); } while (!(c)) |
7cca0d81 |
189 | #define crWaitUntilV(c) do { crReturnV; } while (!(c)) |
374330e2 |
190 | |
0c6c9a70 |
191 | extern char *x11_init (Socket *, char *, void *); |
9c964e85 |
192 | extern void x11_close (Socket); |
193 | extern void x11_send (Socket , char *, int); |
194 | extern void x11_invent_auth(char *, int, char *, int); |
195 | |
5e8358ad |
196 | /* |
197 | * Ciphers for SSH2. We miss out single-DES because it isn't |
198 | * supported; also 3DES and Blowfish are both done differently from |
199 | * SSH1. (3DES uses outer chaining; Blowfish has the opposite |
200 | * endianness and different-sized keys.) |
5e8358ad |
201 | */ |
0a3f1d48 |
202 | const static struct ssh2_ciphers *ciphers[] = { |
203 | &ssh2_aes, |
204 | &ssh2_blowfish, |
205 | &ssh2_3des, |
8479e33c |
206 | }; |
e5574168 |
207 | |
a92dd380 |
208 | const static struct ssh_kex *kex_algs[] = { |
a92dd380 |
209 | &ssh_diffiehellman_gex, |
a92dd380 |
210 | &ssh_diffiehellman }; |
e5574168 |
211 | |
85cc02bb |
212 | const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss }; |
e5574168 |
213 | |
d39f364a |
214 | static void nullmac_key(unsigned char *key) { } |
e5574168 |
215 | static void nullmac_generate(unsigned char *blk, int len, unsigned long seq) { } |
216 | static int nullmac_verify(unsigned char *blk, int len, unsigned long seq) { return 1; } |
57476f6b |
217 | const static struct ssh_mac ssh_mac_none = { |
d39f364a |
218 | nullmac_key, nullmac_key, nullmac_generate, nullmac_verify, "none", 0 |
e5574168 |
219 | }; |
8b2715b2 |
220 | const static struct ssh_mac *macs[] = { |
221 | &ssh_sha1, &ssh_md5, &ssh_mac_none }; |
222 | const static struct ssh_mac *buggymacs[] = { |
223 | &ssh_sha1_buggy, &ssh_md5, &ssh_mac_none }; |
e5574168 |
224 | |
4ba9b64b |
225 | static void ssh_comp_none_init(void) { } |
226 | static int ssh_comp_none_block(unsigned char *block, int len, |
227 | unsigned char **outblock, int *outlen) { |
228 | return 0; |
229 | } |
63b00c83 |
230 | static int ssh_comp_none_disable(void) { return 0; } |
57476f6b |
231 | const static struct ssh_compress ssh_comp_none = { |
4ba9b64b |
232 | "none", |
233 | ssh_comp_none_init, ssh_comp_none_block, |
63b00c83 |
234 | ssh_comp_none_init, ssh_comp_none_block, |
235 | ssh_comp_none_disable |
e5574168 |
236 | }; |
4ba9b64b |
237 | extern const struct ssh_compress ssh_zlib; |
238 | const static struct ssh_compress *compressions[] = { |
239 | &ssh_zlib, &ssh_comp_none }; |
374330e2 |
240 | |
783415f8 |
241 | enum { /* channel types */ |
242 | CHAN_MAINSESSION, |
243 | CHAN_X11, |
244 | CHAN_AGENT, |
245 | }; |
246 | |
dacbd0e8 |
247 | /* |
248 | * 2-3-4 tree storing channels. |
249 | */ |
250 | struct ssh_channel { |
d211621f |
251 | unsigned remoteid, localid; |
dacbd0e8 |
252 | int type; |
253 | int closes; |
783415f8 |
254 | struct ssh2_data_channel { |
255 | unsigned char *outbuffer; |
256 | unsigned outbuflen, outbufsize; |
257 | unsigned remwindow, remmaxpkt; |
258 | } v2; |
dacbd0e8 |
259 | union { |
260 | struct ssh_agent_channel { |
261 | unsigned char *message; |
262 | unsigned char msglen[4]; |
263 | int lensofar, totallen; |
264 | } a; |
9c964e85 |
265 | struct ssh_x11_channel { |
266 | Socket s; |
783415f8 |
267 | } x11; |
dacbd0e8 |
268 | } u; |
269 | }; |
57476f6b |
270 | |
271 | struct Packet { |
272 | long length; |
273 | int type; |
274 | unsigned char *data; |
275 | unsigned char *body; |
276 | long savedpos; |
277 | long maxlen; |
278 | }; |
279 | |
0db56f73 |
280 | static SHA_State exhash, exhashbase; |
57476f6b |
281 | |
8df7a775 |
282 | static Socket s = NULL; |
57476f6b |
283 | |
284 | static unsigned char session_key[32]; |
4ba9b64b |
285 | static int ssh1_compressing; |
b96dc54c |
286 | static int ssh1_remote_protoflags; |
287 | static int ssh1_local_protoflags; |
db7d555c |
288 | static int ssh_agentfwd_enabled; |
9c964e85 |
289 | static int ssh_X11_fwd_enabled; |
7d503c31 |
290 | static int ssh_remote_bugs; |
57476f6b |
291 | static const struct ssh_cipher *cipher = NULL; |
0a3f1d48 |
292 | static const struct ssh2_cipher *cscipher = NULL; |
293 | static const struct ssh2_cipher *sccipher = NULL; |
57476f6b |
294 | static const struct ssh_mac *csmac = NULL; |
295 | static const struct ssh_mac *scmac = NULL; |
296 | static const struct ssh_compress *cscomp = NULL; |
297 | static const struct ssh_compress *sccomp = NULL; |
298 | static const struct ssh_kex *kex = NULL; |
e055a386 |
299 | static const struct ssh_signkey *hostkey = NULL; |
65a22376 |
300 | static unsigned char ssh2_session_id[20]; |
fa17a66e |
301 | int (*ssh_get_line)(const char *prompt, char *str, int maxlen, |
302 | int is_pw) = NULL; |
57476f6b |
303 | |
304 | static char *savedhost; |
305 | static int savedport; |
306 | static int ssh_send_ok; |
0965bee0 |
307 | static int ssh_echoing, ssh_editing; |
57476f6b |
308 | |
dacbd0e8 |
309 | static tree234 *ssh_channels; /* indexed by local id */ |
57476f6b |
310 | static struct ssh_channel *mainchan; /* primary session channel */ |
311 | |
312 | static enum { |
3687d221 |
313 | SSH_STATE_PREPACKET, |
57476f6b |
314 | SSH_STATE_BEFORE_SIZE, |
315 | SSH_STATE_INTERMED, |
316 | SSH_STATE_SESSION, |
317 | SSH_STATE_CLOSED |
3687d221 |
318 | } ssh_state = SSH_STATE_PREPACKET; |
57476f6b |
319 | |
3687d221 |
320 | static int size_needed = FALSE, eof_needed = FALSE; |
57476f6b |
321 | |
322 | static struct Packet pktin = { 0, 0, NULL, NULL, 0 }; |
323 | static struct Packet pktout = { 0, 0, NULL, NULL, 0 }; |
b185170a |
324 | static unsigned char *deferred_send_data = NULL; |
325 | static int deferred_len = 0, deferred_size = 0; |
57476f6b |
326 | |
327 | static int ssh_version; |
328 | static void (*ssh_protocol)(unsigned char *in, int inlen, int ispkt); |
329 | static void ssh1_protocol(unsigned char *in, int inlen, int ispkt); |
330 | static void ssh2_protocol(unsigned char *in, int inlen, int ispkt); |
331 | static void ssh_size(void); |
3687d221 |
332 | static void ssh_special (Telnet_Special); |
783415f8 |
333 | static void ssh2_try_send(struct ssh_channel *c); |
334 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len); |
57476f6b |
335 | |
336 | static int (*s_rdpkt)(unsigned char **data, int *datalen); |
337 | |
338 | static struct rdpkt1_state_tag { |
339 | long len, pad, biglen, to_read; |
340 | unsigned long realcrc, gotcrc; |
341 | unsigned char *p; |
342 | int i; |
343 | int chunk; |
344 | } rdpkt1_state; |
345 | |
960e736a |
346 | static struct rdpkt2_state_tag { |
347 | long len, pad, payload, packetlen, maclen; |
348 | int i; |
349 | int cipherblk; |
350 | unsigned long incoming_sequence; |
351 | } rdpkt2_state; |
352 | |
dacbd0e8 |
353 | static int ssh_channelcmp(void *av, void *bv) { |
354 | struct ssh_channel *a = (struct ssh_channel *)av; |
355 | struct ssh_channel *b = (struct ssh_channel *)bv; |
356 | if (a->localid < b->localid) return -1; |
357 | if (a->localid > b->localid) return +1; |
358 | return 0; |
359 | } |
360 | static int ssh_channelfind(void *av, void *bv) { |
d211621f |
361 | unsigned *a = (unsigned *)av; |
dacbd0e8 |
362 | struct ssh_channel *b = (struct ssh_channel *)bv; |
363 | if (*a < b->localid) return -1; |
364 | if (*a > b->localid) return +1; |
365 | return 0; |
366 | } |
367 | |
d2371c81 |
368 | static int alloc_channel_id(void) { |
260f3dec |
369 | const unsigned CHANNEL_NUMBER_OFFSET = 256; |
370 | unsigned low, high, mid; |
d2371c81 |
371 | int tsize; |
372 | struct ssh_channel *c; |
373 | |
374 | /* |
375 | * First-fit allocation of channel numbers: always pick the |
376 | * lowest unused one. To do this, binary-search using the |
377 | * counted B-tree to find the largest channel ID which is in a |
378 | * contiguous sequence from the beginning. (Precisely |
379 | * everything in that sequence must have ID equal to its tree |
380 | * index plus CHANNEL_NUMBER_OFFSET.) |
381 | */ |
382 | tsize = count234(ssh_channels); |
383 | |
384 | low = -1; high = tsize; |
385 | while (high - low > 1) { |
386 | mid = (high + low) / 2; |
387 | c = index234(ssh_channels, mid); |
388 | if (c->localid == mid + CHANNEL_NUMBER_OFFSET) |
389 | low = mid; /* this one is fine */ |
390 | else |
391 | high = mid; /* this one is past it */ |
392 | } |
393 | /* |
394 | * Now low points to either -1, or the tree index of the |
395 | * largest ID in the initial sequence. |
396 | */ |
397 | { |
398 | unsigned i = low + 1 + CHANNEL_NUMBER_OFFSET; |
399 | assert(NULL == find234(ssh_channels, &i, ssh_channelfind)); |
400 | } |
401 | return low + 1 + CHANNEL_NUMBER_OFFSET; |
402 | } |
403 | |
374330e2 |
404 | static void c_write (char *buf, int len) { |
67779be7 |
405 | if ((flags & FLAG_STDERR)) { |
4017be6d |
406 | int i; |
407 | for (i = 0; i < len; i++) |
408 | if (buf[i] != '\r') |
409 | fputc(buf[i], stderr); |
fb09bf1c |
410 | return; |
411 | } |
fe50e814 |
412 | from_backend(1, buf, len); |
3bdaf79d |
413 | } |
414 | |
a209e957 |
415 | static void c_write_untrusted(char *buf, int len) { |
416 | int i; |
417 | for (i = 0; i < len; i++) { |
418 | if (buf[i] == '\n') |
419 | c_write("\r\n", 2); |
420 | else if ((buf[i] & 0x60) || (buf[i] == '\r')) |
421 | c_write(buf+i, 1); |
422 | } |
423 | } |
424 | |
1408a877 |
425 | static void c_write_str (char *buf) { |
426 | c_write(buf, strlen(buf)); |
427 | } |
428 | |
fb09bf1c |
429 | /* |
430 | * Collect incoming data in the incoming packet buffer. |
e5574168 |
431 | * Decipher and verify the packet when it is completely read. |
432 | * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets. |
fb09bf1c |
433 | * Update the *data and *datalen variables. |
434 | * Return the additional nr of bytes needed, or 0 when |
435 | * a complete packet is available. |
436 | */ |
e5574168 |
437 | static int ssh1_rdpkt(unsigned char **data, int *datalen) |
fb09bf1c |
438 | { |
57476f6b |
439 | struct rdpkt1_state_tag *st = &rdpkt1_state; |
374330e2 |
440 | |
441 | crBegin; |
374330e2 |
442 | |
fb09bf1c |
443 | next_packet: |
37508af4 |
444 | |
fb09bf1c |
445 | pktin.type = 0; |
446 | pktin.length = 0; |
374330e2 |
447 | |
57476f6b |
448 | for (st->i = st->len = 0; st->i < 4; st->i++) { |
fb09bf1c |
449 | while ((*datalen) == 0) |
57476f6b |
450 | crReturn(4-st->i); |
451 | st->len = (st->len << 8) + **data; |
fb09bf1c |
452 | (*data)++, (*datalen)--; |
453 | } |
374330e2 |
454 | |
fb09bf1c |
455 | #ifdef FWHACK |
57476f6b |
456 | if (st->len == 0x52656d6f) { /* "Remo"te server has closed ... */ |
457 | st->len = 0x300; /* big enough to carry to end */ |
fb09bf1c |
458 | } |
459 | #endif |
374330e2 |
460 | |
57476f6b |
461 | st->pad = 8 - (st->len % 8); |
462 | st->biglen = st->len + st->pad; |
463 | pktin.length = st->len - 5; |
fb09bf1c |
464 | |
57476f6b |
465 | if (pktin.maxlen < st->biglen) { |
466 | pktin.maxlen = st->biglen; |
dcbde236 |
467 | pktin.data = (pktin.data == NULL ? smalloc(st->biglen+APIEXTRA) : |
468 | srealloc(pktin.data, st->biglen+APIEXTRA)); |
fb09bf1c |
469 | if (!pktin.data) |
470 | fatalbox("Out of memory"); |
471 | } |
374330e2 |
472 | |
57476f6b |
473 | st->to_read = st->biglen; |
474 | st->p = pktin.data; |
475 | while (st->to_read > 0) { |
476 | st->chunk = st->to_read; |
fb09bf1c |
477 | while ((*datalen) == 0) |
57476f6b |
478 | crReturn(st->to_read); |
479 | if (st->chunk > (*datalen)) |
480 | st->chunk = (*datalen); |
481 | memcpy(st->p, *data, st->chunk); |
482 | *data += st->chunk; |
483 | *datalen -= st->chunk; |
484 | st->p += st->chunk; |
485 | st->to_read -= st->chunk; |
fb09bf1c |
486 | } |
374330e2 |
487 | |
fb09bf1c |
488 | if (cipher) |
57476f6b |
489 | cipher->decrypt(pktin.data, st->biglen); |
698a5108 |
490 | #if 0 |
491 | debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad)); |
492 | for (st->i = 0; st->i < st->biglen; st->i++) |
493 | debug((" %02x", (unsigned char)pktin.data[st->i])); |
494 | debug(("\r\n")); |
495 | #endif |
374330e2 |
496 | |
57476f6b |
497 | st->realcrc = crc32(pktin.data, st->biglen-4); |
498 | st->gotcrc = GET_32BIT(pktin.data+st->biglen-4); |
499 | if (st->gotcrc != st->realcrc) { |
8d5de777 |
500 | bombout(("Incorrect CRC received on packet")); |
501 | crReturn(0); |
fb09bf1c |
502 | } |
572f871e |
503 | |
4ba9b64b |
504 | pktin.body = pktin.data + st->pad + 1; |
505 | |
506 | if (ssh1_compressing) { |
507 | unsigned char *decompblk; |
508 | int decomplen; |
509 | #if 0 |
b96dc54c |
510 | { |
511 | int i; |
512 | debug(("Packet payload pre-decompression:\n")); |
513 | for (i = -1; i < pktin.length; i++) |
514 | debug((" %02x", (unsigned char)pktin.body[i])); |
515 | debug(("\r\n")); |
516 | } |
4ba9b64b |
517 | #endif |
518 | zlib_decompress_block(pktin.body-1, pktin.length+1, |
519 | &decompblk, &decomplen); |
520 | |
521 | if (pktin.maxlen < st->pad + decomplen) { |
522 | pktin.maxlen = st->pad + decomplen; |
dcbde236 |
523 | pktin.data = srealloc(pktin.data, pktin.maxlen+APIEXTRA); |
58298665 |
524 | pktin.body = pktin.data + st->pad + 1; |
4ba9b64b |
525 | if (!pktin.data) |
526 | fatalbox("Out of memory"); |
527 | } |
528 | |
529 | memcpy(pktin.body-1, decompblk, decomplen); |
dcbde236 |
530 | sfree(decompblk); |
4ba9b64b |
531 | pktin.length = decomplen-1; |
532 | #if 0 |
b96dc54c |
533 | { |
534 | int i; |
535 | debug(("Packet payload post-decompression:\n")); |
536 | for (i = -1; i < pktin.length; i++) |
537 | debug((" %02x", (unsigned char)pktin.body[i])); |
538 | debug(("\r\n")); |
539 | } |
4ba9b64b |
540 | #endif |
541 | } |
542 | |
e5574168 |
543 | if (pktin.type == SSH1_SMSG_STDOUT_DATA || |
544 | pktin.type == SSH1_SMSG_STDERR_DATA || |
545 | pktin.type == SSH1_MSG_DEBUG || |
18515c51 |
546 | pktin.type == SSH1_SMSG_AUTH_TIS_CHALLENGE || |
547 | pktin.type == SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
fb09bf1c |
548 | long strlen = GET_32BIT(pktin.body); |
8d5de777 |
549 | if (strlen + 4 != pktin.length) { |
550 | bombout(("Received data packet with bogus string length")); |
551 | crReturn(0); |
552 | } |
fb09bf1c |
553 | } |
554 | |
4ba9b64b |
555 | pktin.type = pktin.body[-1]; |
556 | |
e5574168 |
557 | if (pktin.type == SSH1_MSG_DEBUG) { |
fb09bf1c |
558 | /* log debug message */ |
559 | char buf[80]; |
560 | int strlen = GET_32BIT(pktin.body); |
561 | strcpy(buf, "Remote: "); |
562 | if (strlen > 70) strlen = 70; |
563 | memcpy(buf+8, pktin.body+4, strlen); |
564 | buf[8+strlen] = '\0'; |
565 | logevent(buf); |
566 | goto next_packet; |
e5574168 |
567 | } else if (pktin.type == SSH1_MSG_IGNORE) { |
fb09bf1c |
568 | /* do nothing */ |
569 | goto next_packet; |
570 | } |
571 | |
38c4a8da |
572 | if (pktin.type == SSH1_MSG_DISCONNECT) { |
573 | /* log reason code in disconnect message */ |
574 | char buf[256]; |
260f3dec |
575 | unsigned msglen = GET_32BIT(pktin.body); |
576 | unsigned nowlen; |
38c4a8da |
577 | strcpy(buf, "Remote sent disconnect: "); |
578 | nowlen = strlen(buf); |
579 | if (msglen > sizeof(buf)-nowlen-1) |
580 | msglen = sizeof(buf)-nowlen-1; |
581 | memcpy(buf+nowlen, pktin.body+4, msglen); |
582 | buf[nowlen+msglen] = '\0'; |
583 | logevent(buf); |
584 | } |
585 | |
fb09bf1c |
586 | crFinish(0); |
587 | } |
588 | |
e5574168 |
589 | static int ssh2_rdpkt(unsigned char **data, int *datalen) |
590 | { |
960e736a |
591 | struct rdpkt2_state_tag *st = &rdpkt2_state; |
e5574168 |
592 | |
593 | crBegin; |
594 | |
595 | next_packet: |
e5574168 |
596 | pktin.type = 0; |
597 | pktin.length = 0; |
960e736a |
598 | if (sccipher) |
599 | st->cipherblk = sccipher->blksize; |
e5574168 |
600 | else |
960e736a |
601 | st->cipherblk = 8; |
602 | if (st->cipherblk < 8) |
603 | st->cipherblk = 8; |
604 | |
605 | if (pktin.maxlen < st->cipherblk) { |
606 | pktin.maxlen = st->cipherblk; |
dcbde236 |
607 | pktin.data = (pktin.data == NULL ? smalloc(st->cipherblk+APIEXTRA) : |
608 | srealloc(pktin.data, st->cipherblk+APIEXTRA)); |
e5574168 |
609 | if (!pktin.data) |
610 | fatalbox("Out of memory"); |
611 | } |
612 | |
613 | /* |
614 | * Acquire and decrypt the first block of the packet. This will |
615 | * contain the length and padding details. |
616 | */ |
960e736a |
617 | for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) { |
e5574168 |
618 | while ((*datalen) == 0) |
960e736a |
619 | crReturn(st->cipherblk-st->i); |
620 | pktin.data[st->i] = *(*data)++; |
e5574168 |
621 | (*datalen)--; |
622 | } |
623 | #ifdef FWHACK |
624 | if (!memcmp(pktin.data, "Remo", 4)) {/* "Remo"te server has closed ... */ |
625 | /* FIXME */ |
626 | } |
627 | #endif |
628 | if (sccipher) |
960e736a |
629 | sccipher->decrypt(pktin.data, st->cipherblk); |
e5574168 |
630 | |
631 | /* |
632 | * Now get the length and padding figures. |
633 | */ |
960e736a |
634 | st->len = GET_32BIT(pktin.data); |
635 | st->pad = pktin.data[4]; |
e5574168 |
636 | |
637 | /* |
638 | * This enables us to deduce the payload length. |
639 | */ |
960e736a |
640 | st->payload = st->len - st->pad - 1; |
e5574168 |
641 | |
960e736a |
642 | pktin.length = st->payload + 5; |
e5574168 |
643 | |
644 | /* |
645 | * So now we can work out the total packet length. |
646 | */ |
960e736a |
647 | st->packetlen = st->len + 4; |
648 | st->maclen = scmac ? scmac->len : 0; |
e5574168 |
649 | |
650 | /* |
651 | * Adjust memory allocation if packet is too big. |
652 | */ |
960e736a |
653 | if (pktin.maxlen < st->packetlen+st->maclen) { |
654 | pktin.maxlen = st->packetlen+st->maclen; |
dcbde236 |
655 | pktin.data = (pktin.data == NULL ? smalloc(pktin.maxlen+APIEXTRA) : |
656 | srealloc(pktin.data, pktin.maxlen+APIEXTRA)); |
e5574168 |
657 | if (!pktin.data) |
658 | fatalbox("Out of memory"); |
659 | } |
660 | |
661 | /* |
662 | * Read and decrypt the remainder of the packet. |
663 | */ |
960e736a |
664 | for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; st->i++) { |
e5574168 |
665 | while ((*datalen) == 0) |
960e736a |
666 | crReturn(st->packetlen + st->maclen - st->i); |
667 | pktin.data[st->i] = *(*data)++; |
e5574168 |
668 | (*datalen)--; |
669 | } |
670 | /* Decrypt everything _except_ the MAC. */ |
671 | if (sccipher) |
960e736a |
672 | sccipher->decrypt(pktin.data + st->cipherblk, |
673 | st->packetlen - st->cipherblk); |
e5574168 |
674 | |
7cca0d81 |
675 | #if 0 |
960e736a |
676 | debug(("Got packet len=%d pad=%d\r\n", st->len, st->pad)); |
677 | for (st->i = 0; st->i < st->packetlen; st->i++) |
678 | debug((" %02x", (unsigned char)pktin.data[st->i])); |
d39f364a |
679 | debug(("\r\n")); |
7cca0d81 |
680 | #endif |
d39f364a |
681 | |
e5574168 |
682 | /* |
683 | * Check the MAC. |
684 | */ |
960e736a |
685 | if (scmac && !scmac->verify(pktin.data, st->len+4, st->incoming_sequence)) { |
8d5de777 |
686 | bombout(("Incorrect MAC received on packet")); |
687 | crReturn(0); |
688 | } |
960e736a |
689 | st->incoming_sequence++; /* whether or not we MACed */ |
e5574168 |
690 | |
4ba9b64b |
691 | /* |
692 | * Decompress packet payload. |
693 | */ |
694 | { |
695 | unsigned char *newpayload; |
696 | int newlen; |
697 | if (sccomp && sccomp->decompress(pktin.data+5, pktin.length-5, |
698 | &newpayload, &newlen)) { |
699 | if (pktin.maxlen < newlen+5) { |
700 | pktin.maxlen = newlen+5; |
dcbde236 |
701 | pktin.data = (pktin.data == NULL ? smalloc(pktin.maxlen+APIEXTRA) : |
702 | srealloc(pktin.data, pktin.maxlen+APIEXTRA)); |
4ba9b64b |
703 | if (!pktin.data) |
704 | fatalbox("Out of memory"); |
705 | } |
706 | pktin.length = 5 + newlen; |
707 | memcpy(pktin.data+5, newpayload, newlen); |
708 | #if 0 |
709 | debug(("Post-decompression payload:\r\n")); |
710 | for (st->i = 0; st->i < newlen; st->i++) |
711 | debug((" %02x", (unsigned char)pktin.data[5+st->i])); |
712 | debug(("\r\n")); |
713 | #endif |
714 | |
dcbde236 |
715 | sfree(newpayload); |
4ba9b64b |
716 | } |
717 | } |
718 | |
e5574168 |
719 | pktin.savedpos = 6; |
720 | pktin.type = pktin.data[5]; |
e5574168 |
721 | |
7cca0d81 |
722 | if (pktin.type == SSH2_MSG_IGNORE || pktin.type == SSH2_MSG_DEBUG) |
723 | goto next_packet; /* FIXME: print DEBUG message */ |
e5574168 |
724 | |
38c4a8da |
725 | if (pktin.type == SSH2_MSG_DISCONNECT) { |
726 | /* log reason code in disconnect message */ |
727 | char buf[256]; |
728 | int reason = GET_32BIT(pktin.data+6); |
260f3dec |
729 | unsigned msglen = GET_32BIT(pktin.data+10); |
730 | unsigned nowlen; |
38c4a8da |
731 | if (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) { |
732 | sprintf(buf, "Received disconnect message (%s)", |
733 | ssh2_disconnect_reasons[reason]); |
734 | } else { |
735 | sprintf(buf, "Received disconnect message (unknown type %d)", reason); |
736 | } |
737 | logevent(buf); |
738 | strcpy(buf, "Disconnection message text: "); |
739 | nowlen = strlen(buf); |
740 | if (msglen > sizeof(buf)-nowlen-1) |
741 | msglen = sizeof(buf)-nowlen-1; |
742 | memcpy(buf+nowlen, pktin.data+14, msglen); |
743 | buf[nowlen+msglen] = '\0'; |
744 | logevent(buf); |
745 | } |
746 | |
e5574168 |
747 | crFinish(0); |
748 | } |
749 | |
4ba9b64b |
750 | static void ssh1_pktout_size(int len) { |
374330e2 |
751 | int pad, biglen; |
752 | |
753 | len += 5; /* type and CRC */ |
754 | pad = 8 - (len%8); |
755 | biglen = len + pad; |
756 | |
757 | pktout.length = len-5; |
758 | if (pktout.maxlen < biglen) { |
759 | pktout.maxlen = biglen; |
8f203108 |
760 | #ifdef MSCRYPTOAPI |
fb09bf1c |
761 | /* Allocate enough buffer space for extra block |
8f203108 |
762 | * for MS CryptEncrypt() */ |
dcbde236 |
763 | pktout.data = (pktout.data == NULL ? smalloc(biglen+12) : |
764 | srealloc(pktout.data, biglen+12)); |
8f203108 |
765 | #else |
dcbde236 |
766 | pktout.data = (pktout.data == NULL ? smalloc(biglen+4) : |
767 | srealloc(pktout.data, biglen+4)); |
8f203108 |
768 | #endif |
374330e2 |
769 | if (!pktout.data) |
770 | fatalbox("Out of memory"); |
771 | } |
4ba9b64b |
772 | pktout.body = pktout.data+4+pad+1; |
773 | } |
374330e2 |
774 | |
4ba9b64b |
775 | static void s_wrpkt_start(int type, int len) { |
776 | ssh1_pktout_size(len); |
374330e2 |
777 | pktout.type = type; |
374330e2 |
778 | } |
779 | |
39065bed |
780 | static int s_wrpkt_prepare(void) { |
374330e2 |
781 | int pad, len, biglen, i; |
782 | unsigned long crc; |
783 | |
4ba9b64b |
784 | pktout.body[-1] = pktout.type; |
785 | |
7d503c31 |
786 | #if 0 |
787 | debug(("Packet payload pre-compression:\n")); |
788 | for (i = -1; i < pktout.length; i++) |
789 | debug((" %02x", (unsigned char)pktout.body[i])); |
790 | debug(("\r\n")); |
791 | #endif |
792 | |
4ba9b64b |
793 | if (ssh1_compressing) { |
794 | unsigned char *compblk; |
795 | int complen; |
4ba9b64b |
796 | zlib_compress_block(pktout.body-1, pktout.length+1, |
797 | &compblk, &complen); |
798 | ssh1_pktout_size(complen-1); |
799 | memcpy(pktout.body-1, compblk, complen); |
dcbde236 |
800 | sfree(compblk); |
4ba9b64b |
801 | #if 0 |
802 | debug(("Packet payload post-compression:\n")); |
803 | for (i = -1; i < pktout.length; i++) |
804 | debug((" %02x", (unsigned char)pktout.body[i])); |
805 | debug(("\r\n")); |
806 | #endif |
807 | } |
808 | |
374330e2 |
809 | len = pktout.length + 5; /* type and CRC */ |
810 | pad = 8 - (len%8); |
811 | biglen = len + pad; |
812 | |
374330e2 |
813 | for (i=0; i<pad; i++) |
814 | pktout.data[i+4] = random_byte(); |
815 | crc = crc32(pktout.data+4, biglen-4); |
fb09bf1c |
816 | PUT_32BIT(pktout.data+biglen, crc); |
817 | PUT_32BIT(pktout.data, len); |
374330e2 |
818 | |
698a5108 |
819 | #if 0 |
820 | debug(("Sending packet len=%d\r\n", biglen+4)); |
821 | for (i = 0; i < biglen+4; i++) |
822 | debug((" %02x", (unsigned char)pktout.data[i])); |
823 | debug(("\r\n")); |
824 | #endif |
374330e2 |
825 | if (cipher) |
826 | cipher->encrypt(pktout.data+4, biglen); |
827 | |
39065bed |
828 | return biglen+4; |
829 | } |
830 | |
831 | static void s_wrpkt(void) { |
832 | int len; |
833 | len = s_wrpkt_prepare(); |
834 | sk_write(s, pktout.data, len); |
835 | } |
836 | |
837 | static void s_wrpkt_defer(void) { |
838 | int len; |
839 | len = s_wrpkt_prepare(); |
840 | if (deferred_len + len > deferred_size) { |
841 | deferred_size = deferred_len + len + 128; |
842 | deferred_send_data = srealloc(deferred_send_data, deferred_size); |
843 | } |
844 | memcpy(deferred_send_data+deferred_len, pktout.data, len); |
845 | deferred_len += len; |
374330e2 |
846 | } |
847 | |
fb09bf1c |
848 | /* |
39065bed |
849 | * Construct a packet with the specified contents. |
fb09bf1c |
850 | */ |
39065bed |
851 | static void construct_packet(int pkttype, va_list ap1, va_list ap2) |
fb09bf1c |
852 | { |
fb09bf1c |
853 | unsigned char *p, *argp, argchar; |
854 | unsigned long argint; |
855 | int pktlen, argtype, arglen; |
7cca0d81 |
856 | Bignum bn; |
fb09bf1c |
857 | |
858 | pktlen = 0; |
39065bed |
859 | while ((argtype = va_arg(ap1, int)) != PKT_END) { |
fb09bf1c |
860 | switch (argtype) { |
861 | case PKT_INT: |
39065bed |
862 | (void) va_arg(ap1, int); |
fb09bf1c |
863 | pktlen += 4; |
864 | break; |
865 | case PKT_CHAR: |
39065bed |
866 | (void) va_arg(ap1, char); |
fb09bf1c |
867 | pktlen++; |
868 | break; |
869 | case PKT_DATA: |
39065bed |
870 | (void) va_arg(ap1, unsigned char *); |
871 | arglen = va_arg(ap1, int); |
fb09bf1c |
872 | pktlen += arglen; |
873 | break; |
874 | case PKT_STR: |
39065bed |
875 | argp = va_arg(ap1, unsigned char *); |
fb09bf1c |
876 | arglen = strlen(argp); |
877 | pktlen += 4 + arglen; |
878 | break; |
7cca0d81 |
879 | case PKT_BIGNUM: |
39065bed |
880 | bn = va_arg(ap1, Bignum); |
5c58ad2d |
881 | pktlen += ssh1_bignum_length(bn); |
7cca0d81 |
882 | break; |
fb09bf1c |
883 | default: |
884 | assert(0); |
885 | } |
886 | } |
fb09bf1c |
887 | |
888 | s_wrpkt_start(pkttype, pktlen); |
889 | p = pktout.body; |
890 | |
39065bed |
891 | while ((argtype = va_arg(ap2, int)) != PKT_END) { |
fb09bf1c |
892 | switch (argtype) { |
893 | case PKT_INT: |
39065bed |
894 | argint = va_arg(ap2, int); |
fb09bf1c |
895 | PUT_32BIT(p, argint); |
896 | p += 4; |
897 | break; |
898 | case PKT_CHAR: |
39065bed |
899 | argchar = va_arg(ap2, unsigned char); |
fb09bf1c |
900 | *p = argchar; |
901 | p++; |
902 | break; |
903 | case PKT_DATA: |
39065bed |
904 | argp = va_arg(ap2, unsigned char *); |
905 | arglen = va_arg(ap2, int); |
fb09bf1c |
906 | memcpy(p, argp, arglen); |
907 | p += arglen; |
908 | break; |
909 | case PKT_STR: |
39065bed |
910 | argp = va_arg(ap2, unsigned char *); |
fb09bf1c |
911 | arglen = strlen(argp); |
912 | PUT_32BIT(p, arglen); |
913 | memcpy(p + 4, argp, arglen); |
914 | p += 4 + arglen; |
915 | break; |
7cca0d81 |
916 | case PKT_BIGNUM: |
39065bed |
917 | bn = va_arg(ap2, Bignum); |
5c58ad2d |
918 | p += ssh1_write_bignum(p, bn); |
7cca0d81 |
919 | break; |
fb09bf1c |
920 | } |
921 | } |
39065bed |
922 | } |
fb09bf1c |
923 | |
39065bed |
924 | static void send_packet(int pkttype, ...) { |
925 | va_list ap1, ap2; |
926 | va_start(ap1, pkttype); |
927 | va_start(ap2, pkttype); |
928 | construct_packet(pkttype, ap1, ap2); |
fb09bf1c |
929 | s_wrpkt(); |
930 | } |
931 | |
39065bed |
932 | static void defer_packet(int pkttype, ...) { |
933 | va_list ap1, ap2; |
934 | va_start(ap1, pkttype); |
935 | va_start(ap2, pkttype); |
936 | construct_packet(pkttype, ap1, ap2); |
937 | s_wrpkt_defer(); |
938 | } |
939 | |
9697bfd2 |
940 | static int ssh_versioncmp(char *a, char *b) { |
941 | char *ae, *be; |
942 | unsigned long av, bv; |
943 | |
43aa02a7 |
944 | av = strtoul(a, &ae, 10); |
945 | bv = strtoul(b, &be, 10); |
9697bfd2 |
946 | if (av != bv) return (av < bv ? -1 : +1); |
947 | if (*ae == '.') ae++; |
948 | if (*be == '.') be++; |
43aa02a7 |
949 | av = strtoul(ae, &ae, 10); |
950 | bv = strtoul(be, &be, 10); |
9697bfd2 |
951 | if (av != bv) return (av < bv ? -1 : +1); |
952 | return 0; |
953 | } |
954 | |
e5574168 |
955 | |
956 | /* |
a92dd380 |
957 | * Utility routines for putting an SSH-protocol `string' and |
958 | * `uint32' into a SHA state. |
e5574168 |
959 | */ |
960 | #include <stdio.h> |
75cab814 |
961 | static void sha_string(SHA_State *s, void *str, int len) { |
e5574168 |
962 | unsigned char lenblk[4]; |
e5574168 |
963 | PUT_32BIT(lenblk, len); |
e5574168 |
964 | SHA_Bytes(s, lenblk, 4); |
e5574168 |
965 | SHA_Bytes(s, str, len); |
966 | } |
967 | |
a92dd380 |
968 | static void sha_uint32(SHA_State *s, unsigned i) { |
969 | unsigned char intblk[4]; |
970 | PUT_32BIT(intblk, i); |
971 | SHA_Bytes(s, intblk, 4); |
972 | } |
973 | |
7cca0d81 |
974 | /* |
975 | * SSH2 packet construction functions. |
976 | */ |
783415f8 |
977 | static void ssh2_pkt_ensure(int length) { |
978 | if (pktout.maxlen < length) { |
979 | pktout.maxlen = length + 256; |
dcbde236 |
980 | pktout.data = (pktout.data == NULL ? smalloc(pktout.maxlen+APIEXTRA) : |
981 | srealloc(pktout.data, pktout.maxlen+APIEXTRA)); |
7cca0d81 |
982 | if (!pktout.data) |
983 | fatalbox("Out of memory"); |
984 | } |
783415f8 |
985 | } |
986 | static void ssh2_pkt_adddata(void *data, int len) { |
987 | pktout.length += len; |
988 | ssh2_pkt_ensure(pktout.length); |
7cca0d81 |
989 | memcpy(pktout.data+pktout.length-len, data, len); |
990 | } |
75cab814 |
991 | static void ssh2_pkt_addbyte(unsigned char byte) { |
7cca0d81 |
992 | ssh2_pkt_adddata(&byte, 1); |
993 | } |
75cab814 |
994 | static void ssh2_pkt_init(int pkt_type) { |
7cca0d81 |
995 | pktout.length = 5; |
996 | ssh2_pkt_addbyte((unsigned char)pkt_type); |
997 | } |
75cab814 |
998 | static void ssh2_pkt_addbool(unsigned char value) { |
7cca0d81 |
999 | ssh2_pkt_adddata(&value, 1); |
1000 | } |
75cab814 |
1001 | static void ssh2_pkt_adduint32(unsigned long value) { |
7cca0d81 |
1002 | unsigned char x[4]; |
1003 | PUT_32BIT(x, value); |
1004 | ssh2_pkt_adddata(x, 4); |
1005 | } |
75cab814 |
1006 | static void ssh2_pkt_addstring_start(void) { |
7cca0d81 |
1007 | ssh2_pkt_adduint32(0); |
1008 | pktout.savedpos = pktout.length; |
1009 | } |
75cab814 |
1010 | static void ssh2_pkt_addstring_str(char *data) { |
7cca0d81 |
1011 | ssh2_pkt_adddata(data, strlen(data)); |
1012 | PUT_32BIT(pktout.data + pktout.savedpos - 4, |
1013 | pktout.length - pktout.savedpos); |
1014 | } |
75cab814 |
1015 | static void ssh2_pkt_addstring_data(char *data, int len) { |
7cca0d81 |
1016 | ssh2_pkt_adddata(data, len); |
1017 | PUT_32BIT(pktout.data + pktout.savedpos - 4, |
1018 | pktout.length - pktout.savedpos); |
1019 | } |
75cab814 |
1020 | static void ssh2_pkt_addstring(char *data) { |
7cca0d81 |
1021 | ssh2_pkt_addstring_start(); |
1022 | ssh2_pkt_addstring_str(data); |
1023 | } |
75cab814 |
1024 | static char *ssh2_mpint_fmt(Bignum b, int *len) { |
7cca0d81 |
1025 | unsigned char *p; |
ddecd643 |
1026 | int i, n = (bignum_bitcount(b)+7)/8; |
3709bfe9 |
1027 | p = smalloc(n + 1); |
7cca0d81 |
1028 | if (!p) |
1029 | fatalbox("out of memory"); |
1030 | p[0] = 0; |
3709bfe9 |
1031 | for (i = 1; i <= n; i++) |
1032 | p[i] = bignum_byte(b, n-i); |
7cca0d81 |
1033 | i = 0; |
3709bfe9 |
1034 | while (i <= n && p[i] == 0 && (p[i+1] & 0x80) == 0) |
7cca0d81 |
1035 | i++; |
3709bfe9 |
1036 | memmove(p, p+i, n+1-i); |
1037 | *len = n+1-i; |
7cca0d81 |
1038 | return p; |
1039 | } |
75cab814 |
1040 | static void ssh2_pkt_addmp(Bignum b) { |
7cca0d81 |
1041 | unsigned char *p; |
1042 | int len; |
1043 | p = ssh2_mpint_fmt(b, &len); |
1044 | ssh2_pkt_addstring_start(); |
1045 | ssh2_pkt_addstring_data(p, len); |
dcbde236 |
1046 | sfree(p); |
7cca0d81 |
1047 | } |
b185170a |
1048 | |
1049 | /* |
1050 | * Construct an SSH2 final-form packet: compress it, encrypt it, |
1051 | * put the MAC on it. Final packet, ready to be sent, is stored in |
1052 | * pktout.data. Total length is returned. |
1053 | */ |
1054 | static int ssh2_pkt_construct(void) { |
7cca0d81 |
1055 | int cipherblk, maclen, padding, i; |
1056 | static unsigned long outgoing_sequence = 0; |
1057 | |
1058 | /* |
4ba9b64b |
1059 | * Compress packet payload. |
1060 | */ |
1061 | #if 0 |
1062 | debug(("Pre-compression payload:\r\n")); |
1063 | for (i = 5; i < pktout.length; i++) |
1064 | debug((" %02x", (unsigned char)pktout.data[i])); |
1065 | debug(("\r\n")); |
1066 | #endif |
1067 | { |
1068 | unsigned char *newpayload; |
1069 | int newlen; |
1070 | if (cscomp && cscomp->compress(pktout.data+5, pktout.length-5, |
1071 | &newpayload, &newlen)) { |
1072 | pktout.length = 5; |
1073 | ssh2_pkt_adddata(newpayload, newlen); |
dcbde236 |
1074 | sfree(newpayload); |
4ba9b64b |
1075 | } |
1076 | } |
1077 | |
1078 | /* |
7cca0d81 |
1079 | * Add padding. At least four bytes, and must also bring total |
1080 | * length (minus MAC) up to a multiple of the block size. |
1081 | */ |
8479e33c |
1082 | cipherblk = cscipher ? cscipher->blksize : 8; /* block size */ |
7cca0d81 |
1083 | cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */ |
1084 | padding = 4; |
1085 | padding += (cipherblk - (pktout.length + padding) % cipherblk) % cipherblk; |
783415f8 |
1086 | maclen = csmac ? csmac->len : 0; |
1087 | ssh2_pkt_ensure(pktout.length + padding + maclen); |
7cca0d81 |
1088 | pktout.data[4] = padding; |
1089 | for (i = 0; i < padding; i++) |
1090 | pktout.data[pktout.length + i] = random_byte(); |
1091 | PUT_32BIT(pktout.data, pktout.length + padding - 4); |
1092 | if (csmac) |
1093 | csmac->generate(pktout.data, pktout.length + padding, |
1094 | outgoing_sequence); |
1095 | outgoing_sequence++; /* whether or not we MACed */ |
1096 | |
1097 | #if 0 |
1098 | debug(("Sending packet len=%d\r\n", pktout.length+padding)); |
1099 | for (i = 0; i < pktout.length+padding; i++) |
1100 | debug((" %02x", (unsigned char)pktout.data[i])); |
1101 | debug(("\r\n")); |
1102 | #endif |
1103 | |
1104 | if (cscipher) |
1105 | cscipher->encrypt(pktout.data, pktout.length + padding); |
7cca0d81 |
1106 | |
b185170a |
1107 | /* Ready-to-send packet starts at pktout.data. We return length. */ |
1108 | return pktout.length + padding + maclen; |
1109 | } |
1110 | |
1111 | /* |
1112 | * Construct and send an SSH2 packet immediately. |
1113 | */ |
1114 | static void ssh2_pkt_send(void) { |
1115 | int len = ssh2_pkt_construct(); |
1116 | sk_write(s, pktout.data, len); |
1117 | } |
1118 | |
1119 | /* |
1120 | * Construct an SSH2 packet and add it to a deferred data block. |
1121 | * Useful for sending multiple packets in a single sk_write() call, |
1122 | * to prevent a traffic-analysing listener from being able to work |
1123 | * out the length of any particular packet (such as the password |
1124 | * packet). |
1125 | * |
1126 | * Note that because SSH2 sequence-numbers its packets, this can |
1127 | * NOT be used as an m4-style `defer' allowing packets to be |
1128 | * constructed in one order and sent in another. |
1129 | */ |
1130 | static void ssh2_pkt_defer(void) { |
1131 | int len = ssh2_pkt_construct(); |
1132 | if (deferred_len + len > deferred_size) { |
1133 | deferred_size = deferred_len + len + 128; |
1134 | deferred_send_data = srealloc(deferred_send_data, deferred_size); |
1135 | } |
1136 | memcpy(deferred_send_data+deferred_len, pktout.data, len); |
1137 | deferred_len += len; |
1138 | } |
1139 | |
1140 | /* |
1141 | * Send the whole deferred data block constructed by |
39065bed |
1142 | * ssh2_pkt_defer() or SSH1's defer_packet(). |
b185170a |
1143 | */ |
39065bed |
1144 | static void ssh_pkt_defersend(void) { |
b185170a |
1145 | sk_write(s, deferred_send_data, deferred_len); |
1146 | deferred_len = deferred_size = 0; |
1147 | sfree(deferred_send_data); |
1148 | deferred_send_data = NULL; |
7cca0d81 |
1149 | } |
1150 | |
1151 | #if 0 |
1152 | void bndebug(char *string, Bignum b) { |
1153 | unsigned char *p; |
1154 | int i, len; |
1155 | p = ssh2_mpint_fmt(b, &len); |
1156 | debug(("%s", string)); |
1157 | for (i = 0; i < len; i++) |
1158 | debug((" %02x", p[i])); |
1159 | debug(("\r\n")); |
dcbde236 |
1160 | sfree(p); |
7cca0d81 |
1161 | } |
1162 | #endif |
1163 | |
75cab814 |
1164 | static void sha_mpint(SHA_State *s, Bignum b) { |
7cca0d81 |
1165 | unsigned char *p; |
1166 | int len; |
1167 | p = ssh2_mpint_fmt(b, &len); |
1168 | sha_string(s, p, len); |
dcbde236 |
1169 | sfree(p); |
7cca0d81 |
1170 | } |
1171 | |
1172 | /* |
1173 | * SSH2 packet decode functions. |
1174 | */ |
75cab814 |
1175 | static unsigned long ssh2_pkt_getuint32(void) { |
7cca0d81 |
1176 | unsigned long value; |
1177 | if (pktin.length - pktin.savedpos < 4) |
1178 | return 0; /* arrgh, no way to decline (FIXME?) */ |
1179 | value = GET_32BIT(pktin.data+pktin.savedpos); |
1180 | pktin.savedpos += 4; |
1181 | return value; |
1182 | } |
65a22376 |
1183 | static int ssh2_pkt_getbool(void) { |
1184 | unsigned long value; |
1185 | if (pktin.length - pktin.savedpos < 1) |
1186 | return 0; /* arrgh, no way to decline (FIXME?) */ |
1187 | value = pktin.data[pktin.savedpos] != 0; |
1188 | pktin.savedpos++; |
1189 | return value; |
1190 | } |
75cab814 |
1191 | static void ssh2_pkt_getstring(char **p, int *length) { |
7cca0d81 |
1192 | *p = NULL; |
1193 | if (pktin.length - pktin.savedpos < 4) |
1194 | return; |
1195 | *length = GET_32BIT(pktin.data+pktin.savedpos); |
1196 | pktin.savedpos += 4; |
1197 | if (pktin.length - pktin.savedpos < *length) |
1198 | return; |
1199 | *p = pktin.data+pktin.savedpos; |
1200 | pktin.savedpos += *length; |
1201 | } |
75cab814 |
1202 | static Bignum ssh2_pkt_getmp(void) { |
7cca0d81 |
1203 | char *p; |
3709bfe9 |
1204 | int length; |
7cca0d81 |
1205 | Bignum b; |
1206 | |
1207 | ssh2_pkt_getstring(&p, &length); |
1208 | if (!p) |
1209 | return NULL; |
8d5de777 |
1210 | if (p[0] & 0x80) { |
1211 | bombout(("internal error: Can't handle negative mpints")); |
1212 | return NULL; |
1213 | } |
3709bfe9 |
1214 | b = bignum_from_bytes(p, length); |
7cca0d81 |
1215 | return b; |
1216 | } |
1217 | |
7d503c31 |
1218 | /* |
1219 | * Examine the remote side's version string and compare it against |
1220 | * a list of known buggy implementations. |
1221 | */ |
1222 | static void ssh_detect_bugs(char *vstring) { |
1223 | char *imp; /* pointer to implementation part */ |
1224 | imp = vstring; |
1225 | imp += strcspn(imp, "-"); |
620f0caf |
1226 | if (*imp) imp++; |
7d503c31 |
1227 | imp += strcspn(imp, "-"); |
620f0caf |
1228 | if (*imp) imp++; |
7d503c31 |
1229 | |
1230 | ssh_remote_bugs = 0; |
1231 | |
1232 | if (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") || |
1233 | !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") || |
1234 | !strcmp(imp, "1.2.22")) { |
1235 | /* |
1236 | * These versions don't support SSH1_MSG_IGNORE, so we have |
1237 | * to use a different defence against password length |
1238 | * sniffing. |
1239 | */ |
1240 | ssh_remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE; |
1241 | logevent("We believe remote version has SSH1 ignore bug"); |
1242 | } |
1243 | |
1244 | if (!strncmp(imp, "2.1.0", 5) || !strncmp(imp, "2.0.", 4) || |
1245 | !strncmp(imp, "2.2.0", 5) || !strncmp(imp, "2.3.0", 5) || |
1246 | !strncmp(imp, "2.1 ", 4)) { |
1247 | /* |
1248 | * These versions have the HMAC bug. |
1249 | */ |
1250 | ssh_remote_bugs |= BUG_SSH2_HMAC; |
1251 | logevent("We believe remote version has SSH2 HMAC bug"); |
1252 | } |
1253 | } |
1254 | |
8df7a775 |
1255 | static int do_ssh_init(unsigned char c) { |
50526e47 |
1256 | static char vslen; |
8df7a775 |
1257 | static char version[10]; |
50526e47 |
1258 | static char *vstring; |
1259 | static int vstrsize; |
1260 | static char *vlog; |
8df7a775 |
1261 | static int i; |
374330e2 |
1262 | |
8df7a775 |
1263 | crBegin; |
1264 | |
1265 | /* Search for the string "SSH-" in the input. */ |
374330e2 |
1266 | i = 0; |
8df7a775 |
1267 | while (1) { |
1268 | static const int transS[] = { 1, 2, 2, 1 }; |
1269 | static const int transH[] = { 0, 0, 3, 0 }; |
1270 | static const int transminus[] = { 0, 0, 0, -1 }; |
1271 | if (c == 'S') i = transS[i]; |
1272 | else if (c == 'H') i = transH[i]; |
1273 | else if (c == '-') i = transminus[i]; |
374330e2 |
1274 | else i = 0; |
8df7a775 |
1275 | if (i < 0) |
1276 | break; |
1277 | crReturn(1); /* get another character */ |
374330e2 |
1278 | } |
8df7a775 |
1279 | |
50526e47 |
1280 | vstring = smalloc(16); |
1281 | vstrsize = 16; |
c5e9c988 |
1282 | strcpy(vstring, "SSH-"); |
50526e47 |
1283 | vslen = 4; |
374330e2 |
1284 | i = 0; |
1285 | while (1) { |
8df7a775 |
1286 | crReturn(1); /* get another char */ |
50526e47 |
1287 | if (vslen >= vstrsize-1) { |
1288 | vstrsize += 16; |
1289 | vstring = srealloc(vstring, vstrsize); |
1290 | } |
1291 | vstring[vslen++] = c; |
374330e2 |
1292 | if (i >= 0) { |
1293 | if (c == '-') { |
1294 | version[i] = '\0'; |
1295 | i = -1; |
1296 | } else if (i < sizeof(version)-1) |
1297 | version[i++] = c; |
1298 | } |
1299 | else if (c == '\n') |
1300 | break; |
1301 | } |
1302 | |
db7d555c |
1303 | ssh_agentfwd_enabled = FALSE; |
960e736a |
1304 | rdpkt2_state.incoming_sequence = 0; |
1305 | |
50526e47 |
1306 | vstring[vslen] = 0; |
9275ca8a |
1307 | vlog = smalloc(20 + vslen); |
c5e9c988 |
1308 | sprintf(vlog, "Server version: %s", vstring); |
7d503c31 |
1309 | ssh_detect_bugs(vstring); |
c5e9c988 |
1310 | vlog[strcspn(vlog, "\r\n")] = '\0'; |
1311 | logevent(vlog); |
9275ca8a |
1312 | sfree(vlog); |
c5e9c988 |
1313 | |
adf799dd |
1314 | /* |
1315 | * Server version "1.99" means we can choose whether we use v1 |
1316 | * or v2 protocol. Choice is based on cfg.sshprot. |
1317 | */ |
1318 | if (ssh_versioncmp(version, cfg.sshprot == 1 ? "2.0" : "1.99") >= 0) { |
e5574168 |
1319 | /* |
1320 | * This is a v2 server. Begin v2 protocol. |
1321 | */ |
9275ca8a |
1322 | char verstring[80], vlog[100]; |
900a4ee6 |
1323 | sprintf(verstring, "SSH-2.0-%s", sshver); |
0db56f73 |
1324 | SHA_Init(&exhashbase); |
e5574168 |
1325 | /* |
1326 | * Hash our version string and their version string. |
1327 | */ |
0db56f73 |
1328 | sha_string(&exhashbase, verstring, strlen(verstring)); |
1329 | sha_string(&exhashbase, vstring, strcspn(vstring, "\r\n")); |
e5574168 |
1330 | sprintf(vlog, "We claim version: %s", verstring); |
1331 | logevent(vlog); |
9275ca8a |
1332 | strcat(verstring, "\n"); |
e5574168 |
1333 | logevent("Using SSH protocol version 2"); |
9275ca8a |
1334 | sk_write(s, verstring, strlen(verstring)); |
e5574168 |
1335 | ssh_protocol = ssh2_protocol; |
6abbf9e3 |
1336 | ssh_version = 2; |
e5574168 |
1337 | s_rdpkt = ssh2_rdpkt; |
1338 | } else { |
1339 | /* |
1340 | * This is a v1 server. Begin v1 protocol. |
1341 | */ |
9275ca8a |
1342 | char verstring[80], vlog[100]; |
1343 | sprintf(verstring, "SSH-%s-%s", |
900a4ee6 |
1344 | (ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5"), |
1345 | sshver); |
9275ca8a |
1346 | sprintf(vlog, "We claim version: %s", verstring); |
e5574168 |
1347 | logevent(vlog); |
9275ca8a |
1348 | strcat(verstring, "\n"); |
e5574168 |
1349 | logevent("Using SSH protocol version 1"); |
9275ca8a |
1350 | sk_write(s, verstring, strlen(verstring)); |
e5574168 |
1351 | ssh_protocol = ssh1_protocol; |
6abbf9e3 |
1352 | ssh_version = 1; |
e5574168 |
1353 | s_rdpkt = ssh1_rdpkt; |
1354 | } |
3687d221 |
1355 | ssh_state = SSH_STATE_BEFORE_SIZE; |
8df7a775 |
1356 | |
50526e47 |
1357 | sfree(vstring); |
50526e47 |
1358 | |
8df7a775 |
1359 | crFinish(0); |
1360 | } |
1361 | |
1362 | static void ssh_gotdata(unsigned char *data, int datalen) |
1363 | { |
1364 | crBegin; |
1365 | |
1366 | /* |
1367 | * To begin with, feed the characters one by one to the |
1368 | * protocol initialisation / selection function do_ssh_init(). |
1369 | * When that returns 0, we're done with the initial greeting |
1370 | * exchange and can move on to packet discipline. |
1371 | */ |
1372 | while (1) { |
1373 | int ret; |
1374 | if (datalen == 0) |
1375 | crReturnV; /* more data please */ |
1376 | ret = do_ssh_init(*data); |
1377 | data++; datalen--; |
1378 | if (ret == 0) |
1379 | break; |
1380 | } |
1381 | |
1382 | /* |
1383 | * We emerge from that loop when the initial negotiation is |
1384 | * over and we have selected an s_rdpkt function. Now pass |
1385 | * everything to s_rdpkt, and then pass the resulting packets |
1386 | * to the proper protocol handler. |
1387 | */ |
1388 | if (datalen == 0) |
1389 | crReturnV; |
1390 | while (1) { |
1391 | while (datalen > 0) { |
1392 | if ( s_rdpkt(&data, &datalen) == 0 ) { |
1393 | ssh_protocol(NULL, 0, 1); |
1394 | if (ssh_state == SSH_STATE_CLOSED) { |
1395 | return; |
1396 | } |
1397 | } |
1398 | } |
1399 | crReturnV; |
1400 | } |
1401 | crFinishV; |
1402 | } |
1403 | |
7e78000d |
1404 | static int ssh_closing (Plug plug, char *error_msg, int error_code, int calling_back) { |
1405 | ssh_state = SSH_STATE_CLOSED; |
1406 | sk_close(s); |
1407 | s = NULL; |
1408 | if (error_msg) { |
2c94fd1c |
1409 | /* A socket error has occurred. */ |
7e78000d |
1410 | connection_fatal (error_msg); |
1411 | } else { |
1412 | /* Otherwise, the remote side closed the connection normally. */ |
8df7a775 |
1413 | } |
7e78000d |
1414 | return 0; |
1415 | } |
1416 | |
1417 | static int ssh_receive(Plug plug, int urgent, char *data, int len) { |
8df7a775 |
1418 | ssh_gotdata (data, len); |
3257deae |
1419 | if (ssh_state == SSH_STATE_CLOSED) { |
1420 | if (s) { |
1421 | sk_close(s); |
1422 | s = NULL; |
1423 | } |
1424 | return 0; |
1425 | } |
fef97f43 |
1426 | return 1; |
374330e2 |
1427 | } |
1428 | |
fb09bf1c |
1429 | /* |
8df7a775 |
1430 | * Connect to specified host and port. |
1431 | * Returns an error message, or NULL on success. |
1432 | * Also places the canonical host name into `realhost'. |
1433 | */ |
1434 | static char *connect_to_host(char *host, int port, char **realhost) |
1435 | { |
7e78000d |
1436 | static struct plug_function_table fn_table = { |
1437 | ssh_closing, |
1438 | ssh_receive |
1439 | }, *fn_table_ptr = &fn_table; |
1440 | |
8df7a775 |
1441 | SockAddr addr; |
1442 | char *err; |
1443 | #ifdef FWHACK |
1444 | char *FWhost; |
1445 | int FWport; |
1446 | #endif |
1447 | |
dcbde236 |
1448 | savedhost = smalloc(1+strlen(host)); |
8df7a775 |
1449 | if (!savedhost) |
1450 | fatalbox("Out of memory"); |
1451 | strcpy(savedhost, host); |
1452 | |
1453 | if (port < 0) |
1454 | port = 22; /* default ssh port */ |
1455 | savedport = port; |
1456 | |
1457 | #ifdef FWHACK |
1458 | FWhost = host; |
1459 | FWport = port; |
1460 | host = FWSTR; |
1461 | port = 23; |
1462 | #endif |
1463 | |
1464 | /* |
1465 | * Try to find host. |
1466 | */ |
1467 | addr = sk_namelookup(host, realhost); |
1468 | if ( (err = sk_addr_error(addr)) ) |
1469 | return err; |
1470 | |
1471 | #ifdef FWHACK |
1472 | *realhost = FWhost; |
1473 | #endif |
1474 | |
1475 | /* |
1476 | * Open socket. |
1477 | */ |
7e78000d |
1478 | s = sk_new(addr, port, 0, 1, &fn_table_ptr); |
8df7a775 |
1479 | if ( (err = sk_socket_error(s)) ) |
1480 | return err; |
1481 | |
1482 | #ifdef FWHACK |
1483 | sk_write(s, "connect ", 8); |
1484 | sk_write(s, FWhost, strlen(FWhost)); |
1485 | { |
1486 | char buf[20]; |
1487 | sprintf(buf, " %d\n", FWport); |
1488 | sk_write(s, buf, strlen(buf)); |
1489 | } |
1490 | #endif |
1491 | |
1492 | return NULL; |
1493 | } |
1494 | |
1495 | /* |
fb09bf1c |
1496 | * Handle the key exchange and user authentication phases. |
1497 | */ |
e5574168 |
1498 | static int do_ssh1_login(unsigned char *in, int inlen, int ispkt) |
fb09bf1c |
1499 | { |
374330e2 |
1500 | int i, j, len; |
374330e2 |
1501 | unsigned char *rsabuf, *keystr1, *keystr2; |
1502 | unsigned char cookie[8]; |
1503 | struct RSAKey servkey, hostkey; |
1504 | struct MD5Context md5c; |
ccbfb941 |
1505 | static unsigned long supported_ciphers_mask, supported_auths_mask; |
7cca0d81 |
1506 | static int tried_publickey; |
1507 | static unsigned char session_id[16]; |
bea1ef5f |
1508 | int cipher_type; |
b585ec46 |
1509 | static char username[100]; |
374330e2 |
1510 | |
374330e2 |
1511 | crBegin; |
1512 | |
fb09bf1c |
1513 | if (!ispkt) crWaitUntil(ispkt); |
374330e2 |
1514 | |
8d5de777 |
1515 | if (pktin.type != SSH1_SMSG_PUBLIC_KEY) { |
1516 | bombout(("Public key packet not received")); |
1517 | crReturn(0); |
1518 | } |
374330e2 |
1519 | |
c5e9c988 |
1520 | logevent("Received public keys"); |
374330e2 |
1521 | |
c5e9c988 |
1522 | memcpy(cookie, pktin.body, 8); |
374330e2 |
1523 | |
7cca0d81 |
1524 | i = makekey(pktin.body+8, &servkey, &keystr1, 0); |
1525 | j = makekey(pktin.body+8+i, &hostkey, &keystr2, 0); |
374330e2 |
1526 | |
c5e9c988 |
1527 | /* |
1c2a93c4 |
1528 | * Log the host key fingerprint. |
c5e9c988 |
1529 | */ |
c5e9c988 |
1530 | { |
1531 | char logmsg[80]; |
1c2a93c4 |
1532 | logevent("Host key fingerprint is:"); |
c5e9c988 |
1533 | strcpy(logmsg, " "); |
1c2a93c4 |
1534 | hostkey.comment = NULL; |
1535 | rsa_fingerprint(logmsg+strlen(logmsg), sizeof(logmsg)-strlen(logmsg), |
1536 | &hostkey); |
c5e9c988 |
1537 | logevent(logmsg); |
1538 | } |
1539 | |
b96dc54c |
1540 | ssh1_remote_protoflags = GET_32BIT(pktin.body+8+i+j); |
fb09bf1c |
1541 | supported_ciphers_mask = GET_32BIT(pktin.body+12+i+j); |
1542 | supported_auths_mask = GET_32BIT(pktin.body+16+i+j); |
bea1ef5f |
1543 | |
b96dc54c |
1544 | ssh1_local_protoflags = ssh1_remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED; |
1545 | ssh1_local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER; |
1546 | |
c5e9c988 |
1547 | MD5Init(&md5c); |
374330e2 |
1548 | MD5Update(&md5c, keystr2, hostkey.bytes); |
1549 | MD5Update(&md5c, keystr1, servkey.bytes); |
1550 | MD5Update(&md5c, pktin.body, 8); |
374330e2 |
1551 | MD5Final(session_id, &md5c); |
1552 | |
1553 | for (i=0; i<32; i++) |
1554 | session_key[i] = random_byte(); |
1555 | |
1556 | len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes); |
1557 | |
dcbde236 |
1558 | rsabuf = smalloc(len); |
374330e2 |
1559 | if (!rsabuf) |
1560 | fatalbox("Out of memory"); |
1561 | |
89ee5268 |
1562 | /* |
1563 | * Verify the host key. |
1564 | */ |
1565 | { |
1566 | /* |
1567 | * First format the key into a string. |
1568 | */ |
1569 | int len = rsastr_len(&hostkey); |
d5859615 |
1570 | char fingerprint[100]; |
dcbde236 |
1571 | char *keystr = smalloc(len); |
89ee5268 |
1572 | if (!keystr) |
1573 | fatalbox("Out of memory"); |
1574 | rsastr_fmt(keystr, &hostkey); |
d5859615 |
1575 | rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey); |
d4857987 |
1576 | verify_ssh_host_key(savedhost, savedport, "rsa", keystr, fingerprint); |
dcbde236 |
1577 | sfree(keystr); |
89ee5268 |
1578 | } |
374330e2 |
1579 | |
1580 | for (i=0; i<32; i++) { |
1581 | rsabuf[i] = session_key[i]; |
1582 | if (i < 16) |
1583 | rsabuf[i] ^= session_id[i]; |
1584 | } |
1585 | |
1586 | if (hostkey.bytes > servkey.bytes) { |
1587 | rsaencrypt(rsabuf, 32, &servkey); |
1588 | rsaencrypt(rsabuf, servkey.bytes, &hostkey); |
1589 | } else { |
1590 | rsaencrypt(rsabuf, 32, &hostkey); |
1591 | rsaencrypt(rsabuf, hostkey.bytes, &servkey); |
1592 | } |
1593 | |
c5e9c988 |
1594 | logevent("Encrypted session key"); |
1595 | |
0a3f1d48 |
1596 | switch (cfg.cipher) { |
1597 | case CIPHER_BLOWFISH: cipher_type = SSH_CIPHER_BLOWFISH; break; |
1598 | case CIPHER_DES: cipher_type = SSH_CIPHER_DES; break; |
1599 | case CIPHER_3DES: cipher_type = SSH_CIPHER_3DES; break; |
1600 | case CIPHER_AES: |
1408a877 |
1601 | c_write_str("AES not supported in SSH1, falling back to 3DES\r\n"); |
0a3f1d48 |
1602 | cipher_type = SSH_CIPHER_3DES; |
1603 | break; |
1604 | } |
bea1ef5f |
1605 | if ((supported_ciphers_mask & (1 << cipher_type)) == 0) { |
1408a877 |
1606 | c_write_str("Selected cipher not supported, falling back to 3DES\r\n"); |
bea1ef5f |
1607 | cipher_type = SSH_CIPHER_3DES; |
a99a05c0 |
1608 | if ((supported_ciphers_mask & (1 << cipher_type)) == 0) { |
1609 | bombout(("Server violates SSH 1 protocol by " |
1610 | "not supporting 3DES encryption")); |
1611 | crReturn(0); |
1612 | } |
bea1ef5f |
1613 | } |
c5e9c988 |
1614 | switch (cipher_type) { |
1615 | case SSH_CIPHER_3DES: logevent("Using 3DES encryption"); break; |
1616 | case SSH_CIPHER_DES: logevent("Using single-DES encryption"); break; |
1617 | case SSH_CIPHER_BLOWFISH: logevent("Using Blowfish encryption"); break; |
1618 | } |
bea1ef5f |
1619 | |
e5574168 |
1620 | send_packet(SSH1_CMSG_SESSION_KEY, |
fb09bf1c |
1621 | PKT_CHAR, cipher_type, |
1622 | PKT_DATA, cookie, 8, |
1623 | PKT_CHAR, (len*8) >> 8, PKT_CHAR, (len*8) & 0xFF, |
1624 | PKT_DATA, rsabuf, len, |
b96dc54c |
1625 | PKT_INT, ssh1_local_protoflags, |
fb09bf1c |
1626 | PKT_END); |
1627 | |
c5e9c988 |
1628 | logevent("Trying to enable encryption..."); |
374330e2 |
1629 | |
dcbde236 |
1630 | sfree(rsabuf); |
374330e2 |
1631 | |
5e8358ad |
1632 | cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 : |
9697bfd2 |
1633 | cipher_type == SSH_CIPHER_DES ? &ssh_des : |
bea1ef5f |
1634 | &ssh_3des; |
374330e2 |
1635 | cipher->sesskey(session_key); |
1636 | |
fb09bf1c |
1637 | crWaitUntil(ispkt); |
374330e2 |
1638 | |
8d5de777 |
1639 | if (pktin.type != SSH1_SMSG_SUCCESS) { |
1640 | bombout(("Encryption not successfully enabled")); |
1641 | crReturn(0); |
1642 | } |
374330e2 |
1643 | |
c5e9c988 |
1644 | logevent("Successfully started encryption"); |
1645 | |
374330e2 |
1646 | fflush(stdout); |
1647 | { |
374330e2 |
1648 | static int pos = 0; |
1649 | static char c; |
67779be7 |
1650 | if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { |
fa17a66e |
1651 | if (ssh_get_line) { |
1652 | if (!ssh_get_line("login as: ", |
1653 | username, sizeof(username), FALSE)) { |
1654 | /* |
1655 | * get_line failed to get a username. |
1656 | * Terminate. |
1657 | */ |
1658 | logevent("No username provided. Abandoning session."); |
1659 | ssh_state = SSH_STATE_CLOSED; |
1660 | crReturn(1); |
1661 | } |
1662 | } else { |
1663 | c_write_str("login as: "); |
1664 | ssh_send_ok = 1; |
1665 | while (pos >= 0) { |
1666 | crWaitUntil(!ispkt); |
1667 | while (inlen--) switch (c = *in++) { |
1668 | case 10: case 13: |
1669 | username[pos] = 0; |
1670 | pos = -1; |
1671 | break; |
1672 | case 8: case 127: |
1673 | if (pos > 0) { |
1674 | c_write_str("\b \b"); |
1675 | pos--; |
1676 | } |
1677 | break; |
1678 | case 21: case 27: |
1679 | while (pos > 0) { |
1680 | c_write_str("\b \b"); |
1681 | pos--; |
1682 | } |
1683 | break; |
1684 | case 3: case 4: |
1685 | random_save_seed(); |
1686 | exit(0); |
1687 | break; |
1688 | default: |
1689 | if (((c >= ' ' && c <= '~') || |
1690 | ((unsigned char)c >= 160)) && pos < 40) { |
1691 | username[pos++] = c; |
1692 | c_write(&c, 1); |
1693 | } |
1694 | break; |
1695 | } |
1696 | } |
1697 | c_write_str("\r\n"); |
1698 | username[strcspn(username, "\n\r")] = '\0'; |
1699 | } |
1700 | } else { |
374330e2 |
1701 | strncpy(username, cfg.username, 99); |
1702 | username[99] = '\0'; |
374330e2 |
1703 | } |
fb09bf1c |
1704 | |
e5574168 |
1705 | send_packet(SSH1_CMSG_USER, PKT_STR, username, PKT_END); |
c5e9c988 |
1706 | { |
3c8e959b |
1707 | char userlog[22+sizeof(username)]; |
c5e9c988 |
1708 | sprintf(userlog, "Sent username \"%s\"", username); |
1709 | logevent(userlog); |
3c8e959b |
1710 | if (flags & FLAG_INTERACTIVE && |
1711 | (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) { |
1712 | strcat(userlog, "\r\n"); |
1408a877 |
1713 | c_write_str(userlog); |
3c8e959b |
1714 | } |
c5e9c988 |
1715 | } |
374330e2 |
1716 | } |
1717 | |
fb09bf1c |
1718 | crWaitUntil(ispkt); |
374330e2 |
1719 | |
7cca0d81 |
1720 | tried_publickey = 0; |
1721 | |
e5574168 |
1722 | while (pktin.type == SSH1_SMSG_FAILURE) { |
374330e2 |
1723 | static char password[100]; |
a52f067e |
1724 | static char prompt[200]; |
374330e2 |
1725 | static int pos; |
1726 | static char c; |
ccbfb941 |
1727 | static int pwpkt_type; |
ccbfb941 |
1728 | /* |
1729 | * Show password prompt, having first obtained it via a TIS |
18515c51 |
1730 | * or CryptoCard exchange if we're doing TIS or CryptoCard |
1731 | * authentication. |
ccbfb941 |
1732 | */ |
e5574168 |
1733 | pwpkt_type = SSH1_CMSG_AUTH_PASSWORD; |
5c58ad2d |
1734 | if (agent_exists()) { |
1735 | /* |
1736 | * Attempt RSA authentication using Pageant. |
1737 | */ |
1738 | static unsigned char request[5], *response, *p; |
1739 | static int responselen; |
1740 | static int i, nkeys; |
1741 | static int authed = FALSE; |
1742 | void *r; |
1743 | |
1744 | logevent("Pageant is running. Requesting keys."); |
1745 | |
1746 | /* Request the keys held by the agent. */ |
1747 | PUT_32BIT(request, 1); |
1983e559 |
1748 | request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES; |
5c58ad2d |
1749 | agent_query(request, 5, &r, &responselen); |
1750 | response = (unsigned char *)r; |
95797139 |
1751 | if (response && responselen >= 5 && |
1752 | response[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER) { |
5c58ad2d |
1753 | p = response + 5; |
1754 | nkeys = GET_32BIT(p); p += 4; |
6328ea7a |
1755 | { char buf[64]; sprintf(buf, "Pageant has %d SSH1 keys", nkeys); |
5c58ad2d |
1756 | logevent(buf); } |
1757 | for (i = 0; i < nkeys; i++) { |
1758 | static struct RSAKey key; |
1759 | static Bignum challenge; |
b8bb811a |
1760 | static char *commentp; |
1761 | static int commentlen; |
5c58ad2d |
1762 | |
1763 | { char buf[64]; sprintf(buf, "Trying Pageant key #%d", i); |
1764 | logevent(buf); } |
1765 | p += 4; |
1766 | p += ssh1_read_bignum(p, &key.exponent); |
1767 | p += ssh1_read_bignum(p, &key.modulus); |
b8bb811a |
1768 | commentlen = GET_32BIT(p); p += 4; |
1769 | commentp = p; p += commentlen; |
5c58ad2d |
1770 | send_packet(SSH1_CMSG_AUTH_RSA, |
1771 | PKT_BIGNUM, key.modulus, PKT_END); |
1772 | crWaitUntil(ispkt); |
1773 | if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
1774 | logevent("Key refused"); |
1775 | continue; |
1776 | } |
1777 | logevent("Received RSA challenge"); |
1778 | ssh1_read_bignum(pktin.body, &challenge); |
1779 | { |
1780 | char *agentreq, *q, *ret; |
1781 | int len, retlen; |
1782 | len = 1 + 4; /* message type, bit count */ |
1783 | len += ssh1_bignum_length(key.exponent); |
1784 | len += ssh1_bignum_length(key.modulus); |
1785 | len += ssh1_bignum_length(challenge); |
1786 | len += 16; /* session id */ |
1787 | len += 4; /* response format */ |
dcbde236 |
1788 | agentreq = smalloc(4 + len); |
5c58ad2d |
1789 | PUT_32BIT(agentreq, len); |
1790 | q = agentreq + 4; |
1983e559 |
1791 | *q++ = SSH1_AGENTC_RSA_CHALLENGE; |
ddecd643 |
1792 | PUT_32BIT(q, bignum_bitcount(key.modulus)); |
5c58ad2d |
1793 | q += 4; |
1794 | q += ssh1_write_bignum(q, key.exponent); |
1795 | q += ssh1_write_bignum(q, key.modulus); |
1796 | q += ssh1_write_bignum(q, challenge); |
1797 | memcpy(q, session_id, 16); q += 16; |
1798 | PUT_32BIT(q, 1); /* response format */ |
1799 | agent_query(agentreq, len+4, &ret, &retlen); |
dcbde236 |
1800 | sfree(agentreq); |
5c58ad2d |
1801 | if (ret) { |
1983e559 |
1802 | if (ret[4] == SSH1_AGENT_RSA_RESPONSE) { |
5c58ad2d |
1803 | logevent("Sending Pageant's response"); |
1804 | send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE, |
1805 | PKT_DATA, ret+5, 16, PKT_END); |
dcbde236 |
1806 | sfree(ret); |
5c58ad2d |
1807 | crWaitUntil(ispkt); |
1808 | if (pktin.type == SSH1_SMSG_SUCCESS) { |
1809 | logevent("Pageant's response accepted"); |
c4fa2fce |
1810 | if (flags & FLAG_VERBOSE) { |
1408a877 |
1811 | c_write_str("Authenticated using RSA key \""); |
c4fa2fce |
1812 | c_write(commentp, commentlen); |
1408a877 |
1813 | c_write_str("\" from agent\r\n"); |
c4fa2fce |
1814 | } |
5c58ad2d |
1815 | authed = TRUE; |
1816 | } else |
1817 | logevent("Pageant's response not accepted"); |
1818 | } else { |
1819 | logevent("Pageant failed to answer challenge"); |
dcbde236 |
1820 | sfree(ret); |
5c58ad2d |
1821 | } |
1822 | } else { |
1823 | logevent("No reply received from Pageant"); |
1824 | } |
1825 | } |
1826 | freebn(key.exponent); |
1827 | freebn(key.modulus); |
1828 | freebn(challenge); |
1829 | if (authed) |
1830 | break; |
1831 | } |
1832 | } |
1833 | if (authed) |
1834 | break; |
1835 | } |
7cca0d81 |
1836 | if (*cfg.keyfile && !tried_publickey) |
1837 | pwpkt_type = SSH1_CMSG_AUTH_RSA; |
fb09bf1c |
1838 | |
a52f067e |
1839 | if (pktin.type == SSH1_SMSG_FAILURE && |
1840 | cfg.try_tis_auth && |
1841 | (supported_auths_mask & (1<<SSH1_AUTH_TIS))) { |
1842 | pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE; |
1843 | logevent("Requested TIS authentication"); |
1844 | send_packet(SSH1_CMSG_AUTH_TIS, PKT_END); |
1845 | crWaitUntil(ispkt); |
1846 | if (pktin.type != SSH1_SMSG_AUTH_TIS_CHALLENGE) { |
1847 | logevent("TIS authentication declined"); |
1848 | if (flags & FLAG_INTERACTIVE) |
1408a877 |
1849 | c_write_str("TIS authentication refused.\r\n"); |
a52f067e |
1850 | } else { |
1851 | int challengelen = ((pktin.body[0] << 24) | |
1852 | (pktin.body[1] << 16) | |
1853 | (pktin.body[2] << 8) | |
1854 | (pktin.body[3])); |
1855 | logevent("Received TIS challenge"); |
1856 | if (challengelen > sizeof(prompt)-1) |
1857 | challengelen = sizeof(prompt)-1; /* prevent overrun */ |
1858 | memcpy(prompt, pktin.body+4, challengelen); |
1859 | prompt[challengelen] = '\0'; |
1860 | } |
1861 | } |
1862 | if (pktin.type == SSH1_SMSG_FAILURE && |
1863 | cfg.try_tis_auth && |
1864 | (supported_auths_mask & (1<<SSH1_AUTH_CCARD))) { |
1865 | pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE; |
1866 | logevent("Requested CryptoCard authentication"); |
1867 | send_packet(SSH1_CMSG_AUTH_CCARD, PKT_END); |
1868 | crWaitUntil(ispkt); |
1869 | if (pktin.type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
1870 | logevent("CryptoCard authentication declined"); |
1408a877 |
1871 | c_write_str("CryptoCard authentication refused.\r\n"); |
a52f067e |
1872 | } else { |
1873 | int challengelen = ((pktin.body[0] << 24) | |
1874 | (pktin.body[1] << 16) | |
1875 | (pktin.body[2] << 8) | |
1876 | (pktin.body[3])); |
1877 | logevent("Received CryptoCard challenge"); |
1878 | if (challengelen > sizeof(prompt)-1) |
1879 | challengelen = sizeof(prompt)-1; /* prevent overrun */ |
1880 | memcpy(prompt, pktin.body+4, challengelen); |
1881 | strncpy(prompt + challengelen, "\r\nResponse : ", |
1882 | sizeof(prompt)-challengelen); |
1883 | prompt[sizeof(prompt)-1] = '\0'; |
1884 | } |
1885 | } |
1886 | if (pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
1887 | sprintf(prompt, "%.90s@%.90s's password: ", |
b585ec46 |
1888 | username, savedhost); |
a52f067e |
1889 | } |
1890 | if (pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
1891 | char *comment = NULL; |
1892 | if (flags & FLAG_VERBOSE) |
1408a877 |
1893 | c_write_str("Trying public key authentication.\r\n"); |
a52f067e |
1894 | if (!rsakey_encrypted(cfg.keyfile, &comment)) { |
1895 | if (flags & FLAG_VERBOSE) |
1408a877 |
1896 | c_write_str("No passphrase required.\r\n"); |
a52f067e |
1897 | goto tryauth; |
1898 | } |
1899 | sprintf(prompt, "Passphrase for key \"%.100s\": ", comment); |
dcbde236 |
1900 | sfree(comment); |
a52f067e |
1901 | } |
1902 | |
fa17a66e |
1903 | if (ssh_get_line) { |
1904 | if (!ssh_get_line(prompt, password, sizeof(password), TRUE)) { |
85ee8208 |
1905 | /* |
fa17a66e |
1906 | * get_line failed to get a password (for example |
1907 | * because one was supplied on the command line |
1908 | * which has already failed to work). Terminate. |
85ee8208 |
1909 | */ |
1910 | logevent("No more passwords to try"); |
1911 | ssh_state = SSH_STATE_CLOSED; |
1912 | crReturn(1); |
1913 | } |
fb09bf1c |
1914 | } else { |
1408a877 |
1915 | c_write_str(prompt); |
7cca0d81 |
1916 | pos = 0; |
67779be7 |
1917 | ssh_send_ok = 1; |
7cca0d81 |
1918 | while (pos >= 0) { |
1919 | crWaitUntil(!ispkt); |
1920 | while (inlen--) switch (c = *in++) { |
1921 | case 10: case 13: |
1922 | password[pos] = 0; |
1923 | pos = -1; |
1924 | break; |
1925 | case 8: case 127: |
1926 | if (pos > 0) |
1927 | pos--; |
1928 | break; |
1929 | case 21: case 27: |
1930 | pos = 0; |
1931 | break; |
1932 | case 3: case 4: |
1933 | random_save_seed(); |
1934 | exit(0); |
1935 | break; |
1936 | default: |
1937 | if (((c >= ' ' && c <= '~') || |
1938 | ((unsigned char)c >= 160)) && pos < sizeof(password)) |
1939 | password[pos++] = c; |
1940 | break; |
1941 | } |
1942 | } |
1408a877 |
1943 | c_write_str("\r\n"); |
a52f067e |
1944 | } |
fb09bf1c |
1945 | |
7cca0d81 |
1946 | tryauth: |
1947 | if (pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
1948 | /* |
1949 | * Try public key authentication with the specified |
1950 | * key file. |
1951 | */ |
1952 | static struct RSAKey pubkey; |
1953 | static Bignum challenge, response; |
1954 | static int i; |
1955 | static unsigned char buffer[32]; |
1956 | |
1957 | tried_publickey = 1; |
65a22376 |
1958 | i = loadrsakey(cfg.keyfile, &pubkey, password); |
7cca0d81 |
1959 | if (i == 0) { |
1408a877 |
1960 | c_write_str("Couldn't load public key from "); |
1961 | c_write_str(cfg.keyfile); |
1962 | c_write_str(".\r\n"); |
7cca0d81 |
1963 | continue; /* go and try password */ |
1964 | } |
1965 | if (i == -1) { |
1408a877 |
1966 | c_write_str("Wrong passphrase.\r\n"); |
7cca0d81 |
1967 | tried_publickey = 0; |
1968 | continue; /* try again */ |
1969 | } |
1970 | |
1971 | /* |
1972 | * Send a public key attempt. |
1973 | */ |
1974 | send_packet(SSH1_CMSG_AUTH_RSA, |
1975 | PKT_BIGNUM, pubkey.modulus, PKT_END); |
1976 | |
1977 | crWaitUntil(ispkt); |
1978 | if (pktin.type == SSH1_SMSG_FAILURE) { |
1408a877 |
1979 | c_write_str("Server refused our public key.\r\n"); |
7cca0d81 |
1980 | continue; /* go and try password */ |
1981 | } |
8d5de777 |
1982 | if (pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
1983 | bombout(("Bizarre response to offer of public key")); |
1984 | crReturn(0); |
1985 | } |
7cca0d81 |
1986 | ssh1_read_bignum(pktin.body, &challenge); |
1987 | response = rsadecrypt(challenge, &pubkey); |
1988 | freebn(pubkey.private_exponent); /* burn the evidence */ |
1989 | |
3709bfe9 |
1990 | for (i = 0; i < 32; i++) { |
1991 | buffer[i] = bignum_byte(response, 31-i); |
7cca0d81 |
1992 | } |
1993 | |
1994 | MD5Init(&md5c); |
1995 | MD5Update(&md5c, buffer, 32); |
1996 | MD5Update(&md5c, session_id, 16); |
1997 | MD5Final(buffer, &md5c); |
1998 | |
1999 | send_packet(SSH1_CMSG_AUTH_RSA_RESPONSE, |
2000 | PKT_DATA, buffer, 16, PKT_END); |
2001 | |
2002 | crWaitUntil(ispkt); |
2003 | if (pktin.type == SSH1_SMSG_FAILURE) { |
4017be6d |
2004 | if (flags & FLAG_VERBOSE) |
1408a877 |
2005 | c_write_str("Failed to authenticate with our public key.\r\n"); |
7cca0d81 |
2006 | continue; /* go and try password */ |
2007 | } else if (pktin.type != SSH1_SMSG_SUCCESS) { |
8d5de777 |
2008 | bombout(("Bizarre response to RSA authentication response")); |
2009 | crReturn(0); |
7cca0d81 |
2010 | } |
2011 | |
2012 | break; /* we're through! */ |
2013 | } else { |
39065bed |
2014 | if (pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
2015 | /* |
2016 | * Defence against traffic analysis: we send a |
2017 | * whole bunch of packets containing strings of |
2018 | * different lengths. One of these strings is the |
2019 | * password, in a SSH1_CMSG_AUTH_PASSWORD packet. |
2020 | * The others are all random data in |
2021 | * SSH1_MSG_IGNORE packets. This way a passive |
2022 | * listener can't tell which is the password, and |
2023 | * hence can't deduce the password length. |
2024 | * |
2025 | * Anybody with a password length greater than 16 |
2026 | * bytes is going to have enough entropy in their |
2027 | * password that a listener won't find it _that_ |
2028 | * much help to know how long it is. So what we'll |
2029 | * do is: |
2030 | * |
2031 | * - if password length < 16, we send 15 packets |
2032 | * containing string lengths 1 through 15 |
2033 | * |
2034 | * - otherwise, we let N be the nearest multiple |
2035 | * of 8 below the password length, and send 8 |
2036 | * packets containing string lengths N through |
2037 | * N+7. This won't obscure the order of |
2038 | * magnitude of the password length, but it will |
2039 | * introduce a bit of extra uncertainty. |
7d503c31 |
2040 | * |
2041 | * A few servers (the old 1.2.18 through 1.2.22) |
2042 | * can't deal with SSH1_MSG_IGNORE. For these |
2043 | * servers, we need an alternative defence. We make |
2044 | * use of the fact that the password is interpreted |
2045 | * as a C string: so we can append a NUL, then some |
2046 | * random data. |
39065bed |
2047 | */ |
7d503c31 |
2048 | if (ssh_remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE) { |
2049 | char string[64]; |
2050 | char *s; |
2051 | int len; |
2052 | |
2053 | len = strlen(password); |
2054 | if (len < sizeof(string)) { |
2055 | s = string; |
2056 | strcpy(string, password); |
2057 | len++; /* cover the zero byte */ |
2058 | while (len < sizeof(string)) { |
2059 | string[len++] = (char)random_byte(); |
2060 | } |
2061 | } else { |
2062 | s = password; |
2063 | } |
2064 | send_packet(pwpkt_type, PKT_INT, len, |
2065 | PKT_DATA, s, len, PKT_END); |
39065bed |
2066 | } else { |
7d503c31 |
2067 | int bottom, top, pwlen, i; |
2068 | char *randomstr; |
2069 | |
2070 | pwlen = strlen(password); |
2071 | if (pwlen < 16) { |
98c84eb4 |
2072 | bottom = 0; /* zero length passwords are OK! :-) */ |
7d503c31 |
2073 | top = 15; |
2074 | } else { |
2075 | bottom = pwlen &~ 7; |
2076 | top = bottom + 7; |
2077 | } |
39065bed |
2078 | |
7d503c31 |
2079 | assert(pwlen >= bottom && pwlen <= top); |
39065bed |
2080 | |
7d503c31 |
2081 | randomstr = smalloc(top+1); |
39065bed |
2082 | |
7d503c31 |
2083 | for (i = bottom; i <= top; i++) { |
2084 | if (i == pwlen) |
2085 | defer_packet(pwpkt_type, PKT_STR, password, PKT_END); |
2086 | else { |
2087 | for (j = 0; j < i; j++) { |
2088 | do { |
2089 | randomstr[j] = random_byte(); |
2090 | } while (randomstr[j] == '\0'); |
2091 | } |
2092 | randomstr[i] = '\0'; |
2093 | defer_packet(SSH1_MSG_IGNORE, |
2094 | PKT_STR, randomstr, PKT_END); |
39065bed |
2095 | } |
39065bed |
2096 | } |
7d503c31 |
2097 | ssh_pkt_defersend(); |
39065bed |
2098 | } |
39065bed |
2099 | } else { |
2100 | send_packet(pwpkt_type, PKT_STR, password, PKT_END); |
2101 | } |
7cca0d81 |
2102 | } |
c5e9c988 |
2103 | logevent("Sent password"); |
374330e2 |
2104 | memset(password, 0, strlen(password)); |
fb09bf1c |
2105 | crWaitUntil(ispkt); |
e5574168 |
2106 | if (pktin.type == SSH1_SMSG_FAILURE) { |
4017be6d |
2107 | if (flags & FLAG_VERBOSE) |
1408a877 |
2108 | c_write_str("Access denied\r\n"); |
c5e9c988 |
2109 | logevent("Authentication refused"); |
e5574168 |
2110 | } else if (pktin.type == SSH1_MSG_DISCONNECT) { |
fb09bf1c |
2111 | logevent("Received disconnect request"); |
85ee8208 |
2112 | ssh_state = SSH_STATE_CLOSED; |
2113 | crReturn(1); |
e5574168 |
2114 | } else if (pktin.type != SSH1_SMSG_SUCCESS) { |
8d5de777 |
2115 | bombout(("Strange packet received, type %d", pktin.type)); |
2116 | crReturn(0); |
374330e2 |
2117 | } |
2118 | } |
2119 | |
c5e9c988 |
2120 | logevent("Authentication successful"); |
2121 | |
fb09bf1c |
2122 | crFinish(1); |
2123 | } |
2124 | |
9c964e85 |
2125 | void sshfwd_close(struct ssh_channel *c) { |
80e48603 |
2126 | if (c && !c->closes) { |
783415f8 |
2127 | if (ssh_version == 1) { |
2128 | send_packet(SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid, PKT_END); |
2129 | } else { |
2130 | ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
2131 | ssh2_pkt_adduint32(c->remoteid); |
2132 | ssh2_pkt_send(); |
2133 | } |
2134 | c->closes = 1; |
2135 | if (c->type == CHAN_X11) { |
2136 | c->u.x11.s = NULL; |
2137 | logevent("X11 connection terminated"); |
2138 | } |
2139 | } |
9c964e85 |
2140 | } |
2141 | |
2142 | void sshfwd_write(struct ssh_channel *c, char *buf, int len) { |
783415f8 |
2143 | if (ssh_version == 1) { |
2144 | send_packet(SSH1_MSG_CHANNEL_DATA, |
2145 | PKT_INT, c->remoteid, |
2146 | PKT_INT, len, |
2147 | PKT_DATA, buf, len, |
2148 | PKT_END); |
2149 | } else { |
2150 | ssh2_add_channel_data(c, buf, len); |
2151 | ssh2_try_send(c); |
2152 | } |
9c964e85 |
2153 | } |
2154 | |
e5574168 |
2155 | static void ssh1_protocol(unsigned char *in, int inlen, int ispkt) { |
fb09bf1c |
2156 | crBegin; |
2157 | |
2158 | random_init(); |
2159 | |
e5574168 |
2160 | while (!do_ssh1_login(in, inlen, ispkt)) { |
fb09bf1c |
2161 | crReturnV; |
85ee8208 |
2162 | } |
2163 | if (ssh_state == SSH_STATE_CLOSED) |
2164 | crReturnV; |
fb09bf1c |
2165 | |
979310f1 |
2166 | if (cfg.agentfwd && agent_exists()) { |
dacbd0e8 |
2167 | logevent("Requesting agent forwarding"); |
2168 | send_packet(SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END); |
2169 | do { crReturnV; } while (!ispkt); |
2170 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
8d5de777 |
2171 | bombout(("Protocol confusion")); |
2172 | crReturnV; |
dacbd0e8 |
2173 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
2174 | logevent("Agent forwarding refused"); |
db7d555c |
2175 | } else { |
dacbd0e8 |
2176 | logevent("Agent forwarding enabled"); |
db7d555c |
2177 | ssh_agentfwd_enabled = TRUE; |
2178 | } |
dacbd0e8 |
2179 | } |
2180 | |
9c964e85 |
2181 | if (cfg.x11_forward) { |
2182 | char proto[20], data[64]; |
2183 | logevent("Requesting X11 forwarding"); |
2184 | x11_invent_auth(proto, sizeof(proto), data, sizeof(data)); |
b96dc54c |
2185 | if (ssh1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) { |
2186 | send_packet(SSH1_CMSG_X11_REQUEST_FORWARDING, |
2187 | PKT_STR, proto, PKT_STR, data, |
2188 | PKT_INT, 0, |
2189 | PKT_END); |
2190 | } else { |
2191 | send_packet(SSH1_CMSG_X11_REQUEST_FORWARDING, |
2192 | PKT_STR, proto, PKT_STR, data, |
2193 | PKT_END); |
2194 | } |
9c964e85 |
2195 | do { crReturnV; } while (!ispkt); |
2196 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
2197 | bombout(("Protocol confusion")); |
2198 | crReturnV; |
2199 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
2200 | logevent("X11 forwarding refused"); |
2201 | } else { |
2202 | logevent("X11 forwarding enabled"); |
2203 | ssh_X11_fwd_enabled = TRUE; |
2204 | } |
2205 | } |
2206 | |
fef97f43 |
2207 | if (!cfg.nopty) { |
e5574168 |
2208 | send_packet(SSH1_CMSG_REQUEST_PTY, |
fb09bf1c |
2209 | PKT_STR, cfg.termtype, |
2210 | PKT_INT, rows, PKT_INT, cols, |
2211 | PKT_INT, 0, PKT_INT, 0, |
2212 | PKT_CHAR, 0, |
2213 | PKT_END); |
fef97f43 |
2214 | ssh_state = SSH_STATE_INTERMED; |
2215 | do { crReturnV; } while (!ispkt); |
e5574168 |
2216 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
8d5de777 |
2217 | bombout(("Protocol confusion")); |
2218 | crReturnV; |
e5574168 |
2219 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
1408a877 |
2220 | c_write_str("Server refused to allocate pty\r\n"); |
0965bee0 |
2221 | ssh_editing = ssh_echoing = 1; |
fef97f43 |
2222 | } |
c5e9c988 |
2223 | logevent("Allocated pty"); |
0965bee0 |
2224 | } else { |
2225 | ssh_editing = ssh_echoing = 1; |
374330e2 |
2226 | } |
2227 | |
4ba9b64b |
2228 | if (cfg.compression) { |
2229 | send_packet(SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END); |
2230 | do { crReturnV; } while (!ispkt); |
2231 | if (pktin.type != SSH1_SMSG_SUCCESS && pktin.type != SSH1_SMSG_FAILURE) { |
2232 | bombout(("Protocol confusion")); |
2233 | crReturnV; |
2234 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
1408a877 |
2235 | c_write_str("Server refused to compress\r\n"); |
4ba9b64b |
2236 | } |
2237 | logevent("Started compression"); |
2238 | ssh1_compressing = TRUE; |
2239 | zlib_compress_init(); |
2240 | zlib_decompress_init(); |
2241 | } |
2242 | |
96621a84 |
2243 | if (*cfg.remote_cmd_ptr) |
2244 | send_packet(SSH1_CMSG_EXEC_CMD, PKT_STR, cfg.remote_cmd_ptr, PKT_END); |
6abbf9e3 |
2245 | else |
2246 | send_packet(SSH1_CMSG_EXEC_SHELL, PKT_END); |
c5e9c988 |
2247 | logevent("Started session"); |
374330e2 |
2248 | |
2249 | ssh_state = SSH_STATE_SESSION; |
2250 | if (size_needed) |
2251 | ssh_size(); |
3687d221 |
2252 | if (eof_needed) |
2253 | ssh_special(TS_EOF); |
374330e2 |
2254 | |
0965bee0 |
2255 | ldisc_send(NULL, 0); /* cause ldisc to notice changes */ |
8ccc75b0 |
2256 | ssh_send_ok = 1; |
dacbd0e8 |
2257 | ssh_channels = newtree234(ssh_channelcmp); |
374330e2 |
2258 | while (1) { |
2259 | crReturnV; |
2260 | if (ispkt) { |
e5574168 |
2261 | if (pktin.type == SSH1_SMSG_STDOUT_DATA || |
2262 | pktin.type == SSH1_SMSG_STDERR_DATA) { |
fb09bf1c |
2263 | long len = GET_32BIT(pktin.body); |
fe50e814 |
2264 | from_backend(pktin.type == SSH1_SMSG_STDERR_DATA, |
2265 | pktin.body+4, len); |
e5574168 |
2266 | } else if (pktin.type == SSH1_MSG_DISCONNECT) { |
21248260 |
2267 | ssh_state = SSH_STATE_CLOSED; |
c5e9c988 |
2268 | logevent("Received disconnect request"); |
3257deae |
2269 | crReturnV; |
9c964e85 |
2270 | } else if (pktin.type == SSH1_SMSG_X11_OPEN) { |
2271 | /* Remote side is trying to open a channel to talk to our |
2272 | * X-Server. Give them back a local channel number. */ |
260f3dec |
2273 | struct ssh_channel *c; |
9c964e85 |
2274 | |
2275 | logevent("Received X11 connect request"); |
2276 | /* Refuse if X11 forwarding is disabled. */ |
2277 | if (!ssh_X11_fwd_enabled) { |
2278 | send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE, |
2279 | PKT_INT, GET_32BIT(pktin.body), |
2280 | PKT_END); |
2281 | logevent("Rejected X11 connect request"); |
2282 | } else { |
9c964e85 |
2283 | c = smalloc(sizeof(struct ssh_channel)); |
2284 | |
0c6c9a70 |
2285 | if ( x11_init(&c->u.x11.s, cfg.x11_display, c) != NULL ) { |
9c964e85 |
2286 | logevent("opening X11 forward connection failed"); |
2287 | sfree(c); |
2288 | send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE, |
2289 | PKT_INT, GET_32BIT(pktin.body), |
2290 | PKT_END); |
2291 | } else { |
2292 | logevent("opening X11 forward connection succeeded"); |
9c964e85 |
2293 | c->remoteid = GET_32BIT(pktin.body); |
d2371c81 |
2294 | c->localid = alloc_channel_id(); |
9c964e85 |
2295 | c->closes = 0; |
783415f8 |
2296 | c->type = CHAN_X11; /* identify channel type */ |
9c964e85 |
2297 | add234(ssh_channels, c); |
2298 | send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
2299 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
2300 | PKT_END); |
2301 | logevent("Opened X11 forward channel"); |
2302 | } |
2303 | } |
dacbd0e8 |
2304 | } else if (pktin.type == SSH1_SMSG_AGENT_OPEN) { |
2305 | /* Remote side is trying to open a channel to talk to our |
2306 | * agent. Give them back a local channel number. */ |
dacbd0e8 |
2307 | struct ssh_channel *c; |
db7d555c |
2308 | |
2309 | /* Refuse if agent forwarding is disabled. */ |
2310 | if (!ssh_agentfwd_enabled) { |
2311 | send_packet(SSH1_MSG_CHANNEL_OPEN_FAILURE, |
2312 | PKT_INT, GET_32BIT(pktin.body), |
2313 | PKT_END); |
2314 | } else { |
dcbde236 |
2315 | c = smalloc(sizeof(struct ssh_channel)); |
db7d555c |
2316 | c->remoteid = GET_32BIT(pktin.body); |
d2371c81 |
2317 | c->localid = alloc_channel_id(); |
db7d555c |
2318 | c->closes = 0; |
783415f8 |
2319 | c->type = CHAN_AGENT; /* identify channel type */ |
db7d555c |
2320 | c->u.a.lensofar = 0; |
2321 | add234(ssh_channels, c); |
2322 | send_packet(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
2323 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
2324 | PKT_END); |
2325 | } |
2326 | } else if (pktin.type == SSH1_MSG_CHANNEL_CLOSE || |
2327 | pktin.type == SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION) { |
dacbd0e8 |
2328 | /* Remote side closes a channel. */ |
d211621f |
2329 | unsigned i = GET_32BIT(pktin.body); |
dacbd0e8 |
2330 | struct ssh_channel *c; |
2331 | c = find234(ssh_channels, &i, ssh_channelfind); |
2332 | if (c) { |
2333 | int closetype; |
2334 | closetype = (pktin.type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2); |
824e9f94 |
2335 | if (!(c->closes & closetype)) |
2336 | send_packet(pktin.type, PKT_INT, c->remoteid, PKT_END); |
783415f8 |
2337 | if ((c->closes == 0) && (c->type == CHAN_X11)) { |
9c964e85 |
2338 | logevent("X11 connection closed"); |
2339 | assert(c->u.x11.s != NULL); |
2340 | x11_close(c->u.x11.s); |
2341 | c->u.x11.s = NULL; |
2342 | } |
dacbd0e8 |
2343 | c->closes |= closetype; |
2344 | if (c->closes == 3) { |
2345 | del234(ssh_channels, c); |
dcbde236 |
2346 | sfree(c); |
dacbd0e8 |
2347 | } |
2348 | } |
2349 | } else if (pktin.type == SSH1_MSG_CHANNEL_DATA) { |
2350 | /* Data sent down one of our channels. */ |
2351 | int i = GET_32BIT(pktin.body); |
2352 | int len = GET_32BIT(pktin.body+4); |
2353 | unsigned char *p = pktin.body+8; |
2354 | struct ssh_channel *c; |
2355 | c = find234(ssh_channels, &i, ssh_channelfind); |
2356 | if (c) { |
2357 | switch(c->type) { |
783415f8 |
2358 | case CHAN_X11: |
9c964e85 |
2359 | x11_send(c->u.x11.s, p, len); |
2360 | break; |
783415f8 |
2361 | case CHAN_AGENT: |
dacbd0e8 |
2362 | /* Data for an agent message. Buffer it. */ |
2363 | while (len > 0) { |
2364 | if (c->u.a.lensofar < 4) { |
2365 | int l = min(4 - c->u.a.lensofar, len); |
2366 | memcpy(c->u.a.msglen + c->u.a.lensofar, p, l); |
2367 | p += l; len -= l; c->u.a.lensofar += l; |
2368 | } |
2369 | if (c->u.a.lensofar == 4) { |
2370 | c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen); |
dcbde236 |
2371 | c->u.a.message = smalloc(c->u.a.totallen); |
dacbd0e8 |
2372 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
2373 | } |
2374 | if (c->u.a.lensofar >= 4 && len > 0) { |
2375 | int l = min(c->u.a.totallen - c->u.a.lensofar, len); |
2376 | memcpy(c->u.a.message + c->u.a.lensofar, p, l); |
2377 | p += l; len -= l; c->u.a.lensofar += l; |
2378 | } |
2379 | if (c->u.a.lensofar == c->u.a.totallen) { |
2380 | void *reply, *sentreply; |
2381 | int replylen; |
2382 | agent_query(c->u.a.message, c->u.a.totallen, |
2383 | &reply, &replylen); |
2384 | if (reply) |
2385 | sentreply = reply; |
2386 | else { |
2387 | /* Fake SSH_AGENT_FAILURE. */ |
2388 | sentreply = "\0\0\0\1\5"; |
2389 | replylen = 5; |
2390 | } |
2391 | send_packet(SSH1_MSG_CHANNEL_DATA, |
2392 | PKT_INT, c->remoteid, |
2393 | PKT_INT, replylen, |
2394 | PKT_DATA, sentreply, replylen, |
2395 | PKT_END); |
2396 | if (reply) |
dcbde236 |
2397 | sfree(reply); |
2398 | sfree(c->u.a.message); |
dacbd0e8 |
2399 | c->u.a.lensofar = 0; |
2400 | } |
2401 | } |
2402 | break; |
2403 | } |
2404 | } |
e5574168 |
2405 | } else if (pktin.type == SSH1_SMSG_SUCCESS) { |
972a41c8 |
2406 | /* may be from EXEC_SHELL on some servers */ |
e5574168 |
2407 | } else if (pktin.type == SSH1_SMSG_FAILURE) { |
972a41c8 |
2408 | /* may be from EXEC_SHELL on some servers |
374330e2 |
2409 | * if no pty is available or in other odd cases. Ignore */ |
e5574168 |
2410 | } else if (pktin.type == SSH1_SMSG_EXIT_STATUS) { |
2411 | send_packet(SSH1_CMSG_EXIT_CONFIRMATION, PKT_END); |
374330e2 |
2412 | } else { |
8d5de777 |
2413 | bombout(("Strange packet received: type %d", pktin.type)); |
2414 | crReturnV; |
374330e2 |
2415 | } |
2416 | } else { |
8df7a775 |
2417 | while (inlen > 0) { |
2418 | int len = min(inlen, 512); |
2419 | send_packet(SSH1_CMSG_STDIN_DATA, |
2420 | PKT_INT, len, PKT_DATA, in, len, PKT_END); |
2421 | in += len; |
2422 | inlen -= len; |
2423 | } |
374330e2 |
2424 | } |
2425 | } |
2426 | |
2427 | crFinishV; |
2428 | } |
2429 | |
2430 | /* |
e5574168 |
2431 | * Utility routine for decoding comma-separated strings in KEXINIT. |
2432 | */ |
75cab814 |
2433 | static int in_commasep_string(char *needle, char *haystack, int haylen) { |
e5574168 |
2434 | int needlen = strlen(needle); |
2435 | while (1) { |
2436 | /* |
2437 | * Is it at the start of the string? |
2438 | */ |
2439 | if (haylen >= needlen && /* haystack is long enough */ |
2440 | !memcmp(needle, haystack, needlen) && /* initial match */ |
2441 | (haylen == needlen || haystack[needlen] == ',') |
2442 | /* either , or EOS follows */ |
2443 | ) |
2444 | return 1; |
2445 | /* |
2446 | * If not, search for the next comma and resume after that. |
2447 | * If no comma found, terminate. |
2448 | */ |
2449 | while (haylen > 0 && *haystack != ',') |
2450 | haylen--, haystack++; |
2451 | if (haylen == 0) |
2452 | return 0; |
2453 | haylen--, haystack++; /* skip over comma itself */ |
2454 | } |
2455 | } |
2456 | |
2457 | /* |
d39f364a |
2458 | * SSH2 key creation method. |
2459 | */ |
5e0d7cb8 |
2460 | static void ssh2_mkkey(Bignum K, char *H, char *sessid, char chr, char *keyspace) { |
d39f364a |
2461 | SHA_State s; |
2462 | /* First 20 bytes. */ |
2463 | SHA_Init(&s); |
2464 | sha_mpint(&s, K); |
2465 | SHA_Bytes(&s, H, 20); |
2466 | SHA_Bytes(&s, &chr, 1); |
5e0d7cb8 |
2467 | SHA_Bytes(&s, sessid, 20); |
d39f364a |
2468 | SHA_Final(&s, keyspace); |
2469 | /* Next 20 bytes. */ |
2470 | SHA_Init(&s); |
2471 | sha_mpint(&s, K); |
2472 | SHA_Bytes(&s, H, 20); |
2473 | SHA_Bytes(&s, keyspace, 20); |
2474 | SHA_Final(&s, keyspace+20); |
2475 | } |
2476 | |
2477 | /* |
7cca0d81 |
2478 | * Handle the SSH2 transport layer. |
e5574168 |
2479 | */ |
7cca0d81 |
2480 | static int do_ssh2_transport(unsigned char *in, int inlen, int ispkt) |
e5574168 |
2481 | { |
7bd5a860 |
2482 | static int i, j, len, nbits, pbits; |
e5574168 |
2483 | static char *str; |
a92dd380 |
2484 | static Bignum p, g, e, f, K; |
2485 | static int kex_init_value, kex_reply_value; |
7591b9ff |
2486 | static const struct ssh_mac **maclist; |
8b2715b2 |
2487 | static int nmacs; |
0a3f1d48 |
2488 | static const struct ssh2_cipher *cscipher_tobe = NULL; |
2489 | static const struct ssh2_cipher *sccipher_tobe = NULL; |
57476f6b |
2490 | static const struct ssh_mac *csmac_tobe = NULL; |
2491 | static const struct ssh_mac *scmac_tobe = NULL; |
2492 | static const struct ssh_compress *cscomp_tobe = NULL; |
2493 | static const struct ssh_compress *sccomp_tobe = NULL; |
d5859615 |
2494 | static char *hostkeydata, *sigdata, *keystr, *fingerprint; |
e5574168 |
2495 | static int hostkeylen, siglen; |
e055a386 |
2496 | static void *hkey; /* actual host key */ |
e5574168 |
2497 | static unsigned char exchange_hash[20]; |
d39f364a |
2498 | static unsigned char keyspace[40]; |
0a3f1d48 |
2499 | static const struct ssh2_ciphers *preferred_cipher; |
4ba9b64b |
2500 | static const struct ssh_compress *preferred_comp; |
0db56f73 |
2501 | static int first_kex; |
e5574168 |
2502 | |
2503 | crBegin; |
7cca0d81 |
2504 | random_init(); |
0db56f73 |
2505 | first_kex = 1; |
e5574168 |
2506 | |
5e8358ad |
2507 | /* |
4ba9b64b |
2508 | * Set up the preferred cipher and compression. |
5e8358ad |
2509 | */ |
2510 | if (cfg.cipher == CIPHER_BLOWFISH) { |
0a3f1d48 |
2511 | preferred_cipher = &ssh2_blowfish; |
5e8358ad |
2512 | } else if (cfg.cipher == CIPHER_DES) { |
2513 | logevent("Single DES not supported in SSH2; using 3DES"); |
0a3f1d48 |
2514 | preferred_cipher = &ssh2_3des; |
5e8358ad |
2515 | } else if (cfg.cipher == CIPHER_3DES) { |
0a3f1d48 |
2516 | preferred_cipher = &ssh2_3des; |
2517 | } else if (cfg.cipher == CIPHER_AES) { |
2518 | preferred_cipher = &ssh2_aes; |
5e8358ad |
2519 | } else { |
2520 | /* Shouldn't happen, but we do want to initialise to _something_. */ |
0a3f1d48 |
2521 | preferred_cipher = &ssh2_3des; |
5e8358ad |
2522 | } |
4ba9b64b |
2523 | if (cfg.compression) |
2524 | preferred_comp = &ssh_zlib; |
2525 | else |
2526 | preferred_comp = &ssh_comp_none; |
5e8358ad |
2527 | |
7591b9ff |
2528 | /* |
2529 | * Be prepared to work around the buggy MAC problem. |
2530 | */ |
7d503c31 |
2531 | if (cfg.buggymac || (ssh_remote_bugs & BUG_SSH2_HMAC)) |
8b2715b2 |
2532 | maclist = buggymacs, nmacs = lenof(buggymacs); |
7591b9ff |
2533 | else |
8b2715b2 |
2534 | maclist = macs, nmacs = lenof(macs); |
7591b9ff |
2535 | |
7cca0d81 |
2536 | begin_key_exchange: |
e5574168 |
2537 | /* |
2538 | * Construct and send our key exchange packet. |
2539 | */ |
2540 | ssh2_pkt_init(SSH2_MSG_KEXINIT); |
2541 | for (i = 0; i < 16; i++) |
2542 | ssh2_pkt_addbyte((unsigned char)random_byte()); |
2543 | /* List key exchange algorithms. */ |
2544 | ssh2_pkt_addstring_start(); |
2545 | for (i = 0; i < lenof(kex_algs); i++) { |
2546 | ssh2_pkt_addstring_str(kex_algs[i]->name); |
2547 | if (i < lenof(kex_algs)-1) |
2548 | ssh2_pkt_addstring_str(","); |
2549 | } |
2550 | /* List server host key algorithms. */ |
2551 | ssh2_pkt_addstring_start(); |
2552 | for (i = 0; i < lenof(hostkey_algs); i++) { |
2553 | ssh2_pkt_addstring_str(hostkey_algs[i]->name); |
2554 | if (i < lenof(hostkey_algs)-1) |
2555 | ssh2_pkt_addstring_str(","); |
2556 | } |
2557 | /* List client->server encryption algorithms. */ |
2558 | ssh2_pkt_addstring_start(); |
729e54b4 |
2559 | for (i = 0; i < lenof(ciphers)+1; i++) { |
0a3f1d48 |
2560 | const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1]; |
2561 | for (j = 0; j < c->nciphers; j++) { |
2562 | ssh2_pkt_addstring_str(c->list[j]->name); |
2563 | if (i < lenof(ciphers) || j < c->nciphers-1) |
2564 | ssh2_pkt_addstring_str(","); |
2565 | } |
e5574168 |
2566 | } |
2567 | /* List server->client encryption algorithms. */ |
2568 | ssh2_pkt_addstring_start(); |
729e54b4 |
2569 | for (i = 0; i < lenof(ciphers)+1; i++) { |
0a3f1d48 |
2570 | const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1]; |
2571 | for (j = 0; j < c->nciphers; j++) { |
2572 | ssh2_pkt_addstring_str(c->list[j]->name); |
2573 | if (i < lenof(ciphers) || j < c->nciphers-1) |
2574 | ssh2_pkt_addstring_str(","); |
2575 | } |
e5574168 |
2576 | } |
2577 | /* List client->server MAC algorithms. */ |
2578 | ssh2_pkt_addstring_start(); |
8b2715b2 |
2579 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2580 | ssh2_pkt_addstring_str(maclist[i]->name); |
8b2715b2 |
2581 | if (i < nmacs-1) |
e5574168 |
2582 | ssh2_pkt_addstring_str(","); |
2583 | } |
2584 | /* List server->client MAC algorithms. */ |
2585 | ssh2_pkt_addstring_start(); |
8b2715b2 |
2586 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2587 | ssh2_pkt_addstring_str(maclist[i]->name); |
8b2715b2 |
2588 | if (i < nmacs-1) |
e5574168 |
2589 | ssh2_pkt_addstring_str(","); |
2590 | } |
2591 | /* List client->server compression algorithms. */ |
2592 | ssh2_pkt_addstring_start(); |
4ba9b64b |
2593 | for (i = 0; i < lenof(compressions)+1; i++) { |
2594 | const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1]; |
2595 | ssh2_pkt_addstring_str(c->name); |
2596 | if (i < lenof(compressions)) |
e5574168 |
2597 | ssh2_pkt_addstring_str(","); |
2598 | } |
2599 | /* List server->client compression algorithms. */ |
2600 | ssh2_pkt_addstring_start(); |
4ba9b64b |
2601 | for (i = 0; i < lenof(compressions)+1; i++) { |
2602 | const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1]; |
2603 | ssh2_pkt_addstring_str(c->name); |
2604 | if (i < lenof(compressions)) |
e5574168 |
2605 | ssh2_pkt_addstring_str(","); |
2606 | } |
2607 | /* List client->server languages. Empty list. */ |
2608 | ssh2_pkt_addstring_start(); |
2609 | /* List server->client languages. Empty list. */ |
2610 | ssh2_pkt_addstring_start(); |
2611 | /* First KEX packet does _not_ follow, because we're not that brave. */ |
2612 | ssh2_pkt_addbool(FALSE); |
2613 | /* Reserved. */ |
2614 | ssh2_pkt_adduint32(0); |
0db56f73 |
2615 | |
2616 | exhash = exhashbase; |
e5574168 |
2617 | sha_string(&exhash, pktout.data+5, pktout.length-5); |
0db56f73 |
2618 | |
e5574168 |
2619 | ssh2_pkt_send(); |
2620 | |
2621 | if (!ispkt) crWaitUntil(ispkt); |
2622 | sha_string(&exhash, pktin.data+5, pktin.length-5); |
2623 | |
2624 | /* |
2625 | * Now examine the other side's KEXINIT to see what we're up |
2626 | * to. |
2627 | */ |
7cca0d81 |
2628 | if (pktin.type != SSH2_MSG_KEXINIT) { |
8d5de777 |
2629 | bombout(("expected key exchange packet from server")); |
2630 | crReturn(0); |
7cca0d81 |
2631 | } |
e5574168 |
2632 | kex = NULL; hostkey = NULL; cscipher_tobe = NULL; sccipher_tobe = NULL; |
2633 | csmac_tobe = NULL; scmac_tobe = NULL; cscomp_tobe = NULL; sccomp_tobe = NULL; |
2634 | pktin.savedpos += 16; /* skip garbage cookie */ |
2635 | ssh2_pkt_getstring(&str, &len); /* key exchange algorithms */ |
2636 | for (i = 0; i < lenof(kex_algs); i++) { |
2637 | if (in_commasep_string(kex_algs[i]->name, str, len)) { |
2638 | kex = kex_algs[i]; |
2639 | break; |
2640 | } |
2641 | } |
2642 | ssh2_pkt_getstring(&str, &len); /* host key algorithms */ |
2643 | for (i = 0; i < lenof(hostkey_algs); i++) { |
2644 | if (in_commasep_string(hostkey_algs[i]->name, str, len)) { |
2645 | hostkey = hostkey_algs[i]; |
2646 | break; |
2647 | } |
2648 | } |
2649 | ssh2_pkt_getstring(&str, &len); /* client->server cipher */ |
729e54b4 |
2650 | for (i = 0; i < lenof(ciphers)+1; i++) { |
0a3f1d48 |
2651 | const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1]; |
2652 | for (j = 0; j < c->nciphers; j++) { |
2653 | if (in_commasep_string(c->list[j]->name, str, len)) { |
2654 | cscipher_tobe = c->list[j]; |
2655 | break; |
2656 | } |
e5574168 |
2657 | } |
0a3f1d48 |
2658 | if (cscipher_tobe) |
2659 | break; |
e5574168 |
2660 | } |
2661 | ssh2_pkt_getstring(&str, &len); /* server->client cipher */ |
729e54b4 |
2662 | for (i = 0; i < lenof(ciphers)+1; i++) { |
0a3f1d48 |
2663 | const struct ssh2_ciphers *c = i==0 ? preferred_cipher : ciphers[i-1]; |
2664 | for (j = 0; j < c->nciphers; j++) { |
2665 | if (in_commasep_string(c->list[j]->name, str, len)) { |
2666 | sccipher_tobe = c->list[j]; |
2667 | break; |
2668 | } |
e5574168 |
2669 | } |
0a3f1d48 |
2670 | if (sccipher_tobe) |
2671 | break; |
e5574168 |
2672 | } |
2673 | ssh2_pkt_getstring(&str, &len); /* client->server mac */ |
8b2715b2 |
2674 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2675 | if (in_commasep_string(maclist[i]->name, str, len)) { |
2676 | csmac_tobe = maclist[i]; |
e5574168 |
2677 | break; |
2678 | } |
2679 | } |
2680 | ssh2_pkt_getstring(&str, &len); /* server->client mac */ |
8b2715b2 |
2681 | for (i = 0; i < nmacs; i++) { |
7591b9ff |
2682 | if (in_commasep_string(maclist[i]->name, str, len)) { |
2683 | scmac_tobe = maclist[i]; |
e5574168 |
2684 | break; |
2685 | } |
2686 | } |
2687 | ssh2_pkt_getstring(&str, &len); /* client->server compression */ |
4ba9b64b |
2688 | for (i = 0; i < lenof(compressions)+1; i++) { |
2689 | const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1]; |
2690 | if (in_commasep_string(c->name, str, len)) { |
2691 | cscomp_tobe = c; |
e5574168 |
2692 | break; |
2693 | } |
2694 | } |
2695 | ssh2_pkt_getstring(&str, &len); /* server->client compression */ |
4ba9b64b |
2696 | for (i = 0; i < lenof(compressions)+1; i++) { |
2697 | const struct ssh_compress *c = i==0 ? preferred_comp : compressions[i-1]; |
2698 | if (in_commasep_string(c->name, str, len)) { |
2699 | sccomp_tobe = c; |
e5574168 |
2700 | break; |
2701 | } |
2702 | } |
e5574168 |
2703 | |
2704 | /* |
7bd5a860 |
2705 | * Work out the number of bits of key we will need from the key |
2706 | * exchange. We start with the maximum key length of either |
2707 | * cipher... |
2708 | */ |
2709 | { |
2710 | int csbits, scbits; |
2711 | |
2712 | csbits = cscipher_tobe->keylen; |
2713 | scbits = sccipher_tobe->keylen; |
2714 | nbits = (csbits > scbits ? csbits : scbits); |
2715 | } |
2716 | /* The keys only have 160-bit entropy, since they're based on |
2717 | * a SHA-1 hash. So cap the key size at 160 bits. */ |
2718 | if (nbits > 160) nbits = 160; |
2719 | |
2720 | /* |
a92dd380 |
2721 | * If we're doing Diffie-Hellman group exchange, start by |
2722 | * requesting a group. |
e5574168 |
2723 | */ |
a92dd380 |
2724 | if (kex == &ssh_diffiehellman_gex) { |
a92dd380 |
2725 | logevent("Doing Diffie-Hellman group exchange"); |
2726 | /* |
7bd5a860 |
2727 | * Work out how big a DH group we will need to allow that |
2728 | * much data. |
2729 | */ |
2730 | pbits = 512 << ((nbits-1) / 64); |
a92dd380 |
2731 | ssh2_pkt_init(SSH2_MSG_KEX_DH_GEX_REQUEST); |
7bd5a860 |
2732 | ssh2_pkt_adduint32(pbits); |
a92dd380 |
2733 | ssh2_pkt_send(); |
2734 | |
2735 | crWaitUntil(ispkt); |
2736 | if (pktin.type != SSH2_MSG_KEX_DH_GEX_GROUP) { |
2737 | bombout(("expected key exchange group packet from server")); |
2738 | crReturn(0); |
2739 | } |
2740 | p = ssh2_pkt_getmp(); |
2741 | g = ssh2_pkt_getmp(); |
2742 | dh_setup_group(p, g); |
2743 | kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT; |
2744 | kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY; |
2745 | } else { |
2746 | dh_setup_group1(); |
2747 | kex_init_value = SSH2_MSG_KEXDH_INIT; |
2748 | kex_reply_value = SSH2_MSG_KEXDH_REPLY; |
8d5de777 |
2749 | } |
e5574168 |
2750 | |
a92dd380 |
2751 | logevent("Doing Diffie-Hellman key exchange"); |
e5574168 |
2752 | /* |
a92dd380 |
2753 | * Now generate and send e for Diffie-Hellman. |
e5574168 |
2754 | */ |
7bd5a860 |
2755 | e = dh_create_e(nbits*2); |
a92dd380 |
2756 | ssh2_pkt_init(kex_init_value); |
e5574168 |
2757 | ssh2_pkt_addmp(e); |
2758 | ssh2_pkt_send(); |
2759 | |
2760 | crWaitUntil(ispkt); |
a92dd380 |
2761 | if (pktin.type != kex_reply_value) { |
2762 | bombout(("expected key exchange reply packet from server")); |
8d5de777 |
2763 | crReturn(0); |
7cca0d81 |
2764 | } |
e5574168 |
2765 | ssh2_pkt_getstring(&hostkeydata, &hostkeylen); |
2766 | f = ssh2_pkt_getmp(); |
e5574168 |
2767 | ssh2_pkt_getstring(&sigdata, &siglen); |
2768 | |
2769 | K = dh_find_K(f); |
e5574168 |
2770 | |
2771 | sha_string(&exhash, hostkeydata, hostkeylen); |
a92dd380 |
2772 | if (kex == &ssh_diffiehellman_gex) { |
7bd5a860 |
2773 | sha_uint32(&exhash, pbits); |
a92dd380 |
2774 | sha_mpint(&exhash, p); |
2775 | sha_mpint(&exhash, g); |
2776 | } |
e5574168 |
2777 | sha_mpint(&exhash, e); |
2778 | sha_mpint(&exhash, f); |
2779 | sha_mpint(&exhash, K); |
2780 | SHA_Final(&exhash, exchange_hash); |
2781 | |
3709bfe9 |
2782 | dh_cleanup(); |
2783 | |
7cca0d81 |
2784 | #if 0 |
e5574168 |
2785 | debug(("Exchange hash is:\r\n")); |
2786 | for (i = 0; i < 20; i++) |
2787 | debug((" %02x", exchange_hash[i])); |
2788 | debug(("\r\n")); |
7cca0d81 |
2789 | #endif |
2790 | |
e055a386 |
2791 | hkey = hostkey->newkey(hostkeydata, hostkeylen); |
2792 | if (!hostkey->verifysig(hkey, sigdata, siglen, exchange_hash, 20)) { |
8d5de777 |
2793 | bombout(("Server failed host key check")); |
2794 | crReturn(0); |
2795 | } |
e5574168 |
2796 | |
2797 | /* |
7cca0d81 |
2798 | * Expect SSH2_MSG_NEWKEYS from server. |
d39f364a |
2799 | */ |
7cca0d81 |
2800 | crWaitUntil(ispkt); |
8d5de777 |
2801 | if (pktin.type != SSH2_MSG_NEWKEYS) { |
2802 | bombout(("expected new-keys packet from server")); |
2803 | crReturn(0); |
2804 | } |
d39f364a |
2805 | |
2806 | /* |
7cca0d81 |
2807 | * Authenticate remote host: verify host key. (We've already |
2808 | * checked the signature of the exchange hash.) |
e5574168 |
2809 | */ |
e055a386 |
2810 | keystr = hostkey->fmtkey(hkey); |
2811 | fingerprint = hostkey->fingerprint(hkey); |
d4857987 |
2812 | verify_ssh_host_key(savedhost, savedport, hostkey->keytype, |
2813 | keystr, fingerprint); |
a92dd380 |
2814 | if (first_kex) { /* don't bother logging this in rekeys */ |
5e0d7cb8 |
2815 | logevent("Host key fingerprint is:"); |
2816 | logevent(fingerprint); |
2817 | } |
dcbde236 |
2818 | sfree(fingerprint); |
2819 | sfree(keystr); |
e055a386 |
2820 | hostkey->freekey(hkey); |
d39f364a |
2821 | |
2822 | /* |
7cca0d81 |
2823 | * Send SSH2_MSG_NEWKEYS. |
d39f364a |
2824 | */ |
2825 | ssh2_pkt_init(SSH2_MSG_NEWKEYS); |
2826 | ssh2_pkt_send(); |
d39f364a |
2827 | |
2828 | /* |
2829 | * Create and initialise session keys. |
2830 | */ |
2831 | cscipher = cscipher_tobe; |
2832 | sccipher = sccipher_tobe; |
2833 | csmac = csmac_tobe; |
2834 | scmac = scmac_tobe; |
2835 | cscomp = cscomp_tobe; |
2836 | sccomp = sccomp_tobe; |
4ba9b64b |
2837 | cscomp->compress_init(); |
2838 | sccomp->decompress_init(); |
d39f364a |
2839 | /* |
5e0d7cb8 |
2840 | * Set IVs after keys. Here we use the exchange hash from the |
2841 | * _first_ key exchange. |
d39f364a |
2842 | */ |
5e0d7cb8 |
2843 | if (first_kex) |
65a22376 |
2844 | memcpy(ssh2_session_id, exchange_hash, sizeof(exchange_hash)); |
2845 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'C', keyspace); |
5e0d7cb8 |
2846 | cscipher->setcskey(keyspace); |
65a22376 |
2847 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'D', keyspace); |
5e0d7cb8 |
2848 | sccipher->setsckey(keyspace); |
65a22376 |
2849 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'A', keyspace); |
5e0d7cb8 |
2850 | cscipher->setcsiv(keyspace); |
65a22376 |
2851 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'B', keyspace); |
5e0d7cb8 |
2852 | sccipher->setsciv(keyspace); |
65a22376 |
2853 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'E', keyspace); |
5e0d7cb8 |
2854 | csmac->setcskey(keyspace); |
65a22376 |
2855 | ssh2_mkkey(K, exchange_hash, ssh2_session_id, 'F', keyspace); |
5e0d7cb8 |
2856 | scmac->setsckey(keyspace); |
d39f364a |
2857 | |
033b4cef |
2858 | /* |
0db56f73 |
2859 | * If this is the first key exchange phase, we must pass the |
2860 | * SSH2_MSG_NEWKEYS packet to the next layer, not because it |
2861 | * wants to see it but because it will need time to initialise |
2862 | * itself before it sees an actual packet. In subsequent key |
2863 | * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because |
2864 | * it would only confuse the layer above. |
2865 | */ |
2866 | if (!first_kex) { |
2867 | crReturn(0); |
2868 | } |
2869 | first_kex = 0; |
2870 | |
2871 | /* |
7cca0d81 |
2872 | * Now we're encrypting. Begin returning 1 to the protocol main |
2873 | * function so that other things can run on top of the |
2874 | * transport. If we ever see a KEXINIT, we must go back to the |
2875 | * start. |
033b4cef |
2876 | */ |
e96adf72 |
2877 | while (!(ispkt && pktin.type == SSH2_MSG_KEXINIT)) { |
7cca0d81 |
2878 | crReturn(1); |
e96adf72 |
2879 | } |
5e0d7cb8 |
2880 | logevent("Server initiated key re-exchange"); |
7cca0d81 |
2881 | goto begin_key_exchange; |
e5574168 |
2882 | |
2883 | crFinish(1); |
2884 | } |
2885 | |
7cca0d81 |
2886 | /* |
783415f8 |
2887 | * Add data to an SSH2 channel output buffer. |
2888 | */ |
2889 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len) { |
2890 | if (c->v2.outbufsize < |
2891 | c->v2.outbuflen + len) { |
2892 | c->v2.outbufsize = |
2893 | c->v2.outbuflen + len + 1024; |
2894 | c->v2.outbuffer = srealloc(c->v2.outbuffer, |
2895 | c->v2.outbufsize); |
2896 | } |
2897 | memcpy(c->v2.outbuffer + c->v2.outbuflen, |
2898 | buf, len); |
2899 | c->v2.outbuflen += len; |
2900 | } |
2901 | |
2902 | /* |
2903 | * Attempt to send data on an SSH2 channel. |
2904 | */ |
2905 | static void ssh2_try_send(struct ssh_channel *c) { |
2906 | while (c->v2.remwindow > 0 && |
2907 | c->v2.outbuflen > 0) { |
2908 | unsigned len = c->v2.remwindow; |
2909 | if (len > c->v2.outbuflen) |
2910 | len = c->v2.outbuflen; |
2911 | if (len > c->v2.remmaxpkt) |
2912 | len = c->v2.remmaxpkt; |
2913 | ssh2_pkt_init(SSH2_MSG_CHANNEL_DATA); |
2914 | ssh2_pkt_adduint32(c->remoteid); |
2915 | ssh2_pkt_addstring_start(); |
2916 | ssh2_pkt_addstring_data(c->v2.outbuffer, len); |
2917 | ssh2_pkt_send(); |
2918 | c->v2.outbuflen -= len; |
2919 | memmove(c->v2.outbuffer, c->v2.outbuffer+len, |
2920 | c->v2.outbuflen); |
2921 | c->v2.remwindow -= len; |
2922 | } |
2923 | } |
2924 | |
2925 | /* |
7cca0d81 |
2926 | * Handle the SSH2 userauth and connection layers. |
2927 | */ |
2928 | static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt) |
2929 | { |
7cca0d81 |
2930 | static unsigned long remote_winsize; |
2931 | static unsigned long remote_maxpkt; |
65a22376 |
2932 | static enum { |
2933 | AUTH_INVALID, AUTH_PUBLICKEY_AGENT, AUTH_PUBLICKEY_FILE, AUTH_PASSWORD |
2934 | } method; |
1408a877 |
2935 | static enum { |
2936 | AUTH_TYPE_NONE, |
2937 | AUTH_TYPE_PUBLICKEY, |
2938 | AUTH_TYPE_PUBLICKEY_OFFER_LOUD, |
2939 | AUTH_TYPE_PUBLICKEY_OFFER_QUIET, |
2940 | AUTH_TYPE_PASSWORD |
2941 | } type; |
1983e559 |
2942 | static int gotit, need_pw, can_pubkey, can_passwd; |
2943 | static int tried_pubkey_config, tried_agent; |
1408a877 |
2944 | static int we_are_in; |
65a22376 |
2945 | static char username[100]; |
2946 | static char pwprompt[200]; |
2947 | static char password[100]; |
7cca0d81 |
2948 | |
e5574168 |
2949 | crBegin; |
2950 | |
7cca0d81 |
2951 | /* |
2952 | * Request userauth protocol, and await a response to it. |
2953 | */ |
2954 | ssh2_pkt_init(SSH2_MSG_SERVICE_REQUEST); |
2955 | ssh2_pkt_addstring("ssh-userauth"); |
2956 | ssh2_pkt_send(); |
2957 | crWaitUntilV(ispkt); |
8d5de777 |
2958 | if (pktin.type != SSH2_MSG_SERVICE_ACCEPT) { |
2959 | bombout(("Server refused user authentication protocol")); |
2960 | crReturnV; |
2961 | } |
7cca0d81 |
2962 | |
2963 | /* |
1408a877 |
2964 | * We repeat this whole loop, including the username prompt, |
2965 | * until we manage a successful authentication. If the user |
2966 | * types the wrong _password_, they are sent back to the |
2967 | * beginning to try another username. (If they specify a |
2968 | * username in the config, they are never asked, even if they |
2969 | * do give a wrong password.) |
2970 | * |
2971 | * I think this best serves the needs of |
2972 | * |
2973 | * - the people who have no configuration, no keys, and just |
2974 | * want to try repeated (username,password) pairs until they |
2975 | * type both correctly |
2976 | * |
2977 | * - people who have keys and configuration but occasionally |
2978 | * need to fall back to passwords |
2979 | * |
2980 | * - people with a key held in Pageant, who might not have |
2981 | * logged in to a particular machine before; so they want to |
2982 | * type a username, and then _either_ their key will be |
2983 | * accepted, _or_ they will type a password. If they mistype |
2984 | * the username they will want to be able to get back and |
2985 | * retype it! |
7cca0d81 |
2986 | */ |
1408a877 |
2987 | do { |
2988 | static int pos; |
7cca0d81 |
2989 | static char c; |
e5574168 |
2990 | |
1408a877 |
2991 | /* |
2992 | * Get a username. |
2993 | */ |
2994 | pos = 0; |
67779be7 |
2995 | if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { |
fa17a66e |
2996 | if (ssh_get_line) { |
2997 | if (!ssh_get_line("login as: ", |
2998 | username, sizeof(username), FALSE)) { |
2999 | /* |
3000 | * get_line failed to get a username. |
3001 | * Terminate. |
3002 | */ |
3003 | logevent("No username provided. Abandoning session."); |
3004 | ssh_state = SSH_STATE_CLOSED; |
dae2b91f |
3005 | crReturnV; |
fa17a66e |
3006 | } |
3007 | } else { |
3008 | c_write_str("login as: "); |
3009 | ssh_send_ok = 1; |
3010 | while (pos >= 0) { |
3011 | crWaitUntilV(!ispkt); |
3012 | while (inlen--) switch (c = *in++) { |
3013 | case 10: case 13: |
3014 | username[pos] = 0; |
3015 | pos = -1; |
3016 | break; |
3017 | case 8: case 127: |
3018 | if (pos > 0) { |
3019 | c_write_str("\b \b"); |
3020 | pos--; |
3021 | } |
3022 | break; |
3023 | case 21: case 27: |
3024 | while (pos > 0) { |
3025 | c_write_str("\b \b"); |
3026 | pos--; |
3027 | } |
3028 | break; |
3029 | case 3: case 4: |
3030 | random_save_seed(); |
3031 | exit(0); |
3032 | break; |
3033 | default: |
3034 | if (((c >= ' ' && c <= '~') || |
3035 | ((unsigned char)c >= 160)) && pos < 40) { |
3036 | username[pos++] = c; |
3037 | c_write(&c, 1); |
3038 | } |
3039 | break; |
3040 | } |
3041 | } |
3042 | } |
1408a877 |
3043 | c_write_str("\r\n"); |
7cca0d81 |
3044 | username[strcspn(username, "\n\r")] = '\0'; |
3045 | } else { |
3046 | char stuff[200]; |
3047 | strncpy(username, cfg.username, 99); |
3048 | username[99] = '\0'; |
65a22376 |
3049 | if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { |
7cca0d81 |
3050 | sprintf(stuff, "Using username \"%s\".\r\n", username); |
1408a877 |
3051 | c_write_str(stuff); |
7cca0d81 |
3052 | } |
3053 | } |
3054 | |
65a22376 |
3055 | /* |
1408a877 |
3056 | * Send an authentication request using method "none": (a) |
3057 | * just in case it succeeds, and (b) so that we know what |
3058 | * authentication methods we can usefully try next. |
65a22376 |
3059 | */ |
1408a877 |
3060 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3061 | ssh2_pkt_addstring(username); |
3062 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
3063 | ssh2_pkt_addstring("none"); /* method */ |
3064 | ssh2_pkt_send(); |
3065 | type = AUTH_TYPE_NONE; |
3066 | gotit = FALSE; |
3067 | we_are_in = FALSE; |
7cca0d81 |
3068 | |
1408a877 |
3069 | tried_pubkey_config = FALSE; |
1983e559 |
3070 | tried_agent = FALSE; |
65a22376 |
3071 | |
1408a877 |
3072 | while (1) { |
3073 | /* |
3074 | * Wait for the result of the last authentication request. |
3075 | */ |
3076 | if (!gotit) |
3077 | crWaitUntilV(ispkt); |
3078 | while (pktin.type == SSH2_MSG_USERAUTH_BANNER) { |
a209e957 |
3079 | char *banner; |
3080 | int size; |
10446215 |
3081 | /* |
3082 | * Don't show the banner if we're operating in |
3083 | * non-verbose non-interactive mode. (It's probably |
3084 | * a script, which means nobody will read the |
3085 | * banner _anyway_, and moreover the printing of |
3086 | * the banner will screw up processing on the |
3087 | * output of (say) plink.) |
3088 | */ |
3089 | if (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE)) { |
3090 | ssh2_pkt_getstring(&banner, &size); |
3091 | if (banner) |
3092 | c_write_untrusted(banner, size); |
3093 | } |
1408a877 |
3094 | crWaitUntilV(ispkt); |
3095 | } |
3096 | if (pktin.type == SSH2_MSG_USERAUTH_SUCCESS) { |
3097 | logevent("Access granted"); |
3098 | we_are_in = TRUE; |
3099 | break; |
3100 | } |
65a22376 |
3101 | |
1408a877 |
3102 | if (pktin.type != SSH2_MSG_USERAUTH_FAILURE) { |
3103 | bombout(("Strange packet received during authentication: type %d", |
3104 | pktin.type)); |
38c4a8da |
3105 | crReturnV; |
65a22376 |
3106 | } |
3107 | |
1408a877 |
3108 | gotit = FALSE; |
65a22376 |
3109 | |
1408a877 |
3110 | /* |
3111 | * OK, we're now sitting on a USERAUTH_FAILURE message, so |
3112 | * we can look at the string in it and know what we can |
3113 | * helpfully try next. |
3114 | */ |
3115 | { |
3116 | char *methods; |
3117 | int methlen; |
3118 | ssh2_pkt_getstring(&methods, &methlen); |
3119 | if (!ssh2_pkt_getbool()) { |
3120 | /* |
3121 | * We have received an unequivocal Access |
3122 | * Denied. This can translate to a variety of |
3123 | * messages: |
3124 | * |
3125 | * - if we'd just tried "none" authentication, |
3126 | * it's not worth printing anything at all |
3127 | * |
3128 | * - if we'd just tried a public key _offer_, |
3129 | * the message should be "Server refused our |
3130 | * key" (or no message at all if the key |
3131 | * came from Pageant) |
3132 | * |
3133 | * - if we'd just tried anything else, the |
3134 | * message really should be "Access denied". |
3135 | * |
3136 | * Additionally, if we'd just tried password |
3137 | * authentication, we should break out of this |
3138 | * whole loop so as to go back to the username |
3139 | * prompt. |
3140 | */ |
3141 | if (type == AUTH_TYPE_NONE) { |
3142 | /* do nothing */ |
3143 | } else if (type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD || |
3144 | type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) { |
3145 | if (type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD) |
3146 | c_write_str("Server refused our key\r\n"); |
3147 | logevent("Server refused public key"); |
3148 | } else { |
3149 | c_write_str("Access denied\r\n"); |
3150 | logevent("Access denied"); |
3151 | if (type == AUTH_TYPE_PASSWORD) { |
3152 | we_are_in = FALSE; |
3153 | break; |
3154 | } |
3155 | } |
3156 | } else { |
3157 | c_write_str("Further authentication required\r\n"); |
3158 | logevent("Further authentication required"); |
3159 | } |
65a22376 |
3160 | |
1408a877 |
3161 | can_pubkey = in_commasep_string("publickey", methods, methlen); |
3162 | can_passwd = in_commasep_string("password", methods, methlen); |
3163 | } |
65a22376 |
3164 | |
1408a877 |
3165 | method = 0; |
65a22376 |
3166 | |
78afa559 |
3167 | if (!method && can_pubkey && agent_exists() && !tried_agent) { |
1983e559 |
3168 | /* |
3169 | * Attempt public-key authentication using Pageant. |
3170 | */ |
3171 | static unsigned char request[5], *response, *p; |
3172 | static int responselen; |
3173 | static int i, nkeys; |
3174 | static int authed = FALSE; |
3175 | void *r; |
3176 | |
3177 | tried_agent = TRUE; |
3178 | |
3179 | logevent("Pageant is running. Requesting keys."); |
3180 | |
3181 | /* Request the keys held by the agent. */ |
3182 | PUT_32BIT(request, 1); |
3183 | request[4] = SSH2_AGENTC_REQUEST_IDENTITIES; |
3184 | agent_query(request, 5, &r, &responselen); |
3185 | response = (unsigned char *)r; |
95797139 |
3186 | if (response && responselen >= 5 && |
3187 | response[4] == SSH2_AGENT_IDENTITIES_ANSWER) { |
1983e559 |
3188 | p = response + 5; |
3189 | nkeys = GET_32BIT(p); p += 4; |
6328ea7a |
3190 | { char buf[64]; sprintf(buf, "Pageant has %d SSH2 keys", nkeys); |
1983e559 |
3191 | logevent(buf); } |
3192 | for (i = 0; i < nkeys; i++) { |
3193 | static char *pkblob, *alg, *commentp; |
3194 | static int pklen, alglen, commentlen; |
3195 | static int siglen, retlen, len; |
3196 | static char *q, *agentreq, *ret; |
3197 | |
3198 | { char buf[64]; sprintf(buf, "Trying Pageant key #%d", i); |
3199 | logevent(buf); } |
3200 | pklen = GET_32BIT(p); p += 4; |
3201 | pkblob = p; p += pklen; |
3202 | alglen = GET_32BIT(pkblob); |
3203 | alg = pkblob + 4; |
3204 | commentlen = GET_32BIT(p); p += 4; |
3205 | commentp = p; p += commentlen; |
3206 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3207 | ssh2_pkt_addstring(username); |
3208 | ssh2_pkt_addstring("ssh-connection");/* service requested */ |
3209 | ssh2_pkt_addstring("publickey");/* method */ |
3210 | ssh2_pkt_addbool(FALSE); /* no signature included */ |
3211 | ssh2_pkt_addstring_start(); |
3212 | ssh2_pkt_addstring_data(alg, alglen); |
3213 | ssh2_pkt_addstring_start(); |
3214 | ssh2_pkt_addstring_data(pkblob, pklen); |
3215 | ssh2_pkt_send(); |
3216 | |
3217 | crWaitUntilV(ispkt); |
3218 | if (pktin.type != SSH2_MSG_USERAUTH_PK_OK) { |
3219 | logevent("Key refused"); |
3220 | continue; |
3221 | } |
3222 | |
10446215 |
3223 | if (flags & FLAG_VERBOSE) { |
3224 | c_write_str("Authenticating with public key \""); |
3225 | c_write(commentp, commentlen); |
3226 | c_write_str("\" from agent\r\n"); |
3227 | } |
1983e559 |
3228 | |
3229 | /* |
3230 | * Server is willing to accept the key. |
3231 | * Construct a SIGN_REQUEST. |
3232 | */ |
3233 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3234 | ssh2_pkt_addstring(username); |
3235 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
3236 | ssh2_pkt_addstring("publickey"); /* method */ |
3237 | ssh2_pkt_addbool(TRUE); |
3238 | ssh2_pkt_addstring_start(); |
3239 | ssh2_pkt_addstring_data(alg, alglen); |
3240 | ssh2_pkt_addstring_start(); |
3241 | ssh2_pkt_addstring_data(pkblob, pklen); |
3242 | |
3243 | siglen = pktout.length - 5 + 4 + 20; |
3244 | len = 1; /* message type */ |
3245 | len += 4 + pklen; /* key blob */ |
3246 | len += 4 + siglen; /* data to sign */ |
3247 | len += 4; /* flags */ |
3248 | agentreq = smalloc(4 + len); |
3249 | PUT_32BIT(agentreq, len); |
3250 | q = agentreq + 4; |
3251 | *q++ = SSH2_AGENTC_SIGN_REQUEST; |
3252 | PUT_32BIT(q, pklen); q += 4; |
3253 | memcpy(q, pkblob, pklen); q += pklen; |
3254 | PUT_32BIT(q, siglen); q += 4; |
3255 | /* Now the data to be signed... */ |
3256 | PUT_32BIT(q, 20); q += 4; |
3257 | memcpy(q, ssh2_session_id, 20); q += 20; |
3258 | memcpy(q, pktout.data+5, pktout.length-5); |
3259 | q += pktout.length-5; |
3260 | /* And finally the (zero) flags word. */ |
3261 | PUT_32BIT(q, 0); |
3262 | agent_query(agentreq, len+4, &ret, &retlen); |
3263 | sfree(agentreq); |
3264 | if (ret) { |
3265 | if (ret[4] == SSH2_AGENT_SIGN_RESPONSE) { |
3266 | logevent("Sending Pageant's response"); |
3267 | ssh2_pkt_addstring_start(); |
3268 | ssh2_pkt_addstring_data(ret+9, GET_32BIT(ret+5)); |
3269 | ssh2_pkt_send(); |
3270 | authed = TRUE; |
3271 | break; |
3272 | } else { |
3273 | logevent("Pageant failed to answer challenge"); |
3274 | sfree(ret); |
3275 | } |
3276 | } |
3277 | } |
3278 | if (authed) |
3279 | continue; |
3280 | } |
3281 | } |
3282 | |
1408a877 |
3283 | if (!method && can_pubkey && *cfg.keyfile && !tried_pubkey_config) { |
3284 | unsigned char *pub_blob; |
3285 | char *algorithm, *comment; |
3286 | int pub_blob_len; |
65a22376 |
3287 | |
1408a877 |
3288 | tried_pubkey_config = TRUE; |
65a22376 |
3289 | |
65a22376 |
3290 | /* |
1408a877 |
3291 | * Try the public key supplied in the configuration. |
3292 | * |
3293 | * First, offer the public blob to see if the server is |
3294 | * willing to accept it. |
65a22376 |
3295 | */ |
1408a877 |
3296 | pub_blob = ssh2_userkey_loadpub(cfg.keyfile, &algorithm, |
3297 | &pub_blob_len); |
3298 | if (pub_blob) { |
3299 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3300 | ssh2_pkt_addstring(username); |
3301 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
3302 | ssh2_pkt_addstring("publickey");/* method */ |
3303 | ssh2_pkt_addbool(FALSE); /* no signature included */ |
3304 | ssh2_pkt_addstring(algorithm); |
3305 | ssh2_pkt_addstring_start(); |
3306 | ssh2_pkt_addstring_data(pub_blob, pub_blob_len); |
3307 | ssh2_pkt_send(); |
3308 | logevent("Offered public key"); /* FIXME */ |
3309 | |
3310 | crWaitUntilV(ispkt); |
3311 | if (pktin.type != SSH2_MSG_USERAUTH_PK_OK) { |
3312 | gotit = TRUE; |
3313 | type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD; |
3314 | continue; /* key refused; give up on it */ |
3315 | } |
65a22376 |
3316 | |
1408a877 |
3317 | logevent("Offer of public key accepted"); |
65a22376 |
3318 | /* |
1408a877 |
3319 | * Actually attempt a serious authentication using |
3320 | * the key. |
65a22376 |
3321 | */ |
1408a877 |
3322 | if (ssh2_userkey_encrypted(cfg.keyfile, &comment)) { |
3323 | sprintf(pwprompt, "Passphrase for key \"%.100s\": ", comment); |
3324 | need_pw = TRUE; |
3325 | } else { |
3326 | need_pw = FALSE; |
3327 | } |
3328 | c_write_str("Authenticating with public key \""); |
3329 | c_write_str(comment); |
3330 | c_write_str("\"\r\n"); |
3331 | method = AUTH_PUBLICKEY_FILE; |
65a22376 |
3332 | } |
1408a877 |
3333 | } |
3334 | |
3335 | if (!method && can_passwd) { |
3336 | method = AUTH_PASSWORD; |
3337 | sprintf(pwprompt, "%.90s@%.90s's password: ", username, savedhost); |
3338 | need_pw = TRUE; |
3339 | } |
3340 | |
3341 | if (need_pw) { |
fa17a66e |
3342 | if (ssh_get_line) { |
3343 | if (!ssh_get_line(pwprompt, password, |
3344 | sizeof(password), TRUE)) { |
1408a877 |
3345 | /* |
fa17a66e |
3346 | * get_line failed to get a password (for |
3347 | * example because one was supplied on the |
3348 | * command line which has already failed to |
3349 | * work). Terminate. |
1408a877 |
3350 | */ |
3351 | logevent("No more passwords to try"); |
3352 | ssh_state = SSH_STATE_CLOSED; |
3353 | crReturnV; |
3354 | } |
3355 | } else { |
3356 | static int pos = 0; |
3357 | static char c; |
3358 | |
3359 | c_write_str(pwprompt); |
3360 | ssh_send_ok = 1; |
3361 | |
3362 | pos = 0; |
3363 | while (pos >= 0) { |
3364 | crWaitUntilV(!ispkt); |
3365 | while (inlen--) switch (c = *in++) { |
3366 | case 10: case 13: |
3367 | password[pos] = 0; |
3368 | pos = -1; |
3369 | break; |
3370 | case 8: case 127: |
3371 | if (pos > 0) |
3372 | pos--; |
3373 | break; |
3374 | case 21: case 27: |
3375 | pos = 0; |
3376 | break; |
3377 | case 3: case 4: |
3378 | random_save_seed(); |
3379 | exit(0); |
3380 | break; |
3381 | default: |
3382 | if (((c >= ' ' && c <= '~') || |
3383 | ((unsigned char)c >= 160)) && pos < 40) |
3384 | password[pos++] = c; |
3385 | break; |
3386 | } |
65a22376 |
3387 | } |
1408a877 |
3388 | c_write_str("\r\n"); |
65a22376 |
3389 | } |
65a22376 |
3390 | } |
65a22376 |
3391 | |
1408a877 |
3392 | if (method == AUTH_PUBLICKEY_FILE) { |
3393 | /* |
3394 | * We have our passphrase. Now try the actual authentication. |
3395 | */ |
3396 | struct ssh2_userkey *key; |
65a22376 |
3397 | |
1408a877 |
3398 | key = ssh2_load_userkey(cfg.keyfile, password); |
3399 | if (key == SSH2_WRONG_PASSPHRASE || key == NULL) { |
3400 | if (key == SSH2_WRONG_PASSPHRASE) { |
3401 | c_write_str("Wrong passphrase\r\n"); |
3402 | tried_pubkey_config = FALSE; |
3403 | } else { |
3404 | c_write_str("Unable to load private key\r\n"); |
3405 | tried_pubkey_config = TRUE; |
3406 | } |
3407 | /* Send a spurious AUTH_NONE to return to the top. */ |
3408 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3409 | ssh2_pkt_addstring(username); |
3410 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
3411 | ssh2_pkt_addstring("none"); /* method */ |
3412 | ssh2_pkt_send(); |
3413 | type = AUTH_TYPE_NONE; |
3414 | } else { |
3415 | unsigned char *blob, *sigdata; |
3416 | int blob_len, sigdata_len; |
65a22376 |
3417 | |
1408a877 |
3418 | /* |
3419 | * We have loaded the private key and the server |
3420 | * has announced that it's willing to accept it. |
3421 | * Hallelujah. Generate a signature and send it. |
3422 | */ |
3423 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3424 | ssh2_pkt_addstring(username); |
3425 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
3426 | ssh2_pkt_addstring("publickey"); /* method */ |
3427 | ssh2_pkt_addbool(TRUE); |
3428 | ssh2_pkt_addstring(key->alg->name); |
3429 | blob = key->alg->public_blob(key->data, &blob_len); |
3430 | ssh2_pkt_addstring_start(); |
3431 | ssh2_pkt_addstring_data(blob, blob_len); |
3432 | sfree(blob); |
3433 | |
3434 | /* |
3435 | * The data to be signed is: |
3436 | * |
3437 | * string session-id |
3438 | * |
3439 | * followed by everything so far placed in the |
3440 | * outgoing packet. |
3441 | */ |
3442 | sigdata_len = pktout.length - 5 + 4 + 20; |
3443 | sigdata = smalloc(sigdata_len); |
3444 | PUT_32BIT(sigdata, 20); |
3445 | memcpy(sigdata+4, ssh2_session_id, 20); |
3446 | memcpy(sigdata+24, pktout.data+5, pktout.length-5); |
3447 | blob = key->alg->sign(key->data, sigdata, sigdata_len, &blob_len); |
3448 | ssh2_pkt_addstring_start(); |
3449 | ssh2_pkt_addstring_data(blob, blob_len); |
3450 | sfree(blob); |
3451 | sfree(sigdata); |
3452 | |
3453 | ssh2_pkt_send(); |
3454 | type = AUTH_TYPE_PUBLICKEY; |
3455 | } |
3456 | } else if (method == AUTH_PASSWORD) { |
65a22376 |
3457 | /* |
1408a877 |
3458 | * We send the password packet lumped tightly together with |
3459 | * an SSH_MSG_IGNORE packet. The IGNORE packet contains a |
3460 | * string long enough to make the total length of the two |
3461 | * packets constant. This should ensure that a passive |
3462 | * listener doing traffic analyis can't work out the length |
3463 | * of the password. |
3464 | * |
3465 | * For this to work, we need an assumption about the |
3466 | * maximum length of the password packet. I think 256 is |
3467 | * pretty conservative. Anyone using a password longer than |
3468 | * that probably doesn't have much to worry about from |
3469 | * people who find out how long their password is! |
65a22376 |
3470 | */ |
3471 | ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST); |
3472 | ssh2_pkt_addstring(username); |
3473 | ssh2_pkt_addstring("ssh-connection"); /* service requested */ |
1408a877 |
3474 | ssh2_pkt_addstring("password"); |
3475 | ssh2_pkt_addbool(FALSE); |
3476 | ssh2_pkt_addstring(password); |
3477 | ssh2_pkt_defer(); |
65a22376 |
3478 | /* |
1408a877 |
3479 | * We'll include a string that's an exact multiple of the |
3480 | * cipher block size. If the cipher is NULL for some |
3481 | * reason, we don't do this trick at all because we gain |
3482 | * nothing by it. |
65a22376 |
3483 | */ |
6e9e9520 |
3484 | if (cscipher) { |
3485 | int stringlen, i; |
3486 | |
3487 | stringlen = (256 - deferred_len); |
3488 | stringlen += cscipher->blksize - 1; |
3489 | stringlen -= (stringlen % cscipher->blksize); |
3490 | if (cscomp) { |
3491 | /* |
3492 | * Temporarily disable actual compression, |
3493 | * so we can guarantee to get this string |
3494 | * exactly the length we want it. The |
3495 | * compression-disabling routine should |
3496 | * return an integer indicating how many |
3497 | * bytes we should adjust our string length |
3498 | * by. |
3499 | */ |
3500 | stringlen -= cscomp->disable_compression(); |
3501 | } |
1408a877 |
3502 | ssh2_pkt_init(SSH2_MSG_IGNORE); |
3503 | ssh2_pkt_addstring_start(); |
6e9e9520 |
3504 | for (i = 0; i < stringlen; i++) { |
3505 | char c = (char)random_byte(); |
3506 | ssh2_pkt_addstring_data(&c, 1); |
65a22376 |
3507 | } |
1408a877 |
3508 | ssh2_pkt_defer(); |
65a22376 |
3509 | } |
39065bed |
3510 | ssh_pkt_defersend(); |
0d43337a |
3511 | logevent("Sent password"); |
1408a877 |
3512 | type = AUTH_TYPE_PASSWORD; |
3513 | } else { |
3514 | c_write_str("No supported authentication methods left to try!\r\n"); |
3515 | logevent("No supported authentications offered. Disconnecting"); |
3516 | ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
3517 | ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION); |
3518 | ssh2_pkt_addstring("No supported authentication methods available"); |
3519 | ssh2_pkt_addstring("en"); /* language tag */ |
3520 | ssh2_pkt_send(); |
3521 | ssh_state = SSH_STATE_CLOSED; |
3522 | crReturnV; |
65a22376 |
3523 | } |
65a22376 |
3524 | } |
1408a877 |
3525 | } while (!we_are_in); |
7cca0d81 |
3526 | |
3527 | /* |
3528 | * Now we're authenticated for the connection protocol. The |
3529 | * connection protocol will automatically have started at this |
3530 | * point; there's no need to send SERVICE_REQUEST. |
3531 | */ |
3532 | |
3533 | /* |
3534 | * So now create a channel with a session in it. |
3535 | */ |
d2371c81 |
3536 | ssh_channels = newtree234(ssh_channelcmp); |
dcbde236 |
3537 | mainchan = smalloc(sizeof(struct ssh_channel)); |
d2371c81 |
3538 | mainchan->localid = alloc_channel_id(); |
7cca0d81 |
3539 | ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN); |
3540 | ssh2_pkt_addstring("session"); |
d211621f |
3541 | ssh2_pkt_adduint32(mainchan->localid); |
cf6e59d6 |
3542 | ssh2_pkt_adduint32(0x8000UL); /* our window size */ |
d211621f |
3543 | ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */ |
7cca0d81 |
3544 | ssh2_pkt_send(); |
3545 | crWaitUntilV(ispkt); |
3546 | if (pktin.type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
8d5de777 |
3547 | bombout(("Server refused to open a session")); |
3548 | crReturnV; |
7cca0d81 |
3549 | /* FIXME: error data comes back in FAILURE packet */ |
3550 | } |
d211621f |
3551 | if (ssh2_pkt_getuint32() != mainchan->localid) { |
8d5de777 |
3552 | bombout(("Server's channel confirmation cited wrong channel")); |
3553 | crReturnV; |
7cca0d81 |
3554 | } |
d211621f |
3555 | mainchan->remoteid = ssh2_pkt_getuint32(); |
783415f8 |
3556 | mainchan->type = CHAN_MAINSESSION; |
3557 | mainchan->closes = 0; |
3558 | mainchan->v2.remwindow = ssh2_pkt_getuint32(); |
3559 | mainchan->v2.remmaxpkt = ssh2_pkt_getuint32(); |
3560 | mainchan->v2.outbuffer = NULL; |
3561 | mainchan->v2.outbuflen = mainchan->v2.outbufsize = 0; |
783415f8 |
3562 | add234(ssh_channels, mainchan); |
7cca0d81 |
3563 | logevent("Opened channel for session"); |
3564 | |
3565 | /* |
783415f8 |
3566 | * Potentially enable X11 forwarding. |
3567 | */ |
3568 | if (cfg.x11_forward) { |
3569 | char proto[20], data[64]; |
3570 | logevent("Requesting X11 forwarding"); |
3571 | x11_invent_auth(proto, sizeof(proto), data, sizeof(data)); |
3572 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
3573 | ssh2_pkt_adduint32(mainchan->remoteid); |
3574 | ssh2_pkt_addstring("x11-req"); |
3575 | ssh2_pkt_addbool(1); /* want reply */ |
3576 | ssh2_pkt_addbool(0); /* many connections */ |
3577 | ssh2_pkt_addstring(proto); |
3578 | ssh2_pkt_addstring(data); |
3579 | ssh2_pkt_adduint32(0); /* screen number */ |
3580 | ssh2_pkt_send(); |
3581 | |
3582 | do { |
3583 | crWaitUntilV(ispkt); |
3584 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
3585 | unsigned i = ssh2_pkt_getuint32(); |
3586 | struct ssh_channel *c; |
3587 | c = find234(ssh_channels, &i, ssh_channelfind); |
3588 | if (!c) |
3589 | continue; /* nonexistent channel */ |
3590 | c->v2.remwindow += ssh2_pkt_getuint32(); |
3591 | } |
3592 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
3593 | |
3594 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
3595 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
3596 | bombout(("Server got confused by X11 forwarding request")); |
3597 | crReturnV; |
3598 | } |
3599 | logevent("X11 forwarding refused"); |
3600 | } else { |
3601 | logevent("X11 forwarding enabled"); |
3602 | ssh_X11_fwd_enabled = TRUE; |
3603 | } |
3604 | } |
3605 | |
3606 | /* |
36c2a3e9 |
3607 | * Potentially enable agent forwarding. |
3608 | */ |
3609 | if (cfg.agentfwd && agent_exists()) { |
36c2a3e9 |
3610 | logevent("Requesting OpenSSH-style agent forwarding"); |
36c2a3e9 |
3611 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
3612 | ssh2_pkt_adduint32(mainchan->remoteid); |
3613 | ssh2_pkt_addstring("auth-agent-req@openssh.com"); |
3614 | ssh2_pkt_addbool(1); /* want reply */ |
3615 | ssh2_pkt_send(); |
3616 | |
3617 | do { |
3618 | crWaitUntilV(ispkt); |
3619 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
3620 | unsigned i = ssh2_pkt_getuint32(); |
3621 | struct ssh_channel *c; |
3622 | c = find234(ssh_channels, &i, ssh_channelfind); |
3623 | if (!c) |
3624 | continue; /* nonexistent channel */ |
3625 | c->v2.remwindow += ssh2_pkt_getuint32(); |
3626 | } |
3627 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
3628 | |
3629 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
3630 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
3631 | bombout(("Server got confused by agent forwarding request")); |
3632 | crReturnV; |
3633 | } |
3634 | logevent("Agent forwarding refused"); |
3635 | } else { |
3636 | logevent("Agent forwarding enabled"); |
3637 | ssh_agentfwd_enabled = TRUE; |
3638 | } |
3639 | } |
3640 | |
3641 | /* |
7cca0d81 |
3642 | * Now allocate a pty for the session. |
3643 | */ |
67779be7 |
3644 | if (!cfg.nopty) { |
3645 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
3646 | ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */ |
3647 | ssh2_pkt_addstring("pty-req"); |
3648 | ssh2_pkt_addbool(1); /* want reply */ |
3649 | ssh2_pkt_addstring(cfg.termtype); |
3650 | ssh2_pkt_adduint32(cols); |
3651 | ssh2_pkt_adduint32(rows); |
3652 | ssh2_pkt_adduint32(0); /* pixel width */ |
3653 | ssh2_pkt_adduint32(0); /* pixel height */ |
3654 | ssh2_pkt_addstring_start(); |
3655 | ssh2_pkt_addstring_data("\0", 1);/* TTY_OP_END, no special options */ |
3656 | ssh2_pkt_send(); |
6e48c3fe |
3657 | ssh_state = SSH_STATE_INTERMED; |
7cca0d81 |
3658 | |
67779be7 |
3659 | do { |
3660 | crWaitUntilV(ispkt); |
3661 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
783415f8 |
3662 | unsigned i = ssh2_pkt_getuint32(); |
3663 | struct ssh_channel *c; |
3664 | c = find234(ssh_channels, &i, ssh_channelfind); |
3665 | if (!c) |
3666 | continue; /* nonexistent channel */ |
3667 | c->v2.remwindow += ssh2_pkt_getuint32(); |
67779be7 |
3668 | } |
3669 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
7cca0d81 |
3670 | |
67779be7 |
3671 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
3672 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
8d5de777 |
3673 | bombout(("Server got confused by pty request")); |
3674 | crReturnV; |
67779be7 |
3675 | } |
1408a877 |
3676 | c_write_str("Server refused to allocate pty\r\n"); |
0965bee0 |
3677 | ssh_editing = ssh_echoing = 1; |
67779be7 |
3678 | } else { |
3679 | logevent("Allocated pty"); |
7cca0d81 |
3680 | } |
0965bee0 |
3681 | } else { |
3682 | ssh_editing = ssh_echoing = 1; |
7cca0d81 |
3683 | } |
3684 | |
3685 | /* |
67779be7 |
3686 | * Start a shell or a remote command. |
7cca0d81 |
3687 | */ |
3688 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
d211621f |
3689 | ssh2_pkt_adduint32(mainchan->remoteid); /* recipient channel */ |
4a8fc3c4 |
3690 | if (cfg.ssh_subsys) { |
3691 | ssh2_pkt_addstring("subsystem"); |
3692 | ssh2_pkt_addbool(1); /* want reply */ |
96621a84 |
3693 | ssh2_pkt_addstring(cfg.remote_cmd_ptr); |
3694 | } else if (*cfg.remote_cmd_ptr) { |
67779be7 |
3695 | ssh2_pkt_addstring("exec"); |
3696 | ssh2_pkt_addbool(1); /* want reply */ |
96621a84 |
3697 | ssh2_pkt_addstring(cfg.remote_cmd_ptr); |
67779be7 |
3698 | } else { |
3699 | ssh2_pkt_addstring("shell"); |
3700 | ssh2_pkt_addbool(1); /* want reply */ |
3701 | } |
7cca0d81 |
3702 | ssh2_pkt_send(); |
d211621f |
3703 | do { |
7cca0d81 |
3704 | crWaitUntilV(ispkt); |
d211621f |
3705 | if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
783415f8 |
3706 | unsigned i = ssh2_pkt_getuint32(); |
3707 | struct ssh_channel *c; |
3708 | c = find234(ssh_channels, &i, ssh_channelfind); |
3709 | if (!c) |
3710 | continue; /* nonexistent channel */ |
3711 | c->v2.remwindow += ssh2_pkt_getuint32(); |
d211621f |
3712 | } |
7cca0d81 |
3713 | } while (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
3714 | if (pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
3715 | if (pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
8d5de777 |
3716 | bombout(("Server got confused by shell/command request")); |
3717 | crReturnV; |
7cca0d81 |
3718 | } |
8d5de777 |
3719 | bombout(("Server refused to start a shell/command")); |
3720 | crReturnV; |
7cca0d81 |
3721 | } else { |
8d5de777 |
3722 | logevent("Started a shell/command"); |
7cca0d81 |
3723 | } |
3724 | |
6e48c3fe |
3725 | ssh_state = SSH_STATE_SESSION; |
3726 | if (size_needed) |
3727 | ssh_size(); |
3687d221 |
3728 | if (eof_needed) |
3729 | ssh_special(TS_EOF); |
6e48c3fe |
3730 | |
7cca0d81 |
3731 | /* |
3732 | * Transfer data! |
3733 | */ |
0965bee0 |
3734 | ldisc_send(NULL, 0); /* cause ldisc to notice changes */ |
8ccc75b0 |
3735 | ssh_send_ok = 1; |
7cca0d81 |
3736 | while (1) { |
d211621f |
3737 | static int try_send; |
e5574168 |
3738 | crReturnV; |
d211621f |
3739 | try_send = FALSE; |
7cca0d81 |
3740 | if (ispkt) { |
3741 | if (pktin.type == SSH2_MSG_CHANNEL_DATA || |
3742 | pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA) { |
3743 | char *data; |
3744 | int length; |
783415f8 |
3745 | unsigned i = ssh2_pkt_getuint32(); |
3746 | struct ssh_channel *c; |
3747 | c = find234(ssh_channels, &i, ssh_channelfind); |
3748 | if (!c) |
3749 | continue; /* nonexistent channel */ |
7cca0d81 |
3750 | if (pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA && |
3751 | ssh2_pkt_getuint32() != SSH2_EXTENDED_DATA_STDERR) |
3752 | continue; /* extended but not stderr */ |
3753 | ssh2_pkt_getstring(&data, &length); |
d211621f |
3754 | if (data) { |
783415f8 |
3755 | switch (c->type) { |
3756 | case CHAN_MAINSESSION: |
3757 | from_backend(pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA, |
3758 | data, length); |
3759 | break; |
3760 | case CHAN_X11: |
3761 | x11_send(c->u.x11.s, data, length); |
3762 | break; |
36c2a3e9 |
3763 | case CHAN_AGENT: |
3764 | while (length > 0) { |
3765 | if (c->u.a.lensofar < 4) { |
3766 | int l = min(4 - c->u.a.lensofar, length); |
3767 | memcpy(c->u.a.msglen + c->u.a.lensofar, data, l); |
3768 | data += l; length -= l; c->u.a.lensofar += l; |
3769 | } |
3770 | if (c->u.a.lensofar == 4) { |
3771 | c->u.a.totallen = 4 + GET_32BIT(c->u.a.msglen); |
3772 | c->u.a.message = smalloc(c->u.a.totallen); |
3773 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
3774 | } |
3775 | if (c->u.a.lensofar >= 4 && length > 0) { |
3776 | int l = min(c->u.a.totallen - c->u.a.lensofar, |
3777 | length); |
3778 | memcpy(c->u.a.message + c->u.a.lensofar, data, l); |
3779 | data += l; length -= l; c->u.a.lensofar += l; |
3780 | } |
3781 | if (c->u.a.lensofar == c->u.a.totallen) { |
3782 | void *reply, *sentreply; |
3783 | int replylen; |
3784 | agent_query(c->u.a.message, c->u.a.totallen, |
3785 | &reply, &replylen); |
3786 | if (reply) |
3787 | sentreply = reply; |
3788 | else { |
3789 | /* Fake SSH_AGENT_FAILURE. */ |
3790 | sentreply = "\0\0\0\1\5"; |
3791 | replylen = 5; |
3792 | } |
3793 | ssh2_add_channel_data(c, sentreply, replylen); |
3794 | try_send = TRUE; |
3795 | if (reply) |
3796 | sfree(reply); |
3797 | sfree(c->u.a.message); |
3798 | c->u.a.lensofar = 0; |
3799 | } |
3800 | } |
3801 | break; |
783415f8 |
3802 | } |
d211621f |
3803 | /* |
783415f8 |
3804 | * Enlarge the window again at the remote |
3805 | * side, just in case it ever runs down and |
3806 | * they fail to send us any more data. |
d211621f |
3807 | */ |
3808 | ssh2_pkt_init(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
783415f8 |
3809 | ssh2_pkt_adduint32(c->remoteid); |
d211621f |
3810 | ssh2_pkt_adduint32(length); |
3811 | ssh2_pkt_send(); |
3812 | } |
7cca0d81 |
3813 | } else if (pktin.type == SSH2_MSG_DISCONNECT) { |
3814 | ssh_state = SSH_STATE_CLOSED; |
d211621f |
3815 | logevent("Received disconnect message"); |
3257deae |
3816 | crReturnV; |
7cca0d81 |
3817 | } else if (pktin.type == SSH2_MSG_CHANNEL_REQUEST) { |
3818 | continue; /* exit status et al; ignore (FIXME?) */ |
d211621f |
3819 | } else if (pktin.type == SSH2_MSG_CHANNEL_EOF) { |
783415f8 |
3820 | unsigned i = ssh2_pkt_getuint32(); |
3821 | struct ssh_channel *c; |
3822 | |
3823 | c = find234(ssh_channels, &i, ssh_channelfind); |
3824 | if (!c) |
3825 | continue; /* nonexistent channel */ |
3826 | |
3827 | if (c->type == CHAN_X11) { |
3828 | /* |
3829 | * Remote EOF on an X11 channel means we should |
3830 | * wrap up and close the channel ourselves. |
3831 | */ |
3832 | x11_close(c->u.x11.s); |
3833 | sshfwd_close(c); |
36c2a3e9 |
3834 | } else if (c->type == CHAN_AGENT) { |
3835 | sshfwd_close(c); |
3836 | } |
d211621f |
3837 | } else if (pktin.type == SSH2_MSG_CHANNEL_CLOSE) { |
783415f8 |
3838 | unsigned i = ssh2_pkt_getuint32(); |
3839 | struct ssh_channel *c; |
783415f8 |
3840 | |
3841 | c = find234(ssh_channels, &i, ssh_channelfind); |
3842 | if (!c) |
3843 | continue; /* nonexistent channel */ |
3844 | if (c->closes == 0) { |
3845 | ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE); |
3846 | ssh2_pkt_adduint32(c->remoteid); |
3847 | ssh2_pkt_send(); |
3848 | } |
3849 | /* Do pre-close processing on the channel. */ |
3850 | switch (c->type) { |
3851 | case CHAN_MAINSESSION: |
3852 | break; /* nothing to see here, move along */ |
3853 | case CHAN_X11: |
3854 | break; |
36c2a3e9 |
3855 | case CHAN_AGENT: |
3856 | break; |
783415f8 |
3857 | } |
3858 | del234(ssh_channels, c); |
3859 | sfree(c->v2.outbuffer); |
3860 | sfree(c); |
3861 | |
3862 | /* |
3863 | * See if that was the last channel left open. |
3864 | */ |
d2371c81 |
3865 | if (count234(ssh_channels) == 0) { |
d211621f |
3866 | logevent("All channels closed. Disconnecting"); |
3867 | ssh2_pkt_init(SSH2_MSG_DISCONNECT); |
9005f3ba |
3868 | ssh2_pkt_adduint32(SSH2_DISCONNECT_BY_APPLICATION); |
3869 | ssh2_pkt_addstring("All open channels closed"); |
3870 | ssh2_pkt_addstring("en"); /* language tag */ |
d211621f |
3871 | ssh2_pkt_send(); |
cf6e59d6 |
3872 | ssh_state = SSH_STATE_CLOSED; |
3257deae |
3873 | crReturnV; |
d211621f |
3874 | } |
3875 | continue; /* remote sends close; ignore (FIXME) */ |
7cca0d81 |
3876 | } else if (pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
783415f8 |
3877 | unsigned i = ssh2_pkt_getuint32(); |
3878 | struct ssh_channel *c; |
3879 | c = find234(ssh_channels, &i, ssh_channelfind); |
3880 | if (!c) |
3881 | continue; /* nonexistent channel */ |
0993479d |
3882 | c->v2.remwindow += ssh2_pkt_getuint32(); |
d211621f |
3883 | try_send = TRUE; |
783415f8 |
3884 | } else if (pktin.type == SSH2_MSG_CHANNEL_OPEN) { |
3885 | char *type; |
3886 | int typelen; |
3887 | char *error = NULL; |
3888 | struct ssh_channel *c; |
3889 | ssh2_pkt_getstring(&type, &typelen); |
3890 | c = smalloc(sizeof(struct ssh_channel)); |
3891 | |
3892 | if (typelen == 3 && !memcmp(type, "x11", 3)) { |
783415f8 |
3893 | if (!ssh_X11_fwd_enabled) |
3894 | error = "X11 forwarding is not enabled"; |
0c6c9a70 |
3895 | else if ( x11_init(&c->u.x11.s, cfg.x11_display, c) != NULL ) { |
783415f8 |
3896 | error = "Unable to open an X11 connection"; |
3897 | } else { |
3898 | c->type = CHAN_X11; |
3899 | } |
36c2a3e9 |
3900 | } else if (typelen == 22 && |
3901 | !memcmp(type, "auth-agent@openssh.com", 3)) { |
3902 | if (!ssh_agentfwd_enabled) |
3903 | error = "Agent forwarding is not enabled"; |
3904 | else { |
3905 | c->type = CHAN_AGENT; /* identify channel type */ |
3906 | c->u.a.lensofar = 0; |
3907 | } |
783415f8 |
3908 | } else { |
3909 | error = "Unsupported channel type requested"; |
3910 | } |
3911 | |
3912 | c->remoteid = ssh2_pkt_getuint32(); |
3913 | if (error) { |
3914 | ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
3915 | ssh2_pkt_adduint32(c->remoteid); |
3916 | ssh2_pkt_adduint32(SSH2_OPEN_CONNECT_FAILED); |
3917 | ssh2_pkt_addstring(error); |
3918 | ssh2_pkt_addstring("en"); /* language tag */ |
3919 | ssh2_pkt_send(); |
3920 | sfree(c); |
3921 | } else { |
d2371c81 |
3922 | c->localid = alloc_channel_id(); |
783415f8 |
3923 | c->closes = 0; |
3924 | c->v2.remwindow = ssh2_pkt_getuint32(); |
3925 | c->v2.remmaxpkt = ssh2_pkt_getuint32(); |
3926 | c->v2.outbuffer = NULL; |
3927 | c->v2.outbuflen = c->v2.outbufsize = 0; |
3928 | add234(ssh_channels, c); |
3929 | ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
3930 | ssh2_pkt_adduint32(c->remoteid); |
3931 | ssh2_pkt_adduint32(c->localid); |
3932 | ssh2_pkt_adduint32(0x8000UL); /* our window size */ |
3933 | ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */ |
3934 | ssh2_pkt_send(); |
3935 | } |
7cca0d81 |
3936 | } else { |
8d5de777 |
3937 | bombout(("Strange packet received: type %d", pktin.type)); |
3938 | crReturnV; |
7cca0d81 |
3939 | } |
3940 | } else { |
d211621f |
3941 | /* |
3942 | * We have spare data. Add it to the channel buffer. |
3943 | */ |
783415f8 |
3944 | ssh2_add_channel_data(mainchan, in, inlen); |
d211621f |
3945 | try_send = TRUE; |
7cca0d81 |
3946 | } |
d211621f |
3947 | if (try_send) { |
d2371c81 |
3948 | int i; |
783415f8 |
3949 | struct ssh_channel *c; |
d211621f |
3950 | /* |
783415f8 |
3951 | * Try to send data on all channels if we can. |
d211621f |
3952 | */ |
d2371c81 |
3953 | for (i = 0; NULL != (c = index234(ssh_channels, i)); i++) |
783415f8 |
3954 | ssh2_try_send(c); |
d211621f |
3955 | } |
e5574168 |
3956 | } |
3957 | |
3958 | crFinishV; |
3959 | } |
3960 | |
3961 | /* |
7cca0d81 |
3962 | * Handle the top-level SSH2 protocol. |
3963 | */ |
3964 | static void ssh2_protocol(unsigned char *in, int inlen, int ispkt) |
3965 | { |
3966 | if (do_ssh2_transport(in, inlen, ispkt) == 0) |
3967 | return; |
3968 | do_ssh2_authconn(in, inlen, ispkt); |
3969 | } |
3970 | |
3971 | /* |
8df7a775 |
3972 | * Called to set up the connection. |
374330e2 |
3973 | * |
3974 | * Returns an error message, or NULL on success. |
374330e2 |
3975 | */ |
8df7a775 |
3976 | static char *ssh_init (char *host, int port, char **realhost) { |
fb09bf1c |
3977 | char *p; |
8f203108 |
3978 | |
3979 | #ifdef MSCRYPTOAPI |
3980 | if(crypto_startup() == 0) |
3981 | return "Microsoft high encryption pack not installed!"; |
3982 | #endif |
374330e2 |
3983 | |
8df7a775 |
3984 | ssh_send_ok = 0; |
0965bee0 |
3985 | ssh_editing = 0; |
3986 | ssh_echoing = 0; |
8df7a775 |
3987 | |
fb09bf1c |
3988 | p = connect_to_host(host, port, realhost); |
3989 | if (p != NULL) |
3990 | return p; |
374330e2 |
3991 | |
374330e2 |
3992 | return NULL; |
3993 | } |
3994 | |
3995 | /* |
374330e2 |
3996 | * Called to send data down the Telnet connection. |
3997 | */ |
3998 | static void ssh_send (char *buf, int len) { |
f78051a1 |
3999 | if (s == NULL || ssh_protocol == NULL) |
374330e2 |
4000 | return; |
4001 | |
4002 | ssh_protocol(buf, len, 0); |
4003 | } |
4004 | |
4005 | /* |
6e48c3fe |
4006 | * Called to set the size of the window from SSH's POV. |
374330e2 |
4007 | */ |
4008 | static void ssh_size(void) { |
4009 | switch (ssh_state) { |
4010 | case SSH_STATE_BEFORE_SIZE: |
3687d221 |
4011 | case SSH_STATE_PREPACKET: |
21248260 |
4012 | case SSH_STATE_CLOSED: |
374330e2 |
4013 | break; /* do nothing */ |
4014 | case SSH_STATE_INTERMED: |
4015 | size_needed = TRUE; /* buffer for later */ |
4016 | break; |
4017 | case SSH_STATE_SESSION: |
fef97f43 |
4018 | if (!cfg.nopty) { |
6e48c3fe |
4019 | if (ssh_version == 1) { |
4020 | send_packet(SSH1_CMSG_WINDOW_SIZE, |
4021 | PKT_INT, rows, PKT_INT, cols, |
4022 | PKT_INT, 0, PKT_INT, 0, PKT_END); |
4023 | } else { |
4024 | ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST); |
4025 | ssh2_pkt_adduint32(mainchan->remoteid); |
4026 | ssh2_pkt_addstring("window-change"); |
4027 | ssh2_pkt_addbool(0); |
4028 | ssh2_pkt_adduint32(cols); |
4029 | ssh2_pkt_adduint32(rows); |
4030 | ssh2_pkt_adduint32(0); |
4031 | ssh2_pkt_adduint32(0); |
4032 | ssh2_pkt_send(); |
4033 | } |
fef97f43 |
4034 | } |
3687d221 |
4035 | break; |
374330e2 |
4036 | } |
4037 | } |
4038 | |
4039 | /* |
6abbf9e3 |
4040 | * Send Telnet special codes. TS_EOF is useful for `plink', so you |
4041 | * can send an EOF and collect resulting output (e.g. `plink |
4042 | * hostname sort'). |
374330e2 |
4043 | */ |
4044 | static void ssh_special (Telnet_Special code) { |
6abbf9e3 |
4045 | if (code == TS_EOF) { |
3687d221 |
4046 | if (ssh_state != SSH_STATE_SESSION) { |
4047 | /* |
4048 | * Buffer the EOF in case we are pre-SESSION, so we can |
4049 | * send it as soon as we reach SESSION. |
4050 | */ |
4051 | if (code == TS_EOF) |
4052 | eof_needed = TRUE; |
4053 | return; |
4054 | } |
67779be7 |
4055 | if (ssh_version == 1) { |
6abbf9e3 |
4056 | send_packet(SSH1_CMSG_EOF, PKT_END); |
4057 | } else { |
4058 | ssh2_pkt_init(SSH2_MSG_CHANNEL_EOF); |
d211621f |
4059 | ssh2_pkt_adduint32(mainchan->remoteid); |
6abbf9e3 |
4060 | ssh2_pkt_send(); |
4061 | } |
67779be7 |
4062 | logevent("Sent EOF message"); |
ec55b220 |
4063 | } else if (code == TS_PING) { |
3687d221 |
4064 | if (ssh_state == SSH_STATE_CLOSED || ssh_state == SSH_STATE_PREPACKET) |
4065 | return; |
ec55b220 |
4066 | if (ssh_version == 1) { |
4067 | send_packet(SSH1_MSG_IGNORE, PKT_STR, "", PKT_END); |
4068 | } else { |
4069 | ssh2_pkt_init(SSH2_MSG_IGNORE); |
4070 | ssh2_pkt_addstring_start(); |
4071 | ssh2_pkt_send(); |
4072 | } |
6abbf9e3 |
4073 | } else { |
4074 | /* do nothing */ |
4075 | } |
374330e2 |
4076 | } |
4077 | |
8df7a775 |
4078 | static Socket ssh_socket(void) { return s; } |
8ccc75b0 |
4079 | |
4080 | static int ssh_sendok(void) { return ssh_send_ok; } |
fb09bf1c |
4081 | |
0965bee0 |
4082 | static int ssh_ldisc(int option) { |
4083 | if (option == LD_ECHO) return ssh_echoing; |
4084 | if (option == LD_EDIT) return ssh_editing; |
4085 | return FALSE; |
4086 | } |
4087 | |
374330e2 |
4088 | Backend ssh_backend = { |
4089 | ssh_init, |
374330e2 |
4090 | ssh_send, |
4091 | ssh_size, |
4017be6d |
4092 | ssh_special, |
8ccc75b0 |
4093 | ssh_socket, |
97db3be4 |
4094 | ssh_sendok, |
0965bee0 |
4095 | ssh_ldisc, |
97db3be4 |
4096 | 22 |
374330e2 |
4097 | }; |