ssh1bpp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Binary packet protocol for SSH-1.
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "sshbpp.h"
  8. #include "sshcr.h"
  9. struct ssh1_bpp_state {
  10. int crState;
  11. long len, pad, biglen, length, maxlen;
  12. unsigned char *data;
  13. unsigned long realcrc, gotcrc;
  14. int chunk;
  15. PktIn *pktin;
  16. ssh1_cipher *cipher;
  17. struct crcda_ctx *crcda_ctx;
  18. int pending_compression_request;
  19. ssh_compressor *compctx;
  20. ssh_decompressor *decompctx;
  21. BinaryPacketProtocol bpp;
  22. };
  23. static void ssh1_bpp_free(BinaryPacketProtocol *bpp);
  24. static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp);
  25. static void ssh1_bpp_handle_output(BinaryPacketProtocol *bpp);
  26. static void ssh1_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  27. const char *msg, int category);
  28. static PktOut *ssh1_bpp_new_pktout(int type);
  29. static const struct BinaryPacketProtocolVtable ssh1_bpp_vtable = {
  30. ssh1_bpp_free,
  31. ssh1_bpp_handle_input,
  32. ssh1_bpp_handle_output,
  33. ssh1_bpp_new_pktout,
  34. ssh1_bpp_queue_disconnect,
  35. };
  36. BinaryPacketProtocol *ssh1_bpp_new(Frontend *frontend)
  37. {
  38. struct ssh1_bpp_state *s = snew(struct ssh1_bpp_state);
  39. memset(s, 0, sizeof(*s));
  40. s->bpp.vt = &ssh1_bpp_vtable;
  41. s->bpp.frontend = frontend;
  42. ssh_bpp_common_setup(&s->bpp);
  43. return &s->bpp;
  44. }
  45. static void ssh1_bpp_free(BinaryPacketProtocol *bpp)
  46. {
  47. struct ssh1_bpp_state *s = container_of(bpp, struct ssh1_bpp_state, bpp);
  48. if (s->cipher)
  49. ssh1_cipher_free(s->cipher);
  50. if (s->compctx)
  51. ssh_compressor_free(s->compctx);
  52. if (s->decompctx)
  53. ssh_decompressor_free(s->decompctx);
  54. if (s->crcda_ctx)
  55. crcda_free_context(s->crcda_ctx);
  56. sfree(s->pktin);
  57. sfree(s);
  58. }
  59. #define bpp_logevent(printf_args) \
  60. logevent_and_free(s->bpp.frontend, dupprintf printf_args)
  61. void ssh1_bpp_new_cipher(BinaryPacketProtocol *bpp,
  62. const struct ssh1_cipheralg *cipher,
  63. const void *session_key)
  64. {
  65. struct ssh1_bpp_state *s;
  66. assert(bpp->vt == &ssh1_bpp_vtable);
  67. s = container_of(bpp, struct ssh1_bpp_state, bpp);
  68. assert(!s->cipher);
  69. if (cipher) {
  70. s->cipher = ssh1_cipher_new(cipher);
  71. ssh1_cipher_sesskey(s->cipher, session_key);
  72. assert(!s->crcda_ctx);
  73. s->crcda_ctx = crcda_make_context();
  74. bpp_logevent(("Initialised %s encryption", cipher->text_name));
  75. }
  76. }
  77. #define BPP_READ(ptr, len) do \
  78. { \
  79. crMaybeWaitUntilV(s->bpp.input_eof || \
  80. bufchain_try_fetch_consume( \
  81. s->bpp.in_raw, ptr, len)); \
  82. if (s->bpp.input_eof) \
  83. goto eof; \
  84. } while (0)
  85. static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp)
  86. {
  87. struct ssh1_bpp_state *s = container_of(bpp, struct ssh1_bpp_state, bpp);
  88. crBegin(s->crState);
  89. while (1) {
  90. s->maxlen = 0;
  91. s->length = 0;
  92. {
  93. unsigned char lenbuf[4];
  94. BPP_READ(lenbuf, 4);
  95. s->len = toint(GET_32BIT_MSB_FIRST(lenbuf));
  96. }
  97. if (s->len < 0 || s->len > 262144) { /* SSH1.5-mandated max size */
  98. ssh_sw_abort(s->bpp.ssh,
  99. "Extremely large packet length from server suggests"
  100. " data stream corruption");
  101. crStopV;
  102. }
  103. s->pad = 8 - (s->len % 8);
  104. s->biglen = s->len + s->pad;
  105. s->length = s->len - 5;
  106. /*
  107. * Allocate the packet to return, now we know its length.
  108. */
  109. s->pktin = snew_plus(PktIn, s->biglen);
  110. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  111. s->pktin->qnode.on_free_queue = FALSE;
  112. s->pktin->type = 0;
  113. s->maxlen = s->biglen;
  114. s->data = snew_plus_get_aux(s->pktin);
  115. BPP_READ(s->data, s->biglen);
  116. if (s->cipher && detect_attack(s->crcda_ctx,
  117. s->data, s->biglen, NULL)) {
  118. ssh_sw_abort(s->bpp.ssh,
  119. "Network attack (CRC compensation) detected!");
  120. crStopV;
  121. }
  122. if (s->cipher)
  123. ssh1_cipher_decrypt(s->cipher, s->data, s->biglen);
  124. s->realcrc = crc32_compute(s->data, s->biglen - 4);
  125. s->gotcrc = GET_32BIT(s->data + s->biglen - 4);
  126. if (s->gotcrc != s->realcrc) {
  127. ssh_sw_abort(s->bpp.ssh, "Incorrect CRC received on packet");
  128. crStopV;
  129. }
  130. if (s->decompctx) {
  131. unsigned char *decompblk;
  132. int decomplen;
  133. if (!ssh_decompressor_decompress(
  134. s->decompctx, s->data + s->pad, s->length + 1,
  135. &decompblk, &decomplen)) {
  136. ssh_sw_abort(s->bpp.ssh,
  137. "Zlib decompression encountered invalid data");
  138. crStopV;
  139. }
  140. if (s->maxlen < s->pad + decomplen) {
  141. PktIn *old_pktin = s->pktin;
  142. s->maxlen = s->pad + decomplen;
  143. s->pktin = snew_plus(PktIn, s->maxlen);
  144. *s->pktin = *old_pktin; /* structure copy */
  145. s->data = snew_plus_get_aux(s->pktin);
  146. smemclr(old_pktin, s->biglen);
  147. sfree(old_pktin);
  148. }
  149. memcpy(s->data + s->pad, decompblk, decomplen);
  150. sfree(decompblk);
  151. s->length = decomplen - 1;
  152. }
  153. /*
  154. * Now we can find the bounds of the semantic content of the
  155. * packet, and the initial type byte.
  156. */
  157. s->data += s->pad;
  158. s->pktin->type = *s->data++;
  159. BinarySource_INIT(s->pktin, s->data, s->length);
  160. if (s->bpp.logctx) {
  161. logblank_t blanks[MAX_BLANKS];
  162. int nblanks = ssh1_censor_packet(
  163. s->bpp.pls, s->pktin->type, FALSE,
  164. make_ptrlen(s->data, s->length), blanks);
  165. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  166. ssh1_pkt_type(s->pktin->type),
  167. get_ptr(s->pktin), get_avail(s->pktin), nblanks, blanks,
  168. NULL, 0, NULL);
  169. }
  170. pq_push(&s->bpp.in_pq, s->pktin);
  171. {
  172. int type = s->pktin->type;
  173. s->pktin = NULL;
  174. switch (type) {
  175. case SSH1_SMSG_SUCCESS:
  176. case SSH1_SMSG_FAILURE:
  177. if (s->pending_compression_request) {
  178. /*
  179. * This is the response to
  180. * SSH1_CMSG_REQUEST_COMPRESSION.
  181. */
  182. if (type == SSH1_SMSG_SUCCESS) {
  183. /*
  184. * If the response was positive, start
  185. * compression.
  186. */
  187. assert(!s->compctx);
  188. assert(!s->decompctx);
  189. s->compctx = ssh_compressor_new(&ssh_zlib);
  190. s->decompctx = ssh_decompressor_new(&ssh_zlib);
  191. bpp_logevent(("Started zlib (RFC1950) compression"));
  192. }
  193. /*
  194. * Either way, cancel the pending flag, and
  195. * schedule a run of our output side in case we
  196. * had any packets queued up in the meantime.
  197. */
  198. s->pending_compression_request = FALSE;
  199. queue_idempotent_callback(&s->bpp.ic_out_pq);
  200. }
  201. break;
  202. }
  203. }
  204. }
  205. eof:
  206. if (!s->bpp.expect_close) {
  207. ssh_remote_error(s->bpp.ssh,
  208. "Server unexpectedly closed network connection");
  209. } else {
  210. ssh_remote_eof(s->bpp.ssh, "Server closed network connection");
  211. }
  212. return; /* avoid touching s now it's been freed */
  213. crFinishV;
  214. }
  215. static PktOut *ssh1_bpp_new_pktout(int pkt_type)
  216. {
  217. PktOut *pkt = ssh_new_packet();
  218. pkt->length = 4 + 8; /* space for length + max padding */
  219. put_byte(pkt, pkt_type);
  220. pkt->prefix = pkt->length;
  221. pkt->type = pkt_type;
  222. pkt->downstream_id = 0;
  223. pkt->additional_log_text = NULL;
  224. return pkt;
  225. }
  226. static void ssh1_bpp_format_packet(struct ssh1_bpp_state *s, PktOut *pkt)
  227. {
  228. int pad, biglen, i, pktoffs;
  229. unsigned long crc;
  230. int len;
  231. if (s->bpp.logctx) {
  232. ptrlen pktdata = make_ptrlen(pkt->data + pkt->prefix,
  233. pkt->length - pkt->prefix);
  234. logblank_t blanks[MAX_BLANKS];
  235. int nblanks = ssh1_censor_packet(
  236. s->bpp.pls, pkt->type, TRUE, pktdata, blanks);
  237. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  238. ssh1_pkt_type(pkt->type),
  239. pktdata.ptr, pktdata.len, nblanks, blanks,
  240. NULL, 0, NULL);
  241. }
  242. if (s->compctx) {
  243. unsigned char *compblk;
  244. int complen;
  245. ssh_compressor_compress(s->compctx, pkt->data + 12, pkt->length - 12,
  246. &compblk, &complen, 0);
  247. /* Replace the uncompressed packet data with the compressed
  248. * version. */
  249. pkt->length = 12;
  250. put_data(pkt, compblk, complen);
  251. sfree(compblk);
  252. }
  253. put_uint32(pkt, 0); /* space for CRC */
  254. len = pkt->length - 4 - 8; /* len(type+data+CRC) */
  255. pad = 8 - (len % 8);
  256. pktoffs = 8 - pad;
  257. biglen = len + pad; /* len(padding+type+data+CRC) */
  258. for (i = pktoffs; i < 4+8; i++)
  259. pkt->data[i] = random_byte();
  260. crc = crc32_compute(pkt->data + pktoffs + 4,
  261. biglen - 4); /* all ex len */
  262. PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc);
  263. PUT_32BIT(pkt->data + pktoffs, len);
  264. if (s->cipher)
  265. ssh1_cipher_encrypt(s->cipher, pkt->data + pktoffs + 4, biglen);
  266. bufchain_add(s->bpp.out_raw, pkt->data + pktoffs,
  267. biglen + 4); /* len(length+padding+type+data+CRC) */
  268. }
  269. static void ssh1_bpp_handle_output(BinaryPacketProtocol *bpp)
  270. {
  271. struct ssh1_bpp_state *s = container_of(bpp, struct ssh1_bpp_state, bpp);
  272. PktOut *pkt;
  273. if (s->pending_compression_request) {
  274. /*
  275. * Don't send any output packets while we're awaiting a
  276. * response to SSH1_CMSG_REQUEST_COMPRESSION, because if they
  277. * cross over in transit with the responding SSH1_CMSG_SUCCESS
  278. * then the other end could decode them with the wrong
  279. * compression settings.
  280. */
  281. return;
  282. }
  283. while ((pkt = pq_pop(&s->bpp.out_pq)) != NULL) {
  284. int type = pkt->type;
  285. ssh1_bpp_format_packet(s, pkt);
  286. ssh_free_pktout(pkt);
  287. if (type == SSH1_CMSG_REQUEST_COMPRESSION) {
  288. /*
  289. * When we see the actual compression request go past, set
  290. * the pending flag, and stop processing packets this
  291. * time.
  292. */
  293. s->pending_compression_request = TRUE;
  294. break;
  295. }
  296. }
  297. }
  298. static void ssh1_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  299. const char *msg, int category)
  300. {
  301. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH1_MSG_DISCONNECT);
  302. put_stringz(pkt, msg);
  303. pq_push(&bpp->out_pq, pkt);
  304. }