ssh1bpp.c 12 KB

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