16 #define SSH_MSG_DISCONNECT 1
17 #define SSH_SMSG_PUBLIC_KEY 2
18 #define SSH_CMSG_SESSION_KEY 3
19 #define SSH_CMSG_USER 4
20 #define SSH_CMSG_AUTH_PASSWORD 9
21 #define SSH_CMSG_REQUEST_PTY 10
22 #define SSH_CMSG_EXEC_SHELL 12
23 #define SSH_CMSG_STDIN_DATA 16
24 #define SSH_SMSG_STDOUT_DATA 17
25 #define SSH_SMSG_STDERR_DATA 18
26 #define SSH_SMSG_SUCCESS 14
27 #define SSH_SMSG_FAILURE 15
28 #define SSH_SMSG_EXITSTATUS 20
29 #define SSH_MSG_IGNORE 32
30 #define SSH_CMSG_EXIT_CONFIRMATION 33
31 #define SSH_MSG_DEBUG 36
33 /* Coroutine mechanics for the sillier bits of the code */
34 #define crBegin1 static int crLine = 0;
35 #define crBegin2 switch(crLine) { case 0:;
36 #define crBegin crBegin1; crBegin2;
37 #define crFinish(z) } crLine = 0; return (z)
38 #define crFinishV } crLine = 0; return
41 crLine=__LINE__; return (z); case __LINE__:;\
45 crLine=__LINE__; return; case __LINE__:;\
47 #define crStop(z) do{ crLine = 0; return (z); }while(0)
48 #define crStopV do{ crLine = 0; return; }while(0)
57 static SOCKET s = INVALID_SOCKET;
59 static unsigned char session_key[32];
60 static struct ssh_cipher *cipher = NULL;
62 static char *savedhost;
65 SSH_STATE_BEFORE_SIZE,
69 } ssh_state = SSH_STATE_BEFORE_SIZE;
71 static int size_needed = FALSE;
73 static void s_write (char *buf, int len) {
75 int i = send (s, buf, len, 0);
81 static int s_read (char *buf, int len) {
84 int i = recv (s, buf, len, 0);
86 len -= i, buf += i, ret += i;
93 static void c_write (char *buf, int len) {
95 int new_head = (inbuf_head + 1) & INBUF_MASK;
96 if (new_head != inbuf_reap) {
97 inbuf[inbuf_head] = *buf++;
98 inbuf_head = new_head;
112 static struct Packet pktin = { 0, 0, 0, NULL, 0 };
113 static struct Packet pktout = { 0, 0, 0, NULL, 0 };
115 static void ssh_protocol(unsigned char *in, int inlen, int ispkt);
116 static void ssh_size(void);
118 static void ssh_gotdata(unsigned char *data, int datalen) {
119 static long len, biglen, to_read;
120 static unsigned char *p;
125 for (i = len = 0; i < 4; i++) {
128 len = (len << 8) + *data;
133 if (len == 0x52656d6f) { /* "Remo"te server has closed ... */
134 len = 0x300; /* big enough to carry to end */
142 len -= 5; /* type and CRC */
145 if (pktin.maxlen < biglen) {
146 pktin.maxlen = biglen;
147 pktin.data = (pktin.data == NULL ? malloc(biglen) :
148 realloc(pktin.data, biglen));
150 fatalbox("Out of memory");
153 p = pktin.data, to_read = biglen;
154 while (to_read > 0) {
161 memcpy(p, data, chunk);
169 cipher->decrypt(pktin.data, biglen);
171 pktin.type = pktin.data[pad];
172 pktin.body = pktin.data+pad+1;
174 if (pktin.type == SSH_MSG_DEBUG) {
176 } else if (pktin.type == SSH_MSG_IGNORE) {
179 ssh_protocol(NULL, 0, 1);
184 static void s_wrpkt_start(int type, int len) {
187 len += 5; /* type and CRC */
191 pktout.length = len-5;
192 if (pktout.maxlen < biglen) {
193 pktout.maxlen = biglen;
194 pktout.data = (pktout.data == NULL ? malloc(biglen+4) :
195 realloc(pktout.data, biglen+4));
197 fatalbox("Out of memory");
201 pktout.body = pktout.data+4+pad+1;
204 static void s_wrpkt(void) {
205 int pad, len, biglen, i;
208 len = pktout.length + 5; /* type and CRC */
212 pktout.body[-1] = pktout.type;
213 for (i=0; i<pad; i++)
214 pktout.data[i+4] = random_byte();
215 crc = crc32(pktout.data+4, biglen-4);
217 pktout.data[biglen+0] = (unsigned char) ((crc >> 24) & 0xFF);
218 pktout.data[biglen+1] = (unsigned char) ((crc >> 16) & 0xFF);
219 pktout.data[biglen+2] = (unsigned char) ((crc >> 8) & 0xFF);
220 pktout.data[biglen+3] = (unsigned char) (crc & 0xFF);
222 pktout.data[0] = (len >> 24) & 0xFF;
223 pktout.data[1] = (len >> 16) & 0xFF;
224 pktout.data[2] = (len >> 8) & 0xFF;
225 pktout.data[3] = len & 0xFF;
228 cipher->encrypt(pktout.data+4, biglen);
230 s_write(pktout.data, biglen+4);
233 static int ssh_versioncmp(char *a, char *b) {
235 unsigned long av, bv;
237 av = strtoul(a, &ae, 10);
238 bv = strtoul(b, &be, 10);
239 if (av != bv) return (av < bv ? -1 : +1);
240 if (*ae == '.') ae++;
241 if (*be == '.') be++;
242 av = strtoul(ae, &ae, 10);
243 bv = strtoul(be, &be, 10);
244 if (av != bv) return (av < bv ? -1 : +1);
248 static int do_ssh_init(void) {
256 while (s_read(&c, 1) == 1) {
257 if (c == 'S' && i < 2) i++;
258 else if (c == 'S' && i == 2) i = 2;
259 else if (c == 'H' && i == 2) break;
263 if (s_read(&c,1) != 1 || c != 'S') return 0;
264 if (s_read(&c,1) != 1 || c != 'S') return 0;
265 if (s_read(&c,1) != 1 || c != 'H') return 0;
267 if (s_read(&c,1) != 1 || c != '-') return 0;
270 if (s_read(&c,1) != 1)
276 } else if (i < sizeof(version)-1)
283 sprintf(vstring, "SSH-%s-PuTTY\n",
284 (ssh_versioncmp(version, "1.5") <= 0 ? version : "1.5"));
285 s_write(vstring, strlen(vstring));
289 static void ssh_protocol(unsigned char *in, int inlen, int ispkt) {
291 unsigned char session_id[16];
292 unsigned char *rsabuf, *keystr1, *keystr2;
293 unsigned char cookie[8];
294 struct RSAKey servkey, hostkey;
295 struct MD5Context md5c;
296 unsigned long supported_ciphers_mask;
299 extern struct ssh_cipher ssh_3des;
300 extern struct ssh_cipher ssh_des;
301 extern struct ssh_cipher ssh_blowfish;
310 if (pktin.type != SSH_SMSG_PUBLIC_KEY)
311 fatalbox("Public key packet not received");
313 memcpy(cookie, pktin.body, 8);
317 i = makekey(pktin.body+8, &servkey, &keystr1);
319 j = makekey(pktin.body+8+i, &hostkey, &keystr2);
321 supported_ciphers_mask = (pktin.body[12+i+j] << 24) |
322 (pktin.body[13+i+j] << 16) |
323 (pktin.body[14+i+j] << 8) |
324 (pktin.body[15+i+j]);
326 MD5Update(&md5c, keystr2, hostkey.bytes);
327 MD5Update(&md5c, keystr1, servkey.bytes);
328 MD5Update(&md5c, pktin.body, 8);
330 MD5Final(session_id, &md5c);
333 session_key[i] = random_byte();
335 len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes);
337 rsabuf = malloc(len);
339 fatalbox("Out of memory");
341 verify_ssh_host_key(savedhost, &hostkey);
343 for (i=0; i<32; i++) {
344 rsabuf[i] = session_key[i];
346 rsabuf[i] ^= session_id[i];
349 if (hostkey.bytes > servkey.bytes) {
350 rsaencrypt(rsabuf, 32, &servkey);
351 rsaencrypt(rsabuf, servkey.bytes, &hostkey);
353 rsaencrypt(rsabuf, 32, &hostkey);
354 rsaencrypt(rsabuf, hostkey.bytes, &servkey);
357 cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH :
358 cfg.cipher == CIPHER_DES ? SSH_CIPHER_DES :
360 if ((supported_ciphers_mask & (1 << cipher_type)) == 0) {
361 c_write("Selected cipher not supported, falling back to 3DES\r\n", 53);
362 cipher_type = SSH_CIPHER_3DES;
365 s_wrpkt_start(SSH_CMSG_SESSION_KEY, len+15);
366 pktout.body[0] = cipher_type;
367 memcpy(pktout.body+1, cookie, 8);
368 pktout.body[9] = (len*8) >> 8;
369 pktout.body[10] = (len*8) & 0xFF;
370 memcpy(pktout.body+11, rsabuf, len);
371 pktout.body[len+11] = pktout.body[len+12] = 0; /* protocol flags */
372 pktout.body[len+13] = pktout.body[len+14] = 0;
377 cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish :
378 cipher_type == SSH_CIPHER_DES ? &ssh_des :
380 cipher->sesskey(session_key);
382 do { crReturnV; } while (!ispkt);
384 if (pktin.type != SSH_SMSG_SUCCESS)
385 fatalbox("Encryption not successfully enabled");
389 static char username[100];
392 if (!*cfg.username) {
393 c_write("login as: ", 10);
395 do { crReturnV; } while (ispkt);
396 while (inlen--) switch (c = *in++) {
418 if (c >= ' ' && c <= '~' && pos < 40) {
426 username[strcspn(username, "\n\r")] = '\0';
429 strncpy(username, cfg.username, 99);
431 sprintf(stuff, "Sent username \"%s\".\r\n", username);
432 c_write(stuff, strlen(stuff));
434 s_wrpkt_start(SSH_CMSG_USER, 4+strlen(username));
435 pktout.body[0] = pktout.body[1] = pktout.body[2] = 0;
436 pktout.body[3] = strlen(username);
437 memcpy(pktout.body+4, username, strlen(username));
441 do { crReturnV; } while (!ispkt);
443 while (pktin.type == SSH_SMSG_FAILURE) {
444 static char password[100];
447 c_write("password: ", 10);
450 do { crReturnV; } while (ispkt);
451 while (inlen--) switch (c = *in++) {
468 if (c >= ' ' && c <= '~' && pos < 40)
474 s_wrpkt_start(SSH_CMSG_AUTH_PASSWORD, 4+strlen(password));
475 pktout.body[0] = pktout.body[1] = pktout.body[2] = 0;
476 pktout.body[3] = strlen(password);
477 memcpy(pktout.body+4, password, strlen(password));
479 memset(password, 0, strlen(password));
480 do { crReturnV; } while (!ispkt);
481 if (pktin.type == 15) {
482 c_write("Access denied\r\n", 15);
483 } else if (pktin.type != 14) {
484 fatalbox("Strange packet received, type %d", pktin.type);
489 i = strlen(cfg.termtype);
490 s_wrpkt_start(SSH_CMSG_REQUEST_PTY, i+5*4+1);
491 pktout.body[0] = (i >> 24) & 0xFF;
492 pktout.body[1] = (i >> 16) & 0xFF;
493 pktout.body[2] = (i >> 8) & 0xFF;
494 pktout.body[3] = i & 0xFF;
495 memcpy(pktout.body+4, cfg.termtype, i);
497 pktout.body[i++] = (rows >> 24) & 0xFF;
498 pktout.body[i++] = (rows >> 16) & 0xFF;
499 pktout.body[i++] = (rows >> 8) & 0xFF;
500 pktout.body[i++] = rows & 0xFF;
501 pktout.body[i++] = (cols >> 24) & 0xFF;
502 pktout.body[i++] = (cols >> 16) & 0xFF;
503 pktout.body[i++] = (cols >> 8) & 0xFF;
504 pktout.body[i++] = cols & 0xFF;
505 memset(pktout.body+i, 0, 9); /* 0 pixwidth, 0 pixheight, 0.b endofopt */
507 ssh_state = SSH_STATE_INTERMED;
508 do { crReturnV; } while (!ispkt);
509 if (pktin.type != SSH_MSG_SUCCESS && pktin.type != SSH_MSG_FAILURE) {
510 fatalbox("Protocol confusion");
511 } else if (pktin.type == SSH_MSG_FAILURE) {
512 c_write("Server refused to allocate pty\r\n", 32);
516 s_wrpkt_start(SSH_CMSG_EXEC_SHELL, 0);
519 ssh_state = SSH_STATE_SESSION;
526 if (pktin.type == SSH_SMSG_STDOUT_DATA ||
527 pktin.type == SSH_SMSG_STDERR_DATA) {
529 for (i = 0; i < 4; i++)
530 len = (len << 8) + pktin.body[i];
531 c_write(pktin.body+4, len);
532 } else if (pktin.type == SSH_MSG_DISCONNECT) {
533 ssh_state = SSH_STATE_CLOSED;
534 } else if (pktin.type == SSH_MSG_SUCCESS) {
535 /* may be from EXEC_SHELL on some servers */
536 } else if (pktin.type == SSH_MSG_FAILURE) {
537 /* may be from EXEC_SHELL on some servers
538 * if no pty is available or in other odd cases. Ignore */
539 } else if (pktin.type == SSH_SMSG_EXITSTATUS) {
540 s_wrpkt_start(SSH_CMSG_EXIT_CONFIRMATION, 0);
543 fatalbox("Strange packet received: type %d", pktin.type);
546 s_wrpkt_start(SSH_CMSG_STDIN_DATA, 4+inlen);
547 pktout.body[0] = (inlen >> 24) & 0xFF;
548 pktout.body[1] = (inlen >> 16) & 0xFF;
549 pktout.body[2] = (inlen >> 8) & 0xFF;
550 pktout.body[3] = inlen & 0xFF;
551 memcpy(pktout.body+4, in, inlen);
560 * Called to set up the connection. Will arrange for WM_NETEVENT
561 * messages to be passed to the specified window, whose window
562 * procedure should then call telnet_msg().
564 * Returns an error message, or NULL on success.
566 * Also places the canonical host name into `realhost'.
568 static char *ssh_init (HWND hwnd, char *host, int port, char **realhost) {
577 savedhost = malloc(1+strlen(host));
579 fatalbox("Out of memory");
580 strcpy(savedhost, host);
592 if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
593 if ( (h = gethostbyname(host)) == NULL)
594 switch (WSAGetLastError()) {
595 case WSAENETDOWN: return "Network is down";
596 case WSAHOST_NOT_FOUND: case WSANO_DATA:
597 return "Host does not exist";
598 case WSATRY_AGAIN: return "Host not found";
599 default: return "gethostbyname: unknown error";
601 memcpy (&a, h->h_addr, sizeof(a));
602 *realhost = h->h_name;
611 port = 22; /* default ssh port */
616 s = socket(AF_INET, SOCK_STREAM, 0);
617 if (s == INVALID_SOCKET)
618 switch (WSAGetLastError()) {
619 case WSAENETDOWN: return "Network is down";
620 case WSAEAFNOSUPPORT: return "TCP/IP support not present";
621 default: return "socket(): unknown error";
625 * Bind to local address.
627 addr.sin_family = AF_INET;
628 addr.sin_addr.s_addr = htonl(INADDR_ANY);
629 addr.sin_port = htons(0);
630 if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
631 switch (WSAGetLastError()) {
632 case WSAENETDOWN: return "Network is down";
633 default: return "bind(): unknown error";
637 * Connect to remote address.
639 addr.sin_addr.s_addr = htonl(a);
640 addr.sin_port = htons((short)port);
641 if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
642 switch (WSAGetLastError()) {
643 case WSAENETDOWN: return "Network is down";
644 case WSAECONNREFUSED: return "Connection refused";
645 case WSAENETUNREACH: return "Network is unreachable";
646 case WSAEHOSTUNREACH: return "No route to host";
647 default: return "connect(): unknown error";
651 send(s, "connect ", 8, 0);
652 send(s, FWhost, strlen(FWhost), 0);
655 sprintf(buf, " %d\n", FWport);
656 send (s, buf, strlen(buf), 0);
661 return "Protocol initialisation error";
663 if (WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | FD_CLOSE) == SOCKET_ERROR)
664 switch (WSAGetLastError()) {
665 case WSAENETDOWN: return "Network is down";
666 default: return "WSAAsyncSelect(): unknown error";
673 * Process a WM_NETEVENT message. Will return 0 if the connection
674 * has closed, or <0 for a socket error.
676 static int ssh_msg (WPARAM wParam, LPARAM lParam) {
680 if (s == INVALID_SOCKET) /* how the hell did we get here?! */
683 if (WSAGETSELECTERROR(lParam) != 0)
684 return -WSAGETSELECTERROR(lParam);
686 switch (WSAGETSELECTEVENT(lParam)) {
688 ret = recv(s, buf, sizeof(buf), 0);
689 if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
691 if (ret < 0) /* any _other_ error */
692 return -10000-WSAGetLastError();
695 return 0; /* can't happen, in theory */
697 ssh_gotdata (buf, ret);
701 ssh_state = SSH_STATE_CLOSED;
704 return 1; /* shouldn't happen, but WTF */
708 * Called to send data down the Telnet connection.
710 static void ssh_send (char *buf, int len) {
711 if (s == INVALID_SOCKET)
714 ssh_protocol(buf, len, 0);
718 * Called to set the size of the window from Telnet's POV.
720 static void ssh_size(void) {
722 case SSH_STATE_BEFORE_SIZE:
723 case SSH_STATE_CLOSED:
724 break; /* do nothing */
725 case SSH_STATE_INTERMED:
726 size_needed = TRUE; /* buffer for later */
728 case SSH_STATE_SESSION:
730 s_wrpkt_start(11, 16);
731 pktout.body[0] = (rows >> 24) & 0xFF;
732 pktout.body[1] = (rows >> 16) & 0xFF;
733 pktout.body[2] = (rows >> 8) & 0xFF;
734 pktout.body[3] = rows & 0xFF;
735 pktout.body[4] = (cols >> 24) & 0xFF;
736 pktout.body[5] = (cols >> 16) & 0xFF;
737 pktout.body[6] = (cols >> 8) & 0xFF;
738 pktout.body[7] = cols & 0xFF;
739 memset(pktout.body+8, 0, 8);
746 * (Send Telnet special codes)
748 static void ssh_special (Telnet_Special code) {
752 Backend ssh_backend = {