374330e2 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
fb09bf1c |
3 | #include <stdarg.h> |
4 | #include <assert.h> |
374330e2 |
5 | |
6 | #include "putty.h" |
dacbd0e8 |
7 | #include "tree234.h" |
fb09bf1c |
8 | #include "ssh.h" |
374330e2 |
9 | |
10 | #ifndef FALSE |
11 | #define FALSE 0 |
12 | #endif |
13 | #ifndef TRUE |
14 | #define TRUE 1 |
15 | #endif |
16 | |
32874aea |
17 | #define SSH1_MSG_DISCONNECT 1 /* 0x1 */ |
18 | #define SSH1_SMSG_PUBLIC_KEY 2 /* 0x2 */ |
19 | #define SSH1_CMSG_SESSION_KEY 3 /* 0x3 */ |
20 | #define SSH1_CMSG_USER 4 /* 0x4 */ |
21 | #define SSH1_CMSG_AUTH_RSA 6 /* 0x6 */ |
22 | #define SSH1_SMSG_AUTH_RSA_CHALLENGE 7 /* 0x7 */ |
23 | #define SSH1_CMSG_AUTH_RSA_RESPONSE 8 /* 0x8 */ |
24 | #define SSH1_CMSG_AUTH_PASSWORD 9 /* 0x9 */ |
25 | #define SSH1_CMSG_REQUEST_PTY 10 /* 0xa */ |
26 | #define SSH1_CMSG_WINDOW_SIZE 11 /* 0xb */ |
27 | #define SSH1_CMSG_EXEC_SHELL 12 /* 0xc */ |
28 | #define SSH1_CMSG_EXEC_CMD 13 /* 0xd */ |
29 | #define SSH1_SMSG_SUCCESS 14 /* 0xe */ |
30 | #define SSH1_SMSG_FAILURE 15 /* 0xf */ |
31 | #define SSH1_CMSG_STDIN_DATA 16 /* 0x10 */ |
32 | #define SSH1_SMSG_STDOUT_DATA 17 /* 0x11 */ |
33 | #define SSH1_SMSG_STDERR_DATA 18 /* 0x12 */ |
34 | #define SSH1_CMSG_EOF 19 /* 0x13 */ |
35 | #define SSH1_SMSG_EXIT_STATUS 20 /* 0x14 */ |
36 | #define SSH1_MSG_CHANNEL_OPEN_CONFIRMATION 21 /* 0x15 */ |
37 | #define SSH1_MSG_CHANNEL_OPEN_FAILURE 22 /* 0x16 */ |
38 | #define SSH1_MSG_CHANNEL_DATA 23 /* 0x17 */ |
39 | #define SSH1_MSG_CHANNEL_CLOSE 24 /* 0x18 */ |
40 | #define SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION 25 /* 0x19 */ |
41 | #define SSH1_SMSG_X11_OPEN 27 /* 0x1b */ |
42 | #define SSH1_CMSG_PORT_FORWARD_REQUEST 28 /* 0x1c */ |
43 | #define SSH1_MSG_PORT_OPEN 29 /* 0x1d */ |
44 | #define SSH1_CMSG_AGENT_REQUEST_FORWARDING 30 /* 0x1e */ |
45 | #define SSH1_SMSG_AGENT_OPEN 31 /* 0x1f */ |
46 | #define SSH1_MSG_IGNORE 32 /* 0x20 */ |
47 | #define SSH1_CMSG_EXIT_CONFIRMATION 33 /* 0x21 */ |
48 | #define SSH1_CMSG_X11_REQUEST_FORWARDING 34 /* 0x22 */ |
49 | #define SSH1_CMSG_AUTH_RHOSTS_RSA 35 /* 0x23 */ |
50 | #define SSH1_MSG_DEBUG 36 /* 0x24 */ |
51 | #define SSH1_CMSG_REQUEST_COMPRESSION 37 /* 0x25 */ |
52 | #define SSH1_CMSG_AUTH_TIS 39 /* 0x27 */ |
53 | #define SSH1_SMSG_AUTH_TIS_CHALLENGE 40 /* 0x28 */ |
54 | #define SSH1_CMSG_AUTH_TIS_RESPONSE 41 /* 0x29 */ |
55 | #define SSH1_CMSG_AUTH_CCARD 70 /* 0x46 */ |
56 | #define SSH1_SMSG_AUTH_CCARD_CHALLENGE 71 /* 0x47 */ |
57 | #define SSH1_CMSG_AUTH_CCARD_RESPONSE 72 /* 0x48 */ |
58 | |
59 | #define SSH1_AUTH_TIS 5 /* 0x5 */ |
60 | #define SSH1_AUTH_CCARD 16 /* 0x10 */ |
61 | |
62 | #define SSH1_PROTOFLAG_SCREEN_NUMBER 1 /* 0x1 */ |
b96dc54c |
63 | /* Mask for protoflags we will echo back to server if seen */ |
32874aea |
64 | #define SSH1_PROTOFLAGS_SUPPORTED 0 /* 0x1 */ |
65 | |
66 | #define SSH2_MSG_DISCONNECT 1 /* 0x1 */ |
67 | #define SSH2_MSG_IGNORE 2 /* 0x2 */ |
68 | #define SSH2_MSG_UNIMPLEMENTED 3 /* 0x3 */ |
69 | #define SSH2_MSG_DEBUG 4 /* 0x4 */ |
70 | #define SSH2_MSG_SERVICE_REQUEST 5 /* 0x5 */ |
71 | #define SSH2_MSG_SERVICE_ACCEPT 6 /* 0x6 */ |
72 | #define SSH2_MSG_KEXINIT 20 /* 0x14 */ |
73 | #define SSH2_MSG_NEWKEYS 21 /* 0x15 */ |
74 | #define SSH2_MSG_KEXDH_INIT 30 /* 0x1e */ |
75 | #define SSH2_MSG_KEXDH_REPLY 31 /* 0x1f */ |
76 | #define SSH2_MSG_KEX_DH_GEX_REQUEST 30 /* 0x1e */ |
77 | #define SSH2_MSG_KEX_DH_GEX_GROUP 31 /* 0x1f */ |
78 | #define SSH2_MSG_KEX_DH_GEX_INIT 32 /* 0x20 */ |
79 | #define SSH2_MSG_KEX_DH_GEX_REPLY 33 /* 0x21 */ |
80 | #define SSH2_MSG_USERAUTH_REQUEST 50 /* 0x32 */ |
81 | #define SSH2_MSG_USERAUTH_FAILURE 51 /* 0x33 */ |
82 | #define SSH2_MSG_USERAUTH_SUCCESS 52 /* 0x34 */ |
83 | #define SSH2_MSG_USERAUTH_BANNER 53 /* 0x35 */ |
84 | #define SSH2_MSG_USERAUTH_PK_OK 60 /* 0x3c */ |
85 | #define SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 /* 0x3c */ |
761187b6 |
86 | #define SSH2_MSG_USERAUTH_INFO_REQUEST 60 /* 0x3c */ |
87 | #define SSH2_MSG_USERAUTH_INFO_RESPONSE 61 /* 0x3d */ |
32874aea |
88 | #define SSH2_MSG_GLOBAL_REQUEST 80 /* 0x50 */ |
89 | #define SSH2_MSG_REQUEST_SUCCESS 81 /* 0x51 */ |
90 | #define SSH2_MSG_REQUEST_FAILURE 82 /* 0x52 */ |
91 | #define SSH2_MSG_CHANNEL_OPEN 90 /* 0x5a */ |
92 | #define SSH2_MSG_CHANNEL_OPEN_CONFIRMATION 91 /* 0x5b */ |
93 | #define SSH2_MSG_CHANNEL_OPEN_FAILURE 92 /* 0x5c */ |
94 | #define SSH2_MSG_CHANNEL_WINDOW_ADJUST 93 /* 0x5d */ |
95 | #define SSH2_MSG_CHANNEL_DATA 94 /* 0x5e */ |
96 | #define SSH2_MSG_CHANNEL_EXTENDED_DATA 95 /* 0x5f */ |
97 | #define SSH2_MSG_CHANNEL_EOF 96 /* 0x60 */ |
98 | #define SSH2_MSG_CHANNEL_CLOSE 97 /* 0x61 */ |
99 | #define SSH2_MSG_CHANNEL_REQUEST 98 /* 0x62 */ |
100 | #define SSH2_MSG_CHANNEL_SUCCESS 99 /* 0x63 */ |
101 | #define SSH2_MSG_CHANNEL_FAILURE 100 /* 0x64 */ |
102 | |
00db133f |
103 | /* |
104 | * Packet type contexts, so that ssh2_pkt_type can correctly decode |
105 | * the ambiguous type numbers back into the correct type strings. |
106 | */ |
107 | #define SSH2_PKTCTX_DHGROUP1 0x0001 |
108 | #define SSH2_PKTCTX_DHGEX 0x0002 |
109 | #define SSH2_PKTCTX_PUBLICKEY 0x0010 |
110 | #define SSH2_PKTCTX_PASSWORD 0x0020 |
111 | #define SSH2_PKTCTX_KBDINTER 0x0040 |
112 | #define SSH2_PKTCTX_AUTH_MASK 0x00F0 |
113 | |
32874aea |
114 | #define SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 /* 0x1 */ |
115 | #define SSH2_DISCONNECT_PROTOCOL_ERROR 2 /* 0x2 */ |
116 | #define SSH2_DISCONNECT_KEY_EXCHANGE_FAILED 3 /* 0x3 */ |
117 | #define SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED 4 /* 0x4 */ |
118 | #define SSH2_DISCONNECT_MAC_ERROR 5 /* 0x5 */ |
119 | #define SSH2_DISCONNECT_COMPRESSION_ERROR 6 /* 0x6 */ |
120 | #define SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE 7 /* 0x7 */ |
121 | #define SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8 /* 0x8 */ |
122 | #define SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9 /* 0x9 */ |
123 | #define SSH2_DISCONNECT_CONNECTION_LOST 10 /* 0xa */ |
124 | #define SSH2_DISCONNECT_BY_APPLICATION 11 /* 0xb */ |
125 | #define SSH2_DISCONNECT_TOO_MANY_CONNECTIONS 12 /* 0xc */ |
126 | #define SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER 13 /* 0xd */ |
127 | #define SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14 /* 0xe */ |
128 | #define SSH2_DISCONNECT_ILLEGAL_USER_NAME 15 /* 0xf */ |
38c4a8da |
129 | |
130 | static const char *const ssh2_disconnect_reasons[] = { |
131 | NULL, |
132 | "SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT", |
133 | "SSH_DISCONNECT_PROTOCOL_ERROR", |
134 | "SSH_DISCONNECT_KEY_EXCHANGE_FAILED", |
135 | "SSH_DISCONNECT_HOST_AUTHENTICATION_FAILED", |
136 | "SSH_DISCONNECT_MAC_ERROR", |
137 | "SSH_DISCONNECT_COMPRESSION_ERROR", |
138 | "SSH_DISCONNECT_SERVICE_NOT_AVAILABLE", |
139 | "SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED", |
140 | "SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE", |
141 | "SSH_DISCONNECT_CONNECTION_LOST", |
142 | "SSH_DISCONNECT_BY_APPLICATION", |
143 | "SSH_DISCONNECT_TOO_MANY_CONNECTIONS", |
144 | "SSH_DISCONNECT_AUTH_CANCELLED_BY_USER", |
145 | "SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE", |
146 | "SSH_DISCONNECT_ILLEGAL_USER_NAME", |
147 | }; |
9005f3ba |
148 | |
32874aea |
149 | #define SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED 1 /* 0x1 */ |
150 | #define SSH2_OPEN_CONNECT_FAILED 2 /* 0x2 */ |
151 | #define SSH2_OPEN_UNKNOWN_CHANNEL_TYPE 3 /* 0x3 */ |
152 | #define SSH2_OPEN_RESOURCE_SHORTAGE 4 /* 0x4 */ |
d211621f |
153 | |
32874aea |
154 | #define SSH2_EXTENDED_DATA_STDERR 1 /* 0x1 */ |
fb09bf1c |
155 | |
7d503c31 |
156 | /* |
157 | * Various remote-bug flags. |
158 | */ |
159 | #define BUG_CHOKES_ON_SSH1_IGNORE 1 |
160 | #define BUG_SSH2_HMAC 2 |
bd358db1 |
161 | #define BUG_NEEDS_SSH1_PLAIN_PASSWORD 4 |
0df73905 |
162 | #define BUG_CHOKES_ON_RSA 8 |
1dd353b5 |
163 | #define BUG_SSH2_RSA_PADDING 16 |
088bde77 |
164 | #define BUG_SSH2_DERIVEKEY 32 |
8e975795 |
165 | #define BUG_SSH2_DH_GEX 64 |
bd358db1 |
166 | |
00db133f |
167 | #define translate(x) if (type == x) return #x |
51470298 |
168 | #define translatec(x,ctx) if (type == x && (pkt_ctx & ctx)) return #x |
ae9ae89f |
169 | static char *ssh1_pkt_type(int type) |
00db133f |
170 | { |
171 | translate(SSH1_MSG_DISCONNECT); |
172 | translate(SSH1_SMSG_PUBLIC_KEY); |
173 | translate(SSH1_CMSG_SESSION_KEY); |
174 | translate(SSH1_CMSG_USER); |
175 | translate(SSH1_CMSG_AUTH_RSA); |
176 | translate(SSH1_SMSG_AUTH_RSA_CHALLENGE); |
177 | translate(SSH1_CMSG_AUTH_RSA_RESPONSE); |
178 | translate(SSH1_CMSG_AUTH_PASSWORD); |
179 | translate(SSH1_CMSG_REQUEST_PTY); |
180 | translate(SSH1_CMSG_WINDOW_SIZE); |
181 | translate(SSH1_CMSG_EXEC_SHELL); |
182 | translate(SSH1_CMSG_EXEC_CMD); |
183 | translate(SSH1_SMSG_SUCCESS); |
184 | translate(SSH1_SMSG_FAILURE); |
185 | translate(SSH1_CMSG_STDIN_DATA); |
186 | translate(SSH1_SMSG_STDOUT_DATA); |
187 | translate(SSH1_SMSG_STDERR_DATA); |
188 | translate(SSH1_CMSG_EOF); |
189 | translate(SSH1_SMSG_EXIT_STATUS); |
190 | translate(SSH1_MSG_CHANNEL_OPEN_CONFIRMATION); |
191 | translate(SSH1_MSG_CHANNEL_OPEN_FAILURE); |
192 | translate(SSH1_MSG_CHANNEL_DATA); |
193 | translate(SSH1_MSG_CHANNEL_CLOSE); |
194 | translate(SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION); |
195 | translate(SSH1_SMSG_X11_OPEN); |
196 | translate(SSH1_CMSG_PORT_FORWARD_REQUEST); |
197 | translate(SSH1_MSG_PORT_OPEN); |
198 | translate(SSH1_CMSG_AGENT_REQUEST_FORWARDING); |
199 | translate(SSH1_SMSG_AGENT_OPEN); |
200 | translate(SSH1_MSG_IGNORE); |
201 | translate(SSH1_CMSG_EXIT_CONFIRMATION); |
202 | translate(SSH1_CMSG_X11_REQUEST_FORWARDING); |
203 | translate(SSH1_CMSG_AUTH_RHOSTS_RSA); |
204 | translate(SSH1_MSG_DEBUG); |
205 | translate(SSH1_CMSG_REQUEST_COMPRESSION); |
206 | translate(SSH1_CMSG_AUTH_TIS); |
207 | translate(SSH1_SMSG_AUTH_TIS_CHALLENGE); |
208 | translate(SSH1_CMSG_AUTH_TIS_RESPONSE); |
209 | translate(SSH1_CMSG_AUTH_CCARD); |
210 | translate(SSH1_SMSG_AUTH_CCARD_CHALLENGE); |
211 | translate(SSH1_CMSG_AUTH_CCARD_RESPONSE); |
212 | return "unknown"; |
213 | } |
ae9ae89f |
214 | static char *ssh2_pkt_type(int pkt_ctx, int type) |
00db133f |
215 | { |
216 | translate(SSH2_MSG_DISCONNECT); |
217 | translate(SSH2_MSG_IGNORE); |
218 | translate(SSH2_MSG_UNIMPLEMENTED); |
219 | translate(SSH2_MSG_DEBUG); |
220 | translate(SSH2_MSG_SERVICE_REQUEST); |
221 | translate(SSH2_MSG_SERVICE_ACCEPT); |
222 | translate(SSH2_MSG_KEXINIT); |
223 | translate(SSH2_MSG_NEWKEYS); |
224 | translatec(SSH2_MSG_KEXDH_INIT, SSH2_PKTCTX_DHGROUP1); |
225 | translatec(SSH2_MSG_KEXDH_REPLY, SSH2_PKTCTX_DHGROUP1); |
226 | translatec(SSH2_MSG_KEX_DH_GEX_REQUEST, SSH2_PKTCTX_DHGEX); |
227 | translatec(SSH2_MSG_KEX_DH_GEX_GROUP, SSH2_PKTCTX_DHGEX); |
228 | translatec(SSH2_MSG_KEX_DH_GEX_INIT, SSH2_PKTCTX_DHGEX); |
229 | translatec(SSH2_MSG_KEX_DH_GEX_REPLY, SSH2_PKTCTX_DHGEX); |
230 | translate(SSH2_MSG_USERAUTH_REQUEST); |
231 | translate(SSH2_MSG_USERAUTH_FAILURE); |
232 | translate(SSH2_MSG_USERAUTH_SUCCESS); |
233 | translate(SSH2_MSG_USERAUTH_BANNER); |
234 | translatec(SSH2_MSG_USERAUTH_PK_OK, SSH2_PKTCTX_PUBLICKEY); |
235 | translatec(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, SSH2_PKTCTX_PASSWORD); |
236 | translatec(SSH2_MSG_USERAUTH_INFO_REQUEST, SSH2_PKTCTX_KBDINTER); |
237 | translatec(SSH2_MSG_USERAUTH_INFO_RESPONSE, SSH2_PKTCTX_KBDINTER); |
238 | translate(SSH2_MSG_GLOBAL_REQUEST); |
239 | translate(SSH2_MSG_REQUEST_SUCCESS); |
240 | translate(SSH2_MSG_REQUEST_FAILURE); |
241 | translate(SSH2_MSG_CHANNEL_OPEN); |
242 | translate(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
243 | translate(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
244 | translate(SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
245 | translate(SSH2_MSG_CHANNEL_DATA); |
246 | translate(SSH2_MSG_CHANNEL_EXTENDED_DATA); |
247 | translate(SSH2_MSG_CHANNEL_EOF); |
248 | translate(SSH2_MSG_CHANNEL_CLOSE); |
249 | translate(SSH2_MSG_CHANNEL_REQUEST); |
250 | translate(SSH2_MSG_CHANNEL_SUCCESS); |
251 | translate(SSH2_MSG_CHANNEL_FAILURE); |
252 | return "unknown"; |
253 | } |
254 | #undef translate |
255 | #undef translatec |
7d503c31 |
256 | |
fb09bf1c |
257 | #define GET_32BIT(cp) \ |
258 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
259 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
260 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
261 | ((unsigned long)(unsigned char)(cp)[3])) |
262 | |
263 | #define PUT_32BIT(cp, value) { \ |
264 | (cp)[0] = (unsigned char)((value) >> 24); \ |
265 | (cp)[1] = (unsigned char)((value) >> 16); \ |
266 | (cp)[2] = (unsigned char)((value) >> 8); \ |
267 | (cp)[3] = (unsigned char)(value); } |
268 | |
7cca0d81 |
269 | enum { PKT_END, PKT_INT, PKT_CHAR, PKT_DATA, PKT_STR, PKT_BIGNUM }; |
972a41c8 |
270 | |
374330e2 |
271 | /* Coroutine mechanics for the sillier bits of the code */ |
51470298 |
272 | #define crBegin(v) { int *crLine = &v; switch(v) { case 0:; |
273 | #define crState(t) \ |
274 | struct t *s; \ |
275 | if (!ssh->t) ssh->t = smalloc(sizeof(struct t)); \ |
276 | s = ssh->t; |
277 | #define crFinish(z) } *crLine = 0; return (z); } |
278 | #define crFinishV } *crLine = 0; return; } |
374330e2 |
279 | #define crReturn(z) \ |
280 | do {\ |
51470298 |
281 | *crLine =__LINE__; return (z); case __LINE__:;\ |
374330e2 |
282 | } while (0) |
283 | #define crReturnV \ |
284 | do {\ |
51470298 |
285 | *crLine=__LINE__; return; case __LINE__:;\ |
374330e2 |
286 | } while (0) |
51470298 |
287 | #define crStop(z) do{ *crLine = 0; return (z); }while(0) |
288 | #define crStopV do{ *crLine = 0; return; }while(0) |
fb09bf1c |
289 | #define crWaitUntil(c) do { crReturn(0); } while (!(c)) |
7cca0d81 |
290 | #define crWaitUntilV(c) do { crReturnV; } while (!(c)) |
374330e2 |
291 | |
51470298 |
292 | typedef struct ssh_tag *Ssh; |
293 | |
51470298 |
294 | static void ssh2_pkt_init(Ssh, int pkt_type); |
295 | static void ssh2_pkt_addbool(Ssh, unsigned char value); |
296 | static void ssh2_pkt_adduint32(Ssh, unsigned long value); |
297 | static void ssh2_pkt_addstring_start(Ssh); |
298 | static void ssh2_pkt_addstring_str(Ssh, char *data); |
299 | static void ssh2_pkt_addstring_data(Ssh, char *data, int len); |
300 | static void ssh2_pkt_addstring(Ssh, char *data); |
d8baa528 |
301 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len); |
51470298 |
302 | static void ssh2_pkt_addmp(Ssh, Bignum b); |
303 | static int ssh2_pkt_construct(Ssh); |
304 | static void ssh2_pkt_send(Ssh); |
3d63ca2e |
305 | |
5471d09a |
306 | /* |
307 | * Buffer management constants. There are several of these for |
308 | * various different purposes: |
309 | * |
310 | * - SSH1_BUFFER_LIMIT is the amount of backlog that must build up |
311 | * on a local data stream before we throttle the whole SSH |
312 | * connection (in SSH1 only). Throttling the whole connection is |
313 | * pretty drastic so we set this high in the hope it won't |
314 | * happen very often. |
315 | * |
316 | * - SSH_MAX_BACKLOG is the amount of backlog that must build up |
317 | * on the SSH connection itself before we defensively throttle |
318 | * _all_ local data streams. This is pretty drastic too (though |
319 | * thankfully unlikely in SSH2 since the window mechanism should |
320 | * ensure that the server never has any need to throttle its end |
321 | * of the connection), so we set this high as well. |
322 | * |
323 | * - OUR_V2_WINSIZE is the maximum window size we present on SSH2 |
324 | * channels. |
325 | */ |
326 | |
327 | #define SSH1_BUFFER_LIMIT 32768 |
328 | #define SSH_MAX_BACKLOG 32768 |
329 | #define OUR_V2_WINSIZE 16384 |
d74d141c |
330 | |
a92dd380 |
331 | const static struct ssh_kex *kex_algs[] = { |
a92dd380 |
332 | &ssh_diffiehellman_gex, |
32874aea |
333 | &ssh_diffiehellman |
334 | }; |
e5574168 |
335 | |
85cc02bb |
336 | const static struct ssh_signkey *hostkey_algs[] = { &ssh_rsa, &ssh_dss }; |
e5574168 |
337 | |
e0e1a00d |
338 | static void *nullmac_make_context(void) |
339 | { |
340 | return NULL; |
341 | } |
342 | static void nullmac_free_context(void *handle) |
343 | { |
344 | } |
345 | static void nullmac_key(void *handle, unsigned char *key) |
32874aea |
346 | { |
347 | } |
e0e1a00d |
348 | static void nullmac_generate(void *handle, unsigned char *blk, int len, |
32874aea |
349 | unsigned long seq) |
350 | { |
351 | } |
e0e1a00d |
352 | static int nullmac_verify(void *handle, unsigned char *blk, int len, |
353 | unsigned long seq) |
32874aea |
354 | { |
355 | return 1; |
356 | } |
57476f6b |
357 | const static struct ssh_mac ssh_mac_none = { |
e0e1a00d |
358 | nullmac_make_context, nullmac_free_context, nullmac_key, |
359 | nullmac_generate, nullmac_verify, "none", 0 |
e5574168 |
360 | }; |
8b2715b2 |
361 | const static struct ssh_mac *macs[] = { |
32874aea |
362 | &ssh_sha1, &ssh_md5, &ssh_mac_none |
363 | }; |
8b2715b2 |
364 | const static struct ssh_mac *buggymacs[] = { |
32874aea |
365 | &ssh_sha1_buggy, &ssh_md5, &ssh_mac_none |
366 | }; |
e5574168 |
367 | |
5366aed8 |
368 | static void *ssh_comp_none_init(void) |
369 | { |
370 | return NULL; |
371 | } |
372 | static void ssh_comp_none_cleanup(void *handle) |
32874aea |
373 | { |
374 | } |
5366aed8 |
375 | static int ssh_comp_none_block(void *handle, unsigned char *block, int len, |
32874aea |
376 | unsigned char **outblock, int *outlen) |
377 | { |
378 | return 0; |
379 | } |
5366aed8 |
380 | static int ssh_comp_none_disable(void *handle) |
32874aea |
381 | { |
4ba9b64b |
382 | return 0; |
383 | } |
57476f6b |
384 | const static struct ssh_compress ssh_comp_none = { |
4ba9b64b |
385 | "none", |
5366aed8 |
386 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
387 | ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block, |
388 | ssh_comp_none_disable, NULL |
e5574168 |
389 | }; |
4ba9b64b |
390 | extern const struct ssh_compress ssh_zlib; |
391 | const static struct ssh_compress *compressions[] = { |
32874aea |
392 | &ssh_zlib, &ssh_comp_none |
393 | }; |
374330e2 |
394 | |
32874aea |
395 | enum { /* channel types */ |
783415f8 |
396 | CHAN_MAINSESSION, |
397 | CHAN_X11, |
398 | CHAN_AGENT, |
bc240b21 |
399 | CHAN_SOCKDATA, |
400 | CHAN_SOCKDATA_DORMANT /* one the remote hasn't confirmed */ |
783415f8 |
401 | }; |
402 | |
dacbd0e8 |
403 | /* |
404 | * 2-3-4 tree storing channels. |
405 | */ |
406 | struct ssh_channel { |
51470298 |
407 | Ssh ssh; /* pointer back to main context */ |
d211621f |
408 | unsigned remoteid, localid; |
dacbd0e8 |
409 | int type; |
0357890f |
410 | /* |
411 | * In SSH1, this value contains four bits: |
412 | * |
413 | * 1 We have sent SSH1_MSG_CHANNEL_CLOSE. |
414 | * 2 We have sent SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
415 | * 4 We have received SSH1_MSG_CHANNEL_CLOSE. |
416 | * 8 We have received SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION. |
417 | * |
418 | * A channel is completely finished with when all four bits are set. |
419 | */ |
dacbd0e8 |
420 | int closes; |
5471d09a |
421 | union { |
422 | struct ssh1_data_channel { |
423 | int throttling; |
424 | } v1; |
425 | struct ssh2_data_channel { |
426 | bufchain outbuffer; |
427 | unsigned remwindow, remmaxpkt; |
428 | unsigned locwindow; |
429 | } v2; |
430 | } v; |
dacbd0e8 |
431 | union { |
32874aea |
432 | struct ssh_agent_channel { |
433 | unsigned char *message; |
434 | unsigned char msglen[4]; |
435 | int lensofar, totallen; |
436 | } a; |
437 | struct ssh_x11_channel { |
438 | Socket s; |
439 | } x11; |
d74d141c |
440 | struct ssh_pfd_channel { |
441 | Socket s; |
442 | } pfd; |
dacbd0e8 |
443 | } u; |
444 | }; |
57476f6b |
445 | |
d74d141c |
446 | /* |
bc240b21 |
447 | * 2-3-4 tree storing remote->local port forwardings. SSH 1 and SSH |
448 | * 2 use this structure in different ways, reflecting SSH 2's |
449 | * altogether saner approach to port forwarding. |
450 | * |
451 | * In SSH 1, you arrange a remote forwarding by sending the server |
452 | * the remote port number, and the local destination host:port. |
453 | * When a connection comes in, the server sends you back that |
454 | * host:port pair, and you connect to it. This is a ready-made |
455 | * security hole if you're not on the ball: a malicious server |
456 | * could send you back _any_ host:port pair, so if you trustingly |
457 | * connect to the address it gives you then you've just opened the |
458 | * entire inside of your corporate network just by connecting |
459 | * through it to a dodgy SSH server. Hence, we must store a list of |
460 | * host:port pairs we _are_ trying to forward to, and reject a |
461 | * connection request from the server if it's not in the list. |
462 | * |
463 | * In SSH 2, each side of the connection minds its own business and |
464 | * doesn't send unnecessary information to the other. You arrange a |
465 | * remote forwarding by sending the server just the remote port |
466 | * number. When a connection comes in, the server tells you which |
467 | * of its ports was connected to; and _you_ have to remember what |
468 | * local host:port pair went with that port number. |
469 | * |
470 | * Hence: in SSH 1 this structure stores host:port pairs we intend |
471 | * to allow connections to, and is indexed by those host:port |
472 | * pairs. In SSH 2 it stores a mapping from source port to |
473 | * destination host:port pair, and is indexed by source port. |
d74d141c |
474 | */ |
475 | struct ssh_rportfwd { |
bc240b21 |
476 | unsigned sport, dport; |
477 | char dhost[256]; |
d74d141c |
478 | }; |
479 | |
57476f6b |
480 | struct Packet { |
481 | long length; |
482 | int type; |
483 | unsigned char *data; |
484 | unsigned char *body; |
485 | long savedpos; |
486 | long maxlen; |
487 | }; |
488 | |
51470298 |
489 | static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt); |
490 | static void ssh2_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt); |
491 | static void ssh_size(void *handle, int width, int height); |
492 | static void ssh_special(void *handle, Telnet_Special); |
5471d09a |
493 | static int ssh2_try_send(struct ssh_channel *c); |
51470298 |
494 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, int len); |
495 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize); |
5471d09a |
496 | static void ssh2_set_window(struct ssh_channel *c, unsigned newwin); |
51470298 |
497 | static int ssh_sendbuffer(void *handle); |
57476f6b |
498 | |
51470298 |
499 | struct rdpkt1_state_tag { |
57476f6b |
500 | long len, pad, biglen, to_read; |
501 | unsigned long realcrc, gotcrc; |
502 | unsigned char *p; |
503 | int i; |
504 | int chunk; |
51470298 |
505 | }; |
57476f6b |
506 | |
51470298 |
507 | struct rdpkt2_state_tag { |
960e736a |
508 | long len, pad, payload, packetlen, maclen; |
509 | int i; |
510 | int cipherblk; |
511 | unsigned long incoming_sequence; |
51470298 |
512 | }; |
513 | |
514 | struct ssh_tag { |
515 | const struct plug_function_table *fn; |
516 | /* the above field _must_ be first in the structure */ |
517 | |
518 | SHA_State exhash, exhashbase; |
519 | |
520 | Socket s; |
521 | |
b9d7bcad |
522 | void *ldisc; |
a8327734 |
523 | void *logctx; |
b9d7bcad |
524 | |
51470298 |
525 | unsigned char session_key[32]; |
526 | int v1_compressing; |
527 | int v1_remote_protoflags; |
528 | int v1_local_protoflags; |
529 | int agentfwd_enabled; |
530 | int X11_fwd_enabled; |
531 | int remote_bugs; |
532 | const struct ssh_cipher *cipher; |
371e569c |
533 | void *v1_cipher_ctx; |
0183b242 |
534 | void *crcda_ctx; |
51470298 |
535 | const struct ssh2_cipher *cscipher, *sccipher; |
371e569c |
536 | void *cs_cipher_ctx, *sc_cipher_ctx; |
51470298 |
537 | const struct ssh_mac *csmac, *scmac; |
e0e1a00d |
538 | void *cs_mac_ctx, *sc_mac_ctx; |
51470298 |
539 | const struct ssh_compress *cscomp, *sccomp; |
5366aed8 |
540 | void *cs_comp_ctx, *sc_comp_ctx; |
51470298 |
541 | const struct ssh_kex *kex; |
542 | const struct ssh_signkey *hostkey; |
543 | unsigned char v2_session_id[20]; |
27cd7fc2 |
544 | void *kex_ctx; |
51470298 |
545 | |
546 | char *savedhost; |
547 | int savedport; |
548 | int send_ok; |
549 | int echoing, editing; |
550 | |
551 | void *frontend; |
552 | |
553 | int term_width, term_height; |
554 | |
555 | tree234 *channels; /* indexed by local id */ |
556 | struct ssh_channel *mainchan; /* primary session channel */ |
557 | int exitcode; |
558 | |
559 | tree234 *rportfwds; |
560 | |
561 | enum { |
562 | SSH_STATE_PREPACKET, |
563 | SSH_STATE_BEFORE_SIZE, |
564 | SSH_STATE_INTERMED, |
565 | SSH_STATE_SESSION, |
566 | SSH_STATE_CLOSED |
567 | } state; |
568 | |
569 | int size_needed, eof_needed; |
570 | |
571 | struct Packet pktin; |
572 | struct Packet pktout; |
573 | unsigned char *deferred_send_data; |
574 | int deferred_len, deferred_size; |
575 | |
576 | /* |
577 | * Gross hack: pscp will try to start SFTP but fall back to |
578 | * scp1 if that fails. This variable is the means by which |
579 | * scp.c can reach into the SSH code and find out which one it |
580 | * got. |
581 | */ |
582 | int fallback_cmd; |
583 | |
584 | /* |
585 | * Used for username and password input. |
586 | */ |
587 | char *userpass_input_buffer; |
588 | int userpass_input_buflen; |
589 | int userpass_input_bufpos; |
590 | int userpass_input_echo; |
591 | |
592 | char *portfwd_strptr; |
593 | int pkt_ctx; |
594 | |
302121de |
595 | void *x11auth; |
596 | |
51470298 |
597 | int version; |
598 | int v1_throttle_count; |
599 | int overall_bufsize; |
600 | int throttled_all; |
601 | int v1_stdout_throttling; |
602 | int v2_outgoing_sequence; |
603 | |
604 | int ssh1_rdpkt_crstate; |
605 | int ssh2_rdpkt_crstate; |
606 | int do_ssh_init_crstate; |
607 | int ssh_gotdata_crstate; |
608 | int ssh1_protocol_crstate; |
609 | int do_ssh1_login_crstate; |
610 | int do_ssh2_transport_crstate; |
611 | int do_ssh2_authconn_crstate; |
612 | |
613 | void *do_ssh_init_state; |
614 | void *do_ssh1_login_state; |
615 | void *do_ssh2_transport_state; |
616 | void *do_ssh2_authconn_state; |
617 | |
618 | struct rdpkt1_state_tag rdpkt1_state; |
619 | struct rdpkt2_state_tag rdpkt2_state; |
620 | |
621 | void (*protocol) (Ssh ssh, unsigned char *in, int inlen, int ispkt); |
622 | int (*s_rdpkt) (Ssh ssh, unsigned char **data, int *datalen); |
623 | }; |
960e736a |
624 | |
382908ad |
625 | #define logevent(s) logevent(ssh->frontend, s) |
a8327734 |
626 | |
627 | /* logevent, only printf-formatted. */ |
ae9ae89f |
628 | static void logeventf(Ssh ssh, char *fmt, ...) |
a8327734 |
629 | { |
630 | va_list ap; |
57356d63 |
631 | char *buf; |
a8327734 |
632 | |
633 | va_start(ap, fmt); |
57356d63 |
634 | buf = dupvprintf(fmt, ap); |
a8327734 |
635 | va_end(ap); |
57356d63 |
636 | logevent(buf); |
57356d63 |
637 | sfree(buf); |
a8327734 |
638 | } |
639 | |
640 | #define bombout(msg) ( ssh->state = SSH_STATE_CLOSED, \ |
641 | (ssh->s ? sk_close(ssh->s), ssh->s = NULL : 0), \ |
642 | logeventf msg, connection_fatal msg ) |
643 | |
32874aea |
644 | static int ssh_channelcmp(void *av, void *bv) |
645 | { |
646 | struct ssh_channel *a = (struct ssh_channel *) av; |
647 | struct ssh_channel *b = (struct ssh_channel *) bv; |
648 | if (a->localid < b->localid) |
649 | return -1; |
650 | if (a->localid > b->localid) |
651 | return +1; |
dacbd0e8 |
652 | return 0; |
653 | } |
32874aea |
654 | static int ssh_channelfind(void *av, void *bv) |
655 | { |
656 | unsigned *a = (unsigned *) av; |
657 | struct ssh_channel *b = (struct ssh_channel *) bv; |
658 | if (*a < b->localid) |
659 | return -1; |
660 | if (*a > b->localid) |
661 | return +1; |
dacbd0e8 |
662 | return 0; |
663 | } |
664 | |
bc240b21 |
665 | static int ssh_rportcmp_ssh1(void *av, void *bv) |
d74d141c |
666 | { |
667 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
668 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
669 | int i; |
bc240b21 |
670 | if ( (i = strcmp(a->dhost, b->dhost)) != 0) |
d74d141c |
671 | return i < 0 ? -1 : +1; |
bc240b21 |
672 | if (a->dport > b->dport) |
673 | return +1; |
674 | if (a->dport < b->dport) |
675 | return -1; |
676 | return 0; |
677 | } |
678 | |
679 | static int ssh_rportcmp_ssh2(void *av, void *bv) |
680 | { |
681 | struct ssh_rportfwd *a = (struct ssh_rportfwd *) av; |
682 | struct ssh_rportfwd *b = (struct ssh_rportfwd *) bv; |
cdcbdf3b |
683 | |
bc240b21 |
684 | if (a->sport > b->sport) |
d74d141c |
685 | return +1; |
bc240b21 |
686 | if (a->sport < b->sport) |
687 | return -1; |
d74d141c |
688 | return 0; |
689 | } |
690 | |
51470298 |
691 | static int alloc_channel_id(Ssh ssh) |
32874aea |
692 | { |
260f3dec |
693 | const unsigned CHANNEL_NUMBER_OFFSET = 256; |
694 | unsigned low, high, mid; |
d2371c81 |
695 | int tsize; |
696 | struct ssh_channel *c; |
697 | |
698 | /* |
699 | * First-fit allocation of channel numbers: always pick the |
700 | * lowest unused one. To do this, binary-search using the |
701 | * counted B-tree to find the largest channel ID which is in a |
702 | * contiguous sequence from the beginning. (Precisely |
703 | * everything in that sequence must have ID equal to its tree |
704 | * index plus CHANNEL_NUMBER_OFFSET.) |
705 | */ |
51470298 |
706 | tsize = count234(ssh->channels); |
d2371c81 |
707 | |
32874aea |
708 | low = -1; |
709 | high = tsize; |
d2371c81 |
710 | while (high - low > 1) { |
711 | mid = (high + low) / 2; |
51470298 |
712 | c = index234(ssh->channels, mid); |
d2371c81 |
713 | if (c->localid == mid + CHANNEL_NUMBER_OFFSET) |
714 | low = mid; /* this one is fine */ |
715 | else |
716 | high = mid; /* this one is past it */ |
717 | } |
718 | /* |
719 | * Now low points to either -1, or the tree index of the |
720 | * largest ID in the initial sequence. |
721 | */ |
722 | { |
723 | unsigned i = low + 1 + CHANNEL_NUMBER_OFFSET; |
51470298 |
724 | assert(NULL == find234(ssh->channels, &i, ssh_channelfind)); |
d2371c81 |
725 | } |
726 | return low + 1 + CHANNEL_NUMBER_OFFSET; |
727 | } |
728 | |
51470298 |
729 | static void c_write(Ssh ssh, char *buf, int len) |
32874aea |
730 | { |
67779be7 |
731 | if ((flags & FLAG_STDERR)) { |
32874aea |
732 | int i; |
733 | for (i = 0; i < len; i++) |
734 | if (buf[i] != '\r') |
735 | fputc(buf[i], stderr); |
fb09bf1c |
736 | return; |
737 | } |
51470298 |
738 | from_backend(ssh->frontend, 1, buf, len); |
3bdaf79d |
739 | } |
740 | |
51470298 |
741 | static void c_write_untrusted(Ssh ssh, char *buf, int len) |
32874aea |
742 | { |
a209e957 |
743 | int i; |
744 | for (i = 0; i < len; i++) { |
32874aea |
745 | if (buf[i] == '\n') |
51470298 |
746 | c_write(ssh, "\r\n", 2); |
32874aea |
747 | else if ((buf[i] & 0x60) || (buf[i] == '\r')) |
51470298 |
748 | c_write(ssh, buf + i, 1); |
a209e957 |
749 | } |
750 | } |
751 | |
51470298 |
752 | static void c_write_str(Ssh ssh, char *buf) |
32874aea |
753 | { |
51470298 |
754 | c_write(ssh, buf, strlen(buf)); |
1408a877 |
755 | } |
756 | |
fb09bf1c |
757 | /* |
758 | * Collect incoming data in the incoming packet buffer. |
e5574168 |
759 | * Decipher and verify the packet when it is completely read. |
760 | * Drop SSH1_MSG_DEBUG and SSH1_MSG_IGNORE packets. |
fb09bf1c |
761 | * Update the *data and *datalen variables. |
762 | * Return the additional nr of bytes needed, or 0 when |
763 | * a complete packet is available. |
764 | */ |
51470298 |
765 | static int ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
fb09bf1c |
766 | { |
51470298 |
767 | struct rdpkt1_state_tag *st = &ssh->rdpkt1_state; |
374330e2 |
768 | |
51470298 |
769 | crBegin(ssh->ssh1_rdpkt_crstate); |
374330e2 |
770 | |
32874aea |
771 | next_packet: |
37508af4 |
772 | |
51470298 |
773 | ssh->pktin.type = 0; |
774 | ssh->pktin.length = 0; |
374330e2 |
775 | |
57476f6b |
776 | for (st->i = st->len = 0; st->i < 4; st->i++) { |
fb09bf1c |
777 | while ((*datalen) == 0) |
32874aea |
778 | crReturn(4 - st->i); |
57476f6b |
779 | st->len = (st->len << 8) + **data; |
fb09bf1c |
780 | (*data)++, (*datalen)--; |
781 | } |
374330e2 |
782 | |
57476f6b |
783 | st->pad = 8 - (st->len % 8); |
784 | st->biglen = st->len + st->pad; |
51470298 |
785 | ssh->pktin.length = st->len - 5; |
fb09bf1c |
786 | |
51470298 |
787 | if (ssh->pktin.maxlen < st->biglen) { |
788 | ssh->pktin.maxlen = st->biglen; |
789 | ssh->pktin.data = srealloc(ssh->pktin.data, st->biglen + APIEXTRA); |
fb09bf1c |
790 | } |
374330e2 |
791 | |
57476f6b |
792 | st->to_read = st->biglen; |
51470298 |
793 | st->p = ssh->pktin.data; |
57476f6b |
794 | while (st->to_read > 0) { |
32874aea |
795 | st->chunk = st->to_read; |
fb09bf1c |
796 | while ((*datalen) == 0) |
57476f6b |
797 | crReturn(st->to_read); |
798 | if (st->chunk > (*datalen)) |
799 | st->chunk = (*datalen); |
800 | memcpy(st->p, *data, st->chunk); |
801 | *data += st->chunk; |
802 | *datalen -= st->chunk; |
803 | st->p += st->chunk; |
804 | st->to_read -= st->chunk; |
fb09bf1c |
805 | } |
374330e2 |
806 | |
0183b242 |
807 | if (ssh->cipher && detect_attack(ssh->crcda_ctx, ssh->pktin.data, |
808 | st->biglen, NULL)) { |
a8327734 |
809 | bombout((ssh,"Network attack (CRC compensation) detected!")); |
9a3a93a5 |
810 | crReturn(0); |
811 | } |
812 | |
51470298 |
813 | if (ssh->cipher) |
371e569c |
814 | ssh->cipher->decrypt(ssh->v1_cipher_ctx, ssh->pktin.data, st->biglen); |
374330e2 |
815 | |
51470298 |
816 | st->realcrc = crc32(ssh->pktin.data, st->biglen - 4); |
817 | st->gotcrc = GET_32BIT(ssh->pktin.data + st->biglen - 4); |
57476f6b |
818 | if (st->gotcrc != st->realcrc) { |
a8327734 |
819 | bombout((ssh,"Incorrect CRC received on packet")); |
32874aea |
820 | crReturn(0); |
fb09bf1c |
821 | } |
572f871e |
822 | |
51470298 |
823 | ssh->pktin.body = ssh->pktin.data + st->pad + 1; |
4ba9b64b |
824 | |
51470298 |
825 | if (ssh->v1_compressing) { |
4ba9b64b |
826 | unsigned char *decompblk; |
827 | int decomplen; |
5366aed8 |
828 | zlib_decompress_block(ssh->sc_comp_ctx, |
829 | ssh->pktin.body - 1, ssh->pktin.length + 1, |
4ba9b64b |
830 | &decompblk, &decomplen); |
831 | |
51470298 |
832 | if (ssh->pktin.maxlen < st->pad + decomplen) { |
833 | ssh->pktin.maxlen = st->pad + decomplen; |
834 | ssh->pktin.data = srealloc(ssh->pktin.data, |
835 | ssh->pktin.maxlen + APIEXTRA); |
836 | ssh->pktin.body = ssh->pktin.data + st->pad + 1; |
4ba9b64b |
837 | } |
838 | |
51470298 |
839 | memcpy(ssh->pktin.body - 1, decompblk, decomplen); |
dcbde236 |
840 | sfree(decompblk); |
51470298 |
841 | ssh->pktin.length = decomplen - 1; |
4ba9b64b |
842 | } |
843 | |
51470298 |
844 | ssh->pktin.type = ssh->pktin.body[-1]; |
00db133f |
845 | |
a8327734 |
846 | if (ssh->logctx) |
847 | log_packet(ssh->logctx, |
848 | PKT_INCOMING, ssh->pktin.type, |
849 | ssh1_pkt_type(ssh->pktin.type), |
850 | ssh->pktin.body, ssh->pktin.length); |
00db133f |
851 | |
51470298 |
852 | if (ssh->pktin.type == SSH1_SMSG_STDOUT_DATA || |
853 | ssh->pktin.type == SSH1_SMSG_STDERR_DATA || |
854 | ssh->pktin.type == SSH1_MSG_DEBUG || |
855 | ssh->pktin.type == SSH1_SMSG_AUTH_TIS_CHALLENGE || |
856 | ssh->pktin.type == SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
857 | long stringlen = GET_32BIT(ssh->pktin.body); |
858 | if (stringlen + 4 != ssh->pktin.length) { |
a8327734 |
859 | bombout((ssh,"Received data packet with bogus string length")); |
32874aea |
860 | crReturn(0); |
861 | } |
fb09bf1c |
862 | } |
863 | |
51470298 |
864 | if (ssh->pktin.type == SSH1_MSG_DEBUG) { |
fb09bf1c |
865 | /* log debug message */ |
eb4504cc |
866 | char buf[512]; |
51470298 |
867 | int stringlen = GET_32BIT(ssh->pktin.body); |
eb4504cc |
868 | strcpy(buf, "Remote debug message: "); |
869 | if (stringlen > 480) |
870 | stringlen = 480; |
51470298 |
871 | memcpy(buf + 8, ssh->pktin.body + 4, stringlen); |
3d63ca2e |
872 | buf[8 + stringlen] = '\0'; |
fb09bf1c |
873 | logevent(buf); |
874 | goto next_packet; |
51470298 |
875 | } else if (ssh->pktin.type == SSH1_MSG_IGNORE) { |
fb09bf1c |
876 | /* do nothing */ |
877 | goto next_packet; |
878 | } |
879 | |
51470298 |
880 | if (ssh->pktin.type == SSH1_MSG_DISCONNECT) { |
38c4a8da |
881 | /* log reason code in disconnect message */ |
882 | char buf[256]; |
51470298 |
883 | unsigned msglen = GET_32BIT(ssh->pktin.body); |
260f3dec |
884 | unsigned nowlen; |
38c4a8da |
885 | strcpy(buf, "Remote sent disconnect: "); |
886 | nowlen = strlen(buf); |
32874aea |
887 | if (msglen > sizeof(buf) - nowlen - 1) |
888 | msglen = sizeof(buf) - nowlen - 1; |
51470298 |
889 | memcpy(buf + nowlen, ssh->pktin.body + 4, msglen); |
32874aea |
890 | buf[nowlen + msglen] = '\0'; |
247308b5 |
891 | /* logevent(buf); (this is now done within the bombout macro) */ |
a8327734 |
892 | bombout((ssh,"Server sent disconnect message:\n\"%s\"", buf+nowlen)); |
e14d0f19 |
893 | crReturn(0); |
38c4a8da |
894 | } |
895 | |
fb09bf1c |
896 | crFinish(0); |
897 | } |
898 | |
51470298 |
899 | static int ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen) |
e5574168 |
900 | { |
51470298 |
901 | struct rdpkt2_state_tag *st = &ssh->rdpkt2_state; |
e5574168 |
902 | |
51470298 |
903 | crBegin(ssh->ssh2_rdpkt_crstate); |
e5574168 |
904 | |
32874aea |
905 | next_packet: |
51470298 |
906 | ssh->pktin.type = 0; |
907 | ssh->pktin.length = 0; |
908 | if (ssh->sccipher) |
909 | st->cipherblk = ssh->sccipher->blksize; |
e5574168 |
910 | else |
32874aea |
911 | st->cipherblk = 8; |
960e736a |
912 | if (st->cipherblk < 8) |
32874aea |
913 | st->cipherblk = 8; |
960e736a |
914 | |
51470298 |
915 | if (ssh->pktin.maxlen < st->cipherblk) { |
916 | ssh->pktin.maxlen = st->cipherblk; |
917 | ssh->pktin.data = srealloc(ssh->pktin.data, st->cipherblk + APIEXTRA); |
e5574168 |
918 | } |
919 | |
920 | /* |
921 | * Acquire and decrypt the first block of the packet. This will |
922 | * contain the length and padding details. |
923 | */ |
32874aea |
924 | for (st->i = st->len = 0; st->i < st->cipherblk; st->i++) { |
e5574168 |
925 | while ((*datalen) == 0) |
32874aea |
926 | crReturn(st->cipherblk - st->i); |
51470298 |
927 | ssh->pktin.data[st->i] = *(*data)++; |
32874aea |
928 | (*datalen)--; |
e5574168 |
929 | } |
4252c9cc |
930 | |
51470298 |
931 | if (ssh->sccipher) |
371e569c |
932 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
933 | ssh->pktin.data, st->cipherblk); |
e5574168 |
934 | |
935 | /* |
936 | * Now get the length and padding figures. |
937 | */ |
51470298 |
938 | st->len = GET_32BIT(ssh->pktin.data); |
939 | st->pad = ssh->pktin.data[4]; |
e5574168 |
940 | |
941 | /* |
717dc483 |
942 | * _Completely_ silly lengths should be stomped on before they |
943 | * do us any more damage. |
944 | */ |
945 | if (st->len < 0 || st->pad < 0 || st->len + st->pad < 0) { |
a8327734 |
946 | bombout((ssh,"Incoming packet was garbled on decryption")); |
717dc483 |
947 | crReturn(0); |
948 | } |
949 | |
950 | /* |
e5574168 |
951 | * This enables us to deduce the payload length. |
952 | */ |
960e736a |
953 | st->payload = st->len - st->pad - 1; |
e5574168 |
954 | |
51470298 |
955 | ssh->pktin.length = st->payload + 5; |
e5574168 |
956 | |
957 | /* |
958 | * So now we can work out the total packet length. |
959 | */ |
960e736a |
960 | st->packetlen = st->len + 4; |
51470298 |
961 | st->maclen = ssh->scmac ? ssh->scmac->len : 0; |
e5574168 |
962 | |
963 | /* |
964 | * Adjust memory allocation if packet is too big. |
965 | */ |
51470298 |
966 | if (ssh->pktin.maxlen < st->packetlen + st->maclen) { |
967 | ssh->pktin.maxlen = st->packetlen + st->maclen; |
968 | ssh->pktin.data = srealloc(ssh->pktin.data, |
969 | ssh->pktin.maxlen + APIEXTRA); |
e5574168 |
970 | } |
971 | |
972 | /* |
973 | * Read and decrypt the remainder of the packet. |
974 | */ |
32874aea |
975 | for (st->i = st->cipherblk; st->i < st->packetlen + st->maclen; |
976 | st->i++) { |
e5574168 |
977 | while ((*datalen) == 0) |
960e736a |
978 | crReturn(st->packetlen + st->maclen - st->i); |
51470298 |
979 | ssh->pktin.data[st->i] = *(*data)++; |
32874aea |
980 | (*datalen)--; |
e5574168 |
981 | } |
982 | /* Decrypt everything _except_ the MAC. */ |
51470298 |
983 | if (ssh->sccipher) |
371e569c |
984 | ssh->sccipher->decrypt(ssh->sc_cipher_ctx, |
985 | ssh->pktin.data + st->cipherblk, |
51470298 |
986 | st->packetlen - st->cipherblk); |
e5574168 |
987 | |
988 | /* |
989 | * Check the MAC. |
990 | */ |
51470298 |
991 | if (ssh->scmac |
e0e1a00d |
992 | && !ssh->scmac->verify(ssh->sc_mac_ctx, ssh->pktin.data, st->len + 4, |
51470298 |
993 | st->incoming_sequence)) { |
a8327734 |
994 | bombout((ssh,"Incorrect MAC received on packet")); |
32874aea |
995 | crReturn(0); |
8d5de777 |
996 | } |
32874aea |
997 | st->incoming_sequence++; /* whether or not we MACed */ |
e5574168 |
998 | |
4ba9b64b |
999 | /* |
1000 | * Decompress packet payload. |
1001 | */ |
1002 | { |
1003 | unsigned char *newpayload; |
1004 | int newlen; |
51470298 |
1005 | if (ssh->sccomp && |
5366aed8 |
1006 | ssh->sccomp->decompress(ssh->sc_comp_ctx, |
1007 | ssh->pktin.data + 5, ssh->pktin.length - 5, |
51470298 |
1008 | &newpayload, &newlen)) { |
1009 | if (ssh->pktin.maxlen < newlen + 5) { |
1010 | ssh->pktin.maxlen = newlen + 5; |
1011 | ssh->pktin.data = srealloc(ssh->pktin.data, |
1012 | ssh->pktin.maxlen + APIEXTRA); |
4ba9b64b |
1013 | } |
51470298 |
1014 | ssh->pktin.length = 5 + newlen; |
1015 | memcpy(ssh->pktin.data + 5, newpayload, newlen); |
dcbde236 |
1016 | sfree(newpayload); |
4ba9b64b |
1017 | } |
1018 | } |
1019 | |
51470298 |
1020 | ssh->pktin.savedpos = 6; |
1021 | ssh->pktin.type = ssh->pktin.data[5]; |
e5574168 |
1022 | |
a8327734 |
1023 | if (ssh->logctx) |
1024 | log_packet(ssh->logctx, PKT_INCOMING, ssh->pktin.type, |
1025 | ssh2_pkt_type(ssh->pkt_ctx, ssh->pktin.type), |
1026 | ssh->pktin.data+6, ssh->pktin.length-6); |
00db133f |
1027 | |
51470298 |
1028 | switch (ssh->pktin.type) { |
3d63ca2e |
1029 | /* |
1030 | * These packets we must handle instantly. |
1031 | */ |
1032 | case SSH2_MSG_DISCONNECT: |
1033 | { |
1034 | /* log reason code in disconnect message */ |
57356d63 |
1035 | char *buf; |
1036 | int nowlen; |
51470298 |
1037 | int reason = GET_32BIT(ssh->pktin.data + 6); |
1038 | unsigned msglen = GET_32BIT(ssh->pktin.data + 10); |
57356d63 |
1039 | |
3d63ca2e |
1040 | if (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) { |
57356d63 |
1041 | buf = dupprintf("Received disconnect message (%s)", |
1042 | ssh2_disconnect_reasons[reason]); |
3d63ca2e |
1043 | } else { |
57356d63 |
1044 | buf = dupprintf("Received disconnect message (unknown" |
1045 | " type %d)", reason); |
3d63ca2e |
1046 | } |
1047 | logevent(buf); |
57356d63 |
1048 | sfree(buf); |
1049 | buf = dupprintf("Disconnection message text: %n%.*s", |
33b82b22 |
1050 | &nowlen, msglen, ssh->pktin.data + 14); |
3d63ca2e |
1051 | logevent(buf); |
a8327734 |
1052 | bombout((ssh,"Server sent disconnect message\ntype %d (%s):\n\"%s\"", |
3d63ca2e |
1053 | reason, |
1054 | (reason > 0 && reason < lenof(ssh2_disconnect_reasons)) ? |
1055 | ssh2_disconnect_reasons[reason] : "unknown", |
1056 | buf+nowlen)); |
57356d63 |
1057 | sfree(buf); |
3d63ca2e |
1058 | crReturn(0); |
1059 | } |
1060 | break; |
1061 | case SSH2_MSG_IGNORE: |
1062 | goto next_packet; |
1063 | case SSH2_MSG_DEBUG: |
1064 | { |
1065 | /* log the debug message */ |
1066 | char buf[512]; |
51470298 |
1067 | /* int display = ssh->pktin.body[6]; */ |
1068 | int stringlen = GET_32BIT(ssh->pktin.data+7); |
3d63ca2e |
1069 | int prefix; |
1070 | strcpy(buf, "Remote debug message: "); |
1071 | prefix = strlen(buf); |
51470298 |
1072 | if (stringlen > (int)(sizeof(buf)-prefix-1)) |
3d63ca2e |
1073 | stringlen = sizeof(buf)-prefix-1; |
51470298 |
1074 | memcpy(buf + prefix, ssh->pktin.data + 11, stringlen); |
3d63ca2e |
1075 | buf[prefix + stringlen] = '\0'; |
1076 | logevent(buf); |
38c4a8da |
1077 | } |
3d63ca2e |
1078 | goto next_packet; /* FIXME: print the debug message */ |
1079 | |
1080 | /* |
1081 | * These packets we need do nothing about here. |
1082 | */ |
1083 | case SSH2_MSG_UNIMPLEMENTED: |
1084 | case SSH2_MSG_SERVICE_REQUEST: |
1085 | case SSH2_MSG_SERVICE_ACCEPT: |
1086 | case SSH2_MSG_KEXINIT: |
1087 | case SSH2_MSG_NEWKEYS: |
1088 | case SSH2_MSG_KEXDH_INIT: |
1089 | case SSH2_MSG_KEXDH_REPLY: |
1090 | /* case SSH2_MSG_KEX_DH_GEX_REQUEST: duplicate case value */ |
1091 | /* case SSH2_MSG_KEX_DH_GEX_GROUP: duplicate case value */ |
1092 | case SSH2_MSG_KEX_DH_GEX_INIT: |
1093 | case SSH2_MSG_KEX_DH_GEX_REPLY: |
1094 | case SSH2_MSG_USERAUTH_REQUEST: |
1095 | case SSH2_MSG_USERAUTH_FAILURE: |
1096 | case SSH2_MSG_USERAUTH_SUCCESS: |
1097 | case SSH2_MSG_USERAUTH_BANNER: |
1098 | case SSH2_MSG_USERAUTH_PK_OK: |
1099 | /* case SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ: duplicate case value */ |
1100 | /* case SSH2_MSG_USERAUTH_INFO_REQUEST: duplicate case value */ |
1101 | case SSH2_MSG_USERAUTH_INFO_RESPONSE: |
1102 | case SSH2_MSG_GLOBAL_REQUEST: |
1103 | case SSH2_MSG_REQUEST_SUCCESS: |
1104 | case SSH2_MSG_REQUEST_FAILURE: |
1105 | case SSH2_MSG_CHANNEL_OPEN: |
1106 | case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: |
1107 | case SSH2_MSG_CHANNEL_OPEN_FAILURE: |
1108 | case SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
1109 | case SSH2_MSG_CHANNEL_DATA: |
1110 | case SSH2_MSG_CHANNEL_EXTENDED_DATA: |
1111 | case SSH2_MSG_CHANNEL_EOF: |
1112 | case SSH2_MSG_CHANNEL_CLOSE: |
1113 | case SSH2_MSG_CHANNEL_REQUEST: |
1114 | case SSH2_MSG_CHANNEL_SUCCESS: |
1115 | case SSH2_MSG_CHANNEL_FAILURE: |
1116 | break; |
1117 | |
1118 | /* |
1119 | * For anything else we send SSH2_MSG_UNIMPLEMENTED. |
1120 | */ |
1121 | default: |
51470298 |
1122 | ssh2_pkt_init(ssh, SSH2_MSG_UNIMPLEMENTED); |
1123 | ssh2_pkt_adduint32(ssh, st->incoming_sequence - 1); |
1124 | ssh2_pkt_send(ssh); |
3d63ca2e |
1125 | break; |
38c4a8da |
1126 | } |
1127 | |
e5574168 |
1128 | crFinish(0); |
1129 | } |
1130 | |
51470298 |
1131 | static void ssh1_pktout_size(Ssh ssh, int len) |
32874aea |
1132 | { |
374330e2 |
1133 | int pad, biglen; |
1134 | |
1135 | len += 5; /* type and CRC */ |
32874aea |
1136 | pad = 8 - (len % 8); |
374330e2 |
1137 | biglen = len + pad; |
1138 | |
51470298 |
1139 | ssh->pktout.length = len - 5; |
1140 | if (ssh->pktout.maxlen < biglen) { |
1141 | ssh->pktout.maxlen = biglen; |
8f203108 |
1142 | #ifdef MSCRYPTOAPI |
fb09bf1c |
1143 | /* Allocate enough buffer space for extra block |
8f203108 |
1144 | * for MS CryptEncrypt() */ |
51470298 |
1145 | ssh->pktout.data = srealloc(ssh->pktout.data, biglen + 12); |
8f203108 |
1146 | #else |
51470298 |
1147 | ssh->pktout.data = srealloc(ssh->pktout.data, biglen + 4); |
8f203108 |
1148 | #endif |
374330e2 |
1149 | } |
51470298 |
1150 | ssh->pktout.body = ssh->pktout.data + 4 + pad + 1; |
4ba9b64b |
1151 | } |
374330e2 |
1152 | |
51470298 |
1153 | static void s_wrpkt_start(Ssh ssh, int type, int len) |
32874aea |
1154 | { |
51470298 |
1155 | ssh1_pktout_size(ssh, len); |
1156 | ssh->pktout.type = type; |
374330e2 |
1157 | } |
1158 | |
51470298 |
1159 | static int s_wrpkt_prepare(Ssh ssh) |
32874aea |
1160 | { |
374330e2 |
1161 | int pad, len, biglen, i; |
1162 | unsigned long crc; |
1163 | |
51470298 |
1164 | ssh->pktout.body[-1] = ssh->pktout.type; |
4ba9b64b |
1165 | |
a8327734 |
1166 | if (ssh->logctx) |
1167 | log_packet(ssh->logctx, PKT_OUTGOING, ssh->pktout.type, |
1168 | ssh1_pkt_type(ssh->pktout.type), |
1169 | ssh->pktout.body, ssh->pktout.length); |
00db133f |
1170 | |
51470298 |
1171 | if (ssh->v1_compressing) { |
4ba9b64b |
1172 | unsigned char *compblk; |
1173 | int complen; |
5366aed8 |
1174 | zlib_compress_block(ssh->cs_comp_ctx, |
1175 | ssh->pktout.body - 1, ssh->pktout.length + 1, |
4ba9b64b |
1176 | &compblk, &complen); |
51470298 |
1177 | ssh1_pktout_size(ssh, complen - 1); |
1178 | memcpy(ssh->pktout.body - 1, compblk, complen); |
dcbde236 |
1179 | sfree(compblk); |
4ba9b64b |
1180 | } |
1181 | |
51470298 |
1182 | len = ssh->pktout.length + 5; /* type and CRC */ |
32874aea |
1183 | pad = 8 - (len % 8); |
374330e2 |
1184 | biglen = len + pad; |
1185 | |
32874aea |
1186 | for (i = 0; i < pad; i++) |
51470298 |
1187 | ssh->pktout.data[i + 4] = random_byte(); |
1188 | crc = crc32(ssh->pktout.data + 4, biglen - 4); |
1189 | PUT_32BIT(ssh->pktout.data + biglen, crc); |
1190 | PUT_32BIT(ssh->pktout.data, len); |
374330e2 |
1191 | |
51470298 |
1192 | if (ssh->cipher) |
371e569c |
1193 | ssh->cipher->encrypt(ssh->v1_cipher_ctx, ssh->pktout.data + 4, biglen); |
374330e2 |
1194 | |
32874aea |
1195 | return biglen + 4; |
39065bed |
1196 | } |
1197 | |
51470298 |
1198 | static void s_wrpkt(Ssh ssh) |
32874aea |
1199 | { |
5471d09a |
1200 | int len, backlog; |
51470298 |
1201 | len = s_wrpkt_prepare(ssh); |
d8baa528 |
1202 | backlog = sk_write(ssh->s, (char *)ssh->pktout.data, len); |
5471d09a |
1203 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1204 | ssh_throttle_all(ssh, 1, backlog); |
39065bed |
1205 | } |
1206 | |
51470298 |
1207 | static void s_wrpkt_defer(Ssh ssh) |
32874aea |
1208 | { |
39065bed |
1209 | int len; |
51470298 |
1210 | len = s_wrpkt_prepare(ssh); |
1211 | if (ssh->deferred_len + len > ssh->deferred_size) { |
1212 | ssh->deferred_size = ssh->deferred_len + len + 128; |
1213 | ssh->deferred_send_data = srealloc(ssh->deferred_send_data, |
1214 | ssh->deferred_size); |
39065bed |
1215 | } |
51470298 |
1216 | memcpy(ssh->deferred_send_data + ssh->deferred_len, ssh->pktout.data, len); |
1217 | ssh->deferred_len += len; |
374330e2 |
1218 | } |
1219 | |
fb09bf1c |
1220 | /* |
39065bed |
1221 | * Construct a packet with the specified contents. |
fb09bf1c |
1222 | */ |
51470298 |
1223 | static void construct_packet(Ssh ssh, int pkttype, va_list ap1, va_list ap2) |
fb09bf1c |
1224 | { |
fb09bf1c |
1225 | unsigned char *p, *argp, argchar; |
1226 | unsigned long argint; |
1227 | int pktlen, argtype, arglen; |
7cca0d81 |
1228 | Bignum bn; |
fb09bf1c |
1229 | |
1230 | pktlen = 0; |
39065bed |
1231 | while ((argtype = va_arg(ap1, int)) != PKT_END) { |
fb09bf1c |
1232 | switch (argtype) { |
1233 | case PKT_INT: |
39065bed |
1234 | (void) va_arg(ap1, int); |
fb09bf1c |
1235 | pktlen += 4; |
1236 | break; |
1237 | case PKT_CHAR: |
1ec39246 |
1238 | (void) va_arg(ap1, int); |
fb09bf1c |
1239 | pktlen++; |
1240 | break; |
1241 | case PKT_DATA: |
39065bed |
1242 | (void) va_arg(ap1, unsigned char *); |
1243 | arglen = va_arg(ap1, int); |
fb09bf1c |
1244 | pktlen += arglen; |
1245 | break; |
1246 | case PKT_STR: |
39065bed |
1247 | argp = va_arg(ap1, unsigned char *); |
d8baa528 |
1248 | arglen = strlen((char *)argp); |
fb09bf1c |
1249 | pktlen += 4 + arglen; |
1250 | break; |
7cca0d81 |
1251 | case PKT_BIGNUM: |
39065bed |
1252 | bn = va_arg(ap1, Bignum); |
32874aea |
1253 | pktlen += ssh1_bignum_length(bn); |
7cca0d81 |
1254 | break; |
fb09bf1c |
1255 | default: |
1256 | assert(0); |
1257 | } |
1258 | } |
fb09bf1c |
1259 | |
51470298 |
1260 | s_wrpkt_start(ssh, pkttype, pktlen); |
1261 | p = ssh->pktout.body; |
fb09bf1c |
1262 | |
39065bed |
1263 | while ((argtype = va_arg(ap2, int)) != PKT_END) { |
fb09bf1c |
1264 | switch (argtype) { |
1265 | case PKT_INT: |
39065bed |
1266 | argint = va_arg(ap2, int); |
fb09bf1c |
1267 | PUT_32BIT(p, argint); |
1268 | p += 4; |
1269 | break; |
1270 | case PKT_CHAR: |
1ec39246 |
1271 | argchar = (unsigned char) va_arg(ap2, int); |
fb09bf1c |
1272 | *p = argchar; |
1273 | p++; |
1274 | break; |
1275 | case PKT_DATA: |
39065bed |
1276 | argp = va_arg(ap2, unsigned char *); |
1277 | arglen = va_arg(ap2, int); |
fb09bf1c |
1278 | memcpy(p, argp, arglen); |
1279 | p += arglen; |
1280 | break; |
1281 | case PKT_STR: |
39065bed |
1282 | argp = va_arg(ap2, unsigned char *); |
d8baa528 |
1283 | arglen = strlen((char *)argp); |
fb09bf1c |
1284 | PUT_32BIT(p, arglen); |
1285 | memcpy(p + 4, argp, arglen); |
1286 | p += 4 + arglen; |
1287 | break; |
7cca0d81 |
1288 | case PKT_BIGNUM: |
39065bed |
1289 | bn = va_arg(ap2, Bignum); |
32874aea |
1290 | p += ssh1_write_bignum(p, bn); |
7cca0d81 |
1291 | break; |
fb09bf1c |
1292 | } |
1293 | } |
39065bed |
1294 | } |
fb09bf1c |
1295 | |
51470298 |
1296 | static void send_packet(Ssh ssh, int pkttype, ...) |
32874aea |
1297 | { |
39065bed |
1298 | va_list ap1, ap2; |
1299 | va_start(ap1, pkttype); |
1300 | va_start(ap2, pkttype); |
51470298 |
1301 | construct_packet(ssh, pkttype, ap1, ap2); |
1302 | s_wrpkt(ssh); |
fb09bf1c |
1303 | } |
1304 | |
51470298 |
1305 | static void defer_packet(Ssh ssh, int pkttype, ...) |
32874aea |
1306 | { |
39065bed |
1307 | va_list ap1, ap2; |
1308 | va_start(ap1, pkttype); |
1309 | va_start(ap2, pkttype); |
51470298 |
1310 | construct_packet(ssh, pkttype, ap1, ap2); |
1311 | s_wrpkt_defer(ssh); |
39065bed |
1312 | } |
1313 | |
32874aea |
1314 | static int ssh_versioncmp(char *a, char *b) |
1315 | { |
9697bfd2 |
1316 | char *ae, *be; |
1317 | unsigned long av, bv; |
1318 | |
43aa02a7 |
1319 | av = strtoul(a, &ae, 10); |
1320 | bv = strtoul(b, &be, 10); |
32874aea |
1321 | if (av != bv) |
1322 | return (av < bv ? -1 : +1); |
1323 | if (*ae == '.') |
1324 | ae++; |
1325 | if (*be == '.') |
1326 | be++; |
43aa02a7 |
1327 | av = strtoul(ae, &ae, 10); |
1328 | bv = strtoul(be, &be, 10); |
32874aea |
1329 | if (av != bv) |
1330 | return (av < bv ? -1 : +1); |
9697bfd2 |
1331 | return 0; |
1332 | } |
1333 | |
e5574168 |
1334 | /* |
a92dd380 |
1335 | * Utility routines for putting an SSH-protocol `string' and |
1336 | * `uint32' into a SHA state. |
e5574168 |
1337 | */ |
1338 | #include <stdio.h> |
32874aea |
1339 | static void sha_string(SHA_State * s, void *str, int len) |
1340 | { |
e5574168 |
1341 | unsigned char lenblk[4]; |
e5574168 |
1342 | PUT_32BIT(lenblk, len); |
e5574168 |
1343 | SHA_Bytes(s, lenblk, 4); |
e5574168 |
1344 | SHA_Bytes(s, str, len); |
1345 | } |
1346 | |
32874aea |
1347 | static void sha_uint32(SHA_State * s, unsigned i) |
1348 | { |
a92dd380 |
1349 | unsigned char intblk[4]; |
1350 | PUT_32BIT(intblk, i); |
1351 | SHA_Bytes(s, intblk, 4); |
1352 | } |
1353 | |
7cca0d81 |
1354 | /* |
1355 | * SSH2 packet construction functions. |
1356 | */ |
51470298 |
1357 | static void ssh2_pkt_ensure(Ssh ssh, int length) |
32874aea |
1358 | { |
51470298 |
1359 | if (ssh->pktout.maxlen < length) { |
1360 | ssh->pktout.maxlen = length + 256; |
1361 | ssh->pktout.data = srealloc(ssh->pktout.data, |
1362 | ssh->pktout.maxlen + APIEXTRA); |
1363 | if (!ssh->pktout.data) |
32874aea |
1364 | fatalbox("Out of memory"); |
7cca0d81 |
1365 | } |
783415f8 |
1366 | } |
51470298 |
1367 | static void ssh2_pkt_adddata(Ssh ssh, void *data, int len) |
32874aea |
1368 | { |
51470298 |
1369 | ssh->pktout.length += len; |
1370 | ssh2_pkt_ensure(ssh, ssh->pktout.length); |
1371 | memcpy(ssh->pktout.data + ssh->pktout.length - len, data, len); |
7cca0d81 |
1372 | } |
51470298 |
1373 | static void ssh2_pkt_addbyte(Ssh ssh, unsigned char byte) |
32874aea |
1374 | { |
51470298 |
1375 | ssh2_pkt_adddata(ssh, &byte, 1); |
7cca0d81 |
1376 | } |
51470298 |
1377 | static void ssh2_pkt_init(Ssh ssh, int pkt_type) |
32874aea |
1378 | { |
51470298 |
1379 | ssh->pktout.length = 5; |
1380 | ssh2_pkt_addbyte(ssh, (unsigned char) pkt_type); |
7cca0d81 |
1381 | } |
51470298 |
1382 | static void ssh2_pkt_addbool(Ssh ssh, unsigned char value) |
32874aea |
1383 | { |
51470298 |
1384 | ssh2_pkt_adddata(ssh, &value, 1); |
7cca0d81 |
1385 | } |
51470298 |
1386 | static void ssh2_pkt_adduint32(Ssh ssh, unsigned long value) |
32874aea |
1387 | { |
7cca0d81 |
1388 | unsigned char x[4]; |
1389 | PUT_32BIT(x, value); |
51470298 |
1390 | ssh2_pkt_adddata(ssh, x, 4); |
7cca0d81 |
1391 | } |
51470298 |
1392 | static void ssh2_pkt_addstring_start(Ssh ssh) |
32874aea |
1393 | { |
51470298 |
1394 | ssh2_pkt_adduint32(ssh, 0); |
1395 | ssh->pktout.savedpos = ssh->pktout.length; |
7cca0d81 |
1396 | } |
51470298 |
1397 | static void ssh2_pkt_addstring_str(Ssh ssh, char *data) |
32874aea |
1398 | { |
51470298 |
1399 | ssh2_pkt_adddata(ssh, data, strlen(data)); |
1400 | PUT_32BIT(ssh->pktout.data + ssh->pktout.savedpos - 4, |
1401 | ssh->pktout.length - ssh->pktout.savedpos); |
7cca0d81 |
1402 | } |
51470298 |
1403 | static void ssh2_pkt_addstring_data(Ssh ssh, char *data, int len) |
32874aea |
1404 | { |
51470298 |
1405 | ssh2_pkt_adddata(ssh, data, len); |
1406 | PUT_32BIT(ssh->pktout.data + ssh->pktout.savedpos - 4, |
1407 | ssh->pktout.length - ssh->pktout.savedpos); |
7cca0d81 |
1408 | } |
51470298 |
1409 | static void ssh2_pkt_addstring(Ssh ssh, char *data) |
32874aea |
1410 | { |
51470298 |
1411 | ssh2_pkt_addstring_start(ssh); |
1412 | ssh2_pkt_addstring_str(ssh, data); |
7cca0d81 |
1413 | } |
d8baa528 |
1414 | static unsigned char *ssh2_mpint_fmt(Bignum b, int *len) |
32874aea |
1415 | { |
7cca0d81 |
1416 | unsigned char *p; |
32874aea |
1417 | int i, n = (bignum_bitcount(b) + 7) / 8; |
3709bfe9 |
1418 | p = smalloc(n + 1); |
7cca0d81 |
1419 | if (!p) |
32874aea |
1420 | fatalbox("out of memory"); |
7cca0d81 |
1421 | p[0] = 0; |
3709bfe9 |
1422 | for (i = 1; i <= n; i++) |
32874aea |
1423 | p[i] = bignum_byte(b, n - i); |
7cca0d81 |
1424 | i = 0; |
32874aea |
1425 | while (i <= n && p[i] == 0 && (p[i + 1] & 0x80) == 0) |
1426 | i++; |
1427 | memmove(p, p + i, n + 1 - i); |
1428 | *len = n + 1 - i; |
7cca0d81 |
1429 | return p; |
1430 | } |
51470298 |
1431 | static void ssh2_pkt_addmp(Ssh ssh, Bignum b) |
32874aea |
1432 | { |
7cca0d81 |
1433 | unsigned char *p; |
1434 | int len; |
1435 | p = ssh2_mpint_fmt(b, &len); |
51470298 |
1436 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
1437 | ssh2_pkt_addstring_data(ssh, (char *)p, len); |
dcbde236 |
1438 | sfree(p); |
7cca0d81 |
1439 | } |
b185170a |
1440 | |
1441 | /* |
1442 | * Construct an SSH2 final-form packet: compress it, encrypt it, |
1443 | * put the MAC on it. Final packet, ready to be sent, is stored in |
51470298 |
1444 | * ssh->pktout.data. Total length is returned. |
b185170a |
1445 | */ |
51470298 |
1446 | static int ssh2_pkt_construct(Ssh ssh) |
32874aea |
1447 | { |
7cca0d81 |
1448 | int cipherblk, maclen, padding, i; |
7cca0d81 |
1449 | |
a8327734 |
1450 | if (ssh->logctx) |
1451 | log_packet(ssh->logctx, PKT_OUTGOING, ssh->pktout.data[5], |
1452 | ssh2_pkt_type(ssh->pkt_ctx, ssh->pktout.data[5]), |
1453 | ssh->pktout.data + 6, ssh->pktout.length - 6); |
00db133f |
1454 | |
7cca0d81 |
1455 | /* |
4ba9b64b |
1456 | * Compress packet payload. |
1457 | */ |
4ba9b64b |
1458 | { |
1459 | unsigned char *newpayload; |
1460 | int newlen; |
51470298 |
1461 | if (ssh->cscomp && |
5366aed8 |
1462 | ssh->cscomp->compress(ssh->cs_comp_ctx, ssh->pktout.data + 5, |
1463 | ssh->pktout.length - 5, |
51470298 |
1464 | &newpayload, &newlen)) { |
1465 | ssh->pktout.length = 5; |
1466 | ssh2_pkt_adddata(ssh, newpayload, newlen); |
dcbde236 |
1467 | sfree(newpayload); |
4ba9b64b |
1468 | } |
1469 | } |
1470 | |
1471 | /* |
7cca0d81 |
1472 | * Add padding. At least four bytes, and must also bring total |
1473 | * length (minus MAC) up to a multiple of the block size. |
1474 | */ |
51470298 |
1475 | cipherblk = ssh->cscipher ? ssh->cscipher->blksize : 8; /* block size */ |
32874aea |
1476 | cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */ |
7cca0d81 |
1477 | padding = 4; |
32874aea |
1478 | padding += |
51470298 |
1479 | (cipherblk - (ssh->pktout.length + padding) % cipherblk) % cipherblk; |
1480 | maclen = ssh->csmac ? ssh->csmac->len : 0; |
1481 | ssh2_pkt_ensure(ssh, ssh->pktout.length + padding + maclen); |
1482 | ssh->pktout.data[4] = padding; |
7cca0d81 |
1483 | for (i = 0; i < padding; i++) |
51470298 |
1484 | ssh->pktout.data[ssh->pktout.length + i] = random_byte(); |
1485 | PUT_32BIT(ssh->pktout.data, ssh->pktout.length + padding - 4); |
1486 | if (ssh->csmac) |
e0e1a00d |
1487 | ssh->csmac->generate(ssh->cs_mac_ctx, ssh->pktout.data, |
1488 | ssh->pktout.length + padding, |
51470298 |
1489 | ssh->v2_outgoing_sequence); |
1490 | ssh->v2_outgoing_sequence++; /* whether or not we MACed */ |
1491 | |
1492 | if (ssh->cscipher) |
371e569c |
1493 | ssh->cscipher->encrypt(ssh->cs_cipher_ctx, |
1494 | ssh->pktout.data, ssh->pktout.length + padding); |
51470298 |
1495 | |
1496 | /* Ready-to-send packet starts at ssh->pktout.data. We return length. */ |
1497 | return ssh->pktout.length + padding + maclen; |
b185170a |
1498 | } |
1499 | |
1500 | /* |
1501 | * Construct and send an SSH2 packet immediately. |
1502 | */ |
51470298 |
1503 | static void ssh2_pkt_send(Ssh ssh) |
32874aea |
1504 | { |
5471d09a |
1505 | int len; |
1506 | int backlog; |
51470298 |
1507 | len = ssh2_pkt_construct(ssh); |
d8baa528 |
1508 | backlog = sk_write(ssh->s, (char *)ssh->pktout.data, len); |
5471d09a |
1509 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1510 | ssh_throttle_all(ssh, 1, backlog); |
b185170a |
1511 | } |
1512 | |
1513 | /* |
1514 | * Construct an SSH2 packet and add it to a deferred data block. |
1515 | * Useful for sending multiple packets in a single sk_write() call, |
1516 | * to prevent a traffic-analysing listener from being able to work |
1517 | * out the length of any particular packet (such as the password |
1518 | * packet). |
1519 | * |
1520 | * Note that because SSH2 sequence-numbers its packets, this can |
1521 | * NOT be used as an m4-style `defer' allowing packets to be |
1522 | * constructed in one order and sent in another. |
1523 | */ |
51470298 |
1524 | static void ssh2_pkt_defer(Ssh ssh) |
32874aea |
1525 | { |
51470298 |
1526 | int len = ssh2_pkt_construct(ssh); |
1527 | if (ssh->deferred_len + len > ssh->deferred_size) { |
1528 | ssh->deferred_size = ssh->deferred_len + len + 128; |
1529 | ssh->deferred_send_data = srealloc(ssh->deferred_send_data, |
1530 | ssh->deferred_size); |
b185170a |
1531 | } |
51470298 |
1532 | memcpy(ssh->deferred_send_data + ssh->deferred_len, ssh->pktout.data, len); |
1533 | ssh->deferred_len += len; |
b185170a |
1534 | } |
1535 | |
1536 | /* |
1537 | * Send the whole deferred data block constructed by |
39065bed |
1538 | * ssh2_pkt_defer() or SSH1's defer_packet(). |
b185170a |
1539 | */ |
51470298 |
1540 | static void ssh_pkt_defersend(Ssh ssh) |
32874aea |
1541 | { |
5471d09a |
1542 | int backlog; |
d8baa528 |
1543 | backlog = sk_write(ssh->s, (char *)ssh->deferred_send_data, |
1544 | ssh->deferred_len); |
51470298 |
1545 | ssh->deferred_len = ssh->deferred_size = 0; |
1546 | sfree(ssh->deferred_send_data); |
1547 | ssh->deferred_send_data = NULL; |
5471d09a |
1548 | if (backlog > SSH_MAX_BACKLOG) |
51470298 |
1549 | ssh_throttle_all(ssh, 1, backlog); |
7cca0d81 |
1550 | } |
1551 | |
1552 | #if 0 |
32874aea |
1553 | void bndebug(char *string, Bignum b) |
1554 | { |
7cca0d81 |
1555 | unsigned char *p; |
1556 | int i, len; |
1557 | p = ssh2_mpint_fmt(b, &len); |
1558 | debug(("%s", string)); |
1559 | for (i = 0; i < len; i++) |
32874aea |
1560 | debug((" %02x", p[i])); |
765c4200 |
1561 | debug(("\n")); |
dcbde236 |
1562 | sfree(p); |
7cca0d81 |
1563 | } |
1564 | #endif |
1565 | |
32874aea |
1566 | static void sha_mpint(SHA_State * s, Bignum b) |
1567 | { |
7cca0d81 |
1568 | unsigned char *p; |
1569 | int len; |
1570 | p = ssh2_mpint_fmt(b, &len); |
1571 | sha_string(s, p, len); |
dcbde236 |
1572 | sfree(p); |
7cca0d81 |
1573 | } |
1574 | |
1575 | /* |
1576 | * SSH2 packet decode functions. |
1577 | */ |
51470298 |
1578 | static unsigned long ssh2_pkt_getuint32(Ssh ssh) |
32874aea |
1579 | { |
7cca0d81 |
1580 | unsigned long value; |
51470298 |
1581 | if (ssh->pktin.length - ssh->pktin.savedpos < 4) |
32874aea |
1582 | return 0; /* arrgh, no way to decline (FIXME?) */ |
51470298 |
1583 | value = GET_32BIT(ssh->pktin.data + ssh->pktin.savedpos); |
1584 | ssh->pktin.savedpos += 4; |
7cca0d81 |
1585 | return value; |
1586 | } |
51470298 |
1587 | static int ssh2_pkt_getbool(Ssh ssh) |
32874aea |
1588 | { |
65a22376 |
1589 | unsigned long value; |
51470298 |
1590 | if (ssh->pktin.length - ssh->pktin.savedpos < 1) |
32874aea |
1591 | return 0; /* arrgh, no way to decline (FIXME?) */ |
51470298 |
1592 | value = ssh->pktin.data[ssh->pktin.savedpos] != 0; |
1593 | ssh->pktin.savedpos++; |
65a22376 |
1594 | return value; |
1595 | } |
51470298 |
1596 | static void ssh2_pkt_getstring(Ssh ssh, char **p, int *length) |
32874aea |
1597 | { |
57356d63 |
1598 | int len; |
7cca0d81 |
1599 | *p = NULL; |
45068b27 |
1600 | *length = 0; |
51470298 |
1601 | if (ssh->pktin.length - ssh->pktin.savedpos < 4) |
32874aea |
1602 | return; |
57356d63 |
1603 | len = GET_32BIT(ssh->pktin.data + ssh->pktin.savedpos); |
1604 | if (len < 0) |
1605 | return; |
1606 | *length = len; |
51470298 |
1607 | ssh->pktin.savedpos += 4; |
1608 | if (ssh->pktin.length - ssh->pktin.savedpos < *length) |
32874aea |
1609 | return; |
d8baa528 |
1610 | *p = (char *)(ssh->pktin.data + ssh->pktin.savedpos); |
51470298 |
1611 | ssh->pktin.savedpos += *length; |
7cca0d81 |
1612 | } |
51470298 |
1613 | static Bignum ssh2_pkt_getmp(Ssh ssh) |
32874aea |
1614 | { |
7cca0d81 |
1615 | char *p; |
3709bfe9 |
1616 | int length; |
7cca0d81 |
1617 | Bignum b; |
1618 | |
51470298 |
1619 | ssh2_pkt_getstring(ssh, &p, &length); |
7cca0d81 |
1620 | if (!p) |
32874aea |
1621 | return NULL; |
8d5de777 |
1622 | if (p[0] & 0x80) { |
a8327734 |
1623 | bombout((ssh,"internal error: Can't handle negative mpints")); |
32874aea |
1624 | return NULL; |
8d5de777 |
1625 | } |
d8baa528 |
1626 | b = bignum_from_bytes((unsigned char *)p, length); |
7cca0d81 |
1627 | return b; |
1628 | } |
1629 | |
7d503c31 |
1630 | /* |
1dd353b5 |
1631 | * Helper function to add an SSH2 signature blob to a packet. |
1632 | * Expects to be shown the public key blob as well as the signature |
1633 | * blob. Normally works just like ssh2_pkt_addstring, but will |
1634 | * fiddle with the signature packet if necessary for |
1635 | * BUG_SSH2_RSA_PADDING. |
1636 | */ |
51470298 |
1637 | static void ssh2_add_sigblob(Ssh ssh, void *pkblob_v, int pkblob_len, |
1dd353b5 |
1638 | void *sigblob_v, int sigblob_len) |
1639 | { |
1640 | unsigned char *pkblob = (unsigned char *)pkblob_v; |
1641 | unsigned char *sigblob = (unsigned char *)sigblob_v; |
1642 | |
1643 | /* dmemdump(pkblob, pkblob_len); */ |
1644 | /* dmemdump(sigblob, sigblob_len); */ |
1645 | |
1646 | /* |
1647 | * See if this is in fact an ssh-rsa signature and a buggy |
1648 | * server; otherwise we can just do this the easy way. |
1649 | */ |
51470298 |
1650 | if ((ssh->remote_bugs & BUG_SSH2_RSA_PADDING) && |
1dd353b5 |
1651 | (GET_32BIT(pkblob) == 7 && !memcmp(pkblob+4, "ssh-rsa", 7))) { |
1652 | int pos, len, siglen; |
1653 | |
1654 | /* |
1655 | * Find the byte length of the modulus. |
1656 | */ |
1657 | |
1658 | pos = 4+7; /* skip over "ssh-rsa" */ |
1659 | pos += 4 + GET_32BIT(pkblob+pos); /* skip over exponent */ |
1660 | len = GET_32BIT(pkblob+pos); /* find length of modulus */ |
1661 | pos += 4; /* find modulus itself */ |
1662 | while (len > 0 && pkblob[pos] == 0) |
1663 | len--, pos++; |
1664 | /* debug(("modulus length is %d\n", len)); */ |
1665 | |
1666 | /* |
1667 | * Now find the signature integer. |
1668 | */ |
1669 | pos = 4+7; /* skip over "ssh-rsa" */ |
1670 | siglen = GET_32BIT(sigblob+pos); |
1671 | /* debug(("signature length is %d\n", siglen)); */ |
1672 | |
1673 | if (len != siglen) { |
1674 | unsigned char newlen[4]; |
51470298 |
1675 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
1676 | ssh2_pkt_addstring_data(ssh, (char *)sigblob, pos); |
1dd353b5 |
1677 | /* dmemdump(sigblob, pos); */ |
1678 | pos += 4; /* point to start of actual sig */ |
1679 | PUT_32BIT(newlen, len); |
d8baa528 |
1680 | ssh2_pkt_addstring_data(ssh, (char *)newlen, 4); |
1dd353b5 |
1681 | /* dmemdump(newlen, 4); */ |
1682 | newlen[0] = 0; |
1683 | while (len-- > siglen) { |
d8baa528 |
1684 | ssh2_pkt_addstring_data(ssh, (char *)newlen, 1); |
1dd353b5 |
1685 | /* dmemdump(newlen, 1); */ |
1686 | } |
d8baa528 |
1687 | ssh2_pkt_addstring_data(ssh, (char *)(sigblob+pos), siglen); |
1dd353b5 |
1688 | /* dmemdump(sigblob+pos, siglen); */ |
1689 | return; |
1690 | } |
1691 | |
1692 | /* Otherwise fall through and do it the easy way. */ |
1693 | } |
1694 | |
51470298 |
1695 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
1696 | ssh2_pkt_addstring_data(ssh, (char *)sigblob, sigblob_len); |
1dd353b5 |
1697 | } |
1698 | |
1699 | /* |
7d503c31 |
1700 | * Examine the remote side's version string and compare it against |
1701 | * a list of known buggy implementations. |
1702 | */ |
51470298 |
1703 | static void ssh_detect_bugs(Ssh ssh, char *vstring) |
32874aea |
1704 | { |
1705 | char *imp; /* pointer to implementation part */ |
7d503c31 |
1706 | imp = vstring; |
1707 | imp += strcspn(imp, "-"); |
bd358db1 |
1708 | if (*imp) imp++; |
7d503c31 |
1709 | imp += strcspn(imp, "-"); |
bd358db1 |
1710 | if (*imp) imp++; |
7d503c31 |
1711 | |
51470298 |
1712 | ssh->remote_bugs = 0; |
7d503c31 |
1713 | |
2c9c6388 |
1714 | if (cfg.sshbug_ignore1 == BUG_ON || |
1715 | (cfg.sshbug_ignore1 == BUG_AUTO && |
1716 | (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") || |
1717 | !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") || |
1718 | !strcmp(imp, "1.2.22") || !strcmp(imp, "Cisco-1.25")))) { |
32874aea |
1719 | /* |
1720 | * These versions don't support SSH1_MSG_IGNORE, so we have |
1721 | * to use a different defence against password length |
1722 | * sniffing. |
1723 | */ |
51470298 |
1724 | ssh->remote_bugs |= BUG_CHOKES_ON_SSH1_IGNORE; |
32874aea |
1725 | logevent("We believe remote version has SSH1 ignore bug"); |
7d503c31 |
1726 | } |
1727 | |
2c9c6388 |
1728 | if (cfg.sshbug_plainpw1 == BUG_ON || |
1729 | (cfg.sshbug_plainpw1 == BUG_AUTO && |
1730 | (!strcmp(imp, "Cisco-1.25")))) { |
bd358db1 |
1731 | /* |
1732 | * These versions need a plain password sent; they can't |
1733 | * handle having a null and a random length of data after |
1734 | * the password. |
1735 | */ |
51470298 |
1736 | ssh->remote_bugs |= BUG_NEEDS_SSH1_PLAIN_PASSWORD; |
bd358db1 |
1737 | logevent("We believe remote version needs a plain SSH1 password"); |
1738 | } |
1739 | |
2c9c6388 |
1740 | if (cfg.sshbug_rsa1 == BUG_ON || |
1741 | (cfg.sshbug_rsa1 == BUG_AUTO && |
1742 | (!strcmp(imp, "Cisco-1.25")))) { |
0df73905 |
1743 | /* |
1744 | * These versions apparently have no clue whatever about |
1745 | * RSA authentication and will panic and die if they see |
1746 | * an AUTH_RSA message. |
1747 | */ |
51470298 |
1748 | ssh->remote_bugs |= BUG_CHOKES_ON_RSA; |
0df73905 |
1749 | logevent("We believe remote version can't handle RSA authentication"); |
1750 | } |
1751 | |
2c9c6388 |
1752 | if (cfg.sshbug_hmac2 == BUG_ON || |
1753 | (cfg.sshbug_hmac2 == BUG_AUTO && |
831301f6 |
1754 | (wc_match("2.1.0*", imp) || wc_match("2.0.*", imp) || |
1755 | wc_match("2.2.0*", imp) || wc_match("2.3.0*", imp) || |
1756 | wc_match("2.1 *", imp)))) { |
32874aea |
1757 | /* |
1758 | * These versions have the HMAC bug. |
1759 | */ |
51470298 |
1760 | ssh->remote_bugs |= BUG_SSH2_HMAC; |
32874aea |
1761 | logevent("We believe remote version has SSH2 HMAC bug"); |
7d503c31 |
1762 | } |
1dd353b5 |
1763 | |
2c9c6388 |
1764 | if (cfg.sshbug_derivekey2 == BUG_ON || |
1765 | (cfg.sshbug_derivekey2 == BUG_AUTO && |
d6430b97 |
1766 | (wc_match("2.0.0*", imp) || wc_match("2.0.1[01]*", imp) ))) { |
088bde77 |
1767 | /* |
1768 | * These versions have the key-derivation bug (failing to |
1769 | * include the literal shared secret in the hashes that |
1770 | * generate the keys). |
1771 | */ |
51470298 |
1772 | ssh->remote_bugs |= BUG_SSH2_DERIVEKEY; |
088bde77 |
1773 | logevent("We believe remote version has SSH2 key-derivation bug"); |
1774 | } |
1775 | |
2c9c6388 |
1776 | if (cfg.sshbug_rsapad2 == BUG_ON || |
1777 | (cfg.sshbug_rsapad2 == BUG_AUTO && |
831301f6 |
1778 | (wc_match("OpenSSH_2.[5-9]*", imp) || |
1779 | wc_match("OpenSSH_3.[0-2]*", imp)))) { |
1dd353b5 |
1780 | /* |
1781 | * These versions have the SSH2 RSA padding bug. |
1782 | */ |
51470298 |
1783 | ssh->remote_bugs |= BUG_SSH2_RSA_PADDING; |
1dd353b5 |
1784 | logevent("We believe remote version has SSH2 RSA padding bug"); |
1785 | } |
8e975795 |
1786 | |
1787 | if (cfg.sshbug_dhgex2 == BUG_ON) { |
1788 | /* |
831301f6 |
1789 | * User specified the SSH2 DH GEX bug. |
8e975795 |
1790 | */ |
51470298 |
1791 | ssh->remote_bugs |= BUG_SSH2_DH_GEX; |
8e975795 |
1792 | logevent("We believe remote version has SSH2 DH group exchange bug"); |
1793 | } |
7d503c31 |
1794 | } |
1795 | |
51470298 |
1796 | static int do_ssh_init(Ssh ssh, unsigned char c) |
32874aea |
1797 | { |
51470298 |
1798 | struct do_ssh_init_state { |
1799 | int vslen; |
1800 | char version[10]; |
1801 | char *vstring; |
1802 | int vstrsize; |
1803 | int i; |
1804 | int proto1, proto2; |
1805 | }; |
1806 | crState(do_ssh_init_state); |
374330e2 |
1807 | |
51470298 |
1808 | crBegin(ssh->do_ssh_init_crstate); |
8df7a775 |
1809 | |
1810 | /* Search for the string "SSH-" in the input. */ |
51470298 |
1811 | s->i = 0; |
8df7a775 |
1812 | while (1) { |
1813 | static const int transS[] = { 1, 2, 2, 1 }; |
1814 | static const int transH[] = { 0, 0, 3, 0 }; |
1815 | static const int transminus[] = { 0, 0, 0, -1 }; |
32874aea |
1816 | if (c == 'S') |
51470298 |
1817 | s->i = transS[s->i]; |
32874aea |
1818 | else if (c == 'H') |
51470298 |
1819 | s->i = transH[s->i]; |
32874aea |
1820 | else if (c == '-') |
51470298 |
1821 | s->i = transminus[s->i]; |
32874aea |
1822 | else |
51470298 |
1823 | s->i = 0; |
1824 | if (s->i < 0) |
8df7a775 |
1825 | break; |
1826 | crReturn(1); /* get another character */ |
374330e2 |
1827 | } |
8df7a775 |
1828 | |
51470298 |
1829 | s->vstrsize = 16; |
1830 | s->vstring = smalloc(s->vstrsize); |
1831 | strcpy(s->vstring, "SSH-"); |
1832 | s->vslen = 4; |
1833 | s->i = 0; |
374330e2 |
1834 | while (1) { |
8df7a775 |
1835 | crReturn(1); /* get another char */ |
51470298 |
1836 | if (s->vslen >= s->vstrsize - 1) { |
1837 | s->vstrsize += 16; |
1838 | s->vstring = srealloc(s->vstring, s->vstrsize); |
32874aea |
1839 | } |
51470298 |
1840 | s->vstring[s->vslen++] = c; |
1841 | if (s->i >= 0) { |
374330e2 |
1842 | if (c == '-') { |
51470298 |
1843 | s->version[s->i] = '\0'; |
1844 | s->i = -1; |
1845 | } else if (s->i < sizeof(s->version) - 1) |
1846 | s->version[s->i++] = c; |
c4ffc4d0 |
1847 | } else if (c == '\012') |
374330e2 |
1848 | break; |
1849 | } |
1850 | |
51470298 |
1851 | ssh->agentfwd_enabled = FALSE; |
1852 | ssh->rdpkt2_state.incoming_sequence = 0; |
960e736a |
1853 | |
51470298 |
1854 | s->vstring[s->vslen] = 0; |
1855 | s->vstring[strcspn(s->vstring, "\r\n")] = '\0';/* remove EOL chars */ |
1856 | { |
1857 | char *vlog; |
1858 | vlog = smalloc(20 + s->vslen); |
1859 | sprintf(vlog, "Server version: %s", s->vstring); |
1860 | logevent(vlog); |
1861 | sfree(vlog); |
1862 | } |
1863 | ssh_detect_bugs(ssh, s->vstring); |
c5e9c988 |
1864 | |
adf799dd |
1865 | /* |
38d228a2 |
1866 | * Decide which SSH protocol version to support. |
adf799dd |
1867 | */ |
38d228a2 |
1868 | |
1869 | /* Anything strictly below "2.0" means protocol 1 is supported. */ |
51470298 |
1870 | s->proto1 = ssh_versioncmp(s->version, "2.0") < 0; |
38d228a2 |
1871 | /* Anything greater or equal to "1.99" means protocol 2 is supported. */ |
51470298 |
1872 | s->proto2 = ssh_versioncmp(s->version, "1.99") >= 0; |
38d228a2 |
1873 | |
51470298 |
1874 | if (cfg.sshprot == 0 && !s->proto1) { |
a8327734 |
1875 | bombout((ssh,"SSH protocol version 1 required by user but not provided by server")); |
38d228a2 |
1876 | crReturn(0); |
1877 | } |
51470298 |
1878 | if (cfg.sshprot == 3 && !s->proto2) { |
a8327734 |
1879 | bombout((ssh,"SSH protocol version 2 required by user but not provided by server")); |
38d228a2 |
1880 | crReturn(0); |
1881 | } |
1882 | |
51470298 |
1883 | if (s->proto2 && (cfg.sshprot >= 2 || !s->proto1)) { |
32874aea |
1884 | /* |
38d228a2 |
1885 | * Use v2 protocol. |
32874aea |
1886 | */ |
1887 | char verstring[80], vlog[100]; |
1888 | sprintf(verstring, "SSH-2.0-%s", sshver); |
51470298 |
1889 | SHA_Init(&ssh->exhashbase); |
32874aea |
1890 | /* |
1891 | * Hash our version string and their version string. |
1892 | */ |
51470298 |
1893 | sha_string(&ssh->exhashbase, verstring, strlen(verstring)); |
1894 | sha_string(&ssh->exhashbase, s->vstring, strcspn(s->vstring, "\r\n")); |
32874aea |
1895 | sprintf(vlog, "We claim version: %s", verstring); |
1896 | logevent(vlog); |
c4ffc4d0 |
1897 | strcat(verstring, "\012"); |
32874aea |
1898 | logevent("Using SSH protocol version 2"); |
51470298 |
1899 | sk_write(ssh->s, verstring, strlen(verstring)); |
1900 | ssh->protocol = ssh2_protocol; |
1901 | ssh->version = 2; |
1902 | ssh->s_rdpkt = ssh2_rdpkt; |
e5574168 |
1903 | } else { |
32874aea |
1904 | /* |
38d228a2 |
1905 | * Use v1 protocol. |
32874aea |
1906 | */ |
1907 | char verstring[80], vlog[100]; |
1908 | sprintf(verstring, "SSH-%s-%s", |
51470298 |
1909 | (ssh_versioncmp(s->version, "1.5") <= 0 ? s->version : "1.5"), |
32874aea |
1910 | sshver); |
1911 | sprintf(vlog, "We claim version: %s", verstring); |
1912 | logevent(vlog); |
c4ffc4d0 |
1913 | strcat(verstring, "\012"); |
759712a6 |
1914 | |
32874aea |
1915 | logevent("Using SSH protocol version 1"); |
51470298 |
1916 | sk_write(ssh->s, verstring, strlen(verstring)); |
1917 | ssh->protocol = ssh1_protocol; |
1918 | ssh->version = 1; |
1919 | ssh->s_rdpkt = ssh1_rdpkt; |
e5574168 |
1920 | } |
51470298 |
1921 | ssh->state = SSH_STATE_BEFORE_SIZE; |
8df7a775 |
1922 | |
51470298 |
1923 | sfree(s->vstring); |
50526e47 |
1924 | |
8df7a775 |
1925 | crFinish(0); |
1926 | } |
1927 | |
51470298 |
1928 | static void ssh_gotdata(Ssh ssh, unsigned char *data, int datalen) |
8df7a775 |
1929 | { |
51470298 |
1930 | crBegin(ssh->ssh_gotdata_crstate); |
8df7a775 |
1931 | |
1932 | /* |
1933 | * To begin with, feed the characters one by one to the |
1934 | * protocol initialisation / selection function do_ssh_init(). |
1935 | * When that returns 0, we're done with the initial greeting |
1936 | * exchange and can move on to packet discipline. |
1937 | */ |
1938 | while (1) { |
51470298 |
1939 | int ret; /* need not be kept across crReturn */ |
8df7a775 |
1940 | if (datalen == 0) |
1941 | crReturnV; /* more data please */ |
51470298 |
1942 | ret = do_ssh_init(ssh, *data); |
32874aea |
1943 | data++; |
1944 | datalen--; |
8df7a775 |
1945 | if (ret == 0) |
1946 | break; |
1947 | } |
1948 | |
1949 | /* |
1950 | * We emerge from that loop when the initial negotiation is |
1951 | * over and we have selected an s_rdpkt function. Now pass |
1952 | * everything to s_rdpkt, and then pass the resulting packets |
1953 | * to the proper protocol handler. |
1954 | */ |
1955 | if (datalen == 0) |
1956 | crReturnV; |
1957 | while (1) { |
1958 | while (datalen > 0) { |
51470298 |
1959 | if (ssh->s_rdpkt(ssh, &data, &datalen) == 0) { |
1960 | if (ssh->state == SSH_STATE_CLOSED) { |
e14d0f19 |
1961 | return; |
1962 | } |
51470298 |
1963 | ssh->protocol(ssh, NULL, 0, 1); |
1964 | if (ssh->state == SSH_STATE_CLOSED) { |
8df7a775 |
1965 | return; |
1966 | } |
1967 | } |
1968 | } |
1969 | crReturnV; |
1970 | } |
1971 | crFinishV; |
1972 | } |
1973 | |
32874aea |
1974 | static int ssh_closing(Plug plug, char *error_msg, int error_code, |
1975 | int calling_back) |
1976 | { |
51470298 |
1977 | Ssh ssh = (Ssh) plug; |
1978 | ssh->state = SSH_STATE_CLOSED; |
1979 | if (ssh->s) { |
1980 | sk_close(ssh->s); |
1981 | ssh->s = NULL; |
f3ab576e |
1982 | } |
7e78000d |
1983 | if (error_msg) { |
32874aea |
1984 | /* A socket error has occurred. */ |
247308b5 |
1985 | logevent(error_msg); |
a8327734 |
1986 | connection_fatal(ssh->frontend, error_msg); |
7e78000d |
1987 | } else { |
1988 | /* Otherwise, the remote side closed the connection normally. */ |
8df7a775 |
1989 | } |
7e78000d |
1990 | return 0; |
1991 | } |
1992 | |
32874aea |
1993 | static int ssh_receive(Plug plug, int urgent, char *data, int len) |
1994 | { |
51470298 |
1995 | Ssh ssh = (Ssh) plug; |
d8baa528 |
1996 | ssh_gotdata(ssh, (unsigned char *)data, len); |
51470298 |
1997 | if (ssh->state == SSH_STATE_CLOSED) { |
1998 | if (ssh->s) { |
1999 | sk_close(ssh->s); |
2000 | ssh->s = NULL; |
32874aea |
2001 | } |
2002 | return 0; |
3257deae |
2003 | } |
fef97f43 |
2004 | return 1; |
374330e2 |
2005 | } |
2006 | |
5471d09a |
2007 | static void ssh_sent(Plug plug, int bufsize) |
2008 | { |
51470298 |
2009 | Ssh ssh = (Ssh) plug; |
5471d09a |
2010 | /* |
2011 | * If the send backlog on the SSH socket itself clears, we |
2012 | * should unthrottle the whole world if it was throttled. |
2013 | */ |
2014 | if (bufsize < SSH_MAX_BACKLOG) |
51470298 |
2015 | ssh_throttle_all(ssh, 0, bufsize); |
5471d09a |
2016 | } |
2017 | |
fb09bf1c |
2018 | /* |
8df7a775 |
2019 | * Connect to specified host and port. |
2020 | * Returns an error message, or NULL on success. |
6e1ebb76 |
2021 | * Also places the canonical host name into `realhost'. It must be |
2022 | * freed by the caller. |
8df7a775 |
2023 | */ |
51470298 |
2024 | static char *connect_to_host(Ssh ssh, char *host, int port, |
2025 | char **realhost, int nodelay) |
8df7a775 |
2026 | { |
51470298 |
2027 | static const struct plug_function_table fn_table = { |
7e78000d |
2028 | ssh_closing, |
5471d09a |
2029 | ssh_receive, |
2030 | ssh_sent, |
2031 | NULL |
51470298 |
2032 | }; |
7e78000d |
2033 | |
8df7a775 |
2034 | SockAddr addr; |
2035 | char *err; |
8df7a775 |
2036 | |
51470298 |
2037 | ssh->savedhost = smalloc(1 + strlen(host)); |
2038 | if (!ssh->savedhost) |
8df7a775 |
2039 | fatalbox("Out of memory"); |
51470298 |
2040 | strcpy(ssh->savedhost, host); |
8df7a775 |
2041 | |
2042 | if (port < 0) |
2043 | port = 22; /* default ssh port */ |
51470298 |
2044 | ssh->savedport = port; |
8df7a775 |
2045 | |
2046 | /* |
2047 | * Try to find host. |
2048 | */ |
57356d63 |
2049 | logeventf(ssh, "Looking up host \"%s\"", host); |
b7a189f3 |
2050 | addr = name_lookup(host, port, realhost); |
70e5d0fd |
2051 | if ((err = sk_addr_error(addr)) != NULL) |
8df7a775 |
2052 | return err; |
2053 | |
8df7a775 |
2054 | /* |
2055 | * Open socket. |
2056 | */ |
3ad9d396 |
2057 | { |
57356d63 |
2058 | char addrbuf[100]; |
3ad9d396 |
2059 | sk_getaddr(addr, addrbuf, 100); |
57356d63 |
2060 | logeventf(ssh, "Connecting to %s port %d", addrbuf, port); |
3ad9d396 |
2061 | } |
51470298 |
2062 | ssh->fn = &fn_table; |
2063 | ssh->s = new_connection(addr, *realhost, port, 0, 1, nodelay, (Plug) ssh); |
70e5d0fd |
2064 | if ((err = sk_socket_error(ssh->s)) != NULL) { |
51470298 |
2065 | ssh->s = NULL; |
8df7a775 |
2066 | return err; |
67c4ba2e |
2067 | } |
8df7a775 |
2068 | |
8df7a775 |
2069 | return NULL; |
2070 | } |
2071 | |
2072 | /* |
5471d09a |
2073 | * Throttle or unthrottle the SSH connection. |
2074 | */ |
51470298 |
2075 | static void ssh1_throttle(Ssh ssh, int adjust) |
5471d09a |
2076 | { |
51470298 |
2077 | int old_count = ssh->v1_throttle_count; |
2078 | ssh->v1_throttle_count += adjust; |
2079 | assert(ssh->v1_throttle_count >= 0); |
2080 | if (ssh->v1_throttle_count && !old_count) { |
2081 | sk_set_frozen(ssh->s, 1); |
2082 | } else if (!ssh->v1_throttle_count && old_count) { |
2083 | sk_set_frozen(ssh->s, 0); |
5471d09a |
2084 | } |
2085 | } |
2086 | |
2087 | /* |
2088 | * Throttle or unthrottle _all_ local data streams (for when sends |
2089 | * on the SSH connection itself back up). |
2090 | */ |
51470298 |
2091 | static void ssh_throttle_all(Ssh ssh, int enable, int bufsize) |
5471d09a |
2092 | { |
2093 | int i; |
2094 | struct ssh_channel *c; |
2095 | |
51470298 |
2096 | if (enable == ssh->throttled_all) |
5471d09a |
2097 | return; |
51470298 |
2098 | ssh->throttled_all = enable; |
2099 | ssh->overall_bufsize = bufsize; |
2100 | if (!ssh->channels) |
5471d09a |
2101 | return; |
51470298 |
2102 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
5471d09a |
2103 | switch (c->type) { |
2104 | case CHAN_MAINSESSION: |
2105 | /* |
2106 | * This is treated separately, outside the switch. |
2107 | */ |
2108 | break; |
2109 | case CHAN_X11: |
2110 | x11_override_throttle(c->u.x11.s, enable); |
2111 | break; |
2112 | case CHAN_AGENT: |
2113 | /* Agent channels require no buffer management. */ |
2114 | break; |
2115 | case CHAN_SOCKDATA: |
2116 | pfd_override_throttle(c->u.x11.s, enable); |
2117 | break; |
2118 | } |
2119 | } |
2120 | } |
2121 | |
2122 | /* |
51470298 |
2123 | * Username and password input, abstracted off into routines |
2124 | * reusable in several places - even between SSH1 and SSH2. |
0405e71f |
2125 | */ |
0405e71f |
2126 | |
2127 | /* Set up a username or password input loop on a given buffer. */ |
ae9ae89f |
2128 | static void setup_userpass_input(Ssh ssh, char *buffer, int buflen, int echo) |
0405e71f |
2129 | { |
51470298 |
2130 | ssh->userpass_input_buffer = buffer; |
2131 | ssh->userpass_input_buflen = buflen; |
2132 | ssh->userpass_input_bufpos = 0; |
2133 | ssh->userpass_input_echo = echo; |
0405e71f |
2134 | } |
2135 | |
2136 | /* |
2137 | * Process some terminal data in the course of username/password |
2138 | * input. Returns >0 for success (line of input returned in |
2139 | * buffer), <0 for failure (user hit ^C/^D, bomb out and exit), 0 |
2140 | * for inconclusive (keep waiting for more input please). |
2141 | */ |
ae9ae89f |
2142 | static int process_userpass_input(Ssh ssh, unsigned char *in, int inlen) |
0405e71f |
2143 | { |
2144 | char c; |
2145 | |
2146 | while (inlen--) { |
2147 | switch (c = *in++) { |
2148 | case 10: |
2149 | case 13: |
51470298 |
2150 | ssh->userpass_input_buffer[ssh->userpass_input_bufpos] = 0; |
2151 | ssh->userpass_input_buffer[ssh->userpass_input_buflen-1] = 0; |
0405e71f |
2152 | return +1; |
2153 | break; |
2154 | case 8: |
2155 | case 127: |
51470298 |
2156 | if (ssh->userpass_input_bufpos > 0) { |
2157 | if (ssh->userpass_input_echo) |
2158 | c_write_str(ssh, "\b \b"); |
2159 | ssh->userpass_input_bufpos--; |
0405e71f |
2160 | } |
2161 | break; |
2162 | case 21: |
2163 | case 27: |
51470298 |
2164 | while (ssh->userpass_input_bufpos > 0) { |
2165 | if (ssh->userpass_input_echo) |
2166 | c_write_str(ssh, "\b \b"); |
2167 | ssh->userpass_input_bufpos--; |
0405e71f |
2168 | } |
2169 | break; |
2170 | case 3: |
2171 | case 4: |
2172 | return -1; |
2173 | break; |
2174 | default: |
2175 | if (((c >= ' ' && c <= '~') || |
2176 | ((unsigned char) c >= 160)) |
51470298 |
2177 | && ssh->userpass_input_bufpos < ssh->userpass_input_buflen-1) { |
2178 | ssh->userpass_input_buffer[ssh->userpass_input_bufpos++] = c; |
2179 | if (ssh->userpass_input_echo) |
2180 | c_write(ssh, &c, 1); |
0405e71f |
2181 | } |
2182 | break; |
2183 | } |
2184 | } |
2185 | return 0; |
2186 | } |
2187 | |
2188 | /* |
fb09bf1c |
2189 | * Handle the key exchange and user authentication phases. |
2190 | */ |
51470298 |
2191 | static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
fb09bf1c |
2192 | { |
2d466ffd |
2193 | int i, j; |
374330e2 |
2194 | unsigned char cookie[8]; |
2195 | struct RSAKey servkey, hostkey; |
2196 | struct MD5Context md5c; |
51470298 |
2197 | struct do_ssh1_login_state { |
2198 | int len; |
2199 | unsigned char *rsabuf, *keystr1, *keystr2; |
2200 | unsigned long supported_ciphers_mask, supported_auths_mask; |
2201 | int tried_publickey, tried_agent; |
2202 | int tis_auth_refused, ccard_auth_refused; |
2203 | unsigned char session_id[16]; |
2204 | int cipher_type; |
2205 | char username[100]; |
2206 | void *publickey_blob; |
2207 | int publickey_bloblen; |
2208 | char password[100]; |
2209 | char prompt[200]; |
2210 | int pos; |
2211 | char c; |
2212 | int pwpkt_type; |
2213 | unsigned char request[5], *response, *p; |
2214 | int responselen; |
2215 | int keyi, nkeys; |
2216 | int authed; |
2217 | struct RSAKey key; |
2218 | Bignum challenge; |
2219 | char *commentp; |
2220 | int commentlen; |
2221 | }; |
2222 | crState(do_ssh1_login_state); |
2223 | |
2224 | crBegin(ssh->do_ssh1_login_crstate); |
374330e2 |
2225 | |
32874aea |
2226 | if (!ispkt) |
2227 | crWaitUntil(ispkt); |
374330e2 |
2228 | |
51470298 |
2229 | if (ssh->pktin.type != SSH1_SMSG_PUBLIC_KEY) { |
a8327734 |
2230 | bombout((ssh,"Public key packet not received")); |
32874aea |
2231 | crReturn(0); |
8d5de777 |
2232 | } |
374330e2 |
2233 | |
c5e9c988 |
2234 | logevent("Received public keys"); |
374330e2 |
2235 | |
51470298 |
2236 | memcpy(cookie, ssh->pktin.body, 8); |
374330e2 |
2237 | |
51470298 |
2238 | i = makekey(ssh->pktin.body + 8, &servkey, &s->keystr1, 0); |
2239 | j = makekey(ssh->pktin.body + 8 + i, &hostkey, &s->keystr2, 0); |
374330e2 |
2240 | |
c5e9c988 |
2241 | /* |
1c2a93c4 |
2242 | * Log the host key fingerprint. |
c5e9c988 |
2243 | */ |
c5e9c988 |
2244 | { |
2245 | char logmsg[80]; |
1c2a93c4 |
2246 | logevent("Host key fingerprint is:"); |
c5e9c988 |
2247 | strcpy(logmsg, " "); |
32874aea |
2248 | hostkey.comment = NULL; |
2249 | rsa_fingerprint(logmsg + strlen(logmsg), |
2250 | sizeof(logmsg) - strlen(logmsg), &hostkey); |
c5e9c988 |
2251 | logevent(logmsg); |
2252 | } |
2253 | |
51470298 |
2254 | ssh->v1_remote_protoflags = GET_32BIT(ssh->pktin.body + 8 + i + j); |
2255 | s->supported_ciphers_mask = GET_32BIT(ssh->pktin.body + 12 + i + j); |
2256 | s->supported_auths_mask = GET_32BIT(ssh->pktin.body + 16 + i + j); |
bea1ef5f |
2257 | |
51470298 |
2258 | ssh->v1_local_protoflags = |
2259 | ssh->v1_remote_protoflags & SSH1_PROTOFLAGS_SUPPORTED; |
2260 | ssh->v1_local_protoflags |= SSH1_PROTOFLAG_SCREEN_NUMBER; |
b96dc54c |
2261 | |
c5e9c988 |
2262 | MD5Init(&md5c); |
51470298 |
2263 | MD5Update(&md5c, s->keystr2, hostkey.bytes); |
2264 | MD5Update(&md5c, s->keystr1, servkey.bytes); |
2265 | MD5Update(&md5c, ssh->pktin.body, 8); |
2266 | MD5Final(s->session_id, &md5c); |
374330e2 |
2267 | |
32874aea |
2268 | for (i = 0; i < 32; i++) |
51470298 |
2269 | ssh->session_key[i] = random_byte(); |
374330e2 |
2270 | |
51470298 |
2271 | s->len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes); |
374330e2 |
2272 | |
51470298 |
2273 | s->rsabuf = smalloc(s->len); |
2274 | if (!s->rsabuf) |
374330e2 |
2275 | fatalbox("Out of memory"); |
2276 | |
89ee5268 |
2277 | /* |
2278 | * Verify the host key. |
2279 | */ |
2280 | { |
32874aea |
2281 | /* |
2282 | * First format the key into a string. |
2283 | */ |
2284 | int len = rsastr_len(&hostkey); |
2285 | char fingerprint[100]; |
2286 | char *keystr = smalloc(len); |
2287 | if (!keystr) |
2288 | fatalbox("Out of memory"); |
2289 | rsastr_fmt(keystr, &hostkey); |
2290 | rsa_fingerprint(fingerprint, sizeof(fingerprint), &hostkey); |
a8327734 |
2291 | verify_ssh_host_key(ssh->frontend, |
2292 | ssh->savedhost, ssh->savedport, "rsa", keystr, |
32874aea |
2293 | fingerprint); |
2294 | sfree(keystr); |
2295 | } |
2296 | |
2297 | for (i = 0; i < 32; i++) { |
51470298 |
2298 | s->rsabuf[i] = ssh->session_key[i]; |
374330e2 |
2299 | if (i < 16) |
51470298 |
2300 | s->rsabuf[i] ^= s->session_id[i]; |
374330e2 |
2301 | } |
2302 | |
2303 | if (hostkey.bytes > servkey.bytes) { |
51470298 |
2304 | rsaencrypt(s->rsabuf, 32, &servkey); |
2305 | rsaencrypt(s->rsabuf, servkey.bytes, &hostkey); |
374330e2 |
2306 | } else { |
51470298 |
2307 | rsaencrypt(s->rsabuf, 32, &hostkey); |
2308 | rsaencrypt(s->rsabuf, hostkey.bytes, &servkey); |
374330e2 |
2309 | } |
2310 | |
c5e9c988 |
2311 | logevent("Encrypted session key"); |
2312 | |
ca20bfcf |
2313 | { |
2314 | int cipher_chosen = 0, warn = 0; |
2315 | char *cipher_string = NULL; |
51470298 |
2316 | int i; |
ca20bfcf |
2317 | for (i = 0; !cipher_chosen && i < CIPHER_MAX; i++) { |
2318 | int next_cipher = cfg.ssh_cipherlist[i]; |
2319 | if (next_cipher == CIPHER_WARN) { |
2320 | /* If/when we choose a cipher, warn about it */ |
2321 | warn = 1; |
2322 | } else if (next_cipher == CIPHER_AES) { |
2323 | /* XXX Probably don't need to mention this. */ |
2324 | logevent("AES not supported in SSH1, skipping"); |
2325 | } else { |
2326 | switch (next_cipher) { |
51470298 |
2327 | case CIPHER_3DES: s->cipher_type = SSH_CIPHER_3DES; |
ca20bfcf |
2328 | cipher_string = "3DES"; break; |
51470298 |
2329 | case CIPHER_BLOWFISH: s->cipher_type = SSH_CIPHER_BLOWFISH; |
ca20bfcf |
2330 | cipher_string = "Blowfish"; break; |
51470298 |
2331 | case CIPHER_DES: s->cipher_type = SSH_CIPHER_DES; |
ca20bfcf |
2332 | cipher_string = "single-DES"; break; |
2333 | } |
51470298 |
2334 | if (s->supported_ciphers_mask & (1 << s->cipher_type)) |
ca20bfcf |
2335 | cipher_chosen = 1; |
2336 | } |
2337 | } |
2338 | if (!cipher_chosen) { |
51470298 |
2339 | if ((s->supported_ciphers_mask & (1 << SSH_CIPHER_3DES)) == 0) |
a8327734 |
2340 | bombout((ssh,"Server violates SSH 1 protocol by not " |
ca20bfcf |
2341 | "supporting 3DES encryption")); |
2342 | else |
2343 | /* shouldn't happen */ |
a8327734 |
2344 | bombout((ssh,"No supported ciphers found")); |
a99a05c0 |
2345 | crReturn(0); |
2346 | } |
ca20bfcf |
2347 | |
2348 | /* Warn about chosen cipher if necessary. */ |
2349 | if (warn) |
a8327734 |
2350 | askcipher(ssh->frontend, cipher_string, 0); |
bea1ef5f |
2351 | } |
ca20bfcf |
2352 | |
51470298 |
2353 | switch (s->cipher_type) { |
32874aea |
2354 | case SSH_CIPHER_3DES: |
2355 | logevent("Using 3DES encryption"); |
2356 | break; |
2357 | case SSH_CIPHER_DES: |
2358 | logevent("Using single-DES encryption"); |
2359 | break; |
2360 | case SSH_CIPHER_BLOWFISH: |
2361 | logevent("Using Blowfish encryption"); |
2362 | break; |
c5e9c988 |
2363 | } |
bea1ef5f |
2364 | |
51470298 |
2365 | send_packet(ssh, SSH1_CMSG_SESSION_KEY, |
2366 | PKT_CHAR, s->cipher_type, |
32874aea |
2367 | PKT_DATA, cookie, 8, |
51470298 |
2368 | PKT_CHAR, (s->len * 8) >> 8, PKT_CHAR, (s->len * 8) & 0xFF, |
2369 | PKT_DATA, s->rsabuf, s->len, |
2370 | PKT_INT, ssh->v1_local_protoflags, PKT_END); |
fb09bf1c |
2371 | |
c5e9c988 |
2372 | logevent("Trying to enable encryption..."); |
374330e2 |
2373 | |
51470298 |
2374 | sfree(s->rsabuf); |
374330e2 |
2375 | |
51470298 |
2376 | ssh->cipher = (s->cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish_ssh1 : |
2377 | s->cipher_type == SSH_CIPHER_DES ? &ssh_des : |
2378 | &ssh_3des); |
371e569c |
2379 | ssh->v1_cipher_ctx = ssh->cipher->make_context(); |
2380 | ssh->cipher->sesskey(ssh->v1_cipher_ctx, ssh->session_key); |
57356d63 |
2381 | logeventf(ssh, "Initialised %s encryption", ssh->cipher->text_name); |
374330e2 |
2382 | |
0183b242 |
2383 | ssh->crcda_ctx = crcda_make_context(); |
2384 | logevent("Installing CRC compensation attack detector"); |
2385 | |
fb09bf1c |
2386 | crWaitUntil(ispkt); |
374330e2 |
2387 | |
51470298 |
2388 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS) { |
a8327734 |
2389 | bombout((ssh,"Encryption not successfully enabled")); |
32874aea |
2390 | crReturn(0); |
8d5de777 |
2391 | } |
374330e2 |
2392 | |
c5e9c988 |
2393 | logevent("Successfully started encryption"); |
2394 | |
374330e2 |
2395 | fflush(stdout); |
2396 | { |
67779be7 |
2397 | if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { |
c0a81592 |
2398 | if (ssh_get_line && !ssh_getline_pw_only) { |
32874aea |
2399 | if (!ssh_get_line("login as: ", |
51470298 |
2400 | s->username, sizeof(s->username), FALSE)) { |
32874aea |
2401 | /* |
2402 | * get_line failed to get a username. |
2403 | * Terminate. |
2404 | */ |
2405 | logevent("No username provided. Abandoning session."); |
51470298 |
2406 | ssh->state = SSH_STATE_CLOSED; |
32874aea |
2407 | crReturn(1); |
2408 | } |
2409 | } else { |
51470298 |
2410 | int ret; /* need not be kept over crReturn */ |
2411 | c_write_str(ssh, "login as: "); |
2412 | ssh->send_ok = 1; |
0405e71f |
2413 | |
51470298 |
2414 | setup_userpass_input(ssh, s->username, sizeof(s->username), 1); |
0405e71f |
2415 | do { |
32874aea |
2416 | crWaitUntil(!ispkt); |
51470298 |
2417 | ret = process_userpass_input(ssh, in, inlen); |
0405e71f |
2418 | } while (ret == 0); |
2419 | if (ret < 0) |
2420 | cleanup_exit(0); |
51470298 |
2421 | c_write_str(ssh, "\r\n"); |
32874aea |
2422 | } |
2423 | } else { |
51470298 |
2424 | strncpy(s->username, cfg.username, sizeof(s->username)); |
2425 | s->username[sizeof(s->username)-1] = '\0'; |
374330e2 |
2426 | } |
fb09bf1c |
2427 | |
51470298 |
2428 | send_packet(ssh, SSH1_CMSG_USER, PKT_STR, s->username, PKT_END); |
c5e9c988 |
2429 | { |
51470298 |
2430 | char userlog[22 + sizeof(s->username)]; |
2431 | sprintf(userlog, "Sent username \"%s\"", s->username); |
c5e9c988 |
2432 | logevent(userlog); |
32874aea |
2433 | if (flags & FLAG_INTERACTIVE && |
2434 | (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) { |
3c8e959b |
2435 | strcat(userlog, "\r\n"); |
51470298 |
2436 | c_write_str(ssh, userlog); |
3c8e959b |
2437 | } |
c5e9c988 |
2438 | } |
374330e2 |
2439 | } |
2440 | |
fb09bf1c |
2441 | crWaitUntil(ispkt); |
374330e2 |
2442 | |
51470298 |
2443 | if ((ssh->remote_bugs & BUG_CHOKES_ON_RSA)) { |
0df73905 |
2444 | /* We must not attempt PK auth. Pretend we've already tried it. */ |
51470298 |
2445 | s->tried_publickey = s->tried_agent = 1; |
0df73905 |
2446 | } else { |
51470298 |
2447 | s->tried_publickey = s->tried_agent = 0; |
0df73905 |
2448 | } |
51470298 |
2449 | s->tis_auth_refused = s->ccard_auth_refused = 0; |
396778f1 |
2450 | /* Load the public half of cfg.keyfile so we notice if it's in Pageant */ |
2451 | if (*cfg.keyfile) { |
51470298 |
2452 | if (!rsakey_pubblob(cfg.keyfile, |
2453 | &s->publickey_blob, &s->publickey_bloblen)) |
2454 | s->publickey_blob = NULL; |
396778f1 |
2455 | } else |
51470298 |
2456 | s->publickey_blob = NULL; |
7cca0d81 |
2457 | |
51470298 |
2458 | while (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
2459 | s->pwpkt_type = SSH1_CMSG_AUTH_PASSWORD; |
614a20a0 |
2460 | |
51470298 |
2461 | if (agent_exists() && !s->tried_agent) { |
32874aea |
2462 | /* |
2463 | * Attempt RSA authentication using Pageant. |
2464 | */ |
32874aea |
2465 | void *r; |
2466 | |
51470298 |
2467 | s->authed = FALSE; |
2468 | s->tried_agent = 1; |
32874aea |
2469 | logevent("Pageant is running. Requesting keys."); |
2470 | |
2471 | /* Request the keys held by the agent. */ |
51470298 |
2472 | PUT_32BIT(s->request, 1); |
2473 | s->request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES; |
2474 | agent_query(s->request, 5, &r, &s->responselen); |
2475 | s->response = (unsigned char *) r; |
2476 | if (s->response && s->responselen >= 5 && |
2477 | s->response[4] == SSH1_AGENT_RSA_IDENTITIES_ANSWER) { |
2478 | s->p = s->response + 5; |
2479 | s->nkeys = GET_32BIT(s->p); |
2480 | s->p += 4; |
32874aea |
2481 | { |
2482 | char buf[64]; |
51470298 |
2483 | sprintf(buf, "Pageant has %d SSH1 keys", s->nkeys); |
32874aea |
2484 | logevent(buf); |
2485 | } |
51470298 |
2486 | for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) { |
32874aea |
2487 | { |
2488 | char buf[64]; |
51470298 |
2489 | sprintf(buf, "Trying Pageant key #%d", s->keyi); |
32874aea |
2490 | logevent(buf); |
2491 | } |
51470298 |
2492 | if (s->publickey_blob && |
2493 | !memcmp(s->p, s->publickey_blob, |
2494 | s->publickey_bloblen)) { |
396778f1 |
2495 | logevent("This key matches configured key file"); |
51470298 |
2496 | s->tried_publickey = 1; |
396778f1 |
2497 | } |
51470298 |
2498 | s->p += 4; |
2499 | s->p += ssh1_read_bignum(s->p, &s->key.exponent); |
2500 | s->p += ssh1_read_bignum(s->p, &s->key.modulus); |
2501 | s->commentlen = GET_32BIT(s->p); |
2502 | s->p += 4; |
d8baa528 |
2503 | s->commentp = (char *)s->p; |
51470298 |
2504 | s->p += s->commentlen; |
2505 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
2506 | PKT_BIGNUM, s->key.modulus, PKT_END); |
32874aea |
2507 | crWaitUntil(ispkt); |
51470298 |
2508 | if (ssh->pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
32874aea |
2509 | logevent("Key refused"); |
2510 | continue; |
2511 | } |
2512 | logevent("Received RSA challenge"); |
51470298 |
2513 | ssh1_read_bignum(ssh->pktin.body, &s->challenge); |
32874aea |
2514 | { |
2515 | char *agentreq, *q, *ret; |
2d466ffd |
2516 | void *vret; |
32874aea |
2517 | int len, retlen; |
2518 | len = 1 + 4; /* message type, bit count */ |
51470298 |
2519 | len += ssh1_bignum_length(s->key.exponent); |
2520 | len += ssh1_bignum_length(s->key.modulus); |
2521 | len += ssh1_bignum_length(s->challenge); |
32874aea |
2522 | len += 16; /* session id */ |
2523 | len += 4; /* response format */ |
2524 | agentreq = smalloc(4 + len); |
2525 | PUT_32BIT(agentreq, len); |
2526 | q = agentreq + 4; |
2527 | *q++ = SSH1_AGENTC_RSA_CHALLENGE; |
51470298 |
2528 | PUT_32BIT(q, bignum_bitcount(s->key.modulus)); |
32874aea |
2529 | q += 4; |
51470298 |
2530 | q += ssh1_write_bignum(q, s->key.exponent); |
2531 | q += ssh1_write_bignum(q, s->key.modulus); |
2532 | q += ssh1_write_bignum(q, s->challenge); |
2533 | memcpy(q, s->session_id, 16); |
32874aea |
2534 | q += 16; |
2535 | PUT_32BIT(q, 1); /* response format */ |
2d466ffd |
2536 | agent_query(agentreq, len + 4, &vret, &retlen); |
2537 | ret = vret; |
32874aea |
2538 | sfree(agentreq); |
2539 | if (ret) { |
2540 | if (ret[4] == SSH1_AGENT_RSA_RESPONSE) { |
2541 | logevent("Sending Pageant's response"); |
51470298 |
2542 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
32874aea |
2543 | PKT_DATA, ret + 5, 16, |
2544 | PKT_END); |
2545 | sfree(ret); |
2546 | crWaitUntil(ispkt); |
51470298 |
2547 | if (ssh->pktin.type == SSH1_SMSG_SUCCESS) { |
32874aea |
2548 | logevent |
2549 | ("Pageant's response accepted"); |
2550 | if (flags & FLAG_VERBOSE) { |
51470298 |
2551 | c_write_str(ssh, "Authenticated using" |
2552 | " RSA key \""); |
2553 | c_write(ssh, s->commentp, |
2554 | s->commentlen); |
2555 | c_write_str(ssh, "\" from agent\r\n"); |
32874aea |
2556 | } |
51470298 |
2557 | s->authed = TRUE; |
32874aea |
2558 | } else |
2559 | logevent |
2560 | ("Pageant's response not accepted"); |
2561 | } else { |
2562 | logevent |
2563 | ("Pageant failed to answer challenge"); |
2564 | sfree(ret); |
2565 | } |
2566 | } else { |
2567 | logevent("No reply received from Pageant"); |
2568 | } |
2569 | } |
51470298 |
2570 | freebn(s->key.exponent); |
2571 | freebn(s->key.modulus); |
2572 | freebn(s->challenge); |
2573 | if (s->authed) |
32874aea |
2574 | break; |
2575 | } |
2576 | } |
51470298 |
2577 | if (s->authed) |
32874aea |
2578 | break; |
2579 | } |
51470298 |
2580 | if (*cfg.keyfile && !s->tried_publickey) |
2581 | s->pwpkt_type = SSH1_CMSG_AUTH_RSA; |
32874aea |
2582 | |
614a20a0 |
2583 | if (cfg.try_tis_auth && |
51470298 |
2584 | (s->supported_auths_mask & (1 << SSH1_AUTH_TIS)) && |
2585 | !s->tis_auth_refused) { |
2586 | s->pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE; |
32874aea |
2587 | logevent("Requested TIS authentication"); |
51470298 |
2588 | send_packet(ssh, SSH1_CMSG_AUTH_TIS, PKT_END); |
32874aea |
2589 | crWaitUntil(ispkt); |
51470298 |
2590 | if (ssh->pktin.type != SSH1_SMSG_AUTH_TIS_CHALLENGE) { |
32874aea |
2591 | logevent("TIS authentication declined"); |
2592 | if (flags & FLAG_INTERACTIVE) |
51470298 |
2593 | c_write_str(ssh, "TIS authentication refused.\r\n"); |
2594 | s->tis_auth_refused = 1; |
614a20a0 |
2595 | continue; |
32874aea |
2596 | } else { |
51470298 |
2597 | int challengelen = GET_32BIT(ssh->pktin.body); |
32874aea |
2598 | logevent("Received TIS challenge"); |
51470298 |
2599 | if (challengelen > sizeof(s->prompt) - 1) |
2600 | challengelen = sizeof(s->prompt) - 1;/* prevent overrun */ |
2601 | memcpy(s->prompt, ssh->pktin.body + 4, challengelen); |
614a20a0 |
2602 | /* Prompt heuristic comes from OpenSSH */ |
51470298 |
2603 | strncpy(s->prompt + challengelen, |
2604 | memchr(s->prompt, '\n', challengelen) ? |
614a20a0 |
2605 | "": "\r\nResponse: ", |
51470298 |
2606 | (sizeof s->prompt) - challengelen); |
2607 | s->prompt[(sizeof s->prompt) - 1] = '\0'; |
32874aea |
2608 | } |
2609 | } |
614a20a0 |
2610 | if (cfg.try_tis_auth && |
51470298 |
2611 | (s->supported_auths_mask & (1 << SSH1_AUTH_CCARD)) && |
2612 | !s->ccard_auth_refused) { |
2613 | s->pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE; |
32874aea |
2614 | logevent("Requested CryptoCard authentication"); |
51470298 |
2615 | send_packet(ssh, SSH1_CMSG_AUTH_CCARD, PKT_END); |
32874aea |
2616 | crWaitUntil(ispkt); |
51470298 |
2617 | if (ssh->pktin.type != SSH1_SMSG_AUTH_CCARD_CHALLENGE) { |
32874aea |
2618 | logevent("CryptoCard authentication declined"); |
51470298 |
2619 | c_write_str(ssh, "CryptoCard authentication refused.\r\n"); |
2620 | s->ccard_auth_refused = 1; |
614a20a0 |
2621 | continue; |
32874aea |
2622 | } else { |
51470298 |
2623 | int challengelen = GET_32BIT(ssh->pktin.body); |
32874aea |
2624 | logevent("Received CryptoCard challenge"); |
51470298 |
2625 | if (challengelen > sizeof(s->prompt) - 1) |
2626 | challengelen = sizeof(s->prompt) - 1;/* prevent overrun */ |
2627 | memcpy(s->prompt, ssh->pktin.body + 4, challengelen); |
2628 | strncpy(s->prompt + challengelen, |
2629 | memchr(s->prompt, '\n', challengelen) ? |
614a20a0 |
2630 | "" : "\r\nResponse: ", |
51470298 |
2631 | sizeof(s->prompt) - challengelen); |
2632 | s->prompt[sizeof(s->prompt) - 1] = '\0'; |
32874aea |
2633 | } |
2634 | } |
51470298 |
2635 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
2636 | sprintf(s->prompt, "%.90s@%.90s's password: ", |
2637 | s->username, ssh->savedhost); |
32874aea |
2638 | } |
51470298 |
2639 | if (s->pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
32874aea |
2640 | char *comment = NULL; |
231ee168 |
2641 | int type; |
2642 | char msgbuf[256]; |
32874aea |
2643 | if (flags & FLAG_VERBOSE) |
51470298 |
2644 | c_write_str(ssh, "Trying public key authentication.\r\n"); |
57356d63 |
2645 | logeventf(ssh, "Trying public key \"%s\"", cfg.keyfile); |
231ee168 |
2646 | type = key_type(cfg.keyfile); |
2647 | if (type != SSH_KEYTYPE_SSH1) { |
2648 | sprintf(msgbuf, "Key is of wrong type (%s)", |
2649 | key_type_to_str(type)); |
2650 | logevent(msgbuf); |
51470298 |
2651 | c_write_str(ssh, msgbuf); |
2652 | c_write_str(ssh, "\r\n"); |
2653 | s->tried_publickey = 1; |
231ee168 |
2654 | continue; |
2655 | } |
32874aea |
2656 | if (!rsakey_encrypted(cfg.keyfile, &comment)) { |
2657 | if (flags & FLAG_VERBOSE) |
51470298 |
2658 | c_write_str(ssh, "No passphrase required.\r\n"); |
32874aea |
2659 | goto tryauth; |
2660 | } |
51470298 |
2661 | sprintf(s->prompt, "Passphrase for key \"%.100s\": ", comment); |
32874aea |
2662 | sfree(comment); |
2663 | } |
a52f067e |
2664 | |
614a20a0 |
2665 | /* |
2666 | * Show password prompt, having first obtained it via a TIS |
2667 | * or CryptoCard exchange if we're doing TIS or CryptoCard |
2668 | * authentication. |
2669 | */ |
fa17a66e |
2670 | if (ssh_get_line) { |
51470298 |
2671 | if (!ssh_get_line(s->prompt, s->password, |
2672 | sizeof(s->password), TRUE)) { |
32874aea |
2673 | /* |
2674 | * get_line failed to get a password (for example |
2675 | * because one was supplied on the command line |
2676 | * which has already failed to work). Terminate. |
2677 | */ |
51470298 |
2678 | send_packet(ssh, SSH1_MSG_DISCONNECT, |
2bc6a386 |
2679 | PKT_STR, "No more passwords available to try", |
2680 | PKT_END); |
247308b5 |
2681 | logevent("Unable to authenticate"); |
a8327734 |
2682 | connection_fatal(ssh->frontend, "Unable to authenticate"); |
51470298 |
2683 | ssh->state = SSH_STATE_CLOSED; |
32874aea |
2684 | crReturn(1); |
2685 | } |
fb09bf1c |
2686 | } else { |
614a20a0 |
2687 | /* Prompt may have come from server. We've munged it a bit, so |
2688 | * we know it to be zero-terminated at least once. */ |
51470298 |
2689 | int ret; /* need not be saved over crReturn */ |
2690 | c_write_untrusted(ssh, s->prompt, strlen(s->prompt)); |
2691 | s->pos = 0; |
0405e71f |
2692 | |
51470298 |
2693 | setup_userpass_input(ssh, s->password, sizeof(s->password), 0); |
0405e71f |
2694 | do { |
32874aea |
2695 | crWaitUntil(!ispkt); |
51470298 |
2696 | ret = process_userpass_input(ssh, in, inlen); |
0405e71f |
2697 | } while (ret == 0); |
2698 | if (ret < 0) |
2699 | cleanup_exit(0); |
51470298 |
2700 | c_write_str(ssh, "\r\n"); |
32874aea |
2701 | } |
2702 | |
2703 | tryauth: |
51470298 |
2704 | if (s->pwpkt_type == SSH1_CMSG_AUTH_RSA) { |
32874aea |
2705 | /* |
2706 | * Try public key authentication with the specified |
2707 | * key file. |
2708 | */ |
51470298 |
2709 | s->tried_publickey = 1; |
2710 | |
2711 | { |
2712 | int ret = loadrsakey(cfg.keyfile, &s->key, s->password); |
2713 | if (ret == 0) { |
2714 | c_write_str(ssh, "Couldn't load private key from "); |
2715 | c_write_str(ssh, cfg.keyfile); |
2716 | c_write_str(ssh, ".\r\n"); |
2717 | continue; /* go and try password */ |
2718 | } |
2719 | if (ret == -1) { |
2720 | c_write_str(ssh, "Wrong passphrase.\r\n"); |
2721 | s->tried_publickey = 0; |
2722 | continue; /* try again */ |
2723 | } |
32874aea |
2724 | } |
2725 | |
2726 | /* |
2727 | * Send a public key attempt. |
2728 | */ |
51470298 |
2729 | send_packet(ssh, SSH1_CMSG_AUTH_RSA, |
2730 | PKT_BIGNUM, s->key.modulus, PKT_END); |
32874aea |
2731 | |
2732 | crWaitUntil(ispkt); |
51470298 |
2733 | if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
2734 | c_write_str(ssh, "Server refused our public key.\r\n"); |
32874aea |
2735 | continue; /* go and try password */ |
2736 | } |
51470298 |
2737 | if (ssh->pktin.type != SSH1_SMSG_AUTH_RSA_CHALLENGE) { |
a8327734 |
2738 | bombout((ssh,"Bizarre response to offer of public key")); |
32874aea |
2739 | crReturn(0); |
2740 | } |
32874aea |
2741 | |
51470298 |
2742 | { |
2743 | int i; |
2744 | unsigned char buffer[32]; |
2745 | Bignum challenge, response; |
2746 | |
2747 | ssh1_read_bignum(ssh->pktin.body, &challenge); |
2748 | response = rsadecrypt(challenge, &s->key); |
2749 | freebn(s->key.private_exponent);/* burn the evidence */ |
32874aea |
2750 | |
51470298 |
2751 | for (i = 0; i < 32; i++) { |
2752 | buffer[i] = bignum_byte(response, 31 - i); |
2753 | } |
2754 | |
2755 | MD5Init(&md5c); |
2756 | MD5Update(&md5c, buffer, 32); |
2757 | MD5Update(&md5c, s->session_id, 16); |
2758 | MD5Final(buffer, &md5c); |
32874aea |
2759 | |
51470298 |
2760 | send_packet(ssh, SSH1_CMSG_AUTH_RSA_RESPONSE, |
2761 | PKT_DATA, buffer, 16, PKT_END); |
2762 | |
2763 | freebn(challenge); |
2764 | freebn(response); |
2765 | } |
32874aea |
2766 | |
2767 | crWaitUntil(ispkt); |
51470298 |
2768 | if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
32874aea |
2769 | if (flags & FLAG_VERBOSE) |
51470298 |
2770 | c_write_str(ssh, "Failed to authenticate with" |
2771 | " our public key.\r\n"); |
32874aea |
2772 | continue; /* go and try password */ |
51470298 |
2773 | } else if (ssh->pktin.type != SSH1_SMSG_SUCCESS) { |
a8327734 |
2774 | bombout((ssh,"Bizarre response to RSA authentication response")); |
32874aea |
2775 | crReturn(0); |
2776 | } |
2777 | |
2778 | break; /* we're through! */ |
2779 | } else { |
51470298 |
2780 | if (s->pwpkt_type == SSH1_CMSG_AUTH_PASSWORD) { |
32874aea |
2781 | /* |
2782 | * Defence against traffic analysis: we send a |
2783 | * whole bunch of packets containing strings of |
2784 | * different lengths. One of these strings is the |
2785 | * password, in a SSH1_CMSG_AUTH_PASSWORD packet. |
2786 | * The others are all random data in |
2787 | * SSH1_MSG_IGNORE packets. This way a passive |
2788 | * listener can't tell which is the password, and |
2789 | * hence can't deduce the password length. |
2790 | * |
2791 | * Anybody with a password length greater than 16 |
2792 | * bytes is going to have enough entropy in their |
2793 | * password that a listener won't find it _that_ |
2794 | * much help to know how long it is. So what we'll |
2795 | * do is: |
2796 | * |
2797 | * - if password length < 16, we send 15 packets |
2798 | * containing string lengths 1 through 15 |
2799 | * |
2800 | * - otherwise, we let N be the nearest multiple |
2801 | * of 8 below the password length, and send 8 |
2802 | * packets containing string lengths N through |
2803 | * N+7. This won't obscure the order of |
2804 | * magnitude of the password length, but it will |
2805 | * introduce a bit of extra uncertainty. |
2806 | * |
2807 | * A few servers (the old 1.2.18 through 1.2.22) |
2808 | * can't deal with SSH1_MSG_IGNORE. For these |
2809 | * servers, we need an alternative defence. We make |
2810 | * use of the fact that the password is interpreted |
2811 | * as a C string: so we can append a NUL, then some |
2812 | * random data. |
bd358db1 |
2813 | * |
2814 | * One server (a Cisco one) can deal with neither |
2815 | * SSH1_MSG_IGNORE _nor_ a padded password string. |
2816 | * For this server we are left with no defences |
2817 | * against password length sniffing. |
32874aea |
2818 | */ |
51470298 |
2819 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) { |
b17c8428 |
2820 | /* |
2821 | * The server can deal with SSH1_MSG_IGNORE, so |
2822 | * we can use the primary defence. |
2823 | */ |
32874aea |
2824 | int bottom, top, pwlen, i; |
2825 | char *randomstr; |
2826 | |
51470298 |
2827 | pwlen = strlen(s->password); |
32874aea |
2828 | if (pwlen < 16) { |
2829 | bottom = 0; /* zero length passwords are OK! :-) */ |
2830 | top = 15; |
2831 | } else { |
2832 | bottom = pwlen & ~7; |
2833 | top = bottom + 7; |
2834 | } |
2835 | |
2836 | assert(pwlen >= bottom && pwlen <= top); |
2837 | |
2838 | randomstr = smalloc(top + 1); |
2839 | |
2840 | for (i = bottom; i <= top; i++) { |
2841 | if (i == pwlen) |
51470298 |
2842 | defer_packet(ssh, s->pwpkt_type, |
2843 | PKT_STR, s->password, PKT_END); |
32874aea |
2844 | else { |
2845 | for (j = 0; j < i; j++) { |
2846 | do { |
2847 | randomstr[j] = random_byte(); |
2848 | } while (randomstr[j] == '\0'); |
2849 | } |
2850 | randomstr[i] = '\0'; |
51470298 |
2851 | defer_packet(ssh, SSH1_MSG_IGNORE, |
32874aea |
2852 | PKT_STR, randomstr, PKT_END); |
2853 | } |
2854 | } |
bd358db1 |
2855 | logevent("Sending password with camouflage packets"); |
51470298 |
2856 | ssh_pkt_defersend(ssh); |
bd358db1 |
2857 | } |
51470298 |
2858 | else if (!(ssh->remote_bugs & BUG_NEEDS_SSH1_PLAIN_PASSWORD)) { |
b17c8428 |
2859 | /* |
2860 | * The server can't deal with SSH1_MSG_IGNORE |
2861 | * but can deal with padded passwords, so we |
2862 | * can use the secondary defence. |
2863 | */ |
bd358db1 |
2864 | char string[64]; |
51470298 |
2865 | char *ss; |
bd358db1 |
2866 | int len; |
2867 | |
51470298 |
2868 | len = strlen(s->password); |
bd358db1 |
2869 | if (len < sizeof(string)) { |
51470298 |
2870 | ss = string; |
2871 | strcpy(string, s->password); |
bd358db1 |
2872 | len++; /* cover the zero byte */ |
2873 | while (len < sizeof(string)) { |
2874 | string[len++] = (char) random_byte(); |
2875 | } |
2876 | } else { |
51470298 |
2877 | ss = s->password; |
bd358db1 |
2878 | } |
2879 | logevent("Sending length-padded password"); |
51470298 |
2880 | send_packet(ssh, s->pwpkt_type, PKT_INT, len, |
2881 | PKT_DATA, ss, len, PKT_END); |
bd358db1 |
2882 | } else { |
2883 | /* |
2884 | * The server has _both_ |
2885 | * BUG_CHOKES_ON_SSH1_IGNORE and |
2886 | * BUG_NEEDS_SSH1_PLAIN_PASSWORD. There is |
2887 | * therefore nothing we can do. |
2888 | */ |
2889 | int len; |
51470298 |
2890 | len = strlen(s->password); |
bd358db1 |
2891 | logevent("Sending unpadded password"); |
51470298 |
2892 | send_packet(ssh, s->pwpkt_type, PKT_INT, len, |
2893 | PKT_DATA, s->password, len, PKT_END); |
32874aea |
2894 | } |
2895 | } else { |
51470298 |
2896 | send_packet(ssh, s->pwpkt_type, PKT_STR, s->password, PKT_END); |
32874aea |
2897 | } |
2898 | } |
c5e9c988 |
2899 | logevent("Sent password"); |
51470298 |
2900 | memset(s->password, 0, strlen(s->password)); |
fb09bf1c |
2901 | crWaitUntil(ispkt); |
51470298 |
2902 | if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
32874aea |
2903 | if (flags & FLAG_VERBOSE) |
51470298 |
2904 | c_write_str(ssh, "Access denied\r\n"); |
c5e9c988 |
2905 | logevent("Authentication refused"); |
51470298 |
2906 | } else if (ssh->pktin.type != SSH1_SMSG_SUCCESS) { |
a8327734 |
2907 | bombout((ssh,"Strange packet received, type %d", ssh->pktin.type)); |
32874aea |
2908 | crReturn(0); |
374330e2 |
2909 | } |
2910 | } |
2911 | |
c5e9c988 |
2912 | logevent("Authentication successful"); |
2913 | |
fb09bf1c |
2914 | crFinish(1); |
2915 | } |
2916 | |
32874aea |
2917 | void sshfwd_close(struct ssh_channel *c) |
2918 | { |
51470298 |
2919 | Ssh ssh = c->ssh; |
2920 | |
80e48603 |
2921 | if (c && !c->closes) { |
4ed34d25 |
2922 | /* |
2923 | * If the channel's remoteid is -1, we have sent |
2924 | * CHANNEL_OPEN for this channel, but it hasn't even been |
2925 | * acknowledged by the server. So we must set a close flag |
2926 | * on it now, and then when the server acks the channel |
2927 | * open, we can close it then. |
2928 | */ |
36cac739 |
2929 | if (((int)c->remoteid) != -1) { |
51470298 |
2930 | if (ssh->version == 1) { |
2931 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid, |
4ed34d25 |
2932 | PKT_END); |
2933 | } else { |
51470298 |
2934 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_CLOSE); |
2935 | ssh2_pkt_adduint32(ssh, c->remoteid); |
2936 | ssh2_pkt_send(ssh); |
4ed34d25 |
2937 | } |
32874aea |
2938 | } |
0357890f |
2939 | c->closes = 1; /* sent MSG_CLOSE */ |
32874aea |
2940 | if (c->type == CHAN_X11) { |
2941 | c->u.x11.s = NULL; |
d74d141c |
2942 | logevent("Forwarded X11 connection terminated"); |
4ed34d25 |
2943 | } else if (c->type == CHAN_SOCKDATA || |
2944 | c->type == CHAN_SOCKDATA_DORMANT) { |
d74d141c |
2945 | c->u.pfd.s = NULL; |
2946 | logevent("Forwarded port closed"); |
32874aea |
2947 | } |
2948 | } |
2949 | } |
2950 | |
5471d09a |
2951 | int sshfwd_write(struct ssh_channel *c, char *buf, int len) |
32874aea |
2952 | { |
51470298 |
2953 | Ssh ssh = c->ssh; |
2954 | |
2955 | if (ssh->version == 1) { |
2956 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
32874aea |
2957 | PKT_INT, c->remoteid, |
2958 | PKT_INT, len, PKT_DATA, buf, len, PKT_END); |
5471d09a |
2959 | /* |
2960 | * In SSH1 we can return 0 here - implying that forwarded |
2961 | * connections are never individually throttled - because |
2962 | * the only circumstance that can cause throttling will be |
2963 | * the whole SSH connection backing up, in which case |
2964 | * _everything_ will be throttled as a whole. |
2965 | */ |
2966 | return 0; |
783415f8 |
2967 | } else { |
32874aea |
2968 | ssh2_add_channel_data(c, buf, len); |
5471d09a |
2969 | return ssh2_try_send(c); |
2970 | } |
2971 | } |
2972 | |
2973 | void sshfwd_unthrottle(struct ssh_channel *c, int bufsize) |
2974 | { |
51470298 |
2975 | Ssh ssh = c->ssh; |
2976 | |
2977 | if (ssh->version == 1) { |
5471d09a |
2978 | if (c->v.v1.throttling && bufsize < SSH1_BUFFER_LIMIT) { |
2979 | c->v.v1.throttling = 0; |
51470298 |
2980 | ssh1_throttle(ssh, -1); |
5471d09a |
2981 | } |
2982 | } else { |
2983 | ssh2_set_window(c, OUR_V2_WINSIZE - bufsize); |
783415f8 |
2984 | } |
9c964e85 |
2985 | } |
2986 | |
51470298 |
2987 | static void ssh1_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
32874aea |
2988 | { |
51470298 |
2989 | crBegin(ssh->ssh1_protocol_crstate); |
fb09bf1c |
2990 | |
2991 | random_init(); |
2992 | |
51470298 |
2993 | while (!do_ssh1_login(ssh, in, inlen, ispkt)) { |
fb09bf1c |
2994 | crReturnV; |
85ee8208 |
2995 | } |
51470298 |
2996 | if (ssh->state == SSH_STATE_CLOSED) |
32874aea |
2997 | crReturnV; |
fb09bf1c |
2998 | |
979310f1 |
2999 | if (cfg.agentfwd && agent_exists()) { |
32874aea |
3000 | logevent("Requesting agent forwarding"); |
51470298 |
3001 | send_packet(ssh, SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END); |
32874aea |
3002 | do { |
3003 | crReturnV; |
3004 | } while (!ispkt); |
51470298 |
3005 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3006 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
a8327734 |
3007 | bombout((ssh,"Protocol confusion")); |
32874aea |
3008 | crReturnV; |
51470298 |
3009 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
32874aea |
3010 | logevent("Agent forwarding refused"); |
3011 | } else { |
3012 | logevent("Agent forwarding enabled"); |
51470298 |
3013 | ssh->agentfwd_enabled = TRUE; |
db7d555c |
3014 | } |
dacbd0e8 |
3015 | } |
3016 | |
9c964e85 |
3017 | if (cfg.x11_forward) { |
32874aea |
3018 | char proto[20], data[64]; |
3019 | logevent("Requesting X11 forwarding"); |
302121de |
3020 | ssh->x11auth = x11_invent_auth(proto, sizeof(proto), |
3021 | data, sizeof(data)); |
e0e7dff8 |
3022 | x11_get_real_auth(ssh->x11auth, cfg.x11_display); |
51470298 |
3023 | if (ssh->v1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) { |
3024 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
32874aea |
3025 | PKT_STR, proto, PKT_STR, data, |
421d6835 |
3026 | PKT_INT, x11_get_screen_number(cfg.x11_display), |
3027 | PKT_END); |
32874aea |
3028 | } else { |
51470298 |
3029 | send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING, |
32874aea |
3030 | PKT_STR, proto, PKT_STR, data, PKT_END); |
3031 | } |
3032 | do { |
3033 | crReturnV; |
3034 | } while (!ispkt); |
51470298 |
3035 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3036 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
a8327734 |
3037 | bombout((ssh,"Protocol confusion")); |
32874aea |
3038 | crReturnV; |
51470298 |
3039 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
32874aea |
3040 | logevent("X11 forwarding refused"); |
3041 | } else { |
3042 | logevent("X11 forwarding enabled"); |
51470298 |
3043 | ssh->X11_fwd_enabled = TRUE; |
9c964e85 |
3044 | } |
3045 | } |
3046 | |
d74d141c |
3047 | { |
9bf696b1 |
3048 | char type; |
d74d141c |
3049 | int n; |
a4fc0d74 |
3050 | int sport,dport,sserv,dserv; |
6ee9b735 |
3051 | char sports[256], dports[256], saddr[256], host[256]; |
d74d141c |
3052 | |
51470298 |
3053 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh1); |
d74d141c |
3054 | /* Add port forwardings. */ |
51470298 |
3055 | ssh->portfwd_strptr = cfg.portfwd; |
3056 | while (*ssh->portfwd_strptr) { |
3057 | type = *ssh->portfwd_strptr++; |
6ee9b735 |
3058 | saddr[0] = '\0'; |
d74d141c |
3059 | n = 0; |
6ee9b735 |
3060 | while (*ssh->portfwd_strptr && *ssh->portfwd_strptr != '\t') { |
3061 | if (*ssh->portfwd_strptr == ':') { |
3062 | /* |
3063 | * We've seen a colon in the middle of the |
3064 | * source port number. This means that |
3065 | * everything we've seen until now is the |
3066 | * source _address_, so we'll move it into |
3067 | * saddr and start sports from the beginning |
3068 | * again. |
3069 | */ |
3070 | ssh->portfwd_strptr++; |
3071 | sports[n] = '\0'; |
3072 | strcpy(saddr, sports); |
3073 | n = 0; |
3074 | } |
3075 | if (n < 255) sports[n++] = *ssh->portfwd_strptr++; |
3076 | } |
d74d141c |
3077 | sports[n] = 0; |
51470298 |
3078 | if (*ssh->portfwd_strptr == '\t') |
3079 | ssh->portfwd_strptr++; |
d74d141c |
3080 | n = 0; |
6ee9b735 |
3081 | while (*ssh->portfwd_strptr && *ssh->portfwd_strptr != ':') { |
3082 | if (n < 255) host[n++] = *ssh->portfwd_strptr++; |
3083 | } |
d74d141c |
3084 | host[n] = 0; |
51470298 |
3085 | if (*ssh->portfwd_strptr == ':') |
3086 | ssh->portfwd_strptr++; |
d74d141c |
3087 | n = 0; |
6ee9b735 |
3088 | while (*ssh->portfwd_strptr) { |
3089 | if (n < 255) dports[n++] = *ssh->portfwd_strptr++; |
3090 | } |
d74d141c |
3091 | dports[n] = 0; |
51470298 |
3092 | ssh->portfwd_strptr++; |
d74d141c |
3093 | dport = atoi(dports); |
a4fc0d74 |
3094 | dserv = 0; |
3095 | if (dport == 0) { |
3096 | dserv = 1; |
68a49acb |
3097 | dport = net_service_lookup(dports); |
3098 | if (!dport) { |
57356d63 |
3099 | logeventf(ssh, "Service lookup failed for" |
3100 | " destination port \"%s\"", dports); |
a4fc0d74 |
3101 | } |
3102 | } |
d74d141c |
3103 | sport = atoi(sports); |
a4fc0d74 |
3104 | sserv = 0; |
3105 | if (sport == 0) { |
3106 | sserv = 1; |
68a49acb |
3107 | sport = net_service_lookup(sports); |
3108 | if (!sport) { |
57356d63 |
3109 | logeventf(ssh, "Service lookup failed for source" |
3110 | " port \"%s\"", sports); |
a4fc0d74 |
3111 | } |
3112 | } |
d74d141c |
3113 | if (sport && dport) { |
3114 | if (type == 'L') { |
6ee9b735 |
3115 | pfd_addforward(host, dport, *saddr ? saddr : NULL, |
3116 | sport, ssh); |
3117 | logeventf(ssh, "Local port %.*s%.*s%.*s%.*s%d%.*s" |
3118 | " forwarding to %s:%.*s%.*s%d%.*s", |
3119 | (int)(*saddr?strlen(saddr):0), *saddr?saddr:NULL, |
3120 | (int)(*saddr?1:0), ":", |
57356d63 |
3121 | (int)(sserv ? strlen(sports) : 0), sports, |
3122 | sserv, "(", sport, sserv, ")", |
3123 | host, |
3124 | (int)(dserv ? strlen(dports) : 0), dports, |
3125 | dserv, "(", dport, dserv, ")"); |
d74d141c |
3126 | } else { |
3127 | struct ssh_rportfwd *pf; |
3128 | pf = smalloc(sizeof(*pf)); |
bc240b21 |
3129 | strcpy(pf->dhost, host); |
3130 | pf->dport = dport; |
6ee9b735 |
3131 | if (saddr) { |
3132 | logeventf(ssh, |
3133 | "SSH1 cannot handle source address spec \"%s:%d\"; ignoring", |
3134 | saddr, sport); |
3135 | } |
51470298 |
3136 | if (add234(ssh->rportfwds, pf) != pf) { |
57356d63 |
3137 | logeventf(ssh, |
3138 | "Duplicate remote port forwarding to %s:%d", |
3139 | host, dport); |
bc240b21 |
3140 | sfree(pf); |
d74d141c |
3141 | } else { |
57356d63 |
3142 | logeventf(ssh, "Requesting remote port %.*s%.*s%d%.*s" |
3143 | " forward to %s:%.*s%.*s%d%.*s", |
3144 | (int)(sserv ? strlen(sports) : 0), sports, |
3145 | sserv, "(", sport, sserv, ")", |
3146 | host, |
3147 | (int)(dserv ? strlen(dports) : 0), dports, |
3148 | dserv, "(", dport, dserv, ")"); |
51470298 |
3149 | send_packet(ssh, SSH1_CMSG_PORT_FORWARD_REQUEST, |
d74d141c |
3150 | PKT_INT, sport, |
3151 | PKT_STR, host, |
3152 | PKT_INT, dport, |
3153 | PKT_END); |
9bf696b1 |
3154 | do { |
3155 | crReturnV; |
3156 | } while (!ispkt); |
51470298 |
3157 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3158 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
a8327734 |
3159 | bombout((ssh,"Protocol confusion")); |
9bf696b1 |
3160 | crReturnV; |
51470298 |
3161 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
3162 | c_write_str(ssh, "Server refused port" |
3163 | " forwarding\r\n"); |
9bf696b1 |
3164 | } |
3165 | logevent("Remote port forwarding enabled"); |
d74d141c |
3166 | } |
3167 | } |
3168 | } |
3169 | } |
3170 | } |
3171 | |
fef97f43 |
3172 | if (!cfg.nopty) { |
51470298 |
3173 | send_packet(ssh, SSH1_CMSG_REQUEST_PTY, |
32874aea |
3174 | PKT_STR, cfg.termtype, |
51470298 |
3175 | PKT_INT, ssh->term_height, |
3176 | PKT_INT, ssh->term_width, |
32874aea |
3177 | PKT_INT, 0, PKT_INT, 0, PKT_CHAR, 0, PKT_END); |
51470298 |
3178 | ssh->state = SSH_STATE_INTERMED; |
32874aea |
3179 | do { |
3180 | crReturnV; |
3181 | } while (!ispkt); |
51470298 |
3182 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3183 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
a8327734 |
3184 | bombout((ssh,"Protocol confusion")); |
32874aea |
3185 | crReturnV; |
51470298 |
3186 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
3187 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
3188 | ssh->editing = ssh->echoing = 1; |
32874aea |
3189 | } |
c5e9c988 |
3190 | logevent("Allocated pty"); |
0965bee0 |
3191 | } else { |
51470298 |
3192 | ssh->editing = ssh->echoing = 1; |
374330e2 |
3193 | } |
3194 | |
4ba9b64b |
3195 | if (cfg.compression) { |
51470298 |
3196 | send_packet(ssh, SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END); |
32874aea |
3197 | do { |
3198 | crReturnV; |
3199 | } while (!ispkt); |
51470298 |
3200 | if (ssh->pktin.type != SSH1_SMSG_SUCCESS |
3201 | && ssh->pktin.type != SSH1_SMSG_FAILURE) { |
a8327734 |
3202 | bombout((ssh,"Protocol confusion")); |
32874aea |
3203 | crReturnV; |
51470298 |
3204 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
3205 | c_write_str(ssh, "Server refused to compress\r\n"); |
32874aea |
3206 | } |
4ba9b64b |
3207 | logevent("Started compression"); |
51470298 |
3208 | ssh->v1_compressing = TRUE; |
5366aed8 |
3209 | ssh->cs_comp_ctx = zlib_compress_init(); |
3210 | logevent("Initialised zlib (RFC1950) compression"); |
3211 | ssh->sc_comp_ctx = zlib_decompress_init(); |
3212 | logevent("Initialised zlib (RFC1950) decompression"); |
4ba9b64b |
3213 | } |
3214 | |
fd5e5847 |
3215 | /* |
3216 | * Start the shell or command. |
3217 | * |
3218 | * Special case: if the first-choice command is an SSH2 |
3219 | * subsystem (hence not usable here) and the second choice |
3220 | * exists, we fall straight back to that. |
3221 | */ |
3222 | { |
3223 | char *cmd = cfg.remote_cmd_ptr; |
3224 | |
3225 | if (cfg.ssh_subsys && cfg.remote_cmd_ptr2) { |
3226 | cmd = cfg.remote_cmd_ptr2; |
51470298 |
3227 | ssh->fallback_cmd = TRUE; |
fd5e5847 |
3228 | } |
3229 | if (*cmd) |
51470298 |
3230 | send_packet(ssh, SSH1_CMSG_EXEC_CMD, PKT_STR, cmd, PKT_END); |
fd5e5847 |
3231 | else |
51470298 |
3232 | send_packet(ssh, SSH1_CMSG_EXEC_SHELL, PKT_END); |
fd5e5847 |
3233 | logevent("Started session"); |
3234 | } |
374330e2 |
3235 | |
51470298 |
3236 | ssh->state = SSH_STATE_SESSION; |
3237 | if (ssh->size_needed) |
3238 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
3239 | if (ssh->eof_needed) |
3240 | ssh_special(ssh, TS_EOF); |
374330e2 |
3241 | |
b9d7bcad |
3242 | if (ssh->ldisc) |
3243 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
51470298 |
3244 | ssh->send_ok = 1; |
3245 | ssh->channels = newtree234(ssh_channelcmp); |
374330e2 |
3246 | while (1) { |
3247 | crReturnV; |
3248 | if (ispkt) { |
51470298 |
3249 | if (ssh->pktin.type == SSH1_SMSG_STDOUT_DATA || |
3250 | ssh->pktin.type == SSH1_SMSG_STDERR_DATA) { |
3251 | long len = GET_32BIT(ssh->pktin.body); |
5471d09a |
3252 | int bufsize = |
51470298 |
3253 | from_backend(ssh->frontend, |
3254 | ssh->pktin.type == SSH1_SMSG_STDERR_DATA, |
d8baa528 |
3255 | (char *)(ssh->pktin.body) + 4, len); |
51470298 |
3256 | if (!ssh->v1_stdout_throttling && bufsize > SSH1_BUFFER_LIMIT) { |
3257 | ssh->v1_stdout_throttling = 1; |
3258 | ssh1_throttle(ssh, +1); |
5471d09a |
3259 | } |
51470298 |
3260 | } else if (ssh->pktin.type == SSH1_MSG_DISCONNECT) { |
3261 | ssh->state = SSH_STATE_CLOSED; |
c5e9c988 |
3262 | logevent("Received disconnect request"); |
32874aea |
3263 | crReturnV; |
51470298 |
3264 | } else if (ssh->pktin.type == SSH1_SMSG_X11_OPEN) { |
32874aea |
3265 | /* Remote side is trying to open a channel to talk to our |
3266 | * X-Server. Give them back a local channel number. */ |
3267 | struct ssh_channel *c; |
9c964e85 |
3268 | |
3269 | logevent("Received X11 connect request"); |
3270 | /* Refuse if X11 forwarding is disabled. */ |
51470298 |
3271 | if (!ssh->X11_fwd_enabled) { |
3272 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
3273 | PKT_INT, GET_32BIT(ssh->pktin.body), PKT_END); |
9c964e85 |
3274 | logevent("Rejected X11 connect request"); |
3275 | } else { |
9c964e85 |
3276 | c = smalloc(sizeof(struct ssh_channel)); |
51470298 |
3277 | c->ssh = ssh; |
9c964e85 |
3278 | |
302121de |
3279 | if (x11_init(&c->u.x11.s, cfg.x11_display, c, |
3280 | ssh->x11auth) != NULL) { |
32874aea |
3281 | logevent("opening X11 forward connection failed"); |
3282 | sfree(c); |
51470298 |
3283 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
3284 | PKT_INT, GET_32BIT(ssh->pktin.body), |
32874aea |
3285 | PKT_END); |
9c964e85 |
3286 | } else { |
32874aea |
3287 | logevent |
3288 | ("opening X11 forward connection succeeded"); |
51470298 |
3289 | c->remoteid = GET_32BIT(ssh->pktin.body); |
3290 | c->localid = alloc_channel_id(ssh); |
32874aea |
3291 | c->closes = 0; |
5471d09a |
3292 | c->v.v1.throttling = 0; |
32874aea |
3293 | c->type = CHAN_X11; /* identify channel type */ |
51470298 |
3294 | add234(ssh->channels, c); |
3295 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
32874aea |
3296 | PKT_INT, c->remoteid, PKT_INT, |
3297 | c->localid, PKT_END); |
3298 | logevent("Opened X11 forward channel"); |
9c964e85 |
3299 | } |
3300 | } |
51470298 |
3301 | } else if (ssh->pktin.type == SSH1_SMSG_AGENT_OPEN) { |
32874aea |
3302 | /* Remote side is trying to open a channel to talk to our |
3303 | * agent. Give them back a local channel number. */ |
3304 | struct ssh_channel *c; |
db7d555c |
3305 | |
3306 | /* Refuse if agent forwarding is disabled. */ |
51470298 |
3307 | if (!ssh->agentfwd_enabled) { |
3308 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
3309 | PKT_INT, GET_32BIT(ssh->pktin.body), PKT_END); |
db7d555c |
3310 | } else { |
dcbde236 |
3311 | c = smalloc(sizeof(struct ssh_channel)); |
51470298 |
3312 | c->ssh = ssh; |
3313 | c->remoteid = GET_32BIT(ssh->pktin.body); |
3314 | c->localid = alloc_channel_id(ssh); |
db7d555c |
3315 | c->closes = 0; |
5471d09a |
3316 | c->v.v1.throttling = 0; |
32874aea |
3317 | c->type = CHAN_AGENT; /* identify channel type */ |
db7d555c |
3318 | c->u.a.lensofar = 0; |
51470298 |
3319 | add234(ssh->channels, c); |
3320 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
db7d555c |
3321 | PKT_INT, c->remoteid, PKT_INT, c->localid, |
3322 | PKT_END); |
3323 | } |
51470298 |
3324 | } else if (ssh->pktin.type == SSH1_MSG_PORT_OPEN) { |
d74d141c |
3325 | /* Remote side is trying to open a channel to talk to a |
3326 | * forwarded port. Give them back a local channel number. */ |
3327 | struct ssh_channel *c; |
3328 | struct ssh_rportfwd pf; |
3329 | int hostsize, port; |
3330 | char host[256], buf[1024]; |
3331 | char *p, *h, *e; |
3332 | c = smalloc(sizeof(struct ssh_channel)); |
51470298 |
3333 | c->ssh = ssh; |
d74d141c |
3334 | |
51470298 |
3335 | hostsize = GET_32BIT(ssh->pktin.body+4); |
d8baa528 |
3336 | for (h = host, p = (char *)(ssh->pktin.body+8); |
3337 | hostsize != 0; hostsize--) { |
d74d141c |
3338 | if (h+1 < host+sizeof(host)) |
3339 | *h++ = *p; |
95bc8819 |
3340 | p++; |
d74d141c |
3341 | } |
3342 | *h = 0; |
3343 | port = GET_32BIT(p); |
3344 | |
bc240b21 |
3345 | strcpy(pf.dhost, host); |
3346 | pf.dport = port; |
d74d141c |
3347 | |
51470298 |
3348 | if (find234(ssh->rportfwds, &pf, NULL) == NULL) { |
d74d141c |
3349 | sprintf(buf, "Rejected remote port open request for %s:%d", |
3350 | host, port); |
3351 | logevent(buf); |
51470298 |
3352 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
3353 | PKT_INT, GET_32BIT(ssh->pktin.body), PKT_END); |
d74d141c |
3354 | } else { |
3355 | sprintf(buf, "Received remote port open request for %s:%d", |
3356 | host, port); |
3357 | logevent(buf); |
3358 | e = pfd_newconnect(&c->u.pfd.s, host, port, c); |
3359 | if (e != NULL) { |
3360 | char buf[256]; |
3361 | sprintf(buf, "Port open failed: %s", e); |
3362 | logevent(buf); |
3363 | sfree(c); |
51470298 |
3364 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE, |
3365 | PKT_INT, GET_32BIT(ssh->pktin.body), |
d74d141c |
3366 | PKT_END); |
3367 | } else { |
51470298 |
3368 | c->remoteid = GET_32BIT(ssh->pktin.body); |
3369 | c->localid = alloc_channel_id(ssh); |
d74d141c |
3370 | c->closes = 0; |
5471d09a |
3371 | c->v.v1.throttling = 0; |
d74d141c |
3372 | c->type = CHAN_SOCKDATA; /* identify channel type */ |
51470298 |
3373 | add234(ssh->channels, c); |
3374 | send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_CONFIRMATION, |
d74d141c |
3375 | PKT_INT, c->remoteid, PKT_INT, |
3376 | c->localid, PKT_END); |
3377 | logevent("Forwarded port opened successfully"); |
3378 | } |
3379 | } |
3380 | |
51470298 |
3381 | } else if (ssh->pktin.type == SSH1_MSG_CHANNEL_OPEN_CONFIRMATION) { |
3382 | unsigned int remoteid = GET_32BIT(ssh->pktin.body); |
3383 | unsigned int localid = GET_32BIT(ssh->pktin.body+4); |
bc240b21 |
3384 | struct ssh_channel *c; |
3385 | |
51470298 |
3386 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
bc240b21 |
3387 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
3388 | c->remoteid = localid; |
3389 | c->type = CHAN_SOCKDATA; |
7781f316 |
3390 | c->v.v1.throttling = 0; |
bc240b21 |
3391 | pfd_confirm(c->u.pfd.s); |
3392 | } |
d74d141c |
3393 | |
4ed34d25 |
3394 | if (c && c->closes) { |
3395 | /* |
3396 | * We have a pending close on this channel, |
3397 | * which we decided on before the server acked |
3398 | * the channel open. So now we know the |
3399 | * remoteid, we can close it again. |
3400 | */ |
51470298 |
3401 | send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, |
3402 | PKT_INT, c->remoteid, PKT_END); |
4ed34d25 |
3403 | } |
3404 | |
51470298 |
3405 | } else if (ssh->pktin.type == SSH1_MSG_CHANNEL_OPEN_FAILURE) { |
3406 | unsigned int remoteid = GET_32BIT(ssh->pktin.body); |
724cface |
3407 | struct ssh_channel *c; |
3408 | |
51470298 |
3409 | c = find234(ssh->channels, &remoteid, ssh_channelfind); |
724cface |
3410 | if (c && c->type == CHAN_SOCKDATA_DORMANT) { |
3411 | logevent("Forwarded connection refused by server"); |
3412 | pfd_close(c->u.pfd.s); |
51470298 |
3413 | del234(ssh->channels, c); |
724cface |
3414 | sfree(c); |
3415 | } |
3416 | |
51470298 |
3417 | } else if (ssh->pktin.type == SSH1_MSG_CHANNEL_CLOSE || |
3418 | ssh->pktin.type == SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION) { |
32874aea |
3419 | /* Remote side closes a channel. */ |
51470298 |
3420 | unsigned i = GET_32BIT(ssh->pktin.body); |
32874aea |
3421 | struct ssh_channel *c; |
51470298 |
3422 | c = find234(ssh->channels, &i, ssh_channelfind); |
d0d844c1 |
3423 | if (c && ((int)c->remoteid) != -1) { |
32874aea |
3424 | int closetype; |
3425 | closetype = |
51470298 |
3426 | (ssh->pktin.type == SSH1_MSG_CHANNEL_CLOSE ? 1 : 2); |
0357890f |
3427 | |
783415f8 |
3428 | if ((c->closes == 0) && (c->type == CHAN_X11)) { |
d74d141c |
3429 | logevent("Forwarded X11 connection terminated"); |
9c964e85 |
3430 | assert(c->u.x11.s != NULL); |
3431 | x11_close(c->u.x11.s); |
3432 | c->u.x11.s = NULL; |
3433 | } |
d74d141c |
3434 | if ((c->closes == 0) && (c->type == CHAN_SOCKDATA)) { |
3435 | logevent("Forwarded port closed"); |
3436 | assert(c->u.pfd.s != NULL); |
3437 | pfd_close(c->u.pfd.s); |
3438 | c->u.pfd.s = NULL; |
3439 | } |
0357890f |
3440 | |
3441 | c->closes |= (closetype << 2); /* seen this message */ |
3442 | if (!(c->closes & closetype)) { |
51470298 |
3443 | send_packet(ssh, ssh->pktin.type, PKT_INT, c->remoteid, |
0357890f |
3444 | PKT_END); |
3445 | c->closes |= closetype; /* sent it too */ |
3446 | } |
3447 | |
3448 | if (c->closes == 15) { |
51470298 |
3449 | del234(ssh->channels, c); |
32874aea |
3450 | sfree(c); |
3451 | } |
d0d844c1 |
3452 | } else { |
a8327734 |
3453 | bombout((ssh,"Received CHANNEL_CLOSE%s for %s channel %d\n", |
51470298 |
3454 | ssh->pktin.type == SSH1_MSG_CHANNEL_CLOSE ? "" : |
d0d844c1 |
3455 | "_CONFIRMATION", c ? "half-open" : "nonexistent", |
3456 | i)); |
32874aea |
3457 | } |
51470298 |
3458 | } else if (ssh->pktin.type == SSH1_MSG_CHANNEL_DATA) { |
32874aea |
3459 | /* Data sent down one of our channels. */ |
51470298 |
3460 | int i = GET_32BIT(ssh->pktin.body); |
3461 | int len = GET_32BIT(ssh->pktin.body + 4); |
3462 | unsigned char *p = ssh->pktin.body + 8; |
32874aea |
3463 | struct ssh_channel *c; |
51470298 |
3464 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
3465 | if (c) { |
5471d09a |
3466 | int bufsize; |
32874aea |
3467 | switch (c->type) { |
3468 | case CHAN_X11: |
d8baa528 |
3469 | bufsize = x11_send(c->u.x11.s, (char *)p, len); |
9c964e85 |
3470 | break; |
d74d141c |
3471 | case CHAN_SOCKDATA: |
d8baa528 |
3472 | bufsize = pfd_send(c->u.pfd.s, (char *)p, len); |
bc240b21 |
3473 | break; |
32874aea |
3474 | case CHAN_AGENT: |
3475 | /* Data for an agent message. Buffer it. */ |
3476 | while (len > 0) { |
3477 | if (c->u.a.lensofar < 4) { |
3478 | int l = min(4 - c->u.a.lensofar, len); |
3479 | memcpy(c->u.a.msglen + c->u.a.lensofar, p, |
3480 | l); |
3481 | p += l; |
3482 | len -= l; |
3483 | c->u.a.lensofar += l; |
3484 | } |
3485 | if (c->u.a.lensofar == 4) { |
3486 | c->u.a.totallen = |
3487 | 4 + GET_32BIT(c->u.a.msglen); |
3488 | c->u.a.message = smalloc(c->u.a.totallen); |
3489 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
3490 | } |
3491 | if (c->u.a.lensofar >= 4 && len > 0) { |
3492 | int l = |
3493 | min(c->u.a.totallen - c->u.a.lensofar, |
3494 | len); |
3495 | memcpy(c->u.a.message + c->u.a.lensofar, p, |
3496 | l); |
3497 | p += l; |
3498 | len -= l; |
3499 | c->u.a.lensofar += l; |
3500 | } |
3501 | if (c->u.a.lensofar == c->u.a.totallen) { |
3502 | void *reply, *sentreply; |
3503 | int replylen; |
3504 | agent_query(c->u.a.message, |
3505 | c->u.a.totallen, &reply, |
3506 | &replylen); |
3507 | if (reply) |
3508 | sentreply = reply; |
3509 | else { |
3510 | /* Fake SSH_AGENT_FAILURE. */ |
3511 | sentreply = "\0\0\0\1\5"; |
3512 | replylen = 5; |
3513 | } |
51470298 |
3514 | send_packet(ssh, SSH1_MSG_CHANNEL_DATA, |
32874aea |
3515 | PKT_INT, c->remoteid, |
3516 | PKT_INT, replylen, |
3517 | PKT_DATA, sentreply, replylen, |
3518 | PKT_END); |
3519 | if (reply) |
3520 | sfree(reply); |
3521 | sfree(c->u.a.message); |
3522 | c->u.a.lensofar = 0; |
3523 | } |
3524 | } |
5471d09a |
3525 | bufsize = 0; /* agent channels never back up */ |
32874aea |
3526 | break; |
3527 | } |
90347b95 |
3528 | if (!c->v.v1.throttling && bufsize > SSH1_BUFFER_LIMIT) { |
5471d09a |
3529 | c->v.v1.throttling = 1; |
51470298 |
3530 | ssh1_throttle(ssh, +1); |
5471d09a |
3531 | } |
32874aea |
3532 | } |
51470298 |
3533 | } else if (ssh->pktin.type == SSH1_SMSG_SUCCESS) { |
972a41c8 |
3534 | /* may be from EXEC_SHELL on some servers */ |
51470298 |
3535 | } else if (ssh->pktin.type == SSH1_SMSG_FAILURE) { |
972a41c8 |
3536 | /* may be from EXEC_SHELL on some servers |
374330e2 |
3537 | * if no pty is available or in other odd cases. Ignore */ |
51470298 |
3538 | } else if (ssh->pktin.type == SSH1_SMSG_EXIT_STATUS) { |
d8d6c7e5 |
3539 | char buf[100]; |
51470298 |
3540 | ssh->exitcode = GET_32BIT(ssh->pktin.body); |
d8d6c7e5 |
3541 | sprintf(buf, "Server sent command exit status %d", |
51470298 |
3542 | ssh->exitcode); |
d8d6c7e5 |
3543 | logevent(buf); |
51470298 |
3544 | send_packet(ssh, SSH1_CMSG_EXIT_CONFIRMATION, PKT_END); |
4252c9cc |
3545 | /* |
3546 | * In case `helpful' firewalls or proxies tack |
3547 | * extra human-readable text on the end of the |
3548 | * session which we might mistake for another |
3549 | * encrypted packet, we close the session once |
3550 | * we've sent EXIT_CONFIRMATION. |
3551 | */ |
51470298 |
3552 | ssh->state = SSH_STATE_CLOSED; |
4252c9cc |
3553 | crReturnV; |
374330e2 |
3554 | } else { |
a8327734 |
3555 | bombout((ssh,"Strange packet received: type %d", ssh->pktin.type)); |
32874aea |
3556 | crReturnV; |
374330e2 |
3557 | } |
3558 | } else { |
8df7a775 |
3559 | while (inlen > 0) { |
3560 | int len = min(inlen, 512); |
51470298 |
3561 | send_packet(ssh, SSH1_CMSG_STDIN_DATA, |
8df7a775 |
3562 | PKT_INT, len, PKT_DATA, in, len, PKT_END); |
3563 | in += len; |
3564 | inlen -= len; |
3565 | } |
374330e2 |
3566 | } |
3567 | } |
3568 | |
3569 | crFinishV; |
3570 | } |
3571 | |
3572 | /* |
e5574168 |
3573 | * Utility routine for decoding comma-separated strings in KEXINIT. |
3574 | */ |
32874aea |
3575 | static int in_commasep_string(char *needle, char *haystack, int haylen) |
3576 | { |
57356d63 |
3577 | int needlen; |
3578 | if (!needle || !haystack) /* protect against null pointers */ |
3579 | return 0; |
3580 | needlen = strlen(needle); |
e5574168 |
3581 | while (1) { |
32874aea |
3582 | /* |
3583 | * Is it at the start of the string? |
3584 | */ |
3585 | if (haylen >= needlen && /* haystack is long enough */ |
3586 | !memcmp(needle, haystack, needlen) && /* initial match */ |
3587 | (haylen == needlen || haystack[needlen] == ',') |
3588 | /* either , or EOS follows */ |
3589 | ) |
3590 | return 1; |
3591 | /* |
3592 | * If not, search for the next comma and resume after that. |
3593 | * If no comma found, terminate. |
3594 | */ |
3595 | while (haylen > 0 && *haystack != ',') |
3596 | haylen--, haystack++; |
3597 | if (haylen == 0) |
3598 | return 0; |
3599 | haylen--, haystack++; /* skip over comma itself */ |
e5574168 |
3600 | } |
3601 | } |
3602 | |
3603 | /* |
d39f364a |
3604 | * SSH2 key creation method. |
3605 | */ |
d8baa528 |
3606 | static void ssh2_mkkey(Ssh ssh, Bignum K, unsigned char *H, |
3607 | unsigned char *sessid, char chr, |
3608 | unsigned char *keyspace) |
32874aea |
3609 | { |
d39f364a |
3610 | SHA_State s; |
3611 | /* First 20 bytes. */ |
3612 | SHA_Init(&s); |
51470298 |
3613 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
088bde77 |
3614 | sha_mpint(&s, K); |
d39f364a |
3615 | SHA_Bytes(&s, H, 20); |
3616 | SHA_Bytes(&s, &chr, 1); |
5e0d7cb8 |
3617 | SHA_Bytes(&s, sessid, 20); |
d39f364a |
3618 | SHA_Final(&s, keyspace); |
3619 | /* Next 20 bytes. */ |
3620 | SHA_Init(&s); |
51470298 |
3621 | if (!(ssh->remote_bugs & BUG_SSH2_DERIVEKEY)) |
088bde77 |
3622 | sha_mpint(&s, K); |
d39f364a |
3623 | SHA_Bytes(&s, H, 20); |
3624 | SHA_Bytes(&s, keyspace, 20); |
32874aea |
3625 | SHA_Final(&s, keyspace + 20); |
d39f364a |
3626 | } |
3627 | |
3628 | /* |
7cca0d81 |
3629 | * Handle the SSH2 transport layer. |
e5574168 |
3630 | */ |
51470298 |
3631 | static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
e5574168 |
3632 | { |
51470298 |
3633 | struct do_ssh2_transport_state { |
3634 | int nbits, pbits, warn; |
3635 | Bignum p, g, e, f, K; |
3636 | int kex_init_value, kex_reply_value; |
3637 | const struct ssh_mac **maclist; |
3638 | int nmacs; |
3639 | const struct ssh2_cipher *cscipher_tobe; |
3640 | const struct ssh2_cipher *sccipher_tobe; |
3641 | const struct ssh_mac *csmac_tobe; |
3642 | const struct ssh_mac *scmac_tobe; |
3643 | const struct ssh_compress *cscomp_tobe; |
3644 | const struct ssh_compress *sccomp_tobe; |
3645 | char *hostkeydata, *sigdata, *keystr, *fingerprint; |
3646 | int hostkeylen, siglen; |
3647 | void *hkey; /* actual host key */ |
3648 | unsigned char exchange_hash[20]; |
3649 | int n_preferred_ciphers; |
3650 | const struct ssh2_ciphers *preferred_ciphers[CIPHER_MAX]; |
3651 | const struct ssh_compress *preferred_comp; |
3652 | int first_kex; |
3653 | }; |
3654 | crState(do_ssh2_transport_state); |
3655 | |
3656 | crBegin(ssh->do_ssh2_transport_crstate); |
3657 | |
3658 | s->cscipher_tobe = s->sccipher_tobe = NULL; |
3659 | s->csmac_tobe = s->scmac_tobe = NULL; |
3660 | s->cscomp_tobe = s->sccomp_tobe = NULL; |
3661 | |
7cca0d81 |
3662 | random_init(); |
51470298 |
3663 | s->first_kex = 1; |
e5574168 |
3664 | |
51470298 |
3665 | { |
3666 | int i; |
3667 | /* |
3668 | * Set up the preferred ciphers. (NULL => warn below here) |
3669 | */ |
3670 | s->n_preferred_ciphers = 0; |
3671 | for (i = 0; i < CIPHER_MAX; i++) { |
3672 | switch (cfg.ssh_cipherlist[i]) { |
3673 | case CIPHER_BLOWFISH: |
3674 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_blowfish; |
3675 | break; |
3676 | case CIPHER_DES: |
3677 | if (cfg.ssh2_des_cbc) { |
3678 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_des; |
3679 | } |
3680 | break; |
3681 | case CIPHER_3DES: |
3682 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_3des; |
3683 | break; |
3684 | case CIPHER_AES: |
3685 | s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_aes; |
3686 | break; |
3687 | case CIPHER_WARN: |
3688 | /* Flag for later. Don't bother if it's the last in |
3689 | * the list. */ |
3690 | if (i < CIPHER_MAX - 1) { |
3691 | s->preferred_ciphers[s->n_preferred_ciphers++] = NULL; |
3692 | } |
3693 | break; |
ca20bfcf |
3694 | } |
ca20bfcf |
3695 | } |
5e8358ad |
3696 | } |
ca20bfcf |
3697 | |
3698 | /* |
3699 | * Set up preferred compression. |
3700 | */ |
4ba9b64b |
3701 | if (cfg.compression) |
51470298 |
3702 | s->preferred_comp = &ssh_zlib; |
4ba9b64b |
3703 | else |
51470298 |
3704 | s->preferred_comp = &ssh_comp_none; |
5e8358ad |
3705 | |
7591b9ff |
3706 | /* |
3707 | * Be prepared to work around the buggy MAC problem. |
3708 | */ |
51470298 |
3709 | if (ssh->remote_bugs & BUG_SSH2_HMAC) |
3710 | s->maclist = buggymacs, s->nmacs = lenof(buggymacs); |
7591b9ff |
3711 | else |
51470298 |
3712 | s->maclist = macs, s->nmacs = lenof(macs); |
7591b9ff |
3713 | |
32874aea |
3714 | begin_key_exchange: |
51470298 |
3715 | { |
3716 | int i, j, cipherstr_started; |
3717 | |
3718 | /* |
3719 | * Construct and send our key exchange packet. |
3720 | */ |
3721 | ssh2_pkt_init(ssh, SSH2_MSG_KEXINIT); |
3722 | for (i = 0; i < 16; i++) |
3723 | ssh2_pkt_addbyte(ssh, (unsigned char) random_byte()); |
3724 | /* List key exchange algorithms. */ |
3725 | ssh2_pkt_addstring_start(ssh); |
3726 | for (i = 0; i < lenof(kex_algs); i++) { |
3727 | if (kex_algs[i] == &ssh_diffiehellman_gex && |
3728 | (ssh->remote_bugs & BUG_SSH2_DH_GEX)) |
3729 | continue; |
3730 | ssh2_pkt_addstring_str(ssh, kex_algs[i]->name); |
3731 | if (i < lenof(kex_algs) - 1) |
3732 | ssh2_pkt_addstring_str(ssh, ","); |
32874aea |
3733 | } |
51470298 |
3734 | /* List server host key algorithms. */ |
3735 | ssh2_pkt_addstring_start(ssh); |
3736 | for (i = 0; i < lenof(hostkey_algs); i++) { |
3737 | ssh2_pkt_addstring_str(ssh, hostkey_algs[i]->name); |
3738 | if (i < lenof(hostkey_algs) - 1) |
3739 | ssh2_pkt_addstring_str(ssh, ","); |
32874aea |
3740 | } |
51470298 |
3741 | /* List client->server encryption algorithms. */ |
3742 | ssh2_pkt_addstring_start(ssh); |
3743 | cipherstr_started = 0; |
3744 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
3745 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
3746 | if (!c) continue; /* warning flag */ |
3747 | for (j = 0; j < c->nciphers; j++) { |
3748 | if (cipherstr_started) |
3749 | ssh2_pkt_addstring_str(ssh, ","); |
3750 | ssh2_pkt_addstring_str(ssh, c->list[j]->name); |
3751 | cipherstr_started = 1; |
3752 | } |
3753 | } |
3754 | /* List server->client encryption algorithms. */ |
3755 | ssh2_pkt_addstring_start(ssh); |
3756 | cipherstr_started = 0; |
3757 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
3758 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
3759 | if (!c) continue; /* warning flag */ |
3760 | for (j = 0; j < c->nciphers; j++) { |
3761 | if (cipherstr_started) |
3762 | ssh2_pkt_addstring_str(ssh, ","); |
3763 | ssh2_pkt_addstring_str(ssh, c->list[j]->name); |
3764 | cipherstr_started = 1; |
3765 | } |
3766 | } |
3767 | /* List client->server MAC algorithms. */ |
3768 | ssh2_pkt_addstring_start(ssh); |
3769 | for (i = 0; i < s->nmacs; i++) { |
3770 | ssh2_pkt_addstring_str(ssh, s->maclist[i]->name); |
3771 | if (i < s->nmacs - 1) |
3772 | ssh2_pkt_addstring_str(ssh, ","); |
3773 | } |
3774 | /* List server->client MAC algorithms. */ |
3775 | ssh2_pkt_addstring_start(ssh); |
3776 | for (i = 0; i < s->nmacs; i++) { |
3777 | ssh2_pkt_addstring_str(ssh, s->maclist[i]->name); |
3778 | if (i < s->nmacs - 1) |
3779 | ssh2_pkt_addstring_str(ssh, ","); |
3780 | } |
3781 | /* List client->server compression algorithms. */ |
3782 | ssh2_pkt_addstring_start(ssh); |
3783 | for (i = 0; i < lenof(compressions) + 1; i++) { |
3784 | const struct ssh_compress *c = |
3785 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
3786 | ssh2_pkt_addstring_str(ssh, c->name); |
3787 | if (i < lenof(compressions)) |
3788 | ssh2_pkt_addstring_str(ssh, ","); |
3789 | } |
3790 | /* List server->client compression algorithms. */ |
3791 | ssh2_pkt_addstring_start(ssh); |
3792 | for (i = 0; i < lenof(compressions) + 1; i++) { |
3793 | const struct ssh_compress *c = |
3794 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
3795 | ssh2_pkt_addstring_str(ssh, c->name); |
3796 | if (i < lenof(compressions)) |
3797 | ssh2_pkt_addstring_str(ssh, ","); |
3798 | } |
3799 | /* List client->server languages. Empty list. */ |
3800 | ssh2_pkt_addstring_start(ssh); |
3801 | /* List server->client languages. Empty list. */ |
3802 | ssh2_pkt_addstring_start(ssh); |
3803 | /* First KEX packet does _not_ follow, because we're not that brave. */ |
3804 | ssh2_pkt_addbool(ssh, FALSE); |
3805 | /* Reserved. */ |
3806 | ssh2_pkt_adduint32(ssh, 0); |
e5574168 |
3807 | } |
0db56f73 |
3808 | |
51470298 |
3809 | ssh->exhash = ssh->exhashbase; |
3810 | sha_string(&ssh->exhash, ssh->pktout.data + 5, ssh->pktout.length - 5); |
0db56f73 |
3811 | |
51470298 |
3812 | ssh2_pkt_send(ssh); |
e5574168 |
3813 | |
32874aea |
3814 | if (!ispkt) |
3815 | crWaitUntil(ispkt); |
57356d63 |
3816 | if (ssh->pktin.length > 5) |
3817 | sha_string(&ssh->exhash, ssh->pktin.data + 5, ssh->pktin.length - 5); |
e5574168 |
3818 | |
3819 | /* |
3820 | * Now examine the other side's KEXINIT to see what we're up |
3821 | * to. |
3822 | */ |
51470298 |
3823 | { |
3824 | char *str; |
3825 | int i, j, len; |
3826 | |
3827 | if (ssh->pktin.type != SSH2_MSG_KEXINIT) { |
a8327734 |
3828 | bombout((ssh,"expected key exchange packet from server")); |
51470298 |
3829 | crReturn(0); |
32874aea |
3830 | } |
51470298 |
3831 | ssh->kex = NULL; |
3832 | ssh->hostkey = NULL; |
3833 | s->cscipher_tobe = NULL; |
3834 | s->sccipher_tobe = NULL; |
3835 | s->csmac_tobe = NULL; |
3836 | s->scmac_tobe = NULL; |
3837 | s->cscomp_tobe = NULL; |
3838 | s->sccomp_tobe = NULL; |
3839 | ssh->pktin.savedpos += 16; /* skip garbage cookie */ |
3840 | ssh2_pkt_getstring(ssh, &str, &len); /* key exchange algorithms */ |
3841 | for (i = 0; i < lenof(kex_algs); i++) { |
3842 | if (kex_algs[i] == &ssh_diffiehellman_gex && |
3843 | (ssh->remote_bugs & BUG_SSH2_DH_GEX)) |
3844 | continue; |
3845 | if (in_commasep_string(kex_algs[i]->name, str, len)) { |
3846 | ssh->kex = kex_algs[i]; |
3847 | break; |
3848 | } |
32874aea |
3849 | } |
51470298 |
3850 | ssh2_pkt_getstring(ssh, &str, &len); /* host key algorithms */ |
3851 | for (i = 0; i < lenof(hostkey_algs); i++) { |
3852 | if (in_commasep_string(hostkey_algs[i]->name, str, len)) { |
3853 | ssh->hostkey = hostkey_algs[i]; |
3854 | break; |
3855 | } |
3856 | } |
3857 | ssh2_pkt_getstring(ssh, &str, &len); /* client->server cipher */ |
3858 | s->warn = 0; |
3859 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
3860 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
3861 | if (!c) { |
3862 | s->warn = 1; |
3863 | } else { |
3864 | for (j = 0; j < c->nciphers; j++) { |
3865 | if (in_commasep_string(c->list[j]->name, str, len)) { |
3866 | s->cscipher_tobe = c->list[j]; |
3867 | break; |
3868 | } |
ca20bfcf |
3869 | } |
32874aea |
3870 | } |
51470298 |
3871 | if (s->cscipher_tobe) { |
3872 | if (s->warn) |
a8327734 |
3873 | askcipher(ssh->frontend, s->cscipher_tobe->name, 1); |
51470298 |
3874 | break; |
3875 | } |
32874aea |
3876 | } |
51470298 |
3877 | if (!s->cscipher_tobe) { |
57356d63 |
3878 | bombout((ssh,"Couldn't agree a client-to-server cipher (available: %s)", |
3879 | str ? str : "(null)")); |
51470298 |
3880 | crReturn(0); |
ca20bfcf |
3881 | } |
0ef8f407 |
3882 | |
51470298 |
3883 | ssh2_pkt_getstring(ssh, &str, &len); /* server->client cipher */ |
3884 | s->warn = 0; |
3885 | for (i = 0; i < s->n_preferred_ciphers; i++) { |
3886 | const struct ssh2_ciphers *c = s->preferred_ciphers[i]; |
3887 | if (!c) { |
3888 | s->warn = 1; |
3889 | } else { |
3890 | for (j = 0; j < c->nciphers; j++) { |
3891 | if (in_commasep_string(c->list[j]->name, str, len)) { |
3892 | s->sccipher_tobe = c->list[j]; |
3893 | break; |
3894 | } |
ca20bfcf |
3895 | } |
32874aea |
3896 | } |
51470298 |
3897 | if (s->sccipher_tobe) { |
3898 | if (s->warn) |
a8327734 |
3899 | askcipher(ssh->frontend, s->sccipher_tobe->name, 2); |
51470298 |
3900 | break; |
3901 | } |
32874aea |
3902 | } |
51470298 |
3903 | if (!s->sccipher_tobe) { |
57356d63 |
3904 | bombout((ssh,"Couldn't agree a server-to-client cipher (available: %s)", |
3905 | str ? str : "(null)")); |
51470298 |
3906 | crReturn(0); |
ca20bfcf |
3907 | } |
0ef8f407 |
3908 | |
51470298 |
3909 | ssh2_pkt_getstring(ssh, &str, &len); /* client->server mac */ |
3910 | for (i = 0; i < s->nmacs; i++) { |
3911 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
3912 | s->csmac_tobe = s->maclist[i]; |
3913 | break; |
3914 | } |
32874aea |
3915 | } |
51470298 |
3916 | ssh2_pkt_getstring(ssh, &str, &len); /* server->client mac */ |
3917 | for (i = 0; i < s->nmacs; i++) { |
3918 | if (in_commasep_string(s->maclist[i]->name, str, len)) { |
3919 | s->scmac_tobe = s->maclist[i]; |
3920 | break; |
3921 | } |
32874aea |
3922 | } |
51470298 |
3923 | ssh2_pkt_getstring(ssh, &str, &len); /* client->server compression */ |
3924 | for (i = 0; i < lenof(compressions) + 1; i++) { |
3925 | const struct ssh_compress *c = |
3926 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
3927 | if (in_commasep_string(c->name, str, len)) { |
3928 | s->cscomp_tobe = c; |
3929 | break; |
3930 | } |
32874aea |
3931 | } |
51470298 |
3932 | ssh2_pkt_getstring(ssh, &str, &len); /* server->client compression */ |
3933 | for (i = 0; i < lenof(compressions) + 1; i++) { |
3934 | const struct ssh_compress *c = |
3935 | i == 0 ? s->preferred_comp : compressions[i - 1]; |
3936 | if (in_commasep_string(c->name, str, len)) { |
3937 | s->sccomp_tobe = c; |
3938 | break; |
3939 | } |
32874aea |
3940 | } |
e5574168 |
3941 | } |
e5574168 |
3942 | |
3943 | /* |
7bd5a860 |
3944 | * Work out the number of bits of key we will need from the key |
3945 | * exchange. We start with the maximum key length of either |
3946 | * cipher... |
3947 | */ |
3948 | { |
32874aea |
3949 | int csbits, scbits; |
7bd5a860 |
3950 | |
51470298 |
3951 | csbits = s->cscipher_tobe->keylen; |
3952 | scbits = s->sccipher_tobe->keylen; |
3953 | s->nbits = (csbits > scbits ? csbits : scbits); |
7bd5a860 |
3954 | } |
3955 | /* The keys only have 160-bit entropy, since they're based on |
3956 | * a SHA-1 hash. So cap the key size at 160 bits. */ |
51470298 |
3957 | if (s->nbits > 160) |
3958 | s->nbits = 160; |
7bd5a860 |
3959 | |
3960 | /* |
a92dd380 |
3961 | * If we're doing Diffie-Hellman group exchange, start by |
3962 | * requesting a group. |
e5574168 |
3963 | */ |
51470298 |
3964 | if (ssh->kex == &ssh_diffiehellman_gex) { |
32874aea |
3965 | logevent("Doing Diffie-Hellman group exchange"); |
51470298 |
3966 | ssh->pkt_ctx |= SSH2_PKTCTX_DHGEX; |
32874aea |
3967 | /* |
3968 | * Work out how big a DH group we will need to allow that |
3969 | * much data. |
7bd5a860 |
3970 | */ |
51470298 |
3971 | s->pbits = 512 << ((s->nbits - 1) / 64); |
3972 | ssh2_pkt_init(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST); |
3973 | ssh2_pkt_adduint32(ssh, s->pbits); |
3974 | ssh2_pkt_send(ssh); |
32874aea |
3975 | |
3976 | crWaitUntil(ispkt); |
51470298 |
3977 | if (ssh->pktin.type != SSH2_MSG_KEX_DH_GEX_GROUP) { |
a8327734 |
3978 | bombout((ssh,"expected key exchange group packet from server")); |
32874aea |
3979 | crReturn(0); |
3980 | } |
51470298 |
3981 | s->p = ssh2_pkt_getmp(ssh); |
3982 | s->g = ssh2_pkt_getmp(ssh); |
27cd7fc2 |
3983 | ssh->kex_ctx = dh_setup_group(s->p, s->g); |
51470298 |
3984 | s->kex_init_value = SSH2_MSG_KEX_DH_GEX_INIT; |
3985 | s->kex_reply_value = SSH2_MSG_KEX_DH_GEX_REPLY; |
a92dd380 |
3986 | } else { |
51470298 |
3987 | ssh->pkt_ctx |= SSH2_PKTCTX_DHGROUP1; |
27cd7fc2 |
3988 | ssh->kex_ctx = dh_setup_group1(); |
51470298 |
3989 | s->kex_init_value = SSH2_MSG_KEXDH_INIT; |
3990 | s->kex_reply_value = SSH2_MSG_KEXDH_REPLY; |
8d5de777 |
3991 | } |
e5574168 |
3992 | |
a92dd380 |
3993 | logevent("Doing Diffie-Hellman key exchange"); |
e5574168 |
3994 | /* |
a92dd380 |
3995 | * Now generate and send e for Diffie-Hellman. |
e5574168 |
3996 | */ |
27cd7fc2 |
3997 | s->e = dh_create_e(ssh->kex_ctx, s->nbits * 2); |
51470298 |
3998 | ssh2_pkt_init(ssh, s->kex_init_value); |
3999 | ssh2_pkt_addmp(ssh, s->e); |
4000 | ssh2_pkt_send(ssh); |
e5574168 |
4001 | |
4002 | crWaitUntil(ispkt); |
51470298 |
4003 | if (ssh->pktin.type != s->kex_reply_value) { |
a8327734 |
4004 | bombout((ssh,"expected key exchange reply packet from server")); |
32874aea |
4005 | crReturn(0); |
7cca0d81 |
4006 | } |
51470298 |
4007 | ssh2_pkt_getstring(ssh, &s->hostkeydata, &s->hostkeylen); |
4008 | s->f = ssh2_pkt_getmp(ssh); |
4009 | ssh2_pkt_getstring(ssh, &s->sigdata, &s->siglen); |
e5574168 |
4010 | |
27cd7fc2 |
4011 | s->K = dh_find_K(ssh->kex_ctx, s->f); |
e5574168 |
4012 | |
51470298 |
4013 | sha_string(&ssh->exhash, s->hostkeydata, s->hostkeylen); |
4014 | if (ssh->kex == &ssh_diffiehellman_gex) { |
4015 | sha_uint32(&ssh->exhash, s->pbits); |
4016 | sha_mpint(&ssh->exhash, s->p); |
4017 | sha_mpint(&ssh->exhash, s->g); |
a92dd380 |
4018 | } |
51470298 |
4019 | sha_mpint(&ssh->exhash, s->e); |
4020 | sha_mpint(&ssh->exhash, s->f); |
4021 | sha_mpint(&ssh->exhash, s->K); |
4022 | SHA_Final(&ssh->exhash, s->exchange_hash); |
e5574168 |
4023 | |
27cd7fc2 |
4024 | dh_cleanup(ssh->kex_ctx); |
3709bfe9 |
4025 | |
7cca0d81 |
4026 | #if 0 |
765c4200 |
4027 | debug(("Exchange hash is:\n")); |
51470298 |
4028 | dmemdump(s->exchange_hash, 20); |
7cca0d81 |
4029 | #endif |
4030 | |
51470298 |
4031 | s->hkey = ssh->hostkey->newkey(s->hostkeydata, s->hostkeylen); |
4032 | if (!s->hkey || |
4033 | !ssh->hostkey->verifysig(s->hkey, s->sigdata, s->siglen, |
d8baa528 |
4034 | (char *)s->exchange_hash, 20)) { |
a8327734 |
4035 | bombout((ssh,"Server's host key did not match the signature supplied")); |
32874aea |
4036 | crReturn(0); |
8d5de777 |
4037 | } |
e5574168 |
4038 | |
4039 | /* |
7cca0d81 |
4040 | * Authenticate remote host: verify host key. (We've already |
4041 | * checked the signature of the exchange hash.) |
e5574168 |
4042 | */ |
51470298 |
4043 | s->keystr = ssh->hostkey->fmtkey(s->hkey); |
4044 | s->fingerprint = ssh->hostkey->fingerprint(s->hkey); |
a8327734 |
4045 | verify_ssh_host_key(ssh->frontend, |
4046 | ssh->savedhost, ssh->savedport, ssh->hostkey->keytype, |
51470298 |
4047 | s->keystr, s->fingerprint); |
4048 | if (s->first_kex) { /* don't bother logging this in rekeys */ |
5e0d7cb8 |
4049 | logevent("Host key fingerprint is:"); |
51470298 |
4050 | logevent(s->fingerprint); |
5e0d7cb8 |
4051 | } |
51470298 |
4052 | sfree(s->fingerprint); |
4053 | sfree(s->keystr); |
4054 | ssh->hostkey->freekey(s->hkey); |
d39f364a |
4055 | |
4056 | /* |
7cca0d81 |
4057 | * Send SSH2_MSG_NEWKEYS. |
d39f364a |
4058 | */ |
51470298 |
4059 | ssh2_pkt_init(ssh, SSH2_MSG_NEWKEYS); |
4060 | ssh2_pkt_send(ssh); |
d39f364a |
4061 | |
4062 | /* |
8406eaf9 |
4063 | * Expect SSH2_MSG_NEWKEYS from server. |
4064 | */ |
4065 | crWaitUntil(ispkt); |
51470298 |
4066 | if (ssh->pktin.type != SSH2_MSG_NEWKEYS) { |
a8327734 |
4067 | bombout((ssh,"expected new-keys packet from server")); |
8406eaf9 |
4068 | crReturn(0); |
4069 | } |
4070 | |
4071 | /* |
d39f364a |
4072 | * Create and initialise session keys. |
4073 | */ |
371e569c |
4074 | if (ssh->cs_cipher_ctx) |
4075 | ssh->cscipher->free_context(ssh->cs_cipher_ctx); |
51470298 |
4076 | ssh->cscipher = s->cscipher_tobe; |
371e569c |
4077 | ssh->cs_cipher_ctx = ssh->cscipher->make_context(); |
e0e1a00d |
4078 | |
371e569c |
4079 | if (ssh->sc_cipher_ctx) |
4080 | ssh->sccipher->free_context(ssh->sc_cipher_ctx); |
51470298 |
4081 | ssh->sccipher = s->sccipher_tobe; |
371e569c |
4082 | ssh->sc_cipher_ctx = ssh->sccipher->make_context(); |
e0e1a00d |
4083 | |
4084 | if (ssh->cs_mac_ctx) |
4085 | ssh->csmac->free_context(ssh->cs_mac_ctx); |
51470298 |
4086 | ssh->csmac = s->csmac_tobe; |
e0e1a00d |
4087 | ssh->cs_mac_ctx = ssh->csmac->make_context(); |
4088 | |
4089 | if (ssh->sc_mac_ctx) |
4090 | ssh->scmac->free_context(ssh->sc_mac_ctx); |
51470298 |
4091 | ssh->scmac = s->scmac_tobe; |
e0e1a00d |
4092 | ssh->sc_mac_ctx = ssh->scmac->make_context(); |
4093 | |
5366aed8 |
4094 | if (ssh->cs_comp_ctx) |
4095 | ssh->cscomp->compress_cleanup(ssh->cs_comp_ctx); |
51470298 |
4096 | ssh->cscomp = s->cscomp_tobe; |
5366aed8 |
4097 | ssh->cs_comp_ctx = ssh->cscomp->compress_init(); |
4098 | |
4099 | if (ssh->sc_comp_ctx) |
4100 | ssh->sccomp->decompress_cleanup(ssh->sc_comp_ctx); |
51470298 |
4101 | ssh->sccomp = s->sccomp_tobe; |
5366aed8 |
4102 | ssh->sc_comp_ctx = ssh->sccomp->decompress_init(); |
4103 | |
d39f364a |
4104 | /* |
5e0d7cb8 |
4105 | * Set IVs after keys. Here we use the exchange hash from the |
4106 | * _first_ key exchange. |
d39f364a |
4107 | */ |
51470298 |
4108 | { |
4109 | unsigned char keyspace[40]; |
4110 | if (s->first_kex) |
4111 | memcpy(ssh->v2_session_id, s->exchange_hash, |
4112 | sizeof(s->exchange_hash)); |
4113 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'C',keyspace); |
371e569c |
4114 | ssh->cscipher->setkey(ssh->cs_cipher_ctx, keyspace); |
51470298 |
4115 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'D',keyspace); |
371e569c |
4116 | ssh->sccipher->setkey(ssh->sc_cipher_ctx, keyspace); |
51470298 |
4117 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'A',keyspace); |
371e569c |
4118 | ssh->cscipher->setiv(ssh->cs_cipher_ctx, keyspace); |
51470298 |
4119 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'B',keyspace); |
371e569c |
4120 | ssh->sccipher->setiv(ssh->sc_cipher_ctx, keyspace); |
51470298 |
4121 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'E',keyspace); |
e0e1a00d |
4122 | ssh->csmac->setkey(ssh->cs_mac_ctx, keyspace); |
51470298 |
4123 | ssh2_mkkey(ssh,s->K,s->exchange_hash,ssh->v2_session_id,'F',keyspace); |
e0e1a00d |
4124 | ssh->scmac->setkey(ssh->sc_mac_ctx, keyspace); |
51470298 |
4125 | } |
57356d63 |
4126 | logeventf(ssh, "Initialised %.200s client->server encryption", |
4127 | ssh->cscipher->text_name); |
4128 | logeventf(ssh, "Initialised %.200s server->client encryption", |
4129 | ssh->sccipher->text_name); |
4130 | if (ssh->cscomp->text_name) |
4131 | logeventf(ssh, "Initialised %s compression", |
4132 | ssh->cscomp->text_name); |
4133 | if (ssh->sccomp->text_name) |
4134 | logeventf(ssh, "Initialised %s decompression", |
4135 | ssh->sccomp->text_name); |
d39f364a |
4136 | |
033b4cef |
4137 | /* |
0db56f73 |
4138 | * If this is the first key exchange phase, we must pass the |
4139 | * SSH2_MSG_NEWKEYS packet to the next layer, not because it |
4140 | * wants to see it but because it will need time to initialise |
4141 | * itself before it sees an actual packet. In subsequent key |
4142 | * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because |
4143 | * it would only confuse the layer above. |
4144 | */ |
51470298 |
4145 | if (!s->first_kex) { |
32874aea |
4146 | crReturn(0); |
0db56f73 |
4147 | } |
51470298 |
4148 | s->first_kex = 0; |
0db56f73 |
4149 | |
4150 | /* |
7cca0d81 |
4151 | * Now we're encrypting. Begin returning 1 to the protocol main |
4152 | * function so that other things can run on top of the |
4153 | * transport. If we ever see a KEXINIT, we must go back to the |
4154 | * start. |
033b4cef |
4155 | */ |
51470298 |
4156 | while (!(ispkt && ssh->pktin.type == SSH2_MSG_KEXINIT)) { |
32874aea |
4157 | crReturn(1); |
e96adf72 |
4158 | } |
5e0d7cb8 |
4159 | logevent("Server initiated key re-exchange"); |
7cca0d81 |
4160 | goto begin_key_exchange; |
e5574168 |
4161 | |
4162 | crFinish(1); |
4163 | } |
4164 | |
7cca0d81 |
4165 | /* |
783415f8 |
4166 | * Add data to an SSH2 channel output buffer. |
4167 | */ |
32874aea |
4168 | static void ssh2_add_channel_data(struct ssh_channel *c, char *buf, |
4169 | int len) |
4170 | { |
5471d09a |
4171 | bufchain_add(&c->v.v2.outbuffer, buf, len); |
783415f8 |
4172 | } |
4173 | |
4174 | /* |
4175 | * Attempt to send data on an SSH2 channel. |
4176 | */ |
5471d09a |
4177 | static int ssh2_try_send(struct ssh_channel *c) |
32874aea |
4178 | { |
51470298 |
4179 | Ssh ssh = c->ssh; |
4180 | |
5471d09a |
4181 | while (c->v.v2.remwindow > 0 && bufchain_size(&c->v.v2.outbuffer) > 0) { |
4182 | int len; |
4183 | void *data; |
4184 | bufchain_prefix(&c->v.v2.outbuffer, &data, &len); |
4185 | if ((unsigned)len > c->v.v2.remwindow) |
4186 | len = c->v.v2.remwindow; |
4187 | if ((unsigned)len > c->v.v2.remmaxpkt) |
4188 | len = c->v.v2.remmaxpkt; |
51470298 |
4189 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_DATA); |
4190 | ssh2_pkt_adduint32(ssh, c->remoteid); |
4191 | ssh2_pkt_addstring_start(ssh); |
4192 | ssh2_pkt_addstring_data(ssh, data, len); |
4193 | ssh2_pkt_send(ssh); |
5471d09a |
4194 | bufchain_consume(&c->v.v2.outbuffer, len); |
4195 | c->v.v2.remwindow -= len; |
4196 | } |
4197 | |
4198 | /* |
4199 | * After having sent as much data as we can, return the amount |
4200 | * still buffered. |
4201 | */ |
4202 | return bufchain_size(&c->v.v2.outbuffer); |
4203 | } |
4204 | |
4205 | /* |
4206 | * Potentially enlarge the window on an SSH2 channel. |
4207 | */ |
4208 | static void ssh2_set_window(struct ssh_channel *c, unsigned newwin) |
4209 | { |
51470298 |
4210 | Ssh ssh = c->ssh; |
4211 | |
6b69f42e |
4212 | /* |
4213 | * Never send WINDOW_ADJUST for a channel that the remote side |
4214 | * already thinks it's closed; there's no point, since it won't |
4215 | * be sending any more data anyway. |
4216 | */ |
4217 | if (c->closes != 0) |
4218 | return; |
4219 | |
5471d09a |
4220 | if (newwin > c->v.v2.locwindow) { |
51470298 |
4221 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
4222 | ssh2_pkt_adduint32(ssh, c->remoteid); |
4223 | ssh2_pkt_adduint32(ssh, newwin - c->v.v2.locwindow); |
4224 | ssh2_pkt_send(ssh); |
5471d09a |
4225 | c->v.v2.locwindow = newwin; |
783415f8 |
4226 | } |
4227 | } |
4228 | |
4229 | /* |
7cca0d81 |
4230 | * Handle the SSH2 userauth and connection layers. |
4231 | */ |
51470298 |
4232 | static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
7cca0d81 |
4233 | { |
51470298 |
4234 | struct do_ssh2_authconn_state { |
4235 | enum { |
4236 | AUTH_INVALID, AUTH_PUBLICKEY_AGENT, AUTH_PUBLICKEY_FILE, |
4237 | AUTH_PASSWORD, |
4238 | AUTH_KEYBOARD_INTERACTIVE |
4239 | } method; |
4240 | enum { |
4241 | AUTH_TYPE_NONE, |
4242 | AUTH_TYPE_PUBLICKEY, |
4243 | AUTH_TYPE_PUBLICKEY_OFFER_LOUD, |
4244 | AUTH_TYPE_PUBLICKEY_OFFER_QUIET, |
4245 | AUTH_TYPE_PASSWORD, |
4246 | AUTH_TYPE_KEYBOARD_INTERACTIVE, |
4247 | AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET |
4248 | } type; |
4249 | int gotit, need_pw, can_pubkey, can_passwd, can_keyb_inter; |
4250 | int tried_pubkey_config, tried_agent, tried_keyb_inter; |
4251 | int kbd_inter_running; |
4252 | int we_are_in; |
4253 | int num_prompts, curr_prompt, echo; |
4254 | char username[100]; |
4255 | int got_username; |
4256 | char pwprompt[200]; |
4257 | char password[100]; |
4258 | void *publickey_blob; |
4259 | int publickey_bloblen; |
4260 | unsigned char request[5], *response, *p; |
4261 | int responselen; |
4262 | int keyi, nkeys; |
4263 | int authed; |
4264 | char *pkblob, *alg, *commentp; |
4265 | int pklen, alglen, commentlen; |
4266 | int siglen, retlen, len; |
4267 | char *q, *agentreq, *ret; |
4268 | int try_send; |
4269 | }; |
4270 | crState(do_ssh2_authconn_state); |
4271 | |
4272 | crBegin(ssh->do_ssh2_authconn_crstate); |
e5574168 |
4273 | |
7cca0d81 |
4274 | /* |
4275 | * Request userauth protocol, and await a response to it. |
4276 | */ |
51470298 |
4277 | ssh2_pkt_init(ssh, SSH2_MSG_SERVICE_REQUEST); |
4278 | ssh2_pkt_addstring(ssh, "ssh-userauth"); |
4279 | ssh2_pkt_send(ssh); |
7cca0d81 |
4280 | crWaitUntilV(ispkt); |
51470298 |
4281 | if (ssh->pktin.type != SSH2_MSG_SERVICE_ACCEPT) { |
a8327734 |
4282 | bombout((ssh,"Server refused user authentication protocol")); |
32874aea |
4283 | crReturnV; |
8d5de777 |
4284 | } |
7cca0d81 |
4285 | |
4286 | /* |
1408a877 |
4287 | * We repeat this whole loop, including the username prompt, |
4288 | * until we manage a successful authentication. If the user |
51470298 |
4289 | * types the wrong _password_, they can be sent back to the |
4290 | * beginning to try another username, if this is configured on. |
4291 | * (If they specify a username in the config, they are never |
4292 | * asked, even if they do give a wrong password.) |
1408a877 |
4293 | * |
4294 | * I think this best serves the needs of |
4295 | * |
4296 | * - the people who have no configuration, no keys, and just |
4297 | * want to try repeated (username,password) pairs until they |
4298 | * type both correctly |
4299 | * |
4300 | * - people who have keys and configuration but occasionally |
4301 | * need to fall back to passwords |
4302 | * |
4303 | * - people with a key held in Pageant, who might not have |
4304 | * logged in to a particular machine before; so they want to |
4305 | * type a username, and then _either_ their key will be |
4306 | * accepted, _or_ they will type a password. If they mistype |
4307 | * the username they will want to be able to get back and |
4308 | * retype it! |
7cca0d81 |
4309 | */ |
51470298 |
4310 | s->username[0] = '\0'; |
4311 | s->got_username = FALSE; |
1408a877 |
4312 | do { |
1408a877 |
4313 | /* |
4314 | * Get a username. |
4315 | */ |
51470298 |
4316 | if (s->got_username && !cfg.change_username) { |
5bb641e1 |
4317 | /* |
4318 | * We got a username last time round this loop, and |
4319 | * with change_username turned off we don't try to get |
4320 | * it again. |
4321 | */ |
4322 | } else if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { |
c0a81592 |
4323 | if (ssh_get_line && !ssh_getline_pw_only) { |
32874aea |
4324 | if (!ssh_get_line("login as: ", |
51470298 |
4325 | s->username, sizeof(s->username), FALSE)) { |
32874aea |
4326 | /* |
4327 | * get_line failed to get a username. |
4328 | * Terminate. |
4329 | */ |
4330 | logevent("No username provided. Abandoning session."); |
51470298 |
4331 | ssh->state = SSH_STATE_CLOSED; |
32874aea |
4332 | crReturnV; |
4333 | } |
4334 | } else { |
51470298 |
4335 | int ret; /* need not be saved across crReturn */ |
4336 | c_write_str(ssh, "login as: "); |
4337 | ssh->send_ok = 1; |
4338 | setup_userpass_input(ssh, s->username, sizeof(s->username), 1); |
0405e71f |
4339 | do { |
32874aea |
4340 | crWaitUntilV(!ispkt); |
51470298 |
4341 | ret = process_userpass_input(ssh, in, inlen); |
0405e71f |
4342 | } while (ret == 0); |
4343 | if (ret < 0) |
4344 | cleanup_exit(0); |
4ca0c9d1 |
4345 | c_write_str(ssh, "\r\n"); |
32874aea |
4346 | } |
51470298 |
4347 | s->username[strcspn(s->username, "\n\r")] = '\0'; |
7cca0d81 |
4348 | } else { |
57356d63 |
4349 | char *stuff; |
51470298 |
4350 | strncpy(s->username, cfg.username, sizeof(s->username)); |
4351 | s->username[sizeof(s->username)-1] = '\0'; |
65a22376 |
4352 | if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) { |
57356d63 |
4353 | stuff = dupprintf("Using username \"%s\".\r\n", s->username); |
51470298 |
4354 | c_write_str(ssh, stuff); |
57356d63 |
4355 | sfree(stuff); |
7cca0d81 |
4356 | } |
4357 | } |
51470298 |
4358 | s->got_username = TRUE; |
7cca0d81 |
4359 | |
65a22376 |
4360 | /* |
1408a877 |
4361 | * Send an authentication request using method "none": (a) |
4362 | * just in case it succeeds, and (b) so that we know what |
4363 | * authentication methods we can usefully try next. |
65a22376 |
4364 | */ |
51470298 |
4365 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
4366 | |
4367 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
4368 | ssh2_pkt_addstring(ssh, s->username); |
4369 | ssh2_pkt_addstring(ssh, "ssh-connection");/* service requested */ |
4370 | ssh2_pkt_addstring(ssh, "none"); /* method */ |
4371 | ssh2_pkt_send(ssh); |
4372 | s->type = AUTH_TYPE_NONE; |
4373 | s->gotit = FALSE; |
4374 | s->we_are_in = FALSE; |
4375 | |
4376 | s->tried_pubkey_config = FALSE; |
4377 | s->tried_agent = FALSE; |
4378 | s->tried_keyb_inter = FALSE; |
4379 | s->kbd_inter_running = FALSE; |
396778f1 |
4380 | /* Load the pub half of cfg.keyfile so we notice if it's in Pageant */ |
4381 | if (*cfg.keyfile) { |
231ee168 |
4382 | int keytype; |
c2ad7d0d |
4383 | logeventf(ssh, "Reading private key file \"%.150s\"", cfg.keyfile); |
231ee168 |
4384 | keytype = key_type(cfg.keyfile); |
51470298 |
4385 | if (keytype == SSH_KEYTYPE_SSH2) { |
4386 | s->publickey_blob = |
4387 | ssh2_userkey_loadpub(cfg.keyfile, NULL, |
4388 | &s->publickey_bloblen); |
4389 | } else { |
57356d63 |
4390 | char *msgbuf; |
c2ad7d0d |
4391 | logeventf(ssh, "Unable to use this key file (%s)", |
a8327734 |
4392 | key_type_to_str(keytype)); |
57356d63 |
4393 | msgbuf = dupprintf("Unable to use key file \"%.150s\"" |
4394 | " (%s)\r\n", cfg.keyfile, |
4395 | key_type_to_str(keytype)); |
51470298 |
4396 | c_write_str(ssh, msgbuf); |
57356d63 |
4397 | sfree(msgbuf); |
51470298 |
4398 | s->publickey_blob = NULL; |
231ee168 |
4399 | } |
396778f1 |
4400 | } else |
51470298 |
4401 | s->publickey_blob = NULL; |
65a22376 |
4402 | |
1408a877 |
4403 | while (1) { |
4404 | /* |
4405 | * Wait for the result of the last authentication request. |
4406 | */ |
51470298 |
4407 | if (!s->gotit) |
1408a877 |
4408 | crWaitUntilV(ispkt); |
51470298 |
4409 | while (ssh->pktin.type == SSH2_MSG_USERAUTH_BANNER) { |
32874aea |
4410 | char *banner; |
4411 | int size; |
4412 | /* |
4413 | * Don't show the banner if we're operating in |
4414 | * non-verbose non-interactive mode. (It's probably |
4415 | * a script, which means nobody will read the |
4416 | * banner _anyway_, and moreover the printing of |
4417 | * the banner will screw up processing on the |
4418 | * output of (say) plink.) |
4419 | */ |
4420 | if (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE)) { |
51470298 |
4421 | ssh2_pkt_getstring(ssh, &banner, &size); |
32874aea |
4422 | if (banner) |
51470298 |
4423 | c_write_untrusted(ssh, banner, size); |
32874aea |
4424 | } |
1408a877 |
4425 | crWaitUntilV(ispkt); |
4426 | } |
51470298 |
4427 | if (ssh->pktin.type == SSH2_MSG_USERAUTH_SUCCESS) { |
1408a877 |
4428 | logevent("Access granted"); |
51470298 |
4429 | s->we_are_in = TRUE; |
1408a877 |
4430 | break; |
4431 | } |
65a22376 |
4432 | |
51470298 |
4433 | if (s->kbd_inter_running && |
4434 | ssh->pktin.type == SSH2_MSG_USERAUTH_INFO_REQUEST) { |
b3186d64 |
4435 | /* |
45068b27 |
4436 | * This is either a further set-of-prompts packet |
4437 | * in keyboard-interactive authentication, or it's |
4438 | * the same one and we came back here with `gotit' |
4439 | * set. In the former case, we must reset the |
4440 | * curr_prompt variable. |
b3186d64 |
4441 | */ |
51470298 |
4442 | if (!s->gotit) |
4443 | s->curr_prompt = 0; |
4444 | } else if (ssh->pktin.type != SSH2_MSG_USERAUTH_FAILURE) { |
a8327734 |
4445 | bombout((ssh,"Strange packet received during authentication: type %d", |
51470298 |
4446 | ssh->pktin.type)); |
38c4a8da |
4447 | crReturnV; |
65a22376 |
4448 | } |
4449 | |
51470298 |
4450 | s->gotit = FALSE; |
65a22376 |
4451 | |
1408a877 |
4452 | /* |
4453 | * OK, we're now sitting on a USERAUTH_FAILURE message, so |
4454 | * we can look at the string in it and know what we can |
4455 | * helpfully try next. |
4456 | */ |
51470298 |
4457 | if (ssh->pktin.type == SSH2_MSG_USERAUTH_FAILURE) { |
1408a877 |
4458 | char *methods; |
4459 | int methlen; |
51470298 |
4460 | ssh2_pkt_getstring(ssh, &methods, &methlen); |
4461 | s->kbd_inter_running = FALSE; |
4462 | if (!ssh2_pkt_getbool(ssh)) { |
1408a877 |
4463 | /* |
4464 | * We have received an unequivocal Access |
4465 | * Denied. This can translate to a variety of |
4466 | * messages: |
4467 | * |
4468 | * - if we'd just tried "none" authentication, |
4469 | * it's not worth printing anything at all |
4470 | * |
4471 | * - if we'd just tried a public key _offer_, |
4472 | * the message should be "Server refused our |
4473 | * key" (or no message at all if the key |
4474 | * came from Pageant) |
4475 | * |
4476 | * - if we'd just tried anything else, the |
4477 | * message really should be "Access denied". |
4478 | * |
4479 | * Additionally, if we'd just tried password |
4480 | * authentication, we should break out of this |
4481 | * whole loop so as to go back to the username |
4482 | * prompt. |
4483 | */ |
51470298 |
4484 | if (s->type == AUTH_TYPE_NONE) { |
1408a877 |
4485 | /* do nothing */ |
51470298 |
4486 | } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD || |
4487 | s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) { |
4488 | if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD) |
4489 | c_write_str(ssh, "Server refused our key\r\n"); |
1408a877 |
4490 | logevent("Server refused public key"); |
51470298 |
4491 | } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) { |
4bdf7c46 |
4492 | /* server declined keyboard-interactive; ignore */ |
1408a877 |
4493 | } else { |
51470298 |
4494 | c_write_str(ssh, "Access denied\r\n"); |
1408a877 |
4495 | logevent("Access denied"); |
51470298 |
4496 | if (s->type == AUTH_TYPE_PASSWORD) { |
4497 | s->we_are_in = FALSE; |
1408a877 |
4498 | break; |
4499 | } |
4500 | } |
4501 | } else { |
51470298 |
4502 | c_write_str(ssh, "Further authentication required\r\n"); |
1408a877 |
4503 | logevent("Further authentication required"); |
4504 | } |
65a22376 |
4505 | |
51470298 |
4506 | s->can_pubkey = |
32874aea |
4507 | in_commasep_string("publickey", methods, methlen); |
51470298 |
4508 | s->can_passwd = |
32874aea |
4509 | in_commasep_string("password", methods, methlen); |
51470298 |
4510 | s->can_keyb_inter = cfg.try_ki_auth && |
761187b6 |
4511 | in_commasep_string("keyboard-interactive", methods, methlen); |
1408a877 |
4512 | } |
65a22376 |
4513 | |
51470298 |
4514 | s->method = 0; |
4515 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
65a22376 |
4516 | |
0405e71f |
4517 | /* |
4518 | * Most password/passphrase prompts will be |
4519 | * non-echoing, so we set this to 0 by default. |
4520 | * Exception is that some keyboard-interactive prompts |
4521 | * can be echoing, in which case we'll set this to 1. |
4522 | */ |
51470298 |
4523 | s->echo = 0; |
0405e71f |
4524 | |
51470298 |
4525 | if (!s->method && s->can_pubkey && |
4526 | agent_exists() && !s->tried_agent) { |
1983e559 |
4527 | /* |
4528 | * Attempt public-key authentication using Pageant. |
4529 | */ |
1983e559 |
4530 | void *r; |
51470298 |
4531 | s->authed = FALSE; |
1983e559 |
4532 | |
51470298 |
4533 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
4534 | ssh->pkt_ctx |= SSH2_PKTCTX_PUBLICKEY; |
00db133f |
4535 | |
51470298 |
4536 | s->tried_agent = TRUE; |
1983e559 |
4537 | |
4538 | logevent("Pageant is running. Requesting keys."); |
4539 | |
4540 | /* Request the keys held by the agent. */ |
51470298 |
4541 | PUT_32BIT(s->request, 1); |
4542 | s->request[4] = SSH2_AGENTC_REQUEST_IDENTITIES; |
4543 | agent_query(s->request, 5, &r, &s->responselen); |
4544 | s->response = (unsigned char *) r; |
4545 | if (s->response && s->responselen >= 5 && |
4546 | s->response[4] == SSH2_AGENT_IDENTITIES_ANSWER) { |
4547 | s->p = s->response + 5; |
4548 | s->nkeys = GET_32BIT(s->p); |
4549 | s->p += 4; |
32874aea |
4550 | { |
4551 | char buf[64]; |
51470298 |
4552 | sprintf(buf, "Pageant has %d SSH2 keys", s->nkeys); |
32874aea |
4553 | logevent(buf); |
4554 | } |
51470298 |
4555 | for (s->keyi = 0; s->keyi < s->nkeys; s->keyi++) { |
2d466ffd |
4556 | void *vret; |
1983e559 |
4557 | |
32874aea |
4558 | { |
4559 | char buf[64]; |
51470298 |
4560 | sprintf(buf, "Trying Pageant key #%d", s->keyi); |
32874aea |
4561 | logevent(buf); |
4562 | } |
51470298 |
4563 | s->pklen = GET_32BIT(s->p); |
4564 | s->p += 4; |
4565 | if (s->publickey_blob && |
4566 | s->pklen == s->publickey_bloblen && |
4567 | !memcmp(s->p, s->publickey_blob, |
4568 | s->publickey_bloblen)) { |
396778f1 |
4569 | logevent("This key matches configured key file"); |
51470298 |
4570 | s->tried_pubkey_config = 1; |
396778f1 |
4571 | } |
d8baa528 |
4572 | s->pkblob = (char *)s->p; |
51470298 |
4573 | s->p += s->pklen; |
4574 | s->alglen = GET_32BIT(s->pkblob); |
4575 | s->alg = s->pkblob + 4; |
4576 | s->commentlen = GET_32BIT(s->p); |
4577 | s->p += 4; |
d8baa528 |
4578 | s->commentp = (char *)s->p; |
51470298 |
4579 | s->p += s->commentlen; |
4580 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
4581 | ssh2_pkt_addstring(ssh, s->username); |
4582 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
4583 | ssh2_pkt_addstring(ssh, "publickey"); /* method */ |
4584 | ssh2_pkt_addbool(ssh, FALSE); /* no signature included */ |
4585 | ssh2_pkt_addstring_start(ssh); |
4586 | ssh2_pkt_addstring_data(ssh, s->alg, s->alglen); |
4587 | ssh2_pkt_addstring_start(ssh); |
4588 | ssh2_pkt_addstring_data(ssh, s->pkblob, s->pklen); |
4589 | ssh2_pkt_send(ssh); |
1983e559 |
4590 | |
4591 | crWaitUntilV(ispkt); |
51470298 |
4592 | if (ssh->pktin.type != SSH2_MSG_USERAUTH_PK_OK) { |
1983e559 |
4593 | logevent("Key refused"); |
4594 | continue; |
4595 | } |
4596 | |
32874aea |
4597 | if (flags & FLAG_VERBOSE) { |
51470298 |
4598 | c_write_str(ssh, "Authenticating with " |
4599 | "public key \""); |
4600 | c_write(ssh, s->commentp, s->commentlen); |
4601 | c_write_str(ssh, "\" from agent\r\n"); |
32874aea |
4602 | } |
1983e559 |
4603 | |
4604 | /* |
4605 | * Server is willing to accept the key. |
4606 | * Construct a SIGN_REQUEST. |
4607 | */ |
51470298 |
4608 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
4609 | ssh2_pkt_addstring(ssh, s->username); |
4610 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
4611 | ssh2_pkt_addstring(ssh, "publickey"); /* method */ |
4612 | ssh2_pkt_addbool(ssh, TRUE); |
4613 | ssh2_pkt_addstring_start(ssh); |
4614 | ssh2_pkt_addstring_data(ssh, s->alg, s->alglen); |
4615 | ssh2_pkt_addstring_start(ssh); |
4616 | ssh2_pkt_addstring_data(ssh, s->pkblob, s->pklen); |
4617 | |
4618 | s->siglen = ssh->pktout.length - 5 + 4 + 20; |
4619 | s->len = 1; /* message type */ |
4620 | s->len += 4 + s->pklen; /* key blob */ |
4621 | s->len += 4 + s->siglen; /* data to sign */ |
4622 | s->len += 4; /* flags */ |
4623 | s->agentreq = smalloc(4 + s->len); |
4624 | PUT_32BIT(s->agentreq, s->len); |
4625 | s->q = s->agentreq + 4; |
4626 | *s->q++ = SSH2_AGENTC_SIGN_REQUEST; |
4627 | PUT_32BIT(s->q, s->pklen); |
4628 | s->q += 4; |
4629 | memcpy(s->q, s->pkblob, s->pklen); |
4630 | s->q += s->pklen; |
4631 | PUT_32BIT(s->q, s->siglen); |
4632 | s->q += 4; |
1983e559 |
4633 | /* Now the data to be signed... */ |
51470298 |
4634 | PUT_32BIT(s->q, 20); |
4635 | s->q += 4; |
4636 | memcpy(s->q, ssh->v2_session_id, 20); |
4637 | s->q += 20; |
4638 | memcpy(s->q, ssh->pktout.data + 5, |
4639 | ssh->pktout.length - 5); |
4640 | s->q += ssh->pktout.length - 5; |
1983e559 |
4641 | /* And finally the (zero) flags word. */ |
51470298 |
4642 | PUT_32BIT(s->q, 0); |
4643 | agent_query(s->agentreq, s->len + 4, &vret, &s->retlen); |
4644 | s->ret = vret; |
4645 | sfree(s->agentreq); |
4646 | if (s->ret) { |
4647 | if (s->ret[4] == SSH2_AGENT_SIGN_RESPONSE) { |
1983e559 |
4648 | logevent("Sending Pageant's response"); |
51470298 |
4649 | ssh2_add_sigblob(ssh, s->pkblob, s->pklen, |
4650 | s->ret + 9, |
4651 | GET_32BIT(s->ret + 5)); |
4652 | ssh2_pkt_send(ssh); |
4653 | s->authed = TRUE; |
1983e559 |
4654 | break; |
4655 | } else { |
32874aea |
4656 | logevent |
4657 | ("Pageant failed to answer challenge"); |
51470298 |
4658 | sfree(s->ret); |
1983e559 |
4659 | } |
4660 | } |
4661 | } |
51470298 |
4662 | if (s->authed) |
1983e559 |
4663 | continue; |
4664 | } |
4665 | } |
4666 | |
51470298 |
4667 | if (!s->method && s->can_pubkey && s->publickey_blob |
4668 | && !s->tried_pubkey_config) { |
1408a877 |
4669 | unsigned char *pub_blob; |
4670 | char *algorithm, *comment; |
4671 | int pub_blob_len; |
65a22376 |
4672 | |
51470298 |
4673 | s->tried_pubkey_config = TRUE; |
65a22376 |
4674 | |
51470298 |
4675 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
4676 | ssh->pkt_ctx |= SSH2_PKTCTX_PUBLICKEY; |
00db133f |
4677 | |
65a22376 |
4678 | /* |
1408a877 |
4679 | * Try the public key supplied in the configuration. |
4680 | * |
4681 | * First, offer the public blob to see if the server is |
4682 | * willing to accept it. |
65a22376 |
4683 | */ |
d8baa528 |
4684 | pub_blob = |
4685 | (unsigned char *)ssh2_userkey_loadpub(cfg.keyfile, |
4686 | &algorithm, |
4687 | &pub_blob_len); |
1408a877 |
4688 | if (pub_blob) { |
51470298 |
4689 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
4690 | ssh2_pkt_addstring(ssh, s->username); |
4691 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
4692 | ssh2_pkt_addstring(ssh, "publickey"); /* method */ |
4693 | ssh2_pkt_addbool(ssh, FALSE); /* no signature included */ |
4694 | ssh2_pkt_addstring(ssh, algorithm); |
4695 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
4696 | ssh2_pkt_addstring_data(ssh, (char *)pub_blob, |
4697 | pub_blob_len); |
51470298 |
4698 | ssh2_pkt_send(ssh); |
32874aea |
4699 | logevent("Offered public key"); /* FIXME */ |
1408a877 |
4700 | |
4701 | crWaitUntilV(ispkt); |
51470298 |
4702 | if (ssh->pktin.type != SSH2_MSG_USERAUTH_PK_OK) { |
4703 | s->gotit = TRUE; |
4704 | s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD; |
32874aea |
4705 | continue; /* key refused; give up on it */ |
1408a877 |
4706 | } |
65a22376 |
4707 | |
1408a877 |
4708 | logevent("Offer of public key accepted"); |
65a22376 |
4709 | /* |
1408a877 |
4710 | * Actually attempt a serious authentication using |
4711 | * the key. |
65a22376 |
4712 | */ |
1408a877 |
4713 | if (ssh2_userkey_encrypted(cfg.keyfile, &comment)) { |
51470298 |
4714 | sprintf(s->pwprompt, |
32874aea |
4715 | "Passphrase for key \"%.100s\": ", |
4716 | comment); |
51470298 |
4717 | s->need_pw = TRUE; |
1408a877 |
4718 | } else { |
51470298 |
4719 | s->need_pw = FALSE; |
1408a877 |
4720 | } |
51470298 |
4721 | c_write_str(ssh, "Authenticating with public key \""); |
4722 | c_write_str(ssh, comment); |
4723 | c_write_str(ssh, "\"\r\n"); |
4724 | s->method = AUTH_PUBLICKEY_FILE; |
65a22376 |
4725 | } |
1408a877 |
4726 | } |
4727 | |
51470298 |
4728 | if (!s->method && s->can_keyb_inter && !s->tried_keyb_inter) { |
4729 | s->method = AUTH_KEYBOARD_INTERACTIVE; |
4730 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
4731 | s->tried_keyb_inter = TRUE; |
af659722 |
4732 | |
51470298 |
4733 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
4734 | ssh->pkt_ctx |= SSH2_PKTCTX_KBDINTER; |
00db133f |
4735 | |
51470298 |
4736 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
4737 | ssh2_pkt_addstring(ssh, s->username); |
4738 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
4739 | ssh2_pkt_addstring(ssh, "keyboard-interactive"); /* method */ |
4740 | ssh2_pkt_addstring(ssh, ""); /* lang */ |
4741 | ssh2_pkt_addstring(ssh, ""); |
4742 | ssh2_pkt_send(ssh); |
af659722 |
4743 | |
4744 | crWaitUntilV(ispkt); |
51470298 |
4745 | if (ssh->pktin.type != SSH2_MSG_USERAUTH_INFO_REQUEST) { |
4746 | if (ssh->pktin.type == SSH2_MSG_USERAUTH_FAILURE) |
4747 | s->gotit = TRUE; |
af659722 |
4748 | logevent("Keyboard-interactive authentication refused"); |
51470298 |
4749 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET; |
af659722 |
4750 | continue; |
4751 | } |
4752 | |
51470298 |
4753 | s->kbd_inter_running = TRUE; |
4754 | s->curr_prompt = 0; |
af659722 |
4755 | } |
4756 | |
51470298 |
4757 | if (s->kbd_inter_running) { |
4758 | s->method = AUTH_KEYBOARD_INTERACTIVE; |
4759 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
4760 | s->tried_keyb_inter = TRUE; |
af659722 |
4761 | |
51470298 |
4762 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
4763 | ssh->pkt_ctx |= SSH2_PKTCTX_KBDINTER; |
00db133f |
4764 | |
51470298 |
4765 | if (s->curr_prompt == 0) { |
45068b27 |
4766 | /* |
4767 | * We've got a fresh USERAUTH_INFO_REQUEST. |
4768 | * Display header data, and start going through |
4769 | * the prompts. |
4770 | */ |
4771 | char *name, *inst, *lang; |
4772 | int name_len, inst_len, lang_len; |
4773 | |
51470298 |
4774 | ssh2_pkt_getstring(ssh, &name, &name_len); |
4775 | ssh2_pkt_getstring(ssh, &inst, &inst_len); |
4776 | ssh2_pkt_getstring(ssh, &lang, &lang_len); |
45068b27 |
4777 | if (name_len > 0) { |
51470298 |
4778 | c_write_untrusted(ssh, name, name_len); |
4779 | c_write_str(ssh, "\r\n"); |
45068b27 |
4780 | } |
4781 | if (inst_len > 0) { |
51470298 |
4782 | c_write_untrusted(ssh, inst, inst_len); |
4783 | c_write_str(ssh, "\r\n"); |
45068b27 |
4784 | } |
51470298 |
4785 | s->num_prompts = ssh2_pkt_getuint32(ssh); |
45068b27 |
4786 | } |
af659722 |
4787 | |
45068b27 |
4788 | /* |
4789 | * If there are prompts remaining in the packet, |
4790 | * display one and get a response. |
4791 | */ |
51470298 |
4792 | if (s->curr_prompt < s->num_prompts) { |
45068b27 |
4793 | char *prompt; |
4794 | int prompt_len; |
af659722 |
4795 | |
51470298 |
4796 | ssh2_pkt_getstring(ssh, &prompt, &prompt_len); |
45068b27 |
4797 | if (prompt_len > 0) { |
51470298 |
4798 | strncpy(s->pwprompt, prompt, sizeof(s->pwprompt)); |
4799 | s->pwprompt[prompt_len < sizeof(s->pwprompt) ? |
4800 | prompt_len : sizeof(s->pwprompt)-1] = '\0'; |
45068b27 |
4801 | } else { |
51470298 |
4802 | strcpy(s->pwprompt, |
45068b27 |
4803 | "<server failed to send prompt>: "); |
4804 | } |
51470298 |
4805 | s->echo = ssh2_pkt_getbool(ssh); |
4806 | s->need_pw = TRUE; |
45068b27 |
4807 | } else |
51470298 |
4808 | s->need_pw = FALSE; |
af659722 |
4809 | } |
4810 | |
51470298 |
4811 | if (!s->method && s->can_passwd) { |
4812 | s->method = AUTH_PASSWORD; |
4813 | ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK; |
4814 | ssh->pkt_ctx |= SSH2_PKTCTX_PASSWORD; |
4815 | sprintf(s->pwprompt, "%.90s@%.90s's password: ", s->username, |
4816 | ssh->savedhost); |
4817 | s->need_pw = TRUE; |
1408a877 |
4818 | } |
4819 | |
51470298 |
4820 | if (s->need_pw) { |
fa17a66e |
4821 | if (ssh_get_line) { |
51470298 |
4822 | if (!ssh_get_line(s->pwprompt, s->password, |
4823 | sizeof(s->password), TRUE)) { |
1408a877 |
4824 | /* |
fa17a66e |
4825 | * get_line failed to get a password (for |
4826 | * example because one was supplied on the |
4827 | * command line which has already failed to |
4828 | * work). Terminate. |
1408a877 |
4829 | */ |
51470298 |
4830 | ssh2_pkt_init(ssh, SSH2_MSG_DISCONNECT); |
4831 | ssh2_pkt_adduint32(ssh,SSH2_DISCONNECT_BY_APPLICATION); |
4832 | ssh2_pkt_addstring(ssh, "No more passwords available" |
4833 | " to try"); |
4834 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
4835 | ssh2_pkt_send(ssh); |
247308b5 |
4836 | logevent("Unable to authenticate"); |
a8327734 |
4837 | connection_fatal(ssh->frontend, |
4838 | "Unable to authenticate"); |
51470298 |
4839 | ssh->state = SSH_STATE_CLOSED; |
1408a877 |
4840 | crReturnV; |
4841 | } |
4842 | } else { |
51470298 |
4843 | int ret; /* need not be saved across crReturn */ |
4844 | c_write_untrusted(ssh, s->pwprompt, strlen(s->pwprompt)); |
4845 | ssh->send_ok = 1; |
1408a877 |
4846 | |
51470298 |
4847 | setup_userpass_input(ssh, s->password, |
4848 | sizeof(s->password), s->echo); |
0405e71f |
4849 | do { |
1408a877 |
4850 | crWaitUntilV(!ispkt); |
51470298 |
4851 | ret = process_userpass_input(ssh, in, inlen); |
0405e71f |
4852 | } while (ret == 0); |
4853 | if (ret < 0) |
4854 | cleanup_exit(0); |
51470298 |
4855 | c_write_str(ssh, "\r\n"); |
65a22376 |
4856 | } |
65a22376 |
4857 | } |
65a22376 |
4858 | |
51470298 |
4859 | if (s->method == AUTH_PUBLICKEY_FILE) { |
1408a877 |
4860 | /* |
4861 | * We have our passphrase. Now try the actual authentication. |
4862 | */ |
4863 | struct ssh2_userkey *key; |
65a22376 |
4864 | |
51470298 |
4865 | key = ssh2_load_userkey(cfg.keyfile, s->password); |
1408a877 |
4866 | if (key == SSH2_WRONG_PASSPHRASE || key == NULL) { |
4867 | if (key == SSH2_WRONG_PASSPHRASE) { |
51470298 |
4868 | c_write_str(ssh, "Wrong passphrase\r\n"); |
4869 | s->tried_pubkey_config = FALSE; |
1408a877 |
4870 | } else { |
51470298 |
4871 | c_write_str(ssh, "Unable to load private key\r\n"); |
4872 | s->tried_pubkey_config = TRUE; |
1408a877 |
4873 | } |
4874 | /* Send a spurious AUTH_NONE to return to the top. */ |
51470298 |
4875 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
4876 | ssh2_pkt_addstring(ssh, s->username); |
4877 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
4878 | ssh2_pkt_addstring(ssh, "none"); /* method */ |
4879 | ssh2_pkt_send(ssh); |
4880 | s->type = AUTH_TYPE_NONE; |
1408a877 |
4881 | } else { |
1dd353b5 |
4882 | unsigned char *pkblob, *sigblob, *sigdata; |
4883 | int pkblob_len, sigblob_len, sigdata_len; |
65a22376 |
4884 | |
1408a877 |
4885 | /* |
4886 | * We have loaded the private key and the server |
4887 | * has announced that it's willing to accept it. |
4888 | * Hallelujah. Generate a signature and send it. |
4889 | */ |
51470298 |
4890 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
4891 | ssh2_pkt_addstring(ssh, s->username); |
4892 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
4893 | ssh2_pkt_addstring(ssh, "publickey"); /* method */ |
4894 | ssh2_pkt_addbool(ssh, TRUE); |
4895 | ssh2_pkt_addstring(ssh, key->alg->name); |
1dd353b5 |
4896 | pkblob = key->alg->public_blob(key->data, &pkblob_len); |
51470298 |
4897 | ssh2_pkt_addstring_start(ssh); |
d8baa528 |
4898 | ssh2_pkt_addstring_data(ssh, (char *)pkblob, pkblob_len); |
1408a877 |
4899 | |
4900 | /* |
4901 | * The data to be signed is: |
4902 | * |
4903 | * string session-id |
4904 | * |
4905 | * followed by everything so far placed in the |
4906 | * outgoing packet. |
4907 | */ |
51470298 |
4908 | sigdata_len = ssh->pktout.length - 5 + 4 + 20; |
1408a877 |
4909 | sigdata = smalloc(sigdata_len); |
4910 | PUT_32BIT(sigdata, 20); |
51470298 |
4911 | memcpy(sigdata + 4, ssh->v2_session_id, 20); |
4912 | memcpy(sigdata + 24, ssh->pktout.data + 5, |
4913 | ssh->pktout.length - 5); |
d8baa528 |
4914 | sigblob = key->alg->sign(key->data, (char *)sigdata, |
1dd353b5 |
4915 | sigdata_len, &sigblob_len); |
51470298 |
4916 | ssh2_add_sigblob(ssh, pkblob, pkblob_len, |
1dd353b5 |
4917 | sigblob, sigblob_len); |
4918 | sfree(pkblob); |
4919 | sfree(sigblob); |
1408a877 |
4920 | sfree(sigdata); |
4921 | |
51470298 |
4922 | ssh2_pkt_send(ssh); |
4923 | s->type = AUTH_TYPE_PUBLICKEY; |
1408a877 |
4924 | } |
51470298 |
4925 | } else if (s->method == AUTH_PASSWORD) { |
65a22376 |
4926 | /* |
1408a877 |
4927 | * We send the password packet lumped tightly together with |
4928 | * an SSH_MSG_IGNORE packet. The IGNORE packet contains a |
4929 | * string long enough to make the total length of the two |
4930 | * packets constant. This should ensure that a passive |
4931 | * listener doing traffic analyis can't work out the length |
4932 | * of the password. |
4933 | * |
4934 | * For this to work, we need an assumption about the |
4935 | * maximum length of the password packet. I think 256 is |
4936 | * pretty conservative. Anyone using a password longer than |
4937 | * that probably doesn't have much to worry about from |
4938 | * people who find out how long their password is! |
65a22376 |
4939 | */ |
51470298 |
4940 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_REQUEST); |
4941 | ssh2_pkt_addstring(ssh, s->username); |
4942 | ssh2_pkt_addstring(ssh, "ssh-connection"); /* service requested */ |
4943 | ssh2_pkt_addstring(ssh, "password"); |
4944 | ssh2_pkt_addbool(ssh, FALSE); |
4945 | ssh2_pkt_addstring(ssh, s->password); |
1409c1b3 |
4946 | memset(s->password, 0, sizeof(s->password)); |
51470298 |
4947 | ssh2_pkt_defer(ssh); |
65a22376 |
4948 | /* |
1408a877 |
4949 | * We'll include a string that's an exact multiple of the |
4950 | * cipher block size. If the cipher is NULL for some |
4951 | * reason, we don't do this trick at all because we gain |
4952 | * nothing by it. |
65a22376 |
4953 | */ |
51470298 |
4954 | if (ssh->cscipher) { |
32874aea |
4955 | int stringlen, i; |
4956 | |
51470298 |
4957 | stringlen = (256 - ssh->deferred_len); |
4958 | stringlen += ssh->cscipher->blksize - 1; |
4959 | stringlen -= (stringlen % ssh->cscipher->blksize); |
4960 | if (ssh->cscomp) { |
32874aea |
4961 | /* |
4962 | * Temporarily disable actual compression, |
4963 | * so we can guarantee to get this string |
4964 | * exactly the length we want it. The |
4965 | * compression-disabling routine should |
4966 | * return an integer indicating how many |
4967 | * bytes we should adjust our string length |
4968 | * by. |
4969 | */ |
5366aed8 |
4970 | stringlen -= |
4971 | ssh->cscomp->disable_compression(ssh->cs_comp_ctx); |
32874aea |
4972 | } |
51470298 |
4973 | ssh2_pkt_init(ssh, SSH2_MSG_IGNORE); |
4974 | ssh2_pkt_addstring_start(ssh); |
6e9e9520 |
4975 | for (i = 0; i < stringlen; i++) { |
32874aea |
4976 | char c = (char) random_byte(); |
51470298 |
4977 | ssh2_pkt_addstring_data(ssh, &c, 1); |
65a22376 |
4978 | } |
51470298 |
4979 | ssh2_pkt_defer(ssh); |
65a22376 |
4980 | } |
51470298 |
4981 | ssh_pkt_defersend(ssh); |
0d43337a |
4982 | logevent("Sent password"); |
51470298 |
4983 | s->type = AUTH_TYPE_PASSWORD; |
4984 | } else if (s->method == AUTH_KEYBOARD_INTERACTIVE) { |
4985 | if (s->curr_prompt == 0) { |
4986 | ssh2_pkt_init(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE); |
4987 | ssh2_pkt_adduint32(ssh, s->num_prompts); |
45068b27 |
4988 | } |
51470298 |
4989 | if (s->need_pw) { /* only add pw if we just got one! */ |
4990 | ssh2_pkt_addstring(ssh, s->password); |
4991 | memset(s->password, 0, sizeof(s->password)); |
4992 | s->curr_prompt++; |
45068b27 |
4993 | } |
51470298 |
4994 | if (s->curr_prompt >= s->num_prompts) { |
4995 | ssh2_pkt_send(ssh); |
45068b27 |
4996 | } else { |
4997 | /* |
4998 | * If there are prompts remaining, we set |
4999 | * `gotit' so that we won't attempt to get |
5000 | * another packet. Then we go back round the |
5001 | * loop and will end up retrieving another |
5002 | * prompt out of the existing packet. Funky or |
5003 | * what? |
5004 | */ |
51470298 |
5005 | s->gotit = TRUE; |
45068b27 |
5006 | } |
51470298 |
5007 | s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE; |
1408a877 |
5008 | } else { |
51470298 |
5009 | c_write_str(ssh, "No supported authentication methods" |
5010 | " left to try!\r\n"); |
5011 | logevent("No supported authentications offered." |
5012 | " Disconnecting"); |
5013 | ssh2_pkt_init(ssh, SSH2_MSG_DISCONNECT); |
5014 | ssh2_pkt_adduint32(ssh, SSH2_DISCONNECT_BY_APPLICATION); |
5015 | ssh2_pkt_addstring(ssh, "No supported authentication" |
5016 | " methods available"); |
5017 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
5018 | ssh2_pkt_send(ssh); |
5019 | ssh->state = SSH_STATE_CLOSED; |
1408a877 |
5020 | crReturnV; |
65a22376 |
5021 | } |
65a22376 |
5022 | } |
51470298 |
5023 | } while (!s->we_are_in); |
7cca0d81 |
5024 | |
5025 | /* |
5026 | * Now we're authenticated for the connection protocol. The |
5027 | * connection protocol will automatically have started at this |
5028 | * point; there's no need to send SERVICE_REQUEST. |
5029 | */ |
5030 | |
5031 | /* |
5032 | * So now create a channel with a session in it. |
5033 | */ |
51470298 |
5034 | ssh->channels = newtree234(ssh_channelcmp); |
5035 | ssh->mainchan = smalloc(sizeof(struct ssh_channel)); |
5036 | ssh->mainchan->ssh = ssh; |
5037 | ssh->mainchan->localid = alloc_channel_id(ssh); |
5038 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN); |
5039 | ssh2_pkt_addstring(ssh, "session"); |
5040 | ssh2_pkt_adduint32(ssh, ssh->mainchan->localid); |
5041 | ssh->mainchan->v.v2.locwindow = OUR_V2_WINSIZE; |
5042 | ssh2_pkt_adduint32(ssh, ssh->mainchan->v.v2.locwindow);/* our window size */ |
5043 | ssh2_pkt_adduint32(ssh, 0x4000UL); /* our max pkt size */ |
5044 | ssh2_pkt_send(ssh); |
7cca0d81 |
5045 | crWaitUntilV(ispkt); |
51470298 |
5046 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
a8327734 |
5047 | bombout((ssh,"Server refused to open a session")); |
32874aea |
5048 | crReturnV; |
5049 | /* FIXME: error data comes back in FAILURE packet */ |
7cca0d81 |
5050 | } |
51470298 |
5051 | if (ssh2_pkt_getuint32(ssh) != ssh->mainchan->localid) { |
a8327734 |
5052 | bombout((ssh,"Server's channel confirmation cited wrong channel")); |
32874aea |
5053 | crReturnV; |
7cca0d81 |
5054 | } |
51470298 |
5055 | ssh->mainchan->remoteid = ssh2_pkt_getuint32(ssh); |
5056 | ssh->mainchan->type = CHAN_MAINSESSION; |
5057 | ssh->mainchan->closes = 0; |
5058 | ssh->mainchan->v.v2.remwindow = ssh2_pkt_getuint32(ssh); |
5059 | ssh->mainchan->v.v2.remmaxpkt = ssh2_pkt_getuint32(ssh); |
5060 | bufchain_init(&ssh->mainchan->v.v2.outbuffer); |
5061 | add234(ssh->channels, ssh->mainchan); |
7cca0d81 |
5062 | logevent("Opened channel for session"); |
5063 | |
5064 | /* |
783415f8 |
5065 | * Potentially enable X11 forwarding. |
5066 | */ |
5067 | if (cfg.x11_forward) { |
32874aea |
5068 | char proto[20], data[64]; |
5069 | logevent("Requesting X11 forwarding"); |
302121de |
5070 | ssh->x11auth = x11_invent_auth(proto, sizeof(proto), |
5071 | data, sizeof(data)); |
e0e7dff8 |
5072 | x11_get_real_auth(ssh->x11auth, cfg.x11_display); |
51470298 |
5073 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
5074 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); |
5075 | ssh2_pkt_addstring(ssh, "x11-req"); |
5076 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
5077 | ssh2_pkt_addbool(ssh, 0); /* many connections */ |
5078 | ssh2_pkt_addstring(ssh, proto); |
5079 | ssh2_pkt_addstring(ssh, data); |
421d6835 |
5080 | ssh2_pkt_adduint32(ssh, x11_get_screen_number(cfg.x11_display)); |
51470298 |
5081 | ssh2_pkt_send(ssh); |
32874aea |
5082 | |
5083 | do { |
5084 | crWaitUntilV(ispkt); |
51470298 |
5085 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
5086 | unsigned i = ssh2_pkt_getuint32(ssh); |
32874aea |
5087 | struct ssh_channel *c; |
51470298 |
5088 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
5089 | if (!c) |
5090 | continue; /* nonexistent channel */ |
51470298 |
5091 | c->v.v2.remwindow += ssh2_pkt_getuint32(ssh); |
32874aea |
5092 | } |
51470298 |
5093 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
32874aea |
5094 | |
51470298 |
5095 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
5096 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
a8327734 |
5097 | bombout((ssh,"Unexpected response to X11 forwarding request:" |
51470298 |
5098 | " packet type %d", ssh->pktin.type)); |
32874aea |
5099 | crReturnV; |
5100 | } |
5101 | logevent("X11 forwarding refused"); |
5102 | } else { |
5103 | logevent("X11 forwarding enabled"); |
51470298 |
5104 | ssh->X11_fwd_enabled = TRUE; |
32874aea |
5105 | } |
783415f8 |
5106 | } |
5107 | |
5108 | /* |
bc240b21 |
5109 | * Enable port forwardings. |
5110 | */ |
5111 | { |
bc240b21 |
5112 | char type; |
5113 | int n; |
a4fc0d74 |
5114 | int sport,dport,sserv,dserv; |
6ee9b735 |
5115 | char sports[256], dports[256], saddr[256], host[256]; |
bc240b21 |
5116 | |
51470298 |
5117 | ssh->rportfwds = newtree234(ssh_rportcmp_ssh2); |
bc240b21 |
5118 | /* Add port forwardings. */ |
51470298 |
5119 | ssh->portfwd_strptr = cfg.portfwd; |
5120 | while (*ssh->portfwd_strptr) { |
5121 | type = *ssh->portfwd_strptr++; |
6ee9b735 |
5122 | saddr[0] = '\0'; |
bc240b21 |
5123 | n = 0; |
6ee9b735 |
5124 | while (*ssh->portfwd_strptr && *ssh->portfwd_strptr != '\t') { |
5125 | if (*ssh->portfwd_strptr == ':') { |
5126 | /* |
5127 | * We've seen a colon in the middle of the |
5128 | * source port number. This means that |
5129 | * everything we've seen until now is the |
5130 | * source _address_, so we'll move it into |
5131 | * saddr and start sports from the beginning |
5132 | * again. |
5133 | */ |
5134 | ssh->portfwd_strptr++; |
5135 | sports[n] = '\0'; |
5136 | strcpy(saddr, sports); |
5137 | n = 0; |
5138 | } |
5139 | if (n < 255) sports[n++] = *ssh->portfwd_strptr++; |
5140 | } |
bc240b21 |
5141 | sports[n] = 0; |
51470298 |
5142 | if (*ssh->portfwd_strptr == '\t') |
5143 | ssh->portfwd_strptr++; |
bc240b21 |
5144 | n = 0; |
6ee9b735 |
5145 | while (*ssh->portfwd_strptr && *ssh->portfwd_strptr != ':') { |
5146 | if (n < 255) host[n++] = *ssh->portfwd_strptr++; |
5147 | } |
bc240b21 |
5148 | host[n] = 0; |
51470298 |
5149 | if (*ssh->portfwd_strptr == ':') |
5150 | ssh->portfwd_strptr++; |
bc240b21 |
5151 | n = 0; |
6ee9b735 |
5152 | while (*ssh->portfwd_strptr) { |
5153 | if (n < 255) dports[n++] = *ssh->portfwd_strptr++; |
5154 | } |
bc240b21 |
5155 | dports[n] = 0; |
51470298 |
5156 | ssh->portfwd_strptr++; |
bc240b21 |
5157 | dport = atoi(dports); |
a4fc0d74 |
5158 | dserv = 0; |
5159 | if (dport == 0) { |
5160 | dserv = 1; |
68a49acb |
5161 | dport = net_service_lookup(dports); |
5162 | if (!dport) { |
57356d63 |
5163 | logeventf(ssh, "Service lookup failed for destination" |
5164 | " port \"%s\"", dports); |
a4fc0d74 |
5165 | } |
5166 | } |
bc240b21 |
5167 | sport = atoi(sports); |
a4fc0d74 |
5168 | sserv = 0; |
5169 | if (sport == 0) { |
5170 | sserv = 1; |
68a49acb |
5171 | sport = net_service_lookup(sports); |
5172 | if (!sport) { |
57356d63 |
5173 | logeventf(ssh, "Service lookup failed for source" |
5174 | " port \"%s\"", sports); |
a4fc0d74 |
5175 | } |
5176 | } |
bc240b21 |
5177 | if (sport && dport) { |
5178 | if (type == 'L') { |
6ee9b735 |
5179 | pfd_addforward(host, dport, *saddr ? saddr : NULL, |
5180 | sport, ssh); |
5181 | logeventf(ssh, "Local port %.*s%.*s%.*s%.*s%d%.*s" |
5182 | " forwarding to %s:%.*s%.*s%d%.*s", |
5183 | (int)(*saddr?strlen(saddr):0), *saddr?saddr:NULL, |
5184 | (int)(*saddr?1:0), ":", |
57356d63 |
5185 | (int)(sserv ? strlen(sports) : 0), sports, |
5186 | sserv, "(", sport, sserv, ")", |
5187 | host, |
5188 | (int)(dserv ? strlen(dports) : 0), dports, |
5189 | dserv, "(", dport, dserv, ")"); |
bc240b21 |
5190 | } else { |
5191 | struct ssh_rportfwd *pf; |
5192 | pf = smalloc(sizeof(*pf)); |
5193 | strcpy(pf->dhost, host); |
5194 | pf->dport = dport; |
5195 | pf->sport = sport; |
51470298 |
5196 | if (add234(ssh->rportfwds, pf) != pf) { |
57356d63 |
5197 | logeventf(ssh, "Duplicate remote port forwarding" |
5198 | " to %s:%d", host, dport); |
bc240b21 |
5199 | sfree(pf); |
5200 | } else { |
6ee9b735 |
5201 | logeventf(ssh, "Requesting remote port " |
5202 | "%.*s%.*s%.*s%.*s%d%.*s" |
57356d63 |
5203 | " forward to %s:%.*s%.*s%d%.*s", |
6ee9b735 |
5204 | (int)(*saddr?strlen(saddr):0), |
5205 | *saddr?saddr:NULL, |
5206 | (int)(*saddr?1:0), ":", |
57356d63 |
5207 | (int)(sserv ? strlen(sports) : 0), sports, |
5208 | sserv, "(", sport, sserv, ")", |
5209 | host, |
5210 | (int)(dserv ? strlen(dports) : 0), dports, |
5211 | dserv, "(", dport, dserv, ")"); |
51470298 |
5212 | ssh2_pkt_init(ssh, SSH2_MSG_GLOBAL_REQUEST); |
5213 | ssh2_pkt_addstring(ssh, "tcpip-forward"); |
5214 | ssh2_pkt_addbool(ssh, 1);/* want reply */ |
6ee9b735 |
5215 | if (*saddr) |
5216 | ssh2_pkt_addstring(ssh, saddr); |
beefa433 |
5217 | if (cfg.rport_acceptall) |
51470298 |
5218 | ssh2_pkt_addstring(ssh, "0.0.0.0"); |
beefa433 |
5219 | else |
51470298 |
5220 | ssh2_pkt_addstring(ssh, "127.0.0.1"); |
5221 | ssh2_pkt_adduint32(ssh, sport); |
5222 | ssh2_pkt_send(ssh); |
bc240b21 |
5223 | |
5224 | do { |
5225 | crWaitUntilV(ispkt); |
51470298 |
5226 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
5227 | unsigned i = ssh2_pkt_getuint32(ssh); |
bc240b21 |
5228 | struct ssh_channel *c; |
51470298 |
5229 | c = find234(ssh->channels, &i, ssh_channelfind); |
bc240b21 |
5230 | if (!c) |
5231 | continue;/* nonexistent channel */ |
51470298 |
5232 | c->v.v2.remwindow += ssh2_pkt_getuint32(ssh); |
bc240b21 |
5233 | } |
51470298 |
5234 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
bc240b21 |
5235 | |
51470298 |
5236 | if (ssh->pktin.type != SSH2_MSG_REQUEST_SUCCESS) { |
5237 | if (ssh->pktin.type != SSH2_MSG_REQUEST_FAILURE) { |
a8327734 |
5238 | bombout((ssh,"Unexpected response to port " |
c9886e66 |
5239 | "forwarding request: packet type %d", |
51470298 |
5240 | ssh->pktin.type)); |
bc240b21 |
5241 | crReturnV; |
5242 | } |
5243 | logevent("Server refused this port forwarding"); |
5244 | } else { |
5245 | logevent("Remote port forwarding enabled"); |
5246 | } |
5247 | } |
5248 | } |
5249 | } |
5250 | } |
5251 | } |
5252 | |
5253 | /* |
36c2a3e9 |
5254 | * Potentially enable agent forwarding. |
5255 | */ |
5256 | if (cfg.agentfwd && agent_exists()) { |
32874aea |
5257 | logevent("Requesting OpenSSH-style agent forwarding"); |
51470298 |
5258 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
5259 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); |
5260 | ssh2_pkt_addstring(ssh, "auth-agent-req@openssh.com"); |
5261 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
5262 | ssh2_pkt_send(ssh); |
32874aea |
5263 | |
5264 | do { |
5265 | crWaitUntilV(ispkt); |
51470298 |
5266 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
5267 | unsigned i = ssh2_pkt_getuint32(ssh); |
32874aea |
5268 | struct ssh_channel *c; |
51470298 |
5269 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
5270 | if (!c) |
5271 | continue; /* nonexistent channel */ |
51470298 |
5272 | c->v.v2.remwindow += ssh2_pkt_getuint32(ssh); |
32874aea |
5273 | } |
51470298 |
5274 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
32874aea |
5275 | |
51470298 |
5276 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
5277 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
a8327734 |
5278 | bombout((ssh,"Unexpected response to agent forwarding request:" |
51470298 |
5279 | " packet type %d", ssh->pktin.type)); |
32874aea |
5280 | crReturnV; |
5281 | } |
5282 | logevent("Agent forwarding refused"); |
5283 | } else { |
5284 | logevent("Agent forwarding enabled"); |
51470298 |
5285 | ssh->agentfwd_enabled = TRUE; |
32874aea |
5286 | } |
36c2a3e9 |
5287 | } |
5288 | |
5289 | /* |
7cca0d81 |
5290 | * Now allocate a pty for the session. |
5291 | */ |
67779be7 |
5292 | if (!cfg.nopty) { |
51470298 |
5293 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
5294 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); /* recipient channel */ |
5295 | ssh2_pkt_addstring(ssh, "pty-req"); |
5296 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
5297 | ssh2_pkt_addstring(ssh, cfg.termtype); |
5298 | ssh2_pkt_adduint32(ssh, ssh->term_width); |
5299 | ssh2_pkt_adduint32(ssh, ssh->term_height); |
5300 | ssh2_pkt_adduint32(ssh, 0); /* pixel width */ |
5301 | ssh2_pkt_adduint32(ssh, 0); /* pixel height */ |
5302 | ssh2_pkt_addstring_start(ssh); |
5303 | ssh2_pkt_addstring_data(ssh, "\0", 1); /* TTY_OP_END, no special options */ |
5304 | ssh2_pkt_send(ssh); |
5305 | ssh->state = SSH_STATE_INTERMED; |
32874aea |
5306 | |
5307 | do { |
5308 | crWaitUntilV(ispkt); |
51470298 |
5309 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
5310 | unsigned i = ssh2_pkt_getuint32(ssh); |
32874aea |
5311 | struct ssh_channel *c; |
51470298 |
5312 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
5313 | if (!c) |
5314 | continue; /* nonexistent channel */ |
51470298 |
5315 | c->v.v2.remwindow += ssh2_pkt_getuint32(ssh); |
32874aea |
5316 | } |
51470298 |
5317 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
32874aea |
5318 | |
51470298 |
5319 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
5320 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
a8327734 |
5321 | bombout((ssh,"Unexpected response to pty request:" |
51470298 |
5322 | " packet type %d", ssh->pktin.type)); |
32874aea |
5323 | crReturnV; |
5324 | } |
51470298 |
5325 | c_write_str(ssh, "Server refused to allocate pty\r\n"); |
5326 | ssh->editing = ssh->echoing = 1; |
32874aea |
5327 | } else { |
5328 | logevent("Allocated pty"); |
5329 | } |
0965bee0 |
5330 | } else { |
51470298 |
5331 | ssh->editing = ssh->echoing = 1; |
7cca0d81 |
5332 | } |
5333 | |
5334 | /* |
fd5e5847 |
5335 | * Start a shell or a remote command. We may have to attempt |
5336 | * this twice if the config data has provided a second choice |
5337 | * of command. |
7cca0d81 |
5338 | */ |
fd5e5847 |
5339 | while (1) { |
5340 | int subsys; |
5341 | char *cmd; |
5342 | |
51470298 |
5343 | if (ssh->fallback_cmd) { |
fd5e5847 |
5344 | subsys = cfg.ssh_subsys2; |
5345 | cmd = cfg.remote_cmd_ptr2; |
5346 | } else { |
5347 | subsys = cfg.ssh_subsys; |
5348 | cmd = cfg.remote_cmd_ptr; |
5349 | } |
5350 | |
51470298 |
5351 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
5352 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); /* recipient channel */ |
fd5e5847 |
5353 | if (subsys) { |
51470298 |
5354 | ssh2_pkt_addstring(ssh, "subsystem"); |
5355 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
5356 | ssh2_pkt_addstring(ssh, cmd); |
fd5e5847 |
5357 | } else if (*cmd) { |
51470298 |
5358 | ssh2_pkt_addstring(ssh, "exec"); |
5359 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
5360 | ssh2_pkt_addstring(ssh, cmd); |
fd5e5847 |
5361 | } else { |
51470298 |
5362 | ssh2_pkt_addstring(ssh, "shell"); |
5363 | ssh2_pkt_addbool(ssh, 1); /* want reply */ |
32874aea |
5364 | } |
51470298 |
5365 | ssh2_pkt_send(ssh); |
fd5e5847 |
5366 | do { |
5367 | crWaitUntilV(ispkt); |
51470298 |
5368 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
5369 | unsigned i = ssh2_pkt_getuint32(ssh); |
fd5e5847 |
5370 | struct ssh_channel *c; |
51470298 |
5371 | c = find234(ssh->channels, &i, ssh_channelfind); |
fd5e5847 |
5372 | if (!c) |
5373 | continue; /* nonexistent channel */ |
51470298 |
5374 | c->v.v2.remwindow += ssh2_pkt_getuint32(ssh); |
fd5e5847 |
5375 | } |
51470298 |
5376 | } while (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
5377 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_SUCCESS) { |
5378 | if (ssh->pktin.type != SSH2_MSG_CHANNEL_FAILURE) { |
a8327734 |
5379 | bombout((ssh,"Unexpected response to shell/command request:" |
51470298 |
5380 | " packet type %d", ssh->pktin.type)); |
fd5e5847 |
5381 | crReturnV; |
5382 | } |
5383 | /* |
5384 | * We failed to start the command. If this is the |
5385 | * fallback command, we really are finished; if it's |
5386 | * not, and if the fallback command exists, try falling |
5387 | * back to it before complaining. |
5388 | */ |
51470298 |
5389 | if (!ssh->fallback_cmd && cfg.remote_cmd_ptr2 != NULL) { |
fd5e5847 |
5390 | logevent("Primary command failed; attempting fallback"); |
51470298 |
5391 | ssh->fallback_cmd = TRUE; |
fd5e5847 |
5392 | continue; |
5393 | } |
a8327734 |
5394 | bombout((ssh,"Server refused to start a shell/command")); |
32874aea |
5395 | crReturnV; |
fd5e5847 |
5396 | } else { |
5397 | logevent("Started a shell/command"); |
32874aea |
5398 | } |
fd5e5847 |
5399 | break; |
7cca0d81 |
5400 | } |
5401 | |
51470298 |
5402 | ssh->state = SSH_STATE_SESSION; |
5403 | if (ssh->size_needed) |
5404 | ssh_size(ssh, ssh->term_width, ssh->term_height); |
5405 | if (ssh->eof_needed) |
5406 | ssh_special(ssh, TS_EOF); |
6e48c3fe |
5407 | |
7cca0d81 |
5408 | /* |
5409 | * Transfer data! |
5410 | */ |
b9d7bcad |
5411 | if (ssh->ldisc) |
5412 | ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
51470298 |
5413 | ssh->send_ok = 1; |
7cca0d81 |
5414 | while (1) { |
e5574168 |
5415 | crReturnV; |
51470298 |
5416 | s->try_send = FALSE; |
7cca0d81 |
5417 | if (ispkt) { |
51470298 |
5418 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_DATA || |
5419 | ssh->pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA) { |
32874aea |
5420 | char *data; |
5421 | int length; |
51470298 |
5422 | unsigned i = ssh2_pkt_getuint32(ssh); |
32874aea |
5423 | struct ssh_channel *c; |
51470298 |
5424 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
5425 | if (!c) |
5426 | continue; /* nonexistent channel */ |
51470298 |
5427 | if (ssh->pktin.type == SSH2_MSG_CHANNEL_EXTENDED_DATA && |
5428 | ssh2_pkt_getuint32(ssh) != SSH2_EXTENDED_DATA_STDERR) |
32874aea |
5429 | continue; /* extended but not stderr */ |
51470298 |
5430 | ssh2_pkt_getstring(ssh, &data, &length); |
32874aea |
5431 | if (data) { |
5471d09a |
5432 | int bufsize; |
5433 | c->v.v2.locwindow -= length; |
32874aea |
5434 | switch (c->type) { |
5435 | case CHAN_MAINSESSION: |
5471d09a |
5436 | bufsize = |
51470298 |
5437 | from_backend(ssh->frontend, ssh->pktin.type == |
5471d09a |
5438 | SSH2_MSG_CHANNEL_EXTENDED_DATA, |
5439 | data, length); |
32874aea |
5440 | break; |
5441 | case CHAN_X11: |
5471d09a |
5442 | bufsize = x11_send(c->u.x11.s, data, length); |
32874aea |
5443 | break; |
d74d141c |
5444 | case CHAN_SOCKDATA: |
5471d09a |
5445 | bufsize = pfd_send(c->u.pfd.s, data, length); |
bc240b21 |
5446 | break; |
36c2a3e9 |
5447 | case CHAN_AGENT: |
32874aea |
5448 | while (length > 0) { |
5449 | if (c->u.a.lensofar < 4) { |
5450 | int l = min(4 - c->u.a.lensofar, length); |
5451 | memcpy(c->u.a.msglen + c->u.a.lensofar, |
5452 | data, l); |
5453 | data += l; |
5454 | length -= l; |
5455 | c->u.a.lensofar += l; |
5456 | } |
5457 | if (c->u.a.lensofar == 4) { |
5458 | c->u.a.totallen = |
5459 | 4 + GET_32BIT(c->u.a.msglen); |
5460 | c->u.a.message = smalloc(c->u.a.totallen); |
5461 | memcpy(c->u.a.message, c->u.a.msglen, 4); |
5462 | } |
5463 | if (c->u.a.lensofar >= 4 && length > 0) { |
5464 | int l = |
5465 | min(c->u.a.totallen - c->u.a.lensofar, |
5466 | length); |
5467 | memcpy(c->u.a.message + c->u.a.lensofar, |
5468 | data, l); |
5469 | data += l; |
5470 | length -= l; |
5471 | c->u.a.lensofar += l; |
5472 | } |
5473 | if (c->u.a.lensofar == c->u.a.totallen) { |
5474 | void *reply, *sentreply; |
5475 | int replylen; |
5476 | agent_query(c->u.a.message, |
5477 | c->u.a.totallen, &reply, |
5478 | &replylen); |
5479 | if (reply) |
5480 | sentreply = reply; |
5481 | else { |
5482 | /* Fake SSH_AGENT_FAILURE. */ |
5483 | sentreply = "\0\0\0\1\5"; |
5484 | replylen = 5; |
5485 | } |
51470298 |
5486 | ssh2_add_channel_data(c, sentreply, replylen); |
5487 | s->try_send = TRUE; |
32874aea |
5488 | if (reply) |
5489 | sfree(reply); |
5490 | sfree(c->u.a.message); |
5491 | c->u.a.lensofar = 0; |
5492 | } |
5493 | } |
5471d09a |
5494 | bufsize = 0; |
32874aea |
5495 | break; |
5496 | } |
5497 | /* |
5471d09a |
5498 | * If we are not buffering too much data, |
5499 | * enlarge the window again at the remote side. |
32874aea |
5500 | */ |
5471d09a |
5501 | if (bufsize < OUR_V2_WINSIZE) |
5502 | ssh2_set_window(c, OUR_V2_WINSIZE - bufsize); |
32874aea |
5503 | } |
51470298 |
5504 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_EOF) { |
5505 | unsigned i = ssh2_pkt_getuint32(ssh); |
32874aea |
5506 | struct ssh_channel *c; |
5507 | |
51470298 |
5508 | c = find234(ssh->channels, &i, ssh_channelfind); |
32874aea |
5509 | if (!c) |
5510 | continue; /* nonexistent channel */ |
5511 | |
5512 | if (c->type == CHAN_X11) { |
5513 | /* |
5514 | * Remote EOF on an X11 channel means we should |
5515 | * wrap up and close the channel ourselves. |
5516 | */ |
5517 | x11_close(c->u.x11.s); |
5518 | sshfwd_close(c); |
5519 | } else if (c->type == CHAN_AGENT) { |
36c2a3e9 |
5520 | sshfwd_close(c); |
d74d141c |
5521 | } else if (c->type == CHAN_SOCKDATA) { |
bc240b21 |
5522 | pfd_close(c->u.pfd.s); |
5523 | sshfwd_close(c); |
36c2a3e9 |
5524 | } |
51470298 |
5525 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_CLOSE) { |
5526 | unsigned i = ssh2_pkt_getuint32(ssh); |
32874aea |
5527 | struct ssh_channel *c; |
5528 | |
51470298 |
5529 | c = find234(ssh->channels, &i, ssh_channelfind); |
d0d844c1 |
5530 | if (!c || ((int)c->remoteid) == -1) { |
a8327734 |
5531 | bombout((ssh,"Received CHANNEL_CLOSE for %s channel %d\n", |
d0d844c1 |
5532 | c ? "half-open" : "nonexistent", i)); |
5533 | } |
32874aea |
5534 | /* Do pre-close processing on the channel. */ |
5535 | switch (c->type) { |
5536 | case CHAN_MAINSESSION: |
5537 | break; /* nothing to see here, move along */ |
5538 | case CHAN_X11: |
92f157bd |
5539 | if (c->u.x11.s != NULL) |
5540 | x11_close(c->u.x11.s); |
5541 | sshfwd_close(c); |
32874aea |
5542 | break; |
5543 | case CHAN_AGENT: |
92f157bd |
5544 | sshfwd_close(c); |
32874aea |
5545 | break; |
d74d141c |
5546 | case CHAN_SOCKDATA: |
92f157bd |
5547 | if (c->u.pfd.s != NULL) |
5548 | pfd_close(c->u.pfd.s); |
5549 | sshfwd_close(c); |
bc240b21 |
5550 | break; |
32874aea |
5551 | } |
92f157bd |
5552 | if (c->closes == 0) { |
51470298 |
5553 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_CLOSE); |
5554 | ssh2_pkt_adduint32(ssh, c->remoteid); |
5555 | ssh2_pkt_send(ssh); |
92f157bd |
5556 | } |
51470298 |
5557 | del234(ssh->channels, c); |
5471d09a |
5558 | bufchain_clear(&c->v.v2.outbuffer); |
32874aea |
5559 | sfree(c); |
5560 | |
5561 | /* |
5562 | * See if that was the last channel left open. |
5563 | */ |
51470298 |
5564 | if (count234(ssh->channels) == 0) { |
ca2914d1 |
5565 | #if 0 |
5566 | /* |
5567 | * We used to send SSH_MSG_DISCONNECT here, |
5568 | * because I'd believed that _every_ conforming |
5569 | * SSH2 connection had to end with a disconnect |
5570 | * being sent by at least one side; apparently |
5571 | * I was wrong and it's perfectly OK to |
5572 | * unceremoniously slam the connection shut |
5573 | * when you're done, and indeed OpenSSH feels |
5574 | * this is more polite than sending a |
5575 | * DISCONNECT. So now we don't. |
5576 | */ |
32874aea |
5577 | logevent("All channels closed. Disconnecting"); |
51470298 |
5578 | ssh2_pkt_init(ssh, SSH2_MSG_DISCONNECT); |
5579 | ssh2_pkt_adduint32(ssh, SSH2_DISCONNECT_BY_APPLICATION); |
5580 | ssh2_pkt_addstring(ssh, "All open channels closed"); |
5581 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
5582 | ssh2_pkt_send(ssh); |
ca2914d1 |
5583 | #endif |
51470298 |
5584 | ssh->state = SSH_STATE_CLOSED; |
32874aea |
5585 | crReturnV; |
5586 | } |
5587 | continue; /* remote sends close; ignore (FIXME) */ |
51470298 |
5588 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_WINDOW_ADJUST) { |
5589 | unsigned i = ssh2_pkt_getuint32(ssh); |
32874aea |
5590 | struct ssh_channel *c; |
51470298 |
5591 | c = find234(ssh->channels, &i, ssh_channelfind); |
a6253970 |
5592 | if (!c || c->closes) |
5593 | continue; /* nonexistent or closing channel */ |
51470298 |
5594 | c->v.v2.remwindow += ssh2_pkt_getuint32(ssh); |
5595 | s->try_send = TRUE; |
5596 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { |
5597 | unsigned i = ssh2_pkt_getuint32(ssh); |
bc240b21 |
5598 | struct ssh_channel *c; |
51470298 |
5599 | c = find234(ssh->channels, &i, ssh_channelfind); |
bc240b21 |
5600 | if (!c) |
5601 | continue; /* nonexistent channel */ |
5602 | if (c->type != CHAN_SOCKDATA_DORMANT) |
5603 | continue; /* dunno why they're confirming this */ |
51470298 |
5604 | c->remoteid = ssh2_pkt_getuint32(ssh); |
bc240b21 |
5605 | c->type = CHAN_SOCKDATA; |
51470298 |
5606 | c->v.v2.remwindow = ssh2_pkt_getuint32(ssh); |
5607 | c->v.v2.remmaxpkt = ssh2_pkt_getuint32(ssh); |
4ed34d25 |
5608 | if (c->u.pfd.s) |
5609 | pfd_confirm(c->u.pfd.s); |
5610 | if (c->closes) { |
5611 | /* |
5612 | * We have a pending close on this channel, |
5613 | * which we decided on before the server acked |
5614 | * the channel open. So now we know the |
5615 | * remoteid, we can close it again. |
5616 | */ |
51470298 |
5617 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_CLOSE); |
5618 | ssh2_pkt_adduint32(ssh, c->remoteid); |
5619 | ssh2_pkt_send(ssh); |
4ed34d25 |
5620 | } |
51470298 |
5621 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_OPEN_FAILURE) { |
5622 | unsigned i = ssh2_pkt_getuint32(ssh); |
724cface |
5623 | struct ssh_channel *c; |
51470298 |
5624 | c = find234(ssh->channels, &i, ssh_channelfind); |
724cface |
5625 | if (!c) |
5626 | continue; /* nonexistent channel */ |
5627 | if (c->type != CHAN_SOCKDATA_DORMANT) |
5628 | continue; /* dunno why they're failing this */ |
5629 | |
5630 | logevent("Forwarded connection refused by server"); |
5631 | |
5632 | pfd_close(c->u.pfd.s); |
5633 | |
51470298 |
5634 | del234(ssh->channels, c); |
724cface |
5635 | sfree(c); |
51470298 |
5636 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_REQUEST) { |
ebb0a590 |
5637 | unsigned localid; |
5638 | char *type; |
5639 | int typelen, want_reply; |
5640 | struct ssh_channel *c; |
5641 | |
51470298 |
5642 | localid = ssh2_pkt_getuint32(ssh); |
5643 | ssh2_pkt_getstring(ssh, &type, &typelen); |
5644 | want_reply = ssh2_pkt_getbool(ssh); |
ebb0a590 |
5645 | |
5646 | /* |
5647 | * First, check that the channel exists. Otherwise, |
5648 | * we can instantly disconnect with a rude message. |
5649 | */ |
51470298 |
5650 | c = find234(ssh->channels, &localid, ssh_channelfind); |
ebb0a590 |
5651 | if (!c) { |
5652 | char buf[80]; |
5653 | sprintf(buf, "Received channel request for nonexistent" |
5654 | " channel %d", localid); |
5655 | logevent(buf); |
51470298 |
5656 | ssh2_pkt_init(ssh, SSH2_MSG_DISCONNECT); |
5657 | ssh2_pkt_adduint32(ssh, SSH2_DISCONNECT_BY_APPLICATION); |
5658 | ssh2_pkt_addstring(ssh, buf); |
5659 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
5660 | ssh2_pkt_send(ssh); |
247308b5 |
5661 | connection_fatal("%s", buf); |
51470298 |
5662 | ssh->state = SSH_STATE_CLOSED; |
ebb0a590 |
5663 | crReturnV; |
5664 | } |
5665 | |
5666 | /* |
d8d6c7e5 |
5667 | * Having got the channel number, we now look at |
5668 | * the request type string to see if it's something |
5669 | * we recognise. |
ebb0a590 |
5670 | */ |
d8d6c7e5 |
5671 | if (typelen == 11 && !memcmp(type, "exit-status", 11) && |
51470298 |
5672 | c == ssh->mainchan) { |
d8d6c7e5 |
5673 | /* We recognise "exit-status" on the primary channel. */ |
5674 | char buf[100]; |
51470298 |
5675 | ssh->exitcode = ssh2_pkt_getuint32(ssh); |
d8d6c7e5 |
5676 | sprintf(buf, "Server sent command exit status %d", |
51470298 |
5677 | ssh->exitcode); |
d8d6c7e5 |
5678 | logevent(buf); |
5679 | if (want_reply) { |
51470298 |
5680 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_SUCCESS); |
5681 | ssh2_pkt_adduint32(ssh, c->remoteid); |
5682 | ssh2_pkt_send(ssh); |
d8d6c7e5 |
5683 | } |
5684 | } else { |
5685 | /* |
5686 | * This is a channel request we don't know |
5687 | * about, so we now either ignore the request |
5688 | * or respond with CHANNEL_FAILURE, depending |
5689 | * on want_reply. |
5690 | */ |
5691 | if (want_reply) { |
51470298 |
5692 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_FAILURE); |
5693 | ssh2_pkt_adduint32(ssh, c->remoteid); |
5694 | ssh2_pkt_send(ssh); |
d8d6c7e5 |
5695 | } |
ebb0a590 |
5696 | } |
51470298 |
5697 | } else if (ssh->pktin.type == SSH2_MSG_GLOBAL_REQUEST) { |
697d4856 |
5698 | char *type; |
5699 | int typelen, want_reply; |
5700 | |
51470298 |
5701 | ssh2_pkt_getstring(ssh, &type, &typelen); |
5702 | want_reply = ssh2_pkt_getbool(ssh); |
697d4856 |
5703 | |
5704 | /* |
5705 | * We currently don't support any global requests |
5706 | * at all, so we either ignore the request or |
5707 | * respond with REQUEST_FAILURE, depending on |
5708 | * want_reply. |
5709 | */ |
5710 | if (want_reply) { |
51470298 |
5711 | ssh2_pkt_init(ssh, SSH2_MSG_REQUEST_FAILURE); |
5712 | ssh2_pkt_send(ssh); |
697d4856 |
5713 | } |
51470298 |
5714 | } else if (ssh->pktin.type == SSH2_MSG_CHANNEL_OPEN) { |
32874aea |
5715 | char *type; |
5716 | int typelen; |
5717 | char *error = NULL; |
5718 | struct ssh_channel *c; |
bc240b21 |
5719 | unsigned remid, winsize, pktsize; |
51470298 |
5720 | ssh2_pkt_getstring(ssh, &type, &typelen); |
32874aea |
5721 | c = smalloc(sizeof(struct ssh_channel)); |
51470298 |
5722 | c->ssh = ssh; |
32874aea |
5723 | |
51470298 |
5724 | remid = ssh2_pkt_getuint32(ssh); |
5725 | winsize = ssh2_pkt_getuint32(ssh); |
5726 | pktsize = ssh2_pkt_getuint32(ssh); |
bc240b21 |
5727 | |
32874aea |
5728 | if (typelen == 3 && !memcmp(type, "x11", 3)) { |
51470298 |
5729 | if (!ssh->X11_fwd_enabled) |
32874aea |
5730 | error = "X11 forwarding is not enabled"; |
302121de |
5731 | else if (x11_init(&c->u.x11.s, cfg.x11_display, c, |
5732 | ssh->x11auth) != NULL) { |
32874aea |
5733 | error = "Unable to open an X11 connection"; |
5734 | } else { |
5735 | c->type = CHAN_X11; |
5736 | } |
bc240b21 |
5737 | } else if (typelen == 15 && |
5738 | !memcmp(type, "forwarded-tcpip", 15)) { |
5739 | struct ssh_rportfwd pf, *realpf; |
5740 | char *dummy; |
5741 | int dummylen; |
51470298 |
5742 | ssh2_pkt_getstring(ssh, &dummy, &dummylen);/* skip address */ |
5743 | pf.sport = ssh2_pkt_getuint32(ssh); |
5744 | realpf = find234(ssh->rportfwds, &pf, NULL); |
bc240b21 |
5745 | if (realpf == NULL) { |
5746 | error = "Remote port is not recognised"; |
5747 | } else { |
5748 | char *e = pfd_newconnect(&c->u.pfd.s, realpf->dhost, |
5749 | realpf->dport, c); |
57356d63 |
5750 | logeventf(ssh, "Received remote port open request" |
5751 | " for %s:%d", realpf->dhost, realpf->dport); |
bc240b21 |
5752 | if (e != NULL) { |
57356d63 |
5753 | logeventf(ssh, "Port open failed: %s", e); |
bc240b21 |
5754 | error = "Port open failed"; |
5755 | } else { |
5756 | logevent("Forwarded port opened successfully"); |
5757 | c->type = CHAN_SOCKDATA; |
5758 | } |
5759 | } |
32874aea |
5760 | } else if (typelen == 22 && |
36c2a3e9 |
5761 | !memcmp(type, "auth-agent@openssh.com", 3)) { |
51470298 |
5762 | if (!ssh->agentfwd_enabled) |
32874aea |
5763 | error = "Agent forwarding is not enabled"; |
36c2a3e9 |
5764 | else { |
32874aea |
5765 | c->type = CHAN_AGENT; /* identify channel type */ |
36c2a3e9 |
5766 | c->u.a.lensofar = 0; |
32874aea |
5767 | } |
5768 | } else { |
5769 | error = "Unsupported channel type requested"; |
5770 | } |
5771 | |
bc240b21 |
5772 | c->remoteid = remid; |
32874aea |
5773 | if (error) { |
51470298 |
5774 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE); |
5775 | ssh2_pkt_adduint32(ssh, c->remoteid); |
5776 | ssh2_pkt_adduint32(ssh, SSH2_OPEN_CONNECT_FAILED); |
5777 | ssh2_pkt_addstring(ssh, error); |
5778 | ssh2_pkt_addstring(ssh, "en"); /* language tag */ |
5779 | ssh2_pkt_send(ssh); |
32874aea |
5780 | sfree(c); |
5781 | } else { |
51470298 |
5782 | c->localid = alloc_channel_id(ssh); |
32874aea |
5783 | c->closes = 0; |
5471d09a |
5784 | c->v.v2.locwindow = OUR_V2_WINSIZE; |
5785 | c->v.v2.remwindow = winsize; |
5786 | c->v.v2.remmaxpkt = pktsize; |
5787 | bufchain_init(&c->v.v2.outbuffer); |
51470298 |
5788 | add234(ssh->channels, c); |
5789 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
5790 | ssh2_pkt_adduint32(ssh, c->remoteid); |
5791 | ssh2_pkt_adduint32(ssh, c->localid); |
5792 | ssh2_pkt_adduint32(ssh, c->v.v2.locwindow); |
5793 | ssh2_pkt_adduint32(ssh, 0x4000UL); /* our max pkt size */ |
5794 | ssh2_pkt_send(ssh); |
32874aea |
5795 | } |
7cca0d81 |
5796 | } else { |
a8327734 |
5797 | bombout((ssh,"Strange packet received: type %d", ssh->pktin.type)); |
32874aea |
5798 | crReturnV; |
7cca0d81 |
5799 | } |
5800 | } else { |
32874aea |
5801 | /* |
5802 | * We have spare data. Add it to the channel buffer. |
5803 | */ |
d8baa528 |
5804 | ssh2_add_channel_data(ssh->mainchan, (char *)in, inlen); |
51470298 |
5805 | s->try_send = TRUE; |
32874aea |
5806 | } |
51470298 |
5807 | if (s->try_send) { |
32874aea |
5808 | int i; |
5809 | struct ssh_channel *c; |
5810 | /* |
5811 | * Try to send data on all channels if we can. |
5812 | */ |
51470298 |
5813 | for (i = 0; NULL != (c = index234(ssh->channels, i)); i++) { |
a6253970 |
5814 | int bufsize; |
5815 | if (c->closes) |
5816 | continue; /* don't send on closing channels */ |
5817 | bufsize = ssh2_try_send(c); |
5471d09a |
5818 | if (bufsize == 0) { |
5819 | switch (c->type) { |
5820 | case CHAN_MAINSESSION: |
5821 | /* stdin need not receive an unthrottle |
5822 | * notification since it will be polled */ |
5823 | break; |
5824 | case CHAN_X11: |
5825 | x11_unthrottle(c->u.x11.s); |
5826 | break; |
5827 | case CHAN_AGENT: |
5828 | /* agent sockets are request/response and need no |
5829 | * buffer management */ |
5830 | break; |
5831 | case CHAN_SOCKDATA: |
5832 | pfd_unthrottle(c->u.pfd.s); |
5833 | break; |
5834 | } |
5835 | } |
5836 | } |
7cca0d81 |
5837 | } |
e5574168 |
5838 | } |
5839 | |
5840 | crFinishV; |
5841 | } |
5842 | |
5843 | /* |
7cca0d81 |
5844 | * Handle the top-level SSH2 protocol. |
5845 | */ |
51470298 |
5846 | static void ssh2_protocol(Ssh ssh, unsigned char *in, int inlen, int ispkt) |
7cca0d81 |
5847 | { |
51470298 |
5848 | if (do_ssh2_transport(ssh, in, inlen, ispkt) == 0) |
32874aea |
5849 | return; |
51470298 |
5850 | do_ssh2_authconn(ssh, in, inlen, ispkt); |
7cca0d81 |
5851 | } |
5852 | |
5853 | /* |
8df7a775 |
5854 | * Called to set up the connection. |
374330e2 |
5855 | * |
5856 | * Returns an error message, or NULL on success. |
374330e2 |
5857 | */ |
51470298 |
5858 | static char *ssh_init(void *frontend_handle, void **backend_handle, |
887035a5 |
5859 | char *host, int port, char **realhost, int nodelay) |
32874aea |
5860 | { |
fb09bf1c |
5861 | char *p; |
51470298 |
5862 | Ssh ssh; |
5863 | |
5864 | ssh = smalloc(sizeof(*ssh)); |
5865 | ssh->s = NULL; |
5866 | ssh->cipher = NULL; |
371e569c |
5867 | ssh->v1_cipher_ctx = NULL; |
0183b242 |
5868 | ssh->crcda_ctx = NULL; |
51470298 |
5869 | ssh->cscipher = NULL; |
371e569c |
5870 | ssh->cs_cipher_ctx = NULL; |
51470298 |
5871 | ssh->sccipher = NULL; |
371e569c |
5872 | ssh->sc_cipher_ctx = NULL; |
51470298 |
5873 | ssh->csmac = NULL; |
a8327734 |
5874 | ssh->cs_mac_ctx = NULL; |
51470298 |
5875 | ssh->scmac = NULL; |
e0e1a00d |
5876 | ssh->sc_mac_ctx = NULL; |
51470298 |
5877 | ssh->cscomp = NULL; |
5366aed8 |
5878 | ssh->cs_comp_ctx = NULL; |
51470298 |
5879 | ssh->sccomp = NULL; |
5366aed8 |
5880 | ssh->sc_comp_ctx = NULL; |
51470298 |
5881 | ssh->kex = NULL; |
5882 | ssh->hostkey = NULL; |
5883 | ssh->exitcode = -1; |
5884 | ssh->state = SSH_STATE_PREPACKET; |
5885 | ssh->size_needed = FALSE; |
5886 | ssh->eof_needed = FALSE; |
b9d7bcad |
5887 | ssh->ldisc = NULL; |
a8327734 |
5888 | ssh->logctx = NULL; |
51470298 |
5889 | { |
5890 | static const struct Packet empty = { 0, 0, NULL, NULL, 0 }; |
5891 | ssh->pktin = ssh->pktout = empty; |
5892 | } |
5893 | ssh->deferred_send_data = NULL; |
5894 | ssh->deferred_len = 0; |
5895 | ssh->deferred_size = 0; |
5896 | ssh->fallback_cmd = 0; |
5897 | ssh->pkt_ctx = 0; |
302121de |
5898 | ssh->x11auth = NULL; |
be738459 |
5899 | ssh->v1_compressing = FALSE; |
51470298 |
5900 | ssh->v2_outgoing_sequence = 0; |
5901 | ssh->ssh1_rdpkt_crstate = 0; |
5902 | ssh->ssh2_rdpkt_crstate = 0; |
5903 | ssh->do_ssh_init_crstate = 0; |
5904 | ssh->ssh_gotdata_crstate = 0; |
5905 | ssh->ssh1_protocol_crstate = 0; |
5906 | ssh->do_ssh1_login_crstate = 0; |
5907 | ssh->do_ssh2_transport_crstate = 0; |
5908 | ssh->do_ssh2_authconn_crstate = 0; |
5909 | ssh->do_ssh_init_state = NULL; |
5910 | ssh->do_ssh1_login_state = NULL; |
5911 | ssh->do_ssh2_transport_state = NULL; |
5912 | ssh->do_ssh2_authconn_state = NULL; |
6571dbfd |
5913 | ssh->mainchan = NULL; |
968d2d92 |
5914 | ssh->throttled_all = 0; |
5915 | ssh->v1_stdout_throttling = 0; |
51470298 |
5916 | |
5917 | *backend_handle = ssh; |
32874aea |
5918 | |
8f203108 |
5919 | #ifdef MSCRYPTOAPI |
32874aea |
5920 | if (crypto_startup() == 0) |
8f203108 |
5921 | return "Microsoft high encryption pack not installed!"; |
5922 | #endif |
374330e2 |
5923 | |
51470298 |
5924 | ssh->frontend = frontend_handle; |
5925 | ssh->term_width = cfg.width; |
5926 | ssh->term_height = cfg.height; |
887035a5 |
5927 | |
51470298 |
5928 | ssh->send_ok = 0; |
5929 | ssh->editing = 0; |
5930 | ssh->echoing = 0; |
5931 | ssh->v1_throttle_count = 0; |
5932 | ssh->overall_bufsize = 0; |
5933 | ssh->fallback_cmd = 0; |
8df7a775 |
5934 | |
3648d4c5 |
5935 | ssh->protocol = NULL; |
5936 | |
51470298 |
5937 | p = connect_to_host(ssh, host, port, realhost, nodelay); |
fb09bf1c |
5938 | if (p != NULL) |
5939 | return p; |
374330e2 |
5940 | |
374330e2 |
5941 | return NULL; |
5942 | } |
5943 | |
5944 | /* |
374330e2 |
5945 | * Called to send data down the Telnet connection. |
5946 | */ |
51470298 |
5947 | static int ssh_send(void *handle, char *buf, int len) |
32874aea |
5948 | { |
51470298 |
5949 | Ssh ssh = (Ssh) handle; |
5950 | |
5951 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
5471d09a |
5952 | return 0; |
374330e2 |
5953 | |
d8baa528 |
5954 | ssh->protocol(ssh, (unsigned char *)buf, len, 0); |
5471d09a |
5955 | |
51470298 |
5956 | return ssh_sendbuffer(ssh); |
5471d09a |
5957 | } |
5958 | |
5959 | /* |
5960 | * Called to query the current amount of buffered stdin data. |
5961 | */ |
51470298 |
5962 | static int ssh_sendbuffer(void *handle) |
5471d09a |
5963 | { |
51470298 |
5964 | Ssh ssh = (Ssh) handle; |
5471d09a |
5965 | int override_value; |
5966 | |
51470298 |
5967 | if (ssh == NULL || ssh->s == NULL || ssh->protocol == NULL) |
5471d09a |
5968 | return 0; |
5969 | |
5970 | /* |
5971 | * If the SSH socket itself has backed up, add the total backup |
5972 | * size on that to any individual buffer on the stdin channel. |
5973 | */ |
5974 | override_value = 0; |
51470298 |
5975 | if (ssh->throttled_all) |
5976 | override_value = ssh->overall_bufsize; |
5471d09a |
5977 | |
51470298 |
5978 | if (ssh->version == 1) { |
5471d09a |
5979 | return override_value; |
51470298 |
5980 | } else if (ssh->version == 2) { |
5981 | if (!ssh->mainchan || ssh->mainchan->closes > 0) |
5471d09a |
5982 | return override_value; |
5983 | else |
51470298 |
5984 | return (override_value + |
5985 | bufchain_size(&ssh->mainchan->v.v2.outbuffer)); |
5471d09a |
5986 | } |
5987 | |
5988 | return 0; |
374330e2 |
5989 | } |
5990 | |
5991 | /* |
6e48c3fe |
5992 | * Called to set the size of the window from SSH's POV. |
374330e2 |
5993 | */ |
51470298 |
5994 | static void ssh_size(void *handle, int width, int height) |
32874aea |
5995 | { |
51470298 |
5996 | Ssh ssh = (Ssh) handle; |
5997 | |
5998 | ssh->term_width = width; |
5999 | ssh->term_height = height; |
f278d6f8 |
6000 | |
51470298 |
6001 | switch (ssh->state) { |
374330e2 |
6002 | case SSH_STATE_BEFORE_SIZE: |
3687d221 |
6003 | case SSH_STATE_PREPACKET: |
21248260 |
6004 | case SSH_STATE_CLOSED: |
374330e2 |
6005 | break; /* do nothing */ |
6006 | case SSH_STATE_INTERMED: |
51470298 |
6007 | ssh->size_needed = TRUE; /* buffer for later */ |
374330e2 |
6008 | break; |
6009 | case SSH_STATE_SESSION: |
32874aea |
6010 | if (!cfg.nopty) { |
51470298 |
6011 | if (ssh->version == 1) { |
6012 | send_packet(ssh, SSH1_CMSG_WINDOW_SIZE, |
6013 | PKT_INT, ssh->term_height, |
6014 | PKT_INT, ssh->term_width, |
32874aea |
6015 | PKT_INT, 0, PKT_INT, 0, PKT_END); |
6016 | } else { |
51470298 |
6017 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_REQUEST); |
6018 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); |
6019 | ssh2_pkt_addstring(ssh, "window-change"); |
6020 | ssh2_pkt_addbool(ssh, 0); |
6021 | ssh2_pkt_adduint32(ssh, ssh->term_width); |
6022 | ssh2_pkt_adduint32(ssh, ssh->term_height); |
6023 | ssh2_pkt_adduint32(ssh, 0); |
6024 | ssh2_pkt_adduint32(ssh, 0); |
6025 | ssh2_pkt_send(ssh); |
32874aea |
6026 | } |
6027 | } |
6028 | break; |
374330e2 |
6029 | } |
6030 | } |
6031 | |
6032 | /* |
6abbf9e3 |
6033 | * Send Telnet special codes. TS_EOF is useful for `plink', so you |
6034 | * can send an EOF and collect resulting output (e.g. `plink |
6035 | * hostname sort'). |
374330e2 |
6036 | */ |
51470298 |
6037 | static void ssh_special(void *handle, Telnet_Special code) |
32874aea |
6038 | { |
51470298 |
6039 | Ssh ssh = (Ssh) handle; |
6040 | |
6abbf9e3 |
6041 | if (code == TS_EOF) { |
51470298 |
6042 | if (ssh->state != SSH_STATE_SESSION) { |
32874aea |
6043 | /* |
6044 | * Buffer the EOF in case we are pre-SESSION, so we can |
6045 | * send it as soon as we reach SESSION. |
6046 | */ |
6047 | if (code == TS_EOF) |
51470298 |
6048 | ssh->eof_needed = TRUE; |
32874aea |
6049 | return; |
6050 | } |
51470298 |
6051 | if (ssh->version == 1) { |
6052 | send_packet(ssh, SSH1_CMSG_EOF, PKT_END); |
32874aea |
6053 | } else { |
51470298 |
6054 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_EOF); |
6055 | ssh2_pkt_adduint32(ssh, ssh->mainchan->remoteid); |
6056 | ssh2_pkt_send(ssh); |
32874aea |
6057 | } |
6058 | logevent("Sent EOF message"); |
ec55b220 |
6059 | } else if (code == TS_PING) { |
51470298 |
6060 | if (ssh->state == SSH_STATE_CLOSED |
6061 | || ssh->state == SSH_STATE_PREPACKET) return; |
6062 | if (ssh->version == 1) { |
6063 | if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE)) |
6064 | send_packet(ssh, SSH1_MSG_IGNORE, PKT_STR, "", PKT_END); |
32874aea |
6065 | } else { |
51470298 |
6066 | ssh2_pkt_init(ssh, SSH2_MSG_IGNORE); |
6067 | ssh2_pkt_addstring_start(ssh); |
6068 | ssh2_pkt_send(ssh); |
32874aea |
6069 | } |
6abbf9e3 |
6070 | } else { |
32874aea |
6071 | /* do nothing */ |
6abbf9e3 |
6072 | } |
374330e2 |
6073 | } |
6074 | |
51470298 |
6075 | void *new_sock_channel(void *handle, Socket s) |
d74d141c |
6076 | { |
51470298 |
6077 | Ssh ssh = (Ssh) handle; |
d74d141c |
6078 | struct ssh_channel *c; |
6079 | c = smalloc(sizeof(struct ssh_channel)); |
51470298 |
6080 | c->ssh = ssh; |
d74d141c |
6081 | |
6082 | if (c) { |
bc240b21 |
6083 | c->remoteid = -1; /* to be set when open confirmed */ |
51470298 |
6084 | c->localid = alloc_channel_id(ssh); |
d74d141c |
6085 | c->closes = 0; |
bc240b21 |
6086 | c->type = CHAN_SOCKDATA_DORMANT;/* identify channel type */ |
d74d141c |
6087 | c->u.pfd.s = s; |
013dd8c0 |
6088 | bufchain_init(&c->v.v2.outbuffer); |
51470298 |
6089 | add234(ssh->channels, c); |
d74d141c |
6090 | } |
6091 | return c; |
6092 | } |
6093 | |
5471d09a |
6094 | /* |
6095 | * This is called when stdout/stderr (the entity to which |
6096 | * from_backend sends data) manages to clear some backlog. |
6097 | */ |
ae9ae89f |
6098 | static void ssh_unthrottle(void *handle, int bufsize) |
5471d09a |
6099 | { |
51470298 |
6100 | Ssh ssh = (Ssh) handle; |
6101 | if (ssh->version == 1) { |
6102 | if (ssh->v1_stdout_throttling && bufsize < SSH1_BUFFER_LIMIT) { |
6103 | ssh->v1_stdout_throttling = 0; |
6104 | ssh1_throttle(ssh, -1); |
5471d09a |
6105 | } |
6106 | } else { |
51470298 |
6107 | if (ssh->mainchan && ssh->mainchan->closes == 0) |
6108 | ssh2_set_window(ssh->mainchan, OUR_V2_WINSIZE - bufsize); |
5471d09a |
6109 | } |
6110 | } |
6111 | |
6b78788a |
6112 | void ssh_send_port_open(void *channel, char *hostname, int port, char *org) |
d74d141c |
6113 | { |
6114 | struct ssh_channel *c = (struct ssh_channel *)channel; |
6b78788a |
6115 | Ssh ssh = c->ssh; |
d74d141c |
6116 | |
57356d63 |
6117 | logeventf(ssh, "Opening forwarded connection to %s:%d", hostname, port); |
d74d141c |
6118 | |
51470298 |
6119 | if (ssh->version == 1) { |
6120 | send_packet(ssh, SSH1_MSG_PORT_OPEN, |
bc240b21 |
6121 | PKT_INT, c->localid, |
6122 | PKT_STR, hostname, |
6123 | PKT_INT, port, |
6124 | //PKT_STR, <org:orgport>, |
6125 | PKT_END); |
6126 | } else { |
51470298 |
6127 | ssh2_pkt_init(ssh, SSH2_MSG_CHANNEL_OPEN); |
6128 | ssh2_pkt_addstring(ssh, "direct-tcpip"); |
6129 | ssh2_pkt_adduint32(ssh, c->localid); |
5471d09a |
6130 | c->v.v2.locwindow = OUR_V2_WINSIZE; |
51470298 |
6131 | ssh2_pkt_adduint32(ssh, c->v.v2.locwindow);/* our window size */ |
6132 | ssh2_pkt_adduint32(ssh, 0x4000UL); /* our max pkt size */ |
6133 | ssh2_pkt_addstring(ssh, hostname); |
6134 | ssh2_pkt_adduint32(ssh, port); |
bc240b21 |
6135 | /* |
6136 | * We make up values for the originator data; partly it's |
6137 | * too much hassle to keep track, and partly I'm not |
6138 | * convinced the server should be told details like that |
6139 | * about my local network configuration. |
6140 | */ |
51470298 |
6141 | ssh2_pkt_addstring(ssh, "client-side-connection"); |
6142 | ssh2_pkt_adduint32(ssh, 0); |
6143 | ssh2_pkt_send(ssh); |
bc240b21 |
6144 | } |
d74d141c |
6145 | } |
6146 | |
6147 | |
51470298 |
6148 | static Socket ssh_socket(void *handle) |
32874aea |
6149 | { |
51470298 |
6150 | Ssh ssh = (Ssh) handle; |
6151 | return ssh->s; |
32874aea |
6152 | } |
8ccc75b0 |
6153 | |
51470298 |
6154 | static int ssh_sendok(void *handle) |
32874aea |
6155 | { |
51470298 |
6156 | Ssh ssh = (Ssh) handle; |
6157 | return ssh->send_ok; |
32874aea |
6158 | } |
fb09bf1c |
6159 | |
51470298 |
6160 | static int ssh_ldisc(void *handle, int option) |
32874aea |
6161 | { |
51470298 |
6162 | Ssh ssh = (Ssh) handle; |
32874aea |
6163 | if (option == LD_ECHO) |
51470298 |
6164 | return ssh->echoing; |
32874aea |
6165 | if (option == LD_EDIT) |
51470298 |
6166 | return ssh->editing; |
0965bee0 |
6167 | return FALSE; |
6168 | } |
6169 | |
b9d7bcad |
6170 | static void ssh_provide_ldisc(void *handle, void *ldisc) |
6171 | { |
6172 | Ssh ssh = (Ssh) handle; |
6173 | ssh->ldisc = ldisc; |
6174 | } |
6175 | |
a8327734 |
6176 | static void ssh_provide_logctx(void *handle, void *logctx) |
6177 | { |
6178 | Ssh ssh = (Ssh) handle; |
6179 | ssh->logctx = logctx; |
6180 | } |
6181 | |
51470298 |
6182 | static int ssh_return_exitcode(void *handle) |
6183 | { |
6184 | Ssh ssh = (Ssh) handle; |
6185 | return ssh->exitcode; |
6186 | } |
6187 | |
6188 | /* |
6189 | * Gross hack: pscp will try to start SFTP but fall back to scp1 if |
6190 | * that fails. This variable is the means by which scp.c can reach |
6191 | * into the SSH code and find out which one it got. |
6192 | */ |
6193 | extern int ssh_fallback_cmd(void *handle) |
d8d6c7e5 |
6194 | { |
51470298 |
6195 | Ssh ssh = (Ssh) handle; |
6196 | return ssh->fallback_cmd; |
d8d6c7e5 |
6197 | } |
6198 | |
374330e2 |
6199 | Backend ssh_backend = { |
6200 | ssh_init, |
374330e2 |
6201 | ssh_send, |
5471d09a |
6202 | ssh_sendbuffer, |
374330e2 |
6203 | ssh_size, |
4017be6d |
6204 | ssh_special, |
8ccc75b0 |
6205 | ssh_socket, |
d8d6c7e5 |
6206 | ssh_return_exitcode, |
97db3be4 |
6207 | ssh_sendok, |
0965bee0 |
6208 | ssh_ldisc, |
b9d7bcad |
6209 | ssh_provide_ldisc, |
a8327734 |
6210 | ssh_provide_logctx, |
5471d09a |
6211 | ssh_unthrottle, |
97db3be4 |
6212 | 22 |
bc240b21 |
6213 | }; |