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