ssh1bpp.c 12 KB

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