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