ssh1bpp.c 11 KB

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